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