Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5052 was 5052, checked in by bensch, 19 years ago

orxonox/trunk: Vector and Quaternion comparison

File size: 15.1 KB
Line 
1/*!
2 * @file vector.h
3 * 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 v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */
30  inline bool operator== (const Vector& v) const { return (this->x==v.x&&this->y==v.y&&this->z==v.z)?true:false; };
31  /** @param index The index of the "array" @returns the x/y/z coordinate */
32  inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z; }
33  /** @param v The vector to add @returns the addition between two vectors (this + v) */
34  inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); };
35  /** @param v The vector to add @returns the addition between two vectors (this + v) */
36  inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); };
37  /** @param v The vector to add  @returns the addition 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 vector to substract  @returns the substraction between two vectors (this - v) */
40  inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; };
41  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
42  inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
43  /** @param v The vector to substract  @returns the substraction between two vectors (this - v) */
44  inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); }
45  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
46  inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; };
47  /** @param v The vector to substract  @returns the substraction between two vectors (this -= v) */
48  inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; };
49  /** @param v the second vector  @returns The dotProduct between two vector (this (dot) v) */
50  inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; };
51  /** @todo strange */
52  inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; };
53  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */
54  inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); };
55  /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */
56  inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; };
57  /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */
58  inline Vector operator/ (float f) const { return (unlikely(f == 0.0))?Vector(0,0,0):Vector(this->x / f, this->y / f, this->z / f); };
59  /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */
60  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; };
61  /**  copy constructor @todo (i do not know it this is faster) @param v the vector to assign to this vector. @returns the vector v */
62  inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; };
63  /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */
64  inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; }
65  /** @param v: the other vector \return the dot product of the vectors */
66  float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; };
67  /** @param v: the corss-product partner @returns the cross-product between this and v (this (x) v) */
68  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 ); }
69  /** scales the this vector with v* @param v the vector to scale this with */
70  void scale(const Vector& v) {   x *= v.x;  y *= v.y; z *= v.z; };
71  /** @returns the length of the vector */
72  inline float len() const { return sqrt (x*x+y*y+z*z); }
73  /** normalizes the vector */
74  inline void normalize() {
75                      float l = len();
76                      if( unlikely(l == 0.0))
77                        {
78                          // Prevent divide by zero
79                          return;
80                        }
81                      x = x / l;
82                      y = y / l;
83                      z = z / l;
84                    }
85  Vector getNormalized() const;
86  Vector abs();
87
88  void debug() const;
89
90 public:
91  float    x;     //!< The x Coordinate of the Vector.
92  float    y;     //!< The y Coordinate of the Vector.
93  float    z;     //!< The z Coordinate of the Vector.
94};
95
96/**
97 *  calculate the angle between two vectors in radiances
98 * @param v1: a vector
99 * @param v2: another vector
100 * @return the angle between the vectors in radians
101*/
102inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); };
103/**
104 *  calculate the angle between two vectors in degrees
105 * @param v1: a vector
106 * @param v2: another vector
107 * @return the angle between the vectors in degrees
108*/
109inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; };
110
111/** an easy way to create a Random Vector @param sideLength the length of the Vector (x not sqrt(x^2...)) */
112#define VECTOR_RAND(sideLength)  (Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength)
113
114
115//! Quaternion
116/**
117   Class to handle 3-dimensional rotation efficiently
118*/
119class Quaternion
120{
121 public:
122  /** creates a Default quaternion (multiplicational identity Quaternion)*/
123  inline Quaternion () { w = 1; v = Vector(0,0,0); }
124  /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */
125  inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; }
126  Quaternion (float m[4][4]);
127  /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */
128  inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); }
129  Quaternion (const Vector& dir, const Vector& up);
130  Quaternion (float roll, float pitch, float yaw);
131
132  /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */
133  inline bool operator== (const Quaternion& q) const { return (unlikely(this->v==q.v&&this->w==q.w))?true:false; };
134  /** @param f: a real value @return a Quaternion containing the quotient */
135  inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); };
136  /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */
137  inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;}
138  /** @param f: a real value @return a Quaternion containing the product */
139  inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); };
140  /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */
141  inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;}
142  /** @param q: another Quaternion to rotate this by @return a quaternion that represents the first one rotated by the second one (WARUNING: this operation is not commutative! e.g. (A*B) != (B*A)) */
143  Quaternion operator* (const Quaternion& q) const { return Quaternion(Vector(this->w*q.v.x + this->v.x*q.w + this->v.y*q.v.z - this->v.z*q.v.y,
144                                                                         this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z,
145                                                                         this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x),
146                                                                         this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z); };
147  /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */
148  inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; };
149  /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */
150  inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); };
151  /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */
152  inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; };
153  /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */
154  inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); };
155  /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */
156  inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; };
157  /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */
158  inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
159  /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */
160  inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; };
161  /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */
162  inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
163  /** conjugates this Quaternion @returns the conjugate */
164  inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); };
165  /** @returns the norm of The Quaternion */
166  inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
167  /** @returns the inverted Quaterntion of this */
168  inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); };
169  /** @param v: the Vector  @return a new Vector representing v rotated by the Quaternion */
170  inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; };
171  void matrix (float m[4][4]) const;
172  /** @returns the normalized Quaternion (|this|) */
173  inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); };
174  /** normalizes the current Quaternion */
175  inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; };
176
177  /** @returns the rotational axis of this Quaternion */
178  inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ };
179  /** @returns the rotational angle of this Quaternion around getSpacialAxis()  !! IN DEGREE !! */
180  inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos(this->w); };
181
182  static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t);
183
184  void debug();
185  void debug2();
186
187
188 public:
189  Vector    v;        //!< Imaginary Vector
190  float     w;        //!< Real part of the number
191
192};
193
194
195
196
197//! 3D rotation (OBSOLETE)
198/**
199  Class to handle 3-dimensional rotations.
200  Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix
201*/
202class Rotation {
203  public:
204
205  float m[9]; //!< 3x3 Rotation Matrix
206
207  Rotation ( const Vector& v);
208  Rotation ( const Vector& axis, float angle);
209  Rotation ( float pitch, float yaw, float roll);
210  Rotation ();
211  ~Rotation () {}
212
213  Rotation operator* (const Rotation& r);
214
215  void glmatrix (float* buffer);
216};
217
218//!< Apply a rotation to a vector
219Vector rotateVector( const Vector& v, const Rotation& r);
220
221//! 3D line
222/**
223  Class to store Lines in 3-dimensional space
224
225  Supports line-to-line distance measurements and rotation
226*/
227class Line
228{
229  public:
230
231  Vector r;   //!< Offset
232  Vector a;   //!< Direction
233
234  Line ( Vector r, Vector a) : r(r), a(a) {}  //!< assignment constructor
235  Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {}
236  ~Line () {}
237
238  float distance (const Line& l) const;
239  float distancePoint (const Vector& v) const;
240  float distancePoint (const sVec3D& v) const;
241  Vector* footpoints (const Line& l) const;
242  float len () const;
243
244  void rotate(const Rotation& rot);
245};
246
247//! 3D plane
248/**
249  Class to handle planes in 3-dimensional space
250
251  Critical for polygon-based collision detection
252*/
253class Plane
254{
255  public:
256
257  Vector n;   //!< Normal vector
258  float k;    //!< Offset constant
259
260  Plane (Vector a, Vector b, Vector c);
261  Plane (Vector norm, Vector p);
262  Plane (Vector norm, sVec3D p);
263  Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor
264  Plane () : n(Vector(1,1,1)), k(0) {}
265  ~Plane () {}
266
267  Vector intersectLine (const Line& l) const;
268  float distancePoint (const Vector& p) const;
269  float distancePoint (const sVec3D& p) const;
270  float locatePoint (const Vector& p) const;
271};
272
273
274
275//! A class that represents a rectangle, this is needed for SpatialSeparation
276class Rectangle
277{
278
279  public:
280    Rectangle() { this->center = new Vector(); }
281    Rectangle(const Vector &center, float len) { this->center = new Vector(center.x, center.y, center.z); this->axis[0] = len; this->axis[1] = len; }
282    virtual ~Rectangle() {}
283
284    /** \brief sets the center of the rectangle to a defined vector @param center the new center */
285   inline void setCenter(const Vector &center) { *this->center = center;}
286    /** \brief sets the center of the rectangle to a defined vector @param x coord of the center @param y coord of the center @param z coord of the center */
287   inline void setCenter(float x, float y, float z) { this->center->x = x; this->center->y = y; this->center->z = z; }
288   /** \brief returns the center of the rectangle to a defined vector @returns center the new center */
289   inline const Vector* getCenter() const { return this->center; }
290
291   /** \brief sets both axis of the rectangle to a defined vector @param unityLength the new center */
292   inline void setAxis(float unityLength) { this->axis[0] = unityLength; this->axis[1] = unityLength; }
293   /** \brief sets both axis of the rectangle to a defined vector @param v1 the length of the x axis @param v2 the length of the z axis*/
294   inline void setAxis(float v1, float v2) { this->axis[0] = v1; this->axis[1] = v2; }
295   /** \brief gets one axis length of the rectangle  @returns the length of the axis 0 */
296   inline float getAxis() { return this-> axis[0]; }
297
298  private:
299    Vector*         center;
300    float           axis[2];
301};
302
303
304#endif /* _VECTOR_H */
305
Note: See TracBrowser for help on using the repository browser.