--- trunk/src/math/Vector.hpp 2004/10/18 23:13:23 101 +++ trunk/src/math/Vector.hpp 2004/10/25 22:46:19 151 @@ -55,6 +55,7 @@ namespace oopse { inline bool equal(double e1, double e2) { return fabs(e1 - e2) < epsilon; } + /** * @class Vector Vector.hpp "math/Vector.hpp" @@ -64,6 +65,9 @@ namespace oopse { class Vector{ public: + typedef Real ElemType; + typedef Real* ElemPoinerType; + /** default constructor */ inline Vector(){ for (unsigned int i = 0; i < Dim; i++) @@ -93,7 +97,7 @@ namespace oopse { } /** Constructs and initializes a Vector from an array */ - inline Vector( double* v) { + inline Vector( Real* v) { for (unsigned int i = 0; i < Dim; i++) data_[i] = v[i]; } @@ -103,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]; } @@ -113,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]; } @@ -123,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]; } @@ -133,11 +137,23 @@ 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 @@ -184,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). @@ -221,7 +237,7 @@ 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 ) { + inline void mul( Real s ) { for (unsigned int i = 0; i < Dim; i++) data_[i] *= s; } @@ -232,7 +248,7 @@ namespace oopse { * @param v1 the vector * @param s the scalar value */ - inline void mul( const Vector& v1, double s) { + inline void mul( const Vector& v1, Real s) { for (unsigned int i = 0; i < Dim; i++) data_[i] = s * v1.data_[i]; } @@ -241,7 +257,7 @@ 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) { + inline void div( Real s) { for (unsigned int i = 0; i < Dim; i++) data_[i] /= s; } @@ -251,7 +267,7 @@ namespace oopse { * @param v1 the source vector * @param s the scalar value */ - inline void div( const Vector& v1, double s ) { + inline void div( const Vector& v1, Real s ) { for (unsigned int i = 0; i < Dim; i++) data_[i] = v1.data_[i] / s; } @@ -269,13 +285,13 @@ namespace oopse { } /** @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; } @@ -284,23 +300,27 @@ 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 lengthSquare() { + 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; } @@ -308,13 +328,12 @@ namespace oopse { * Tests if this vector is normalized * @return true if this vector is normalized, otherwise return false */ - inline bool isNormalized() const - { - return lengthSquare() == 1.0; + inline bool isNormalized() { + return equal(lengthSquare(), 1.0); } protected: - double data_[Dim]; + Real data_[Dim]; }; @@ -360,7 +379,7 @@ namespace oopse { * @param s the scalar value */ template - Vector operator * ( const Vector& v1, double s) { + Vector operator * ( const Vector& v1, Real s) { Vector result; result.mul(v1,s); return result; @@ -373,7 +392,7 @@ namespace oopse { * @param v1 the source vector */ template - Vector operator * ( double s, const Vector& v1 ) { + Vector operator * ( Real s, const Vector& v1 ) { Vector result; result.mul(v1, s); return result; @@ -386,19 +405,12 @@ namespace oopse { * @param s the scalar value */ template - Vector operator / ( const Vector& v1, double s) { + Vector operator / ( const Vector& v1, Real s) { 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 @@ -411,7 +423,7 @@ namespace oopse { tmp = 0; for (unsigned int i = 0; i < Dim; i++) - tmp += v1[i] + v2[i]; + tmp += v1[i] * v2[i]; return tmp; } @@ -446,7 +458,17 @@ namespace oopse { template std::ostream &operator<< ( std::ostream& o, const Vector& v) { - o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl; + o << "[ "; + + for (unsigned int i = 0 ; i< Dim; i++) { + o << v[i]; + + if (i != Dim -1) { + o<< ", "; + } + } + + o << " ]"; return o; }