#ifndef __GenVector #define __GenVector #include #include #include //enable the padding conversion function - slow //#define VECTOR_PADDING_CONVERSION //control if all components are set to zero on construction #define VECTOR_INITIALIZE_TO_ZERO //default vector #define v3float float //error checking for divide by 0 and other degenerate cases //#define VECTOR_ERROR_CHECKING // set an epsilon if not set #ifndef EPSILON #define EPSILON 0.000000001f #endif template class GenVector { public: static const int dim = dimension; T c[dimension+padding]; //typically 3 dim of x, y, z // GenVector creation GenVector() { #ifdef VECTOR_INITIALIZE_TO_ZERO for(int i=0; i operator GenVector() const { GenVector out; for(int i=0; ic[i]; return out; } #endif // operator overload GenVector operator+(const GenVector &v1) const { GenVector t; for(int i=0; i GenVector& operator=(const GenVector &v1) { #ifdef VECTOR_ERROR_CHECKING if(otherDim < dimension) { throw(1); } #endif //truncate extra dimension in input vector for(int i=0; isquaredLength()); } GenVector& normalize() { T normalizeLength; normalizeLength = this->length(); #ifdef VECTOR_ERROR_CHECKING if(normalizeLength <= EPSILON) { throw(1); return *this; } #endif *this /= normalizeLength; return *this; } // GenVector combination operations T distanceSquared(const GenVector &a) { T d = 0.0; for(int i=0; idistanceSquared(a); return sqrt( disSquared ); } T angleBetweem(const GenVector &a) { return acos(this->dot(a) / this->length() / a.length()); } GenVector reflect(const GenVector &normal) { T dp; GenVector dest; dp = 2*this->dot(normal); dest = *this - (normal*dp); return dest; } void projectToPlane(const GenVector &normal) { float scale = this->dot(normal); *this = *this - (normal * scale); } int maxComponent() { int largest = 0; for(int i=1; i c[i-1]) largest = i; return largest; } int maxMagnitudeComponent() { int largest = 0; for(int i=1; i fabs(c[i-1])) largest = i; return largest; } }; template static GenVector operator*(const T f, const GenVector &v1) { return v1*f; } template static GenVector operator-(const T f, const GenVector &v1) { GenVector t; for(int i=0; i static GenVector operator/(const T f, const GenVector &v1) { GenVector t; for(int i=0; i Vector2; typedef GenVector<3> Vector3; typedef GenVector<4> Vector4; typedef GenVector<3, unsigned char> Color; #endif