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