33 #include "linearAlgebra/WLinearAlgebra.h"
35 #include "../WDefines.h"
60 WMatrix(
size_t nbRows,
size_t nbCols );
80 WMatrix(
const Eigen::MatrixXd& newMatrix );
130 operator osg::Matrixd()
const;
137 operator Eigen::MatrixXd()
const;
192 for(
size_t i = 0; i < this->
size(); ++i )
194 ( *this )[ i ] = 0.0;
210 :
WValue<
T >( nbRows * nbCols )
229 for(
size_t i = 0; i < 4; ++i )
231 for(
size_t j = 0; j < 4; ++j )
233 ( *this )( i, j ) = newMatrix( i, j );
239 :
WValue<
T >( newMatrix.cols() * newMatrix.rows() )
241 m_nbCols =
static_cast< size_t >( newMatrix.cols() );
242 for(
int row = 0; row < newMatrix.rows(); ++row )
244 for(
int col = 0; col < newMatrix.cols(); ++col )
246 ( *this )( row, col ) = static_cast< T >( newMatrix( row, col ) );
254 size_t nbRows = this->size() / m_nbCols;
255 WAssert( m_nbCols == 4 && nbRows == 4,
"This is no 4x4 matrix." );
257 for(
size_t i = 0; i < nbRows; ++i )
259 for(
size_t j = 0; j < m_nbCols; ++j )
261 m( i, j ) = ( *this )( i, j );
269 WAssert( ( getNbRows() == 3 || getNbRows() == 4 ) && ( getNbCols() == 3 || getNbCols() == 4 ),
270 "Only 3x3 or 4x4 matrices allowed." );
273 if( getNbRows() == 4 )
275 return osg::Matrixd( ( *
this )[ 0 ], ( *
this )[ 4 ], ( *
this )[ 8 ], ( *
this )[ 12 ],
276 ( *
this )[ 1 ], ( *
this )[ 5 ], ( *
this )[ 9 ], ( *
this )[ 13 ],
277 ( *
this )[ 2 ], ( *
this )[ 6 ], ( *
this )[ 10 ], ( *
this )[ 14 ],
278 ( *
this )[ 3 ], ( *
this )[ 7 ], ( *
this )[ 11 ], ( *
this )[ 15 ]
283 return osg::Matrixd( ( *
this )[ 0 ], ( *
this )[ 1 ], ( *
this )[ 2 ], 0.0,
284 ( *
this )[ 3 ], ( *
this )[ 4 ], ( *
this )[ 5 ], 0.0,
285 ( *
this )[ 6 ], ( *
this )[ 7 ], ( *
this )[ 8 ], 0.0,
286 ( *
this )[ 9 ], ( *
this )[ 10 ], ( *
this )[ 11 ], 1.0
293 Eigen::MatrixXd matrix( this->getNbRows(), this->getNbCols() );
294 for(
int row = 0; row < matrix.rows(); ++row )
296 for(
int col = 0; col < matrix.cols(); ++col )
298 matrix( row, col ) = ( *this )( row, col );
309 size_t nbRows = this->size() / m_nbCols;
310 for(
size_t i = 0; i < nbRows; ++i )
312 for(
size_t j = 0; j < m_nbCols; ++j )
332 return this->size() / m_nbCols;
351 WAssert( j < m_nbCols && i * m_nbCols < this->size(),
"Index out of bounds." );
352 return (*
this)[i * m_nbCols + j];
363 WAssert( j < m_nbCols && i * m_nbCols < this->size(),
"Index out of bounds." );
364 return (*
this)[i * m_nbCols + j];
401 WMatrix result( m_nbCols, getNbRows() );
403 for( std::size_t i = 0; i < getNbRows(); i++ )
404 for( std::size_t j = 0; j < m_nbCols; j++ )
405 result( j, i ) = (*this)( i, j);
411 WAssert( rhs.
getNbRows() == getNbCols(),
"Incompatible number of rows of rhs and columns of lhs." );
414 for(
size_t r = 0; r < getNbRows(); ++r)
416 for(
size_t c = 0; c < rhs.
getNbCols(); ++c )
418 for(
size_t i = 0; i < getNbCols(); ++i )
420 result( r, c ) += ( *this )( r, i ) * rhs( i, c );
429 WAssert( rhs.
size() == getNbCols(),
"Incompatible number of rows of rhs and columns of lhs." );
432 for(
size_t r = 0; r < getNbRows(); ++r)
434 for(
size_t i = 0; i < getNbCols(); ++i )
436 result[r] += ( *this )( r, i ) * rhs[i];
444 WAssert( rhs.
getRows() == getNbCols(),
"Incompatible number of rows of rhs and columns of lhs." );
447 for(
size_t r = 0; r < getNbRows(); ++r)
449 for(
size_t i = 0; i < getNbCols(); ++i )
451 result[r] += ( *this )( r, i ) * rhs[i];
457 template<
typename T >
458 inline std::ostream& operator<<( std::ostream& os, const WMatrix< T >& m )
460 os << std::setprecision( 5 ) << std::fixed;
461 for(
size_t i = 0; i < m.getNbRows(); ++i )
471 for(
size_t j = 0; j < m.getNbCols(); ++j )
473 os << std::setw( 12 ) << m( i, j );
474 if( j < m.getNbCols() - 1 )
478 else if( i < m.getNbRows() - 1 )