Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: removed the modelinfo from the includes of vector.h

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