[2014] | 1 | /*! |
---|
| 2 | \file vector.h |
---|
| 3 | \brief A basic 3D math framework |
---|
| 4 | |
---|
| 5 | Contains classes to handle vectors, lines, rotations and planes |
---|
| 6 | */ |
---|
[1939] | 7 | |
---|
| 8 | #ifndef VECTOR_H |
---|
| 9 | #define VECTOR_H |
---|
| 10 | |
---|
[1954] | 11 | #include <math.h> |
---|
| 12 | #define PI 3.14159265359f |
---|
[1939] | 13 | |
---|
| 14 | class Vector { |
---|
| 15 | |
---|
| 16 | public: |
---|
| 17 | |
---|
| 18 | float x, y, z; |
---|
| 19 | |
---|
[2012] | 20 | Vector (float x, float y, float z) : x(x), y(y), z(z) {} //!< assignment constructor |
---|
[1939] | 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 | |
---|
[2012] | 38 | //! 3D rotation |
---|
[2010] | 39 | /** |
---|
[2014] | 40 | Class to handle 3-dimensional rotations. |
---|
[2010] | 41 | Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix |
---|
| 42 | */ |
---|
[1954] | 43 | class Rotation { |
---|
| 44 | public: |
---|
| 45 | |
---|
[2014] | 46 | float m[9]; //!< 3x3 Rotation Matrix |
---|
[1954] | 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 | |
---|
[2080] | 54 | Rotation operator* (const Rotation& r); |
---|
| 55 | |
---|
[2100] | 56 | void glmatrix (float* buffer); |
---|
[2014] | 57 | }; |
---|
| 58 | |
---|
| 59 | //!< Apply a rotation to a vector |
---|
[1954] | 60 | Vector rotate_vector( const Vector& v, const Rotation& r); |
---|
| 61 | |
---|
[2012] | 62 | //! 3D line |
---|
[2010] | 63 | /** |
---|
| 64 | Class to store Lines in 3-dimensional space |
---|
| 65 | |
---|
| 66 | Supports line-to-line distance measurements and rotation |
---|
| 67 | */ |
---|
[1954] | 68 | class Line |
---|
| 69 | { |
---|
| 70 | public: |
---|
| 71 | |
---|
[2012] | 72 | Vector r; //!< Offset |
---|
| 73 | Vector a; //!< Direction |
---|
[1954] | 74 | |
---|
[2012] | 75 | Line ( Vector r, Vector a) : r(r), a(a) {} //!< assignment constructor |
---|
[1954] | 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 | |
---|
[2012] | 87 | //! 3D plane |
---|
[2010] | 88 | /** |
---|
| 89 | Class to handle planes in 3-dimensional space |
---|
[2012] | 90 | |
---|
[2010] | 91 | Critical for polygon-based collision detection |
---|
| 92 | */ |
---|
[1954] | 93 | class Plane |
---|
| 94 | { |
---|
| 95 | public: |
---|
| 96 | |
---|
[2012] | 97 | Vector n; //!< Normal vector |
---|
| 98 | float k; //!< Offset constant |
---|
[1954] | 99 | |
---|
| 100 | Plane (Vector a, Vector b, Vector c); |
---|
| 101 | Plane (Vector norm, Vector p); |
---|
[2012] | 102 | Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor |
---|
[1954] | 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 | |
---|
[1939] | 111 | #endif |
---|