- 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.

(cherry picked from commit 2331300989)
This commit is contained in:
Andrea Catania 2020-06-12 18:39:59 +02:00 committed by Rémi Verschelde
parent 438f1a8238
commit 74d10e766e
6 changed files with 595 additions and 17 deletions

View File

@ -427,12 +427,9 @@ Vector3 Basis::get_euler_xyz() const {
// -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
Vector3 euler; Vector3 euler;
#ifdef MATH_CHECKS
ERR_FAIL_COND_V(!is_rotation(), euler);
#endif
real_t sy = elements[0][2]; real_t sy = elements[0][2];
if (sy < 1.0) { if (sy < (1.0 - CMP_EPSILON)) {
if (sy > -1.0) { if (sy > -(1.0 - CMP_EPSILON)) {
// is this a pure Y rotation? // 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) { 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) // return the simplest form (human friendlier in editor and scripts)
@ -445,12 +442,12 @@ Vector3 Basis::get_euler_xyz() const {
euler.z = Math::atan2(-elements[0][1], elements[0][0]); euler.z = Math::atan2(-elements[0][1], elements[0][0]);
} }
} else { } 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.y = -Math_PI / 2.0;
euler.z = 0.0; euler.z = 0.0;
} }
} else { } 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.y = Math_PI / 2.0;
euler.z = 0.0; euler.z = 0.0;
} }
@ -481,16 +478,106 @@ void Basis::set_euler_xyz(const Vector3 &p_euler) {
*this = xmat * (ymat * zmat); *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, // 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 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. // as the x, y, and z components of a Vector3 respectively.
Vector3 Basis::get_euler_yxz() const { 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. // Euler angles in YXZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
// //
@ -502,8 +589,8 @@ Vector3 Basis::get_euler_yxz() const {
real_t m12 = elements[1][2]; real_t m12 = elements[1][2];
if (m12 < 1) { if (m12 < (1 - CMP_EPSILON)) {
if (m12 > -1) { if (m12 > -(1 - CMP_EPSILON)) {
// is this a pure X rotation? // 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) { 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) // return the simplest form (human friendlier in editor and scripts)
@ -517,12 +604,12 @@ Vector3 Basis::get_euler_yxz() const {
} }
} else { // m12 == -1 } else { // m12 == -1
euler.x = Math_PI * 0.5; 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; euler.z = 0;
} }
} else { // m12 == 1 } else { // m12 == 1
euler.x = -Math_PI * 0.5; 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; euler.z = 0;
} }
@ -553,6 +640,100 @@ void Basis::set_euler_yxz(const Vector3 &p_euler) {
*this = ymat * xmat * zmat; *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 { 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]); 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]);

View File

@ -90,9 +90,22 @@ public:
Vector3 get_euler_xyz() const; Vector3 get_euler_xyz() const;
void set_euler_xyz(const Vector3 &p_euler); 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; Vector3 get_euler_yxz() const;
void set_euler_yxz(const Vector3 &p_euler); 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; Quat get_quat() const;
void set_quat(const Quat &p_quat); void set_quat(const Quat &p_quat);

View File

@ -824,6 +824,18 @@ struct _VariantCall {
VCALL_PTR1R(Basis, scaled); VCALL_PTR1R(Basis, scaled);
VCALL_PTR0R(Basis, get_scale); VCALL_PTR0R(Basis, get_scale);
VCALL_PTR0R(Basis, get_euler); 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, tdotx);
VCALL_PTR1R(Basis, tdoty); VCALL_PTR1R(Basis, tdoty);
VCALL_PTR1R(Basis, tdotz); VCALL_PTR1R(Basis, tdotz);

325
main/tests/test_basis.cpp Normal file
View File

@ -0,0 +1,325 @@
/*************************************************************************/
/* test_fbx.cpp */
/*************************************************************************/
/* 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. */
/*************************************************************************/
#include "test_basis.h"
#include "core/math/random_number_generator.h"
#include "core/os/os.h"
#include "core/ustring.h"
namespace TestBasis {
enum RotOrder {
EulerXYZ,
EulerXZY,
EulerYZX,
EulerYXZ,
EulerZXY,
EulerZYX
};
Vector3 deg2rad(const Vector3 &p_rotation) {
return p_rotation / 180.0 * Math_PI;
}
Vector3 rad2deg(const Vector3 &p_rotation) {
return p_rotation / Math_PI * 180.0;
}
Basis EulerToBasis(RotOrder mode, const Vector3 &p_rotation) {
Basis ret;
switch (mode) {
case EulerXYZ:
ret.set_euler_xyz(p_rotation);
break;
case EulerXZY:
ret.set_euler_xzy(p_rotation);
break;
case EulerYZX:
ret.set_euler_yzx(p_rotation);
break;
case EulerYXZ:
ret.set_euler_yxz(p_rotation);
break;
case EulerZXY:
ret.set_euler_zxy(p_rotation);
break;
case EulerZYX:
ret.set_euler_zyx(p_rotation);
break;
default:
// If you land here, Please integrate all rotation orders.
CRASH_NOW_MSG("This is not unreachable.");
}
return ret;
}
Vector3 BasisToEuler(RotOrder mode, const Basis &p_rotation) {
switch (mode) {
case EulerXYZ:
return p_rotation.get_euler_xyz();
case EulerXZY:
return p_rotation.get_euler_xzy();
case EulerYZX:
return p_rotation.get_euler_yzx();
case EulerYXZ:
return p_rotation.get_euler_yxz();
case EulerZXY:
return p_rotation.get_euler_zxy();
case EulerZYX:
return p_rotation.get_euler_zyx();
default:
// If you land here, Please integrate all rotation orders.
CRASH_NOW_MSG("This is not unreachable.");
return Vector3();
}
}
String get_rot_order_name(RotOrder ro) {
switch (ro) {
case EulerXYZ:
return "XYZ";
case EulerXZY:
return "XZY";
case EulerYZX:
return "YZX";
case EulerYXZ:
return "YXZ";
case EulerZXY:
return "ZXY";
case EulerZYX:
return "ZYX";
default:
return "[Not supported]";
}
}
bool test_rotation(Vector3 deg_original_euler, RotOrder rot_order) {
// This test:
// 1. Converts the rotation vector from deg to rad.
// 2. Converts euler to basis.
// 3. Converts the above basis back into euler.
// 4. Converts the above euler into basis again.
// 5. Compares the basis obtained in step 2 with the basis of step 4
//
// The conversion "basis to euler", done in the step 3, may be different from
// the original euler, even if the final rotation are the same.
// This happens because there are more ways to represents the same rotation,
// both valid, using eulers.
// For this reason is necessary to convert that euler back to basis and finally
// compares it.
//
// In this way we can assert that both functions: basis to euler / euler to basis
// are correct.
bool pass = true;
// Euler to rotation
const Vector3 original_euler = deg2rad(deg_original_euler);
const Basis to_rotation = EulerToBasis(rot_order, original_euler);
// Euler from rotation
const Vector3 euler_from_rotation = BasisToEuler(rot_order, to_rotation);
const Basis rotation_from_computed_euler = EulerToBasis(rot_order, euler_from_rotation);
Basis res = to_rotation.inverse() * rotation_from_computed_euler;
if ((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() > 0.1) {
OS::get_singleton()->print("Fail due to X %ls\n", String(res.get_axis(0)).c_str());
pass = false;
}
if ((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() > 0.1) {
OS::get_singleton()->print("Fail due to Y %ls\n", String(res.get_axis(1)).c_str());
pass = false;
}
if ((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() > 0.1) {
OS::get_singleton()->print("Fail due to Z %ls\n", String(res.get_axis(2)).c_str());
pass = false;
}
if (pass) {
// Double check `to_rotation` decomposing with XYZ rotation order.
const Vector3 euler_xyz_from_rotation = to_rotation.get_euler_xyz();
Basis rotation_from_xyz_computed_euler;
rotation_from_xyz_computed_euler.set_euler_xyz(euler_xyz_from_rotation);
res = to_rotation.inverse() * rotation_from_xyz_computed_euler;
if ((res.get_axis(0) - Vector3(1.0, 0.0, 0.0)).length() > 0.1) {
OS::get_singleton()->print("Double check with XYZ rot order failed, due to X %ls\n", String(res.get_axis(0)).c_str());
pass = false;
}
if ((res.get_axis(1) - Vector3(0.0, 1.0, 0.0)).length() > 0.1) {
OS::get_singleton()->print("Double check with XYZ rot order failed, due to Y %ls\n", String(res.get_axis(1)).c_str());
pass = false;
}
if ((res.get_axis(2) - Vector3(0.0, 0.0, 1.0)).length() > 0.1) {
OS::get_singleton()->print("Double check with XYZ rot order failed, due to Z %ls\n", String(res.get_axis(2)).c_str());
pass = false;
}
}
if (pass == false) {
// Print phase only if not pass.
OS *os = OS::get_singleton();
os->print("Rotation order: %ls\n.", get_rot_order_name(rot_order).c_str());
os->print("Original Rotation: %ls\n", String(deg_original_euler).c_str());
os->print("Quaternion to rotation order: %ls\n", String(rad2deg(euler_from_rotation)).c_str());
}
return pass;
}
void test_euler_conversion() {
Vector<RotOrder> rotorder_to_test;
rotorder_to_test.push_back(EulerXYZ);
rotorder_to_test.push_back(EulerXZY);
rotorder_to_test.push_back(EulerYZX);
rotorder_to_test.push_back(EulerYXZ);
rotorder_to_test.push_back(EulerZXY);
rotorder_to_test.push_back(EulerZYX);
Vector<Vector3> vectors_to_test;
// Test the special cases.
vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0));
vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5));
vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5));
vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0));
vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0));
vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0));
vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0));
vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0));
vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0));
vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0));
vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0));
vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0));
vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0));
vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0));
vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0));
vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0));
vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0));
vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0));
vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0));
vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0));
vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0));
vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0));
vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0));
vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0));
vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0));
vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0));
vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0));
vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0));
vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0));
vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0));
vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0));
vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0));
vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0));
vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0));
vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0));
vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0));
vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0));
vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0));
vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0));
vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0));
vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0));
vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0));
vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0));
vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0));
vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0));
vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0));
vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0));
vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0));
vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0));
vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0));
vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0));
vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0));
// Add 1000 random vectors with weirds numbers.
RandomNumberGenerator rng;
for (int _ = 0; _ < 1000; _ += 1) {
vectors_to_test.push_back(Vector3(
rng.randf_range(-1800, 1800),
rng.randf_range(-1800, 1800),
rng.randf_range(-1800, 1800)));
}
bool success = true;
for (int h = 0; h < rotorder_to_test.size(); h += 1) {
int passed = 0;
int failed = 0;
for (int i = 0; i < vectors_to_test.size(); i += 1) {
if (test_rotation(vectors_to_test[i], rotorder_to_test[h])) {
//OS::get_singleton()->print("Success. \n\n");
passed += 1;
} else {
OS::get_singleton()->print("FAILED FAILED FAILED. \n\n");
OS::get_singleton()->print("------------>\n");
OS::get_singleton()->print("------------>\n");
failed += 1;
success = false;
}
}
if (failed == 0) {
OS::get_singleton()->print("%i passed tests for rotation order: %ls.\n", passed, get_rot_order_name(rotorder_to_test[h]).c_str());
} else {
OS::get_singleton()->print("%i FAILED tests for rotation order: %ls.\n", failed, get_rot_order_name(rotorder_to_test[h]).c_str());
}
}
if (success) {
OS::get_singleton()->print("Euler conversion checks passed.\n");
} else {
OS::get_singleton()->print("Euler conversion checks FAILED.\n");
}
}
MainLoop *test() {
OS::get_singleton()->print("Start euler conversion checks.\n");
test_euler_conversion();
return NULL;
}
} // namespace TestBasis

40
main/tests/test_basis.h Normal file
View File

@ -0,0 +1,40 @@
/*************************************************************************/
/* 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

View File

@ -35,6 +35,7 @@
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
#include "test_astar.h" #include "test_astar.h"
#include "test_basis.h"
#include "test_gdscript.h" #include "test_gdscript.h"
#include "test_gui.h" #include "test_gui.h"
#include "test_math.h" #include "test_math.h"
@ -51,6 +52,7 @@ const char **tests_get_names() {
static const char *test_names[] = { static const char *test_names[] = {
"string", "string",
"math", "math",
"basis",
"physics", "physics",
"physics_2d", "physics_2d",
"render", "render",
@ -81,6 +83,11 @@ MainLoop *test_main(String p_test, const List<String> &p_args) {
return TestMath::test(); return TestMath::test();
} }
if (p_test == "basis") {
return TestBasis::test();
}
if (p_test == "physics") { if (p_test == "physics") {
return TestPhysics::test(); return TestPhysics::test();