--- trunk/src/math/Vector.hpp 2004/10/13 06:51:09 70 +++ trunk/src/math/Vector.hpp 2004/10/25 22:46:19 151 @@ -35,17 +35,39 @@ #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: + typedef Real ElemType; + typedef Real* ElemPoinerType; + /** default constructor */ inline Vector(){ for (unsigned int i = 0; i < Dim; i++) @@ -67,11 +89,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]; + inline Vector( Real* v) { + for (unsigned int i = 0; i < Dim; i++) + data_[i] = v[i]; } /** @@ -79,7 +107,7 @@ namespace oopse { * @return reference of ith element * @param i index */ - inline double& operator[](unsigned int i) { + inline Real& operator[](unsigned int i) { assert( i < Dim); return data_[i]; } @@ -89,7 +117,7 @@ namespace oopse { * @return reference of ith element * @param i index */ - inline double& operator()(unsigned int i) { + inline Real& operator()(unsigned int i) { assert( i < Dim); return data_[i]; } @@ -99,7 +127,7 @@ namespace oopse { * @return reference of ith element * @param i index */ - inline const double& operator[](unsigned int i) const { + inline const Real& operator[](unsigned int i) const { assert( i < Dim); return data_[i]; } @@ -109,16 +137,52 @@ namespace oopse { * @return reference of ith element * @param i index */ - inline const double& operator()(unsigned int i) const { + inline const Real& operator()(unsigned int i) const { assert( i < Dim); return data_[i]; } + /** Copy the internal data to an array*/ + void getArray(Real* array) { + for (unsigned int i = 0; i < Dim; i ++) { + array[i] = data_[i]; + } + } + + /** Returns the pointer of internal array */ + Real* getArrayPointer() { + return data_; + } + + /** + * 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]; } /** @@ -136,9 +200,9 @@ 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]; + } /** * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2). @@ -146,8 +210,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]; } /** @@ -173,19 +237,19 @@ namespace oopse { * Sets the value of this vector to the scalar multiplication of itself (*this *= s). * @param s the scalar value */ - inline void mul( double s ) { - for (unsigned int i = 0; i < Dim; i++) + inline void mul( Real s ) { + 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, Real s) { + for (unsigned int i = 0; i < Dim; i++) data_[i] = s * v1.data_[i]; } @@ -193,8 +257,8 @@ namespace oopse { * Sets the value of this vector to the scalar division of itself (*this /= s ). * @param s the scalar value */ - inline void div( double s) { - for (unsigned int i = 0; i < Dim; i++) + inline void div( Real s) { + for (unsigned int i = 0; i < Dim; i++) data_[i] /= s; } @@ -203,31 +267,31 @@ namespace oopse { * @param v1 the source vector * @param s the scalar value */ - inline void div( const Vector& v1, double s ) { - for (unsigned int i = 0; i < Dim; i++) + inline void div( const Vector& v1, Real s ) { + 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 *=( Real s) { mul(s); return *this; } /** @see #div */ - inline Vector operator /=( double s ) { + inline Vector& operator /=( Real s ) { div(s); return *this; } @@ -236,36 +300,49 @@ namespace oopse { * Returns the length of this vector. * @return the length of this vector */ - inline double length() { - return sqrt(lengthSquared()); + inline Real length() { + return sqrt(lengthSquare()); } /** * Returns the squared length of this vector. * @return the squared length of this vector */ - inline double lengthSquared() { + inline Real lengthSquare() { return dot(*this, *this); } /** Normalizes this vector in place */ inline void normalize() { - double len; + Real len; len = length(); + + //if (len < oopse:epsilon) + // throw(); + *this /= len; } + + /** + * Tests if this vector is normalized + * @return true if this vector is normalized, otherwise return false + */ + inline bool isNormalized() { + return equal(lengthSquare(), 1.0); + } protected: - double data_[3]; + Real data_[Dim]; }; /** unary minus*/ - template + template inline Vector operator -(const Vector& v1){ - Vector tmp(v1); - return tmp.negate(); + Vector tmp(v1); + tmp.negate(); + return tmp; } /** @@ -274,7 +351,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 +365,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,10 +378,10 @@ namespace oopse { * @param v1 the source vector * @param s the scalar value */ - template - Vector operator * ( const Vector& v1, double s) { + template + Vector operator * ( const Vector& v1, Real s) { Vector result; - result.mul(s, v1); + result.mul(v1,s); return result; } @@ -314,10 +391,10 @@ namespace oopse { * @param s the scalar value * @param v1 the source vector */ - template - Vector operator * ( double s, const Vector& v1 ) { + template + Vector operator * ( Real s, const Vector& v1 ) { Vector result; - result.mul(s, v1); + result.mul(v1, s); return result; } @@ -327,48 +404,28 @@ namespace oopse { * @param v1 the source vector * @param s the scalar value */ - template - Vector operator / ( const Vector& v1, double s) { + template + Vector operator / ( const Vector& v1, Real s) { Vector result; result.div( v1,s); 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 ) { - - } - - - /** * Returns the dot product of two Vectors * @param v1 first vector * @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 +434,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 +446,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 +455,20 @@ 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 << "[ "; + for (unsigned int i = 0 ; i< Dim; i++) { + o << v[i]; + + if (i != Dim -1) { + o<< ", "; + } + } + + o << " ]"; return o; }