ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Vector3d.hpp
Revision: 1254
Committed: Wed Jun 9 16:16:33 2004 UTC (20 years, 1 month ago) by tim
File size: 5307 byte(s)
Log Message:
1. adding some useful math classes(Mat3x3d, Vector3d, Quaternion, Euler3)
 these classes use anonymous union and struct to support
 double[3], double[3][3] and double[4]
2. adding roll constraint algorithm

File Contents

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

Properties

Name Value
svn:executable *