ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/math/Vector.hpp
(Generate patch)

Comparing trunk/OOPSE-4/src/math/Vector.hpp (file contents):
Revision 1563 by tim, Wed Oct 13 06:51:09 2004 UTC vs.
Revision 1630 by tim, Thu Oct 21 21:31:39 2004 UTC

# Line 35 | Line 35
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, int Dim>
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++)
# Line 67 | Line 89 | namespace oopse {
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( double* v) {
101 <                for (unsigned int i = 0; i < Dim; i++)
102 <                    data_[i] = v[i];
100 >            inline Vector( Real* v) {
101 >                for (unsigned int i = 0; i < Dim; i++)
102 >                    data_[i] = v[i];
103              }
104  
105              /**
# Line 79 | Line 107 | namespace oopse {
107               * @return reference of ith element
108               * @param i index
109               */
110 <            inline double& operator[](unsigned int  i) {
110 >            inline Real& operator[](unsigned int  i) {
111                  assert( i < Dim);
112                  return data_[i];
113              }
# Line 89 | Line 117 | namespace oopse {
117               * @return reference of ith element
118               * @param i index
119               */
120 <            inline double& operator()(unsigned int  i) {
120 >            inline Real& operator()(unsigned int  i) {
121                  assert( i < Dim);
122                  return data_[i];
123              }
# Line 99 | Line 127 | namespace oopse {
127               * @return reference of ith element
128               * @param i index
129               */
130 <            inline  const double& operator[](unsigned int i) const {
130 >            inline  const Real& operator[](unsigned int i) const {
131                  assert( i < Dim);
132                  return data_[i];
133              }
# Line 109 | Line 137 | namespace oopse {
137               * @return reference of ith element
138               * @param i index
139               */
140 <            inline  const double& operator()(unsigned int i) const {
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 <                data_[0] = -data_[0];
178 <                data_[1] = -data_[1];
121 <                data_[2] = -data_[2];
177 >                for (unsigned int i = 0; i < Dim; i++)
178 >                    data_[i] = -data_[i];
179              }
180  
181              /**
# Line 136 | Line 193 | namespace oopse {
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 <                }
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).
# Line 146 | Line 203 | namespace oopse {
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];
206 >                for (unsigned int i = 0; i < Dim; i++)
207 >                    data_[i] = v1.data_[i] + v2.data_[i];
208              }
209  
210              /**
# Line 173 | Line 230 | namespace oopse {
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( double s ) {
234 <                for (unsigned int i = 0; i < Dim; i++)
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
185            * @param v1 the vector
243              */
244 <            inline void mul( double s, const Vector<Real, Dim>& v1 ) {
245 <                for (unsigned int i = 0; i < Dim; i++)
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  
# Line 193 | Line 250 | namespace oopse {
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( double s) {
254 <                for (unsigned int i = 0; i < Dim; i++)            
253 >            inline void div( Real s) {
254 >                for (unsigned int i = 0; i < Dim; i++)            
255                      data_[i] /= s;
256              }
257  
# Line 203 | Line 260 | namespace oopse {
260              * @param v1 the source vector
261              * @param s the scalar value
262              */                        
263 <            inline void div( const Vector<Real, Dim>& v1, double s ) {
264 <                for (unsigned int i = 0; i < Dim; i++)
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 ) {
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 ) {
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 *=( double s) {
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 /=( double s ) {
287 >            inline Vector<Real, Dim>& operator /=( Real s ) {
288                  div(s);
289                  return *this;
290              }
# Line 236 | Line 293 | namespace oopse {
293               * Returns the length of this vector.
294               * @return the length of this vector
295               */
296 <             inline double length() {
297 <                return sqrt(lengthSquared());  
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 double lengthSquared() {
304 >             inline Real lengthSquare() {
305                  return dot(*this, *this);
306              }
307              
308              /** Normalizes this vector in place */
309              inline void normalize() {
310 <                double len;
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 <            double data_[3];
329 >            Real data_[Dim];
330          
331      };
332  
333      /** unary minus*/
334 <    template<typename Real, int Dim>    
334 >    template<typename Real, unsigned int Dim>    
335      inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
336 <        Vector tmp(v1);
337 <        return tmp.negate();
336 >        Vector<Real, Dim> tmp(v1);
337 >        tmp.negate();
338 >        return tmp;
339      }
340  
341      /**
# Line 274 | Line 344 | namespace oopse {
344       * @param v1 the first vector
345       * @param v2 the second vector
346       */  
347 <    template<typename Real, int Dim>    
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          
# Line 288 | Line 358 | namespace oopse {
358       * @param v1 the first vector
359       * @param v2 the second vector
360       */  
361 <    template<typename Real, int Dim>    
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);
# Line 301 | Line 371 | namespace oopse {
371       * @param v1 the source vector
372       * @param s the scalar value
373       */
374 <    template<typename Real, int Dim>                
375 <    Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) {      
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(s, v1);
377 >        result.mul(v1,s);
378          return result;          
379      }
380      
# Line 314 | Line 384 | namespace oopse {
384       * @param s the scalar value
385       * @param v1 the source vector
386       */  
387 <    template<typename Real, int Dim>
388 <    Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) {
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(s, v1);
390 >        result.mul(v1, s);
391          return result;          
392      }
393  
# Line 327 | Line 397 | namespace oopse {
397       * @param v1 the source vector
398       * @param s the scalar value
399       */
400 <    template<typename Real, int Dim>    
401 <    Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) {      
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      /**
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    /**
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, int Dim>    
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;
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;
418 >        for (unsigned int i = 0; i < Dim; i++)
419 >            tmp += v1[i] * v2[i];
420 >
421 >        return tmp;
422      }
423  
424      /**
# Line 377 | Line 427 | namespace oopse {
427       * @param v2 second vector
428       * @return the distance between v1 and v2
429       */
430 <    template<typename Real, int Dim>    
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();
# Line 389 | Line 439 | namespace oopse {
439       * @param v2 second vector
440       * @return the squared distance between v1 and v2
441       */
442 <    template<typename Real, int Dim>
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();
# Line 398 | Line 448 | namespace oopse {
448      /**
449       * Write to an output stream
450       */
451 <    template<typename Real, int Dim>
452 <    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) {
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      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines