summaryrefslogtreecommitdiffstats
path: root/src/gui/math3d/qquaternion.cpp
diff options
context:
space:
mode:
authorRhys Weatherley <rhys.weatherley@nokia.com>2009-04-15 23:21:44 (GMT)
committerRhys Weatherley <rhys.weatherley@nokia.com>2009-04-15 23:25:22 (GMT)
commitc57b1a8629b7fa79f48a35eea2f09d923b665dc0 (patch)
treef17a70d2534cea9b095afa479135b1e5ea79805e /src/gui/math3d/qquaternion.cpp
parent1cb11b428ef1bee070af72676b7eb1fa325bb980 (diff)
downloadQt-c57b1a8629b7fa79f48a35eea2f09d923b665dc0.zip
Qt-c57b1a8629b7fa79f48a35eea2f09d923b665dc0.tar.gz
Qt-c57b1a8629b7fa79f48a35eea2f09d923b665dc0.tar.bz2
Optimize vector normalize for vectors of length 1
If the square of the length is very close to 1, then avoid the qSqrt(). Reviewed-by: trustme
Diffstat (limited to 'src/gui/math3d/qquaternion.cpp')
-rw-r--r--src/gui/math3d/qquaternion.cpp24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp
index a91b0b9..96659ea 100644
--- a/src/gui/math3d/qquaternion.cpp
+++ b/src/gui/math3d/qquaternion.cpp
@@ -230,40 +230,46 @@ qreal QQuaternion::lengthSquared() const
}
/*!
- Returns the normalized unit form of this quaternion. If this quaternion
- is not null, the returned quaternion is guaranteed to be 1.0 in length.
+ Returns the normalized unit form of this quaternion.
+
If this quaternion is null, then a null quaternion is returned.
+ If the length of the quaternion is very close to 1, then the quaternion
+ will be returned as-is. Otherwise the normalized form of the
+ quaternion of length 1 will be returned.
\sa length(), normalize()
*/
QQuaternion QQuaternion::normalized() const
{
- qreal len = length();
- if (!qIsNull(len))
- return *this / len;
+ qreal len = lengthSquared();
+ if (qFuzzyIsNull(len - 1.0f))
+ return *this;
+ else if (!qFuzzyIsNull(len))
+ return *this / qSqrt(len);
else
return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
}
/*!
Normalizes the currect quaternion in place. Nothing happens if this
- is a null quaternion.
+ is a null quaternion or the length of the quaternion is very close to 1.
\sa length(), normalized()
*/
void QQuaternion::normalize()
{
- qreal len = length();
- if (qIsNull(len))
+ qreal len = lengthSquared();
+ if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
return;
+ len = qSqrt(len);
+
xp /= len;
yp /= len;
zp /= len;
wp /= len;
}
-
/*!
\fn QQuaternion QQuaternion::conjugate() const