Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/math/vector.h @ 4559

Last change on this file since 4559 was 4545, checked in by patrick, 19 years ago

orxonox/trunk: further improved the vector class, now it can be used like an array :)), finished calculation of the covariance matrix

File size: 9.5 KB
Line 
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#include "compiler.h"
13#include "abstract_model.h"
14//! PI the circle-constant
15#define PI 3.14159265359f
16
17//! 3D Vector
18/**
19        Class to handle 3D Vectors
20*/
21class Vector {
22
23
24 public:
25  Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor
26  Vector () : x(0), y(0), z(0) {}
27  ~Vector () {}
28
29  /** \param index The index of the "array" \returns the x/y/z coordinate */
30  inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z;}
31  /**  \param v The vector to add \returns the addition between two vectors (this + v) */
32  inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); };
33  /** \param v The vector to add  \returns the addition between two vectors (this += v) */
34  inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; };
35  /** \param v The vector to substract  \returns the substraction between two vectors (this - v) */
36  inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
37  /** \param v The vector to substract  \returns the substraction between two vectors (this -= v) */
38  inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; };
39  /** \param v the second vector  \returns The dotProduct between two vector (this (dot) v) */
40  inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; };
41  /** \todo strange */
42  inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; };
43  /** \param f a factor to multiply the vector with \returns the vector multiplied by f (this * f) */
44  inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); };
45  /** \param f a factor to multiply the vector with \returns the vector multiplied by f (this *= f) */
46  inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; };
47  /** \param f a factor to divide the vector with \returns the vector divided by f (this / f) */
48  inline Vector operator/ (float f) const {if (unlikely(f == 0.0)) return Vector(0,0,0); else return Vector(this->x / f, this->y / f, this->z / f); };
49  /** \param f a factor to divide the vector with \returns the vector divided by f (this /= f) */
50  inline const Vector& operator/= (float f) {if (unlikely(f == 0.0)) {this->x=0;this->y=0;this->z=0;} else {this->x /= f; this->y /= f; this->z /= f;} return *this; };
51  /** \brief copy constructor \todo (i do not know it this is faster) \param v the vector to assign to this vector. \returns the vector v */
52  inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; };
53  /** \brief copy constructor  \param v the sVec3D to assign to this vector. \returns the vector v */
54  inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; }
55  /** \param v: the other vector \return the dot product of the vectors */
56  float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; };
57  /** \param v: the corss-product partner \returns the cross-product between this and v (this (x) v) */
58  inline Vector cross (const Vector& v) const { return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x ); }
59  /** \brief scales the this vector with v  \param v the vector to scale this with */
60  void scale(const Vector& v) {   x *= v.x;  y *= v.y; z *= v.z; };
61  /** \returns the length of the vector */
62  inline float len() const { return sqrt (x*x+y*y+z*z); }
63  /** \brief normalizes the vector */
64  inline void normalize() {
65                      float l = len(); 
66                      if( unlikely(l == 0.0)) 
67                        { 
68                          // Prevent divide by zero
69                          return;
70                        }
71                      x = x / l;
72                      y = y / l;
73                      z = z / l; 
74                    }
75  Vector getNormalized() const;
76  Vector abs();
77
78  void debug() const;
79
80 public:
81  float    x;     //!< The x Coordinate of the Vector.
82  float    y;     //!< The y Coordinate of the Vector.
83  float    z;     //!< The z Coordinate of the Vector.
84};
85
86/**
87   \brief calculate the angle between two vectors in radiances
88   \param v1: a vector
89   \param v2: another vector
90   \return the angle between the vectors in radians
91*/
92inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); };
93/**
94   \brief calculate the angle between two vectors in degrees
95   \param v1: a vector
96   \param v2: another vector
97   \return the angle between the vectors in degrees
98*/
99inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; };
100
101
102//! Quaternion
103/**
104   Class to handle 3-dimensional rotation efficiently
105*/
106class Quaternion
107{
108 public:
109  /** \brief creates a Default quaternion (multiplicational identity Quaternion)*/
110  inline Quaternion () { w = 1; v = Vector(0,0,0); }
111  /** \brief creates a Quaternion looking into the direction v \param v: the direction \param f: the value */
112  inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
113  Quaternion (float m[4][4]);
114  /** \brief turns a rotation along an axis into a Quaternion \param angle: the amount of radians to rotate \param axis: the axis to rotate around */
115  inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); }
116  Quaternion (const Vector& dir, const Vector& up);
117  Quaternion (float roll, float pitch, float yaw);
118  Quaternion operator/ (const float& f) const;
119  /** \param f: the value to divide by \returns the quaternion devided by f (this /= f) */
120  inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
121  Quaternion operator* (const float& f) const;
122  /** \param f: the value to multiply by \returns the quaternion multiplied by f (this *= f) */
123  inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
124  Quaternion operator* (const Quaternion& q) const;
125  /** \param q: the Quaternion to multiply by \returns the quaternion multiplied by q (this *= q) */ 
126  inline const Quaternion operator*= (const Quaternion& q) {*this = *this * q; return *this; };
127  /** \param q the Quaternion to add to this \returns the quaternion added with q (this + q) */
128  inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
129  /** \param q the Quaternion to add to this \returns the quaternion added with q (this += q) */
130  inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
131  /** \param q the Quaternion to substrace from this \returns the quaternion substracted by q (this - q) */
132  inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
133  /** \param q the Quaternion to substrace from this \returns the quaternion substracted by q (this -= q) */
134  inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
135  /** \brief copy constructor \param q: the Quaternion to set this to. \returns the Quaternion q (or this) */
136  inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
137  /** \brief conjugates this Quaternion \returns the conjugate */
138  inline Quaternion conjugate () const {  Quaternion r(*this);  r.v = Vector() - r.v;  return r;}
139  Quaternion inverse () const;
140  Vector apply (const Vector& f) const;
141  float norm () const;
142  void matrix (float m[4][4]) const;
143 
144  void debug();
145
146 public:
147  Vector    v;        //!< Imaginary Vector
148  float     w;        //!< Real part of the number
149
150};
151
152Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
153
154
155
156//! 3D rotation (OBSOLETE)
157/**
158  Class to handle 3-dimensional rotations.
159  Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix
160*/
161class Rotation {
162  public:
163 
164  float m[9]; //!< 3x3 Rotation Matrix
165 
166  Rotation ( const Vector& v);
167  Rotation ( const Vector& axis, float angle);
168  Rotation ( float pitch, float yaw, float roll);
169  Rotation ();
170  ~Rotation () {}
171 
172  Rotation operator* (const Rotation& r);
173 
174  void glmatrix (float* buffer);
175};
176
177//!< Apply a rotation to a vector
178Vector rotateVector( const Vector& v, const Rotation& r);
179
180//! 3D line
181/**
182  Class to store Lines in 3-dimensional space
183
184  Supports line-to-line distance measurements and rotation
185*/
186class Line
187{
188  public:
189 
190  Vector r;   //!< Offset
191  Vector a;   //!< Direction
192 
193  Line ( Vector r, Vector a) : r(r), a(a) {}  //!< assignment constructor
194  Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {}
195  ~Line () {}
196 
197  float distance (const Line& l) const;
198  float distancePoint (const Vector& v) const;
199  Vector* footpoints (const Line& l) const;
200  float len () const;
201 
202  void rotate(const Rotation& rot);
203};
204
205//! 3D plane
206/**
207  Class to handle planes in 3-dimensional space
208 
209  Critical for polygon-based collision detection
210*/
211class Plane
212{
213  public:
214 
215  Vector n;   //!< Normal vector
216  float k;    //!< Offset constant
217 
218  Plane (Vector a, Vector b, Vector c);
219  Plane (Vector norm, Vector p);
220  Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor
221  Plane () : n(Vector(1,1,1)), k(0) {}
222  ~Plane () {}
223 
224  Vector intersectLine (const Line& l) const;
225  float distancePoint (const Vector& p) const;
226  float locatePoint (const Vector& p) const;
227};
228
229#endif /* _VECTOR_H */
Note: See TracBrowser for help on using the repository browser.