2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* quat.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "quat.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2019-02-09 06:24:02 +01:00
|
|
|
#include "core/math/basis.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/print_string.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-08 00:42:28 +01:00
|
|
|
real_t Quat::angle_to(const Quat &p_to) const {
|
|
|
|
real_t d = dot(p_to);
|
2023-05-09 14:27:09 +02:00
|
|
|
|
|
|
|
// acos does clamping.
|
|
|
|
return Math::acos(d * d * 2 - 1);
|
2020-12-08 00:42:28 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 04:55:52 +02:00
|
|
|
// set_euler_xyz expects a vector containing the Euler angles in the format
|
|
|
|
// (ax,ay,az), where ax is the angle of rotation around x axis,
|
|
|
|
// and similar for other axes.
|
|
|
|
// This implementation uses XYZ convention (Z is the first rotation).
|
|
|
|
void Quat::set_euler_xyz(const Vector3 &p_euler) {
|
2022-02-24 09:41:35 +01:00
|
|
|
real_t half_a1 = p_euler.x * 0.5f;
|
|
|
|
real_t half_a2 = p_euler.y * 0.5f;
|
|
|
|
real_t half_a3 = p_euler.z * 0.5f;
|
2016-10-18 22:50:21 +02:00
|
|
|
|
|
|
|
// R = X(a1).Y(a2).Z(a3) convention for Euler angles.
|
|
|
|
// Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-2)
|
|
|
|
// a3 is the angle of the first rotation, following the notation in this reference.
|
|
|
|
|
|
|
|
real_t cos_a1 = Math::cos(half_a1);
|
|
|
|
real_t sin_a1 = Math::sin(half_a1);
|
|
|
|
real_t cos_a2 = Math::cos(half_a2);
|
|
|
|
real_t sin_a2 = Math::sin(half_a2);
|
|
|
|
real_t cos_a3 = Math::cos(half_a3);
|
|
|
|
real_t sin_a3 = Math::sin(half_a3);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set(sin_a1 * cos_a2 * cos_a3 + sin_a2 * sin_a3 * cos_a1,
|
|
|
|
-sin_a1 * sin_a3 * cos_a2 + sin_a2 * cos_a1 * cos_a3,
|
|
|
|
sin_a1 * sin_a2 * cos_a3 + sin_a3 * cos_a1 * cos_a2,
|
|
|
|
-sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
|
2016-10-18 22:50:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 04:55:52 +02:00
|
|
|
// get_euler_xyz returns a vector containing the Euler angles in the format
|
|
|
|
// (ax,ay,az), where ax is the angle of rotation around x axis,
|
|
|
|
// and similar for other axes.
|
|
|
|
// This implementation uses XYZ convention (Z is the first rotation).
|
|
|
|
Vector3 Quat::get_euler_xyz() const {
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis m(*this);
|
2017-08-09 04:55:52 +02:00
|
|
|
return m.get_euler_xyz();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_euler_yxz expects a vector containing the Euler angles in the format
|
|
|
|
// (ax,ay,az), where ax is the angle of rotation around x axis,
|
|
|
|
// and similar for other axes.
|
|
|
|
// This implementation uses YXZ convention (Z is the first rotation).
|
|
|
|
void Quat::set_euler_yxz(const Vector3 &p_euler) {
|
2022-02-24 09:41:35 +01:00
|
|
|
real_t half_a1 = p_euler.y * 0.5f;
|
|
|
|
real_t half_a2 = p_euler.x * 0.5f;
|
|
|
|
real_t half_a3 = p_euler.z * 0.5f;
|
2017-08-09 04:55:52 +02:00
|
|
|
|
|
|
|
// R = Y(a1).X(a2).Z(a3) convention for Euler angles.
|
|
|
|
// Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6)
|
|
|
|
// a3 is the angle of the first rotation, following the notation in this reference.
|
|
|
|
|
|
|
|
real_t cos_a1 = Math::cos(half_a1);
|
|
|
|
real_t sin_a1 = Math::sin(half_a1);
|
|
|
|
real_t cos_a2 = Math::cos(half_a2);
|
|
|
|
real_t sin_a2 = Math::sin(half_a2);
|
|
|
|
real_t cos_a3 = Math::cos(half_a3);
|
|
|
|
real_t sin_a3 = Math::sin(half_a3);
|
|
|
|
|
|
|
|
set(sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3,
|
|
|
|
sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3,
|
2018-04-09 08:48:03 +02:00
|
|
|
-sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3,
|
2017-08-09 04:55:52 +02:00
|
|
|
sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_euler_yxz returns a vector containing the Euler angles in the format
|
|
|
|
// (ax,ay,az), where ax is the angle of rotation around x axis,
|
|
|
|
// and similar for other axes.
|
|
|
|
// This implementation uses YXZ convention (Z is the first rotation).
|
|
|
|
Vector3 Quat::get_euler_yxz() const {
|
2018-05-12 02:14:39 +02:00
|
|
|
#ifdef MATH_CHECKS
|
2020-01-24 13:51:25 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized.");
|
2018-05-12 02:14:39 +02:00
|
|
|
#endif
|
2017-08-09 04:55:52 +02:00
|
|
|
Basis m(*this);
|
|
|
|
return m.get_euler_yxz();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
void Quat::operator*=(const Quat &p_q) {
|
|
|
|
set(w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y,
|
|
|
|
w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z,
|
|
|
|
w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x,
|
|
|
|
w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
Quat Quat::operator*(const Quat &p_q) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
Quat r = *this;
|
2021-04-22 19:59:16 +02:00
|
|
|
r *= p_q;
|
2014-02-10 02:10:30 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-10-14 22:33:45 +02:00
|
|
|
bool Quat::is_equal_approx(const Quat &p_quat) const {
|
|
|
|
return Math::is_equal_approx(x, p_quat.x) && Math::is_equal_approx(y, p_quat.y) && Math::is_equal_approx(z, p_quat.z) && Math::is_equal_approx(w, p_quat.w);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
real_t Quat::length() const {
|
|
|
|
return Math::sqrt(length_squared());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Quat::normalize() {
|
|
|
|
*this /= length();
|
|
|
|
}
|
|
|
|
|
|
|
|
Quat Quat::normalized() const {
|
|
|
|
return *this / length();
|
2016-03-09 00:00:52 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-04-06 00:47:13 +02:00
|
|
|
bool Quat::is_normalized() const {
|
2021-05-20 12:26:45 +02:00
|
|
|
return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon
|
2017-04-06 00:47:13 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Quat Quat::inverse() const {
|
2018-05-12 02:14:39 +02:00
|
|
|
#ifdef MATH_CHECKS
|
2020-01-24 13:51:25 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The quaternion must be normalized.");
|
2018-05-12 02:14:39 +02:00
|
|
|
#endif
|
2017-03-05 16:44:50 +01:00
|
|
|
return Quat(-x, -y, -z, w);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
Quat Quat::slerp(const Quat &p_to, real_t p_weight) const {
|
2018-05-12 02:14:39 +02:00
|
|
|
#ifdef MATH_CHECKS
|
2020-01-24 13:51:25 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized.");
|
2021-04-22 19:59:16 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quat(), "The end quaternion must be normalized.");
|
2018-05-12 02:14:39 +02:00
|
|
|
#endif
|
2017-03-05 16:44:50 +01:00
|
|
|
Quat to1;
|
|
|
|
real_t omega, cosom, sinom, scale0, scale1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// calc cosine
|
2021-04-22 19:59:16 +02:00
|
|
|
cosom = dot(p_to);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// adjust signs (if necessary)
|
2022-02-24 09:41:35 +01:00
|
|
|
if (cosom < 0) {
|
2016-10-18 22:50:21 +02:00
|
|
|
cosom = -cosom;
|
2021-04-22 19:59:16 +02:00
|
|
|
to1.x = -p_to.x;
|
|
|
|
to1.y = -p_to.y;
|
|
|
|
to1.z = -p_to.z;
|
|
|
|
to1.w = -p_to.w;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2021-04-22 19:59:16 +02:00
|
|
|
to1.x = p_to.x;
|
|
|
|
to1.y = p_to.y;
|
|
|
|
to1.z = p_to.z;
|
|
|
|
to1.w = p_to.w;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate coefficients
|
|
|
|
|
2022-02-24 09:41:35 +01:00
|
|
|
if ((1 - cosom) > (real_t)CMP_EPSILON) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// standard case (slerp)
|
|
|
|
omega = Math::acos(cosom);
|
|
|
|
sinom = Math::sin(omega);
|
2022-02-24 09:41:35 +01:00
|
|
|
scale0 = Math::sin((1 - p_weight) * omega) / sinom;
|
2021-04-22 19:59:16 +02:00
|
|
|
scale1 = Math::sin(p_weight * omega) / sinom;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
// "from" and "to" quaternions are very close
|
|
|
|
// ... so we can do a linear interpolation
|
2022-02-24 09:41:35 +01:00
|
|
|
scale0 = 1 - p_weight;
|
2021-04-22 19:59:16 +02:00
|
|
|
scale1 = p_weight;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
// calculate final values
|
|
|
|
return Quat(
|
2017-03-05 16:44:50 +01:00
|
|
|
scale0 * x + scale1 * to1.x,
|
|
|
|
scale0 * y + scale1 * to1.y,
|
|
|
|
scale0 * z + scale1 * to1.z,
|
|
|
|
scale0 * w + scale1 * to1.w);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
Quat Quat::slerpni(const Quat &p_to, real_t p_weight) const {
|
2018-05-27 05:14:05 +02:00
|
|
|
#ifdef MATH_CHECKS
|
2020-01-24 13:51:25 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized.");
|
2021-04-22 19:59:16 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quat(), "The end quaternion must be normalized.");
|
2018-05-27 05:14:05 +02:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
const Quat &from = *this;
|
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
real_t dot = from.dot(p_to);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-02-24 09:41:35 +01:00
|
|
|
if (Math::absf(dot) > 0.9999f) {
|
2021-05-04 14:28:27 +02:00
|
|
|
return from;
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t theta = Math::acos(dot),
|
2022-02-24 09:41:35 +01:00
|
|
|
sinT = 1 / Math::sin(theta),
|
2021-04-22 19:59:16 +02:00
|
|
|
newFactor = Math::sin(p_weight * theta) * sinT,
|
2022-02-24 09:41:35 +01:00
|
|
|
invFactor = Math::sin((1 - p_weight) * theta) * sinT;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-04-22 19:59:16 +02:00
|
|
|
return Quat(invFactor * from.x + newFactor * p_to.x,
|
|
|
|
invFactor * from.y + newFactor * p_to.y,
|
|
|
|
invFactor * from.z + newFactor * p_to.z,
|
|
|
|
invFactor * from.w + newFactor * p_to.w);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
Quat Quat::cubic_slerp(const Quat &p_b, const Quat &p_pre_a, const Quat &p_post_b, real_t p_weight) const {
|
2018-05-27 05:14:05 +02:00
|
|
|
#ifdef MATH_CHECKS
|
2020-01-24 13:51:25 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_normalized(), Quat(), "The start quaternion must be normalized.");
|
2021-04-22 19:59:16 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quat(), "The end quaternion must be normalized.");
|
2018-05-27 05:14:05 +02:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
//the only way to do slerp :|
|
2022-02-24 09:41:35 +01:00
|
|
|
real_t t2 = (1 - p_weight) * p_weight * 2;
|
2021-04-22 19:59:16 +02:00
|
|
|
Quat sp = this->slerp(p_b, p_weight);
|
|
|
|
Quat sq = p_pre_a.slerpni(p_post_b, p_weight);
|
2017-03-05 16:44:50 +01:00
|
|
|
return sp.slerpni(sq, t2);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Quat::operator String() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:31:21 +01:00
|
|
|
void Quat::set_axis_angle(const Vector3 &p_axis, real_t p_angle) {
|
2018-05-12 02:14:39 +02:00
|
|
|
#ifdef MATH_CHECKS
|
2024-02-18 11:31:21 +01:00
|
|
|
ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
|
2018-05-12 02:14:39 +02:00
|
|
|
#endif
|
2024-02-18 11:31:21 +01:00
|
|
|
real_t d = p_axis.length();
|
2021-05-05 12:44:11 +02:00
|
|
|
if (d == 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
set(0, 0, 0, 0);
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2024-02-18 11:31:21 +01:00
|
|
|
real_t sin_angle = Math::sin(p_angle * 0.5f);
|
|
|
|
real_t cos_angle = Math::cos(p_angle * 0.5f);
|
2016-10-18 22:50:21 +02:00
|
|
|
real_t s = sin_angle / d;
|
2024-02-18 11:31:21 +01:00
|
|
|
set(p_axis.x * s, p_axis.y * s, p_axis.z * s,
|
2017-03-05 16:44:50 +01:00
|
|
|
cos_angle);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|