Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: quaternion functions inlined

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