Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: definition of the separation plane and partition of the vertex. some vector sVec3D modification

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