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