summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKonstantin Ritt <ritt.ks@gmail.com>2015-02-26 17:46:14 +0400
committerAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-03-05 10:46:07 +0000
commita5d5353b59ab7eb41d37048064f46490f98a228a (patch)
tree4783dfbb5b98c6acf87d28985cdb2d1042bc083e /src
parent6c973dee2cb1686ea32657fff7dced3e611b98ce (diff)
Introduce QQuaternion::dotProduct()
Change-Id: I14b9857ca0a43808b7d536fc258a6bb10f611211 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/gui/math3d/qquaternion.cpp37
-rw-r--r--src/gui/math3d/qquaternion.h7
2 files changed, 27 insertions, 17 deletions
diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp
index 4f07d82a25..141651eda1 100644
--- a/src/gui/math3d/qquaternion.cpp
+++ b/src/gui/math3d/qquaternion.cpp
@@ -213,9 +213,18 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \fn float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2)
+ \since 5.5
+
+ Returns the dot product of \a q1 and \a q2.
+
+ \sa length()
+*/
+
+/*!
Returns the length of the quaternion. This is also called the "norm".
- \sa lengthSquared(), normalized()
+ \sa lengthSquared(), normalized(), dotProduct()
*/
float QQuaternion::length() const
{
@@ -225,7 +234,7 @@ float QQuaternion::length() const
/*!
Returns the squared length of the quaternion.
- \sa length()
+ \sa length(), dotProduct()
*/
float QQuaternion::lengthSquared() const
{
@@ -240,7 +249,7 @@ float QQuaternion::lengthSquared() const
will be returned as-is. Otherwise the normalized form of the
quaternion of length 1 will be returned.
- \sa length(), normalize()
+ \sa normalize(), length(), dotProduct()
*/
QQuaternion QQuaternion::normalized() const
{
@@ -792,13 +801,10 @@ QQuaternion QQuaternion::slerp
return q2;
// Determine the angle between the two quaternions.
- QQuaternion q2b;
- float dot;
- dot = q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp + q1.wp * q2.wp;
- if (dot >= 0.0f) {
- q2b = q2;
- } else {
- q2b = -q2;
+ QQuaternion q2b(q2);
+ float dot = QQuaternion::dotProduct(q1, q2);
+ if (dot < 0.0f) {
+ q2b = -q2b;
dot = -dot;
}
@@ -844,13 +850,10 @@ QQuaternion QQuaternion::nlerp
return q2;
// Determine the angle between the two quaternions.
- QQuaternion q2b;
- float dot;
- dot = q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp + q1.wp * q2.wp;
- if (dot >= 0.0f)
- q2b = q2;
- else
- q2b = -q2;
+ QQuaternion q2b(q2);
+ float dot = QQuaternion::dotProduct(q1, q2);
+ if (dot < 0.0f)
+ q2b = -q2b;
// Perform the linear interpolation.
return (q1 * (1.0f - t) + q2b * t).normalized();
diff --git a/src/gui/math3d/qquaternion.h b/src/gui/math3d/qquaternion.h
index a02c37ce1d..b4022e8579 100644
--- a/src/gui/math3d/qquaternion.h
+++ b/src/gui/math3d/qquaternion.h
@@ -78,6 +78,8 @@ public:
void setZ(float z);
void setScalar(float scalar);
+ Q_DECL_CONSTEXPR static inline float dotProduct(const QQuaternion &q1, const QQuaternion &q2);
+
float length() const;
float lengthSquared() const;
@@ -168,6 +170,11 @@ inline void QQuaternion::setY(float aY) { yp = aY; }
inline void QQuaternion::setZ(float aZ) { zp = aZ; }
inline void QQuaternion::setScalar(float aScalar) { wp = aScalar; }
+Q_DECL_CONSTEXPR inline float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2)
+{
+ return q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp + q1.wp * q2.wp;
+}
+
inline QQuaternion QQuaternion::inverted() const
{
// Need some extra precision if the length is very small.