--- trunk/src/math/Vector.hpp 2004/10/13 06:51:09 70 +++ trunk/src/math/Vector.hpp 2004/10/17 01:19:11 93 @@ -35,14 +35,32 @@ #include #include +#include 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 */ - template + template class Vector{ public: @@ -70,8 +88,8 @@ namespace oopse { /** 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]; } /** @@ -114,6 +132,31 @@ 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]; @@ -136,8 +179,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]; } /** @@ -146,8 +189,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]; } /** @@ -174,7 +217,7 @@ 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; } @@ -185,7 +228,7 @@ namespace oopse { * @param v1 the vector */ inline void mul( double s, const Vector& v1 ) { - for (unsigned int i = 0; i < Dim; i++) + for (unsigned int i = 0; i < Dim; i++) data_[i] = s * v1.data_[i]; } @@ -194,7 +237,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; } @@ -204,30 +247,30 @@ 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; } /** @see #add */ - inline Vector operator +=( const Vector& v1 ) { + inline Vector& operator +=( const Vector& v1 ) { add(v1); return *this; } /** @see #sub */ - inline Vector operator -=( const Vector& v1 ) { + inline Vector& operator -=( const Vector& v1 ) { sub(v1); return *this; } /** @see #mul */ - inline Vector operator *=( double s) { + inline Vector& operator *=( double s) { mul(s); return *this; } /** @see #div */ - inline Vector operator /=( double s ) { + inline Vector& operator /=( double s ) { div(s); return *this; } @@ -244,7 +287,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); } @@ -255,6 +298,15 @@ 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]; @@ -262,7 +314,7 @@ namespace oopse { }; /** unary minus*/ - template + template inline Vector operator -(const Vector& v1){ Vector tmp(v1); return tmp.negate(); @@ -274,7 +326,7 @@ namespace oopse { * @param v1 the first vector * @param v2 the second vector */ - template + template inline Vector operator +(const Vector& v1, const Vector& v2) { Vector result; @@ -288,7 +340,7 @@ namespace oopse { * @param v1 the first vector * @param v2 the second vector */ - template + template Vector operator -(const Vector& v1, const Vector& v2) { Vector result; result.sub(v1, v2); @@ -301,7 +353,7 @@ namespace oopse { * @param v1 the source vector * @param s the scalar value */ - template + template Vector operator * ( const Vector& v1, double s) { Vector result; result.mul(s, v1); @@ -314,7 +366,7 @@ namespace oopse { * @param s the scalar value * @param v1 the source vector */ - template + template Vector operator * ( double s, const Vector& v1 ) { Vector result; result.mul(s, v1); @@ -327,7 +379,7 @@ namespace oopse { * @param v1 the source vector * @param s the scalar value */ - template + template Vector operator / ( const Vector& v1, double s) { Vector result; result.div( v1,s); @@ -340,7 +392,7 @@ namespace oopse { * @param s the scalar value * @param v1 the source vector */ - template + template inline Vector operator /( double s, const Vector& v1 ) { Vector result; result.div( v1,s); @@ -348,7 +400,7 @@ namespace oopse { } /** fuzzy comparson */ - template + template inline bool epsilonEqual( const Vector& v1, const Vector& v2 ) { } @@ -360,15 +412,15 @@ namespace oopse { * @param v2 second vector * @return the dot product of v1 and v2 */ - template + 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; } /** @@ -377,7 +429,7 @@ namespace oopse { * @param v2 second vector * @return the distance between v1 and v2 */ - template + template inline Real distance( const Vector& v1, const Vector& v2 ) { Vector tempVector = v1 - v2; return tempVector.length(); @@ -389,7 +441,7 @@ namespace oopse { * @param v2 second vector * @return the squared distance between v1 and v2 */ - template + template inline Real distanceSquare( const Vector& v1, const Vector& v2 ) { Vector tempVector = v1 - v2; return tempVector.lengthSquare(); @@ -398,9 +450,10 @@ namespace oopse { /** * Write to an output stream */ - template - std::ostream &operator<< ( std::ostream& o, const Vector& v1 ) { - + template + std::ostream &operator<< ( std::ostream& o, const Vector& v) { + + o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; return o; }