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