| 85 |
|
|
| 86 |
|
return *this; |
| 87 |
|
} |
| 88 |
+ |
|
| 89 |
+ |
template<typename T> |
| 90 |
+ |
inline Vector(const T& s){ |
| 91 |
+ |
for (unsigned int i = 0; i < Dim; i++) |
| 92 |
+ |
data_[i] = s; |
| 93 |
+ |
} |
| 94 |
|
|
| 95 |
|
/** Constructs and initializes a Vector from an array */ |
| 96 |
|
inline Vector( double* v) { |
| 97 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 98 |
< |
data_[i] = v[i]; |
| 97 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 98 |
> |
data_[i] = v[i]; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
/** |
| 165 |
|
|
| 166 |
|
/** Negates the value of this vector in place. */ |
| 167 |
|
inline void negate() { |
| 168 |
< |
data_[0] = -data_[0]; |
| 169 |
< |
data_[1] = -data_[1]; |
| 164 |
< |
data_[2] = -data_[2]; |
| 168 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 169 |
> |
data_[i] = -data_[i]; |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
/** |
| 184 |
|
* @param v1 the other vector |
| 185 |
|
*/ |
| 186 |
|
inline void add( const Vector<Real, Dim>& v1 ) { |
| 187 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 188 |
< |
data_[i] += v1.data_[i]; |
| 187 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 188 |
> |
data_[i] += v1.data_[i]; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
/** |
| 194 |
|
* @param v2 the second vector |
| 195 |
|
*/ |
| 196 |
|
inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 197 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 198 |
< |
data_[i] = v1.data_[i] + v2.data_[i]; |
| 197 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 198 |
> |
data_[i] = v1.data_[i] + v2.data_[i]; |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
/** |
| 222 |
|
* @param s the scalar value |
| 223 |
|
*/ |
| 224 |
|
inline void mul( double s ) { |
| 225 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 225 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 226 |
|
data_[i] *= s; |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
/** |
| 230 |
|
* Sets the value of this vector to the scalar multiplication of vector v1 |
| 231 |
|
* (*this = s * v1). |
| 232 |
+ |
* @param v1 the vector |
| 233 |
|
* @param s the scalar value |
| 228 |
– |
* @param v1 the vector |
| 234 |
|
*/ |
| 235 |
< |
inline void mul( double s, const Vector<Real, Dim>& v1 ) { |
| 236 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 235 |
> |
inline void mul( const Vector<Real, Dim>& v1, double s) { |
| 236 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 237 |
|
data_[i] = s * v1.data_[i]; |
| 238 |
|
} |
| 239 |
|
|
| 242 |
|
* @param s the scalar value |
| 243 |
|
*/ |
| 244 |
|
inline void div( double s) { |
| 245 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 245 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 246 |
|
data_[i] /= s; |
| 247 |
|
} |
| 248 |
|
|
| 252 |
|
* @param s the scalar value |
| 253 |
|
*/ |
| 254 |
|
inline void div( const Vector<Real, Dim>& v1, double s ) { |
| 255 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 255 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 256 |
|
data_[i] = v1.data_[i] / s; |
| 257 |
|
} |
| 258 |
|
|
| 292 |
|
* Returns the squared length of this vector. |
| 293 |
|
* @return the squared length of this vector |
| 294 |
|
*/ |
| 295 |
< |
inline double lengthSquared() { |
| 295 |
> |
inline double lengthSquare() { |
| 296 |
|
return dot(*this, *this); |
| 297 |
|
} |
| 298 |
|
|
| 301 |
|
double len; |
| 302 |
|
|
| 303 |
|
len = length(); |
| 304 |
+ |
|
| 305 |
+ |
//if (len < oopse:epsilon) |
| 306 |
+ |
// throw(); |
| 307 |
+ |
|
| 308 |
|
*this /= len; |
| 309 |
|
} |
| 310 |
+ |
|
| 311 |
+ |
/** |
| 312 |
+ |
* Tests if this vector is normalized |
| 313 |
+ |
* @return true if this vector is normalized, otherwise return false |
| 314 |
+ |
*/ |
| 315 |
+ |
inline bool isNormalized() const |
| 316 |
+ |
{ |
| 317 |
+ |
return lengthSquare() == 1.0; |
| 318 |
+ |
} |
| 319 |
|
|
| 320 |
|
protected: |
| 321 |
< |
double data_[3]; |
| 321 |
> |
double data_[Dim]; |
| 322 |
|
|
| 323 |
|
}; |
| 324 |
|
|
| 325 |
|
/** unary minus*/ |
| 326 |
|
template<typename Real, unsigned int Dim> |
| 327 |
|
inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){ |
| 328 |
< |
Vector tmp(v1); |
| 329 |
< |
return tmp.negate(); |
| 328 |
> |
Vector<Real, Dim> tmp(v1); |
| 329 |
> |
tmp.negate(); |
| 330 |
> |
return tmp; |
| 331 |
|
} |
| 332 |
|
|
| 333 |
|
/** |
| 366 |
|
template<typename Real, unsigned int Dim> |
| 367 |
|
Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) { |
| 368 |
|
Vector<Real, Dim> result; |
| 369 |
< |
result.mul(s, v1); |
| 369 |
> |
result.mul(v1,s); |
| 370 |
|
return result; |
| 371 |
|
} |
| 372 |
|
|
| 379 |
|
template<typename Real, unsigned int Dim> |
| 380 |
|
Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) { |
| 381 |
|
Vector<Real, Dim> result; |
| 382 |
< |
result.mul(s, v1); |
| 382 |
> |
result.mul(v1, s); |
| 383 |
|
return result; |
| 384 |
|
} |
| 385 |
|
|
| 397 |
|
} |
| 398 |
|
|
| 399 |
|
/** |
| 381 |
– |
* Returns the value of division of a vector by a scalar. |
| 382 |
– |
* @return the vaule of scalar division of this vector |
| 383 |
– |
* @param s the scalar value |
| 384 |
– |
* @param v1 the source vector |
| 385 |
– |
*/ |
| 386 |
– |
template<typename Real, unsigned int Dim> |
| 387 |
– |
inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) { |
| 388 |
– |
Vector<Real, Dim> result; |
| 389 |
– |
result.div( v1,s); |
| 390 |
– |
return result; |
| 391 |
– |
} |
| 392 |
– |
|
| 393 |
– |
/** fuzzy comparson */ |
| 394 |
– |
template<typename Real, unsigned int Dim> |
| 395 |
– |
inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 396 |
– |
|
| 397 |
– |
} |
| 398 |
– |
|
| 399 |
– |
|
| 400 |
– |
/** |
| 400 |
|
* Returns the dot product of two Vectors |
| 401 |
|
* @param v1 first vector |
| 402 |
|
* @param v2 second vector |
| 404 |
|
*/ |
| 405 |
|
template<typename Real, unsigned int Dim> |
| 406 |
|
inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) { |
| 407 |
< |
Real tmp; |
| 408 |
< |
tmp = 0; |
| 407 |
> |
Real tmp; |
| 408 |
> |
tmp = 0; |
| 409 |
|
|
| 410 |
< |
for (unsigned int i = 0; i < Dim; i++) |
| 411 |
< |
tmp += v1[i] + v2[i]; |
| 412 |
< |
|
| 413 |
< |
return tmp; |
| 410 |
> |
for (unsigned int i = 0; i < Dim; i++) |
| 411 |
> |
tmp += v1[i] + v2[i]; |
| 412 |
> |
|
| 413 |
> |
return tmp; |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
/** |
| 441 |
|
* Write to an output stream |
| 442 |
|
*/ |
| 443 |
|
template<typename Real, unsigned int Dim> |
| 444 |
< |
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) { |
| 445 |
< |
|
| 444 |
> |
std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) { |
| 445 |
> |
|
| 446 |
> |
o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; |
| 447 |
|
return o; |
| 448 |
|
} |
| 449 |
|
|