Page 1 of 1

[C++] Orthogonal Matrices Check

Posted: Sun Jan 04, 2009 9:27 am
by Halifax
Well, I noticied that there were no methods to check whether a matrix is orthogonal in Irrlicht. At least, I haven't found it. At any rate, here's a function to do it outside of Irrlicht:

Code: Select all

using namespace irr;
...
template <class T>
inline bool isOrthogonal( const core::CMatrix4<T>& mat )
{
	T dp1, dp2, dp3, dp4, dp5, dp6;

	dp1 = mat[0] * mat[4 ] + mat[1] * mat[5 ] + mat[2 ] * mat[6 ] + mat[3 ] * mat[7 ];
	dp2 = mat[0] * mat[8 ] + mat[1] * mat[9 ] + mat[2 ] * mat[10] + mat[3 ] * mat[11];
	dp3 = mat[0] * mat[12] + mat[1] * mat[13] + mat[2 ] * mat[14] + mat[3 ] * mat[15];
	dp4 = mat[4] * mat[8 ] + mat[5] * mat[9 ] + mat[6 ] * mat[10] + mat[7 ] * mat[11];
	dp5 = mat[4] * mat[12] + mat[5] * mat[13] + mat[6 ] * mat[14] + mat[7 ] * mat[15];
	dp6 = mat[8] * mat[12] + mat[9] * mat[13] + mat[10] * mat[14] + mat[11] * mat[15];

	if( core::equals( dp1, 0.f ) &&
	    core::equals( dp2, 0.f ) &&
	    core::equals( dp3, 0.f ) &&
	    core::equals( dp4, 0.f ) &&
	    core::equals( dp5, 0.f ) &&
	    core::equals( dp6, 0.f ) )
		return true;
	return false;
}
If you want to do it inside the engine, then here is the patch: http://sourceforge.net/tracker/index.ph ... tid=540678

FAQ:
*Why is this check beneficial?
- This checks tells whether the transpose of the matrix is equal to the inverse of the matrix. An inverse matrix calculation is very expensive. A transpose is not.
*Well, isn't the time increased if the matrix is not orthogonal, and I have to invert it anyways?
- You would be correct. This check increases the time necessary if the matrix is not orthogonal.
*Any good examples of why to ues this?
- Yes. When transforming a model, if a the transformation matrix is not orthogonal, then you must invert and transpose the transformation matrix for the normals. If it is orthogonal, then there is no need for the expensive inversion or transposition.