Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

matrix.hpp

Go to the documentation of this file.
00001 /* --------------------------------------------------------------- *\
00002 
00003    file: matrix.hpp
00004    date: 030825 22:16:28
00005 
00006    author:
00007 
00008       mathias gumz <gumz at cs.uni-magdeburg.de>
00009 
00010    license:
00011 
00012       see LICENSE.txt
00013 
00014    about:
00015 
00016       generic matrix class
00017 
00018 \* --------------------------------------------------------------- */
00019 #ifndef _MATRIX_HPP_
00020 #define _MATRIX_HPP_
00021 
00022 namespace math
00023 {
00024 
00030 template<typename T, int W, int H> class Mat;
00031 
00032 
00033 /* --------------------------------------------------------------- *\
00034  *  traits for the matrix
00035 \* --------------------------------------------------------------- */
00036 
00037 template<typename T, int W, int H>
00038 struct Traits< Mat<T, W, H> >
00039 {
00040   Mat<T, W, H> static zero() { return Mat<T, W, H>(Mat<T, W, H>::ValTrait::zero()); }
00041 }; // struct Traits
00042 
00043 template<typename T, int W>
00044 struct Traits< Mat<T, W, W> >
00045 {
00046   Mat<T, W, W> static zero() { return Mat<T, W, W>(Mat<T, W, W>::ValTrait::zero()); }
00047   Mat<T, W, W> static one()  {
00048     Mat<T, W, W> o(Mat<T, W, W>::ValTrait::zero());
00049     for(int i= 0; i < W;i++)
00050       o(i,i)= Mat<T, W, W>::ValTrait::one();
00051     return o;
00052   }
00053 }; // struct Traits
00054 
00055 template<typename T>
00056 struct Traits< Mat<T, 3, 3> >
00057 {
00058   Mat<T, 3, 3> static zero() {
00059     static T z[]= { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00060     return Mat<T, 3, 3>(z);
00061   }
00062 
00063   Mat<T, 3, 3> static one() {
00064     static T o[]= { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
00065     return Mat<T, 3, 3>(o);
00066   }
00067 }; // Traits<T, 3, 3>
00068 
00069 
00070 
00071 /* --------------------------------------------------------------- *\
00072     functions for Mat
00073 \* --------------------------------------------------------------- */
00074 namespace matrix
00075 {
00077   template<typename T, int W, int H>
00078   inline Mat<T, W, H>& assign(Mat<T, W, H>& m, const T& scalar);
00079 
00081   template<typename T, int W, int H>
00082   inline Mat<T, W, H>& assign(Mat<T, W, H>& m, const T* valptr);
00083 
00085   template<typename T, int W, int H>
00086   inline Mat<T, W, H>& assign(Mat<T, W, H>& m, const Mat<T, W, H>& mat);
00087 
00088   template<typename T, int W, int H>
00089   inline Mat<T, W, H>& add(Mat<T, W, H>& m, const Mat<T, W, H>& mat );
00090 
00091   template<typename T, int W, int H>
00092   inline Mat<T, W, H>  add(const Mat<T, W, H>& m, const Mat<T, W, H>& mat );
00093 
00094   template<typename T, int W, int H>
00095   inline Mat<T, W, H>& multLeft(Mat<T, W, H>& m, const Mat<T, W, H>& mat );
00096 
00097   template<typename T, int W, int H>
00098   inline Mat<T, W, H>  multLeft(const Mat<T, W, H>& m, const Mat<T, W, H>& mat );
00099 
00100 }; // namespace matrix
00101 
00102 
00103 /* --------------------------------------------------------------- *\
00104     the matrix class
00105 \* --------------------------------------------------------------- */
00106 
00107 template<typename T, int W, int H>
00108 class Mat
00109 {
00110   public:
00111 
00112     typedef T                         ValType;
00113     typedef const T                   ConstValType;
00114     typedef T*                        ValTypePtr;
00115     typedef const T*                  ConstValTypePtr;
00116 
00117     typedef Mat<ValType, W, H>        MatType;
00118     typedef const Mat<ValType, W, H>  ConstMatType;
00119 
00120     typedef Traits<Mat<T, W, H> >     Trait;
00121     typedef Traits<T>                 ValTrait;
00122 
00123   public:
00124 /* --------------------------------------------------------------- *\
00125      constructor
00126 \* --------------------------------------------------------------- */
00127     Mat() { };
00128     Mat(const ValType&   scalar)    { operator=(scalar); }
00129     Mat(const ValTypePtr valptr)    { operator=(valptr); }
00130     Mat(const MatType&   mat)       { operator=(mat); }
00131 
00132     static unsigned int width()    { return W; };
00133     static unsigned int height()   { return H; };
00134     static unsigned int size()     { return W * H; };
00135     static unsigned int bytesize() { return sizeof(Mat<T, W, H>); }
00136 /* --------------------------------------------------------------- *\
00137      accessors
00138 \* --------------------------------------------------------------- */
00139     ValTypePtr        begin()       { return val[0].begin(); }
00140     ConstValTypePtr   begin() const { return val[0].begin(); }
00141     ValTypePtr        end()         { return val[W-1].end(); }
00142     ConstValTypePtr   end()   const { return val[W-1].end(); }
00143 
00144     ValType&       operator()(const int i, const int j)       { return val[i][j]; }
00145     const ValType& operator()(const int i, const int j) const { return val[i][j]; }
00146 
00147     ValType&       operator[](const int i)       { return (&val)[i]; }
00148     const ValType& operator[](const int i) const { return (&val)[i]; }
00149 
00150 
00151 /* --------------------------------------------------------------- *\
00152      operators
00153 \* --------------------------------------------------------------- */
00154 
00155     inline MatType& operator=(ConstValType&    scalar);
00156     inline MatType& operator=(ConstValTypePtr  valptr);
00157     inline MatType& operator=(ConstMatType&    mat);
00158 
00159     inline MatType  operator+(ConstMatType&    mat)          const;
00160     inline MatType  operator-(ConstMatType&    mat)          const;
00161     inline MatType& operator+=(ConstMatType&   mat);
00162     inline MatType& operator-=(ConstMatType&   mat);
00163 
00164     template<int WW>
00165     inline Mat<T, WW, H> operator*(const Mat<T, WW, W>& mat) const;
00166     inline Vec<T, W> operator*(const Vec<T, W>& vec)         const;
00167 
00168     inline MatType& operator*=(const Mat<T, H, W>& mat);
00169 
00170     inline MatType  operator*(ConstValType& scalar)          const;
00171     inline MatType  operator/(ConstValType& scalar)          const;
00172     inline MatType& operator*=(ConstValType& scalar);
00173     inline MatType& operator/=(ConstValType& scalar);
00174 
00175  public:
00176     Vec<T, H> val[W];
00177 
00178 }; // class Mat
00179 
00180 #include <ak/matrix.inl>
00181 
00182 }; // namespace math
00183 
00184 #endif // _MATRIX_HPP_
00185 /* --------------------------------------------------------------- *\
00186 \* --------------------------------------------------------------- */
00187 
00188 /* -- emacs - settings ------------------------------------------- *\
00189    ;;; Local Variables: ***
00190    ;;; mode:C++ ***
00191    ;;; c-basic-offset:2 ***
00192    ;;; End: ***
00193 \* --------------------------------------------------------------- */
00194 /* -- vim - settings --------------------------------------------- *\
00195   vim:set ft=cpp ts=2 ss=2:
00196 \* --------------------------------------------------------------- */
00197 

Generated on Tue Sep 2 11:33:27 2003 for math by doxygen 1.3.2