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