TooN 2.1
internal/reference.hh
00001 // -*- c++ -*-
00002 
00003 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
00004 // Ed Rosten (er258@cam.ac.uk)
00005 //
00006 // This file is part of the TooN Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 namespace TooN {
00032 
00033 ////////////////////////////////////////////////////////////////////////////////
00034 //
00035 // Helper classes for matrices constructed as references to foreign data
00036 //
00037 
00038 struct Reference
00039 {
00040 
00041     template<int Size, typename Precision>
00042     struct VLayout
00043         : public Internal::GenericVBase<Size, Precision, 1, Internal::VectorSlice<Size, Precision> >
00044     {
00045 
00046         VLayout(Precision* p, int sz=0)
00047             : Internal::GenericVBase<Size, Precision, 1, Internal::VectorSlice<Size, Precision> >(p, sz, 0)
00048         {}
00049     };
00050 
00051 
00052     struct RowMajor
00053     {
00054         template<int Rows, int Cols, class Precision>
00055         struct MLayout
00056             : public Internal::GenericMBase<Rows, Cols, Precision, (Cols==-1?-2:Cols), 1, Internal::MatrixSlice<Rows, Cols, Precision> >
00057         {
00058 
00059             MLayout(Precision* p)
00060                 : Internal::GenericMBase<Rows,Cols,Precision, (Cols==-1?-2:Cols), 1, Internal::MatrixSlice<Rows, Cols, Precision> > (p)
00061             {}
00062             MLayout(Precision* p, int r, int c)
00063                 : Internal::GenericMBase<Rows,Cols,Precision, (Cols==-1?-2:Cols), 1, Internal::MatrixSlice<Rows, Cols, Precision> > (p, r, c, 0, 0)
00064             {}
00065         };
00066     };
00067 
00068     struct ColMajor
00069     {
00070         template<int Rows, int Cols, class Precision> struct MLayout: public Internal::GenericMBase<Rows, Cols, Precision, 1, (Rows==-1?-2:Rows), Internal::MatrixSlice<Rows, Cols, Precision> >
00071         {
00072             MLayout(Precision* p)
00073                 : Internal::GenericMBase<Rows, Cols, Precision, 1, (Rows==-1?-2:Rows), Internal::MatrixSlice<Rows, Cols, Precision> >(p)
00074             {}
00075             MLayout(Precision* p, int r, int c)
00076                 : Internal::GenericMBase<Rows, Cols, Precision, 1, (Rows==-1?-2:Rows), Internal::MatrixSlice<Rows, Cols, Precision> >(p, r, c, 0, 0)
00077             {}
00078         };
00079     };
00080 };
00081 
00082 
00083                                     inline       Vector<Dynamic, double,          Reference> wrapVector(double* data, int size)          { return Vector<Dynamic, double,          Reference>(data, size); }
00084                                     inline const Vector<Dynamic, const double,    Reference> wrapVector(const double* data, int size)    { return Vector<Dynamic, const double,    Reference>(data, size); }
00085 template<int Size>                  inline       Vector<Size,    double,          Reference> wrapVector(double* data)                    { return Vector<Size,    double,          Reference>(data); }
00086 template<int Size>                  inline const Vector<Size,    const double,    Reference> wrapVector(const double* data)              { return Vector<Size,    const double,    Reference>(data); }
00087 template<class Precision>           inline       Vector<Dynamic, Precision,       Reference> wrapVector(Precision* data, int size)       { return Vector<Dynamic, Precision,       Reference>(data, size); }
00088 template<class Precision>           inline const Vector<Dynamic, const Precision, Reference> wrapVector(const Precision* data, int size) { return Vector<Dynamic, const Precision, Reference>(data, size); }
00089 template<int Size, class Precision> inline       Vector<Size,    Precision,       Reference> wrapVector(Precision* data)                 { return Vector<Size,    Precision,       Reference>(data); }
00090 template<int Size, class Precision> inline const Vector<Size,    const Precision, Reference> wrapVector(const Precision* data)           { return Vector<Size,    const Precision, Reference>(data); }
00091 
00092 ///Wrap external data as a \link TooN::Matrix Matrix \endlink
00093 ///As usual, if template sizes are provided, then the run-time size is only
00094 ///used if the template size is not Dynamic.
00095 ///@ingroup gLinAlg
00096 ///@{
00097 //Fully static matrices, ie no size parameters
00098 template<int Rows, int Cols>                  inline       Matrix<Rows, Cols,       double,          Reference::RowMajor> wrapMatrix(double*    data)                           { return Matrix<Rows, Cols,       double,          Reference::RowMajor>(data);}
00099 template<int Rows, int Cols>                  inline const Matrix<Rows, Cols,       const double,    Reference::RowMajor> wrapMatrix(const double*    data)                     { return Matrix<Rows, Cols,       const double,    Reference::RowMajor>(data);}
00100 template<int Rows, int Cols, class Precision> inline       Matrix<Rows, Cols,       Precision,       Reference::RowMajor> wrapMatrix(Precision* data)                           { return Matrix<Rows, Cols,       Precision,       Reference::RowMajor>(data);}
00101 template<int Rows, int Cols, class Precision> inline const Matrix<Rows, Cols,       const Precision, Reference::RowMajor> wrapMatrix(const Precision* data)                     { return Matrix<Rows, Cols,       const Precision, Reference::RowMajor>(data);}
00102 //Static sizes with size parameters (useful for half-dynamic matrices)
00103 template<int Rows, int Cols>                  inline       Matrix<Rows, Cols,       double,          Reference::RowMajor> wrapMatrix(double*    data, int rows, int cols)       { return Matrix<Rows, Cols,       double,          Reference::RowMajor>(data, rows, cols);}
00104 template<int Rows, int Cols>                  inline const Matrix<Rows, Cols,       const double,    Reference::RowMajor> wrapMatrix(const double*    data, int rows, int cols) { return Matrix<Rows, Cols,       const double,    Reference::RowMajor>(data, rows, cols);}
00105 template<int Rows, int Cols, class Precision> inline       Matrix<Rows, Cols,       Precision,       Reference::RowMajor> wrapMatrix(Precision* data, int rows, int cols)       { return Matrix<Rows, Cols,       Precision,       Reference::RowMajor>(data, rows, cols);}
00106 template<int Rows, int Cols, class Precision> inline const Matrix<Rows, Cols,       const Precision, Reference::RowMajor> wrapMatrix(const Precision* data, int rows, int cols) { return Matrix<Rows, Cols,       const Precision, Reference::RowMajor>(data, rows, cols);}
00107 //Fully dynamic
00108                                               inline       Matrix<Dynamic, Dynamic, double,          Reference::RowMajor> wrapMatrix(double*          data, int rows, int cols) { return Matrix<Dynamic, Dynamic, double,          Reference::RowMajor>(data, rows, cols);}
00109                                               inline const Matrix<Dynamic, Dynamic, const double,    Reference::RowMajor> wrapMatrix(const double*    data, int rows, int cols) { return Matrix<Dynamic, Dynamic, const double,    Reference::RowMajor>(data, rows, cols);}
00110 template<class Precision>                     inline       Matrix<Dynamic, Dynamic, Precision,       Reference::RowMajor> wrapMatrix(Precision* data, int rows, int cols)       { return Matrix<Dynamic, Dynamic, Precision,       Reference::RowMajor>(data, rows, cols);}
00111 template<class Precision>                     inline const Matrix<Dynamic, Dynamic, const Precision, Reference::RowMajor> wrapMatrix(const Precision* data, int rows, int cols) { return Matrix<Dynamic, Dynamic, const Precision, Reference::RowMajor>(data, rows, cols);}
00112 ///@}
00113 }