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 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); } |
---|
36 | Vector operator/ (float f) const; |
---|
37 | float dot (const Vector& v) const; |
---|
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 ); } |
---|
39 | void scale(const Vector& v); |
---|
40 | inline float len() const { return sqrt (x*x+y*y+z*z); } |
---|
41 | inline void normalize() { |
---|
42 | float l = len(); |
---|
43 | if( unlikely(l == 0.0)) |
---|
44 | { |
---|
45 | // Prevent divide by zero |
---|
46 | return; |
---|
47 | } |
---|
48 | x = x / l; |
---|
49 | y = y / l; |
---|
50 | z = z / l; |
---|
51 | } |
---|
52 | Vector* getNormalized(); |
---|
53 | Vector abs(); |
---|
54 | |
---|
55 | void debug(); |
---|
56 | }; |
---|
57 | |
---|
58 | float angleDeg (const Vector& v1, const Vector& v2); |
---|
59 | float angleRad (const Vector& v1, const Vector& v2); |
---|
60 | |
---|
61 | //! Quaternion |
---|
62 | /** |
---|
63 | Class to handle 3-dimensional rotation efficiently |
---|
64 | */ |
---|
65 | class Quaternion |
---|
66 | { |
---|
67 | public: |
---|
68 | Vector v; //!< Imaginary Vector |
---|
69 | float w; //!< Real part of the number |
---|
70 | |
---|
71 | inline Quaternion () { w = 1; v = Vector(0,0,0); } |
---|
72 | inline Quaternion (const Vector& b, float a) { w = a; v = b; } |
---|
73 | Quaternion (float m[4][4]); |
---|
74 | inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); } |
---|
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; |
---|
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); } |
---|
82 | Quaternion conjugate () const { Quaternion r(*this); |
---|
83 | r.v = Vector() - r.v; |
---|
84 | return r;} |
---|
85 | Quaternion inverse () const; |
---|
86 | Vector apply (Vector& f) const; |
---|
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(); |
---|
92 | private: |
---|
93 | float DELTA; //!< resolution of calculation |
---|
94 | |
---|
95 | }; |
---|
96 | |
---|
97 | //! 3D rotation (OBSOLETE) |
---|
98 | /** |
---|
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 | |
---|
113 | Rotation operator* (const Rotation& r); |
---|
114 | |
---|
115 | void glmatrix (float* buffer); |
---|
116 | }; |
---|
117 | |
---|
118 | //!< Apply a rotation to a vector |
---|
119 | Vector rotateVector( const Vector& v, const Rotation& r); |
---|
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; |
---|
139 | float distancePoint (const Vector& v) const; |
---|
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 | |
---|
165 | Vector intersectLine (const Line& l) const; |
---|
166 | float distancePoint (const Vector& p) const; |
---|
167 | float locatePoint (const Vector& p) const; |
---|
168 | }; |
---|
169 | |
---|
170 | #endif /* _VECTOR_H */ |
---|