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