Optimize quaternion to rotation matrix convertion

Rearrange operands to get rid of 50% of multiplications
(i.e. `2 * (x * x + z * z)` -> `(x + x) * x + (z + z) * z`).

Change-Id: Ib5279425ead999fc571b4964ac1681b6e22f9a7e
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
This commit is contained in:
Konstantin Ritt 2015-02-20 03:38:32 +04:00
parent e628c80dc3
commit b7d4e4ffeb
2 changed files with 46 additions and 37 deletions

View File

@ -1322,27 +1322,33 @@ void QMatrix4x4::rotate(const QQuaternion& quaternion)
{
// Algorithm from:
// http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q54
QMatrix4x4 m(1);
float xx = quaternion.x() * quaternion.x();
float xy = quaternion.x() * quaternion.y();
float xz = quaternion.x() * quaternion.z();
float xw = quaternion.x() * quaternion.scalar();
float yy = quaternion.y() * quaternion.y();
float yz = quaternion.y() * quaternion.z();
float yw = quaternion.y() * quaternion.scalar();
float zz = quaternion.z() * quaternion.z();
float zw = quaternion.z() * quaternion.scalar();
m.m[0][0] = 1.0f - 2 * (yy + zz);
m.m[1][0] = 2 * (xy - zw);
m.m[2][0] = 2 * (xz + yw);
QMatrix4x4 m(Qt::Uninitialized);
const float f2x = quaternion.x() + quaternion.x();
const float f2y = quaternion.y() + quaternion.y();
const float f2z = quaternion.z() + quaternion.z();
const float f2xw = f2x * quaternion.scalar();
const float f2yw = f2y * quaternion.scalar();
const float f2zw = f2z * quaternion.scalar();
const float f2xx = f2x * quaternion.x();
const float f2xy = f2x * quaternion.y();
const float f2xz = f2x * quaternion.z();
const float f2yy = f2y * quaternion.y();
const float f2yz = f2y * quaternion.z();
const float f2zz = f2z * quaternion.z();
m.m[0][0] = 1.0f - (f2yy + f2zz);
m.m[1][0] = f2xy - f2zw;
m.m[2][0] = f2xz + f2yw;
m.m[3][0] = 0.0f;
m.m[0][1] = 2 * (xy + zw);
m.m[1][1] = 1.0f - 2 * (xx + zz);
m.m[2][1] = 2 * (yz - xw);
m.m[0][1] = f2xy + f2zw;
m.m[1][1] = 1.0f - (f2xx + f2zz);
m.m[2][1] = f2yz - f2xw;
m.m[3][1] = 0.0f;
m.m[0][2] = 2 * (xz - yw);
m.m[1][2] = 2 * (yz + xw);
m.m[2][2] = 1.0f - 2 * (xx + yy);
m.m[0][2] = f2xz - f2yw;
m.m[1][2] = f2yz + f2xw;
m.m[2][2] = 1.0f - (f2xx + f2yy);
m.m[3][2] = 0.0f;
m.m[0][3] = 0.0f;
m.m[1][3] = 0.0f;

View File

@ -592,25 +592,28 @@ QMatrix3x3 QQuaternion::toRotationMatrix() const
QMatrix3x3 rot3x3(Qt::Uninitialized);
const float xx = xp * xp;
const float xy = xp * yp;
const float xz = xp * zp;
const float xw = xp * wp;
const float yy = yp * yp;
const float yz = yp * zp;
const float yw = yp * wp;
const float zz = zp * zp;
const float zw = zp * wp;
const float f2x = xp + xp;
const float f2y = yp + yp;
const float f2z = zp + zp;
const float f2xw = f2x * wp;
const float f2yw = f2y * wp;
const float f2zw = f2z * wp;
const float f2xx = f2x * xp;
const float f2xy = f2x * yp;
const float f2xz = f2x * zp;
const float f2yy = f2y * yp;
const float f2yz = f2y * zp;
const float f2zz = f2z * zp;
rot3x3(0, 0) = 1.0f - 2.0f * (yy + zz);
rot3x3(0, 1) = 2.0f * (xy - zw);
rot3x3(0, 2) = 2.0f * (xz + yw);
rot3x3(1, 0) = 2.0f * (xy + zw);
rot3x3(1, 1) = 1.0f - 2.0f * (xx + zz);
rot3x3(1, 2) = 2.0f * (yz - xw);
rot3x3(2, 0) = 2.0f * (xz - yw);
rot3x3(2, 1) = 2.0f * (yz + xw);
rot3x3(2, 2) = 1.0f - 2.0f * (xx + yy);
rot3x3(0, 0) = 1.0f - (f2yy + f2zz);
rot3x3(0, 1) = f2xy - f2zw;
rot3x3(0, 2) = f2xz + f2yw;
rot3x3(1, 0) = f2xy + f2zw;
rot3x3(1, 1) = 1.0f - (f2xx + f2zz);
rot3x3(1, 2) = f2yz - f2xw;
rot3x3(2, 0) = f2xz - f2yw;
rot3x3(2, 1) = f2yz + f2xw;
rot3x3(2, 2) = 1.0f - (f2xx + f2yy);
return rot3x3;
}