1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #ifndef GEO
#define GEO
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <stdint.h>
//[comment]
// Implementation of a generic vector class - it will be used to deal with 3D points, vectors and normals.
// The class is implemented as a template. While it may complicate the code a bit, it gives us
// the flexibility later, to specialize the type of the coordinates into anything we want.
// For example: Vec3f if we want the coordinates to be floats or Vec3i if we want the coordinates to be integers.
//
// Vec3 is a standard/common way of naming vectors, points, etc. The OpenEXR and Autodesk libraries
// use this convention for instance.
//[/comment]
template<typename T>
class Vec3
{
public:
Vec3() : x(T(0)), y(T(0)), z(T(0)) {}
Vec3(T xx) : x(xx), y(xx), z(xx) {}
Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
Vec3 operator + (const Vec3 &v) const
{ return Vec3(x + v.x, y + v.y, z + v.z); }
Vec3 operator - (const Vec3 &v) const
{ return Vec3(x - v.x, y - v.y, z - v.z); }
Vec3 operator - () const
{ return Vec3(-x, -y, -z); }
Vec3 operator * (const T &r) const
{ return Vec3(x * r, y * r, z * r); }
Vec3 operator * (const Vec3 &v) const
{ return Vec3(x * v.x, y * v.y, z * v.z); }
T dotProduct(const Vec3<T> &v) const
{ return x * v.x + y * v.y + z * v.z; }
Vec3& operator /= (const T &r)
{ x /= r, y /= r, z /= r; return *this; }
Vec3& operator *= (const T &r)
{ x *= r, y *= r, z *= r; return *this; }
Vec3 crossProduct(const Vec3<T> &v) const
{ return Vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); }
T norm() const
{ return x * x + y * y + z * z; }
T length() const
{ return sqrt(norm()); }
// fonction non implémentée pour la classe mere du coup
/*bool operator < (const Vec3<T> &v) const
{
if (x != v.x)
return (x < v.x);
if (y != v.y)
return (y < v.y);
return (z < v.z);
}*/
//[comment]
// The next two operators are sometimes called access operators or
// accessors. The Vec coordinates can be accessed that way v[0], v[1], v[2],
// rather than using the more traditional form v.x, v.y, v.z. This useful
// when vectors are used in loops: the coordinates can be accessed with the
// loop index (e.g. v[i]).
//[/comment]
const T& operator [] (uint8_t i) const { return (&x)[i]; }
T& operator [] (uint8_t i) { return (&x)[i]; }
Vec3& normalize()
{
T n = norm();
if (n > 0) {
T factor = 1 / sqrt(n);
x *= factor, y *= factor, z *= factor;
}
return *this;
}
friend Vec3 operator * (const T &r, const Vec3 &v)
{ return Vec3<T>(v.x * r, v.y * r, v.z * r); }
friend Vec3 operator / (const T &r, const Vec3 &v)
{ return Vec3<T>(r / v.x, r / v.y, r / v.z); }
friend std::ostream& operator << (std::ostream &s, const Vec3<T> &v)
{
return s << '[' << v.x << ' ' << v.y << ' ' << v.z << ']';
}
T x, y, z;
};
template<typename T>
T max3Values(T const& a, T const& b, T const& c){
return std::max(a,std::max(b,c));
}
template<typename T>
T min3Values(T const& a, T const& b, T const& c){
return std::min(a,std::min(b,c));
}
//[comment]
// Now you can specialize the class. We are just showing two examples here. In your code
// you can declare a vector either that way: Vec3<float> a, or that way: Vec3f a
//[/comment]
typedef Vec3<float> Vec3f;
typedef Vec3<float> Point3D;
typedef Vec3<float> Normal;
typedef Vec3<int> Vec3i;
typedef Vec3<int> indPointsTriangle;
class Point3D_X_highter : public Point3D
{
template<typename T>
bool operator < (const Vec3<T> &v) const
{
if (x != v.x)
return (x < v.x);
if (y != v.y)
return (y < v.y);
return (z < v.z);
}
};
class Point3D_Y_highter: public Point3D
{
template<typename T>
bool operator < (const Vec3<T> &v) const
{
if (y != v.y)
return (y < v.y);
if (x != v.x)
return (x < v.x);
return (z < v.z);
}
};
class Point3D_Z_highter: public Point3D
{
template<typename T>
bool operator < (const Vec3<T> &v) const
{
if (z != v.z)
return (z < v.z);
if (x != v.x)
return (x < v.x);
return (y < v.y);
}
};
#endif |
Partager