Biomechanical Joint Model
 Author: Anderson Maciel

vecarray.h

Go to the documentation of this file.
00001 #ifndef AMAURY_ARRAY_OF_VECS_H
00002 #define AMAURY_ARRAY_OF_VECS_H
00003 
00004 #include "vec.h"
00005 
00006 namespace LinAlg{
00007 
00008   // class for triple-indexing (can be also used for double indexing)
00009 struct idx3 {
00010    idx3() {};
00011    idx3(int i_, int j_, int k_=0) : i(i_), j(j_), k(k_) {};
00012    idx3(const idx3 &id) : i(id.i), j(id.j), k(id.k) {};
00013    
00014    void set(int i_, int j_, int k_=0) { i=i_, j=j_, k=k_; };  
00015    bool operator ==(const idx3 &id) const { return (i==id.i && j==id.j && k==id.k); };
00016    bool operator !=(const idx3 &id) const { return (i!=id.i || j!=id.j || k!=id.k); };
00017    int operator[](int m) const { return (m==0)? i : (m==1)? j: k; };
00018    int& operator[](int m) { return (m==0)? i : (m==1)? j: k; };
00019    
00020    int i, j, k;
00021 };
00022 
00023  idx3 operator+(const idx3 &id1, const idx3 &id2);
00024  idx3 operator-(const idx3 &id1, const idx3 &id2);
00025  bool operator<(const idx3 &id1, const idx3 &id2);
00026  bool operator>(const idx3 &id1, const idx3 &id2); 
00027  std::ostream& operator<<(std::ostream &s, const idx3 &id);
00028 
00029 // container (1,2 or 3D) for VECs that has built-in useful operations (+,-,*,/) 
00030 // 1D is merely a restriction of the more general 2D case (only one column),
00031 // which is in turn a restriction of the 3D case (one-depth cell).
00032 // Internally, everything is linear anyway.
00033 //
00034 // VECArray implements VEC but IS NOT a VEC (hence the private inheritance)
00035 // We simply use the convenient code provided by VEC
00036 // 
00037 // There's a bug with operator = when two operands do not have same size
00038 // It occurs when the left-hand size is empty (size zero) i.e.:
00039 // 
00040 // VECArray A;
00041 // VECArray B(10);
00042 // A = B;          // won't work because A's size is 0
00043 //
00044 // As a work-around, declare A with an initial size>0, for instance "VECArray A(1)"
00045 //
00046 class VECArray : private VEC
00047 {
00048 public:   
00049    // constructors
00050    VECArray();
00051    VECArray(const VECArray &A); // copy constructor
00052    VECArray(const VEC &A, unsigned row, unsigned col=1, unsigned depth=1);
00053    VECArray(unsigned row, unsigned col=1, unsigned depth=1, 
00054             REAL value = REAL(0), unsigned K=dimspace);
00055 
00056    virtual ~VECArray();
00057 
00058     // methods 
00059    unsigned size() const { return row_ * col_ * depth_; };
00060    unsigned getNum() const { return size(); };
00061    unsigned row() const { return row_; }; 
00062    unsigned col() const { return col_; }; 
00063    unsigned depth() const { return depth_; }; 
00064 
00065 
00066    // container stuff
00067    typedef         VEC   value_type;
00068    typedef         VEC*  pointer;
00069    typedef         VEC*  iterator;
00070    typedef         VEC&  reference;
00071    typedef const   VEC*  const_iterator;
00072    typedef const   VEC&  const_reference;  
00073    iterator begin() { return vv;}
00074    iterator end()   { return vv + size(); }
00075    const_iterator begin() const { return vv;}
00076    const_iterator end() const  { return vv + size(); }
00077 
00078    // access
00079    // 1-dim array access
00080    reference operator[](int i);  // hide base class
00081    const_reference operator[](int i) const;  
00082    // 2-dim array access
00083    reference operator()(int i, int j);
00084    const_reference operator()(int i, int j) const;
00085    // 3-dim array access
00086    reference operator[](const idx3 &id);
00087    const_reference operator[](const idx3 &id) const;  
00088 
00089    // assignments
00090    VECArray& operator=(const VECArray &A);
00091    VECArray& operator+=(const VECArray &A);
00092    VECArray& operator-=(const VECArray &A);
00093    VECArray& operator*=(const REAL& scalar);
00094    VECArray& operator/=(const REAL& scalar);
00095 
00096    bool operator >=(const REAL& scalar) { return VEC::operator>=(scalar);};
00097    bool operator <=(const REAL& scalar) { return VEC::operator<=(scalar);};
00098    bool operator >(const REAL& scalar) { return VEC::operator>(scalar);};
00099    bool operator <(const REAL& scalar) { return VEC::operator<(scalar);};
00100 
00101     // binary operators
00102    friend VECArray operator+(const VECArray &A, const VECArray &B);
00103    friend VECArray operator-(const VECArray &A, const VECArray &B);
00104    friend VECArray operator*(const VECArray &A, double s);
00105    friend VECArray operator*(double s, const VECArray &A);
00106    friend VECArray operator/(const VECArray &A, double s);
00107 
00108    // unary operators
00109    friend VECArray operator-(const VECArray &A);
00110    friend std::ostream& operator<<(std::ostream &s, const VECArray &A);
00111    friend VECArray& abs(VECArray &A);
00112 
00113 private:
00114    unsigned k_; // dimension of a contained VEC element
00115    unsigned row_, col_, depth_;
00116 
00117    virtual void initialize(unsigned N);
00118    virtual void destroy();
00119 
00120    void init_vv();
00121 
00122     // special Vector whose dimension CANNOT (and MAY NOT anyway) be changed
00123     // during a copy. ONLY to be used in class VECArray!!!
00124    struct specialVEC : public VEC
00125    {
00126       specialVEC() {};
00127       virtual ~specialVEC() { f_ = NULL; v_ = NULL; n_ = nf_ = 0; };
00128       void redirect(REAL* v) { v_ = v; };
00129       void resize(unsigned N) { n_ = N; };
00130       virtual VEC& operator=(const VEC &A);
00131    } *vv;  // vv = vec-vector
00132 };
00133 
00134  // binary operators
00135   VECArray operator+(const VECArray &A, const VECArray &B);
00136   VECArray operator-(const VECArray &A, const VECArray &B);
00137   VECArray operator*(const VECArray &A, double s);
00138   VECArray operator*(double s, const VECArray &A);
00139   VECArray operator/(const VECArray &A, double s);
00140   
00141   // unary operators
00142   VECArray operator-(const VECArray &A);
00143   std::ostream& operator<<(std::ostream &s, const VECArray &A);
00144   VECArray& abs(VECArray &A);
00145 
00146 inline idx3 operator+(const idx3 &id1, const idx3 &id2) 
00147 {
00148    idx3 id(id1);
00149    id.i += id2.i; id.j += id2.j; id.k += id2.k;
00150    return id;
00151 }
00152 
00153 inline idx3 operator-(const idx3 &id1, const idx3 &id2) 
00154 {
00155    idx3 id(id1);
00156    id.i -= id2.i; id.j -= id2.j; id.k -= id2.k;
00157    return id;
00158 }
00159 
00160 inline bool operator<(const idx3 &id1, const idx3 &id2) 
00161 {
00162    return (id1.i<id2.i && id1.j<id2.j && id1.k<id2.k);
00163 }
00164 
00165 inline bool operator>(const idx3 &id1, const idx3 &id2) 
00166 {
00167    return (id1.i>id2.i && id1.j>id2.j && id1.k>id2.k);
00168 }  
00169 
00170 inline std::ostream& operator<<(std::ostream &s, const idx3 &id)
00171 {
00172    s << "(" << id.i << ", " << id.j << ", " << id.k << ")";   
00173    return s;   
00174 }
00175 
00176 /**************[ implementation of class VECArray ]********************/
00177 
00178 // constructors
00179 inline VECArray::VECArray() : VEC(), k_(0), vv(NULL), row_(0), col_(0), depth_(0)
00180 {}
00181 
00182 // copy ctor
00183 inline VECArray::VECArray(const VECArray &A) 
00184   : k_(A.k_), vv(NULL), row_(A.row_), col_(A.col_), depth_(A.depth_)
00185 {
00186   initialize(A.n_);
00187   copy(A.v_);
00188 }
00189 
00190 inline VECArray::VECArray(const VEC &A, unsigned row, unsigned col, unsigned depth) 
00191   : k_(A.dim()), vv(NULL), row_(row), col_(col), depth_(depth)
00192 {  
00193   initialize(row*col*depth*k_);
00194   for (int i=0; i<dim()/k_; i++)
00195     for (int j=0; j<k_; j++)
00196       v_[i*k_+j] = A[j];
00197 }
00198 
00199 
00200 inline VECArray::VECArray(unsigned row, unsigned col, unsigned depth, REAL value, unsigned K) 
00201   : k_(K), vv(NULL), row_(row), col_(col), depth_(depth)
00202 {
00203   initialize(row*col*depth*K);
00204   set(value);
00205 }
00206 
00207 inline VECArray& VECArray::operator=(const VECArray &A)
00208 {
00209   VEC::operator=(A);
00210   if (k_ != A.k_)
00211   {      
00212     delete [] vv;
00213     vv = NULL;
00214     k_ = A.k_;
00215     init_vv();
00216   }
00217   return *this;
00218 }
00219 
00220 // access member
00221 inline VECArray::reference VECArray::operator[](int i)
00222 {
00223 #ifdef BOUNDS_CHECK
00224   assert(0<=i && i<dim()/k_);
00225 #endif
00226   return vv[i];
00227 }
00228 
00229 inline VECArray::const_reference VECArray::operator[](int i) const
00230 {
00231 #ifdef BOUNDS_CHECK
00232   assert(0<=i && i<dim()/k_);
00233 #endif
00234   return vv[i];
00235 }
00236 
00237 inline VECArray::reference VECArray::operator()(int i, int j)
00238 {
00239 #ifdef BOUNDS_CHECK
00240   assert(0<=i && 0<=j);
00241   assert(i < row());
00242   assert(j < col());
00243   assert(depth() == 1);
00244 #endif
00245   return vv[i*col()+j];
00246 }
00247 
00248 inline VECArray::const_reference VECArray::operator()(int i, int j) const
00249 {
00250 #ifdef BOUNDS_CHECK
00251   assert(0<=i && 0<=j);
00252   assert(i < row());
00253   assert(j < col());
00254   assert(depth() == 1);
00255 #endif
00256   return vv[i*col()+j];
00257 }
00258 
00259 inline VECArray::reference VECArray::operator[](const idx3 &id)
00260 {
00261   int i=id.i, j=id.j, k=id.k;     
00262 #ifdef BOUNDS_CHECK
00263   assert(0<=i && 0<=j &&0<=k);
00264   assert(i < row());
00265   assert(j < col());
00266   assert(k < depth());
00267 #endif
00268   return vv[ (i*col()+j)*depth() + k];
00269 }
00270 
00271 inline VECArray::const_reference VECArray::operator[](const idx3 &id) const
00272 {
00273   int i=id.i, j=id.j, k=id.k;     
00274 #ifdef BOUNDS_CHECK
00275   assert(0<=i && 0<=j &&0<=k);
00276   assert(i < row());
00277   assert(j < col());
00278   assert(k < depth());
00279 #endif
00280   return vv[ (i*col()+j)*depth() + k];
00281 }
00282 
00283 
00284 // assignments
00285 
00286 inline VECArray& VECArray::operator+=(const VECArray &A)
00287 {
00288   VEC::operator+=(A);
00289   return *this;
00290 }
00291 
00292 inline VECArray& VECArray::operator-=(const VECArray &A)
00293 {
00294   VEC::operator-=(A);
00295   return *this;
00296 }
00297 
00298 inline VECArray& VECArray::operator*=(const REAL& scalar)
00299 {
00300   VEC::operator*=(scalar);
00301   return *this;
00302 }
00303 
00304 inline VECArray& VECArray::operator/=(const REAL& scalar)
00305 {
00306   VEC::operator/=(scalar);
00307   return *this;
00308 }
00309 
00310 // protected members
00311 
00312 
00313 /****************************  I/O  ********************************/
00314 
00315 inline std::ostream& operator<<(std::ostream &s, const VECArray &A)
00316 {
00317     unsigned N=A.dim()/A.k_;
00318 
00319     for (unsigned i=0; i<N; i++)
00320         s   << A[i] << " ";
00321     return s;
00322 }
00323 
00324 
00325 /**************[ Basic Operations on VECArrays ]********************/
00326 
00327 inline VECArray operator+(const VECArray &A, const VECArray &B)
00328 {
00329   VECArray tmp(A);
00330   tmp += B;
00331   return tmp;
00332 }
00333 
00334 inline VECArray operator-(const VECArray &A, const VECArray &B)
00335 {
00336   VECArray tmp(A);
00337   tmp -= B;
00338   return tmp;
00339 }
00340 
00341 inline VECArray operator*(const VECArray &A, double s)
00342 {
00343   VECArray tmp(A);
00344   tmp *= s;
00345   return tmp;
00346 }
00347 
00348 inline VECArray operator*(double s, const VECArray &A)
00349 {
00350   VECArray tmp(A);
00351   tmp *= s;
00352   return tmp;
00353 }
00354 
00355 inline VECArray operator/(const VECArray &A, double s)
00356 {
00357   VECArray tmp(A);
00358   tmp /= s;
00359   return tmp;
00360 }
00361 
00362 // unary operators
00363 inline VECArray operator-(const VECArray &A)
00364 {
00365   VECArray tmp(A);
00366   tmp *= -1;
00367   return tmp;
00368 }
00369 
00370 inline VECArray& abs(VECArray &A)
00371 {
00372    for (int i=0; i<A.size(); i++)
00373       abs(A.vv[i]);
00374    return A;
00375 }
00376 
00377 
00378 
00379 
00380 }
00381 
00382 #endif
00383 // ARRAY_OF_VECS_H
00384 
00385 

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