From 85a9e63996d2ba7bedbe753498c9e24941ab0acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 23 Jul 2026 17:30:47 +0200 Subject: [PATCH] Math: slightly modernize certain Matrix[34] test code. --- src/Magnum/Math/Test/Matrix3Test.cpp | 23 +++++---- src/Magnum/Math/Test/Matrix4Test.cpp | 72 ++++++++++++++-------------- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/Magnum/Math/Test/Matrix3Test.cpp b/src/Magnum/Math/Test/Matrix3Test.cpp index 194634ad6..c74a385da 100644 --- a/src/Magnum/Math/Test/Matrix3Test.cpp +++ b/src/Magnum/Math/Test/Matrix3Test.cpp @@ -511,26 +511,25 @@ void Matrix3Test::shearingY() { } void Matrix3Test::projection() { - Matrix3 expected({2.0f/5.0f, 0.0f, 0.0f}, - { 0.0f, 2.0f/4.0f, 0.0f}, - { 0.0f, 0.0f, 1.0f}); - - CORRADE_COMPARE(Matrix3::projection({5.0f, 4.0f}), expected); + CORRADE_COMPARE(Matrix3::projection({5.0f, 4.0f}), (Matrix3{ + {2.0f/5.0f, 0.0f, 0.0f}, + { 0.0f, 2.0f/4.0f, 0.0f}, + { 0.0f, 0.0f, 1.0f}})); } void Matrix3Test::projectionOffCenter() { - Matrix3 expected({0.4f, 0.0f, 0.0f}, - {0.0f, 0.5f, 0.0f}, - {0.4f, 0.25f, 1.0f}); /* Shifted by (-1, -0.5) compared to the projection() test */ - Matrix3 actual = Matrix3::projection({-3.5f, -2.5f}, {1.5f, 1.5f}); - CORRADE_COMPARE(actual, expected); + Matrix3 projection = Matrix3::projection({-3.5f, -2.5f}, {1.5f, 1.5f}); + CORRADE_COMPARE(projection, (Matrix3{ + {0.4f, 0.0f, 0.0f}, + {0.0f, 0.5f, 0.0f}, + {0.4f, 0.25f, 1.0f}})); /* NDC is left-handed, so point on the near plane top right corner should be (1, 1), and a point on the far plane bottom left corner should be (-1, -1) */ - CORRADE_COMPARE(actual.transformPoint({1.5f, 1.5f}), (Vector2{1.0f, 1.0f})); - CORRADE_COMPARE(actual.transformPoint({-3.5f, -2.5f}), (Vector2{-1.0f, -1.0f})); + CORRADE_COMPARE(projection.transformPoint({1.5f, 1.5f}), (Vector2{1.0f, 1.0f})); + CORRADE_COMPARE(projection.transformPoint({-3.5f, -2.5f}), (Vector2{-1.0f, -1.0f})); } void Matrix3Test::fromParts() { diff --git a/src/Magnum/Math/Test/Matrix4Test.cpp b/src/Magnum/Math/Test/Matrix4Test.cpp index bdb29a8e3..7187e6b5e 100644 --- a/src/Magnum/Math/Test/Matrix4Test.cpp +++ b/src/Magnum/Math/Test/Matrix4Test.cpp @@ -641,26 +641,26 @@ void Matrix4Test::shearingYZ() { } void Matrix4Test::orthographicProjection() { - Matrix4 expected({0.4f, 0.0f, 0.0f, 0.0f}, - {0.0f, 0.5f, 0.0f, 0.0f}, - {0.0f, 0.0f, -0.25f, 0.0f}, - {0.0f, 0.0f, -1.25f, 1.0f}); Matrix4 actual = Matrix4::orthographicProjection({5.0f, 4.0f}, 1.0f, 9.0f); - CORRADE_COMPARE(actual, expected); + CORRADE_COMPARE(actual, (Matrix4{ + {0.4f, 0.0f, 0.0f, 0.0f}, + {0.0f, 0.5f, 0.0f, 0.0f}, + {0.0f, 0.0f, -0.25f, 0.0f}, + {0.0f, 0.0f, -1.25f, 1.0f}})); /* NDC is left-handed, so point on near plane should be -1, far +1 */ - CORRADE_COMPARE(actual.transformPoint({0.0f, 0.0f, -1.0f}), Vector3(0.0f, 0.0f, -1.0f)); - CORRADE_COMPARE(actual.transformPoint({0.0f, 0.0f, -9.0f}), Vector3(0.0f, 0.0f, +1.0f)); + CORRADE_COMPARE(actual.transformPoint({0.0f, 0.0f, -1.0f}), (Vector3{0.0f, 0.0f, -1.0f})); + CORRADE_COMPARE(actual.transformPoint({0.0f, 0.0f, -9.0f}), (Vector3{0.0f, 0.0f, +1.0f})); } void Matrix4Test::orthographicProjectionOffCenter() { - Matrix4 expected({0.4f, 0.0f, 0.0f, 0.0f}, - {0.0f, 0.5f, 0.0f, 0.0f}, - {0.0f, 0.0f, -0.25f, 0.0f}, - {0.4f, 0.25f, -1.25f, 1.0f}); /* Shifted by (-1, -0.5) compared to the orthographicProjection() test */ Matrix4 actual = Matrix4::orthographicProjection({-3.5f, -2.5f}, {1.5f, 1.5f}, 1.0f, 9.0f); - CORRADE_COMPARE(actual, expected); + CORRADE_COMPARE(actual, (Matrix4{ + {0.4f, 0.0f, 0.0f, 0.0f}, + {0.0f, 0.5f, 0.0f, 0.0f}, + {0.0f, 0.0f, -0.25f, 0.0f}, + {0.4f, 0.25f, -1.25f, 1.0f}})); /* NDC is left-handed, so point on the near plane top right corner should be (1, 1, -1), and a point on the far plane bottom left corner should be @@ -670,10 +670,10 @@ void Matrix4Test::orthographicProjectionOffCenter() { } void Matrix4Test::perspectiveProjection() { - Matrix4 expected({4.0f, 0.0f, 0.0f, 0.0f}, + Matrix4 expected{{4.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 7.111111f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.9411764f, -1.0f}, - {0.0f, 0.0f, -94.1176452f, 0.0f}); + {0.0f, 0.0f, -94.1176452f, 0.0f}}; Matrix4 actual = Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, 100.0f); CORRADE_COMPARE(actual, expected); @@ -687,10 +687,10 @@ void Matrix4Test::perspectiveProjection() { } void Matrix4Test::perspectiveProjectionInfiniteFar() { - Matrix4 expected({4.0f, 0.0f, 0.0f, 0.0f}, + Matrix4 expected{{4.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 7.111111f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f, -1.0f}, - {0.0f, 0.0f, -64.0f, 0.0f}); + {0.0f, 0.0f, -64.0f, 0.0f}}; Matrix4 actual = Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, Constants::inf()); CORRADE_COMPARE(actual, expected); @@ -705,51 +705,51 @@ void Matrix4Test::perspectiveProjectionInfiniteFar() { } void Matrix4Test::perspectiveProjectionFov() { - Matrix4 expected({4.1652994f, 0.0f, 0.0f, 0.0f}, + Matrix4 expected{{4.1652994f, 0.0f, 0.0f, 0.0f}, { 0.0f, 9.788454f, 0.0f, 0.0f}, { 0.0f, 0.0f, -1.9411764f, -1.0f}, - { 0.0f, 0.0f, -94.1176452f, 0.0f}); + { 0.0f, 0.0f, -94.1176452f, 0.0f}}; CORRADE_COMPARE(Matrix4::perspectiveProjection(27.0_degf, 2.35f, 32.0f, 100.0f), expected); } void Matrix4Test::perspectiveProjectionFovInfiniteFar() { - Matrix4 expected({4.1652994f, 0.0f, 0.0f, 0.0f}, + Matrix4 expected{{4.1652994f, 0.0f, 0.0f, 0.0f}, { 0.0f, 9.788454f, 0.0f, 0.0f}, { 0.0f, 0.0f, -1.0f, -1.0f}, - { 0.0f, 0.0f, -64.0f, 0.0f}); + { 0.0f, 0.0f, -64.0f, 0.0f}}; CORRADE_COMPARE(Matrix4::perspectiveProjection(27.0_degf, 2.35f, 32.0f, Constants::inf()), expected); } void Matrix4Test::perspectiveProjectionOffCenter() { - Matrix4 expected({ 4.0f, 0.0f, 0.0f, 0.0f}, - { 0.0f, 7.111111f, 0.0f, 0.0f}, - {-0.125f, -0.1111111f, -1.9411764f, -1.0f}, - { 0.0f, 0.0f, -94.1176452f, 0.0f}); /* Shifted by (-1, -0.5) compared to the perspectiveProjection() test */ - Matrix4 actual = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, 100.0f); - CORRADE_COMPARE(actual, expected); + Matrix4 projection = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, 100.0f); + CORRADE_COMPARE(projection, (Matrix4{ + { 4.0f, 0.0f, 0.0f, 0.0f}, + { 0.0f, 7.111111f, 0.0f, 0.0f}, + {-0.125f, -0.1111111f, -1.9411764f, -1.0f}, + { 0.0f, 0.0f, -94.1176452f, 0.0f}})); /* NDC is left-handed, so point on the near plane top right corner should be (1, 1, -1), and a point in the center on the far plane roughly (0, 0, +1) due to the "off-centerness" */ - CORRADE_COMPARE(actual.transformPoint({7.0f, 4.0f, -32.0f}), Vector3(1.0f, 1.0f, -1.0f)); - CORRADE_COMPARE(actual.transformPoint({0.0f, 0.0f, -100.0f}), Vector3(0.125f, 0.1111111f, +1.0f)); + CORRADE_COMPARE(projection.transformPoint({7.0f, 4.0f, -32.0f}), Vector3(1.0f, 1.0f, -1.0f)); + CORRADE_COMPARE(projection.transformPoint({0.0f, 0.0f, -100.0f}), Vector3(0.125f, 0.1111111f, +1.0f)); } void Matrix4Test::perspectiveProjectionOffCenterInfiniteFar() { - Matrix4 expected({ 4.0f, 0.0f, 0.0f, 0.0f}, - { 0.0f, 7.111111f, 0.0f, 0.0f}, - {-0.125f, -0.1111111f, -1.0f, -1.0f}, - { 0.0f, 0.0f, -64.0f, 0.0f}); /* Shifted by (-1, -0.5) compared to perspectiveProjectionInfiniteFar() */ - Matrix4 actual = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, Constants::inf()); - CORRADE_COMPARE(actual, expected); + Matrix4 projection = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, Constants::inf()); + CORRADE_COMPARE(projection, (Matrix4{ + { 4.0f, 0.0f, 0.0f, 0.0f}, + { 0.0f, 7.111111f, 0.0f, 0.0f}, + {-0.125f, -0.1111111f, -1.0f, -1.0f}, + { 0.0f, 0.0f, -64.0f, 0.0f}})); /* NDC is left-handed, so point on the near plane bottom left corner should be (1, 1, -1) and a *vector* in the direction of the far plane roughly (0, 0, +1) due to the "off-centerness" */ - CORRADE_COMPARE(actual.transformPoint({-9.0f, -5.0f, -32.0f}), Vector3(-1.0f, -1.0f, -1.0f)); - CORRADE_COMPARE(actual.transformVector({0.0f, 0.0f, -1.0f}), Vector3(0.125f, 0.1111111f, +1.0f)); + CORRADE_COMPARE(projection.transformPoint({-9.0f, -5.0f, -32.0f}), Vector3(-1.0f, -1.0f, -1.0f)); + CORRADE_COMPARE(projection.transformVector({0.0f, 0.0f, -1.0f}), Vector3(0.125f, 0.1111111f, +1.0f)); } void Matrix4Test::lookAt() {