25 #ifndef WMATRIXFIXED_H
26 #define WMATRIXFIXED_H
31 #include <boost/static_assert.hpp>
32 #include <boost/tokenizer.hpp>
42 #include <osg/Matrixd>
48 #include "../../WDefines.h"
49 #include "../../WStringUtils.h"
50 #include "../../WTypeTraits.h"
52 #include "../../exceptions/WOutOfBounds.h"
54 #include "../WValue.h"
59 #define ValueStoreTemplate template< typename, size_t, size_t > class
74 template<
typename ValueT,
size_t Rows,
size_t Cols >
103 const ValueT&
operator()(
size_t row,
size_t col )
const throw()
105 return m_values[ row * Cols + col ];
117 template <
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
120 for(
size_t row = 0; row < Rows; ++row )
122 for(
size_t col = 0; col < Cols; ++col )
148 template<
typename ValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate ValueStoreT = ValueStore >
155 template<
typename ValueTT,
size_t Rowss,
size_t Colss, ValueStoreTemplate ValueStoreTT >
219 for(
size_t row = 0; row < Rows; ++row )
221 for(
size_t col = 0; col < Cols; ++col )
236 BOOST_STATIC_ASSERT( Rows == 2 );
251 BOOST_STATIC_ASSERT( Rows == 3 );
268 BOOST_STATIC_ASSERT( Rows == 4 );
283 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
297 WAssert( val.
size() == Rows,
"The size of the given WValue doesn't equal the number of rows." );
299 for(
size_t i = 0; i < Rows; i++ )
313 for(
size_t i = 0; i < std::min( Rows, Cols ); ++i )
315 m( i, i ) = ValueT( 1 );
328 for(
size_t row = 0; row < Rows; ++row )
330 for(
size_t col = 0; col < Cols; ++col )
332 m( row, col ) = ValueT( 0 );
355 template<
typename RHSValueT,
size_t RHSRows,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
357 size_t colOffset = 0 )
379 template<
typename RHSValueT,
size_t RHSRows,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
382 size_t colOffset = 0 )
385 for(
size_t row = 0; row < Rows; ++row )
387 for(
size_t col = 0; col < Cols; ++col )
389 if( ( row >= rowOffset ) && ( col >= colOffset ) )
392 size_t srcRow = row - rowOffset;
393 size_t srcCol = col - colOffset;
396 if( ( srcRow < RHSRows ) && ( srcCol < RHSCols ) )
398 result( row, col ) = src( srcRow, srcCol );
402 result( row, col ) = m( row, col );
407 result( row, col ) = m( row, col );
424 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
427 for(
size_t col = 0; col < Cols; col++ )
429 at( index, col ) = vec( col, 0 );
443 for(
size_t col = 0; col < Cols; col++ )
445 result( col, 0 ) =
at( index, col );
461 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
464 for(
size_t row = 0; row < Rows; row++ )
466 at( row, index ) = vec( row, 0 );
480 for(
size_t row = 0; row < Rows; row++ )
482 result( row, 0 ) =
at( row, index );
497 operator Eigen::Matrix< ValueT, Rows, Cols >()
const
499 Eigen::Matrix< ValueT, Rows, Cols > m;
500 for(
size_t row = 0; row < Rows; ++row )
502 for(
size_t col = 0; col < Cols; ++col )
515 operator osg::Vec2d()
const
518 BOOST_STATIC_ASSERT( Rows == 2 );
519 return osg::Vec2d(
operator[]( 0 ),
operator[]( 1 ) );
527 operator osg::Vec2f()
const
530 BOOST_STATIC_ASSERT( Rows == 2 );
531 return osg::Vec2f(
operator[]( 0 ),
operator[]( 1 ) );
539 operator osg::Vec3d()
const
542 BOOST_STATIC_ASSERT( ( Rows == 3 ) || ( Rows == 4 ) );
543 return osg::Vec3d(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ) );
551 operator osg::Vec3f()
const
554 BOOST_STATIC_ASSERT( ( Rows == 3 ) || ( Rows == 4 ) );
555 return osg::Vec3f(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ) );
563 operator osg::Vec4d()
const
566 BOOST_STATIC_ASSERT( Rows == 4 );
567 return osg::Vec4d(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ),
operator[]( 3 ) );
575 operator osg::Vec4f()
const
578 BOOST_STATIC_ASSERT( Rows == 4 );
579 return osg::Vec4f(
operator[]( 0 ),
operator[]( 1 ),
operator[]( 2 ),
operator[]( 3 ) );
587 operator osg::Matrixd()
const
589 BOOST_STATIC_ASSERT( Rows == 4 );
590 BOOST_STATIC_ASSERT( Cols == 4 );
593 for(
size_t row = 0; row < 4; ++row )
595 for(
size_t col = 0; col < 4; ++col )
613 template<
typename TargetType >
614 TargetType
as()
const
616 return operator TargetType();
627 template <
typename ResultValueType, ValueStoreTemplate ResultValueStore >
642 for(
size_t row = 0; row < Rows; ++row )
644 for(
size_t col = 0; col < Cols; ++col )
658 BOOST_STATIC_ASSERT( Rows == 4 );
659 BOOST_STATIC_ASSERT( Cols == 4 );
661 for(
size_t row = 0; row < 4; ++row )
663 for(
size_t col = 0; col < 4; ++col )
677 BOOST_STATIC_ASSERT( Rows == 3 );
678 BOOST_STATIC_ASSERT( Cols == 1 );
692 BOOST_STATIC_ASSERT( Rows == 3 );
693 BOOST_STATIC_ASSERT( Cols == 1 );
707 BOOST_STATIC_ASSERT( Rows == 4 );
708 BOOST_STATIC_ASSERT( Cols == 1 );
723 BOOST_STATIC_ASSERT( Rows == 4 );
724 BOOST_STATIC_ASSERT( Cols == 1 );
744 template <
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
764 template<
typename RHSValueT,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
772 for( std::size_t row = 0; row < Rows; ++row )
774 for( std::size_t col = 0; col < RHSCols; ++col )
776 m( row, col ) = ResultValueType();
778 for( std::size_t i = 0; i < Cols; ++i )
780 m( row, col ) +=
operator()( row, i ) * rhs( i, col );
793 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
807 template<
typename RHSValueT >
812 for(
size_t row = 0; row < Rows; ++row )
814 for(
size_t col = 0; col < Cols; ++col )
828 template<
typename RHSValueT >
842 template<
typename RHSValueT >
847 return operator*( ResultT( 1 ) /
static_cast< ResultT
>( rhs ) );
856 template<
typename RHSValueT >
870 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
875 for(
size_t row = 0; row < Rows; ++row )
877 for(
size_t col = 0; col < Cols; ++col )
879 m( row, col ) =
operator()( row, col ) + rhs( row, col );
891 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
905 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
910 for(
size_t row = 0; row < Rows; ++row )
912 for(
size_t col = 0; col < Cols; ++col )
914 m( row, col ) =
operator()( row, col ) - rhs( row, col );
926 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
959 const ValueT&
operator()(
size_t row,
size_t col )
const throw()
974 BOOST_STATIC_ASSERT( Cols == 1 );
988 BOOST_STATIC_ASSERT( Cols == 1 );
1003 ValueT&
at(
size_t row,
size_t col )
1005 if( ( row >= Rows ) || ( col >= Cols ) )
1026 const ValueT&
at(
size_t row,
size_t col )
const
1028 if( ( row >= Rows ) || ( col >= Cols ) )
1044 BOOST_STATIC_ASSERT( Rows >= 1 );
1045 BOOST_STATIC_ASSERT( Cols == 1 );
1054 const ValueT&
x()
const throw()
1056 BOOST_STATIC_ASSERT( Rows >= 1 );
1057 BOOST_STATIC_ASSERT( Cols == 1 );
1068 BOOST_STATIC_ASSERT( Rows >= 2 );
1069 BOOST_STATIC_ASSERT( Cols == 1 );
1078 const ValueT&
y()
const throw()
1080 BOOST_STATIC_ASSERT( Rows >= 2 );
1081 BOOST_STATIC_ASSERT( Cols == 1 );
1092 BOOST_STATIC_ASSERT( Rows >= 3 );
1093 BOOST_STATIC_ASSERT( Cols == 1 );
1102 const ValueT&
z()
const throw()
1104 BOOST_STATIC_ASSERT( Rows >= 3 );
1105 BOOST_STATIC_ASSERT( Cols == 1 );
1121 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1125 for(
size_t row = 0; eq && ( row < Rows ); ++row )
1127 for(
size_t col = 0; eq && ( col < Cols ); ++col )
1129 eq = eq && (
operator()( row, col ) == rhs( row, col ) );
1143 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1148 for(
size_t row = 0; eq && ( row < Rows ); ++row )
1150 for(
size_t col = 0; eq && ( col < Cols ); ++col )
1152 eq = eq && (
operator()( row, col ) == rhs( row, col ) );
1153 result = (
operator()( row, col ) < rhs( row, col ) );
1166 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1184 template<
typename RHSValueT, ValueStoreTemplate RHSValueStoreT >
1185 void setValues(
const RHSValueStoreT< RHSValueT, Rows, Cols >& values )
1187 for( std::size_t i = 0; i < Rows; ++i )
1189 for( std::size_t j = 0; j < Cols; ++j )
1191 m_values( i, j ) =
static_cast< ValueT
>( values( i, j ) );
1223 template <
typename ScalarT,
1224 typename RHSValueT,
size_t RHSRows,
size_t RHSCols, ValueStoreTemplate RHSValueStoreT >
1249 template<
typename AValueT, ValueStoreTemplate AValueStoreT,
1250 typename BValueT, ValueStoreTemplate BValueStoreT,
1256 ResultType r = ResultType();
1257 for(
size_t i = 0; i < Rows; ++i )
1259 r += a( i, 0 ) * b( i, 0 );
1277 template<
typename AValueT, ValueStoreTemplate AValueStoreT,
1278 typename BValueT, ValueStoreTemplate BValueStoreT >
1287 v[0] = a[1] * b[2] - a[2] * b[1];
1288 v[1] = a[2] * b[0] - a[0] * b[2];
1289 v[2] = a[0] * b[1] - a[1] * b[0];
1303 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows >
1306 ValueT r = ValueT();
1307 for(
size_t i = 0; i < Rows; ++i )
1309 r += a( i, 0 ) * a( i, 0 );
1324 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Cols >
1327 ValueT r = ValueT();
1328 for(
size_t i = 0; i < Cols; ++i )
1330 r += a( 0, i ) * a( 0, i );
1345 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows >
1348 return sqrt( length2( a ) );
1361 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Cols >
1364 return sqrt( length2( a ) );
1379 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows,
size_t Cols >
1382 BOOST_STATIC_ASSERT( Rows == 1 || Cols == 1 );
1383 ValueT r = ValueT();
1384 ValueT t = ValueT();
1385 for(
size_t row = 0; row < Rows; ++row )
1387 for(
size_t col = 0; col < Cols; ++col )
1389 t = a( row, col ) - b( row, col );
1408 template<
typename ValueT, ValueStoreTemplate ValueStoreT,
size_t Rows,
size_t Cols >
1411 return sqrt( distance2( a, b ) );
1425 template<
typename RHSValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate RHSValueStoreT >
1429 return m *
static_cast< RHSValueT
>( 1.0 / length( m ) );
1444 template<
typename ValueT, std::
size_t Size,
template<
typename, std::
size_t, std::
size_t >
class ValueStoreT >
1465 template<
typename ValueT, std::
size_t Rows, std::
size_t Cols,
template<
typename, std::
size_t, std::
size_t >
class ValueStoreT >
1469 for(
size_t row = 0; row < mat.
getRows(); ++row )
1471 for(
size_t col = 0; col < mat.
getColumns(); ++col )
1473 res( col, row ) = mat( row, col );
1492 template<
typename ValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate ValueStoreT >
1496 for(
size_t row = 0; row < m.
getRows(); ++row )
1498 for(
size_t col = 0; col < m.
getColumns(); ++col )
1500 out << m( row, col ) <<
";";
1519 template<
typename ValueT,
size_t Rows,
size_t Cols, ValueStoreTemplate ValueStoreT >
1523 typedef boost::tokenizer< boost::char_separator< char > > Tokenizer;
1527 boost::char_separator< char > separators(
" ;" );
1528 Tokenizer t( s, separators );
1530 Tokenizer::iterator it = t.begin();
1531 for( std::size_t row = 0; row < Rows; ++row )
1533 for( std::size_t col = 0; col < Cols; ++col )
1539 m( row, col ) = string_utils::fromString< ValueT >( *it );
A data store with the specified dimensions and type.
ValueStore< ValueT, Rows, Cols > & operator=(RHSValueStoreT< RHSValueT, Rows, Cols > const &rhs)
Replaces the values in this array.
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.
ValueT m_values[Rows *Cols]
The value array.
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...
A fixed size matrix class.
WMatrixFixed< ValueT, Rows, Cols, ValueStoreT > MatrixType
The whole matrix as a type for lazy programmers.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator/(const RHSValueT &rhs) const
Matrix-Scalar division.
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.
const ValueT & y() const
Access y element of vector.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator-(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Matrix subtraction.
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.
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(const osg::Vec4f &m)
Creates a WMatrix from a given OSG Vector.
static MatrixType identity()
Returns an identity matrix.
MatrixType & operator=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Assigns the given argument matrix to this one.
WMatrixFixed(const Eigen::Matrix< ValueT, Rows, Cols > &m)
Creates a WMatrix from a given Eigen3 Matrix.
ValueT & z()
Access z element of vector.
void operator*=(const RHSValueT &rhs)
Matrix-Scalar multiplication with self-assignment.
WMatrixFixed(const osg::Vec3d &m)
Creates a WMatrix from a given OSG Vector.
WMatrixFixed(const osg::Vec3f &m)
Creates a WMatrix from a given OSG Vector.
ValueT ValueType
The integral type used in this matrix.
size_t getColumns() const
The number of columns.
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.
bool operator<(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Compares two matrices and returns true if this is smaller than the specified one (component-wise).
ValueStoreType m_values
The value array.
void setColumnVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a column to a specific vector.
void setValues(const RHSValueStoreT< RHSValueT, Rows, Cols > &values)
Sets the new values.
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.
const ValueT & z() const
Access z element of vector.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator*(const RHSValueT &rhs) const
Matrix-Scalar multiplication.
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...
TargetType as() const
A convenience function to cast the WMatrixFixed types to arbitrary other vector/matrix types that are...
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...
static MatrixType zero()
Returns a zero-initialized matrix.
WMatrixFixed< typename WTypeTraits::TypePromotion< ValueT, RHSValueT >::Result, Rows, Cols, ValueStoreT > operator+(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Matrix addition.
const ValueT & operator[](size_t row) const
Returns a reference to the component of the first column to provide access to the component.
WMatrixFixed(const ValueT &x, const ValueT &y, const ValueT &z, const ValueT &w)
Constructor easing the initialization of vectors.
ValueT & y()
Access y element of vector.
size_t size() const
The number of entries.
void operator-=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Matrix subtraction with self-assignment.
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...
ValueT & x()
Access x element of vector.
ValueStoreT< ValueT, Rows, Cols > ValueStoreType
The storage container.
WMatrixFixed(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &m)
Copy construction casting the given value type.
WMatrixFixed(const osg::Matrixd &m)
Creates a WMatrix from a given OSG 4x4 Matrix.
size_t getRows() const
The number of rows.
void operator+=(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs)
Matrix addition with self-assignment.
ValueT & operator[](size_t row)
Returns a reference to the component of the first column to provide access to the component.
void setRowVector(size_t index, const WMatrixFixed< RHSValueT, Rows, 1, RHSValueStoreT > &vec)
Set a row to a specific vector.
WMatrixFixed()
Default constructor.
const ValueT & x() const
Access x element of vector.
WMatrixFixed(const WValue< ValueT > &val)
Casting constructor for WValue.
void operator/=(const RHSValueT &rhs)
Matrix-Scalar division with self-assignmnet.
WMatrixFixed< ValueT, Cols, 1, ValueStoreT > getRowVector(size_t index) const
Get a vector containing a specific row.
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.
bool operator==(const WMatrixFixed< RHSValueT, Rows, Cols, RHSValueStoreT > &rhs) const
Compares two matrices and returns true if they are equal (component-wise).
WMatrixFixed(const ValueT &x, const ValueT &y)
Constructor easing the initialization of vectors.
Indicates invalid element access of a container.
Base class for all higher level values like tensors, vectors, matrices and so on.
size_t size() const
Get number of components the value consists of.
std::string toString(const T &value)
Convert a given value to a string.