Sorry Jim.iMalc yes this is true, as i pointed to my first post.I posted this by accident.If i add another const then there is going to be problem in the body of the overloaded operaton in these lines
Code:
M(r,c) += _v[r][i]*m(i,c);
M(r,c,-1) += m(r,i,-1)*_inv[i][c];
The function for the () is this
Code:
/*
* write,read the matrix element at i,j:
* element of the matrix when no third parameter is specified
* the element at [i,j] in the inverse matrix otherwise
*/
double& operator() (int i, int j, int invFlag = 0)
{
if (invFlag == 0)
return _v[i][j];
else
return _inv[i][j];
};
but as you can see in the translation function i can not make the () const because it will throw an error for invalid initialization.
I will post here the code with iMalc's suggestion
Code:
#include "Matrix4.h"
int main()
{
Matrix4d T = Matrix4d::translation (-1.0, -1.0, 5.0);
Matrix4d R1 = Matrix4d::rotation (PI/4.0,'x');
Matrix4d R4 = Matrix4d::rotation (1.0,'y');
Matrix4d M = T*R4*R1.inverse(); //here it is!
return 0;
}
Matrix4.h
Code:
#ifndef MATRIX4_H
#define MATRIX4_H
/***************************************************************************
MATRIX.h
Comment: This file contains all matrix definitions.
***************************************************************************/
//-----------------------------------------------------------------------------
/// Template class for 4x4-Matrices.
/**
* This is the main class for all 4x4-Matrices.
* The type of the 16 components is variable.
*/
class Matrix4d {
//-----------------------------------------------------------------------------
public:
...
/*
* write,read the matrix element at i,j:
* element of the matrix when no third parameter is specified
* the element at [i,j] in the inverse matrix otherwise
*/
double& operator() (int i, int j, int invFlag = 0)
{
if (invFlag == 0)
return _v[i][j];
else
return _inv[i][j];
};
/*
* Constructor for a translation matrix:
*/
static Matrix4d translation( const double x, const double y, const double z)
{
Matrix4d M;
// set the last column of the matrix
M(0,3) = x;
M(1,3) = y;
M(2,3) = z;
// set the last column of the inverse matrix
M(0,3,-1) = -x;
M(1,3,-1) = -y;
M(2,3,-1) = -z;
return M;
};
/*
* Constructor for a rotation matrix:
*/
static Matrix4d rotation( const double angle, const char axis)
{
Matrix4d M;
// set rotation around x axis:
if ((axis == 'x') || (axis == 'X')) {
M(1,1) = cos(angle); M(1,1,-1) = cos(angle);
M(1,2) = -sin(angle); M(1,2,-1) = sin(angle);
M(2,1) = sin(angle); M(2,1,-1) = -sin(angle);
M(2,2) = cos(angle); M(2,2,-1) = cos(angle);
return M;
}
// set rotation around y axis:
if ((axis == 'y') || (axis == 'Y')) {
M(0,0) = cos(angle); M(0,0,-1) = cos(angle);
M(0,2) = sin(angle); M(0,2,-1) = -sin(angle);
M(2,0) = -sin(angle); M(2,0,-1) = sin(angle);
M(2,2) = cos(angle); M(2,2,-1) = cos(angle);
return M;
}
// set rotation around z axis:
if ((axis == 'z') || (axis == 'Z')) {
M(0,0) = cos(angle); M(0,0,-1) = cos(angle);
M(0,1) = -sin(angle); M(0,1,-1) = sin(angle);
M(1,0) = sin(angle); M(1,0,-1) = -sin(angle);
M(1,1) = cos(angle); M(1,1,-1) = cos(angle);
return M;
}
return M;
};
....
/*
* returns the invers of the current matrix:
*/
Matrix4d inverse()
{
Matrix4d M;
for (int r=0; r<4; r++)
for (int c=0; c<4; c++) {
M(r,c) = _inv[r][c];
M(r,c,-1) = _v[r][c];
}
return M;
};
....
/**
* Multiplication of two matrices.
* @param m - the other Matrix
* @return the <b> product </b> of the matrix multiplication
*/
const Matrix4d operator * ( const Matrix4d& m ) const {
Matrix4d M;
// set matrix diagonals to 0
for (int r=0; r<4; r++) { M(r,r) = 0; M(r,r,-1) = 0;}
// Now multiply the two matrices:
for (int r=0; r<4; r++)
for (int c=0; c<4; c++)
for (int i=0; i<4; i++) {
M(r,c) += _v[r][i]*m(i,c);
M(r,c,-1) += m(r,i,-1)*_inv[i][c];
}
return M;
};
..
//-----------------------------------------------------------------------------
private:
/*
* The components of the Matrix in _v[][]
* The components of the inverse Matrix in _inv[][]
*/
double _v[4][4];
double _inv[4][4];
};
#endif