[2043] | 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 | |
---|
[3224] | 8 | #ifndef _VECTOR_H |
---|
| 9 | #define _VECTOR_H |
---|
[2043] | 10 | |
---|
| 11 | #include <math.h> |
---|
[3860] | 12 | #include "compiler.h" |
---|
[3449] | 13 | //! PI the circle-constant |
---|
[2043] | 14 | #define PI 3.14159265359f |
---|
| 15 | |
---|
[2190] | 16 | //! 3D Vector |
---|
[2043] | 17 | /** |
---|
[2190] | 18 | Class to handle 3D Vectors |
---|
[2043] | 19 | */ |
---|
| 20 | class Vector { |
---|
| 21 | |
---|
| 22 | |
---|
[4476] | 23 | public: |
---|
[2043] | 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 | |
---|
[4476] | 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) */ |
---|
[3819] | 33 | inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } |
---|
[4476] | 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) */ |
---|
[3966] | 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 ); } |
---|
[4477] | 54 | /** \brief scales the this vector with v \param v the vector to scale this with */ |
---|
[4476] | 55 | void scale(const Vector& v) { x *= v.x; y *= v.y; z *= v.z; }; |
---|
| 56 | /** \returns the length of the vector */ |
---|
[3819] | 57 | inline float len() const { return sqrt (x*x+y*y+z*z); } |
---|
[4476] | 58 | /** \brief normalizes the vector */ |
---|
| 59 | inline void normalize() { |
---|
[3819] | 60 | float l = len(); |
---|
[3860] | 61 | if( unlikely(l == 0.0)) |
---|
[3819] | 62 | { |
---|
| 63 | // Prevent divide by zero |
---|
| 64 | return; |
---|
| 65 | } |
---|
| 66 | x = x / l; |
---|
| 67 | y = y / l; |
---|
| 68 | z = z / l; |
---|
| 69 | } |
---|
[4372] | 70 | Vector getNormalized() const; |
---|
[2551] | 71 | Vector abs(); |
---|
[3541] | 72 | |
---|
[3966] | 73 | void debug() const; |
---|
[4476] | 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. |
---|
[2043] | 79 | }; |
---|
| 80 | |
---|
[4476] | 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; }; |
---|
[2043] | 95 | |
---|
[4476] | 96 | |
---|
[2190] | 97 | //! Quaternion |
---|
[2043] | 98 | /** |
---|
[4476] | 99 | Class to handle 3-dimensional rotation efficiently |
---|
[2190] | 100 | */ |
---|
| 101 | class Quaternion |
---|
| 102 | { |
---|
| 103 | public: |
---|
[4477] | 104 | /** \brief creates a Default quaternion (multiplicational identity Quaternion)*/ |
---|
[3822] | 105 | inline Quaternion () { w = 1; v = Vector(0,0,0); } |
---|
[4477] | 106 | /** \brief creates a Quaternion looking into the direction v \param v: the direction \param f: the value */ |
---|
[3971] | 107 | inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; } |
---|
[3541] | 108 | Quaternion (float m[4][4]); |
---|
[4477] | 109 | /** \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 */ |
---|
[3822] | 110 | inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); } |
---|
[3541] | 111 | Quaternion (const Vector& dir, const Vector& up); |
---|
| 112 | Quaternion (float roll, float pitch, float yaw); |
---|
| 113 | Quaternion operator/ (const float& f) const; |
---|
[4477] | 114 | /** \param f: the value to divide by \returns the quaternion devided by f (this /= f) */ |
---|
| 115 | inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;} |
---|
[3541] | 116 | Quaternion operator* (const float& f) const; |
---|
[4477] | 117 | /** \param f: the value to multiply by \returns the quaternion multiplied by f (this *= f) */ |
---|
| 118 | inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;} |
---|
[3541] | 119 | Quaternion operator* (const Quaternion& q) const; |
---|
[4477] | 120 | /** \param q: the Quaternion to multiply by \returns the quaternion multiplied by q (this *= q) */ |
---|
| 121 | inline const Quaternion operator*= (const Quaternion& q) {*this = *this * q; return *this; }; |
---|
| 122 | /** \param q the Quaternion to add to this \returns the quaternion added with q (this + q) */ |
---|
| 123 | inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); }; |
---|
| 124 | /** \param q the Quaternion to add to this \returns the quaternion added with q (this += q) */ |
---|
| 125 | inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; }; |
---|
| 126 | /** \param q the Quaternion to substrace from this \returns the quaternion substracted by q (this - q) */ |
---|
[3822] | 127 | inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); } |
---|
[4477] | 128 | /** \param q the Quaternion to substrace from this \returns the quaternion substracted by q (this -= q) */ |
---|
| 129 | inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; }; |
---|
| 130 | /** \brief copy constructor \param q: the Quaternion to set this to. \returns the Quaternion q (or this) */ |
---|
[3966] | 131 | inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;} |
---|
[4477] | 132 | /** \brief conjugates this Quaternion \returns the conjugate */ |
---|
| 133 | inline Quaternion conjugate () const { Quaternion r(*this); r.v = Vector() - r.v; return r;} |
---|
[3541] | 134 | Quaternion inverse () const; |
---|
[4372] | 135 | Vector apply (const Vector& f) const; |
---|
[3541] | 136 | float norm () const; |
---|
| 137 | void matrix (float m[4][4]) const; |
---|
| 138 | |
---|
| 139 | void debug(); |
---|
[4477] | 140 | |
---|
| 141 | public: |
---|
| 142 | Vector v; //!< Imaginary Vector |
---|
| 143 | float w; //!< Real part of the number |
---|
| 144 | |
---|
[2190] | 145 | }; |
---|
| 146 | |
---|
[3971] | 147 | Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t); |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|
[2190] | 151 | //! 3D rotation (OBSOLETE) |
---|
| 152 | /** |
---|
[2043] | 153 | Class to handle 3-dimensional rotations. |
---|
| 154 | Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix |
---|
| 155 | */ |
---|
| 156 | class Rotation { |
---|
| 157 | public: |
---|
| 158 | |
---|
| 159 | float m[9]; //!< 3x3 Rotation Matrix |
---|
| 160 | |
---|
| 161 | Rotation ( const Vector& v); |
---|
| 162 | Rotation ( const Vector& axis, float angle); |
---|
| 163 | Rotation ( float pitch, float yaw, float roll); |
---|
| 164 | Rotation (); |
---|
| 165 | ~Rotation () {} |
---|
| 166 | |
---|
[2190] | 167 | Rotation operator* (const Rotation& r); |
---|
| 168 | |
---|
| 169 | void glmatrix (float* buffer); |
---|
[2043] | 170 | }; |
---|
[2551] | 171 | |
---|
[2043] | 172 | //!< Apply a rotation to a vector |
---|
[3228] | 173 | Vector rotateVector( const Vector& v, const Rotation& r); |
---|
[2043] | 174 | |
---|
| 175 | //! 3D line |
---|
| 176 | /** |
---|
| 177 | Class to store Lines in 3-dimensional space |
---|
| 178 | |
---|
| 179 | Supports line-to-line distance measurements and rotation |
---|
| 180 | */ |
---|
| 181 | class Line |
---|
| 182 | { |
---|
| 183 | public: |
---|
| 184 | |
---|
| 185 | Vector r; //!< Offset |
---|
| 186 | Vector a; //!< Direction |
---|
| 187 | |
---|
| 188 | Line ( Vector r, Vector a) : r(r), a(a) {} //!< assignment constructor |
---|
| 189 | Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} |
---|
| 190 | ~Line () {} |
---|
| 191 | |
---|
| 192 | float distance (const Line& l) const; |
---|
[3228] | 193 | float distancePoint (const Vector& v) const; |
---|
[2043] | 194 | Vector* footpoints (const Line& l) const; |
---|
| 195 | float len () const; |
---|
| 196 | |
---|
| 197 | void rotate(const Rotation& rot); |
---|
| 198 | }; |
---|
| 199 | |
---|
| 200 | //! 3D plane |
---|
| 201 | /** |
---|
| 202 | Class to handle planes in 3-dimensional space |
---|
| 203 | |
---|
| 204 | Critical for polygon-based collision detection |
---|
| 205 | */ |
---|
| 206 | class Plane |
---|
| 207 | { |
---|
| 208 | public: |
---|
| 209 | |
---|
| 210 | Vector n; //!< Normal vector |
---|
| 211 | float k; //!< Offset constant |
---|
| 212 | |
---|
| 213 | Plane (Vector a, Vector b, Vector c); |
---|
| 214 | Plane (Vector norm, Vector p); |
---|
| 215 | Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor |
---|
| 216 | Plane () : n(Vector(1,1,1)), k(0) {} |
---|
| 217 | ~Plane () {} |
---|
| 218 | |
---|
[3228] | 219 | Vector intersectLine (const Line& l) const; |
---|
| 220 | float distancePoint (const Vector& p) const; |
---|
| 221 | float locatePoint (const Vector& p) const; |
---|
[2043] | 222 | }; |
---|
| 223 | |
---|
[3224] | 224 | #endif /* _VECTOR_H */ |
---|