ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/math/Vector.hpp
Revision: 1564
Committed: Wed Oct 13 22:24:59 2004 UTC (19 years, 9 months ago) by tim
File size: 13264 byte(s)
Log Message:
adding RectMatrix

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