Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particleEngine/src/lib/math/vector.h @ 3960

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

orxonox/branches/particleEngine: copy-constuctor-prevention due to copying values

File size: 5.3 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  public:
23 
24  float x; //!< The x Coordinate of the Vector.
25  float y; //!< The y Coordinate of the Vector.
26  float z; //!< The z Coordinate of the Vector.
27
28  Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor
29  Vector () : x(0), y(0), z(0) {}
30  ~Vector () {}
31
32  inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
33  inline const Vector operator+= (const Vector& v) {this->x += v.x; this->y += v.y; this->z += v.z; return *this;}
34  inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
35  inline const Vector operator-= (const Vector& v) {this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this;}
36  inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; }
37  inline const Vector operator*= (const Vector& v) {this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this;}
38  inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); }
39  inline const Vector operator*= (float f) {this->x *= f; this->y *= f; this->z *= f; return *this;}
40  Vector operator/ (float f) const;
41  inline const Vector operator/= (float f) {this->x /= f; this->y /= f; this->z /= f; return *this;}
42  inline const Vector operator= (const Vector& v) {this->x = v.x; this->y = v.y; this->z = v.z; return *this;}
43  float dot (const Vector& v) const;
44  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 ); }
45  void scale(const Vector& v);
46  inline float len() const { return sqrt (x*x+y*y+z*z); }
47  inline void normalize() { 
48                      float l = len(); 
49                      if( unlikely(l == 0.0)) 
50                        { 
51                          // Prevent divide by zero
52                          return;
53                        }
54                      x = x / l;
55                      y = y / l;
56                      z = z / l; 
57                    }
58  const Vector getNormalized();
59  Vector abs();
60
61  void debug() const;
62};
63
64float angleDeg (const Vector& v1, const Vector& v2);
65float angleRad (const Vector& v1, const Vector& v2);
66
67//! Quaternion
68/**
69        Class to handle 3-dimensional rotation efficiently
70*/
71class Quaternion
72{
73 public:
74  Vector v;     //!< Imaginary Vector
75  float w;        //!< Real part of the number
76 
77  inline Quaternion () { w = 1; v = Vector(0,0,0); }
78  inline Quaternion (const Vector& b, float a) { w = a; v = b; }
79  Quaternion (float m[4][4]);
80  inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); }
81  Quaternion (const Vector& dir, const Vector& up);
82  Quaternion (float roll, float pitch, float yaw);
83  Quaternion operator/ (const float& f) const;
84  Quaternion operator* (const float& f) const;
85  Quaternion operator* (const Quaternion& q) const;
86  inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); }
87  inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); }
88  inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;}
89  Quaternion conjugate () const {  Quaternion r(*this);
90  r.v = Vector() - r.v;
91  return r;}
92  Quaternion inverse () const;
93  Vector apply (Vector& f) const;
94  float norm () const;
95  void matrix (float m[4][4]) const;
96  void quatSlerp(const Quaternion* from, const Quaternion* to, const float t, Quaternion* res);
97 
98  void debug();
99 private:
100  float DELTA;      //!< resolution of calculation
101
102};
103
104//! 3D rotation (OBSOLETE)
105/**
106  Class to handle 3-dimensional rotations.
107  Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix
108*/
109class Rotation {
110  public:
111 
112  float m[9]; //!< 3x3 Rotation Matrix
113 
114  Rotation ( const Vector& v);
115  Rotation ( const Vector& axis, float angle);
116  Rotation ( float pitch, float yaw, float roll);
117  Rotation ();
118  ~Rotation () {}
119 
120  Rotation operator* (const Rotation& r);
121 
122  void glmatrix (float* buffer);
123};
124
125//!< Apply a rotation to a vector
126Vector rotateVector( const Vector& v, const Rotation& r);
127
128//! 3D line
129/**
130  Class to store Lines in 3-dimensional space
131
132  Supports line-to-line distance measurements and rotation
133*/
134class Line
135{
136  public:
137 
138  Vector r;   //!< Offset
139  Vector a;   //!< Direction
140 
141  Line ( Vector r, Vector a) : r(r), a(a) {}  //!< assignment constructor
142  Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {}
143  ~Line () {}
144 
145  float distance (const Line& l) const;
146  float distancePoint (const Vector& v) const;
147  Vector* footpoints (const Line& l) const;
148  float len () const;
149 
150  void rotate(const Rotation& rot);
151};
152
153//! 3D plane
154/**
155  Class to handle planes in 3-dimensional space
156 
157  Critical for polygon-based collision detection
158*/
159class Plane
160{
161  public:
162 
163  Vector n;   //!< Normal vector
164  float k;    //!< Offset constant
165 
166  Plane (Vector a, Vector b, Vector c);
167  Plane (Vector norm, Vector p);
168  Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor
169  Plane () : n(Vector(1,1,1)), k(0) {}
170  ~Plane () {}
171 
172  Vector intersectLine (const Line& l) const;
173  float distancePoint (const Vector& p) const;
174  float locatePoint (const Vector& p) const;
175};
176
177#endif /* _VECTOR_H */
Note: See TracBrowser for help on using the repository browser.