/ Published in: C++
URL: http://cryptex3d.blogspot.com
tplVector is a template class with vectorial arithmetics operator overloading.
Please make me know if you use it. Any corrections/suggestions are welcome.
Expand |
Embed | Plain Text
/* * tplVector by Marcelo Barreiros Furtado - 2009 * * To complainers: * You're free to use this code as you wish, take in mind altough that * it's provided "as is". I claim no credit over it - neither shame. * * To contributors: * Feel free to use this code in any creative and original way you wish. * Please e-mail me any issue/bug/error you detect on it. */ #ifndef __VECTOR_TPP__ #define __VECTOR_TPP__ //! comment the line bellow to wipe away unnecessary code //#define __VECTOR_TPP__DEBUG__ #include <cmath> #ifdef __VECTOR_TPP__DEBUG__ #include <iostream> using namespace std; #endif //! The vector class template template<class T> class tplVector; //! the dot product operator template <class T> T operator*(tplVector<T>& left, tplVector<T>& right); //! the cross product operator template <class T> tplVector<T> operator%(tplVector<T> left, tplVector<T> right); //! the scalar multiplication operator 1 template <class T> tplVector<T>& operator*(T scalar, tplVector<T>& vector); //! the scalar multiplication operator 2 template <class T> tplVector<T>& operator*(tplVector<T>& vector, T scalar); //! the sum operator template <class T> tplVector<T>& operator+(tplVector<T>& left, tplVector<T>& right); //! the subtraction operator template <class T> tplVector<T>& operator-(tplVector<T>& left, tplVector<T>& right); //! the vector inversion operator template <class T> tplVector<T>& operator-(tplVector<T>& vector); //////////////////////////////////////////////////////////// //! The vector class template template<class T> class tplVector { private: T ijk[3]; public: //! the default constructor tplVector<T>(){ ijk[0] = T(0); ijk[1] = T(0); ijk[2] = T(0); } //! the coefficient constructor tplVector<T>(const T& i, const T& j, const T& k){ ijk[0] = i; ijk[1] = j; ijk[2] = k; } //! the point subtraction constructor. //! constructs the ab vector tplVector<T>(const T* a, const T* b){ ijk[0] = b[0]-a[0]; ijk[1] = b[1]-a[1]; ijk[2] = b[2]-a[2]; } //! the subscript operator T& operator[](const int& index){ return ijk[index % 3]; } //! the assignment operator tplVector<T>& operator=(tplVector<T>& vector){ ijk[0] = vector[0]; ijk[1] = vector[1]; ijk[2] = vector[2]; return *this; } //! the vector's length T length(){ return sqrt(ijk[0]*ijk[0]+ijk[1]*ijk[1]+ijk[2]*ijk[2]); } }; //! the dot product operator template <class T> T operator*(tplVector<T>& left, tplVector<T>& right){ return left[0]*right[0] + left[1]*right[1] + left[2]*right[2]; } //! the cross product operator template <class T> tplVector<T> operator%(tplVector<T> left, tplVector<T> right){ tplVector<T> result; result[0] = left[1] * right[2] - left[2] * right[1]; result[1] = left[2] * right[0] - left[0] * right[2]; result[2] = left[0] * right[1] - left[1] * right[0]; return result; } //! the scalar multiplication operator 1 template <class T> tplVector<T>& operator*(T scalar, tplVector<T>& vector){ vector[0] *= scalar; vector[1] *= scalar; vector[2] *= scalar; return vector; } //! the scalar multiplication operator 2 template <class T> tplVector<T>& operator*(tplVector<T>& vector, T scalar){ vector[0] *= scalar; vector[1] *= scalar; vector[2] *= scalar; return vector; } //! the sum operator template <class T> tplVector<T>& operator+(tplVector<T>& left, tplVector<T>& right){ left[0] += right[0]; left[1] += right[1]; left[2] += right[2]; return left; } //! the subtraction operator template <class T> tplVector<T>& operator-(tplVector<T>& left, tplVector<T>& right){ left[0] -= right[0]; left[1] -= right[1]; left[2] -= right[2]; return left; } //! the vector inversion operator template <class T> tplVector<T>& operator-(tplVector<T>& vector){ return T(-1)*vector; } #ifdef __VECTOR_TPP__DEBUG__ ////////////////////////////////////////////////////////// //! debug only:////////////////////////////////////////// //! the stream output operator template <class T> ostream& operator<< (ostream& out, const tplVector<T>& vector){ tplVector<T> aux = vector; out << "(" << aux[0] << ", " << aux[1] << ", " << aux[2] << ")"; return out; } int main(int argc, char** argv){ float a[3] = {1,2,3}, b[3]= {4,5,6}; tplVector<float> i(1,0,0), j(0,1,0), k(0,0,1), ab(a,b); cout << "ab\t" << ab << "\n"; return 0; } #endif ////////////////////////////////////////////////////////// #endif
You need to login to post a comment.
