2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* quat.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "quat.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
2019-02-09 05:24:02 +00:00
|
|
|
#include "core/math/basis.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/print_string.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-09 02:55:52 +00: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) {
|
2016-10-18 20:50:21 +00:00
|
|
|
real_t half_a1 = p_euler.x * 0.5;
|
|
|
|
real_t half_a2 = p_euler.y * 0.5;
|
|
|
|
real_t half_a3 = p_euler.z * 0.5;
|
|
|
|
|
|
|
|
// 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 15:44:50 +00: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 20:50:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 02:55:52 +00: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 03:52:51 +00:00
|
|
|
Basis m(*this);
|
2017-08-09 02:55:52 +00: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) {
|
|
|
|
real_t half_a1 = p_euler.y * 0.5;
|
|
|
|
real_t half_a2 = p_euler.x * 0.5;
|
|
|
|
real_t half_a3 = p_euler.z * 0.5;
|
|
|
|
|
|
|
|
// 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 06:48:03 +00:00
|
|
|
-sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3,
|
2017-08-09 02:55:52 +00: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 00:14:39 +00:00
|
|
|
#ifdef MATH_CHECKS
|
2018-10-06 20:20:41 +00:00
|
|
|
ERR_FAIL_COND_V(!is_normalized(), Vector3(0, 0, 0));
|
2018-05-12 00:14:39 +00:00
|
|
|
#endif
|
2017-08-09 02:55:52 +00:00
|
|
|
Basis m(*this);
|
|
|
|
return m.get_euler_yxz();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Quat::operator*=(const Quat &q) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set(w * q.x + x * q.w + y * q.z - z * q.y,
|
|
|
|
w * q.y + y * q.w + z * q.x - x * q.z,
|
|
|
|
w * q.z + z * q.w + x * q.y - y * q.x,
|
|
|
|
w * q.w - x * q.x - y * q.y - z * q.z);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Quat Quat::operator*(const Quat &q) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Quat r = *this;
|
|
|
|
r *= q;
|
2014-02-10 01:10:30 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t Quat::length() const {
|
|
|
|
|
|
|
|
return Math::sqrt(length_squared());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Quat::normalize() {
|
|
|
|
*this /= length();
|
|
|
|
}
|
|
|
|
|
|
|
|
Quat Quat::normalized() const {
|
|
|
|
return *this / length();
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-05 22:47:13 +00:00
|
|
|
bool Quat::is_normalized() const {
|
2019-02-26 00:46:24 +00:00
|
|
|
return Math::is_equal_approx(length_squared(), 1.0, UNIT_EPSILON); //use less epsilon
|
2017-04-05 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Quat Quat::inverse() const {
|
2018-05-12 00:14:39 +00:00
|
|
|
#ifdef MATH_CHECKS
|
2018-10-06 20:20:41 +00:00
|
|
|
ERR_FAIL_COND_V(!is_normalized(), Quat());
|
2018-05-12 00:14:39 +00:00
|
|
|
#endif
|
2017-03-05 15:44:50 +00:00
|
|
|
return Quat(-x, -y, -z, w);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Quat Quat::slerp(const Quat &q, const real_t &t) const {
|
2018-05-12 00:14:39 +00:00
|
|
|
#ifdef MATH_CHECKS
|
2018-10-06 20:20:41 +00:00
|
|
|
ERR_FAIL_COND_V(!is_normalized(), Quat());
|
|
|
|
ERR_FAIL_COND_V(!q.is_normalized(), Quat());
|
2018-05-12 00:14:39 +00:00
|
|
|
#endif
|
2017-03-05 15:44:50 +00:00
|
|
|
Quat to1;
|
|
|
|
real_t omega, cosom, sinom, scale0, scale1;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
// calc cosine
|
2016-10-18 20:50:21 +00:00
|
|
|
cosom = dot(q);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
// adjust signs (if necessary)
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cosom < 0.0) {
|
2016-10-18 20:50:21 +00:00
|
|
|
cosom = -cosom;
|
2017-03-05 15:44:50 +00:00
|
|
|
to1.x = -q.x;
|
|
|
|
to1.y = -q.y;
|
|
|
|
to1.z = -q.z;
|
|
|
|
to1.w = -q.w;
|
|
|
|
} else {
|
2016-10-18 20:50:21 +00:00
|
|
|
to1.x = q.x;
|
|
|
|
to1.y = q.y;
|
|
|
|
to1.z = q.z;
|
|
|
|
to1.w = q.w;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate coefficients
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((1.0 - cosom) > CMP_EPSILON) {
|
2014-02-10 01:10:30 +00:00
|
|
|
// standard case (slerp)
|
|
|
|
omega = Math::acos(cosom);
|
|
|
|
sinom = Math::sin(omega);
|
|
|
|
scale0 = Math::sin((1.0 - t) * omega) / sinom;
|
|
|
|
scale1 = Math::sin(t * omega) / sinom;
|
|
|
|
} else {
|
|
|
|
// "from" and "to" quaternions are very close
|
|
|
|
// ... so we can do a linear interpolation
|
|
|
|
scale0 = 1.0 - t;
|
|
|
|
scale1 = t;
|
|
|
|
}
|
|
|
|
// calculate final values
|
|
|
|
return Quat(
|
2017-03-05 15:44:50 +00:00
|
|
|
scale0 * x + scale1 * to1.x,
|
|
|
|
scale0 * y + scale1 * to1.y,
|
|
|
|
scale0 * z + scale1 * to1.z,
|
|
|
|
scale0 * w + scale1 * to1.w);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Quat Quat::slerpni(const Quat &q, const real_t &t) const {
|
2018-05-27 03:14:05 +00:00
|
|
|
#ifdef MATH_CHECKS
|
2018-10-06 20:20:41 +00:00
|
|
|
ERR_FAIL_COND_V(!is_normalized(), Quat());
|
|
|
|
ERR_FAIL_COND_V(!q.is_normalized(), Quat());
|
2018-05-27 03:14:05 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
const Quat &from = *this;
|
|
|
|
|
2017-01-14 20:35:39 +00:00
|
|
|
real_t dot = from.dot(q);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-14 20:35:39 +00:00
|
|
|
if (Math::absf(dot) > 0.9999) return from;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t theta = Math::acos(dot),
|
|
|
|
sinT = 1.0 / Math::sin(theta),
|
|
|
|
newFactor = Math::sin(t * theta) * sinT,
|
|
|
|
invFactor = Math::sin((1.0 - t) * theta) * sinT;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-10-18 20:50:21 +00:00
|
|
|
return Quat(invFactor * from.x + newFactor * q.x,
|
2017-03-05 15:44:50 +00:00
|
|
|
invFactor * from.y + newFactor * q.y,
|
|
|
|
invFactor * from.z + newFactor * q.z,
|
|
|
|
invFactor * from.w + newFactor * q.w);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Quat Quat::cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const {
|
2018-05-27 03:14:05 +00:00
|
|
|
#ifdef MATH_CHECKS
|
2018-10-06 20:20:41 +00:00
|
|
|
ERR_FAIL_COND_V(!is_normalized(), Quat());
|
|
|
|
ERR_FAIL_COND_V(!q.is_normalized(), Quat());
|
2018-05-27 03:14:05 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
//the only way to do slerp :|
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t t2 = (1.0 - t) * t * 2;
|
|
|
|
Quat sp = this->slerp(q, t);
|
|
|
|
Quat sq = prep.slerpni(postq, t);
|
|
|
|
return sp.slerpni(sq, t2);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Quat::operator String() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return String::num(x) + ", " + String::num(y) + ", " + String::num(z) + ", " + String::num(w);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 00:14:39 +00:00
|
|
|
void Quat::set_axis_angle(const Vector3 &axis, const real_t &angle) {
|
|
|
|
#ifdef MATH_CHECKS
|
2018-10-06 20:20:41 +00:00
|
|
|
ERR_FAIL_COND(!axis.is_normalized());
|
2018-05-12 00:14:39 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
real_t d = axis.length();
|
2017-03-05 15:44:50 +00:00
|
|
|
if (d == 0)
|
|
|
|
set(0, 0, 0, 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
else {
|
2016-10-18 20:50:21 +00:00
|
|
|
real_t sin_angle = Math::sin(angle * 0.5);
|
|
|
|
real_t cos_angle = Math::cos(angle * 0.5);
|
|
|
|
real_t s = sin_angle / d;
|
2016-03-08 23:00:52 +00:00
|
|
|
set(axis.x * s, axis.y * s, axis.z * s,
|
2017-03-05 15:44:50 +00:00
|
|
|
cos_angle);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|