Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4536 was 4477, checked in by bensch, 20 years ago

orxonox/trunk: vector is completely documented again :)

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