# | Line 39 | Line 39 | namespace oopse { | |
---|---|---|
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 | |
# | Line 47 | Line 65 | namespace oopse { | |
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++) | |
# | Line 68 | Line 89 | namespace oopse { | |
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) { |
101 | < | for (unsigned int i = 0; i < Dim; i++) |
102 | < | data_[i] = v[i]; |
100 | > | inline Vector( Real* v) { |
101 | > | for (unsigned int i = 0; i < Dim; i++) |
102 | > | data_[i] = v[i]; |
103 | } | |
104 | ||
105 | /** | |
# | Line 80 | Line 107 | namespace oopse { | |
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 | } | |
# | Line 90 | Line 117 | namespace oopse { | |
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 | } | |
# | Line 100 | Line 127 | namespace oopse { | |
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 | } | |
# | Line 110 | Line 137 | namespace oopse { | |
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 | + | /** Returns the pointer of internal array */ |
146 | + | Real* getArrayPointer() { |
147 | + | return data_; |
148 | + | } |
149 | + | |
150 | + | /** |
151 | + | * Tests if this vetor is equal to other vector |
152 | + | * @return true if equal, otherwise return false |
153 | + | * @param v vector to be compared |
154 | + | */ |
155 | + | inline bool operator ==(const Vector<Real, Dim>& v) { |
156 | + | |
157 | + | for (unsigned int i = 0; i < Dim; i ++) { |
158 | + | if (!equal(data_[i], v[i])) { |
159 | + | return false; |
160 | + | } |
161 | + | } |
162 | + | |
163 | + | return true; |
164 | + | } |
165 | + | |
166 | + | /** |
167 | + | * Tests if this vetor is not equal to other vector |
168 | + | * @return true if equal, otherwise return false |
169 | + | * @param v vector to be compared |
170 | + | */ |
171 | + | inline bool operator !=(const Vector<Real, Dim>& v) { |
172 | + | return !(*this == v); |
173 | + | } |
174 | + | |
175 | /** Negates the value of this vector in place. */ | |
176 | inline void negate() { | |
177 | < | data_[0] = -data_[0]; |
178 | < | data_[1] = -data_[1]; |
122 | < | data_[2] = -data_[2]; |
177 | > | for (unsigned int i = 0; i < Dim; i++) |
178 | > | data_[i] = -data_[i]; |
179 | } | |
180 | ||
181 | /** | |
# | Line 137 | Line 193 | namespace oopse { | |
193 | * @param v1 the other vector | |
194 | */ | |
195 | inline void add( const Vector<Real, Dim>& v1 ) { | |
196 | < | for (unsigned int i = 0; i < Dim; i++) |
197 | < | data_[i] += v1.data_[i]; |
198 | < | } |
196 | > | for (unsigned int i = 0; i < Dim; i++) |
197 | > | data_[i] += v1.data_[i]; |
198 | > | } |
199 | ||
200 | /** | |
201 | * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). | |
# | Line 147 | Line 203 | namespace oopse { | |
203 | * @param v2 the second vector | |
204 | */ | |
205 | inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | |
206 | < | for (unsigned int i = 0; i < Dim; i++) |
207 | < | data_[i] = v1.data_[i] + v2.data_[i]; |
206 | > | for (unsigned int i = 0; i < Dim; i++) |
207 | > | data_[i] = v1.data_[i] + v2.data_[i]; |
208 | } | |
209 | ||
210 | /** | |
# | Line 174 | Line 230 | namespace oopse { | |
230 | * Sets the value of this vector to the scalar multiplication of itself (*this *= s). | |
231 | * @param s the scalar value | |
232 | */ | |
233 | < | inline void mul( double s ) { |
234 | < | for (unsigned int i = 0; i < Dim; i++) |
233 | > | inline void mul( Real s ) { |
234 | > | for (unsigned int i = 0; i < Dim; i++) |
235 | data_[i] *= s; | |
236 | } | |
237 | ||
238 | /** | |
239 | * Sets the value of this vector to the scalar multiplication of vector v1 | |
240 | * (*this = s * v1). | |
241 | + | * @param v1 the vector |
242 | * @param s the scalar value | |
186 | – | * @param v1 the vector |
243 | */ | |
244 | < | inline void mul( double s, const Vector<Real, Dim>& v1 ) { |
245 | < | for (unsigned int i = 0; i < Dim; i++) |
244 | > | inline void mul( const Vector<Real, Dim>& v1, Real s) { |
245 | > | for (unsigned int i = 0; i < Dim; i++) |
246 | data_[i] = s * v1.data_[i]; | |
247 | } | |
248 | ||
# | Line 194 | Line 250 | namespace oopse { | |
250 | * Sets the value of this vector to the scalar division of itself (*this /= s ). | |
251 | * @param s the scalar value | |
252 | */ | |
253 | < | inline void div( double s) { |
254 | < | for (unsigned int i = 0; i < Dim; i++) |
253 | > | inline void div( Real s) { |
254 | > | for (unsigned int i = 0; i < Dim; i++) |
255 | data_[i] /= s; | |
256 | } | |
257 | ||
# | Line 204 | Line 260 | namespace oopse { | |
260 | * @param v1 the source vector | |
261 | * @param s the scalar value | |
262 | */ | |
263 | < | inline void div( const Vector<Real, Dim>& v1, double s ) { |
264 | < | for (unsigned int i = 0; i < Dim; i++) |
263 | > | inline void div( const Vector<Real, Dim>& v1, Real s ) { |
264 | > | for (unsigned int i = 0; i < Dim; i++) |
265 | data_[i] = v1.data_[i] / s; | |
266 | } | |
267 | ||
# | Line 222 | Line 278 | namespace oopse { | |
278 | } | |
279 | ||
280 | /** @see #mul */ | |
281 | < | inline Vector<Real, Dim>& operator *=( double s) { |
281 | > | inline Vector<Real, Dim>& operator *=( Real s) { |
282 | mul(s); | |
283 | return *this; | |
284 | } | |
285 | ||
286 | /** @see #div */ | |
287 | < | inline Vector<Real, Dim>& operator /=( double s ) { |
287 | > | inline Vector<Real, Dim>& operator /=( Real s ) { |
288 | div(s); | |
289 | return *this; | |
290 | } | |
# | Line 237 | Line 293 | namespace oopse { | |
293 | * Returns the length of this vector. | |
294 | * @return the length of this vector | |
295 | */ | |
296 | < | inline double length() { |
297 | < | return sqrt(lengthSquared()); |
296 | > | inline Real length() { |
297 | > | return sqrt(lengthSquare()); |
298 | } | |
299 | ||
300 | /** | |
301 | * Returns the squared length of this vector. | |
302 | * @return the squared length of this vector | |
303 | */ | |
304 | < | inline double lengthSquared() { |
304 | > | inline Real lengthSquare() { |
305 | return dot(*this, *this); | |
306 | } | |
307 | ||
308 | /** Normalizes this vector in place */ | |
309 | inline void normalize() { | |
310 | < | double len; |
310 | > | Real len; |
311 | ||
312 | len = length(); | |
313 | + | |
314 | + | //if (len < oopse:epsilon) |
315 | + | // throw(); |
316 | + | |
317 | *this /= len; | |
318 | } | |
319 | + | |
320 | + | /** |
321 | + | * Tests if this vector is normalized |
322 | + | * @return true if this vector is normalized, otherwise return false |
323 | + | */ |
324 | + | inline bool isNormalized() { |
325 | + | return equal(lengthSquare(), 1.0); |
326 | + | } |
327 | ||
328 | protected: | |
329 | < | double data_[3]; |
329 | > | Real data_[Dim]; |
330 | ||
331 | }; | |
332 | ||
333 | /** unary minus*/ | |
334 | template<typename Real, unsigned int Dim> | |
335 | inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ | |
336 | < | Vector tmp(v1); |
337 | < | return tmp.negate(); |
336 | > | Vector<Real, Dim> tmp(v1); |
337 | > | tmp.negate(); |
338 | > | return tmp; |
339 | } | |
340 | ||
341 | /** | |
# | Line 303 | Line 372 | namespace oopse { | |
372 | * @param s the scalar value | |
373 | */ | |
374 | template<typename Real, unsigned int Dim> | |
375 | < | Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { |
375 | > | Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, Real s) { |
376 | Vector<Real, Dim> result; | |
377 | < | result.mul(s, v1); |
377 | > | result.mul(v1,s); |
378 | return result; | |
379 | } | |
380 | ||
# | Line 316 | Line 385 | namespace oopse { | |
385 | * @param v1 the source vector | |
386 | */ | |
387 | template<typename Real, unsigned int Dim> | |
388 | < | Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { |
388 | > | Vector<Real, Dim> operator * ( Real s, const Vector<Real, Dim>& v1 ) { |
389 | Vector<Real, Dim> result; | |
390 | < | result.mul(s, v1); |
390 | > | result.mul(v1, s); |
391 | return result; | |
392 | } | |
393 | ||
# | Line 329 | Line 398 | namespace oopse { | |
398 | * @param s the scalar value | |
399 | */ | |
400 | template<typename Real, unsigned int Dim> | |
401 | < | Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) { |
401 | > | Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, Real s) { |
402 | Vector<Real, Dim> result; | |
403 | result.div( v1,s); | |
404 | return result; | |
405 | } | |
406 | ||
407 | /** | |
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 | – | /** |
408 | * Returns the dot product of two Vectors | |
409 | * @param v1 first vector | |
410 | * @param v2 second vector | |
# | Line 363 | Line 412 | namespace oopse { | |
412 | */ | |
413 | template<typename Real, unsigned int Dim> | |
414 | inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { | |
415 | < | Real tmp; |
416 | < | tmp = 0; |
415 | > | Real tmp; |
416 | > | tmp = 0; |
417 | ||
418 | < | for (unsigned int i = 0; i < Dim; i++) |
419 | < | tmp += v1[i] + v2[i]; |
420 | < | |
421 | < | return tmp; |
418 | > | for (unsigned int i = 0; i < Dim; i++) |
419 | > | tmp += v1[i] * v2[i]; |
420 | > | |
421 | > | return tmp; |
422 | } | |
423 | ||
424 | /** | |
# | Line 400 | Line 449 | namespace oopse { | |
449 | * Write to an output stream | |
450 | */ | |
451 | template<typename Real, unsigned int Dim> | |
452 | < | std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { |
452 | > | std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { |
453 | > | |
454 | > | o << "[ "; |
455 | ||
456 | + | for (unsigned int i = 0 ; i< Dim; i++) { |
457 | + | o << v[i]; |
458 | + | |
459 | + | if (i != Dim -1) { |
460 | + | o<< ", "; |
461 | + | } |
462 | + | } |
463 | + | |
464 | + | o << " ]"; |
465 | return o; | |
466 | } | |
467 |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |