39 |
|
|
40 |
|
namespace oopse { |
41 |
|
|
42 |
+ |
const double epsilon = 0.000001; |
43 |
+ |
|
44 |
+ |
template<typename T> |
45 |
+ |
inline bool equal(T e1, T e2) { |
46 |
+ |
return e1 == e2; |
47 |
+ |
} |
48 |
+ |
|
49 |
+ |
template<> |
50 |
+ |
inline bool equal(float e1, float e2) { |
51 |
+ |
return fabs(e1 - e2) < epsilon; |
52 |
+ |
} |
53 |
+ |
|
54 |
+ |
template<> |
55 |
+ |
inline bool equal(double e1, double e2) { |
56 |
+ |
return fabs(e1 - e2) < epsilon; |
57 |
+ |
} |
58 |
+ |
|
59 |
+ |
|
60 |
|
/** |
61 |
|
* @class Vector Vector.hpp "math/Vector.hpp" |
62 |
|
* @brief Fix length vector class |
86 |
|
|
87 |
|
return *this; |
88 |
|
} |
89 |
+ |
|
90 |
+ |
template<typename T> |
91 |
+ |
inline Vector(const T& s){ |
92 |
+ |
for (unsigned int i = 0; i < Dim; i++) |
93 |
+ |
data_[i] = s; |
94 |
+ |
} |
95 |
|
|
96 |
|
/** Constructs and initializes a Vector from an array */ |
97 |
< |
inline Vector( double* v) { |
98 |
< |
for (unsigned int i = 0; i < Dim; i++) |
99 |
< |
data_[i] = v[i]; |
97 |
> |
inline Vector( Real* v) { |
98 |
> |
for (unsigned int i = 0; i < Dim; i++) |
99 |
> |
data_[i] = v[i]; |
100 |
|
} |
101 |
|
|
102 |
|
/** |
104 |
|
* @return reference of ith element |
105 |
|
* @param i index |
106 |
|
*/ |
107 |
< |
inline double& operator[](unsigned int i) { |
107 |
> |
inline Real& operator[](unsigned int i) { |
108 |
|
assert( i < Dim); |
109 |
|
return data_[i]; |
110 |
|
} |
114 |
|
* @return reference of ith element |
115 |
|
* @param i index |
116 |
|
*/ |
117 |
< |
inline double& operator()(unsigned int i) { |
117 |
> |
inline Real& operator()(unsigned int i) { |
118 |
|
assert( i < Dim); |
119 |
|
return data_[i]; |
120 |
|
} |
124 |
|
* @return reference of ith element |
125 |
|
* @param i index |
126 |
|
*/ |
127 |
< |
inline const double& operator[](unsigned int i) const { |
127 |
> |
inline const Real& operator[](unsigned int i) const { |
128 |
|
assert( i < Dim); |
129 |
|
return data_[i]; |
130 |
|
} |
134 |
|
* @return reference of ith element |
135 |
|
* @param i index |
136 |
|
*/ |
137 |
< |
inline const double& operator()(unsigned int i) const { |
137 |
> |
inline const Real& operator()(unsigned int i) const { |
138 |
|
assert( i < Dim); |
139 |
|
return data_[i]; |
140 |
|
} |
141 |
|
|
142 |
+ |
/** |
143 |
+ |
* Tests if this vetor is equal to other vector |
144 |
+ |
* @return true if equal, otherwise return false |
145 |
+ |
* @param v vector to be compared |
146 |
+ |
*/ |
147 |
+ |
inline bool operator ==(const Vector<Real, Dim>& v) { |
148 |
+ |
|
149 |
+ |
for (unsigned int i = 0; i < Dim; i ++) { |
150 |
+ |
if (!equal(data_[i], v[i])) { |
151 |
+ |
return false; |
152 |
+ |
} |
153 |
+ |
} |
154 |
+ |
|
155 |
+ |
return true; |
156 |
+ |
} |
157 |
+ |
|
158 |
+ |
/** |
159 |
+ |
* Tests if this vetor is not equal to other vector |
160 |
+ |
* @return true if equal, otherwise return false |
161 |
+ |
* @param v vector to be compared |
162 |
+ |
*/ |
163 |
+ |
inline bool operator !=(const Vector<Real, Dim>& v) { |
164 |
+ |
return !(*this == v); |
165 |
+ |
} |
166 |
+ |
|
167 |
|
/** Negates the value of this vector in place. */ |
168 |
|
inline void negate() { |
169 |
< |
data_[0] = -data_[0]; |
170 |
< |
data_[1] = -data_[1]; |
122 |
< |
data_[2] = -data_[2]; |
169 |
> |
for (unsigned int i = 0; i < Dim; i++) |
170 |
> |
data_[i] = -data_[i]; |
171 |
|
} |
172 |
|
|
173 |
|
/** |
185 |
|
* @param v1 the other vector |
186 |
|
*/ |
187 |
|
inline void add( const Vector<Real, Dim>& v1 ) { |
188 |
< |
for (unsigned int i = 0; i < Dim; i++) |
189 |
< |
data_[i] += v1.data_[i]; |
190 |
< |
} |
188 |
> |
for (unsigned int i = 0; i < Dim; i++) |
189 |
> |
data_[i] += v1.data_[i]; |
190 |
> |
} |
191 |
|
|
192 |
|
/** |
193 |
|
* Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). |
195 |
|
* @param v2 the second vector |
196 |
|
*/ |
197 |
|
inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
198 |
< |
for (unsigned int i = 0; i < Dim; i++) |
199 |
< |
data_[i] = v1.data_[i] + v2.data_[i]; |
198 |
> |
for (unsigned int i = 0; i < Dim; i++) |
199 |
> |
data_[i] = v1.data_[i] + v2.data_[i]; |
200 |
|
} |
201 |
|
|
202 |
|
/** |
222 |
|
* Sets the value of this vector to the scalar multiplication of itself (*this *= s). |
223 |
|
* @param s the scalar value |
224 |
|
*/ |
225 |
< |
inline void mul( double s ) { |
226 |
< |
for (unsigned int i = 0; i < Dim; i++) |
225 |
> |
inline void mul( Real s ) { |
226 |
> |
for (unsigned int i = 0; i < Dim; i++) |
227 |
|
data_[i] *= s; |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
|
* Sets the value of this vector to the scalar multiplication of vector v1 |
232 |
|
* (*this = s * v1). |
233 |
+ |
* @param v1 the vector |
234 |
|
* @param s the scalar value |
186 |
– |
* @param v1 the vector |
235 |
|
*/ |
236 |
< |
inline void mul( double s, const Vector<Real, Dim>& v1 ) { |
237 |
< |
for (unsigned int i = 0; i < Dim; i++) |
236 |
> |
inline void mul( const Vector<Real, Dim>& v1, Real s) { |
237 |
> |
for (unsigned int i = 0; i < Dim; i++) |
238 |
|
data_[i] = s * v1.data_[i]; |
239 |
|
} |
240 |
|
|
242 |
|
* Sets the value of this vector to the scalar division of itself (*this /= s ). |
243 |
|
* @param s the scalar value |
244 |
|
*/ |
245 |
< |
inline void div( double s) { |
246 |
< |
for (unsigned int i = 0; i < Dim; i++) |
245 |
> |
inline void div( Real s) { |
246 |
> |
for (unsigned int i = 0; i < Dim; i++) |
247 |
|
data_[i] /= s; |
248 |
|
} |
249 |
|
|
252 |
|
* @param v1 the source vector |
253 |
|
* @param s the scalar value |
254 |
|
*/ |
255 |
< |
inline void div( const Vector<Real, Dim>& v1, double s ) { |
256 |
< |
for (unsigned int i = 0; i < Dim; i++) |
255 |
> |
inline void div( const Vector<Real, Dim>& v1, Real s ) { |
256 |
> |
for (unsigned int i = 0; i < Dim; i++) |
257 |
|
data_[i] = v1.data_[i] / s; |
258 |
|
} |
259 |
|
|
270 |
|
} |
271 |
|
|
272 |
|
/** @see #mul */ |
273 |
< |
inline Vector<Real, Dim>& operator *=( double s) { |
273 |
> |
inline Vector<Real, Dim>& operator *=( Real s) { |
274 |
|
mul(s); |
275 |
|
return *this; |
276 |
|
} |
277 |
|
|
278 |
|
/** @see #div */ |
279 |
< |
inline Vector<Real, Dim>& operator /=( double s ) { |
279 |
> |
inline Vector<Real, Dim>& operator /=( Real s ) { |
280 |
|
div(s); |
281 |
|
return *this; |
282 |
|
} |
285 |
|
* Returns the length of this vector. |
286 |
|
* @return the length of this vector |
287 |
|
*/ |
288 |
< |
inline double length() { |
289 |
< |
return sqrt(lengthSquared()); |
288 |
> |
inline Real length() { |
289 |
> |
return sqrt(lengthSquare()); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
|
* Returns the squared length of this vector. |
294 |
|
* @return the squared length of this vector |
295 |
|
*/ |
296 |
< |
inline double lengthSquared() { |
296 |
> |
inline Real lengthSquare() { |
297 |
|
return dot(*this, *this); |
298 |
|
} |
299 |
|
|
300 |
|
/** Normalizes this vector in place */ |
301 |
|
inline void normalize() { |
302 |
< |
double len; |
302 |
> |
Real len; |
303 |
|
|
304 |
|
len = length(); |
305 |
+ |
|
306 |
+ |
//if (len < oopse:epsilon) |
307 |
+ |
// throw(); |
308 |
+ |
|
309 |
|
*this /= len; |
310 |
|
} |
311 |
+ |
|
312 |
+ |
/** |
313 |
+ |
* Tests if this vector is normalized |
314 |
+ |
* @return true if this vector is normalized, otherwise return false |
315 |
+ |
*/ |
316 |
+ |
inline bool isNormalized() { |
317 |
+ |
return equal(lengthSquare(), 1.0); |
318 |
+ |
} |
319 |
|
|
320 |
|
protected: |
321 |
< |
double data_[3]; |
321 |
> |
Real data_[Dim]; |
322 |
|
|
323 |
|
}; |
324 |
|
|
325 |
|
/** unary minus*/ |
326 |
|
template<typename Real, unsigned int Dim> |
327 |
|
inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ |
328 |
< |
Vector tmp(v1); |
329 |
< |
return tmp.negate(); |
328 |
> |
Vector<Real, Dim> tmp(v1); |
329 |
> |
tmp.negate(); |
330 |
> |
return tmp; |
331 |
|
} |
332 |
|
|
333 |
|
/** |
364 |
|
* @param s the scalar value |
365 |
|
*/ |
366 |
|
template<typename Real, unsigned int Dim> |
367 |
< |
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { |
367 |
> |
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) { |
368 |
|
Vector<Real, Dim> result; |
369 |
< |
result.mul(s, v1); |
369 |
> |
result.mul(v1,s); |
370 |
|
return result; |
371 |
|
} |
372 |
|
|
377 |
|
* @param v1 the source vector |
378 |
|
*/ |
379 |
|
template<typename Real, unsigned int Dim> |
380 |
< |
Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { |
380 |
> |
Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) { |
381 |
|
Vector<Real, Dim> result; |
382 |
< |
result.mul(s, v1); |
382 |
> |
result.mul(v1, s); |
383 |
|
return result; |
384 |
|
} |
385 |
|
|
390 |
|
* @param s the scalar value |
391 |
|
*/ |
392 |
|
template<typename Real, unsigned int Dim> |
393 |
< |
Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) { |
393 |
> |
Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) { |
394 |
|
Vector<Real, Dim> result; |
395 |
|
result.div( v1,s); |
396 |
|
return result; |
397 |
|
} |
398 |
|
|
399 |
|
/** |
339 |
– |
* Returns the value of division of a vector by a scalar. |
340 |
– |
* @return the vaule of scalar division of this vector |
341 |
– |
* @param s the scalar value |
342 |
– |
* @param v1 the source vector |
343 |
– |
*/ |
344 |
– |
template<typename Real, unsigned int Dim> |
345 |
– |
inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) { |
346 |
– |
Vector<Real, Dim> result; |
347 |
– |
result.div( v1,s); |
348 |
– |
return result; |
349 |
– |
} |
350 |
– |
|
351 |
– |
/** fuzzy comparson */ |
352 |
– |
template<typename Real, unsigned int Dim> |
353 |
– |
inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
354 |
– |
|
355 |
– |
} |
356 |
– |
|
357 |
– |
|
358 |
– |
/** |
400 |
|
* Returns the dot product of two Vectors |
401 |
|
* @param v1 first vector |
402 |
|
* @param v2 second vector |
404 |
|
*/ |
405 |
|
template<typename Real, unsigned int Dim> |
406 |
|
inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
407 |
< |
Real tmp; |
408 |
< |
tmp = 0; |
407 |
> |
Real tmp; |
408 |
> |
tmp = 0; |
409 |
|
|
410 |
< |
for (unsigned int i = 0; i < Dim; i++) |
411 |
< |
tmp += v1[i] + v2[i]; |
412 |
< |
|
413 |
< |
return tmp; |
410 |
> |
for (unsigned int i = 0; i < Dim; i++) |
411 |
> |
tmp += v1[i] * v2[i]; |
412 |
> |
|
413 |
> |
return tmp; |
414 |
|
} |
415 |
|
|
416 |
|
/** |
441 |
|
* Write to an output stream |
442 |
|
*/ |
443 |
|
template<typename Real, unsigned int Dim> |
444 |
< |
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { |
444 |
> |
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { |
445 |
> |
|
446 |
> |
o << "[ "; |
447 |
|
|
448 |
+ |
for (unsigned int i = 0 ; i< Dim; i++) { |
449 |
+ |
o << v[i]; |
450 |
+ |
|
451 |
+ |
if (i != Dim -1) { |
452 |
+ |
o<< ", "; |
453 |
+ |
} |
454 |
+ |
} |
455 |
+ |
|
456 |
+ |
o << " ]"; |
457 |
|
return o; |
458 |
|
} |
459 |
|
|