1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Test & document conventions used by matrix4::setRotation* (#15542)

Also includes a minor `matrix4::transformVect` refactor to make testing easier.
This commit is contained in:
Lars Müller 2024-12-14 17:02:16 +01:00 committed by GitHub
parent f7a695c212
commit 23e502fa0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 94 additions and 23 deletions

View file

@ -53,6 +53,7 @@ set (UNITTEST_CLIENT_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_eventmanager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_gameui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_irr_gltf_mesh_loader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_irr_matrix4.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mesh_compare.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp
PARENT_SCOPE)

View file

@ -0,0 +1,69 @@
// Luanti
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "catch.h"
#include "irrMath.h"
#include "matrix4.h"
#include "irr_v3d.h"
using matrix4 = core::matrix4;
static bool matrix_equals(const matrix4 &a, const matrix4 &b) {
return a.equals(b, 0.00001f);
}
constexpr v3f x{1, 0, 0};
constexpr v3f y{0, 1, 0};
constexpr v3f z{0, 0, 1};
TEST_CASE("matrix4") {
SECTION("setRotationRadians") {
SECTION("rotation order is ZYX (matrix notation)") {
v3f rot{1, 2, 3};
matrix4 X, Y, Z, ZYX;
X.setRotationRadians({rot.X, 0, 0});
Y.setRotationRadians({0, rot.Y, 0});
Z.setRotationRadians({0, 0, rot.Z});
ZYX.setRotationRadians(rot);
CHECK(!matrix_equals(X * Y * Z, ZYX));
CHECK(!matrix_equals(X * Z * Y, ZYX));
CHECK(!matrix_equals(Y * X * Z, ZYX));
CHECK(!matrix_equals(Y * Z * X, ZYX));
CHECK(!matrix_equals(Z * X * Y, ZYX));
CHECK(matrix_equals(Z * Y * X, ZYX));
}
const f32 quarter_turn = core::PI / 2;
// See https://en.wikipedia.org/wiki/Right-hand_rule#/media/File:Cartesian_coordinate_system_handedness.svg
// for a visualization of what handedness means for rotations
SECTION("rotation is right-handed") {
SECTION("rotation around the X-axis is Z-up, counter-clockwise") {
matrix4 X;
X.setRotationRadians({quarter_turn, 0, 0});
CHECK(X.transformVect(x).equals(x));
CHECK(X.transformVect(y).equals(z));
CHECK(X.transformVect(z).equals(-y));
}
SECTION("rotation around the Y-axis is Z-up, clockwise") {
matrix4 Y;
Y.setRotationRadians({0, quarter_turn, 0});
CHECK(Y.transformVect(y).equals(y));
CHECK(Y.transformVect(x).equals(-z));
CHECK(Y.transformVect(z).equals(x));
}
SECTION("rotation around the Z-axis is Y-up, counter-clockwise") {
matrix4 Z;
Z.setRotationRadians({0, 0, quarter_turn});
CHECK(Z.transformVect(z).equals(z));
CHECK(Z.transformVect(x).equals(y));
CHECK(Z.transformVect(y).equals(-x));
}
}
}
}