ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-1.0/utils/sysbuilder/Vector3d.hpp
Revision: 1444
Committed: Fri Jul 30 14:51:44 2004 UTC (19 years, 11 months ago) by tim
File size: 4865 byte(s)
Log Message:
refine Vector3d.hpp

File Contents

# User Rev Content
1 tim 1427 #ifndef _VECTOR3D_H_
2     #define _VECTOR3D_H_
3    
4     #include <cmath>
5     #include <iostream>
6    
7     using namespace std;
8    
9     class Vector3d{
10    
11     public:
12    
13     Vector3d(){
14     this->x = double();
15     this->y = double();
16     this->z = double();
17    
18     }
19    
20     Vector3d( double x, double y, double z){
21     this->x = x;
22     this->y = y;
23     this->z = z;
24     }
25    
26     Vector3d(const Vector3d& v1){
27     this->x = v1.x;
28     this->y = v1.y;
29     this->z = v1.z;
30     }
31    
32     double& operator[](unsigned int index){
33     switch (index){
34     case 0 :
35     return x;
36    
37     case 1 :
38     return y;
39    
40     case 2 :
41     return z;
42    
43     default:
44     cerr << index <<" is invalid index" << endl;
45     exit(1);
46     }
47     }
48    
49     const double& operator[](unsigned int index) const{
50     switch (index){
51     case 0 :
52     return x;
53    
54     case 1 :
55     return y;
56    
57     case 2 :
58     return z;
59    
60     default:
61     cerr << index <<" is invalid index" << endl;
62     exit(1);
63     }
64     }
65    
66     Vector3d& operator=( const Vector3d& v1 ){
67     if(this == & v1)
68     return *this;
69    
70     this->x = v1.x;
71     this->y = v1.y;
72     this->z = v1.z;
73    
74     return *this;
75     }
76    
77     bool operator ==( const Vector3d& v1 ){
78     return this->x == v1.x && this->y == v1.y && this->z == v1.z;
79     }
80    
81     bool operator !=( const Vector3d& v1 ){
82     return this->x != v1.x || this->y != v1.y || this->z != v1.z;
83     }
84    
85     void neg(){
86     this->x = -this->x;
87     this->y = -this->y;
88     this->z = -this->z;
89     }
90    
91     void add( const Vector3d& v1 ){
92     this->x += v1.x;
93     this->y += v1.y;
94     this->z += v1.z;
95     }
96    
97     void add( const Vector3d& v1, const Vector3d &v2 ){
98     this->x = v1.x + v1.x;
99     this->y = v1.y + v2.y;
100     this->z = v1.z + v2.z;
101    
102     }
103    
104     void sub( const Vector3d& v1 ){
105     this->x -= v1.x;
106     this->y -= v1.y;
107     this->z -= v1.z;
108     }
109    
110     void sub( const Vector3d& v1, const Vector3d &v2 ){
111     this->x = v1.x - v1.x;
112     this->y = v1.y - v2.y;
113     this->z = v1.z - v2.z;
114     }
115    
116     void mul( double r ){
117     this->x *= r;
118     this->y *= r;
119     this->z *= r;
120     }
121    
122     void mul( double r, const Vector3d& v1 ){
123     this->x = r * v1.x;
124     this->y = r * v1.y;
125     this->z = r * v1.z;
126    
127     }
128    
129     void mul( const Vector3d& v1, double r ){
130     this->x = v1.x * r;
131     this->y = v1.y * r;
132     this->z = v1.z * r;
133     }
134    
135     void div( double r){
136     this->x /= r;
137     this->y /= r;
138     this->z /= r;
139     }
140    
141     void div( const Vector3d& v1, double r ){
142     this->x = v1.x/r;
143     this->y = v1.y/r;
144     this->z = v1.z/r;
145     }
146    
147     void operator +=( const Vector3d& v1 ){
148     this->x += v1.x;
149     this->y += v1.y;
150     this->z += v1.z;
151     }
152    
153     void operator -=( const Vector3d& v1 ){
154     this->x -= v1.x;
155     this->y -= v1.y;
156     this->z -= v1.z;
157     }
158    
159     void operator *=( double r ){
160     this->x *= r;
161     this->y *= r;
162     this->z *= r;
163     }
164    
165     void operator /=( double r ){
166     this->x /= r;
167     this->y /= r;
168     this->z /= r;
169     }
170    
171     //vector addition
172     friend Vector3d operator+ ( const Vector3d& v1, const Vector3d& v2){
173     return Vector3d(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
174     }
175    
176     //vector subtraction
177     friend Vector3d operator- ( const Vector3d& v1, const Vector3d& v2) {
178     return Vector3d(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
179     }
180     //unary minus
181     friend Vector3d operator- ( const Vector3d& v) {
182     return Vector3d(-v.x, -v.y, -v.z);
183     }
184     //multiply by a scalar
185     friend Vector3d operator* ( const double& r, const Vector3d& v) {
186     return Vector3d( r*v.x, r*v.y, r*v.z);
187     }
188    
189     //multiply by a scalar
190     friend Vector3d operator* ( const Vector3d& v, const double& r) {
191     return Vector3d( r*v.x, r*v.y, r*v.z);
192     }
193     //divide by a scalar
194     friend Vector3d operator/ ( const Vector3d& v, const double& r) {
195     return Vector3d( v.x/r, v.y/r, v.z/r);
196     }
197    
198    
199     //dot product
200     friend double dotProduct (const Vector3d& v1, const Vector3d& v2){
201     return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
202     }
203    
204     //cross product
205     friend Vector3d crossProduct (const Vector3d& v1, const Vector3d& v2){
206     Vector3d result;
207    
208     result.x = v1.y * v2.z - v1.z * v2.y ;
209     result.y = -v1.x * v2.z + v1.z * v2.x ;
210     result.z = v1.x * v2.y - v1.y * v2.x;
211    
212     return result;
213     }
214    
215     void normalize(){
216     double len;
217    
218     len = length();
219     x /= len ;
220     y /= len;
221     z /= len;
222     }
223    
224     double length(){
225     return sqrt(x*x + y*y + z*z);
226     }
227    
228     double length2(){
229     return x*x + y*y + z*z;
230     }
231    
232     public:
233    
234 tim 1443 double x;
235     double y;
236     double z;
237 tim 1427 };
238    
239    
240     #endif //end ifndef _VECTOR3D_H_

Properties

Name Value
svn:executable *