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 | inline const Vector& operator= (const Vector& v) {this->x = v.x; this->y = v.y; this->z = v.z; return *this;} |
---|
43 | float dot (const Vector& v) const; |
---|
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 ); } |
---|
45 | void scale(const Vector& v); |
---|
46 | inline float len() const { return sqrt (x*x+y*y+z*z); } |
---|
47 | inline void normalize() { |
---|
48 | float l = len(); |
---|
49 | if( unlikely(l == 0.0)) |
---|
50 | { |
---|
51 | // Prevent divide by zero |
---|
52 | return; |
---|
53 | } |
---|
54 | x = x / l; |
---|
55 | y = y / l; |
---|
56 | z = z / l; |
---|
57 | } |
---|
58 | Vector getNormalized(); |
---|
59 | Vector abs(); |
---|
60 | |
---|
61 | void debug() const; |
---|
62 | }; |
---|
63 | |
---|
64 | float angleDeg (const Vector& v1, const Vector& v2); |
---|
65 | float angleRad (const Vector& v1, const Vector& v2); |
---|
66 | |
---|
67 | //! Quaternion |
---|
68 | /** |
---|
69 | Class to handle 3-dimensional rotation efficiently |
---|
70 | */ |
---|
71 | class Quaternion |
---|
72 | { |
---|
73 | public: |
---|
74 | Vector v; //!< Imaginary Vector |
---|
75 | float w; //!< Real part of the number |
---|
76 | |
---|
77 | inline Quaternion () { w = 1; v = Vector(0,0,0); } |
---|
78 | inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; } |
---|
79 | Quaternion (float m[4][4]); |
---|
80 | inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); } |
---|
81 | Quaternion (const Vector& dir, const Vector& up); |
---|
82 | Quaternion (float roll, float pitch, float yaw); |
---|
83 | Quaternion operator/ (const float& f) const; |
---|
84 | inline const Quaternion operator/= (const float& f) {*this = *this / f; return *this;} |
---|
85 | Quaternion operator* (const float& f) const; |
---|
86 | inline const Quaternion operator*= (const float& f) {*this = *this * f; return *this;} |
---|
87 | Quaternion operator* (const Quaternion& q) const; |
---|
88 | inline const Quaternion operator*= (const Quaternion& q) {*this = *this * q; return *this;} |
---|
89 | inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); } |
---|
90 | inline const Quaternion& operator+= (const Quaternion& q) {this->v += q.v; this->w += q.w; return *this;} |
---|
91 | inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); } |
---|
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;} |
---|
94 | Quaternion conjugate () const { Quaternion r(*this); |
---|
95 | r.v = Vector() - r.v; |
---|
96 | return r;} |
---|
97 | Quaternion inverse () const; |
---|
98 | Vector apply (const Vector& f) const; |
---|
99 | float norm () const; |
---|
100 | void matrix (float m[4][4]) const; |
---|
101 | |
---|
102 | void debug(); |
---|
103 | }; |
---|
104 | |
---|
105 | Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t); |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | //! 3D rotation (OBSOLETE) |
---|
110 | /** |
---|
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 | |
---|
125 | Rotation operator* (const Rotation& r); |
---|
126 | |
---|
127 | void glmatrix (float* buffer); |
---|
128 | }; |
---|
129 | |
---|
130 | //!< Apply a rotation to a vector |
---|
131 | Vector rotateVector( const Vector& v, const Rotation& r); |
---|
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; |
---|
151 | float distancePoint (const Vector& v) const; |
---|
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 | |
---|
177 | Vector intersectLine (const Line& l) const; |
---|
178 | float distancePoint (const Vector& p) const; |
---|
179 | float locatePoint (const Vector& p) const; |
---|
180 | }; |
---|
181 | |
---|
182 | #endif /* _VECTOR_H */ |
---|