# | Line 55 | Line 55 | namespace oopse { | |
---|---|---|
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" | |
# | Line 64 | 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 93 | Line 97 | namespace oopse { | |
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 | } | |
# | Line 103 | 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 113 | 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 123 | 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 133 | 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 | + | /** 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 | |
# | Line 184 | Line 200 | namespace oopse { | |
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). | |
# | Line 221 | Line 237 | namespace oopse { | |
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 | } | |
# | Line 232 | Line 248 | namespace oopse { | |
248 | * @param v1 the vector | |
249 | * @param s the scalar value | |
250 | */ | |
251 | < | inline void mul( const Vector<Real, Dim>& v1, double s) { |
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 | } | |
# | Line 241 | Line 257 | namespace oopse { | |
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 | } | |
# | Line 251 | Line 267 | namespace oopse { | |
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 | } | |
# | Line 269 | Line 285 | namespace oopse { | |
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 | } | |
# | Line 284 | Line 300 | namespace oopse { | |
300 | * Returns the length of this vector. | |
301 | * @return the length of this vector | |
302 | */ | |
303 | < | inline double length() { |
303 | > | inline Real length() { |
304 | return sqrt(lengthSquare()); | |
305 | } | |
306 | ||
# | Line 292 | Line 308 | namespace oopse { | |
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 | ||
# | Line 317 | Line 333 | namespace oopse { | |
333 | } | |
334 | ||
335 | protected: | |
336 | < | double data_[Dim]; |
336 | > | Real data_[Dim]; |
337 | ||
338 | }; | |
339 | ||
# | Line 363 | Line 379 | namespace oopse { | |
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(v1,s); | |
385 | return result; | |
# | Line 376 | Line 392 | namespace oopse { | |
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(v1, s); | |
398 | return result; | |
# | Line 389 | Line 405 | namespace oopse { | |
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; | |
# | Line 407 | Line 423 | namespace oopse { | |
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 | } | |
# | Line 442 | Line 458 | namespace oopse { | |
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 |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |