TuttleOFX  1
Matrix.hpp
Go to the documentation of this file.
00001 #ifndef _TERRY_MATH_MATRIX_HPP_
00002 #define _TERRY_MATH_MATRIX_HPP_
00003 
00004 #ifndef BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
00005         #define BOOST_UBLAS_SHALLOW_ARRAY_ADAPTOR
00006 #endif
00007 
00008 #include <boost/numeric/ublas/storage.hpp>
00009 #include <boost/numeric/ublas/matrix.hpp>
00010 #include <boost/numeric/ublas/vector.hpp>
00011 
00012 #include <memory>
00013 
00014 
00015 namespace terry {
00016 namespace math {
00017 
00018 typedef boost::numeric::ublas::row_major DefaultStorageOrder;
00019 
00020 
00021 /*
00022 // C++-1x solution
00023 template<typename BaseType, class Alloc = std::allocator<BaseType> >
00024 typedef boost::numeric::ublas::matrix<BaseType, StorageOrder, boost::numeric::ublas::unbounded_array<BaseType, Alloc > > Matrix;
00025 
00026 template<typename T>
00027 void f( Matrix<T> & m );
00028 
00029 Matrixd m;
00030 f(m);
00031 */
00032 
00033 template<typename T, typename SO = DefaultStorageOrder, class Alloc = std::allocator<T> >
00034 struct Matrix
00035 {
00036         typedef T BaseType;
00037         typedef boost::numeric::ublas::unbounded_array<BaseType, Alloc > StorageType;
00038         typedef boost::numeric::ublas::matrix<BaseType, SO, StorageType> Type;
00039 
00040         typedef boost::numeric::ublas::shallow_array_adaptor<BaseType>        ShallowStorageType;
00041         typedef boost::numeric::ublas::matrix<BaseType, SO, ShallowStorageType>   ShallowType;
00042 
00043 private:
00044         Matrix(){}
00045 };
00046 
00047 template<class T, std::size_t M, std::size_t N, typename SO = DefaultStorageOrder>
00048 struct BoundedMatrix
00049 {
00050         typedef T BaseType;
00051         typedef boost::numeric::ublas::bounded_matrix<BaseType, M, N, SO> Type;
00052 
00053 private:
00054         BoundedMatrix(){}
00055 };
00056 
00057 template<typename T, class Alloc = std::allocator<T> >
00058 struct Vector
00059 {
00060         typedef T BaseType;
00061         typedef boost::numeric::ublas::unbounded_array<BaseType, Alloc > StorageType;
00062         typedef boost::numeric::ublas::vector<BaseType, StorageType> Type;
00063 
00064         typedef boost::numeric::ublas::shallow_array_adaptor<BaseType>        ShallowStorageType;
00065         typedef boost::numeric::ublas::vector<BaseType, ShallowStorageType>   ShallowType;
00066 
00067 private:
00068         Vector(){}
00069 };
00070 
00071 template<typename T, std::size_t S>
00072 struct BoundedVector
00073 {
00074         typedef T BaseType;
00075         typedef boost::numeric::ublas::bounded_vector<BaseType, S> Type;
00076         
00077 private:
00078         BoundedVector(){}
00079 };
00080 
00081 
00082 
00083 
00084 #define TERRY_DEFINE_MATRIX_TYPES( TYPE, ORDER, ORDERSTR, POST ) \
00085         \
00086         typedef Matrix<TYPE,ORDER>::Type Matrix##ORDERSTR##POST; \
00087         typedef Matrix<TYPE,ORDER>::ShallowType MatrixView##ORDERSTR##POST; \
00088         typedef Matrix<TYPE,ORDER>::ShallowStorageType MatrixViewInit##ORDERSTR##POST; \
00089         \
00090         typedef BoundedMatrix<TYPE, 2, 2, ORDER>::Type BoundedMatrix2x2##ORDERSTR##POST; \
00091         typedef BoundedMatrix<TYPE, 2, 3, ORDER>::Type BoundedMatrix2x3##ORDERSTR##POST; \
00092         typedef BoundedMatrix<TYPE, 2, 4, ORDER>::Type BoundedMatrix2x4##ORDERSTR##POST; \
00093         \
00094         typedef BoundedMatrix<TYPE, 3, 2, ORDER>::Type BoundedMatrix3x2##ORDERSTR##POST; \
00095         typedef BoundedMatrix<TYPE, 3, 3, ORDER>::Type BoundedMatrix3x3##ORDERSTR##POST; \
00096         typedef BoundedMatrix<TYPE, 3, 4, ORDER>::Type BoundedMatrix3x4##ORDERSTR##POST; \
00097         \
00098         typedef BoundedMatrix<TYPE, 4, 2, ORDER>::Type BoundedMatrix4x2##ORDERSTR##POST; \
00099         typedef BoundedMatrix<TYPE, 4, 3, ORDER>::Type BoundedMatrix4x3##ORDERSTR##POST; \
00100         typedef BoundedMatrix<TYPE, 4, 4, ORDER>::Type BoundedMatrix4x4##ORDERSTR##POST; \
00101 //
00102 
00103 #define TERRY_DEFINE_TYPES( TYPE, POST ) \
00104         \
00105         TERRY_DEFINE_MATRIX_TYPES( TYPE, boost::numeric::ublas::row_major, , POST ) \
00106         TERRY_DEFINE_MATRIX_TYPES( TYPE, boost::numeric::ublas::column_major, C, POST ) \
00107         TERRY_DEFINE_MATRIX_TYPES( TYPE, boost::numeric::ublas::row_major, R, POST ) \
00108         \
00109         typedef Vector<TYPE>::Type Vector##POST; \
00110         typedef Vector<TYPE>::ShallowType VectorView##POST; \
00111         \
00112         typedef boost::numeric::ublas::shallow_array_adaptor<TYPE> ArrayViewInit##POST; \
00113         \
00114         typedef BoundedVector<TYPE, 2>::Type BoundedVector2##POST; \
00115         typedef BoundedVector<TYPE, 3>::Type BoundedVector3##POST; \
00116         typedef BoundedVector<TYPE, 4>::Type BoundedVector4##POST; \
00117 //
00118 
00119 
00120 TERRY_DEFINE_TYPES( double, d )
00121 TERRY_DEFINE_TYPES( float, f )
00122 TERRY_DEFINE_TYPES( int, i )
00123 TERRY_DEFINE_TYPES( char, c )
00124 
00125 
00126 }
00127 }
00128 
00129 #endif
00130