TORCS  1.3.9
The Open Racing Car Simulator
v3_t.h
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : v3_t.h
4  created : Due Apr 5 13:51:00 CET 2005
5  copyright : (C) 2005 by Bernhard Wymann
6  email : berniw@bluewin.ch
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 /*
21  Template for 3d vector of primitive types (float, double). This template is NOT
22  intended to work with classes which allocate memory. Be aware that there are more
23  efficient methods for doing most of the operations (avoiding temp verctors and make
24  better use of registers). Later I will try to improve the performance (SSE,
25  "abuse" of templates to avoid temporaries).
26 */
27 
28 #ifndef _LINALG_V3_T_H_
29 #define _LINALG_V3_T_H_
30 
31 #include <math.h>
32 
33 template<class T> class v3t;
34 #ifndef _MSC_VER
35 template<class T> v3t<T> operator*(const T s, const v3t<T> & src);
36 template<class T> v3t<T> crossProduct(const v3t<T> &a, const v3t<T> &b);
37 #endif //_MSC_VER
38 
39 
40 template<class T> class v3t {
41  public:
42  // Friends.
43 #ifndef _MSC_VER
44  friend v3t<T> operator*<>(const T s, const v3t<T> & src); // Multiply by scalar.
45  friend v3t<T> crossProduct<>(const v3t<T> &a, const v3t<T> &b); // return a X b
46 #endif //_MSC_VER
47 
48  // Constructors.
49  v3t() {}
50  v3t(const v3t<T> &src):x(src.x), y(src.y), z(src.z) {}
51  v3t(const T x, const T y, const T z):x(x), y(y), z(z) {}
52  v3t(const T s):x(s), y(s), z(s) {}
53 
54  // Operators.
55  v3t<T>& operator=(const v3t<T> &src); // Assignment.
56  v3t<T>& operator+=(const v3t<T> &src); // Addition.
57  v3t<T>& operator-=(const v3t<T> &src); // Subtraction.
58  v3t<T>& operator*=(const T s); // Multiply with scalar.
59  v3t<T>& operator/=(const T s); // Divide by scalar.
60 
61  v3t<T> operator-(void) const; // Negation.
62  v3t<T> operator+(const v3t<T> &src) const; // Addition.
63  v3t<T> operator-(const v3t<T> &src) const; // Subtraction.
64 
65 
66  v3t<T> operator*(const T s) const; // Multiply by scalar.
67  T operator*(const v3t<T> &src) const; // Dot product.
68  v3t<T> operator/(const T s) const; // Divide by scalar.
69  int operator==(const v3t<T> &src) const; // all fields equal?
70  int operator!=(const v3t<T> &src) const; // not all fields equal?
71 
72 
73  // Other methods.
74  T len(void);
75  void normalize(void);
76  void crossProduct(const v3t<T> &b, v3t<T> &r) const; // r := this X b
77 
78  void dirVector(const v3t<T>* b, v3t<T>* r); // r := this - b
79  int approxEquals(const v3t<T> &cmp, T eps); // Approximately equality per component, eps > 0.0. TODO: Test
80 
81  union {
82  struct { T x, y, z; };
83  T vec[3];
84  };
85 };
86 
87 
88 template<class T> inline v3t<T> & v3t<T>::operator=(const v3t<T> &src)
89 {
90  x = src.x; y = src.y; z = src.z;
91  return *this;
92 }
93 
94 
95 // Addition
96 template<class T> inline v3t<T>& v3t<T>::operator+=(const v3t<T> &src)
97 {
98  x += src.x; y += src.y; z += src.z;
99  return *this;
100 }
101 
102 
103 // Subtraction.
104 template<class T> inline v3t<T>& v3t<T>::operator-=(const v3t<T> &src)
105 {
106  x -= src.x; y -= src.y; z -= src.z;
107  return *this;
108 }
109 
110 
111 // Multiply with scalar.
112 template<class T> inline v3t<T>& v3t<T>::operator*=(const T s)
113 {
114  x *= s; y *= s; z *= s;
115  return *this;
116 }
117 
118 
119 // Divide by scalar.
120 template<class T> inline v3t<T>& v3t<T>::operator/=(const T s)
121 {
122  x /= s; y /= s; z /= s;
123  return *this;
124 }
125 
126 
127 template<class T> inline v3t<T> v3t<T>::operator-(void) const
128 {
129  return v3t(-this->x, -this->y, -this->z);
130 }
131 
132 
133 template<class T> inline v3t<T> v3t<T>::operator-(const v3t<T> &src) const
134 {
135  return v3t(this->x - src.x, this->y - src.y, this->z - src.z);
136 }
137 
138 
139 template<class T> inline v3t<T> v3t<T>::operator+(const v3t<T> &src) const
140 {
141  return v3t(this->x + src.x, this->y + src.y, this->z + src.z);
142 }
143 
144 
145 template<class T> inline int v3t<T>::operator==(const v3t<T> &s) const
146 {
147  if (this->x == s.x && this->y == s.y && this->z == s.z) {
148  return 1;
149  } else {
150  return 0;
151  }
152 }
153 
154 
155 template<class T> inline int v3t<T>::operator!=(const v3t<T> &s) const
156 {
157  return !(*this == s);
158 }
159 
160 
161 // Approximately equality per component.
162 template<class T> inline int v3t<T>::approxEquals(const v3t<T> &cmp, T eps)
163 {
164  if (fabs(cmp.x - this->x) < eps &&
165  fabs(cmp.y - this->y) < eps &&
166  fabs(cmp.z - this->z) < eps)
167  {
168  return 1;
169  } else {
170  return 0;
171  }
172 }
173 
174 
175 template<class T> inline v3t<T> v3t<T>::operator*(const T s) const
176 {
177  return v3t(s*this->x, s*this->y, s*this->z);
178 }
179 
180 
181 template<class T> inline T v3t<T>::operator*(const v3t<T> &src) const
182 {
183  return this->x*src.x + this->y*src.y + this->z*src.z;
184 }
185 
186 
187 template<class T> inline v3t<T> v3t<T>::operator/(const T s) const
188 {
189  return v3t(this->x/s, this->y/s, this->z/s);
190 }
191 
192 
193 template<class T> inline T v3t<T>::len(void)
194 {
195  return sqrt(x*x + y*y + z*z);
196 }
197 
198 
199 template<class T> inline void v3t<T>::normalize(void)
200 {
201  T len = this->len();
202  x /= len; y /= len; z /= len;
203 }
204 
205 
206 template<class T> inline void v3t<T>::crossProduct(const v3t<T>& b, v3t<T>& r) const
207 {
208  r.x = this->y*b.z - this->z*b.y;
209  r.y = this->z*b.x - this->x*b.z;
210  r.z = this->x*b.y - this->y*b.x;
211 }
212 
213 
214 template<class T> inline void v3t<T>::dirVector(const v3t<T>* b, v3t<T>* r)
215 {
216  r->x = this->x - b->x;
217  r->y = this->y - b->y;
218  r->z = this->z - b->z;
219 }
220 
221 
222 // Friends.
223 
224 // Friend, scalar*vector.
225 template<class T> inline v3t<T> operator*(const T s, const v3t<T> & src)
226 {
227  return v3t<T>(s*src.x, s*src.y, s*src.z);
228 }
229 
230 
231 // Friend, Vector cross product.
232 template<class T> inline v3t<T> crossProduct(const v3t<T>& a, const v3t<T>& b)
233 {
234  return v3t<T>(a.y*b.z - a.z*b.y,
235  a.z*b.x - a.x*b.z,
236  a.x*b.y - a.y*b.x);
237 }
238 
239 
240 #endif // _LINALG_V3_T_H_
241 
v3t< T > & operator-=(const v3t< T > &src)
Definition: v3_t.h:104
friend v3t< T > operator*(const T s, const v3t< T > &src)
Definition: v3_t.h:225
v3t< T > & operator+=(const v3t< T > &src)
Definition: v3_t.h:96
v3t< T > operator+(const v3t< T > &src) const
Definition: v3_t.h:139
v3t< T > operator/(const T s) const
Definition: v3_t.h:187
v3t< T > crossProduct(const v3t< T > &a, const v3t< T > &b)
Definition: v3_t.h:232
T z
Definition: v3_t.h:82
v3t< T > operator-(void) const
Definition: v3_t.h:127
v3t()
Definition: v3_t.h:49
void normalize(void)
Definition: v3_t.h:199
v3t< T > & operator/=(const T s)
Definition: v3_t.h:120
v3t(const T x, const T y, const T z)
Definition: v3_t.h:51
T y
Definition: v3_t.h:82
v3t< T > & operator*=(const T s)
Definition: v3_t.h:112
int operator==(const v3t< T > &src) const
Definition: v3_t.h:145
T vec[3]
Definition: v3_t.h:83
Definition: v3_t.h:33
int approxEquals(const v3t< T > &cmp, T eps)
Definition: v3_t.h:162
T len(void)
Definition: v3_t.h:193
void dirVector(const v3t< T > *b, v3t< T > *r)
Definition: v3_t.h:214
static Vector y[4]
Definition: Convex.cpp:56
v3t< T > & operator=(const v3t< T > &src)
Definition: v3_t.h:88
v3t(const v3t< T > &src)
Definition: v3_t.h:50
int operator!=(const v3t< T > &src) const
Definition: v3_t.h:155
v3t< T > operator*(const T s, const v3t< T > &src)
Definition: v3_t.h:225
v3t(const T s)
Definition: v3_t.h:52
T x
Definition: v3_t.h:82
friend v3t< T > crossProduct(const v3t< T > &a, const v3t< T > &b)
Definition: v3_t.h:232