Reduce redundant code in core::quaternion

Instead of using same matrix calculations 3 times, getMatrixCenter and getMatrix now both call getMatrixFast.
Additional function call might be slight cost in debug, in release compilers hopefully inline it away.
Also getMatrix_transposed now split into getMatrix_transposed and getMatrixFast_transposed to make it similar to getMatrix.
This also avoids a bunch of level 4 warnings in VS about function variables hiding class variables, which was why I started on this. Thought I also prefer having less code here.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6587 dfc29bdd-3216-0410-991c-e03cc46cb475
This commit is contained in:
cutealien 2024-01-27 14:31:08 +00:00
parent c35e0412a3
commit 1d3794c8b4
2 changed files with 17 additions and 52 deletions

View File

@ -130,7 +130,10 @@ class quaternion
*/
void getMatrixCenter( matrix4 &dest, const core::vector3df &center, const core::vector3df &translation ) const;
//! Creates a matrix from this quaternion
//! Faster method to create a transposed matrix, you should normalize the quaternion before!
inline void getMatrixFast_transposed(matrix4 &dest) const;
//! Creates a transposed matrix from this quaternion
inline void getMatrix_transposed( matrix4 &dest ) const;
//! Inverts this quaternion
@ -392,32 +395,11 @@ inline void quaternion::getMatrix(matrix4 &dest,
quaternion q( *this);
q.normalize();
f32 X = q.X;
f32 Y = q.Y;
f32 Z = q.Z;
f32 W = q.W;
dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
dest[1] = 2.0f*X*Y + 2.0f*Z*W;
dest[2] = 2.0f*X*Z - 2.0f*Y*W;
dest[3] = 0.0f;
dest[4] = 2.0f*X*Y - 2.0f*Z*W;
dest[5] = 1.0f - 2.0f*X*X - 2.0f*Z*Z;
dest[6] = 2.0f*Z*Y + 2.0f*X*W;
dest[7] = 0.0f;
dest[8] = 2.0f*X*Z + 2.0f*Y*W;
dest[9] = 2.0f*Z*Y - 2.0f*X*W;
dest[10] = 1.0f - 2.0f*X*X - 2.0f*Y*Y;
dest[11] = 0.0f;
q.getMatrixFast(dest);
dest[12] = center.X;
dest[13] = center.Y;
dest[14] = center.Z;
dest[15] = 1.f;
dest.setDefinitelyIdentityMatrix ( false );
}
@ -439,39 +421,14 @@ inline void quaternion::getMatrixCenter(matrix4 &dest,
{
quaternion q(*this);
q.normalize();
f32 X = q.X;
f32 Y = q.Y;
f32 Z = q.Z;
f32 W = q.W;
dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
dest[1] = 2.0f*X*Y + 2.0f*Z*W;
dest[2] = 2.0f*X*Z - 2.0f*Y*W;
dest[3] = 0.0f;
dest[4] = 2.0f*X*Y - 2.0f*Z*W;
dest[5] = 1.0f - 2.0f*X*X - 2.0f*Z*Z;
dest[6] = 2.0f*Z*Y + 2.0f*X*W;
dest[7] = 0.0f;
dest[8] = 2.0f*X*Z + 2.0f*Y*W;
dest[9] = 2.0f*Z*Y - 2.0f*X*W;
dest[10] = 1.0f - 2.0f*X*X - 2.0f*Y*Y;
dest[11] = 0.0f;
q.getMatrixFast(dest);
dest.setRotationCenter ( center, translation );
}
// Creates a matrix from this quaternion
inline void quaternion::getMatrix_transposed(matrix4 &dest) const
//! Faster method to create a transposed matrix, you should normalize the quaternion before!
inline void quaternion::getMatrixFast_transposed(matrix4 &dest) const
{
quaternion q(*this);
q.normalize();
f32 X = q.X;
f32 Y = q.Y;
f32 Z = q.Z;
f32 W = q.W;
dest[0] = 1.0f - 2.0f*Y*Y - 2.0f*Z*Z;
dest[4] = 2.0f*X*Y + 2.0f*Z*W;
dest[8] = 2.0f*X*Z - 2.0f*Y*W;
@ -495,6 +452,14 @@ inline void quaternion::getMatrix_transposed(matrix4 &dest) const
dest.setDefinitelyIdentityMatrix(false);
}
// Creates a matrix from this quaternion
inline void quaternion::getMatrix_transposed(matrix4 &dest) const
{
quaternion q(*this);
q.normalize();
q.getMatrixFast_transposed(dest);
}
// Inverts this quaternion
inline quaternion& quaternion::makeInverse()

View File

@ -1,4 +1,4 @@
Tests finished. 72 tests of 72 passed.
Compiled as DEBUG
Test suite pass at GMT Sun Dec 31 17:50:49 2023
Test suite pass at GMT Sat Jan 27 14:25:49 2024