Commit 23313009 by Andrea Catania

- Added more euler rotation orders support.

- Fixed floating point issue on the old one. - Fixed the equation on the get_euler_yxz function. - Added unit tests. This work has been kindly sponsored by IMVU.
parent 84abf5a9
......@@ -428,12 +428,9 @@ Vector3 Basis::get_euler_xyz() const {
// -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
Vector3 euler;
#ifdef MATH_CHECKS
ERR_FAIL_COND_V(!is_rotation(), euler);
#endif
real_t sy = elements[0][2];
if (sy < 1.0) {
if (sy > -1.0) {
if (sy < (1.0 - CMP_EPSILON)) {
if (sy > -(1.0 - CMP_EPSILON)) {
// is this a pure Y rotation?
if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
// return the simplest form (human friendlier in editor and scripts)
......@@ -446,12 +443,12 @@ Vector3 Basis::get_euler_xyz() const {
euler.z = Math::atan2(-elements[0][1], elements[0][0]);
}
} else {
euler.x = -Math::atan2(elements[0][1], elements[1][1]);
euler.x = Math::atan2(elements[2][1], elements[1][1]);
euler.y = -Math_PI / 2.0;
euler.z = 0.0;
}
} else {
euler.x = Math::atan2(elements[0][1], elements[1][1]);
euler.x = Math::atan2(elements[2][1], elements[1][1]);
euler.y = Math_PI / 2.0;
euler.z = 0.0;
}
......@@ -481,15 +478,106 @@ void Basis::set_euler_xyz(const Vector3 &p_euler) {
*this = xmat * (ymat * zmat);
}
Vector3 Basis::get_euler_xzy() const {
// Euler angles in XZY convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
// rot = cz*cy -sz cz*sy
// sx*sy+cx*cy*sz cx*cz cx*sz*sy-cy*sx
// cy*sx*sz cz*sx cx*cy+sx*sz*sy
Vector3 euler;
real_t sz = elements[0][1];
if (sz < (1.0 - CMP_EPSILON)) {
if (sz > -(1.0 - CMP_EPSILON)) {
euler.x = Math::atan2(elements[2][1], elements[1][1]);
euler.y = Math::atan2(elements[0][2], elements[0][0]);
euler.z = Math::asin(-sz);
} else {
// It's -1
euler.x = -Math::atan2(elements[1][2], elements[2][2]);
euler.y = 0.0;
euler.z = Math_PI / 2.0;
}
} else {
// It's 1
euler.x = -Math::atan2(elements[1][2], elements[2][2]);
euler.y = 0.0;
euler.z = -Math_PI / 2.0;
}
return euler;
}
void Basis::set_euler_xzy(const Vector3 &p_euler) {
real_t c, s;
c = Math::cos(p_euler.x);
s = Math::sin(p_euler.x);
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
c = Math::cos(p_euler.y);
s = Math::sin(p_euler.y);
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
c = Math::cos(p_euler.z);
s = Math::sin(p_euler.z);
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
*this = xmat * zmat * ymat;
}
Vector3 Basis::get_euler_yzx() const {
// Euler angles in YZX convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
// rot = cy*cz sy*sx-cy*cx*sz cx*sy+cy*sz*sx
// sz cz*cx -cz*sx
// -cz*sy cy*sx+cx*sy*sz cy*cx-sy*sz*sx
Vector3 euler;
real_t sz = elements[1][0];
if (sz < (1.0 - CMP_EPSILON)) {
if (sz > -(1.0 - CMP_EPSILON)) {
euler.x = Math::atan2(-elements[1][2], elements[1][1]);
euler.y = Math::atan2(-elements[2][0], elements[0][0]);
euler.z = Math::asin(sz);
} else {
// It's -1
euler.x = Math::atan2(elements[2][1], elements[2][2]);
euler.y = 0.0;
euler.z = -Math_PI / 2.0;
}
} else {
// It's 1
euler.x = Math::atan2(elements[2][1], elements[2][2]);
euler.y = 0.0;
euler.z = Math_PI / 2.0;
}
return euler;
}
void Basis::set_euler_yzx(const Vector3 &p_euler) {
real_t c, s;
c = Math::cos(p_euler.x);
s = Math::sin(p_euler.x);
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
c = Math::cos(p_euler.y);
s = Math::sin(p_euler.y);
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
c = Math::cos(p_euler.z);
s = Math::sin(p_euler.z);
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
*this = ymat * zmat * xmat;
}
// get_euler_yxz returns a vector containing the Euler angles in the YXZ convention,
// as in first-Z, then-X, last-Y. The angles for X, Y, and Z rotations are returned
// as the x, y, and z components of a Vector3 respectively.
Vector3 Basis::get_euler_yxz() const {
/* checking this is a bad idea, because obtaining from scaled transform is a valid use case
#ifdef MATH_CHECKS
ERR_FAIL_COND(!is_rotation());
#endif
*/
// Euler angles in YXZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
......@@ -501,8 +589,8 @@ Vector3 Basis::get_euler_yxz() const {
real_t m12 = elements[1][2];
if (m12 < 1) {
if (m12 > -1) {
if (m12 < (1 - CMP_EPSILON)) {
if (m12 > -(1 - CMP_EPSILON)) {
// is this a pure X rotation?
if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) {
// return the simplest form (human friendlier in editor and scripts)
......@@ -516,12 +604,12 @@ Vector3 Basis::get_euler_yxz() const {
}
} else { // m12 == -1
euler.x = Math_PI * 0.5;
euler.y = -atan2(-elements[0][1], elements[0][0]);
euler.y = atan2(elements[0][1], elements[0][0]);
euler.z = 0;
}
} else { // m12 == 1
euler.x = -Math_PI * 0.5;
euler.y = -atan2(-elements[0][1], elements[0][0]);
euler.y = -atan2(elements[0][1], elements[0][0]);
euler.z = 0;
}
......@@ -551,6 +639,100 @@ void Basis::set_euler_yxz(const Vector3 &p_euler) {
*this = ymat * xmat * zmat;
}
Vector3 Basis::get_euler_zxy() const {
// Euler angles in ZXY convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
// rot = cz*cy-sz*sx*sy -cx*sz cz*sy+cy*sz*sx
// cy*sz+cz*sx*sy cz*cx sz*sy-cz*cy*sx
// -cx*sy sx cx*cy
Vector3 euler;
real_t sx = elements[2][1];
if (sx < (1.0 - CMP_EPSILON)) {
if (sx > -(1.0 - CMP_EPSILON)) {
euler.x = Math::asin(sx);
euler.y = Math::atan2(-elements[2][0], elements[2][2]);
euler.z = Math::atan2(-elements[0][1], elements[1][1]);
} else {
// It's -1
euler.x = -Math_PI / 2.0;
euler.y = Math::atan2(elements[0][2], elements[0][0]);
euler.z = 0;
}
} else {
// It's 1
euler.x = Math_PI / 2.0;
euler.y = Math::atan2(elements[0][2], elements[0][0]);
euler.z = 0;
}
return euler;
}
void Basis::set_euler_zxy(const Vector3 &p_euler) {
real_t c, s;
c = Math::cos(p_euler.x);
s = Math::sin(p_euler.x);
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
c = Math::cos(p_euler.y);
s = Math::sin(p_euler.y);
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
c = Math::cos(p_euler.z);
s = Math::sin(p_euler.z);
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
*this = zmat * xmat * ymat;
}
Vector3 Basis::get_euler_zyx() const {
// Euler angles in ZYX convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
// rot = cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*cy
// cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx
// -sy cy*sx cy*cx
Vector3 euler;
real_t sy = elements[2][0];
if (sy < (1.0 - CMP_EPSILON)) {
if (sy > -(1.0 - CMP_EPSILON)) {
euler.x = Math::atan2(elements[2][1], elements[2][2]);
euler.y = Math::asin(-sy);
euler.z = Math::atan2(elements[1][0], elements[0][0]);
} else {
// It's -1
euler.x = 0;
euler.y = Math_PI / 2.0;
euler.z = -Math::atan2(elements[0][1], elements[1][1]);
}
} else {
// It's 1
euler.x = 0;
euler.y = -Math_PI / 2.0;
euler.z = -Math::atan2(elements[0][1], elements[1][1]);
}
return euler;
}
void Basis::set_euler_zyx(const Vector3 &p_euler) {
real_t c, s;
c = Math::cos(p_euler.x);
s = Math::sin(p_euler.x);
Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
c = Math::cos(p_euler.y);
s = Math::sin(p_euler.y);
Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
c = Math::cos(p_euler.z);
s = Math::sin(p_euler.z);
Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
*this = zmat * ymat * xmat;
}
bool Basis::is_equal_approx(const Basis &p_basis) const {
return elements[0].is_equal_approx(p_basis.elements[0]) && elements[1].is_equal_approx(p_basis.elements[1]) && elements[2].is_equal_approx(p_basis.elements[2]);
}
......
......@@ -88,9 +88,22 @@ public:
Vector3 get_euler_xyz() const;
void set_euler_xyz(const Vector3 &p_euler);
Vector3 get_euler_xzy() const;
void set_euler_xzy(const Vector3 &p_euler);
Vector3 get_euler_yzx() const;
void set_euler_yzx(const Vector3 &p_euler);
Vector3 get_euler_yxz() const;
void set_euler_yxz(const Vector3 &p_euler);
Vector3 get_euler_zxy() const;
void set_euler_zxy(const Vector3 &p_euler);
Vector3 get_euler_zyx() const;
void set_euler_zyx(const Vector3 &p_euler);
Quat get_quat() const;
void set_quat(const Quat &p_quat);
......
......@@ -923,6 +923,18 @@ struct _VariantCall {
VCALL_PTR1R(Basis, scaled);
VCALL_PTR0R(Basis, get_scale);
VCALL_PTR0R(Basis, get_euler);
VCALL_PTR0R(Basis, get_euler_xyz);
VCALL_PTR1(Basis, set_euler_xyz);
VCALL_PTR0R(Basis, get_euler_xzy);
VCALL_PTR1(Basis, set_euler_xzy);
VCALL_PTR0R(Basis, get_euler_yzx);
VCALL_PTR1(Basis, set_euler_yzx);
VCALL_PTR0R(Basis, get_euler_yxz);
VCALL_PTR1(Basis, set_euler_yxz);
VCALL_PTR0R(Basis, get_euler_zxy);
VCALL_PTR1(Basis, set_euler_zxy);
VCALL_PTR0R(Basis, get_euler_zyx);
VCALL_PTR1(Basis, set_euler_zyx);
VCALL_PTR1R(Basis, tdotx);
VCALL_PTR1R(Basis, tdoty);
VCALL_PTR1R(Basis, tdotz);
......
/*************************************************************************/
/* test_fbx.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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. */
/*************************************************************************/
#ifndef TEST_BASIS_H
#define TEST_BASIS_H
#include "core/os/main_loop.h"
namespace TestBasis {
MainLoop *test();
}
#endif
......@@ -35,6 +35,7 @@
#ifdef DEBUG_ENABLED
#include "test_astar.h"
#include "test_basis.h"
#include "test_class_db.h"
#include "test_gdscript.h"
#include "test_gui.h"
......@@ -51,6 +52,7 @@ const char **tests_get_names() {
static const char *test_names[] = {
"string",
"math",
"basis",
"physics_2d",
"physics_3d",
"render",
......@@ -79,6 +81,10 @@ MainLoop *test_main(String p_test, const List<String> &p_args) {
return TestMath::test();
}
if (p_test == "basis") {
return TestBasis::test();
}
if (p_test == "physics_2d") {
return TestPhysics2D::test();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment