| 1 | /*! | 
|---|
| 2 |     \file vector.h | 
|---|
| 3 |     \brief A basic 3D math framework | 
|---|
| 4 |  | 
|---|
| 5 |     Contains classes to handle vectors, lines, rotations and planes | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef _VECTOR_H | 
|---|
| 9 | #define _VECTOR_H | 
|---|
| 10 |  | 
|---|
| 11 | #include <math.h> | 
|---|
| 12 | #include "compiler.h" | 
|---|
| 13 | #include "abstract_model.h" | 
|---|
| 14 | //! PI the circle-constant | 
|---|
| 15 | #define PI 3.14159265359f | 
|---|
| 16 |  | 
|---|
| 17 | //! 3D Vector | 
|---|
| 18 | /** | 
|---|
| 19 |         Class to handle 3D Vectors | 
|---|
| 20 | */ | 
|---|
| 21 | class Vector { | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 |  public: | 
|---|
| 25 |   Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor | 
|---|
| 26 |   Vector () : x(0), y(0), z(0) {} | 
|---|
| 27 |   ~Vector () {} | 
|---|
| 28 |  | 
|---|
| 29 |   /** \param index The index of the "array" \returns the x/y/z coordinate */ | 
|---|
| 30 |   inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z; } | 
|---|
| 31 |   /**  \param v The vector to add \returns the addition between two vectors (this + v) */ | 
|---|
| 32 |   inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }; | 
|---|
| 33 |   /**  \param v The vector to add \returns the addition between two vectors (this + v) */ | 
|---|
| 34 |   inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); }; | 
|---|
| 35 |   /** \param v The vector to add  \returns the addition between two vectors (this += v) */ | 
|---|
| 36 |   inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; }; | 
|---|
| 37 |   /** \param v The vector to substract  \returns the substraction between two vectors (this - v) */ | 
|---|
| 38 |   inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; }; | 
|---|
| 39 |   /** \param v The vector to substract  \returns the substraction between two vectors (this - v) */ | 
|---|
| 40 |   inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } | 
|---|
| 41 |   /** \param v The vector to substract  \returns the substraction between two vectors (this - v) */ | 
|---|
| 42 |   inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); } | 
|---|
| 43 |   /** \param v The vector to substract  \returns the substraction between two vectors (this -= v) */ | 
|---|
| 44 |   inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; }; | 
|---|
| 45 |   /** \param v The vector to substract  \returns the substraction between two vectors (this -= v) */ | 
|---|
| 46 |   inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; }; | 
|---|
| 47 |   /** \param v the second vector  \returns The dotProduct between two vector (this (dot) v) */ | 
|---|
| 48 |   inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; }; | 
|---|
| 49 |   /** \todo strange */ | 
|---|
| 50 |   inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; }; | 
|---|
| 51 |   /** \param f a factor to multiply the vector with \returns the vector multiplied by f (this * f) */ | 
|---|
| 52 |   inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); }; | 
|---|
| 53 |   /** \param f a factor to multiply the vector with \returns the vector multiplied by f (this *= f) */ | 
|---|
| 54 |   inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; }; | 
|---|
| 55 |   /** \param f a factor to divide the vector with \returns the vector divided by f (this / f) */ | 
|---|
| 56 |   inline Vector operator/ (float f) const {if (unlikely(f == 0.0)) return Vector(0,0,0); else return Vector(this->x / f, this->y / f, this->z / f); }; | 
|---|
| 57 |   /** \param f a factor to divide the vector with \returns the vector divided by f (this /= f) */ | 
|---|
| 58 |   inline const Vector& operator/= (float f) {if (unlikely(f == 0.0)) {this->x=0;this->y=0;this->z=0;} else {this->x /= f; this->y /= f; this->z /= f;} return *this; }; | 
|---|
| 59 |   /** \brief copy constructor \todo (i do not know it this is faster) \param v the vector to assign to this vector. \returns the vector v */ | 
|---|
| 60 |   inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; }; | 
|---|
| 61 |   /** \brief copy constructor  \param v the sVec3D to assign to this vector. \returns the vector v */ | 
|---|
| 62 |   inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; } | 
|---|
| 63 |   /** \param v: the other vector \return the dot product of the vectors */ | 
|---|
| 64 |   float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; }; | 
|---|
| 65 |   /** \param v: the corss-product partner \returns the cross-product between this and v (this (x) v) */ | 
|---|
| 66 |   inline Vector cross (const Vector& v) const { return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x ); } | 
|---|
| 67 |   /** \brief scales the this vector with v  \param v the vector to scale this with */ | 
|---|
| 68 |   void scale(const Vector& v) {   x *= v.x;  y *= v.y; z *= v.z; }; | 
|---|
| 69 |   /** \returns the length of the vector */ | 
|---|
| 70 |   inline float len() const { return sqrt (x*x+y*y+z*z); } | 
|---|
| 71 |   /** \brief normalizes the vector */ | 
|---|
| 72 |   inline void normalize() { | 
|---|
| 73 |                       float l = len(); | 
|---|
| 74 |                       if( unlikely(l == 0.0)) | 
|---|
| 75 |                         { | 
|---|
| 76 |                           // Prevent divide by zero | 
|---|
| 77 |                           return; | 
|---|
| 78 |                         } | 
|---|
| 79 |                       x = x / l; | 
|---|
| 80 |                       y = y / l; | 
|---|
| 81 |                       z = z / l; | 
|---|
| 82 |                     } | 
|---|
| 83 |   Vector getNormalized() const; | 
|---|
| 84 |   Vector abs(); | 
|---|
| 85 |  | 
|---|
| 86 |   void debug() const; | 
|---|
| 87 |  | 
|---|
| 88 |  public: | 
|---|
| 89 |   float    x;     //!< The x Coordinate of the Vector. | 
|---|
| 90 |   float    y;     //!< The y Coordinate of the Vector. | 
|---|
| 91 |   float    z;     //!< The z Coordinate of the Vector. | 
|---|
| 92 | }; | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 |    \brief calculate the angle between two vectors in radiances | 
|---|
| 96 |    \param v1: a vector | 
|---|
| 97 |    \param v2: another vector | 
|---|
| 98 |    \return the angle between the vectors in radians | 
|---|
| 99 | */ | 
|---|
| 100 | inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); }; | 
|---|
| 101 | /** | 
|---|
| 102 |    \brief calculate the angle between two vectors in degrees | 
|---|
| 103 |    \param v1: a vector | 
|---|
| 104 |    \param v2: another vector | 
|---|
| 105 |    \return the angle between the vectors in degrees | 
|---|
| 106 | */ | 
|---|
| 107 | inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; }; | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | //! Quaternion | 
|---|
| 111 | /** | 
|---|
| 112 |    Class to handle 3-dimensional rotation efficiently | 
|---|
| 113 | */ | 
|---|
| 114 | class Quaternion | 
|---|
| 115 | { | 
|---|
| 116 |  public: | 
|---|
| 117 |   /** \brief creates a Default quaternion (multiplicational identity Quaternion)*/ | 
|---|
| 118 |   inline Quaternion () { w = 1; v = Vector(0,0,0); } | 
|---|
| 119 |   /** \brief creates a Quaternion looking into the direction v \param v: the direction \param f: the value */ | 
|---|
| 120 |   inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; } | 
|---|
| 121 |   Quaternion (float m[4][4]); | 
|---|
| 122 |   /** \brief turns a rotation along an axis into a Quaternion \param angle: the amount of radians to rotate \param axis: the axis to rotate around */ | 
|---|
| 123 |   inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); } | 
|---|
| 124 |   Quaternion (const Vector& dir, const Vector& up); | 
|---|
| 125 |   Quaternion (float roll, float pitch, float yaw); | 
|---|
| 126 |   Quaternion operator/ (const float& f) const; | 
|---|
| 127 |   /** \param f: the value to divide by \returns the quaternion devided by f (this /= f) */ | 
|---|
| 128 |   inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;} | 
|---|
| 129 |   Quaternion operator* (const float& f) const; | 
|---|
| 130 |   /** \param f: the value to multiply by \returns the quaternion multiplied by f (this *= f) */ | 
|---|
| 131 |   inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;} | 
|---|
| 132 |   Quaternion operator* (const Quaternion& q) const; | 
|---|
| 133 |   /** \param q: the Quaternion to multiply by \returns the quaternion multiplied by q (this *= q) */ | 
|---|
| 134 |   inline const Quaternion operator*= (const Quaternion& q) {*this = *this * q; return *this; }; | 
|---|
| 135 |   /** \param q the Quaternion to add to this \returns the quaternion added with q (this + q) */ | 
|---|
| 136 |   inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); }; | 
|---|
| 137 |   /** \param q the Quaternion to add to this \returns the quaternion added with q (this += q) */ | 
|---|
| 138 |   inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; }; | 
|---|
| 139 |   /** \param q the Quaternion to substrace from this \returns the quaternion substracted by q (this - q) */ | 
|---|
| 140 |   inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); } | 
|---|
| 141 |   /** \param q the Quaternion to substrace from this \returns the quaternion substracted by q (this -= q) */ | 
|---|
| 142 |   inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; }; | 
|---|
| 143 |   /** \brief copy constructor \param q: the Quaternion to set this to. \returns the Quaternion q (or this) */ | 
|---|
| 144 |   inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;} | 
|---|
| 145 |   /** \brief conjugates this Quaternion \returns the conjugate */ | 
|---|
| 146 |   inline Quaternion conjugate () const {  Quaternion r(*this);  r.v = Vector() - r.v;  return r;} | 
|---|
| 147 |   Quaternion inverse () const; | 
|---|
| 148 |   Vector apply (const Vector& f) const; | 
|---|
| 149 |   float norm () const; | 
|---|
| 150 |   void matrix (float m[4][4]) const; | 
|---|
| 151 |  | 
|---|
| 152 |   void debug(); | 
|---|
| 153 |  | 
|---|
| 154 |  public: | 
|---|
| 155 |   Vector    v;        //!< Imaginary Vector | 
|---|
| 156 |   float     w;        //!< Real part of the number | 
|---|
| 157 |  | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 | Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t); | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | //! 3D rotation (OBSOLETE) | 
|---|
| 165 | /** | 
|---|
| 166 |   Class to handle 3-dimensional rotations. | 
|---|
| 167 |   Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix | 
|---|
| 168 | */ | 
|---|
| 169 | class Rotation { | 
|---|
| 170 |   public: | 
|---|
| 171 |  | 
|---|
| 172 |   float m[9]; //!< 3x3 Rotation Matrix | 
|---|
| 173 |  | 
|---|
| 174 |   Rotation ( const Vector& v); | 
|---|
| 175 |   Rotation ( const Vector& axis, float angle); | 
|---|
| 176 |   Rotation ( float pitch, float yaw, float roll); | 
|---|
| 177 |   Rotation (); | 
|---|
| 178 |   ~Rotation () {} | 
|---|
| 179 |  | 
|---|
| 180 |   Rotation operator* (const Rotation& r); | 
|---|
| 181 |  | 
|---|
| 182 |   void glmatrix (float* buffer); | 
|---|
| 183 | }; | 
|---|
| 184 |  | 
|---|
| 185 | //!< Apply a rotation to a vector | 
|---|
| 186 | Vector rotateVector( const Vector& v, const Rotation& r); | 
|---|
| 187 |  | 
|---|
| 188 | //! 3D line | 
|---|
| 189 | /** | 
|---|
| 190 |   Class to store Lines in 3-dimensional space | 
|---|
| 191 |  | 
|---|
| 192 |   Supports line-to-line distance measurements and rotation | 
|---|
| 193 | */ | 
|---|
| 194 | class Line | 
|---|
| 195 | { | 
|---|
| 196 |   public: | 
|---|
| 197 |  | 
|---|
| 198 |   Vector r;   //!< Offset | 
|---|
| 199 |   Vector a;   //!< Direction | 
|---|
| 200 |  | 
|---|
| 201 |   Line ( Vector r, Vector a) : r(r), a(a) {}  //!< assignment constructor | 
|---|
| 202 |   Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} | 
|---|
| 203 |   ~Line () {} | 
|---|
| 204 |  | 
|---|
| 205 |   float distance (const Line& l) const; | 
|---|
| 206 |   float distancePoint (const Vector& v) const; | 
|---|
| 207 |   float distancePoint (const sVec3D& v) const; | 
|---|
| 208 |   Vector* footpoints (const Line& l) const; | 
|---|
| 209 |   float len () const; | 
|---|
| 210 |  | 
|---|
| 211 |   void rotate(const Rotation& rot); | 
|---|
| 212 | }; | 
|---|
| 213 |  | 
|---|
| 214 | //! 3D plane | 
|---|
| 215 | /** | 
|---|
| 216 |   Class to handle planes in 3-dimensional space | 
|---|
| 217 |  | 
|---|
| 218 |   Critical for polygon-based collision detection | 
|---|
| 219 | */ | 
|---|
| 220 | class Plane | 
|---|
| 221 | { | 
|---|
| 222 |   public: | 
|---|
| 223 |  | 
|---|
| 224 |   Vector n;   //!< Normal vector | 
|---|
| 225 |   float k;    //!< Offset constant | 
|---|
| 226 |  | 
|---|
| 227 |   Plane (Vector a, Vector b, Vector c); | 
|---|
| 228 |   Plane (Vector norm, Vector p); | 
|---|
| 229 |   Plane (Vector norm, sVec3D p); | 
|---|
| 230 |   Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor | 
|---|
| 231 |   Plane () : n(Vector(1,1,1)), k(0) {} | 
|---|
| 232 |   ~Plane () {} | 
|---|
| 233 |  | 
|---|
| 234 |   Vector intersectLine (const Line& l) const; | 
|---|
| 235 |   float distancePoint (const Vector& p) const; | 
|---|
| 236 |   float distancePoint (const sVec3D& p) const; | 
|---|
| 237 |   float locatePoint (const Vector& p) const; | 
|---|
| 238 | }; | 
|---|
| 239 |  | 
|---|
| 240 | #endif /* _VECTOR_H */ | 
|---|