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