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" |
65 |
|
class Vector{ |
66 |
|
public: |
67 |
|
|
68 |
+ |
typedef Real ElemType; |
69 |
+ |
typedef Real* ElemPoinerType; |
70 |
+ |
|
71 |
|
/** default constructor */ |
72 |
|
inline Vector(){ |
73 |
|
for (unsigned int i = 0; i < Dim; i++) |
89 |
|
|
90 |
|
return *this; |
91 |
|
} |
92 |
+ |
|
93 |
+ |
template<typename T> |
94 |
+ |
inline Vector(const T& s){ |
95 |
+ |
for (unsigned int i = 0; i < Dim; i++) |
96 |
+ |
data_[i] = s; |
97 |
+ |
} |
98 |
|
|
99 |
|
/** Constructs and initializes a Vector from an array */ |
100 |
< |
inline Vector( double* v) { |
100 |
> |
inline Vector( Real* v) { |
101 |
|
for (unsigned int i = 0; i < Dim; i++) |
102 |
|
data_[i] = v[i]; |
103 |
|
} |
107 |
|
* @return reference of ith element |
108 |
|
* @param i index |
109 |
|
*/ |
110 |
< |
inline double& operator[](unsigned int i) { |
110 |
> |
inline Real& operator[](unsigned int i) { |
111 |
|
assert( i < Dim); |
112 |
|
return data_[i]; |
113 |
|
} |
117 |
|
* @return reference of ith element |
118 |
|
* @param i index |
119 |
|
*/ |
120 |
< |
inline double& operator()(unsigned int i) { |
120 |
> |
inline Real& operator()(unsigned int i) { |
121 |
|
assert( i < Dim); |
122 |
|
return data_[i]; |
123 |
|
} |
127 |
|
* @return reference of ith element |
128 |
|
* @param i index |
129 |
|
*/ |
130 |
< |
inline const double& operator[](unsigned int i) const { |
130 |
> |
inline const Real& operator[](unsigned int i) const { |
131 |
|
assert( i < Dim); |
132 |
|
return data_[i]; |
133 |
|
} |
137 |
|
* @return reference of ith element |
138 |
|
* @param i index |
139 |
|
*/ |
140 |
< |
inline const double& operator()(unsigned int i) const { |
140 |
> |
inline const Real& operator()(unsigned int i) const { |
141 |
|
assert( i < Dim); |
142 |
|
return data_[i]; |
143 |
|
} |
144 |
|
|
145 |
+ |
/** Copy the internal data to an array*/ |
146 |
+ |
void getArray(Real* array) { |
147 |
+ |
for (unsigned int i = 0; i < Dim; i ++) { |
148 |
+ |
array[i] = data_[i]; |
149 |
+ |
} |
150 |
+ |
} |
151 |
+ |
|
152 |
+ |
/** Returns the pointer of internal array */ |
153 |
+ |
Real* getArrayPointer() { |
154 |
+ |
return data_; |
155 |
+ |
} |
156 |
+ |
|
157 |
|
/** |
158 |
|
* Tests if this vetor is equal to other vector |
159 |
|
* @return true if equal, otherwise return false |
181 |
|
|
182 |
|
/** Negates the value of this vector in place. */ |
183 |
|
inline void negate() { |
184 |
< |
data_[0] = -data_[0]; |
185 |
< |
data_[1] = -data_[1]; |
164 |
< |
data_[2] = -data_[2]; |
184 |
> |
for (unsigned int i = 0; i < Dim; i++) |
185 |
> |
data_[i] = -data_[i]; |
186 |
|
} |
187 |
|
|
188 |
|
/** |
200 |
|
* @param v1 the other vector |
201 |
|
*/ |
202 |
|
inline void add( const Vector<Real, Dim>& v1 ) { |
203 |
< |
for (unsigned int i = 0; i < Dim; i++) |
204 |
< |
data_[i] += v1.data_[i]; |
205 |
< |
} |
203 |
> |
for (unsigned int i = 0; i < Dim; i++) |
204 |
> |
data_[i] += v1.data_[i]; |
205 |
> |
} |
206 |
|
|
207 |
|
/** |
208 |
|
* Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). |
237 |
|
* Sets the value of this vector to the scalar multiplication of itself (*this *= s). |
238 |
|
* @param s the scalar value |
239 |
|
*/ |
240 |
< |
inline void mul( double s ) { |
240 |
> |
inline void mul( Real s ) { |
241 |
|
for (unsigned int i = 0; i < Dim; i++) |
242 |
|
data_[i] *= s; |
243 |
|
} |
245 |
|
/** |
246 |
|
* Sets the value of this vector to the scalar multiplication of vector v1 |
247 |
|
* (*this = s * v1). |
248 |
+ |
* @param v1 the vector |
249 |
|
* @param s the scalar value |
228 |
– |
* @param v1 the vector |
250 |
|
*/ |
251 |
< |
inline void mul( double s, const Vector<Real, Dim>& v1 ) { |
251 |
> |
inline void mul( const Vector<Real, Dim>& v1, Real s) { |
252 |
|
for (unsigned int i = 0; i < Dim; i++) |
253 |
|
data_[i] = s * v1.data_[i]; |
254 |
|
} |
257 |
|
* Sets the value of this vector to the scalar division of itself (*this /= s ). |
258 |
|
* @param s the scalar value |
259 |
|
*/ |
260 |
< |
inline void div( double s) { |
260 |
> |
inline void div( Real s) { |
261 |
|
for (unsigned int i = 0; i < Dim; i++) |
262 |
|
data_[i] /= s; |
263 |
|
} |
267 |
|
* @param v1 the source vector |
268 |
|
* @param s the scalar value |
269 |
|
*/ |
270 |
< |
inline void div( const Vector<Real, Dim>& v1, double s ) { |
270 |
> |
inline void div( const Vector<Real, Dim>& v1, Real s ) { |
271 |
|
for (unsigned int i = 0; i < Dim; i++) |
272 |
|
data_[i] = v1.data_[i] / s; |
273 |
|
} |
285 |
|
} |
286 |
|
|
287 |
|
/** @see #mul */ |
288 |
< |
inline Vector<Real, Dim>& operator *=( double s) { |
288 |
> |
inline Vector<Real, Dim>& operator *=( Real s) { |
289 |
|
mul(s); |
290 |
|
return *this; |
291 |
|
} |
292 |
|
|
293 |
|
/** @see #div */ |
294 |
< |
inline Vector<Real, Dim>& operator /=( double s ) { |
294 |
> |
inline Vector<Real, Dim>& operator /=( Real s ) { |
295 |
|
div(s); |
296 |
|
return *this; |
297 |
|
} |
300 |
|
* Returns the length of this vector. |
301 |
|
* @return the length of this vector |
302 |
|
*/ |
303 |
< |
inline double length() { |
304 |
< |
return sqrt(lengthSquared()); |
303 |
> |
inline Real length() { |
304 |
> |
return sqrt(lengthSquare()); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
|
* Returns the squared length of this vector. |
309 |
|
* @return the squared length of this vector |
310 |
|
*/ |
311 |
< |
inline double lengthSquare() { |
311 |
> |
inline Real lengthSquare() { |
312 |
|
return dot(*this, *this); |
313 |
|
} |
314 |
|
|
315 |
|
/** Normalizes this vector in place */ |
316 |
|
inline void normalize() { |
317 |
< |
double len; |
317 |
> |
Real len; |
318 |
|
|
319 |
|
len = length(); |
320 |
+ |
|
321 |
+ |
//if (len < oopse:epsilon) |
322 |
+ |
// throw(); |
323 |
+ |
|
324 |
|
*this /= len; |
325 |
|
} |
326 |
|
|
328 |
|
* Tests if this vector is normalized |
329 |
|
* @return true if this vector is normalized, otherwise return false |
330 |
|
*/ |
331 |
< |
inline bool isNormalized() const |
332 |
< |
{ |
308 |
< |
return lengthSquare() == 1.0; |
331 |
> |
inline bool isNormalized() { |
332 |
> |
return equal(lengthSquare(), 1.0); |
333 |
|
} |
334 |
|
|
335 |
|
protected: |
336 |
< |
double data_[3]; |
336 |
> |
Real data_[Dim]; |
337 |
|
|
338 |
|
}; |
339 |
|
|
340 |
|
/** unary minus*/ |
341 |
|
template<typename Real, unsigned int Dim> |
342 |
|
inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ |
343 |
< |
Vector tmp(v1); |
344 |
< |
return tmp.negate(); |
343 |
> |
Vector<Real, Dim> tmp(v1); |
344 |
> |
tmp.negate(); |
345 |
> |
return tmp; |
346 |
|
} |
347 |
|
|
348 |
|
/** |
379 |
|
* @param s the scalar value |
380 |
|
*/ |
381 |
|
template<typename Real, unsigned int Dim> |
382 |
< |
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { |
382 |
> |
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) { |
383 |
|
Vector<Real, Dim> result; |
384 |
< |
result.mul(s, v1); |
384 |
> |
result.mul(v1,s); |
385 |
|
return result; |
386 |
|
} |
387 |
|
|
392 |
|
* @param v1 the source vector |
393 |
|
*/ |
394 |
|
template<typename Real, unsigned int Dim> |
395 |
< |
Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { |
395 |
> |
Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) { |
396 |
|
Vector<Real, Dim> result; |
397 |
< |
result.mul(s, v1); |
397 |
> |
result.mul(v1, s); |
398 |
|
return result; |
399 |
|
} |
400 |
|
|
405 |
|
* @param s the scalar value |
406 |
|
*/ |
407 |
|
template<typename Real, unsigned int Dim> |
408 |
< |
Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) { |
408 |
> |
Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) { |
409 |
|
Vector<Real, Dim> result; |
410 |
|
result.div( v1,s); |
411 |
|
return result; |
412 |
|
} |
413 |
|
|
414 |
|
/** |
390 |
– |
* Returns the value of division of a vector by a scalar. |
391 |
– |
* @return the vaule of scalar division of this vector |
392 |
– |
* @param s the scalar value |
393 |
– |
* @param v1 the source vector |
394 |
– |
*/ |
395 |
– |
template<typename Real, unsigned int Dim> |
396 |
– |
inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) { |
397 |
– |
Vector<Real, Dim> result; |
398 |
– |
result.div( v1,s); |
399 |
– |
return result; |
400 |
– |
} |
401 |
– |
|
402 |
– |
/** fuzzy comparson */ |
403 |
– |
template<typename Real, unsigned int Dim> |
404 |
– |
inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
405 |
– |
|
406 |
– |
} |
407 |
– |
|
408 |
– |
|
409 |
– |
/** |
415 |
|
* Returns the dot product of two Vectors |
416 |
|
* @param v1 first vector |
417 |
|
* @param v2 second vector |
423 |
|
tmp = 0; |
424 |
|
|
425 |
|
for (unsigned int i = 0; i < Dim; i++) |
426 |
< |
tmp += v1[i] + v2[i]; |
426 |
> |
tmp += v1[i] * v2[i]; |
427 |
|
|
428 |
|
return tmp; |
429 |
|
} |
458 |
|
template<typename Real, unsigned int Dim> |
459 |
|
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { |
460 |
|
|
461 |
< |
o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; |
461 |
> |
o << "[ "; |
462 |
> |
|
463 |
> |
for (unsigned int i = 0 ; i< Dim; i++) { |
464 |
> |
o << v[i]; |
465 |
> |
|
466 |
> |
if (i != Dim -1) { |
467 |
> |
o<< ", "; |
468 |
> |
} |
469 |
> |
} |
470 |
> |
|
471 |
> |
o << " ]"; |
472 |
|
return o; |
473 |
|
} |
474 |
|
|