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