| 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 | public: | 
|---|
| 23 |  | 
|---|
| 24 | float x; //!< The x Coordinate of the Vector. | 
|---|
| 25 | float y; //!< The y Coordinate of the Vector. | 
|---|
| 26 | float z; //!< The z Coordinate of the Vector. | 
|---|
| 27 |  | 
|---|
| 28 | Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor | 
|---|
| 29 | Vector () : x(0), y(0), z(0) {} | 
|---|
| 30 | ~Vector () {} | 
|---|
| 31 |  | 
|---|
| 32 | inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); } | 
|---|
| 33 | inline const Vector operator+= (const Vector& v) {this->x += v.x; this->y += v.y; this->z += v.z; return *this;} | 
|---|
| 34 | inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } | 
|---|
| 35 | inline const Vector operator-= (const Vector& v) {this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this;} | 
|---|
| 36 | inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; } | 
|---|
| 37 | inline const Vector operator*= (const Vector& v) {this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this;} | 
|---|
| 38 | inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); } | 
|---|
| 39 | inline const Vector operator*= (float f) {this->x *= f; this->y *= f; this->z *= f; return *this;} | 
|---|
| 40 | Vector operator/ (float f) const; | 
|---|
| 41 | inline const Vector operator/= (float f) {this->x /= f; this->y /= f; this->z /= f; return *this;} | 
|---|
| 42 | float dot (const Vector& v) const; | 
|---|
| 43 | 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 ); } | 
|---|
| 44 | void scale(const Vector& v); | 
|---|
| 45 | inline float len() const { return sqrt (x*x+y*y+z*z); } | 
|---|
| 46 | inline void normalize() { | 
|---|
| 47 | float l = len(); | 
|---|
| 48 | if( unlikely(l == 0.0)) | 
|---|
| 49 | { | 
|---|
| 50 | // Prevent divide by zero | 
|---|
| 51 | return; | 
|---|
| 52 | } | 
|---|
| 53 | x = x / l; | 
|---|
| 54 | y = y / l; | 
|---|
| 55 | z = z / l; | 
|---|
| 56 | } | 
|---|
| 57 | const Vector getNormalized(); | 
|---|
| 58 | Vector abs(); | 
|---|
| 59 |  | 
|---|
| 60 | void debug() const; | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | float angleDeg (const Vector& v1, const Vector& v2); | 
|---|
| 64 | float angleRad (const Vector& v1, const Vector& v2); | 
|---|
| 65 |  | 
|---|
| 66 | //! Quaternion | 
|---|
| 67 | /** | 
|---|
| 68 | Class to handle 3-dimensional rotation efficiently | 
|---|
| 69 | */ | 
|---|
| 70 | class Quaternion | 
|---|
| 71 | { | 
|---|
| 72 | public: | 
|---|
| 73 | Vector v;     //!< Imaginary Vector | 
|---|
| 74 | float w;        //!< Real part of the number | 
|---|
| 75 |  | 
|---|
| 76 | inline Quaternion () { w = 1; v = Vector(0,0,0); } | 
|---|
| 77 | inline Quaternion (const Vector& b, float a) { w = a; v = b; } | 
|---|
| 78 | Quaternion (float m[4][4]); | 
|---|
| 79 | inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); } | 
|---|
| 80 | Quaternion (const Vector& dir, const Vector& up); | 
|---|
| 81 | Quaternion (float roll, float pitch, float yaw); | 
|---|
| 82 | Quaternion operator/ (const float& f) const; | 
|---|
| 83 | Quaternion operator* (const float& f) const; | 
|---|
| 84 | Quaternion operator* (const Quaternion& q) const; | 
|---|
| 85 | inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); } | 
|---|
| 86 | inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); } | 
|---|
| 87 | Quaternion conjugate () const {  Quaternion r(*this); | 
|---|
| 88 | r.v = Vector() - r.v; | 
|---|
| 89 | return r;} | 
|---|
| 90 | Quaternion inverse () const; | 
|---|
| 91 | Vector apply (Vector& f) const; | 
|---|
| 92 | float norm () const; | 
|---|
| 93 | void matrix (float m[4][4]) const; | 
|---|
| 94 | void quatSlerp(const Quaternion* from, const Quaternion* to, const float t, Quaternion* res); | 
|---|
| 95 |  | 
|---|
| 96 | void debug(); | 
|---|
| 97 | private: | 
|---|
| 98 | float DELTA;      //!< resolution of calculation | 
|---|
| 99 |  | 
|---|
| 100 | }; | 
|---|
| 101 |  | 
|---|
| 102 | //! 3D rotation (OBSOLETE) | 
|---|
| 103 | /** | 
|---|
| 104 | Class to handle 3-dimensional rotations. | 
|---|
| 105 | Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix | 
|---|
| 106 | */ | 
|---|
| 107 | class Rotation { | 
|---|
| 108 | public: | 
|---|
| 109 |  | 
|---|
| 110 | float m[9]; //!< 3x3 Rotation Matrix | 
|---|
| 111 |  | 
|---|
| 112 | Rotation ( const Vector& v); | 
|---|
| 113 | Rotation ( const Vector& axis, float angle); | 
|---|
| 114 | Rotation ( float pitch, float yaw, float roll); | 
|---|
| 115 | Rotation (); | 
|---|
| 116 | ~Rotation () {} | 
|---|
| 117 |  | 
|---|
| 118 | Rotation operator* (const Rotation& r); | 
|---|
| 119 |  | 
|---|
| 120 | void glmatrix (float* buffer); | 
|---|
| 121 | }; | 
|---|
| 122 |  | 
|---|
| 123 | //!< Apply a rotation to a vector | 
|---|
| 124 | Vector rotateVector( const Vector& v, const Rotation& r); | 
|---|
| 125 |  | 
|---|
| 126 | //! 3D line | 
|---|
| 127 | /** | 
|---|
| 128 | Class to store Lines in 3-dimensional space | 
|---|
| 129 |  | 
|---|
| 130 | Supports line-to-line distance measurements and rotation | 
|---|
| 131 | */ | 
|---|
| 132 | class Line | 
|---|
| 133 | { | 
|---|
| 134 | public: | 
|---|
| 135 |  | 
|---|
| 136 | Vector r;   //!< Offset | 
|---|
| 137 | Vector a;   //!< Direction | 
|---|
| 138 |  | 
|---|
| 139 | Line ( Vector r, Vector a) : r(r), a(a) {}  //!< assignment constructor | 
|---|
| 140 | Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} | 
|---|
| 141 | ~Line () {} | 
|---|
| 142 |  | 
|---|
| 143 | float distance (const Line& l) const; | 
|---|
| 144 | float distancePoint (const Vector& v) const; | 
|---|
| 145 | Vector* footpoints (const Line& l) const; | 
|---|
| 146 | float len () const; | 
|---|
| 147 |  | 
|---|
| 148 | void rotate(const Rotation& rot); | 
|---|
| 149 | }; | 
|---|
| 150 |  | 
|---|
| 151 | //! 3D plane | 
|---|
| 152 | /** | 
|---|
| 153 | Class to handle planes in 3-dimensional space | 
|---|
| 154 |  | 
|---|
| 155 | Critical for polygon-based collision detection | 
|---|
| 156 | */ | 
|---|
| 157 | class Plane | 
|---|
| 158 | { | 
|---|
| 159 | public: | 
|---|
| 160 |  | 
|---|
| 161 | Vector n;   //!< Normal vector | 
|---|
| 162 | float k;    //!< Offset constant | 
|---|
| 163 |  | 
|---|
| 164 | Plane (Vector a, Vector b, Vector c); | 
|---|
| 165 | Plane (Vector norm, Vector p); | 
|---|
| 166 | Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor | 
|---|
| 167 | Plane () : n(Vector(1,1,1)), k(0) {} | 
|---|
| 168 | ~Plane () {} | 
|---|
| 169 |  | 
|---|
| 170 | Vector intersectLine (const Line& l) const; | 
|---|
| 171 | float distancePoint (const Vector& p) const; | 
|---|
| 172 | float locatePoint (const Vector& p) const; | 
|---|
| 173 | }; | 
|---|
| 174 |  | 
|---|
| 175 | #endif /* _VECTOR_H */ | 
|---|