# | 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 | + | /** 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 | |
# | Line 184 | 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 221 | 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 ) { |
233 | > | inline void mul( Real s ) { |
234 | for (unsigned int i = 0; i < Dim; i++) | |
235 | data_[i] *= s; | |
236 | } | |
# | Line 232 | Line 241 | namespace oopse { | |
241 | * @param v1 the vector | |
242 | * @param s the scalar value | |
243 | */ | |
244 | < | inline void mul( const Vector<Real, Dim>& v1, double s) { |
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 | } | |
# | Line 241 | 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) { |
253 | > | inline void div( Real s) { |
254 | for (unsigned int i = 0; i < Dim; i++) | |
255 | data_[i] /= s; | |
256 | } | |
# | Line 251 | 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 ) { |
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 | } | |
# | Line 269 | 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 284 | Line 293 | namespace oopse { | |
293 | * Returns the length of this vector. | |
294 | * @return the length of this vector | |
295 | */ | |
296 | < | inline double length() { |
296 | > | inline Real length() { |
297 | return sqrt(lengthSquare()); | |
298 | } | |
299 | ||
# | Line 292 | Line 301 | namespace oopse { | |
301 | * Returns the squared length of this vector. | |
302 | * @return the squared length of this vector | |
303 | */ | |
304 | < | inline double lengthSquare() { |
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 | ||
# | Line 317 | Line 326 | namespace oopse { | |
326 | } | |
327 | ||
328 | protected: | |
329 | < | double data_[Dim]; |
329 | > | Real data_[Dim]; |
330 | ||
331 | }; | |
332 | ||
# | Line 363 | 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(v1,s); | |
378 | return result; | |
# | Line 376 | 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(v1, s); | |
391 | return result; | |
# | Line 389 | 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; | |
# | Line 407 | Line 416 | namespace oopse { | |
416 | tmp = 0; | |
417 | ||
418 | for (unsigned int i = 0; i < Dim; i++) | |
419 | < | tmp += v1[i] + v2[i]; |
419 | > | tmp += v1[i] * v2[i]; |
420 | ||
421 | return tmp; | |
422 | } | |
# | Line 442 | Line 451 | namespace oopse { | |
451 | template<typename Real, unsigned int Dim> | |
452 | std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { | |
453 | ||
454 | < | o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; |
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 |