Browse Source

Math: slightly modernize certain Matrix[34] test code.

next
Vladimír Vondruš 3 weeks ago
parent
commit
85a9e63996
  1. 23
      src/Magnum/Math/Test/Matrix3Test.cpp
  2. 72
      src/Magnum/Math/Test/Matrix4Test.cpp

23
src/Magnum/Math/Test/Matrix3Test.cpp

@ -511,26 +511,25 @@ void Matrix3Test::shearingY() {
} }
void Matrix3Test::projection() { void Matrix3Test::projection() {
Matrix3 expected({2.0f/5.0f, 0.0f, 0.0f}, CORRADE_COMPARE(Matrix3::projection({5.0f, 4.0f}), (Matrix3{
{ 0.0f, 2.0f/4.0f, 0.0f}, {2.0f/5.0f, 0.0f, 0.0f},
{ 0.0f, 0.0f, 1.0f}); { 0.0f, 2.0f/4.0f, 0.0f},
{ 0.0f, 0.0f, 1.0f}}));
CORRADE_COMPARE(Matrix3::projection({5.0f, 4.0f}), expected);
} }
void Matrix3Test::projectionOffCenter() { 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 */ /* Shifted by (-1, -0.5) compared to the projection() test */
Matrix3 actual = Matrix3::projection({-3.5f, -2.5f}, {1.5f, 1.5f}); Matrix3 projection = Matrix3::projection({-3.5f, -2.5f}, {1.5f, 1.5f});
CORRADE_COMPARE(actual, expected); 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 /* 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 be (1, 1), and a point on the far plane bottom left corner should be
(-1, -1) */ (-1, -1) */
CORRADE_COMPARE(actual.transformPoint({1.5f, 1.5f}), (Vector2{1.0f, 1.0f})); CORRADE_COMPARE(projection.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({-3.5f, -2.5f}), (Vector2{-1.0f, -1.0f}));
} }
void Matrix3Test::fromParts() { void Matrix3Test::fromParts() {

72
src/Magnum/Math/Test/Matrix4Test.cpp

@ -641,26 +641,26 @@ void Matrix4Test::shearingYZ() {
} }
void Matrix4Test::orthographicProjection() { 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); 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 */ /* 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, -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, -9.0f}), (Vector3{0.0f, 0.0f, +1.0f}));
} }
void Matrix4Test::orthographicProjectionOffCenter() { 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 */ /* 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); 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 /* 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 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() { 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, 7.111111f, 0.0f, 0.0f},
{0.0f, 0.0f, -1.9411764f, -1.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); Matrix4 actual = Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, 100.0f);
CORRADE_COMPARE(actual, expected); CORRADE_COMPARE(actual, expected);
@ -687,10 +687,10 @@ void Matrix4Test::perspectiveProjection() {
} }
void Matrix4Test::perspectiveProjectionInfiniteFar() { 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, 7.111111f, 0.0f, 0.0f},
{0.0f, 0.0f, -1.0f, -1.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()); Matrix4 actual = Matrix4::perspectiveProjection({16.0f, 9.0f}, 32.0f, Constants::inf());
CORRADE_COMPARE(actual, expected); CORRADE_COMPARE(actual, expected);
@ -705,51 +705,51 @@ void Matrix4Test::perspectiveProjectionInfiniteFar() {
} }
void Matrix4Test::perspectiveProjectionFov() { 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, 9.788454f, 0.0f, 0.0f},
{ 0.0f, 0.0f, -1.9411764f, -1.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); CORRADE_COMPARE(Matrix4::perspectiveProjection(27.0_degf, 2.35f, 32.0f, 100.0f), expected);
} }
void Matrix4Test::perspectiveProjectionFovInfiniteFar() { 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, 9.788454f, 0.0f, 0.0f},
{ 0.0f, 0.0f, -1.0f, -1.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); CORRADE_COMPARE(Matrix4::perspectiveProjection(27.0_degf, 2.35f, 32.0f, Constants::inf()), expected);
} }
void Matrix4Test::perspectiveProjectionOffCenter() { 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 */ /* 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); Matrix4 projection = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, 100.0f);
CORRADE_COMPARE(actual, expected); 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 /* 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, be (1, 1, -1), and a point in the center on the far plane roughly (0, 0,
+1) due to the "off-centerness" */ +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(projection.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({0.0f, 0.0f, -100.0f}), Vector3(0.125f, 0.1111111f, +1.0f));
} }
void Matrix4Test::perspectiveProjectionOffCenterInfiniteFar() { 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() */ /* Shifted by (-1, -0.5) compared to perspectiveProjectionInfiniteFar() */
Matrix4 actual = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, Constants::inf()); Matrix4 projection = Matrix4::perspectiveProjection({-9.0f, -5.0f}, {7.0f, 4.0f}, 32.0f, Constants::inf());
CORRADE_COMPARE(actual, expected); 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 /* 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 be (1, 1, -1) and a *vector* in the direction of the far plane roughly
(0, 0, +1) due to the "off-centerness" */ (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(projection.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.transformVector({0.0f, 0.0f, -1.0f}), Vector3(0.125f, 0.1111111f, +1.0f));
} }
void Matrix4Test::lookAt() { void Matrix4Test::lookAt() {

Loading…
Cancel
Save