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 angle_deg (const Vector& v1, const Vector& v2); |
---|
43 | float angle_rad (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 rotate_vector( 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 distance_point (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 intersect_line (const Line& l) const; |
---|
147 | float distance_point (const Vector& p) const; |
---|
148 | float locate_point (const Vector& p) const; |
---|
149 | }; |
---|
150 | |
---|
151 | #endif |
---|