ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/math/Vector3d.hpp
Revision: 1561
Committed: Tue Oct 12 23:57:40 2004 UTC (19 years, 8 months ago) by tim
File size: 16774 byte(s)
Log Message:
add Vector3d.hpp and  Matrix3x3d.hpp

File Contents

# Content
1 /*
2 * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project
3 *
4 * Contact: oopse@oopse.org
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
10 * All we ask is that proper credit is given for our work, which includes
11 * - but is not limited to - adding the above copyright notice to the beginning
12 * of your source code files, and to any copyright notice that you may distribute
13 * with programs based on this work.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 */
25
26 /**
27 * @file Vector3d.hpp
28 * @author Teng Lin
29 * @date 09/14/2004
30 * @version 1.0
31 */
32
33 #ifndef MATH_VECTOR_HPP
34 #define MATH_VECTOR_HPP
35
36 #include <cassert>
37 #include <cmath>
38
39 namespace oopse {
40
41 /**
42 * @class Vector3d Vector3d.hpp "math/Vector3d.hpp"
43 * @brief Fix length vector class
44 */
45 class Vector3d{
46 public:
47
48 /** default constructor */
49 inline Vector3d(){
50 data_[0] = 0.0;
51 data_[1] = 0.0;
52 data_[2] = 0.0;
53 }
54
55 /** Constructs and initializes a Vector3d from an array */
56 inline Vector3d( double* v) {
57 data_[0] = v[0];
58 data_[1] = v[1];
59 data_[2] = v[2];
60 }
61
62 /** Constructs and initializes a Vector3d from x, y, z coordinates */
63 inline Vector3d( double x, double y, double z) {
64 data_[0] = x;
65 data_[1] = y;
66 data_[2] = z;
67 }
68
69 /**
70 * Returns reference of ith element.
71 * @return reference of ith element
72 * @param i index
73 */
74 inline double& operator[](unsigned int i) {
75 assert( i < 3);
76 return data_[i]
77 }
78
79 /**
80 * Returns reference of ith element.
81 * @return reference of ith element
82 * @param i index
83 */
84 inline double& operator()(unsigned int i) {
85 assert( i < 3);
86 return data_[i]
87 }
88
89 /**
90 * Returns constant reference of ith element.
91 * @return reference of ith element
92 * @param i index
93 */
94 inline const double& operator[](unsigned int i) const {
95 assert( i < 3);
96 return data_[i]
97 }
98
99 /**
100 * Returns constant reference of ith element.
101 * @return reference of ith element
102 * @param i index
103 */
104 inline const double& operator()(unsigned int i) const {
105 assert( i < 3);
106 return data_[i]
107 }
108 /**
109 * Retunrs reference of the first element of Vector3.
110 * @return reference of the first element of Vector3
111 */
112 inline double& x() { return data_[0];}
113
114 /**
115 * Retunrs the first element of Vector3.
116 * @return the first element of Vector3
117 */
118 inline double x() const { return data_[0];}
119
120 /**
121 * Retunrs reference of the second element of Vector3.
122 * @return reference of the second element of Vector3
123 */
124 inline double& y() { return data_[1];}
125
126 /**
127 * Retunrs the second element of Vector3.
128 * @return c the second element of Vector3
129 */
130 inline double y() const { return data_[1];}
131
132 /**
133 * Retunrs reference of the third element of Vector3.
134 * @return reference of the third element of Vector3
135 */
136 inline double& z() { return data_[2];}
137
138 /**
139 * Retunrs the third element of Vector3.
140 * @return f the third element of Vector3
141 */
142 inline double z() const { return data_[2];}
143
144 /**
145 * Returns multiplication of matrix and vector
146 * @return multiplication of matrix and vector
147 * @param m matrix
148 * @param v vector
149 */
150 /** Negates the value of this vector in place. */
151 inline void negate() {
152 data_[0] = -data_[0];
153 data_[1] = -data_[1];
154 data_[2] = -data_[2];
155 }
156
157 /**
158 * Sets the value of this vector to the negation of vector v1.
159 * @param v1 the source vector
160 */
161 inline void negate(const Vector3d& v1) {
162 data_[0] = -v1.data_[0];
163 data_[1] = -v1.data_[1];
164 data_[2] = -v1.data_[2];
165 }
166
167 /**
168 * Sets the value of this vector to the sum of itself and v1 (*this += v1).
169 * @param v1 the other vector
170 */
171 inline void add( const Vector3d& v1 ) {
172 data_[0] += v1.data_[0];
173 data_[1] += v1.data_[1];
174 data_[2] += v1.data_[2];
175 }
176
177 /**
178 * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
179 * @param v1 the first vector
180 * @param v2 the second vector
181 */
182 inline void add( const Vector3d& v1, const Vector3d& v2 ) {
183 data_[0] = v1.data_[0] + v2.data_[0];
184 data_[1] = v1.data_[1] + v2.data_[1];
185 data_[2] = v1.data_[2] + v2.data_[2];
186 }
187
188 /**
189 * Sets the value of this vector to the difference of itself and v1 (*this -= v1).
190 * @param v1 the other vector
191 */
192 inline void sub( const Vector3d& v1 ) {
193 data_[0] -= v1.data_[0];
194 data_[1] -= v1.data_[1];
195 data_[2] -= v1.data_[2];
196 }
197
198 /**
199 * Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2).
200 * @param v1 the first vector
201 * @param v2 the second vector
202 */
203 inline void sub( const Vector3d& v1, const Vector3d &v2 ){
204 data_[0] = v1.data_[0] - v2.data_[0];
205 data_[1] = v1.data_[1] - v2.data_[1];
206 data_[2] = v1.data_[2] - v2.data_[2];
207 }
208
209 /**
210 * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
211 * @param s the scalar value
212 */
213 inline void mul( double s ) {
214 data_[0] *= s;
215 data_[1] *= s;
216 data_[2] *= s;
217 }
218
219 /**
220 * Sets the value of this vector to the scalar multiplication of vector v1
221 * (*this = s * v1).
222 * @param s the scalar value
223 * @param v1 the vector
224 */
225 inline void mul( double s, const Vector3d& v1 ) {
226 data_[0] = s * v1.data_[0];
227 data_[1] = s * v1.data_[1];
228 data_[2] = s * v1.data_[2];
229 }
230
231 /**
232 * Sets the value of this vector to the scalar division of itself (*this /= s ).
233 * @param s the scalar value
234 */
235 inline void div( double s) {
236 data_[0] /= s;
237 data_[1] /= s;
238 data_[2] /= s;
239 }
240
241 /**
242 * Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ).
243 * @paran v1 the source vector
244 * @param s the scalar value
245 */
246 inline void div( const Vector3d& v1, double s ) {
247 data_[0] = v1.data_[0] / s;
248 data_[1] = v1.data_[1] /s;
249 data_[2] = v1.data_[2] /s;
250 }
251
252 /** @see #add */
253 inline void operator +=( const Vector3d& v1 ) {
254 data_[0] += v1.data_[0];
255 data_[1] += v1.data_[1];
256 data_[2] += v1.data_[2];
257 }
258
259 /** @see #sub */
260 inline void operator -=( const Vector3d& v1 ) {
261 data_[0] -= v1.data_[0];
262 data_[1] -= v1.data_[1];
263 data_[2] -= v1.data_[2];
264 }
265
266 /** @see #mul */
267 inline void operator *=( double s) {
268 data_[0] *= s;
269 data_[1] *= s;
270 data_[2] *= s;
271 }
272
273 /** @see #div */
274 inline void operator /=( double s ) {
275 data_[0] /= s;
276 data_[1] /= s;
277 data_[2] /= s;
278 }
279
280 /**
281 * Returns the length of this vector.
282 * @return the length of this vector
283 */
284 inline double length() {
285 return sqrt(data_[0] * data_[0] + data_[1] * data_[1] + data_[2] * data_[2]);
286 }
287
288 /**
289 * Returns the squared length of this vector.
290 * @return the squared length of this vector
291 */
292 inline double lengthSquared() {
293 return data_[0] * data_[0] + data_[1] * data_[1] + data_[2] * data_[2];
294 }
295
296 /** Normalizes this vector in place */
297 inline void normalize() {
298 double len;
299
300 len = length();
301
302 //if (len == 0)
303 // throw Exception("");
304
305 *this /= len;
306 }
307
308 /** unary minus*/
309 friend inline Vector3d operator -(const Vector3d& v1);
310
311 /**
312 * Return the sum of two vectors (v1 - v2).
313 * @return the sum of two vectors
314 * @param v1 the first vector
315 * @param v2 the second vector
316 */
317 friend inline Vector3d operator +( const Vector3d& v1 const Vector3d& v2) const;
318
319 /**
320 * Return the difference of two vectors (v1 - v2).
321 * @return the difference of two vectors
322 * @param v1 the first vector
323 * @param v2 the second vector
324 */
325 friend inline Vector3d operator -(const Vector3d& v1 const Vector3d& v2) const;
326
327 /**
328 * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
329 * @return the vaule of scalar multiplication of this vector
330 * @param v1 the source vector
331 * @param s the scalar value
332 */
333 friend inline Vector3d operator * ( const Vector3d& v1, double s);
334
335 /**
336 * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
337 * @return the vaule of scalar multiplication of this vector
338 * @param s the scalar value
339 * @param v1 the source vector
340 */
341 friend inline Vector3d operator * ( double s, const Vector3d& v1 );
342
343 /**
344 * Returns the value of division of a vector by a scalar.
345 * @return the vaule of scalar division of this vector
346 * @param v1 the source vector
347 * @param s the scalar value
348 */
349 friend inline Vector3d operator /( const Vector3d& v1, double s ) const;
350
351 /**
352 * Returns the value of division of a vector by a scalar.
353 * @return the vaule of scalar division of this vector
354 * @param s the scalar value
355 * @param v1 the source vector
356 */
357 friend inline Vector3d operator /( double s, const Vector3d& v1 ) const;
358
359 /** */
360 friend inline bool epsilonEqual( const Vector3d& v1, const Vector3d& v2);
361
362 /**
363 * Returns the dot product of two Vectors
364 * @param v1 first vector
365 * @param v2 second vector
366 * @return the dot product of v1 and v2
367 */
368 friend inline double dot( const Vector3d& v1, const Vector3d& v2 );
369
370 /**
371 * Returns the cross product of two Vectors
372 * @param v1 first vector
373 * @param v2 second vector
374 * @return the cross product of v1 and v2
375 * @see #vector::dot
376 */
377 friend inline Vector3d cross( const Vector3d& v1, const Vector3d& v2 );
378
379 /**
380 * Returns the distance between two Vectors
381 * @param v1 first vector
382 * @param v2 second vector
383 * @return the distance between v1 and v2
384 */
385 friend inline double distance( const Vector3d& v1, const Vector3d& v2 );
386
387 /**
388 * Returns the squared distance between two Vectors
389 * @param v1 first vector
390 * @param v2 second vector
391 * @return the squared distance between v1 and v2
392 */
393 friend inline double distanceSquare( const Vector3d& v1, const Vector3d& v2 );
394
395 /**
396 * Write to an output stream
397 */
398 friend std::ostream &operator<< ( std::ostream& o, const Vector3d& v1 );
399
400 private:
401 double data_[3];
402
403 };
404
405 Vector3d operator -(const Vector3d& v1){
406 Vector3d tmp(v1);
407 return tmp.negate();
408 }
409
410 Vector3d operator +(const Vector3d& v1, const Vector3d& v2) {
411 Vector3d result;
412
413 result.data_[0] = v1.data_[0] + v2.data_[0];
414 result.data_[1] = v1.data_[1] + v2.data_[1];
415 result.data_[2] = v1.data_[2] + v2.data_[2];
416
417 return result;
418 }
419
420 Vector3d operator -(const Vector3d& v1, const Vector3d& v2) {
421 Vector3d result;
422
423 result.data_[0] = v1.data_[0] - v2.data_[0];
424 result.data_[1] = v1.data_[1] - v2.data_[1];
425 result.data_[2] = v1.data_[2] - v2.data_[2];
426
427 return result;
428 }
429
430 Vector3d operator * ( const Vector3d& v1, double s) {
431 Vector3d result;
432
433 result.data_[0] = v1.data_[0] * s;
434 result.data_[1] = v1.data_[1] * s;
435 result.data_[2] = v1.data_[2] * s;
436
437 return result;
438 }
439
440 Vector3d operator * ( double s, const Vector3d& v1 ) {
441 return v1 * s;
442 }
443
444 Vector3d operator / ( const Vector3d& v1, double s) {
445 Vector3d result;
446
447 result.data_[0] = v1.data_[0] / s;
448 result.data_[1] = v1.data_[1] / s;
449 result.data_[2] = v1.data_[2] / s;
450
451 return result;
452 }
453
454 Vector3d operator / ( double s, const Vector3d& v1 ) {
455 return v1 / s;
456 }
457
458
459 bool epsilonEqual( const Vector3d& v1, const Vector3d& v2 ) {
460
461 }
462
463 double dot( const Vector3d& v1, const Vector3d& v2 ) {
464 return v1.data_[0] * v2.data_[0] +
465 v1.data_[1] * v2.data_[1] +
466 v1.data_[2] * v2.data_[2];
467 }
468
469 Vector3d cross( const Vector3d& v1, const Vector3d& v2 ) {
470 Vector3d result;
471
472 result.data_[0] = v1.data_[1] * v2.data_[2] - v1.data_[2] * v2.data_[1] ;
473 result.data_[1] = v1.data_[2] * v2.data_[0] - v1.data_[0] * v2.data_[2] ;
474 result.data_[2] = v1.data_[0] * v2.data_[1] - v1.data_[1] * v2.data_[0];
475
476 return result;
477 }
478
479 double distance( const Vector3d& v1, const Vector3d& v2 ) {
480 Vector3d tempVector = v1 - v2;
481 return tempVector.length();
482 }
483
484 double distanceSquare( const Vector3d& v1, const Vector3d& v2 ) {
485 Vector3d tempVector = v1 - v2;
486 return tempVector.lengthSquare();
487 }
488
489 std::ostream &operator<< ( std::ostream& o, const Vector3d& v1 ) {
490 o << " x = " << v1.data_[0] <<" y = " << v1.data_[1] << " z = " << v1.data_[2];
491 return o;
492 }
493
494
495 }
496
497
498 #endif

Properties

Name Value
svn:executable *