Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/math/vector_new.h @ 3607

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

orxonox/trunk: stdincl was still included everywhere. removed it out of the stdincl.h file to enable the possibility of compile-speedup. added some testclasses for vector/quaternion.

File size: 4.7 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//! PI the circle-constant
13#define PI 3.14159265359f  /* this is not nice... make a math_def file or something */ 
14
15//! 3D Vector
16/**
17        Class to handle 3D Vectors
18*/
19class Vector {
20
21  public:
22 
23  float x; //!< The x Coordinate of the Vector.
24  float y; //!< The y Coordinate of the Vector.
25  float z; //!< The z Coordinate of the Vector.
26
27  Vector (float x, float y, float z) : x(x), y(y), z(z) {}  //!< assignment constructor
28  Vector () : x(0), y(0), z(0) {}
29  ~Vector () {}
30
31  Vector operator+ (const Vector& v) const;
32  Vector operator- (const Vector& v) const;
33  float operator* (const Vector& v) const;
34  Vector operator* (float f) const;
35  Vector operator/ (float f) const;
36  inline float dot (const Vector& v) const;
37  Vector cross (const Vector& v) const;
38  void scale(const Vector& v);
39  float len() const;
40  void normalize();
41  Vector* getNormalized();
42  Vector abs();
43
44  void debug();
45};
46
47//float angleDeg (const Vector& v1, const Vector& v2);
48//float angleRad (const Vector& v1, const Vector& v2);
49
50
51/**
52   \brief add two vectors
53   \param v: the other vector
54   \return the sum of both vectors
55*/
56Vector Vector::operator+ (const Vector& v) const
57{
58  Vector r;
59
60  r.x = x + v.x;
61  r.y = y + v.y;
62  r.z = z + v.z;
63 
64  return r;
65}
66
67/**
68   \brief subtract a vector from another
69   \param v: the other vector
70   \return the difference between the vectors
71*/
72Vector Vector::operator- (const Vector& v) const
73{
74  Vector r;
75 
76  r.x = x - v.x;
77  r.y = y - v.y;
78  r.z = z - v.z;
79 
80  return r;
81}
82
83/**
84   \brief calculate the dot product of two vectors
85   \param v: the other vector
86   \return the dot product of the vectors
87*/
88float Vector::operator* (const Vector& v) const
89{
90  return x * v.x + y * v.y + z * v.z;
91}
92
93/**
94   \brief multiply a vector with a float
95   \param f: the factor
96   \return the vector multipied by f
97*/
98Vector Vector::operator* (float f) const
99{ 
100  Vector r;
101 
102  r.x = x * f;
103  r.y = y * f;
104  r.z = z * f;
105 
106  return r;
107}
108
109/**
110   \brief divide a vector with a float
111   \param f: the divisor
112   \return the vector divided by f   
113*/
114Vector Vector::operator/ (float f) const
115{
116  Vector r;
117 
118  if( f == 0.0)
119  {
120    // Prevent divide by zero
121    return Vector (0,0,0);
122  }
123 
124  r.x = x / f;
125  r.y = y / f;
126  r.z = z / f;
127 
128  return r;
129}
130
131/**
132   \brief calculate the dot product of two vectors
133   \param v: the other vector
134   \return the dot product of the vectors
135*/
136inline float Vector::dot (const Vector& v) const
137{
138  return x*v.x+y*v.y+z*v.z;
139}
140
141/**
142  \brief calculate the cross product of two vectors
143  \param v: the other vector
144        \return the cross product of the vectors
145*/
146Vector Vector::cross (const Vector& v) const
147{
148  Vector r;
149 
150  r.x = y * v.z - z * v.y;
151  r.y = z * v.x - x * v.z;
152  r.z = x * v.y - y * v.x;
153 
154  return r;
155}
156 
157/**
158   \brief normalizes the vector to lenght 1.0
159*/
160void Vector::normalize ()
161{
162  float l = len();
163 
164  if( l == 0.0)
165  {
166    // Prevent divide by zero
167    return;
168  }
169 
170  x = x / l;
171  y = y / l;
172  z = z / l;
173}
174
175
176/**
177   \brief returns the voctor normalized to length 1.0
178*/
179
180Vector* Vector::getNormalized()
181{
182  float l = len();
183  if(l != 1.0)
184    {
185      return this;
186    }
187  else if(l == 0.0)
188    {
189      return 0;
190    }
191 
192  Vector *normalizedVector = new Vector(x / l, y /l, z / l);
193  return normalizedVector;
194}
195
196/**
197   \brief scales this Vector with Vector v.
198   \param v the vector to scale this vector with
199*/
200void Vector::scale(const Vector& v)
201{
202  x *= v.x;
203  y *= v.y;
204  z *= v.z;
205}
206
207 
208/**
209   \brief calculates the lenght of the vector
210   \return the lenght of the vector
211*/
212float Vector::len () const
213{
214  return sqrt (x*x+y*y+z*z);
215}
216
217
218/**
219   \brief Vector is looking in the positive direction on all axes after this
220*/
221Vector Vector::abs() 
222{
223  Vector v(fabs(x), fabs(y), fabs(z));
224  return v;
225}
226
227/**
228   \brief calculate the angle between two vectors in radiances
229   \param v1: a vector
230   \param v2: another vector
231   \return the angle between the vectors in radians
232*/
233float angleRad (const Vector& v1, const Vector& v2)
234{
235  return acos( v1 * v2 / (v1.len() * v2.len()));
236}
237
238
239/**
240   \brief calculate the angle between two vectors in degrees
241   \param v1: a vector
242   \param v2: another vector
243   \return the angle between the vectors in degrees
244*/
245float angleDeg (const Vector& v1, const Vector& v2)
246{
247  float f;
248  f = acos( v1 * v2 / (v1.len() * v2.len()));
249  return f * 180 / PI;
250}
251
252
253/**
254   \brief Outputs the values of the Vector
255*/
256void Vector::debug(void)
257{
258  PRINT(0)("Vector Debug information\n");
259  PRINT(0)("x: %f; y: %f; z: %f", x, y, z);
260  PRINT(3)(" lenght: %f", len());
261  PRINT(0)("\n");
262}
263
264
265#endif /* _VECTOR_H */
Note: See TracBrowser for help on using the repository browser.