MBSim

Project Page
nurbs_defs.h
00001 #ifndef NURBS_DEFS_H_
00002 #define NURBS_DEFS_H_
00003 
00004 #include <fmatvec/fmatvec.h>
00005 #include <mbsim/mbsim_event.h>
00006 #include <iostream>
00007 
00008 namespace fmatvec {
00012   template <int N>
00013   class Point : public Vector<Fixed<N>, double> {
00014     public:
00015       Point<N>();
00016 
00017       Point<N>(Vector<Fixed<N>, double> pnt);
00018 
00019       virtual ~Point<N>();
00020 
00021     protected:
00022   };
00023 
00024   template <int N>
00025   Point<N>::Point() :
00026       Vector<Fixed<N>, double>() {
00027 
00028   }
00029 
00030   template <int N>
00031   Point<N>::Point(Vector<Fixed<N>, double> pnt) :
00032       Vector<Fixed<N>, double>(pnt) {
00033   }
00034 
00035   template <int N>
00036   Point<N>::~Point() {
00037   }
00038 
00042   template <int N>
00043   class HPoint : public Vector<Fixed<N + 1>, double> {
00044     public:
00045       HPoint<N>();
00046 
00047       HPoint<N>(Vector<Fixed<N + 1>, double> pnt);
00048 
00049       virtual ~HPoint<N>();
00050 
00051       Point<N> projectW();
00052 
00053       double w();
00054 
00055       double w() const;
00056 
00057     protected:
00058   };
00059 
00060   template <int N>
00061   HPoint<N>::HPoint() :
00062       Vector<Fixed<N + 1>, double>() {
00063   }
00064 
00065   template <int N>
00066   HPoint<N>::HPoint(Vector<Fixed<N + 1>, double> pnt) :
00067       Vector<Fixed<N + 1>, double>(pnt) {
00068   }
00069 
00070   template <int N>
00071   HPoint<N>::~HPoint() {
00072   }
00073 
00074   template <int N>
00075   Point<N> HPoint<N>::projectW() {
00076     Point<N> pnt;
00077     for (int i = 0; i < N; i++)
00078       pnt(i) = (*this)(i);
00079     return pnt;
00080   }
00081 
00082   template <int N>
00083   double HPoint<N>::w() {
00084     return (*this)(N);
00085   }
00086 
00087   template <int N>
00088   double HPoint<N>::w() const {
00089     return (*this)(N);
00090   }
00091 
00092   /*mathematical operations*/
00093   template <int N>
00094   Point<N> project(const HPoint<N> & hPnt) {
00095     Point<N> pnt;
00096     for (int i = 0; i < N; i++)
00097       pnt(i) = hPnt(i) / hPnt.w();
00098     return pnt;
00099   }
00100 //  /*!
00101 //   * \brief wrapper class for nurbs surface interpolating data point
00102 //   */
00103 
00104   template <typename T>
00105   class GeneralMatrix {
00106     public:
00107       GeneralMatrix();
00108       GeneralMatrix(int nrows_, int ncols_);
00109       GeneralMatrix(const GeneralMatrix<T>& m);
00110       ~GeneralMatrix();
00111 
00112       GeneralMatrix<T>& operator=(const GeneralMatrix<T>& m);
00113 
00114       T& operator()(int i, int j);
00115       T const& operator()(int i, int j) const;
00116       int rows() const;
00117       int cols() const;
00118       void resize(int nrows_, int ncols_);
00119       template <typename J>
00120       friend std::ostream& operator<<(std::ostream &os, const GeneralMatrix<J> &A);
00121     private:
00122       int nrows, ncols;
00123       T* data;
00124   };
00125 
00126   template <typename T>
00127   inline GeneralMatrix<T>::GeneralMatrix() :
00128       nrows(0), ncols(0), data(NULL) {
00129   }
00130 
00131   template <typename T>
00132   inline GeneralMatrix<T>::GeneralMatrix(int nrows_, int ncols_) :
00133       nrows(nrows_), ncols(ncols_) {
00134     if (nrows_ == 0 || ncols_ == 0)
00135       throw MBSim::MBSimError("(GeneralMatrix:: The number of rows or columns of the matrix is zero!)");
00136     data = new T[nrows_ * ncols_];
00137   }
00138   template <typename T>
00139   inline GeneralMatrix<T>::GeneralMatrix(const GeneralMatrix<T>& m) :
00140       nrows(m.nrows), ncols(m.ncols) {
00141     data = new T[nrows * ncols];
00142     std::copy(m.data, m.data + nrows * ncols, data);
00143   }
00144 
00145   template <typename T>
00146   inline GeneralMatrix<T>::~GeneralMatrix() {
00147     delete[] data;
00148   }
00149 
00150   template <typename T>
00151   inline GeneralMatrix<T>& GeneralMatrix<T>::operator=(const GeneralMatrix<T>& m) {
00152     nrows = m.nrows;
00153     ncols = m.ncols;
00154     data = m.data;
00155     return *this;
00156   }
00157 
00158   template <typename T>
00159   inline T& GeneralMatrix<T>::operator()(int row, int col) {
00160     if (row >= nrows || col >= ncols)
00161       throw MBSim::MBSimError("(GeneralMatrix::operator(): trying to access data out of range of the GeneralMatrix!)");
00162     return data[row * ncols + col];
00163   }
00164 
00165   template <typename T>
00166   inline T const& GeneralMatrix<T>::operator()(int row, int col) const {
00167     if (row >= nrows || col >= ncols)
00168       throw MBSim::MBSimError("(GeneralMatrix::operator(): trying to access data out of range of the GeneralMatrix!)");
00169     return data[row * ncols + col];
00170   }
00171 
00172   template <typename T>
00173   inline int GeneralMatrix<T>::rows() const {
00174     return nrows;
00175   }
00176 
00177   template <typename T>
00178   inline int GeneralMatrix<T>::cols() const {
00179     return ncols;
00180   }
00181 
00182   template <typename T>
00183   void GeneralMatrix<T>::resize(int nrows_, int ncols_) {
00184 
00185     if (nrows_ <= 0 || ncols_ <= 0) {
00186       throw MBSim::MBSimError("(GeneralMatrix::resize(): non-positive resize number of rows or columns!)");
00187       return;
00188     }
00189 
00190     if (nrows_ == nrows && ncols_ == ncols)
00191       return;
00192 
00193     T* newData = new T[nrows_ * ncols_];
00194 
00195     delete[] data;
00196     data = newData;
00197     nrows = nrows_;
00198     ncols = ncols_;
00199   }
00200 
00201   template <typename T>
00202   std::ostream& operator<<(std::ostream &os, const GeneralMatrix<T> &A) {
00203     os << A.rows() << " x " << A.cols() << std::endl;
00204     os << " = " << std::endl;
00205     os << "[ ";
00206     for (int i = 0; i < A.rows(); ++i) {
00207       for (int j = 0; j < A.cols(); ++j) {
00208         os << trans(A.data[i * A.ncols + j]);
00209       }
00210       if (i != A.rows() - 1)
00211         os << std::endl << "  ";
00212     }
00213     os << " ];";
00214     return os;
00215   }
00216 
00217 }
00218 
00219 #endif