Biomechanical Joint Model
 Author: Anderson Maciel

vec.h

Go to the documentation of this file.
00001 #ifndef AMAURY_VEC_H
00002 #define AMAURY_VEC_H
00003 
00004 #include <iostream>
00005 #include <sstream>
00006 #include <assert.h>
00007 #include <math.h>
00008 
00009 
00010 namespace LinAlg{
00011 
00012 #define REAL double
00013 
00014  const double epsilon=1E-8;
00015  const double infinity=1E16;
00016  const int dimspace = 3;   // most vectors have dimension 3
00017 
00018 class Vector 
00019 {
00020 
00021 
00022 public:
00023     typedef         double   value_type;
00024     typedef         double*  pointer;
00025     typedef         double*  iterator;
00026     typedef         double&  reference;
00027     typedef const   double*  const_iterator;
00028     typedef const   double&  const_reference;
00029   
00030     // constructors
00031     Vector();
00032     Vector(const Vector &A);
00033     Vector(unsigned N, const double& value = double(0));
00034     Vector(unsigned N, const double* v);
00035     Vector(const float *v, unsigned N);
00036     Vector(unsigned N, const char *s);
00037 
00038     // destructor
00039     virtual ~Vector();
00040 
00041     // assignments
00042     virtual Vector& operator=(const Vector &A);
00043     Vector& operator=(const double& scalar);
00044     Vector& operator+=(const Vector &A);
00045     Vector& operator-=(const Vector &A);
00046     Vector& operator*=(const double& scalar);
00047     Vector& operator/=(const double& scalar);
00048 
00049     // methods
00050     unsigned dim() const;
00051     double norm2() const;
00052     double norm() const;
00053     double normalize();
00054 
00055     bool operator >=(const double& scalar) const;
00056     bool operator <=(const double& scalar) const;
00057     bool operator >(const double& scalar) const;
00058     bool operator <(const double& scalar) const;
00059     
00060     // to build & manipulate vectors of another dimension
00061     Vector& insert(const Vector &A, int pos=0);
00062     Vector& add(const Vector &A, int pos=0);
00063     Vector extract(int pos=0, unsigned N=dimspace) const;
00064    
00065     // compatibility with other libraries that use floats only
00066     Vector& setVecf(const float *A);
00067     float *getVecf();
00068     operator float *();
00069 
00070     // access
00071     iterator begin() { return v_;}
00072     iterator end()   { return v_ + n_; }
00073     const_iterator begin() const { return v_;}
00074     const_iterator end() const  { return v_ + n_; }
00075     reference operator[](int i);
00076     const_reference operator[](int i) const;
00077   
00078     // binary operators
00079     friend Vector operator+(const Vector &A, const Vector &B);
00080     friend Vector operator-(const Vector &A, const Vector &B);
00081     friend Vector operator*(const Vector &A, const Vector &B);
00082     friend Vector operator*(const Vector &A, double s);
00083     friend Vector operator*(double s, const Vector &A);
00084     friend Vector operator/(const Vector &A, double s);
00085     friend bool operator ==(const Vector &A, const Vector &B);
00086     friend bool operator !=(const Vector &A, const Vector &B);
00087 
00088     friend bool operator >=(const Vector &A, const Vector &B);
00089     friend bool operator <=(const Vector &A, const Vector &B);
00090     friend bool operator >(const Vector &A, const Vector &B);
00091     friend bool operator <(const Vector &A, const Vector &B);
00092 
00093     friend double dot_prod(const Vector &A, const Vector &B);
00094     friend Vector cross_prod(const Vector &A, const Vector &B);
00095 
00096     friend double angle_between(const Vector &A, const Vector &B);
00097 
00098     // unary operators
00099     friend Vector& Sqrt(Vector &A);
00100     friend Vector& abs(Vector &A);
00101     friend Vector operator-(const Vector &A);
00102 
00103 protected:
00104     double* v_;
00105     float *f_;
00106     unsigned n_, nf_;
00107     // internal helper function to create the array
00108     // of row pointers
00109     virtual void initialize(unsigned N);
00110     void copy(const double*  v);
00111     void set(const double& val);
00112     virtual void destroy(); 
00113     
00114     Vector& div(const Vector &A);
00115     Vector& div(const double& scalar);
00116 };
00117  
00118   typedef Vector VEC;
00119 
00120   std::ostream& operator<<(std::ostream &s, const Vector &A);
00121 
00122   // friend function prototypes
00123   Vector operator+(const Vector &A, const Vector &B);
00124   Vector operator-(const Vector &A, const Vector &B);
00125   Vector operator*(const Vector &A, const Vector &B);
00126   Vector operator*(const Vector &A, double s);
00127   Vector operator*(double s, const Vector &A);
00128   Vector operator/(const Vector &A, double s);
00129   bool operator ==(const Vector &A, const Vector &B);
00130   bool operator !=(const Vector &A, const Vector &B);
00131   
00132   bool operator >=(const Vector &A, const Vector &B);
00133   bool operator <=(const Vector &A, const Vector &B);
00134   bool operator >(const Vector &A, const Vector &B);
00135   bool operator <(const Vector &A, const Vector &B);
00136   
00137   double dot_prod(const Vector &A, const Vector &B);
00138   Vector cross_prod(const Vector &A, const Vector &B);
00139   double angle_between(const Vector &A, const Vector &B);
00140   
00141   // unary operators
00142   Vector& Sqrt(Vector &A);
00143   Vector& abs(Vector &A);
00144   Vector operator-(const Vector &A);
00145 
00146 
00147  // private members
00148 
00149 
00150 inline void Vector::copy(const double*  v)
00151 {
00152   memcpy(v_, v, n_*sizeof(REAL));
00153 }
00154 
00155 
00156 inline void Vector::set(const double& val)
00157 {
00158   unsigned N = n_;
00159   unsigned i;
00160 
00161   for (i=0; i< N; i++)
00162     v_[i] = val;        
00163 }
00164 
00165 
00166 inline Vector& Vector::div(const double& scalar)
00167 {
00168   assert(scalar!=0);
00169   for (int i=0; i<n_; i++)
00170     v_[i] /= scalar;
00171   return *this;
00172 }
00173  
00175 inline Vector::~Vector()
00176 { 
00177    destroy(); 
00178 }
00179 
00180 // private members
00181 
00182 inline void Vector::destroy()
00183 {       
00184    delete [] v_;
00185    delete [] f_;
00186    f_ = NULL;
00187    v_ = NULL;
00188    n_ = nf_ = 0;
00189 }
00190 
00191 
00192 inline void Vector::initialize(unsigned N)
00193 {
00194    v_ = new double[N];
00195    n_ = N;
00196 }
00197 
00198 // assignments
00199 
00200 inline Vector& Vector::operator=(const Vector &A)
00201 {
00202    if (v_ == A.v_)
00203       return *this;
00204 
00205    if (n_ == A.n_)         // no need to re-alloc
00206       copy(A.v_);
00207    else
00208    {
00209       destroy();
00210       initialize(A.n_);
00211       copy(A.v_);
00212    }
00213 
00214    return *this;
00215 }
00216 
00217 inline Vector operator*(const Vector &A, const Vector &B)
00218 {
00219     unsigned N = A.n_;
00220 
00221     assert(N==B.n_);
00222 
00223     Vector tmp(N);
00224     unsigned i;
00225 
00226     for (i=0; i<N; i++)
00227             tmp.v_[i] = A.v_[i] * B.v_[i];
00228 
00229     return tmp;
00230 }
00231 
00233 
00234 /**************[ implementation of class Vector ]********************/
00235 
00236 // constructors
00237 
00238 
00239 inline Vector::Vector() : v_(0), n_(0), nf_(0), f_(0)
00240 {}
00241 
00242 
00243 inline Vector::Vector(const Vector &A) : v_(0), n_(0), nf_(0), f_(0)
00244 {
00245   initialize(A.n_);
00246   copy(A.v_);
00247 }
00248 
00249 
00250 inline Vector::Vector(unsigned N, const double& value) :  v_(0),  n_(0), nf_(0), f_(0)
00251 {
00252   initialize(N);
00253   set(value);
00254 }
00255 
00256 
00257 inline Vector::Vector(unsigned N, const double* v) :  v_(0), n_(0), nf_(0), f_(0)
00258 {
00259   initialize(N);
00260   copy(v);
00261 }
00262  
00263 
00264 inline Vector::Vector(const  float *v, unsigned N) :  v_(0), n_(0), nf_(0), f_(0)
00265 {
00266   initialize(N);
00267   for (int i=0; i< N; i++)
00268     v_[i] = v[i];
00269 }
00270  
00271 
00272 inline Vector::Vector(unsigned N, const char *s) :  v_(0),  n_(0), nf_(0), f_(0)
00273 {
00274   initialize(N);
00275   std::istringstream ins(s);
00276 
00277   unsigned i;
00278 
00279   for (i=0; i<N; i++)
00280     ins >> v_[i];
00281 }
00282 
00283 
00284 // methods
00285 
00286 // assignments
00287 //
00288 
00289         
00290 
00291 inline Vector& Vector::operator=(const double& scalar)
00292 { 
00293   set(scalar);  
00294   return *this;
00295 }
00296 
00297 
00298 inline Vector& Vector::operator+=(const Vector &A)
00299 {
00300   assert(n_==A.n_);
00301   for (int i=0; i<n_; i++)
00302     v_[i] += A.v_[i];
00303   return *this;
00304 }
00305 
00306 
00307 inline Vector& Vector::operator-=(const Vector &A)
00308 {
00309   assert(n_==A.n_);
00310   for (int i=0; i<n_; i++)
00311     v_[i] -= A.v_[i];
00312   return *this;
00313 }
00314 
00315 
00316 inline Vector& Vector::operator*=(const double& scalar)
00317 {
00318   for (int i=0; i<n_; i++)
00319     v_[i] *= scalar;
00320   return *this;
00321 }
00322 
00323 
00324 inline Vector& Vector::operator/=(const double& scalar)
00325 {
00326   return div(scalar);
00327 }
00328 
00329 
00330 inline unsigned Vector::dim() const 
00331 {
00332   return  n_; 
00333 }
00334 
00335 
00336 inline double Vector::norm2() const 
00337 {
00338   double sum = 0;
00339 
00340   for (int i=0; i<n_; i++)
00341     sum += v_[i] * v_[i];
00342   return sum; 
00343 }
00344 
00345 
00346 inline double Vector::norm() const 
00347 {
00348   double sum = norm2();
00349   return sqrt(sum);
00350 }
00351 
00352 
00353 inline double Vector::normalize()
00354 {
00355   double length = norm();
00356   if (length>epsilon) div(length);
00357   return length;
00358 }
00359 
00360 
00361 inline bool Vector::operator >= (const double& scalar) const
00362 {
00363     for (int i=0; i<n_; i++)
00364       if (v_[i] < scalar) return false;
00365     return true;
00366 }
00367 
00368 
00369 inline bool Vector::operator <= (const double& scalar) const
00370 {
00371     for (int i=0; i<n_; i++)
00372       if (v_[i] > scalar)  return false;
00373     return true;
00374 }
00375 
00376 
00377 inline bool Vector::operator > (const double& scalar) const     
00378 {
00379     for (int i=0; i<n_; i++)
00380       if (v_[i] <= scalar) return false;
00381     return true;
00382 }
00383 
00384 
00385 inline bool Vector::operator < (const double& scalar) const      
00386 {
00387     for (int i=0; i<n_; i++)
00388       if (v_[i] >= scalar)  return false;
00389     return true;
00390 }
00391    
00392 
00393 inline Vector& Vector::setVecf(const float *A)
00394 {
00395   for (int i=0; i<n_; i++)
00396     v_[i] = A[i];
00397   return *this;
00398 }
00399 
00400 
00401 inline Vector::reference Vector::operator[](int i)
00402 { 
00403 #ifdef BOUNDS_CHECK
00404   assert(0<=i);
00405   assert(i < n_);
00406 #endif
00407   return v_[i]; 
00408 }
00409 
00410 
00411 inline Vector::const_reference Vector::operator[](int i) const
00412 {
00413 #ifdef BOUNDS_CHECK
00414   assert(0<=i);
00415   assert(i < n_);
00416 #endif
00417   return v_[i]; 
00418 }
00419 
00420 
00421 inline Vector& Vector::insert(const Vector &A, int pos)
00422 {
00423     unsigned N = A.n_;
00424 #ifdef BOUNDS_CHECK
00425     assert(0<=pos);
00426     assert(N+pos <= n_);
00427 #endif
00428     for (int i=0; i<N; i++)
00429       v_[i+pos] = A.v_[i];
00430 
00431     return *this;
00432 }
00433 
00434 
00435 inline Vector& Vector::add(const Vector &A, int pos)
00436 {
00437     unsigned N = A.n_;
00438 #ifdef BOUNDS_CHECK
00439     assert(0<=pos);
00440     assert(N+pos <= n_);
00441 #endif
00442     for (int i=0; i<N; i++)
00443       v_[i+pos] += A.v_[i];
00444 
00445     return *this;
00446 }
00447 
00448 inline Vector Vector::extract(int pos, unsigned N) const
00449 {
00450 #ifdef BOUNDS_CHECK
00451     assert(0<=pos);
00452     assert( (N+pos) <= n_);
00453 #endif
00454     Vector tmp(N);
00455 
00456     for (int i=0; i<N; i++)
00457       tmp.v_[i] = v_[i+pos];
00458 
00459     return tmp;
00460 }
00461 
00462 // to be compatible with other libraries
00463 
00464 inline float* Vector::getVecf()
00465 {   
00466   if (nf_ != n_)
00467     {
00468       delete [] f_;
00469       f_ = new float[n_];
00470     }
00471   
00472   for (int i=0; i<n_; i++) 
00473     f_[i] = v_[i];
00474         return f_;    
00475 }
00476 
00477 
00478 inline Vector::operator float *()
00479 { return getVecf(); }
00480 
00481 /****************************  I/O  ********************************/
00482 
00483 
00484 inline std::ostream& operator<<(std::ostream &s, const Vector &A)
00485 {
00486     unsigned N=A.dim();
00487 
00488     s <<  "[ ";
00489 
00490     for (unsigned i=0; i<N; i++)
00491         s   << A[i] << " ";
00492     s << "]";
00493 
00494     return s;
00495 }
00496 
00497 
00498 inline Vector operator+(const Vector &A, const Vector &B)
00499 {
00500   unsigned N = A.dim();  
00501   assert(N==B.dim());
00502   
00503   Vector tmp(N);
00504   unsigned i;
00505   
00506   for (i=0; i<N; i++)
00507       tmp.v_[i] = A.v_[i] + B.v_[i];
00508   
00509   return tmp;
00510 }
00511 
00512 
00513 inline Vector operator-(const Vector &A, const Vector &B)
00514 {
00515     unsigned N = A.n_;
00516 
00517     assert(N==B.n_);
00518 
00519     Vector tmp(N);
00520     unsigned i;
00521 
00522     for (i=0; i<N; i++)
00523       tmp.v_[i] = A.v_[i] - B.v_[i];
00524 
00525     return tmp;
00526 }
00527 
00528 
00529 
00530 inline Vector operator-(const Vector &A)
00531 {
00532     unsigned N = A.n_;
00533 
00534     Vector tmp(N);
00535     unsigned i;
00536 
00537     for (i=0; i<N; i++)
00538             tmp.v_[i] = -A.v_[i];
00539 
00540     return tmp;
00541 }
00542 
00543 
00544 inline Vector operator*(const Vector &A, double s)
00545 {
00546     unsigned N = A.n_;
00547 
00548     Vector tmp(N);
00549     unsigned i;
00550 
00551     for (i=0; i<N; i++)
00552             tmp.v_[i] = A.v_[i] * s;
00553 
00554     return tmp;
00555 }
00556 
00557 
00558 inline Vector operator*(double s, const Vector &A)
00559 {
00560     return A*s;
00561 }
00562 
00563 
00564 inline Vector operator/(const Vector &A, double s)
00565 {
00566     unsigned N = A.n_;    
00567     Vector tmp(N);
00568     unsigned i;
00569     assert(s!=0);
00570 
00571     for (i=0; i<N; i++)
00572             tmp.v_[i] = A.v_[i] / s;
00573 
00574     return tmp;
00575 }
00576 
00577 
00578 inline double dot_prod(const Vector &A, const Vector &B)
00579 {
00580     unsigned N = A.n_;
00581     assert(N == B.n_);
00582 
00583     unsigned i;
00584     double sum = 0;
00585 
00586     for (i=0; i<N; i++)
00587         sum += A.v_[i] * B.v_[i];
00588 
00589     return sum;
00590 }
00591 
00592 
00593 inline double angle_between(const Vector &A, const Vector &B)
00594 {
00595     VEC U(A), V(B);
00596     U.normalize();
00597     V.normalize();
00598 
00599     return acosf( dot_prod(U,V) );
00600 }
00601 
00602 // defined for dim-3 vectors only
00603 
00604 inline Vector cross_prod(const Vector &A, const Vector &B)
00605 {
00606     assert(A.n_==3 && B.n_==3);
00607     Vector tmp(3);
00608 
00609     tmp[0] =   A[1]*B[2] - A[2]*B[1] ;
00610     tmp[1] = - A[0]*B[2] + A[2]*B[0] ;
00611     tmp[2] =   A[0]*B[1] - A[1]*B[0] ;
00612 
00613     return tmp;
00614 }
00615 
00616 
00617 inline bool operator ==(const Vector &A, const Vector &B)
00618 {
00619     unsigned N = A.n_;
00620     if (N == B.n_)
00621     {
00622       unsigned i;
00623       for ( i=0; i<N; i++)
00624         if (A.v_[i] != B.v_[i]) break;
00625       return (i==N);
00626     }
00627     return false;
00628 }
00629 
00630 
00631 inline bool operator!=(const Vector &A, const  Vector &B)      
00632 {
00633   return !(A==B);
00634 }
00635 
00636 
00637 inline bool operator >=(const Vector &A, const Vector &B)
00638 {
00639     unsigned N = A.n_;
00640     if (N != B.n_) return false;
00641 
00642     for (int i=0; i<N; i++)
00643       if (A.v_[i] < B.v_[i]) return false;
00644     return true;
00645 }
00646 
00647 
00648 inline bool operator <=(const Vector &A, const Vector &B)
00649 {
00650     unsigned N = A.n_;
00651     if (N != B.n_) return false;
00652 
00653     for (int i=0; i<N; i++)
00654       if (A.v_[i] > B.v_[i]) return false;
00655     return true;
00656 }
00657 
00658 
00659 inline bool operator >(const Vector &A, const Vector &B)      
00660 {
00661     unsigned N = A.n_;
00662     if (N != B.n_) return false;
00663 
00664     for (int i=0; i<N; i++)
00665     {
00666        REAL delta = A.v_[i] - B.v_[i];
00667        if (delta <= 0) return false;
00668        if (fabs(delta)<1E-7) return false;
00669     }
00670     return true;
00671 }
00672 
00673 
00674 inline bool operator <(const Vector &A, const Vector &B)      
00675 {
00676     unsigned N = A.n_;
00677     if (N != B.n_) return false;
00678 
00679     for (int i=0; i<N; i++)
00680     {
00681        REAL delta = A.v_[i] - B.v_[i];
00682        if (delta >= 0) return false;
00683        if (fabs(delta)<1E-7) return false;
00684     }
00685     return true;
00686 }
00687 
00688 
00689 inline Vector& Sqrt(Vector &A)
00690 {  
00691   assert(A>=0);
00692 
00693   for (int i=0; i<A.n_; i++)
00694     A.v_[i] = sqrt(A.v_[i]);
00695   return A;
00696 }
00697 
00698 inline Vector& abs(Vector &A)
00699 {  
00700   for (int i=0; i<A.n_; i++)
00701     A.v_[i] = fabs(A.v_[i]);
00702   return A;
00703 }
00704    
00705 
00706 }
00707 
00708 
00709 #endif
00710 // VECdoubleOR_H
00711 
00712 
00713 
00714 
00715 
00716 
00717 
00718 
00719 

Generated on Thu Dec 1 10:13:40 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5