examples/calibrate3d/TooN/mbase.hh

00001 
00002 /*                       
00003          Copyright (C) 2005 Tom Drummond
00004 
00005      This library is free software; you can redistribute it and/or
00006      modify it under the terms of the GNU Lesser General Public
00007      License as published by the Free Software Foundation; either
00008      version 2.1 of the License, or (at your option) any later version.
00009 
00010      This library is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013      Lesser General Public License for more details.
00014 
00015      You should have received a copy of the GNU Lesser General Public
00016      License along with this library; if not, write to the Free Software
00017      Foundation, Inc.
00018      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019 */
00020 #ifndef __MBASE_HH
00021 #define __MBASE_HH
00022 #include <assert.h>
00023 
00024 
00025 template <class Accessor>
00026 class MatrixBase : public Accessor {
00027  public:
00028   const double* get_data_ptr()const{return this->my_values;}
00029   double* get_data_ptr(){return this->my_values;}
00030 
00031   DynamicMatrix<Accessor>& down_cast() {return reinterpret_cast<DynamicMatrix<Accessor>&> (*this);}
00032   const DynamicMatrix<Accessor>& down_cast() const {return reinterpret_cast<const DynamicMatrix<Accessor>&> (*this);}
00033 };
00034 
00035 // operator ostream& <<
00036 template <class Accessor>
00037 std::ostream& operator << (std::ostream& os, const MatrixBase<Accessor>& m){
00038   for(int r=0; r<m.num_rows(); r++){
00039     os << m[r] << std::endl;
00040   }
00041   return os;
00042 }
00043 
00044 // operator istream& >>
00045 template <class Accessor>
00046 std::istream& operator >> (std::istream& is, MatrixBase<Accessor>& m){
00047   for(int r=0; r<m.num_rows(); r++){
00048     is >> m[r];
00049   }
00050   return is;
00051 }
00052 
00053 // Data copying
00054 
00055 template <class Accessor1, class Accessor2>
00056 struct MatrixCopy {
00057   inline static void eval(MatrixBase<Accessor1>& to, const MatrixBase<Accessor2>& from){
00058     for(int r=0; r<from.num_rows(); r++){
00059       for(int c=0; c<from.num_cols(); c++){
00060         to(r,c)=from(r,c);
00061       }
00062     }
00063   }
00064 };
00065 
00066 template<int Rows, int Cols, class Layout, class Zone1, class Zone2>
00067 struct MatrixCopy<FixedMAccessor<Rows,Cols,Layout,Zone1>,FixedMAccessor<Rows,Cols,Layout,Zone2> > {
00068   inline static void eval(MatrixBase<FixedMAccessor<Rows,Cols,Layout,Zone1> >& to,
00069                           const MatrixBase<FixedMAccessor<Rows,Cols,Layout,Zone2> >& from) {
00070     memcpy(to.get_data_ptr(), from.get_data_ptr(), Rows*Cols*sizeof(double));
00071   }
00072 };
00073 
00074 template<class Layout>
00075 struct MatrixCopy<DynamicMAccessor<Layout>,DynamicMAccessor<Layout> > {
00076   inline static void eval(MatrixBase<DynamicMAccessor<Layout> >& to,
00077                           const MatrixBase<DynamicMAccessor<Layout> >& from){
00078     memcpy(to.get_data_ptr(), from.get_data_ptr(), from.num_rows()*from.num_cols()*sizeof(double));
00079   }
00080 };
00081 
00082 
00084 //                                                    //
00085 // Fixed and Dynamic Matrix classes                   //
00086 // all the arithmetic and assignment                  //
00087 // operations are applied to these                    //
00088 //                                                    //
00090 
00091 template<int Rows, int Cols, class Accessor>
00092 struct FixedMatrix : public MatrixBase<Accessor> {
00093   // assignment from correct sized FixedMatrix
00094   template<class Accessor2>
00095   inline FixedMatrix& operator=(const FixedMatrix<Rows,Cols,Accessor2>& from){
00096     MatrixCopy<Accessor, Accessor2>::eval(*this,from);
00097     return *this;
00098   }
00099 
00100   // copy assignment
00101   inline FixedMatrix& operator=(const FixedMatrix& from){
00102     MatrixCopy<Accessor,Accessor>::eval(*this,from);
00103     return *this;
00104   }
00105   
00106   // assignment from any DynamicMatrix
00107   template<class Accessor2>
00108     inline FixedMatrix& operator=(const DynamicMatrix<Accessor2>& from){
00109     assert(from.num_rows() == Rows && from.num_cols() == Cols);
00110     MatrixCopy<Accessor,Accessor2>::eval(*this,from);
00111     return *this;
00112   }
00113     static void dummy() {}
00114 };
00115 
00116 template<class Accessor>
00117 struct DynamicMatrix : public MatrixBase<Accessor> {
00118   // assignment from any MatrixBase
00119   template<class Accessor2>
00120   DynamicMatrix& operator=(const MatrixBase<Accessor2>& from){
00121     assert(from.num_rows() == this->num_rows() && from.num_cols() == this->num_cols());
00122     MatrixCopy<Accessor,Accessor2>::eval(*this,from);
00123     return *this;
00124   }
00125 
00126 
00127   // repeated for explicit copy assignment
00128   DynamicMatrix& operator=(const DynamicMatrix& from){
00129     assert(from.num_rows() == this->num_rows() && from.num_cols() == this->num_cols());
00130     MatrixCopy<Accessor,Accessor>::eval(*this,from);
00131     return *this;
00132   }
00133 
00134     operator DynamicMatrix& () { return *this; }
00135     operator const DynamicMatrix& () const { return *this; }
00136 
00137 };
00138 
00139 
00140 #endif

Generated on Fri Feb 22 18:26:54 2008 for QVision by  doxygen 1.5.3