summaryrefslogtreecommitdiffstats
path: root/src/gui/math3d/qquaternion.cpp
diff options
context:
space:
mode:
authorKonstantin Ritt <ritt.ks@gmail.com>2015-02-09 17:50:25 +0400
committerKonstantin Ritt <ritt.ks@gmail.com>2015-02-14 05:45:00 +0000
commit5d784deb71edd1e6584cd2f118d27dd3be2da5e6 (patch)
tree6152eae42ac7299652fae4475e7d5bc0f5725c94 /src/gui/math3d/qquaternion.cpp
parent255ecba26909408d57c0d58ca211f810c66ce126 (diff)
[QQuaternion] Introduce toAxisAndAngle()
This operation is the exact opposite to QQuaternion::fromAxisAndAngle() (so that it is a way to extract the axis and angle values suitable to create the same quaternion via QQuaternion::fromAxisAndAngle()). Change-Id: I41fda58f5fb2b867cccd6b2faf58ab671fa070da Reviewed-by: Laszlo Agocs <laszlo.agocs@theqtcompany.com>
Diffstat (limited to 'src/gui/math3d/qquaternion.cpp')
-rw-r--r--src/gui/math3d/qquaternion.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp
index 8e59e8a2f1..9c8fa3310d 100644
--- a/src/gui/math3d/qquaternion.cpp
+++ b/src/gui/math3d/qquaternion.cpp
@@ -363,8 +363,21 @@ QVector3D QQuaternion::rotatedVector(const QVector3D& vector) const
#ifndef QT_NO_VECTOR3D
/*!
+ \fn void QQuaternion::toAxisAndAngle(QVector3D *axis, float *angle) const
+ \since 5.5
+ \overload
+
+ Extracts a 3D axis \a axis and a rotating angle \a angle (in degrees)
+ that corresponds to this quaternion.
+
+ \sa fromAxisAndAngle()
+*/
+
+/*!
Creates a normalized quaternion that corresponds to rotating through
\a angle degrees about the specified 3D \a axis.
+
+ \sa toAxisAndAngle()
*/
QQuaternion QQuaternion::fromAxisAndAngle(const QVector3D& axis, float angle)
{
@@ -382,8 +395,45 @@ QQuaternion QQuaternion::fromAxisAndAngle(const QVector3D& axis, float angle)
#endif
/*!
+ \since 5.5
+
+ Extracts a 3D axis (\a x, \a y, \a z) and a rotating angle \a angle (in degrees)
+ that corresponds to this quaternion.
+
+ \sa fromAxisAndAngle()
+*/
+void QQuaternion::toAxisAndAngle(float *x, float *y, float *z, float *angle) const
+{
+ Q_ASSERT(x && y && z && angle);
+
+ // The quaternion representing the rotation is
+ // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
+
+ float length = xp * xp + yp * yp + zp * zp;
+ if (!qFuzzyIsNull(length)) {
+ *x = xp;
+ *y = yp;
+ *z = zp;
+ if (!qFuzzyIsNull(length - 1.0f)) {
+ length = sqrtf(length);
+ *x /= length;
+ *y /= length;
+ *z /= length;
+ }
+ *angle = 2.0f * acosf(wp);
+ } else {
+ // angle is 0 (mod 2*pi), so any axis will fit
+ *x = *y = *z = *angle = 0.0f;
+ }
+
+ *angle = qRadiansToDegrees(*angle);
+}
+
+/*!
Creates a normalized quaternion that corresponds to rotating through
\a angle degrees about the 3D axis (\a x, \a y, \a z).
+
+ \sa toAxisAndAngle()
*/
QQuaternion QQuaternion::fromAxisAndAngle
(float x, float y, float z, float angle)