From 1d3794c8b4b3cc9e6b9d130a499dc765eff4693c Mon Sep 17 00:00:00 2001 From: cutealien Date: Sat, 27 Jan 2024 14:31:08 +0000 Subject: [PATCH] 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 --- include/quaternion.h | 67 ++++++++-------------------------- tests/tests-last-passed-at.txt | 2 +- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/include/quaternion.h b/include/quaternion.h index 863f073e..b2bfe091 100644 --- a/include/quaternion.h +++ b/include/quaternion.h @@ -130,7 +130,10 @@ class quaternion */ void getMatrixCenter( matrix4 &dest, const core::vector3df ¢er, 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() diff --git a/tests/tests-last-passed-at.txt b/tests/tests-last-passed-at.txt index b43c0c91..bc7540d4 100644 --- a/tests/tests-last-passed-at.txt +++ b/tests/tests-last-passed-at.txt @@ -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