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: 1644
Committed: Mon Oct 25 22:46:19 2004 UTC (19 years, 8 months ago) by tim
Original Path: trunk/OOPSE-3.0/src/math/Vector.hpp
File size: 15141 byte(s)
Log Message:
add getArray function to  RectMatrix and Vector classes

File Contents

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