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 | class Vector { |
---|
15 | |
---|
16 | public: |
---|
17 | |
---|
18 | float x, y, z; |
---|
19 | |
---|
20 | Vector (float x, float y, float z) : x(x), y(y), z(z) {} //!< assignment constructor |
---|
21 | Vector () : x(0), y(0), z(0) {} |
---|
22 | ~Vector () {} |
---|
23 | |
---|
24 | Vector operator+ (const Vector& v) const; |
---|
25 | Vector operator- (const Vector& v) const; |
---|
26 | float operator* (const Vector& v) const; |
---|
27 | Vector operator* (float f) const; |
---|
28 | Vector operator/ (float f) const; |
---|
29 | float dot (const Vector& v) const; |
---|
30 | Vector cross (const Vector& v) const; |
---|
31 | float len() const; |
---|
32 | void normalize(); |
---|
33 | }; |
---|
34 | |
---|
35 | float angle_deg (const Vector& v1, const Vector& v2); |
---|
36 | float angle_rad (const Vector& v1, const Vector& v2); |
---|
37 | |
---|
38 | //! 3D rotation |
---|
39 | /** |
---|
40 | Class to handle 3-dimensional rotations. |
---|
41 | Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix |
---|
42 | */ |
---|
43 | class Rotation { |
---|
44 | public: |
---|
45 | |
---|
46 | float m[9]; //!< 3x3 Rotation Matrix |
---|
47 | |
---|
48 | Rotation ( const Vector& v); |
---|
49 | Rotation ( const Vector& axis, float angle); |
---|
50 | Rotation ( float pitch, float yaw, float roll); |
---|
51 | Rotation (); |
---|
52 | ~Rotation () {} |
---|
53 | |
---|
54 | Rotation operator* (const Rotation& r); |
---|
55 | |
---|
56 | void glmatrix (float* buffer); |
---|
57 | }; |
---|
58 | |
---|
59 | //!< Apply a rotation to a vector |
---|
60 | Vector rotate_vector( const Vector& v, const Rotation& r); |
---|
61 | |
---|
62 | //! 3D line |
---|
63 | /** |
---|
64 | Class to store Lines in 3-dimensional space |
---|
65 | |
---|
66 | Supports line-to-line distance measurements and rotation |
---|
67 | */ |
---|
68 | class Line |
---|
69 | { |
---|
70 | public: |
---|
71 | |
---|
72 | Vector r; //!< Offset |
---|
73 | Vector a; //!< Direction |
---|
74 | |
---|
75 | Line ( Vector r, Vector a) : r(r), a(a) {} //!< assignment constructor |
---|
76 | Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} |
---|
77 | ~Line () {} |
---|
78 | |
---|
79 | float distance (const Line& l) const; |
---|
80 | float distance_point (const Vector& v) const; |
---|
81 | Vector* footpoints (const Line& l) const; |
---|
82 | float len () const; |
---|
83 | |
---|
84 | void rotate(const Rotation& rot); |
---|
85 | }; |
---|
86 | |
---|
87 | //! 3D plane |
---|
88 | /** |
---|
89 | Class to handle planes in 3-dimensional space |
---|
90 | |
---|
91 | Critical for polygon-based collision detection |
---|
92 | */ |
---|
93 | class Plane |
---|
94 | { |
---|
95 | public: |
---|
96 | |
---|
97 | Vector n; //!< Normal vector |
---|
98 | float k; //!< Offset constant |
---|
99 | |
---|
100 | Plane (Vector a, Vector b, Vector c); |
---|
101 | Plane (Vector norm, Vector p); |
---|
102 | Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor |
---|
103 | Plane () : n(Vector(1,1,1)), k(0) {} |
---|
104 | ~Plane () {} |
---|
105 | |
---|
106 | Vector intersect_line (const Line& l) const; |
---|
107 | float distance_point (const Vector& p) const; |
---|
108 | float locate_point (const Vector& p) const; |
---|
109 | }; |
---|
110 | |
---|
111 | #endif |
---|