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