| 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++) |
| 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 |
+ |
/** 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 |
| 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). |
| 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 |
|
} |
| 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 |
|
} |
| 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 |
|
} |
| 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 |
|
} |
| 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 |
|
} |
| 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 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 |
+ |
|
| 314 |
+ |
//if (len < oopse:epsilon) |
| 315 |
+ |
// throw(); |
| 316 |
+ |
|
| 317 |
|
*this /= len; |
| 318 |
|
} |
| 319 |
|
|
| 321 |
|
* Tests if this vector is normalized |
| 322 |
|
* @return true if this vector is normalized, otherwise return false |
| 323 |
|
*/ |
| 324 |
< |
inline bool isNormalized() const |
| 325 |
< |
{ |
| 313 |
< |
return lengthSquare() == 1.0; |
| 324 |
> |
inline bool isNormalized() { |
| 325 |
> |
return equal(lengthSquare(), 1.0); |
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
protected: |
| 329 |
< |
double data_[Dim]; |
| 329 |
> |
Real data_[Dim]; |
| 330 |
|
|
| 331 |
|
}; |
| 332 |
|
|
| 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; |
| 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; |
| 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 |
|
|
| 395 |
– |
/** fuzzy comparson */ |
| 396 |
– |
template<typename Real, unsigned int Dim> |
| 397 |
– |
inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 398 |
– |
|
| 399 |
– |
} |
| 400 |
– |
|
| 401 |
– |
|
| 407 |
|
/** |
| 408 |
|
* Returns the dot product of two Vectors |
| 409 |
|
* @param v1 first vector |
| 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 |
|
} |
| 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 |
|
|