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