Blame view

rts/rtsVector3d.h 3.71 KB
ebb721c7   David Mayerich   new repository fo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  ///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