rtsVector3d.h 3.71 KB
///This class is used to store information for a 3D vector.
#include <iostream>
#include <math.h>
using namespace std;

#ifndef RTSVECTOR3D_H
#define RTSVECTOR3D_H


template <class T> class vector3D
{
	//friend class point3D<T>;
public:
	//data values
	T x;
	T y;
	T z;

	vector3D();								///<Constructor.  The resulting vector is initialized to (0,0,0)
	vector3D(T new_x, T new_y, T new_z);	///<Constructor.  Initialize the resulting vector to the value specified by (new_x, new_y, new_z)

	//operators
	vector3D<T> operator+(vector3D<T> param);	///<Used for vector arithmetic.  Allows two vectors with similar types to be added.
	vector3D<T> operator-(vector3D<T> param);	///<Used for vector arithmetic.  Allows two vectors with similar types to be subtracted.
	T operator*(vector3D<T> param);				///<Used for vector arithmetic.  Computes the inner product of two vectors of the same type.
	vector3D<T> operator*(T param);				///<Used for vector arithmetic.  Multiplies a vector by a scalar value of type T.
	vector3D<T> X(vector3D<T> param);			///<Compute the cross product between two vectors
	
	friend vector3D<T> operator*(const T lhs, vector3D<T> rhs){return rhs*lhs;}	///<Allows vectors to be multiplied by scalars in either order

	//methods
	vector3D<T> Times(vector3D<T> param);			///<Performs piecewise multiplication between two vectors [ex. (x1*x2, y1*y2, z1*z2)].
	vector3D<T> Normalize();						///<Returns the normalized version of the vector.
	T Length();										///<Returns the length of the vector.

	//casting operators
	template <class U> operator vector3D<U>();		///<Casting operator allows explicit casting between vector types.
	//output
	friend ostream& operator<<(ostream& os, const vector3D<T> &rhs)
	{
		os<<"("<<rhs.x<<","<<rhs.y<<","<<rhs.z<<")";
		return os;
	}
	void print();
};

template <class T>
template <class U>
vector3D<T>::operator vector3D<U>()
{
	vector3D<U> cast_result(x, y, z);
	return cast_result;
}

template <class T> vector3D<T>::vector3D()
{
	x=0;
	y=0;
	z=0;
}

template <class T> vector3D<T>::vector3D(T new_x, T new_y, T new_z)
{
	x=new_x;
	y=new_y;
	z=new_z;
}

template <class T>
vector3D<T> vector3D<T>::operator -(vector3D<T> param)
{
	vector3D<T> result;			//create the result
	
	result.x = x-param.x;		//perform the computation
	result.y = y-param.y;
	result.z = z-param.z;

	return result;
}

template <class T>
vector3D<T> vector3D<T>::operator+(vector3D<T> param)
{
	vector3D<T> result;			//create the result
	
	result.x = x+param.x;		//perform the computation
	result.y = y+param.y;
	result.z = z+param.z;

	return result;
}

template <class T>
vector3D<T> vector3D<T>::Times(vector3D<T> param)
{
	vector3D<T> result;			//create the result
	
	result.x = x*param.x;		//perform the computation
	result.y = y*param.y;
	result.z = z*param.z;

	return result;
}

template <class T>
vector3D<T> vector3D<T>::operator*(T param)
{
	vector3D<T> result;			//create the result
	
	result.x = x*param;		//perform the computation
	result.y = y*param;
	result.z = z*param;

	return result;
}

template <class T>
T vector3D<T>::operator *(vector3D<T> param)
{
	//This function computes the dot product between two vectors
	return x*param.x + y*param.y + z*param.z;
}

template <class T>
vector3D<T> vector3D<T>::X(vector3D<T> param)
{
	vector3D<T> result;
	result.x=y*param.z - z*param.y;
	result.y=z*param.x - x*param.z;
	result.z=x*param.y - y*param.x;

	return result;
}

template <class T>
vector3D<T> vector3D<T>::Normalize()
{
	T length = Length();
	if(length ==0.0)
	{
		x = y = z = 0;
	}
	else
	{
		x/= length;
		y /=length;
		z /=length;
	}
	return *this;
}

template <class T>
T vector3D<T>::Length()
{
	return sqrt(x*x + y*y + z*z);
}

template <class T>
void vector3D<T>::print()
{
	cout<<"["<<x<<","<<y<<","<<z<<"]"<<endl;
}

#endif