25 #ifndef WMATRIXFIXED_H
26 #define WMATRIXFIXED_H
32 #include <boost/static_assert.hpp>
35 #include <boost/tokenizer.hpp>
46 #include <osg/Matrixd>
52 #include "../../WDefines.h"
53 #include "../../WStringUtils.h"
54 #include "../../WTypeTraits.h"
56 #include "../../exceptions/WOutOfBounds.h"
58 #include "../WValue.h"
63 #define ValueStoreTemplate template< typename, size_t, size_t > class
78 template<
typename ValueT,
size_t Rows,
size_t Cols >
107 const ValueT&
operator()(
size_t row,
size_t col )
const throw()
109 return m_values[ row * Cols + col ];
121 template <
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
124 for(
size_t row = 0; row < Rows; ++row )
126 for(
size_t col = 0; col < Cols; ++col )
152 template<
typename ValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate ValueStoreT = ValueStore >
159 template<
typename ValueTT,
size_t Rowss,
size_t Colss, ValueStoreTemplate ValueStoreTT >
223 for(
size_t row = 0; row < Rows; ++row )
225 for(
size_t col = 0; col < Cols; ++col )
240 BOOST_STATIC_ASSERT( Rows == 2 );
255 BOOST_STATIC_ASSERT( Rows == 3 );
272 BOOST_STATIC_ASSERT( Rows == 4 );
287 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
301 WAssert( val.
size() == Rows,
"The size of the given WValue doesn't equal the number of rows." );
303 for(
size_t i = 0; i < Rows; i++ )
316 MatrixType m =
zero();
317 for(
size_t i = 0; i < std::min( Rows, Cols ); ++i )
319 m( i, i ) = ValueT( 1 );
332 for(
size_t row = 0; row < Rows; ++row )
334 for(
size_t col = 0; col < Cols; ++col )
336 m( row, col ) = ValueT( 0 );
359 template<
typename RHSValueT,
size_t RHSRows,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
361 size_t colOffset = 0 )
383 template<
typename RHSValueT,
size_t RHSRows,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
386 size_t colOffset = 0 )
389 for(
size_t row = 0; row < Rows; ++row )
391 for(
size_t col = 0; col < Cols; ++col )
393 if( ( row >= rowOffset ) && ( col >= colOffset ) )
396 size_t srcRow = row - rowOffset;
397 size_t srcCol = col - colOffset;
400 if( ( srcRow < RHSRows ) && ( srcCol < RHSCols ) )
402 result( row, col ) = src( srcRow, srcCol );
406 result( row, col ) = m( row, col );
411 result( row, col ) = m( row, col );
428 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
431 for(
size_t col = 0; col < Cols; col++ )
433 at( index, col ) = vec( col, 0 );
447 for(
size_t col = 0; col < Cols; col++ )
449 result( col, 0 ) =
at( index, col );
465 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
468 for(
size_t row = 0; row < Rows; row++ )
470 at( row, index ) = vec( row, 0 );
484 for(
size_t row = 0; row < Rows; row++ )
486 result( row, 0 ) =
at( row, index );
501 operator Eigen::Matrix< ValueT, Rows, Cols >()
const
503 Eigen::Matrix< ValueT, Rows, Cols > m;
504 for(
size_t row = 0; row < Rows; ++row )
506 for(
size_t col = 0; col < Cols; ++col )
519 operator osg::Vec2d()
const
522 BOOST_STATIC_ASSERT( Rows == 2 );
523 return osg::Vec2d(
operator[]( 0 ),
operator[]( 1 ) );
531 operator osg::Vec2f()
const
534 BOOST_STATIC_ASSERT( Rows == 2 );
535 return osg::Vec2f(
operator[]( 0 ),
operator[]( 1 ) );
543 operator osg::Vec3d()
const
546 BOOST_STATIC_ASSERT( ( Rows == 3 ) || ( Rows == 4 ) );
547 return osg::Vec3d(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ) );
555 operator osg::Vec3f()
const
558 BOOST_STATIC_ASSERT( ( Rows == 3 ) || ( Rows == 4 ) );
559 return osg::Vec3f(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ) );
567 operator osg::Vec4d()
const
570 BOOST_STATIC_ASSERT( Rows == 4 );
571 return osg::Vec4d(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ),
operator[]( 3 ) );
579 operator osg::Vec4f()
const
582 BOOST_STATIC_ASSERT( Rows == 4 );
583 return osg::Vec4f(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ),
operator[]( 3 ) );
591 operator osg::Matrixd()
const
593 BOOST_STATIC_ASSERT( Rows == 4 );
594 BOOST_STATIC_ASSERT( Cols == 4 );
597 for(
size_t row = 0; row < 4; ++row )
599 for(
size_t col = 0; col < 4; ++col )
617 template<
typename TargetType >
618 TargetType
as()
const
620 return operator TargetType();
631 template <
typename ResultValueType, ValueStoreTemplate ResultValueStore >
646 for(
size_t row = 0; row < Rows; ++row )
648 for(
size_t col = 0; col < Cols; ++col )
662 BOOST_STATIC_ASSERT( Rows == 4 );
663 BOOST_STATIC_ASSERT( Cols == 4 );
665 for(
size_t row = 0; row < 4; ++row )
667 for(
size_t col = 0; col < 4; ++col )
681 BOOST_STATIC_ASSERT( Rows == 3 );
682 BOOST_STATIC_ASSERT( Cols == 1 );
696 BOOST_STATIC_ASSERT( Rows == 3 );
697 BOOST_STATIC_ASSERT( Cols == 1 );
711 BOOST_STATIC_ASSERT( Rows == 4 );
712 BOOST_STATIC_ASSERT( Cols == 1 );
727 BOOST_STATIC_ASSERT( Rows == 4 );
728 BOOST_STATIC_ASSERT( Cols == 1 );
748 template <
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
768 template<
typename RHSValueT,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
776 for( std::size_t row = 0; row < Rows; ++row )
778 for( std::size_t col = 0; col < RHSCols; ++col )
780 m( row, col ) = ResultValueType();
782 for( std::size_t i = 0; i < Cols; ++i )
784 m( row, col ) +=
operator()( row, i ) * rhs( i, col );
797 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
811 template<
typename RHSValueT >
812 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT >
815 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > m;
816 for(
size_t row = 0; row < Rows; ++row )
818 for(
size_t col = 0; col < Cols; ++col )
832 template<
typename RHSValueT >
846 template<
typename RHSValueT >
847 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT >
851 return operator*( ResultT( 1 ) / static_cast< ResultT >( rhs ) );
860 template<
typename RHSValueT >
874 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
875 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT >
878 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > m;
879 for(
size_t row = 0; row < Rows; ++row )
881 for(
size_t col = 0; col < Cols; ++col )
883 m( row, col ) =
operator()( row, col ) + rhs( row, col );
895 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
909 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
910 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT >
913 WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > m;
914 for(
size_t row = 0; row < Rows; ++row )
916 for(
size_t col = 0; col < Cols; ++col )
918 m( row, col ) =
operator()( row, col ) - rhs( row, col );
930 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
963 const ValueT&
operator()(
size_t row,
size_t col )
const throw()
978 BOOST_STATIC_ASSERT( Cols == 1 );
992 BOOST_STATIC_ASSERT( Cols == 1 );
1009 if( ( row >= Rows ) || ( col >= Cols ) )
1032 if( ( row >= Rows ) || ( col >= Cols ) )
1048 BOOST_STATIC_ASSERT( Rows >= 1 );
1049 BOOST_STATIC_ASSERT( Cols == 1 );
1058 const ValueT&
x()
const throw()
1060 BOOST_STATIC_ASSERT( Rows >= 1 );
1061 BOOST_STATIC_ASSERT( Cols == 1 );
1072 BOOST_STATIC_ASSERT( Rows >= 2 );
1073 BOOST_STATIC_ASSERT( Cols == 1 );
1082 const ValueT&
y()
const throw()
1084 BOOST_STATIC_ASSERT( Rows >= 2 );
1085 BOOST_STATIC_ASSERT( Cols == 1 );
1096 BOOST_STATIC_ASSERT( Rows >= 3 );
1097 BOOST_STATIC_ASSERT( Cols == 1 );
1106 const ValueT&
z()
const throw()
1108 BOOST_STATIC_ASSERT( Rows >= 3 );
1109 BOOST_STATIC_ASSERT( Cols == 1 );
1125 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1129 for(
size_t row = 0; eq && ( row < Rows ); ++row )
1131 for(
size_t col = 0; eq && ( col < Cols ); ++col )
1133 eq = eq && (
operator()( row, col ) == rhs( row, col ) );
1147 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1148 bool operator<( const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT >& rhs )
const throw()
1152 for(
size_t row = 0; eq && ( row < Rows ); ++row )
1154 for(
size_t col = 0; eq && ( col < Cols ); ++col )
1156 eq = eq && (
operator()( row, col ) == rhs( row, col ) );
1157 result = (
operator()( row, col ) < rhs( row, col ) );
1170 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1188 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1189 void setValues(
const RHSValueStoreT< RHSValueT, Rows, Cols >& values )
1191 for( std::size_t i = 0; i < Rows; ++i )
1193 for( std::size_t j = 0; j < Cols; ++j )
1195 m_values( i, j ) =
static_cast< ValueT
>( values( i, j ) );
1227 template <
typename ScalarT,
1228 typename RHSValueT,
size_t RHSRows,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
1253 template<
typename AValueT, ValueStoreTemplate AValueStoreT,
1254 typename BValueT, ValueStoreTemplate BValueStoreT,
1260 ResultType r = ResultType();
1261 for(
size_t i = 0; i < Rows; ++i )
1263 r += a( i, 0 ) * b( i, 0 );
1281 template<
typename AValueT, ValueStoreTemplate AValueStoreT,
1282 typename BValueT, ValueStoreTemplate BValueStoreT >
1286 typedef WMatrixFixed< typename WTypeTraits::TypePromotion< AValueT, BValueT >::Result, 3, 1 > ResultT;
1291 v[0] = a[1] * b[2] - a[2] * b[1];
1292 v[1] = a[2] * b[0] - a[0] * b[2];
1293 v[2] = a[0] * b[1] - a[1] * b[0];
1307 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows >
1310 ValueT r = ValueT();
1311 for(
size_t i = 0; i < Rows; ++i )
1313 r += a( i, 0 ) * a( i, 0 );
1328 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Cols >
1331 ValueT r = ValueT();
1332 for(
size_t i = 0; i < Cols; ++i )
1334 r += a( 0, i ) * a( 0, i );
1349 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows >
1352 return sqrt( length2( a ) );
1365 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Cols >
1368 return sqrt( length2( a ) );
1383 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows,
size_t Cols >
1386 BOOST_STATIC_ASSERT( Rows == 1 || Cols == 1 );
1387 ValueT r = ValueT();
1388 ValueT t = ValueT();
1389 for(
size_t row = 0; row < Rows; ++row )
1391 for(
size_t col = 0; col < Cols; ++col )
1393 t = a( row, col ) - b( row, col );
1412 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows,
size_t Cols >
1415 return sqrt( distance2( a, b ) );
1429 template<
typename RHSValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate RHSValueStoreT >
1433 return m *
static_cast< RHSValueT
>( 1.0 / length( m ) );
1448 template<
typename ValueT, std::
size_t Size,
template<
typename, std::
size_t, std::
size_t >
class ValueStoreT >
1469 template<
typename ValueT, std::
size_t Rows, std::
size_t Cols,
template<
typename, std::
size_t, std::
size_t >
class ValueStoreT >
1473 for(
size_t row = 0; row < mat.
getRows(); ++row )
1475 for(
size_t col = 0; col < mat.
getColumns(); ++col )
1477 res( col, row ) = mat( row, col );
1496 template<
typename ValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate ValueStoreT >
1497 std::ostream& operator<<( std::ostream& out, const WMatrixFixed< ValueT, Rows, Cols, ValueStoreT >& m )
1500 for(
size_t row = 0; row < m.getRows(); ++row )
1502 for(
size_t col = 0; col < m.getColumns(); ++col )
1504 out << m( row, col ) <<
";";
1523 template<
typename ValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate ValueStoreT >
1527 typedef boost::tokenizer< boost::char_separator< char > > Tokenizer;
1531 boost::char_separator< char > separators(
" ;" );
1532 Tokenizer t( s, separators );
1534 Tokenizer::iterator it = t.begin();
1535 for( std::size_t row = 0; row < Rows; ++row )
1537 for( std::size_t col = 0; col < Cols; ++col )
1543 m( row, col ) = string_utils::fromString< ValueT >( *it );
1551 #endif // WMATRIXFIXED_H
ValueStore< ValueT, Rows, Cols > & operator=(RHSValueStoreT< RHSValueT, Rows, Cols > const &rhs)
Replaces the values in this array.
ValueStoreT< ValueT, Rows, Cols > ValueStoreType
The storage container.
size_t getRows() const
The number of rows.
WMatrixFixed(const Eigen::Matrix< ValueT, Rows, Cols > &m)
Creates a WMatrix from a given Eigen3 Matrix.
TargetType as() const
A convenience function to cast the WMatrixFixed types to arbitrary other vector/matrix types that are...
size_t getColumns() const
The number of columns.
Base class for all higher level values like tensors, vectors, matrices and so on. ...
A data store with the specified dimensions and type.
ValueT & z()
Access z element of vector.
ValueT ValueType
The integral type used in this matrix.
bool operator==(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Compares two matrices and returns true if they are equal (component-wise).
WMatrixFixed(const osg::Vec4f &m)
Creates a WMatrix from a given OSG Vector.
void setValues(const RHSValueStoreT< RHSValueT, Rows, Cols > &values)
Sets the new values.
WMatrixFixed(const osg::Vec3d &m)
Creates a WMatrix from a given OSG Vector.
WMatrixFixed(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &m)
Copy construction casting the given value type.
static MatrixType zero()
Returns a zero-initialized matrix.
const ValueT & y() const
Access y element of vector.
size_t size() const
The number of entries.
WMatrixFixed< ValueT, Rows, Cols, ValueStoreT > MatrixType
The whole matrix as a type for lazy programmers.
Indicates invalid element access of a container.
const ValueT & at(size_t row, size_t col) const
Returns a const reference to the component of an row and column in order to provide access to the com...
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator*(const RHSValueT &rhs) const
Matrix-Scalar multiplication.
static MatrixType fromMatrix(const WMatrixFixed< RHSValueT, RHSRows, RHSCols, RHSValueStoreT > &src, size_t rowOffset=0, size_t colOffset=0)
Copy construction allowing the creation of a WMatrixFixed by another matrix of different size...
void operator/=(const RHSValueT &rhs)
Matrix-Scalar division with self-assignmnet.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator/(const RHSValueT &rhs) const
Matrix-Scalar division.
WMatrixFixed(const ValueT &x, const ValueT &y)
Constructor easing the initialization of vectors.
void operator*=(const RHSValueT &rhs)
Matrix-Scalar multiplication with self-assignment.
const ValueT & x() const
Access x element of vector.
A fixed size matrix class.
ValueT & operator()(size_t row, size_t col)
Returns a reference to the component of a row and column in order to provide access to the component...
WMatrixFixed()
Default constructor.
const ValueT & operator[](size_t row) const
Returns a reference to the component of the first column to provide access to the component...
const ValueT & operator()(size_t row, size_t col) const
Returns a const reference to the component of an row and column in order to provide access to the com...
void setColumnVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a column to a specific vector.
std::string toString(const T &value)
Convert a given value to a string.
ValueT m_values[Rows *Cols]
The value array.
ValueT & operator()(size_t row, size_t col)
Returns a reference to the component of an row and column in order to provide access to the component...
static MatrixType identity()
Returns an identity matrix.
const ValueT & operator()(size_t row, size_t col) const
Returns a reference to the component of an row and column in order to provide access to the component...
void operator+=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Matrix addition with self-assignment.
ValueT & at(size_t row, size_t col)
Returns a reference to the component of an row and column in order to provide access to the component...
WMatrixFixed(const WValue< ValueT > &val)
Casting constructor for WValue.
ValueT & operator[](size_t row)
Returns a reference to the component of the first column to provide access to the component...
size_t size() const
Get number of components the value consists of.
WMatrixFixed< ValueT, Cols, 1, ValueStoreT > getRowVector(size_t index) const
Get a vector containing a specific row.
WMatrixFixed(const ValueT &x, const ValueT &y, const ValueT &z, const ValueT &w)
Constructor easing the initialization of vectors.
void operator*=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Matrix-Matrix multiplication with self-assignment.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, RHSCols, ValueStoreT > operator*(const WMatrixFixed< RHSValueT, Cols, RHSCols, RHSValueStoreT > &rhs) const
Matrix-Matrix multiplication.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator-(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Matrix subtraction.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator+(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Matrix addition.
ValueT & y()
Access y element of vector.
WMatrixFixed(const ValueT &x, const ValueT &y, const ValueT &z)
Constructor easing the initialization of vectors.
bool operator!=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Compares two matrices and returns true if they are not equal.
void setRowVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a row to a specific vector.
WMatrixFixed(const osg::Vec3f &m)
Creates a WMatrix from a given OSG Vector.
MatrixType & operator=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Assigns the given argument matrix to this one.
ValueT & x()
Access x element of vector.
const ValueT & z() const
Access z element of vector.
ValueStoreType m_values
The value array.
static MatrixType fromMatrices(const MatrixType &m, const WMatrixFixed< RHSValueT, RHSRows, RHSCols, RHSValueStoreT > &src, size_t rowOffset=0, size_t colOffset=0)
Copy construction allowing the creation of a WMatrixFixed by another matrix of different size...
WMatrixFixed(const osg::Matrixd &m)
Creates a WMatrix from a given OSG 4x4 Matrix.
void operator-=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Matrix subtraction with self-assignment.
WMatrixFixed(const osg::Vec4d &m)
Creates a WMatrix from a given OSG Vector.
WMatrixFixed< ValueT, Rows, 1 > getColumnVector(size_t index) const
Get a vector containing a specific column.