ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/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 1595 by tim, Tue Oct 19 04:21:07 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       * @class Vector Vector.hpp "math/Vector.hpp"
61       * @brief Fix length vector class
62       */
63 <    template<typename Real, int Dim>
63 >    template<typename Real, unsigned int Dim>
64      class Vector{
65          public:
66  
# Line 67 | Line 85 | namespace oopse {
85                  
86                  return *this;
87              }
88 +
89 +            template<typename T>
90 +            inline Vector(const T& s){
91 +                for (unsigned int i = 0; i < Dim; i++)
92 +                    data_[i] = s;
93 +            }
94              
95              /** Constructs and initializes a Vector from an array */            
96              inline Vector( double* v) {
97 <                for (unsigned int i = 0; i < Dim; i++)
98 <                    data_[i] = v[i];
97 >                for (unsigned int i = 0; i < Dim; i++)
98 >                    data_[i] = v[i];
99              }
100  
101              /**
# Line 112 | Line 136 | namespace oopse {
136              inline  const double& operator()(unsigned int i) const {
137                  assert( i < Dim);
138                  return data_[i];
139 +            }
140 +
141 +            /**
142 +             * Tests if this vetor is equal to other vector
143 +             * @return true if equal, otherwise return false
144 +             * @param v vector to be compared
145 +             */
146 +             inline bool operator ==(const Vector<Real, Dim>& v) {
147 +
148 +                for (unsigned int i = 0; i < Dim; i ++) {
149 +                    if (!equal(data_[i], v[i])) {
150 +                        return false;
151 +                    }
152 +                }
153 +                
154 +                return true;
155              }
156  
157 +            /**
158 +             * Tests if this vetor is not 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 +                return !(*this == v);
164 +            }
165 +            
166              /** Negates the value of this vector in place. */          
167              inline void negate() {
168 <                data_[0] = -data_[0];
169 <                data_[1] = -data_[1];
121 <                data_[2] = -data_[2];
168 >                for (unsigned int i = 0; i < Dim; i++)
169 >                    data_[i] = -data_[i];
170              }
171  
172              /**
# Line 136 | Line 184 | namespace oopse {
184              * @param v1 the other vector
185              */
186              inline void add( const Vector<Real, Dim>& v1 ) {
187 <                for (unsigned int i = 0; i < Dim; i++)
188 <                    data_[i] += v1.data_[i];
187 >            for (unsigned int i = 0; i < Dim; i++)
188 >                data_[i] += v1.data_[i];
189                  }
190  
191              /**
# Line 146 | Line 194 | namespace oopse {
194              * @param v2 the second vector
195              */
196              inline void add( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
197 <                for (unsigned int i = 0; i < Dim; i++)
198 <                    data_[i] = v1.data_[i] + v2.data_[i];
197 >                for (unsigned int i = 0; i < Dim; i++)
198 >                    data_[i] = v1.data_[i] + v2.data_[i];
199              }
200  
201              /**
# Line 174 | Line 222 | namespace oopse {
222              * @param s the scalar value
223              */
224              inline void mul( double s ) {
225 <                for (unsigned int i = 0; i < Dim; i++)
225 >                for (unsigned int i = 0; i < Dim; i++)
226                     data_[i] *= s;
227              }
228  
229              /**
230              * Sets the value of this vector to the scalar multiplication of vector v1  
231              * (*this = s * v1).
232 +            * @param v1 the vector            
233              * @param s the scalar value
185            * @param v1 the vector
234              */
235 <            inline void mul( double s, const Vector<Real, Dim>& v1 ) {
236 <                for (unsigned int i = 0; i < Dim; i++)
235 >            inline void mul( const Vector<Real, Dim>& v1, double s) {
236 >                for (unsigned int i = 0; i < Dim; i++)
237                      data_[i] = s * v1.data_[i];
238              }
239  
# Line 194 | Line 242 | namespace oopse {
242              * @param s the scalar value
243              */            
244              inline void div( double s) {
245 <                for (unsigned int i = 0; i < Dim; i++)            
245 >                for (unsigned int i = 0; i < Dim; i++)            
246                      data_[i] /= s;
247              }
248  
# Line 204 | Line 252 | namespace oopse {
252              * @param s the scalar value
253              */                        
254              inline void div( const Vector<Real, Dim>& v1, double s ) {
255 <                for (unsigned int i = 0; i < Dim; i++)
255 >                for (unsigned int i = 0; i < Dim; i++)
256                      data_[i] = v1.data_[i] / s;
257              }
258  
259              /** @see #add */
260 <            inline Vector<Real, Dim> operator +=( const Vector<Real, Dim>& v1 ) {
260 >            inline Vector<Real, Dim>& operator +=( const Vector<Real, Dim>& v1 ) {
261                  add(v1);
262                  return *this;
263              }
264  
265              /** @see #sub */
266 <            inline Vector<Real, Dim> operator -=( const Vector<Real, Dim>& v1 ) {
266 >            inline Vector<Real, Dim>& operator -=( const Vector<Real, Dim>& v1 ) {
267                  sub(v1);
268                  return *this;
269              }
270  
271              /** @see #mul */
272 <            inline Vector<Real, Dim> operator *=( double s) {
272 >            inline Vector<Real, Dim>& operator *=( double s) {
273                  mul(s);
274                  return *this;
275              }
276  
277              /** @see #div */
278 <            inline Vector<Real, Dim> operator /=( double s ) {
278 >            inline Vector<Real, Dim>& operator /=( double s ) {
279                  div(s);
280                  return *this;
281              }
# Line 244 | Line 292 | namespace oopse {
292               * Returns the squared length of this vector.
293               * @return the squared length of this vector
294               */
295 <             inline double lengthSquared() {
295 >             inline double lengthSquare() {
296                  return dot(*this, *this);
297              }
298              
# Line 253 | Line 301 | namespace oopse {
301                  double len;
302  
303                  len = length();
304 +                
305 +                //if (len < oopse:epsilon)
306 +                //  throw();
307 +                
308                  *this /= len;
309              }
310 +
311 +            /**
312 +             * Tests if this vector is normalized
313 +             * @return true if this vector is normalized, otherwise return false
314 +             */
315 +            inline bool isNormalized() const
316 +            {
317 +                return lengthSquare() == 1.0;
318 +            }          
319              
320          protected:
321 <            double data_[3];
321 >            double data_[Dim];
322          
323      };
324  
325      /** unary minus*/
326 <    template<typename Real, int Dim>    
326 >    template<typename Real, unsigned int Dim>    
327      inline Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1){
328 <        Vector tmp(v1);
329 <        return tmp.negate();
328 >        Vector<Real, Dim> tmp(v1);
329 >        tmp.negate();
330 >        return tmp;
331      }
332  
333      /**
# Line 274 | Line 336 | namespace oopse {
336       * @param v1 the first vector
337       * @param v2 the second vector
338       */  
339 <    template<typename Real, int Dim>    
339 >    template<typename Real, unsigned int Dim>    
340      inline Vector<Real, Dim> operator +(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
341          Vector<Real, Dim> result;
342          
# Line 288 | Line 350 | namespace oopse {
350       * @param v1 the first vector
351       * @param v2 the second vector
352       */  
353 <    template<typename Real, int Dim>    
353 >    template<typename Real, unsigned int Dim>    
354      Vector<Real, Dim> operator -(const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2) {
355          Vector<Real, Dim> result;
356          result.sub(v1, v2);
# Line 301 | Line 363 | namespace oopse {
363       * @param v1 the source vector
364       * @param s the scalar value
365       */
366 <    template<typename Real, int Dim>                
366 >    template<typename Real, unsigned int Dim>                
367      Vector<Real, Dim> operator * ( const Vector<Real, Dim>& v1, double s) {      
368          Vector<Real, Dim> result;
369 <        result.mul(s, v1);
369 >        result.mul(v1,s);
370          return result;          
371      }
372      
# Line 314 | Line 376 | namespace oopse {
376       * @param s the scalar value
377       * @param v1 the source vector
378       */  
379 <    template<typename Real, int Dim>
379 >    template<typename Real, unsigned int Dim>
380      Vector<Real, Dim> operator * ( double s, const Vector<Real, Dim>& v1 ) {
381          Vector<Real, Dim> result;
382 <        result.mul(s, v1);
382 >        result.mul(v1, s);
383          return result;          
384      }
385  
# Line 327 | Line 389 | namespace oopse {
389       * @param v1 the source vector
390       * @param s the scalar value
391       */
392 <    template<typename Real, int Dim>    
392 >    template<typename Real, unsigned int Dim>    
393      Vector<Real, Dim> operator / ( const Vector<Real, Dim>& v1, double s) {      
394          Vector<Real, Dim> result;
395          result.div( v1,s);
# Line 335 | Line 397 | namespace oopse {
397      }
398      
399      /**
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    /**
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 <    template<typename Real, int Dim>    
405 >    template<typename Real, unsigned int Dim>    
406      inline Real dot( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
407 <                Real tmp;
408 <                tmp = 0;
407 >        Real tmp;
408 >        tmp = 0;
409  
410 <                for (unsigned int i = 0; i < Dim; i++)
411 <                        tmp += v1[i] + v2[i];
412 <                
413 <                return tmp;
410 >        for (unsigned int i = 0; i < Dim; i++)
411 >            tmp += v1[i] + v2[i];
412 >
413 >        return tmp;
414      }
415  
416      /**
# Line 377 | Line 419 | namespace oopse {
419       * @param v2 second vector
420       * @return the distance between v1 and v2
421       */
422 <    template<typename Real, int Dim>    
422 >    template<typename Real, unsigned int Dim>    
423      inline Real distance( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
424          Vector<Real, Dim> tempVector = v1 - v2;
425          return tempVector.length();
# Line 389 | Line 431 | namespace oopse {
431       * @param v2 second vector
432       * @return the squared distance between v1 and v2
433       */
434 <    template<typename Real, int Dim>
434 >    template<typename Real, unsigned int Dim>
435      inline Real distanceSquare( const Vector<Real, Dim>& v1, const Vector<Real, Dim>& v2 ) {
436          Vector<Real, Dim> tempVector = v1 - v2;
437          return tempVector.lengthSquare();
# Line 398 | Line 440 | namespace oopse {
440      /**
441       * Write to an output stream
442       */
443 <    template<typename Real, int Dim>
444 <    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v1 ) {
445 <        
443 >    template<typename Real, unsigned int Dim>
444 >    std::ostream &operator<< ( std::ostream& o, const Vector<Real, Dim>& v) {
445 >
446 >        o << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]" << endl;
447          return o;        
448      }
449      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines