ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/utils/Vector3.hpp
Revision: 1162
Committed: Tue May 11 22:28:57 2004 UTC (20 years, 4 months ago) by tim
File size: 6324 byte(s)
Log Message:
adding generic Vector3 class

File Contents

# User Rev Content
1 tim 1162 #ifndef _VECTOR3_H_
2     #define _VECTOR3_H_
3     #include <iostream>
4     //generic vector3 class
5     template<typename Real> class Vector3{
6     public:
7     inline Vector3();
8     inline Vector3( Real x, Real y, Real z);
9     inline Vector3( Real* a);
10     inline Vector3( const Vector3<Real>& v1 );
11     inline ~Vector3() {}
12     inline Vector3<Real>& operator=( const Vector3<Real>& v1 );
13    
14     inline Real& operator[](unsigned int i) {return vec3[i];}
15    
16     inline const Real& operator[](unsigned int i) const {return vec3[i];}
17    
18     inline bool operator ==( const Vector3<Real>& v1 );
19    
20     inline bool operator !=( const Vector3<Real>& v1);
21    
22     inline void neg();
23    
24     inline void add( const Vector3<Real>& v1 );
25    
26     inline void add( const Vector3<Real>& v1, const Vector3<Real> &v2 );
27    
28     inline void sub( const Vector3<Real>& v1 );
29    
30     inline void sub( const Vector3<Real>& v1, const Vector3<Real> &v2 );
31    
32     inline void mul( Real r );
33    
34     inline void mul( Real r, const Vector3<Real>& v1 );
35    
36     inline void mul( const Vector3<Real>& v1, Real r );
37    
38     inline void div( Real r);
39    
40     inline void div( const Vector3<Real>& v1, Real r );
41    
42     inline void operator +=( const Vector3<Real>& v1 );
43    
44     inline void operator -=( const Vector3<Real>& v1 );
45    
46     inline void operator *=( Real r );
47    
48     inline void operator /=( Real r );
49    
50     inline Vector3<Real> operator+( const Vector3<Real>& v1) const;
51    
52     inline Vector3<Real> operator-( const Vector3<Real>& v1) const;
53    
54     inline Vector3<Real> operator*( Real r) const;
55    
56     inline Vector3<Real> operator/( Real r) const;
57    
58     friend Vector3<Real> operator* ( Real r, const Vector3<Real>& v1 );
59    
60     friend Vector3<Real> operator* ( const Vector3<Real>& v1, Real r);
61    
62     inline Real norm() const;
63    
64     inline Real abs() const;
65    
66     inline int normalize();
67    
68     static inline Real length( const Vector3<Real>& v1, const Vector3<Real>& v2 );
69    
70     static inline Real dot( const Vector3<Real>& v1, const Vector3<Real>& v2 );
71    
72     friend std::ostream &operator<< ( std::ostream& o, const Vector3<Real>& v1 );
73    
74     //use anonymous union and struct to provide the transparency for operator []
75     //is it compiler and platform independent?
76     union{
77     Real vec[3];
78    
79     struct{
80     Real x;
81     Real y;
82     Real z;
83     };
84     };
85     };
86    
87    
88     template<typename Real> Vector3<Real>::Vector3(){
89     this->x = Real();
90     this->y = Real();
91     this->z = Real();
92    
93     }
94    
95     template<typename Real> Vector3<Real>::Vector3( Real x, Real y, Real z){
96     this->x = x;
97     this->y = y;
98     this->z = z;
99     }
100    
101     template<typename Real> Vector3<Real>::Vector3(Real* r){
102     this->x = r[0];
103     this->y = r[1];
104     this->z = r[2];
105     }
106    
107     template<typename Real> Vector3<Real>::Vector3(const Vector3<Real>& v1){
108     this->x = v1.x;
109     this->y = v1.y;
110     this->z = v1.z;
111     }
112    
113     template<typename Real> Vector3<Real>& Vector3<Real>::operator=( const Vector3<Real>& v1 ){
114     if(this == & v1)
115     return *this;
116    
117     this->x = v1.x
118     this->y = v1.y;
119     this->z = v1.z;
120    
121     return *this;
122     }
123    
124     template<typename Real> bool Vector3<Real>::operator ==( const Vector3<Real>& v1 ){
125     return this->x == v1.x && this->y == v1.y && this->z == v1.z;
126     }
127    
128     template<typename Real> bool Vector3<Real>::operator !=( const Vector3<Real>& v1 ){
129     return this->x != v1.x || this->y != v1.y || this->z != v1.z;
130     }
131    
132     template<typename Real> void Vector3<Real>::neg(){
133     this->x = -this->x;
134     this->y = -this->y;
135     this->z = -this->z;
136     }
137    
138     template<typename Real> void Vector3<Real>::add( const Vector3<Real>& v1 ){
139     this->x += v1.x;
140     this->y += v1.y;
141     this->z += v1.z;
142     }
143    
144     template<typename Real> void Vector3<Real>::add( const Vector3<Real>& v1, const Vector3<Real> &v2 ){
145     this->x = v1.x + v1.x;
146     this->y = v1.y + v2.y;
147     this->z = v1.z + v2.z;
148    
149     }
150    
151     template<typename Real> void Vector3<Real>::sub( const Vector3<Real>& v1 ){
152     this->x -= v1.x;
153     this->y -= v1.y;
154     this->z -= v1.z;
155     }
156    
157     template<typename Real> void Vector3<Real>::sub( const Vector3<Real>& v1, const Vector3<Real> &v2 ){
158     this->x = v1.x - v1.x;
159     this->y = v1.y - v2.y;
160     this->z = v1.z - v2.z;
161     }
162    
163     template<typename Real> void Vector3<Real>::mul( Real r ){
164     this->x *= r;
165     this->y *= r;
166     this->z *= r;
167     }
168    
169     template<typename Real> void Vector3<Real>::mul( Real r, const Vector3<Real>& v1 ){
170     this->x = r * v1.x;
171     this->y = r * v1.y;
172     this->z = r * v1.z;
173    
174     }
175    
176     template<typename Real> void Vector3<Real>::mul( const Vector3<Real>& v1, Real r ){
177     this->x = v1.x * r;
178     this->y = v1.y * r;
179     this->z = v1.z * r;
180     }
181    
182     template<typename Real> void Vector3<Real>::div( Real r){
183     this->x /= r;
184     this->y /= r;
185     this->z /= r;
186     }
187    
188     template<typename Real> void Vector3<Real>::div( const Vector3<Real>& v1, Real r ){
189     this->x = v1.x/r;
190     this->y = v1.y/r;
191     this->z = v1.z/r;
192     }
193    
194     template<typename Real> void Vector3<Real>::operator +=( const Vector3<Real>& v1 ){
195     this->x += v1.x;
196     this->y += v1.y;
197     this->z += v1.z;
198     }
199    
200     template<typename Real> void Vector3<Real>::operator -=( const Vector3<Real>& v1 ){
201     this->x -= v1.x;
202     this->y -= v1.y;
203     this->z -= v1.z;
204     }
205    
206     template<typename Real> void Vector3<Real>::operator *=( Real r ){
207     this->x *= r;
208     this->y *= r;
209     this->z *= r;
210     }
211    
212     template<typename Real> void Vector3<Real>::operator /=( Real r ){
213     this->x /= r;
214     this->y /= r;
215     this->z /= r;
216     }
217    
218     template<typename Real> Vector3<Real> Vector3<Real>::operator+( const Vector3<Real>& v1) const{
219     return Vector3<Real>(x + v1.x, y + v1.y, z + v1.z);
220     }
221    
222     template<typename Real> Vector3<Real> Vector3<Real>::operator-( const Vector3<Real>& v1) const{
223     return Vector3<Real>(x - v1.x, y - v1.y, z - v1.z);
224     }
225    
226     template<typename Real> Vector3<Real> Vector3<Real>::operator*( Real r) const{
227     return Vector3<Real>(x*r, y*r, z*r);
228     }
229    
230     template<typename Real> Vector3<Real> Vector3<Real>::operator /( Real r) const{
231     return Vector3<Real>(x/r, y/r, z/r);
232     }
233    
234    
235     template <typename Real> Vector3<Real> operator* ( Real r, const Vector3<Real>& v1 ){
236     return Vector<Real>(v1.x * r, v1.y * r, v1.z * r);
237    
238     }
239    
240     template <typename Real> Vector3<Real> operator* ( const Vector3<Real>& v1, Real r){
241     return Vector<Real>(v1.x * r, v1.y * r, v1.z * r);
242     }
243    
244     typedef Vector3<double> Vector3d;
245     typedef Vector3<float> Vector3f;
246    
247     #endif //_VECTOR3_H_
248    

Properties

Name Value
svn:executable *