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