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