Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _VECTORS_
- #define _VECTORS_
- /**
- * 3D and 4D vector manager template
- * @author Caiwan
- */
- #include <cmath>
- template <class T> class vector3d {
- public:
- T x,y,z;
- vector3d();
- vector3d(T, T, T);
- vector3d(const vector3d&);
- vector3d &operator = (vector3d);
- vector3d operator+ (const vector3d &);
- vector3d operator- (const vector3d &);
- vector3d operator+ (T);
- vector3d operator- (T);
- vector3d operator* (T);
- vector3d operator/ (T);
- vector3d operator+= (T);
- vector3d operator-= (T);
- vector3d operator+= (const vector3d&);
- vector3d operator-= (const vector3d&);
- vector3d operator*= (T);
- vector3d operator/= (T);
- vector3d operator*= (const vector3d&);
- vector3d operator/= (const vector3d&);
- vector3d crossProduct(const vector3d&);
- // extra vector fn.s
- T vlen ();
- void setZero();
- void set(T, T, T);
- };
- typedef vector3d<float> vec3float;
- typedef vector3d<double> vec3double;
- typedef vector3d<int> vec3int;
- typedef vector3d<unsigned int> vec3uint;
- typedef vector3d<short> vec3short;
- typedef vector3d<unsigned short> vec3ushort;
- // ------------------------------------------------
- template <class T> vector3d<T>::vector3d(void){
- x = 0;
- y = 0;
- z = 0;
- }
- template <class T> vector3d<T>::vector3d(T a, T b, T c){
- x = a;
- y = b;
- z = c;
- }
- template <class T> vector3d<T>::vector3d(const vector3d<T> &v){
- x = v.x;
- y = v.y;
- z = v.z;
- }
- template <class T> vector3d<T>& vector3d<T>::operator = (vector3d<T> v){
- x = v.x;
- y = v.y;
- z = v.z;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator + (const vector3d & v){
- vector3d<T> tmp (*this);
- tmp.x += v.x;
- tmp.y += v.y;
- tmp.z += v.z;
- return tmp;
- }
- template <class T> vector3d<T> vector3d<T>::operator + (T a){
- register vector3d<T> tmp (*this);
- tmp.x += a;
- tmp.y += a;
- tmp.z += a;
- return tmp;
- }
- template <class T> vector3d<T> vector3d<T>::operator - (T a){
- register vector3d<T> tmp (*this);
- tmp.x -= a;
- tmp.y -= a;
- tmp.z -= a;
- return tmp;
- }
- template <class T> vector3d<T> vector3d<T>::operator - (const vector3d & v){
- register vector3d<T> tmp (*this);
- tmp.x -= v.x;
- tmp.y -= v.y;
- tmp.z -= v.z;
- return tmp;
- }
- template <class T> vector3d<T> vector3d<T>::operator+= (T a){
- x += a;
- y += a;
- z += a;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator+= (const vector3d & v){
- x += v.x;
- y += v.y;
- z += v.z;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator-= (T a){
- x -= a;
- y -= a;
- z -= a;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator-= (const vector3d & v){
- x -= v.x;
- y -= v.y;
- z -= v.z;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator* (T a){
- x *= a;
- y *= a;
- z *= a;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator/ (T a){
- if (a == 0) a=1; // should throw exception
- x /= a;
- y /= a;
- z /= a;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator*= (T a){
- x *= a;
- y *= a;
- z *= a;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator/= (T a){
- if (a == 0) a=1; // should throw exception
- x /= a;
- y /= a;
- z /= a;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator*= (const vector3d & v){
- x *= v.x;
- y *= v.y;
- z *= v.z;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::operator/= (const vector3d & v){
- if ((v.x*v.y*v.z) == 0) return NULL; // should throw exception
- x /= v.x;
- y /= v.y;
- z /= v.z;
- return *this;
- }
- template <class T> vector3d<T> vector3d<T>::crossProduct(const vector3d<T> & v2){
- register vector3d<T> tmp, v1 = *this;
- tmp.x = (v1.y * v2.z) - (v1.z * v2.y) ;
- tmp.y = -((v2.z * v1.x) - (v2.x * v1.z));
- tmp.z = (v1.x * v2.y) - (v1.y * v2.x);
- return tmp;
- }
- template <class T> T vector3d<T>::vlen (){
- return sqrt((x * x) + (y * y) + (z * z));
- }
- template <class T> void vector3d<T>::setZero (){
- x = y = z = 0;
- }
- template <class T> void vector3d<T>::set (T a, T b, T c){
- x = a;
- y = b;
- z = c;
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- // 4D vector
- //////////////////////////////////////////////////////////////////////////////////////////
- template <class T> class vector4d {
- public:
- T x,y,z,w;
- vector4d();
- vector4d(T, T, T, T);
- vector4d(const vector4d&);
- vector4d &operator = (vector4d);
- //vector4d &operator = (vector3d); // allow casting
- vector4d operator+ (const vector4d &);
- vector4d operator- (const vector4d &);
- vector4d operator+ (T);
- vector4d operator- (T);
- vector4d operator* (T);
- vector4d operator/ (T);
- vector4d operator+= (T);
- vector4d operator-= (T);
- vector4d operator+= (const vector4d&);
- vector4d operator-= (const vector4d&);
- vector4d operator*= (T);
- vector4d operator/= (T);
- vector4d operator*= (const vector4d&);
- vector4d operator/= (const vector4d&);
- vector4d crossProduct(const vector4d&);
- // extra vector fn.s
- T vlen ();
- void setZero();
- void set(T, T, T, T);
- };
- typedef vector4d<float> vec4float;
- typedef vector4d<double> vec4double;
- typedef vector4d<int> vec4int;
- typedef vector4d<unsigned int> vec4uint;
- typedef vector4d<short> vec4short;
- typedef vector4d<unsigned short> vec4ushort;
- // ------------------------------------------------
- template <class T> vector4d<T>::vector4d(void){
- x = 0;
- y = 0;
- z = 0;
- w = 0;
- }
- template <class T> vector4d<T>::vector4d(T a, T b, T c, T d){
- x = a;
- y = b;
- z = c;
- w = d;
- }
- template <class T> vector4d<T>::vector4d(const vector4d<T> &v){
- x = v.x;
- y = v.y;
- z = v.z;
- w = v.w;
- }
- template <class T> vector4d<T>& vector4d<T>::operator = (vector4d<T> v){
- x = v.x;
- y = v.y;
- z = v.z;
- w = v.w;
- return *this;
- }
- /*
- template <class T> vector4d<T>& vector4d<T>::operator = (vector3d<T> v){
- x = v.x;
- y = v.y;
- z = v.z;
- w = 0.0;
- return *this;
- }
- */
- template <class T> vector4d<T> vector4d<T>::operator + (const vector4d & v){
- vector4d<T> tmp (*this);
- tmp.x += v.x;
- tmp.y += v.y;
- tmp.z += v.z;
- tmp.w += v.w;
- return tmp;
- }
- template <class T> vector4d<T> vector4d<T>::operator + (T a){
- register vector4d<T> tmp (*this);
- tmp.x += a;
- tmp.y += a;
- tmp.z += a;
- tmp.w += a;
- return tmp;
- }
- template <class T> vector4d<T> vector4d<T>::operator - (T a){
- register vector4d<T> tmp (*this);
- tmp.x -= a;
- tmp.y -= a;
- tmp.z -= a;
- tmp.w -= a;
- return tmp;
- }
- template <class T> vector4d<T> vector4d<T>::operator - (const vector4d & v){
- register vector4d<T> tmp (*this);
- tmp.x -= v.x;
- tmp.y -= v.y;
- tmp.z -= v.z;
- tmp.w -= v.w;
- return tmp;
- }
- template <class T> vector4d<T> vector4d<T>::operator+= (T a){
- x += a;
- y += a;
- z += a;
- w += a;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator+= (const vector4d & v){
- x += v.x;
- y += v.y;
- z += v.z;
- w += v.w;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator-= (T a){
- x -= a;
- y -= a;
- z -= a;
- w -= a;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator-= (const vector4d & v){
- x -= v.x;
- y -= v.y;
- z -= v.z;
- w -= v.w;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator* (T a){
- x *= a;
- y *= a;
- z *= a;
- w *= a;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator/ (T a){
- if (a == 0) a=1; // should throw exception
- x /= a;
- y /= a;
- z /= a;
- w /= a;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator*= (T a){
- x *= a;
- y *= a;
- z *= a;
- w *= a;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator/= (T a){
- if (a == 0) a=1; // should throw exception
- x /= a;
- y /= a;
- z /= a;
- w /= a;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator*= (const vector4d & v){
- x *= v.x;
- y *= v.y;
- z *= v.z;
- w *= v.w
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::operator/= (const vector4d & v){
- if ((v.x*v.y*v.z) == 0) return NULL; // should throw exception
- x /= v.x;
- y /= v.y;
- z /= v.z;
- w /= v.w;
- return *this;
- }
- template <class T> vector4d<T> vector4d<T>::crossProduct(const vector4d<T> & v2){
- register vector4d<T> tmp, v1 = *this;
- tmp.x = (v1.y * v2.z) - (v1.z * v2.y) ;
- tmp.y = -((v2.z * v1.x) - (v2.x * v1.z));
- tmp.z = (v1.x * v2.y) - (v1.y * v2.x); // TODO: 4d crossprodukt
- return tmp;
- }
- template <class T> T vector4d<T>::vlen (){
- return sqrt((x * x) + (y * y) + (z * z) + (w * w));
- }
- template <class T> void vector4d<T>::setZero (){
- x = y = z = w = 0;
- }
- template <class T> void vector4d<T>::set (T a, T b, T c, T d){
- x = a;
- y = b;
- z = c;
- w = d;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement