33 #include "linearAlgebra/WMatrixFixed.h"
34 #include "linearAlgebra/WVectorFixed.h"
36 #include "../WDefines.h"
81 WMatrix(
const Eigen::MatrixXd& newMatrix );
88 WMatrix(
const Eigen::MatrixXf& newMatrix );
95 WMatrix(
const Eigen::MatrixXi& newMatrix );
145 operator osg::Matrixd()
const;
154 template<
typename EigenDataType >
155 operator Eigen::Matrix< EigenDataType, Eigen::Dynamic, Eigen::Dynamic >()
const;
210 for(
size_t i = 0; i < this->
size(); ++i )
212 ( *this )[ i ] = 0.0;
236 template<
typename EigenDataType >
237 void copyFromEigenMatrix(
const Eigen::Matrix< EigenDataType, Eigen::Dynamic, Eigen::Dynamic >& newMatrix );
249 :
WValue< T >( nbRows * nbCols )
259 :
WValue< T >( newMatrix )
268 for(
size_t i = 0; i < 4; ++i )
270 for(
size_t j = 0; j < 4; ++j )
272 ( *this )( i, j ) = newMatrix( i, j );
278 :
WValue< T >( newMatrix.cols() * newMatrix.rows() )
280 copyFromEigenMatrix< double >( newMatrix );
284 :
WValue< T >( newMatrix.cols() * newMatrix.rows() )
286 copyFromEigenMatrix< float >( newMatrix );
290 :
WValue< T >( newMatrix.cols() * newMatrix.rows() )
292 copyFromEigenMatrix< int >( newMatrix );
297 size_t nbRows = this->size() / m_nbCols;
298 WAssert( m_nbCols == 4 && nbRows == 4,
"This is no 4x4 matrix." );
300 for(
size_t i = 0; i < nbRows; ++i )
302 for(
size_t j = 0; j < m_nbCols; ++j )
304 m( i, j ) = ( *this )( i, j );
312 WAssert( ( getNbRows() == 3 || getNbRows() == 4 ) && ( getNbCols() == 3 || getNbCols() == 4 ),
313 "Only 3x3 or 4x4 matrices allowed." );
316 if( getNbRows() == 4 )
318 return osg::Matrixd( ( *
this )[ 0 ], ( *
this )[ 4 ], ( *
this )[ 8 ], ( *
this )[ 12 ],
319 ( *
this )[ 1 ], ( *
this )[ 5 ], ( *
this )[ 9 ], ( *
this )[ 13 ],
320 ( *
this )[ 2 ], ( *
this )[ 6 ], ( *
this )[ 10 ], ( *
this )[ 14 ],
321 ( *
this )[ 3 ], ( *
this )[ 7 ], ( *
this )[ 11 ], ( *
this )[ 15 ]
326 return osg::Matrixd( ( *
this )[ 0 ], ( *
this )[ 1 ], ( *
this )[ 2 ], 0.0,
327 ( *
this )[ 3 ], ( *
this )[ 4 ], ( *
this )[ 5 ], 0.0,
328 ( *
this )[ 6 ], ( *
this )[ 7 ], ( *
this )[ 8 ], 0.0,
329 ( *
this )[ 9 ], ( *
this )[ 10 ], ( *
this )[ 11 ], 1.0
334 template<
typename T >
335 template<
typename EigenDataType >
WMatrix< T >::operator Eigen::Matrix< EigenDataType, Eigen::Dynamic, Eigen::Dynamic >()
const
337 Eigen::Matrix< EigenDataType, Eigen::Dynamic, Eigen::Dynamic > matrix( this->getNbRows(), this->getNbCols() );
338 for(
int row = 0; row < matrix.rows(); ++row )
340 for(
int col = 0; col < matrix.cols(); ++col )
342 matrix( row, col ) =
static_cast< EigenDataType
>( ( *this )( row, col ) );
354 size_t nbRows = this->size() / m_nbCols;
355 for(
size_t i = 0; i < nbRows; ++i )
357 for(
size_t j = 0; j < m_nbCols; ++j )
377 return this->size() / m_nbCols;
396 WAssert( j < m_nbCols && i * m_nbCols < this->size(),
"Index out of bounds." );
397 return (*
this)[i * m_nbCols + j];
408 WAssert( j < m_nbCols && i * m_nbCols < this->size(),
"Index out of bounds." );
409 return (*
this)[i * m_nbCols + j];
449 WMatrix result( m_nbCols, getNbRows() );
451 for( std::size_t i = 0; i < getNbRows(); i++ )
452 for( std::size_t j = 0; j < m_nbCols; j++ )
453 result( j, i ) = (*this)( i, j);
459 WAssert( rhs.
getNbRows() == getNbCols(),
"Incompatible number of rows of rhs and columns of lhs." );
462 for(
size_t r = 0; r < getNbRows(); ++r )
464 for(
size_t c = 0; c < rhs.
getNbCols(); ++c )
466 for(
size_t i = 0; i < getNbCols(); ++i )
468 result( r, c ) += ( *this )( r, i ) * rhs( i, c );
477 WAssert( rhs.
size() == getNbCols(),
"Incompatible number of rows of rhs and columns of lhs." );
480 for(
size_t r = 0; r < getNbRows(); ++r )
482 for(
size_t i = 0; i < getNbCols(); ++i )
484 result[r] += ( *this )( r, i ) * rhs[i];
492 WAssert( rhs.
getRows() == getNbCols(),
"Incompatible number of rows of rhs and columns of lhs." );
495 for(
size_t r = 0; r < getNbRows(); ++r )
497 for(
size_t i = 0; i < getNbCols(); ++i )
499 result[r] += ( *this )( r, i ) * rhs[i];
507 return getNbRows() == getNbCols();
517 for(
size_t row = 0; row < getNbRows(); row++ )
519 for(
size_t col = 0; col < getNbCols(); col++ )
521 T val = ( *this )( row, col );
522 T expected = ( row == col ? T( 1.0 ) : T( 0.0 ) );
523 if( std::fabs( val - expected ) > delta )
532 template<
typename T >
533 template<
typename EigenDataType >
536 m_nbCols =
static_cast< size_t >( newMatrix.cols() );
537 for(
int row = 0; row < newMatrix.rows(); ++row )
539 for(
int col = 0; col < newMatrix.cols(); ++col )
541 ( *this )( row, col ) =
static_cast< T
>( newMatrix( row, col ) );
546 template<
typename T >
547 inline std::ostream& operator<<( std::ostream& os,
const WMatrix< T >& m )
549 os << std::setprecision( 5 ) << std::fixed;
550 for(
size_t i = 0; i < m.
getNbRows(); ++i )
560 for(
size_t j = 0; j < m.
getNbCols(); ++j )
562 os << std::setw( 12 ) << m( i, j );
size_t getRows() const
The number of rows.
Matrix template class with variable number of rows and columns.
T & operator()(size_t i, size_t j)
Returns a reference to the component an row i, columns j in order to provide access to the component.
WVector3d operator*(const WVector3d &rhs) const
Multiplication with a vector.
bool operator==(const WMatrix &rhs) const
Compares two matrices and returns true if they are equal.
size_t getNbRows() const
Get number of rows.
bool isIdentity(T delta=T(0.0)) const
Returns true if the matrix is a identity matrix.
WValue< T > operator*(const WValue< T > &rhs) const
Multiplication with a vector.
WMatrix & operator=(const WMatrix &rhs)
Assigns the argument WMatrix to this WMatrix.
WMatrix transposed() const
Returns the transposed matrix.
size_t getNbCols() const
Get number of columns.
WMatrix(const WMatrix &newMatrix)
Produces a matrix as copy of the one given as parameter.
WMatrix & makeIdentity()
Makes the matrix contain the identity matrix, i.e.
WMatrix operator*(const WMatrix &rhs) const
Multiplication of the current matrix with andother matrix.
WMatrix(const Eigen::MatrixXf &newMatrix)
Copies the specified Eigen::MatrixXf.
WMatrix(size_t n)
Produces a square matrix with the given number of components.
WMatrix(size_t nbRows, size_t nbCols)
Produces a matrix with the given number of components.
WMatrix(const WMatrix4d &newMatrix)
Copies the specified 4x4 matrix.
void setZero()
Resets the matrix components to zero.
bool isSquare() const
Returns true if the matrix is a square matrix.
WMatrix(const Eigen::MatrixXd &newMatrix)
Copies the specified Eigen::MatrixXd.
size_t m_nbCols
Number of columns of the matrix. The number of rows will be computed by (size/m_nbCols).
const T & operator()(size_t i, size_t j) const
Returns a const reference to the component an row i, columns j in order to provide read-only access t...
WMatrix(const Eigen::MatrixXi &newMatrix)
Copies the specified Eigen::MatrixXi.
bool operator!=(const WMatrix &rhs) const
Compares two matrices and returns true if they are not equal.
void copyFromEigenMatrix(const Eigen::Matrix< EigenDataType, Eigen::Dynamic, Eigen::Dynamic > &newMatrix)
This function is used by the constructors that have the different Eigen::MatrixX types as parameter.
Base class for all higher level values like tensors, vectors, matrices and so on.
bool operator!=(const WValue &rhs) const
Compares two WValues and returns true if they contain the different data.
size_t size() const
Get number of components the value consists of.
bool operator==(const WValue &rhs) const
Compares two WValues and returns true if they contain the same data.
WValue & operator=(const WValue &rhs)
Assigns the contents of its argument to the contents of this WValue.