--- trunk/src/math/Vector.hpp 2004/10/13 22:24:59 71 +++ trunk/src/math/Vector.hpp 2004/10/18 23:13:23 101 @@ -39,6 +39,23 @@ namespace oopse { namespace oopse { + const double epsilon = 0.000001; + + template + inline bool equal(T e1, T e2) { + return e1 == e2; + } + + template<> + inline bool equal(float e1, float e2) { + return fabs(e1 - e2) < epsilon; + } + + template<> + inline bool equal(double e1, double e2) { + return fabs(e1 - e2) < epsilon; + } + /** * @class Vector Vector.hpp "math/Vector.hpp" * @brief Fix length vector class @@ -68,11 +85,17 @@ namespace oopse { return *this; } + + template + inline Vector(const T& s){ + for (unsigned int i = 0; i < Dim; i++) + data_[i] = s; + } /** Constructs and initializes a Vector from an array */ inline Vector( double* v) { - for (unsigned int i = 0; i < Dim; i++) - data_[i] = v[i]; + for (unsigned int i = 0; i < Dim; i++) + data_[i] = v[i]; } /** @@ -115,11 +138,35 @@ namespace oopse { return data_[i]; } + /** + * Tests if this vetor is equal to other vector + * @return true if equal, otherwise return false + * @param v vector to be compared + */ + inline bool operator ==(const Vector& v) { + + for (unsigned int i = 0; i < Dim; i ++) { + if (!equal(data_[i], v[i])) { + return false; + } + } + + return true; + } + + /** + * Tests if this vetor is not equal to other vector + * @return true if equal, otherwise return false + * @param v vector to be compared + */ + inline bool operator !=(const Vector& v) { + return !(*this == v); + } + /** Negates the value of this vector in place. */ inline void negate() { - data_[0] = -data_[0]; - data_[1] = -data_[1]; - data_[2] = -data_[2]; + for (unsigned int i = 0; i < Dim; i++) + data_[i] = -data_[i]; } /** @@ -137,8 +184,8 @@ namespace oopse { * @param v1 the other vector */ inline void add( const Vector& v1 ) { - for (unsigned int i = 0; i < Dim; i++) - data_[i] += v1.data_[i]; + for (unsigned int i = 0; i < Dim; i++) + data_[i] += v1.data_[i]; } /** @@ -147,8 +194,8 @@ namespace oopse { * @param v2 the second vector */ inline void add( const Vector& v1, const Vector& v2 ) { - for (unsigned int i = 0; i < Dim; i++) - data_[i] = v1.data_[i] + v2.data_[i]; + for (unsigned int i = 0; i < Dim; i++) + data_[i] = v1.data_[i] + v2.data_[i]; } /** @@ -175,18 +222,18 @@ namespace oopse { * @param s the scalar value */ inline void mul( double s ) { - for (unsigned int i = 0; i < Dim; i++) + for (unsigned int i = 0; i < Dim; i++) data_[i] *= s; } /** * Sets the value of this vector to the scalar multiplication of vector v1 * (*this = s * v1). + * @param v1 the vector * @param s the scalar value - * @param v1 the vector */ - inline void mul( double s, const Vector& v1 ) { - for (unsigned int i = 0; i < Dim; i++) + inline void mul( const Vector& v1, double s) { + for (unsigned int i = 0; i < Dim; i++) data_[i] = s * v1.data_[i]; } @@ -195,7 +242,7 @@ namespace oopse { * @param s the scalar value */ inline void div( double s) { - for (unsigned int i = 0; i < Dim; i++) + for (unsigned int i = 0; i < Dim; i++) data_[i] /= s; } @@ -205,7 +252,7 @@ namespace oopse { * @param s the scalar value */ inline void div( const Vector& v1, double s ) { - for (unsigned int i = 0; i < Dim; i++) + for (unsigned int i = 0; i < Dim; i++) data_[i] = v1.data_[i] / s; } @@ -245,7 +292,7 @@ namespace oopse { * Returns the squared length of this vector. * @return the squared length of this vector */ - inline double lengthSquared() { + inline double lengthSquare() { return dot(*this, *this); } @@ -256,17 +303,27 @@ namespace oopse { len = length(); *this /= len; } + + /** + * Tests if this vector is normalized + * @return true if this vector is normalized, otherwise return false + */ + inline bool isNormalized() const + { + return lengthSquare() == 1.0; + } protected: - double data_[3]; + double data_[Dim]; }; /** unary minus*/ template inline Vector operator -(const Vector& v1){ - Vector tmp(v1); - return tmp.negate(); + Vector tmp(v1); + tmp.negate(); + return tmp; } /** @@ -305,7 +362,7 @@ namespace oopse { template Vector operator * ( const Vector& v1, double s) { Vector result; - result.mul(s, v1); + result.mul(v1,s); return result; } @@ -318,7 +375,7 @@ namespace oopse { template Vector operator * ( double s, const Vector& v1 ) { Vector result; - result.mul(s, v1); + result.mul(v1, s); return result; } @@ -335,19 +392,6 @@ namespace oopse { return result; } - /** - * Returns the value of division of a vector by a scalar. - * @return the vaule of scalar division of this vector - * @param s the scalar value - * @param v1 the source vector - */ - template - inline Vector operator /( double s, const Vector& v1 ) { - Vector result; - result.div( v1,s); - return result; - } - /** fuzzy comparson */ template inline bool epsilonEqual( const Vector& v1, const Vector& v2 ) { @@ -363,13 +407,13 @@ namespace oopse { */ template inline Real dot( const Vector& v1, const Vector& v2 ) { - Real tmp; - tmp = 0; + Real tmp; + tmp = 0; - for (unsigned int i = 0; i < Dim; i++) - tmp += v1[i] + v2[i]; - - return tmp; + for (unsigned int i = 0; i < Dim; i++) + tmp += v1[i] + v2[i]; + + return tmp; } /** @@ -400,8 +444,9 @@ namespace oopse { * Write to an output stream */ template - std::ostream &operator<< ( std::ostream& o, const Vector& v1 ) { - + std::ostream &operator<< ( std::ostream& o, const Vector& v) { + + o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; return o; }