From 98fcf3cb611fe895e6998726ae39bdd30512c98b Mon Sep 17 00:00:00 2001 From: Inho Lee Date: Mon, 10 May 2021 15:32:26 +0200 Subject: QtGui/math3d : Fix QQuaternion::getEulerAngles When rotating M_PI_2 based on x-axis, quaternion to euler conversion makes NaN for the x-rotation value. This patch fixes this corner case. Fixes: QTBUG-93600 Change-Id: Ice321a80ad90dba9cf3ee3a14ec7d3d047c21bd3 Reviewed-by: Laszlo Agocs Reviewed-by: Andy Nichols (cherry picked from commit 7ea2fbddcf674d49ad7d219cdb8a4b760258360c) Reviewed-by: Qt Cherry-pick Bot --- src/gui/math3d/qquaternion.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gui/math3d/qquaternion.cpp b/src/gui/math3d/qquaternion.cpp index 440ed3c99b..86e7d8ce62 100644 --- a/src/gui/math3d/qquaternion.cpp +++ b/src/gui/math3d/qquaternion.cpp @@ -527,7 +527,11 @@ void QQuaternion::getEulerAngles(float *pitch, float *yaw, float *roll) const zw /= lengthSquared; } - *pitch = std::asin(-2.0f * (yz - xw)); + const float sinp = -2.0f * (yz - xw); + if (std::abs(sinp) >= 1.0f) + *pitch = std::copysign(M_PI_2, sinp); + else + *pitch = std::asin(sinp); if (*pitch < M_PI_2) { if (*pitch > -M_PI_2) { *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy)); -- cgit v1.2.3