ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/math/Vector.hpp
Revision: 1586
Committed: Sun Oct 17 01:19:11 2004 UTC (19 years, 8 months ago) by tim
Original Path: trunk/OOPSE-3.0/src/math/Vector.hpp
File size: 14915 byte(s)
Log Message:
math library in progress

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 Vector.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 #include <iostream>
39
40 namespace oopse {
41
42 const double epsilon = 0.000001;
43
44 template<typename T>
45 inline bool equal(T e1, T e2) {
46 return e1 == e2;
47 }
48
49 template<>
50 inline bool equal(float e1, float e2) {
51 return fabs(e1 - e2) < epsilon;
52 }
53
54 template<>
55 inline bool equal(double e1, double e2) {
56 return fabs(e1 - e2) < epsilon;
57 }
58
59 /**
60 * @class Vector Vector.hpp "math/Vector.hpp"
61 * @brief Fix length vector class
62 */
63 template<typename Real, unsigned int Dim>
64 class Vector{
65 public:
66
67 /** default constructor */
68 inline Vector(){
69 for (unsigned int i = 0; i < Dim; i++)
70 data_[i] = 0.0;
71 }
72
73 /** Constructs and initializes a Vector from a vector */
74 inline Vector(const Vector<Real, Dim>& v) {
75 *this = v;
76 }
77
78 /** copy assignment operator */
79 inline Vector<Real, Dim>& operator=(const Vector<Real, Dim>& v) {
80 if (this == &v)
81 return *this;
82
83 for (unsigned int i = 0; i < Dim; i++)
84 data_[i] = v[i];
85
86 return *this;
87 }
88
89 /** Constructs and initializes a Vector from an array */
90 inline Vector( double* v) {
91 for (unsigned int i = 0; i < Dim; i++)
92 data_[i] = v[i];
93 }
94
95 /**
96 * Returns reference of ith element.
97 * @return reference of ith element
98 * @param i index
99 */
100 inline double& operator[](unsigned int i) {
101 assert( i < Dim);
102 return data_[i];
103 }
104
105 /**
106 * Returns reference of ith element.
107 * @return reference of ith element
108 * @param i index
109 */
110 inline double& operator()(unsigned int i) {
111 assert( i < Dim);
112 return data_[i];
113 }
114
115 /**
116 * Returns constant reference of ith element.
117 * @return reference of ith element
118 * @param i index
119 */
120 inline const double& operator[](unsigned int i) const {
121 assert( i < Dim);
122 return data_[i];
123 }
124
125 /**
126 * Returns constant reference of ith element.
127 * @return reference of ith element
128 * @param i index
129 */
130 inline const double& operator()(unsigned int i) const {
131 assert( i < Dim);
132 return data_[i];
133 }
134
135 /**
136 * Tests if this vetor is equal to other vector
137 * @return true if equal, otherwise return false
138 * @param v vector to be compared
139 */
140 inline bool operator ==(const Vector<Real, Dim>& v) {
141
142 for (unsigned int i = 0; i < Dim; i ++) {
143 if (!equal(data_[i], v[i])) {
144 return false;
145 }
146 }
147
148 return true;
149 }
150
151 /**
152 * Tests if this vetor is not equal to other vector
153 * @return true if equal, otherwise return false
154 * @param v vector to be compared
155 */
156 inline bool operator !=(const Vector<Real, Dim>& v) {
157 return !(*this == v);
158 }
159
160 /** Negates the value of this vector in place. */
161 inline void negate() {
162 data_[0] = -data_[0];
163 data_[1] = -data_[1];
164 data_[2] = -data_[2];
165 }
166
167 /**
168 * Sets the value of this vector to the negation of vector v1.
169 * @param v1 the source vector
170 */
171 inline void negate(const Vector<Real, Dim>& v1) {
172 for (unsigned int i = 0; i < Dim; i++)
173 data_[i] = -v1.data_[i];
174
175 }
176
177 /**
178 * Sets the value of this vector to the sum of itself and v1 (*this += v1).
179 * @param v1 the other vector
180 */
181 inline void add( const Vector<Real, Dim>& v1 ) {
182 for (unsigned int i = 0; i < Dim; i++)
183 data_[i] += v1.data_[i];
184 }
185
186 /**
187 * Sets the value of this vector to the sum of v1 and v2 (*this = v1 + v2).
188 * @param v1 the first vector
189 * @param v2 the second vector
190 */
191 inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
192 for (unsigned int i = 0; i < Dim; i++)
193 data_[i] = v1.data_[i] + v2.data_[i];
194 }
195
196 /**
197 * Sets the value of this vector to the difference of itself and v1 (*this -= v1).
198 * @param v1 the other vector
199 */
200 inline void sub( const Vector<Real, Dim>& v1 ) {
201 for (unsigned int i = 0; i < Dim; i++)
202 data_[i] -= v1.data_[i];
203 }
204
205 /**
206 * Sets the value of this vector to the difference of vector v1 and v2 (*this = v1 - v2).
207 * @param v1 the first vector
208 * @param v2 the second vector
209 */
210 inline void sub( const Vector<Real, Dim>& v1, const Vector &v2 ){
211 for (unsigned int i = 0; i < Dim; i++)
212 data_[i] = v1.data_[i] - v2.data_[i];
213 }
214
215 /**
216 * Sets the value of this vector to the scalar multiplication of itself (*this *= s).
217 * @param s the scalar value
218 */
219 inline void mul( double s ) {
220 for (unsigned int i = 0; i < Dim; i++)
221 data_[i] *= s;
222 }
223
224 /**
225 * Sets the value of this vector to the scalar multiplication of vector v1
226 * (*this = s * v1).
227 * @param s the scalar value
228 * @param v1 the vector
229 */
230 inline void mul( double s, const Vector<Real, Dim>& v1 ) {
231 for (unsigned int i = 0; i < Dim; i++)
232 data_[i] = s * v1.data_[i];
233 }
234
235 /**
236 * Sets the value of this vector to the scalar division of itself (*this /= s ).
237 * @param s the scalar value
238 */
239 inline void div( double s) {
240 for (unsigned int i = 0; i < Dim; i++)
241 data_[i] /= s;
242 }
243
244 /**
245 * Sets the value of this vector to the scalar division of vector v1 (*this = v1 / s ).
246 * @param v1 the source vector
247 * @param s the scalar value
248 */
249 inline void div( const Vector<Real, Dim>& v1, double s ) {
250 for (unsigned int i = 0; i < Dim; i++)
251 data_[i] = v1.data_[i] / s;
252 }
253
254 /** @see #add */
255 inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
256 add(v1);
257 return *this;
258 }
259
260 /** @see #sub */
261 inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
262 sub(v1);
263 return *this;
264 }
265
266 /** @see #mul */
267 inline Vector<Real, Dim>& operator *=( double s) {
268 mul(s);
269 return *this;
270 }
271
272 /** @see #div */
273 inline Vector<Real, Dim>& operator /=( double s ) {
274 div(s);
275 return *this;
276 }
277
278 /**
279 * Returns the length of this vector.
280 * @return the length of this vector
281 */
282 inline double length() {
283 return sqrt(lengthSquared());
284 }
285
286 /**
287 * Returns the squared length of this vector.
288 * @return the squared length of this vector
289 */
290 inline double lengthSquare() {
291 return dot(*this, *this);
292 }
293
294 /** Normalizes this vector in place */
295 inline void normalize() {
296 double len;
297
298 len = length();
299 *this /= len;
300 }
301
302 /**
303 * Tests if this vector is normalized
304 * @return true if this vector is normalized, otherwise return false
305 */
306 inline bool isNormalized() const
307 {
308 return lengthSquare() == 1.0;
309 }
310
311 protected:
312 double data_[3];
313
314 };
315
316 /** unary minus*/
317 template<typename Real, unsigned int Dim>
318 inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
319 Vector tmp(v1);
320 return tmp.negate();
321 }
322
323 /**
324 * Return the sum of two vectors (v1 - v2).
325 * @return the sum of two vectors
326 * @param v1 the first vector
327 * @param v2 the second vector
328 */
329 template<typename Real, unsigned int Dim>
330 inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
331 Vector<Real, Dim> result;
332
333 result.add(v1, v2);
334 return result;
335 }
336
337 /**
338 * Return the difference of two vectors (v1 - v2).
339 * @return the difference of two vectors
340 * @param v1 the first vector
341 * @param v2 the second vector
342 */
343 template<typename Real, unsigned int Dim>
344 Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
345 Vector<Real, Dim> result;
346 result.sub(v1, v2);
347 return result;
348 }
349
350 /**
351 * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
352 * @return the vaule of scalar multiplication of this vector
353 * @param v1 the source vector
354 * @param s the scalar value
355 */
356 template<typename Real, unsigned int Dim>
357 Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) {
358 Vector<Real, Dim> result;
359 result.mul(s, v1);
360 return result;
361 }
362
363 /**
364 * Returns the vaule of scalar multiplication of this vector v1 (v1 * r).
365 * @return the vaule of scalar multiplication of this vector
366 * @param s the scalar value
367 * @param v1 the source vector
368 */
369 template<typename Real, unsigned int Dim>
370 Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) {
371 Vector<Real, Dim> result;
372 result.mul(s, v1);
373 return result;
374 }
375
376 /**
377 * Returns the value of division of a vector by a scalar.
378 * @return the vaule of scalar division of this vector
379 * @param v1 the source vector
380 * @param s the scalar value
381 */
382 template<typename Real, unsigned int Dim>
383 Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) {
384 Vector<Real, Dim> result;
385 result.div( v1,s);
386 return result;
387 }
388
389 /**
390 * Returns the value of division of a vector by a scalar.
391 * @return the vaule of scalar division of this vector
392 * @param s the scalar value
393 * @param v1 the source vector
394 */
395 template<typename Real, unsigned int Dim>
396 inline Vector<Real, Dim> operator /( double s, const Vector<Real, Dim>& v1 ) {
397 Vector<Real, Dim> result;
398 result.div( v1,s);
399 return result;
400 }
401
402 /** fuzzy comparson */
403 template<typename Real, unsigned int Dim>
404 inline bool epsilonEqual( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
405
406 }
407
408
409 /**
410 * Returns the dot product of two Vectors
411 * @param v1 first vector
412 * @param v2 second vector
413 * @return the dot product of v1 and v2
414 */
415 template<typename Real, unsigned int Dim>
416 inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
417 Real tmp;
418 tmp = 0;
419
420 for (unsigned int i = 0; i < Dim; i++)
421 tmp += v1[i] + v2[i];
422
423 return tmp;
424 }
425
426 /**
427 * Returns the distance between two Vectors
428 * @param v1 first vector
429 * @param v2 second vector
430 * @return the distance between v1 and v2
431 */
432 template<typename Real, unsigned int Dim>
433 inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
434 Vector<Real, Dim> tempVector = v1 - v2;
435 return tempVector.length();
436 }
437
438 /**
439 * Returns the squared distance between two Vectors
440 * @param v1 first vector
441 * @param v2 second vector
442 * @return the squared distance between v1 and v2
443 */
444 template<typename Real, unsigned int Dim>
445 inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
446 Vector<Real, Dim> tempVector = v1 - v2;
447 return tempVector.lengthSquare();
448 }
449
450 /**
451 * Write to an output stream
452 */
453 template<typename Real, unsigned int Dim>
454 std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) {
455
456 o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl;
457 return o;
458 }
459
460 }
461 #endif