diff options
author | Sven Göthel <[email protected]> | 2024-04-29 01:14:16 +0200 |
---|---|---|
committer | Sven Göthel <[email protected]> | 2024-04-29 01:14:16 +0200 |
commit | 839acae6d3d4c70f5dbf2469f99af2a48e21e5fa (patch) | |
tree | ed17c5c8e0e8db25a544526dfe9d3e427e1510a9 | |
parent | 06261cfdfeec0d8186b62d7e1be849fb5276227d (diff) |
math: Fix and test Matrix4::mapWinToObj, unit test test_math_mat4f_10_project01
-rw-r--r-- | include/jau/math/mat4f.hpp | 4 | ||||
-rw-r--r-- | test/test_math_mat4f_10_project01.cpp | 8 | ||||
-rw-r--r-- | test/test_math_mat4f_11_unproject.cpp | 109 |
3 files changed, 115 insertions, 6 deletions
diff --git a/include/jau/math/mat4f.hpp b/include/jau/math/mat4f.hpp index 6ba18c9..fa0afcf 100644 --- a/include/jau/math/mat4f.hpp +++ b/include/jau/math/mat4f.hpp @@ -1551,7 +1551,7 @@ class alignas(Value_type) Matrix4 { Matrix4& mat4Tmp) noexcept { // invPMv = Inv(P x Mv) - const Matrix4& invPMv = mat4Tmp.mul(mP, mMv); + Matrix4& invPMv = mat4Tmp.mul(mP, mMv); if( !invPMv.invert() ) { return false; } @@ -1694,7 +1694,7 @@ class alignas(Value_type) Matrix4 { Matrix4& mat4Tmp) noexcept { // invPMv = Inv(P x Mv) - const Matrix4& invPMv = mat4Tmp.mul(mP, mMv); + Matrix4& invPMv = mat4Tmp.mul(mP, mMv); if( !invPMv.invert() ) { return false; } diff --git a/test/test_math_mat4f_10_project01.cpp b/test/test_math_mat4f_10_project01.cpp index a639832..ab6a6bf 100644 --- a/test/test_math_mat4f_10_project01.cpp +++ b/test/test_math_mat4f_10_project01.cpp @@ -41,7 +41,7 @@ static const float EPSILON = std::numeric_limits<float>::epsilon(); * * Both using same Mat4f::mapObjToWin(..). */ -TEST_CASE( "Test 01 PMVMatrixToMatrix4f", "[mat4f][linear_algebra][math]" ) { +TEST_CASE( "Test 01 Project PMVMatrixToMatrix4f", "[project][mat4f][linear_algebra][math]" ) { // Simple 10 x 10 view port const Recti viewport(0,0,10,10); @@ -90,7 +90,7 @@ TEST_CASE( "Test 01 PMVMatrixToMatrix4f", "[mat4f][linear_algebra][math]" ) { * * Both using same Mat4f::mapObjToWin(). */ -TEST_CASE( "Test 02 PMVMatrixToMatrix4f 2", "[mat4f][linear_algebra][math]" ) { +TEST_CASE( "Test 02 Project PMVMatrixToMatrix4f 2", "[project][mat4f][linear_algebra][math]" ) { // Simple 10 x 10 view port const Recti viewport(0,0,10,10); @@ -155,7 +155,7 @@ TEST_CASE( "Test 02 PMVMatrixToMatrix4f 2", "[mat4f][linear_algebra][math]" ) { REQUIRE_MSG("A/B 1.1 Project 0,0 failure", winB11 == winA11); } -TEST_CASE( "Test 10 Matrix4f 1", "[mat4f][linear_algebra][math]" ) { +TEST_CASE( "Test 10 Project Matrix4f 1", "[project][mat4f][linear_algebra][math]" ) { Vec3f winHas; Vec2f winExp( 297, 360 ); @@ -187,7 +187,7 @@ TEST_CASE( "Test 10 Matrix4f 1", "[mat4f][linear_algebra][math]" ) { REQUIRE_THAT( winExp.y, Catch::Matchers::WithinAbs(std::round(winHas.y), EPSILON) ); } -TEST_CASE( "Test 11 Matrix4f 2", "[mat4f][linear_algebra][math]" ) { +TEST_CASE( "Test 11 Project Matrix4f 2", "[project][mat4f][linear_algebra][math]" ) { Vec3f winHas; Vec2f winExp( 136, 360 ); diff --git a/test/test_math_mat4f_11_unproject.cpp b/test/test_math_mat4f_11_unproject.cpp new file mode 100644 index 0000000..e407645 --- /dev/null +++ b/test/test_math_mat4f_11_unproject.cpp @@ -0,0 +1,109 @@ +/* + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2024 Gothel Software e.K. + * + * 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 <thread> +#include <cassert> +#include <cinttypes> +#include <cstring> + +#include <jau/test/catch2_ext.hpp> + +#include <jau/math/util/pmvmat4f.hpp> + +using namespace jau; +using namespace jau::math; +using namespace jau::math::util; + +static const float NaN = std::numeric_limits<float>::quiet_NaN(); + +TEST_CASE( "Test 01 Unproject NaN", "[unproject][mat4f][linear_algebra][math]" ) { + const Mat4f mMv({1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}); + const Mat4f mP({1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}); + const Recti viewport(0,0,800,600); + const float pick[] = { 400, 300, 0 }; + + Vec3f objPos(NaN, NaN, NaN); + Mat4f tmp; + + // gluUnProject + bool res = Mat4f::mapWinToObj(pick[0], pick[1], pick[2], + mMv, mP, viewport, + objPos, tmp); + + REQUIRE( false == std::isnan(objPos.x) ); + REQUIRE( false == std::isnan(objPos.y) ); + REQUIRE( false == std::isnan(objPos.z) ); + + REQUIRE( true == res ); +} + +TEST_CASE( "Test 10 Unproject Pick 1", "[unproject][mat4f][linear_algebra][math]" ) { + const Mat4f mMv({ 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 }); + const Mat4f mP({ 2.3464675f, 0, 0, 0, + 0, 2.4142134f, 0, 0, + 0, 0, -1.0002f, -1, + 0, 0, -20.002f, 0 }); + const Recti viewport(0, 0, 1000, 1000); + const float pick[] = { 250, 250, 0.5f }; + + const Vec3f expected(-4.2612f, -4.1417f, -19.9980f ); + Vec3f result; + Mat4f tmp; + + // gluUnProject + bool res = Mat4f::mapWinToObj(pick[0], pick[1], pick[2], + mMv, mP, viewport, + result, tmp); + + REQUIRE( true == res ); + COMPARE_NARRAYS_EPS(expected.cbegin(), result.cbegin(), 3, 0.0001f); +} + +TEST_CASE( "Test 11 Unproject Pick 2", "[unproject][mat4f][linear_algebra][math]" ) { + const Mat4f mMv({ 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, -200, 1 }); + const Mat4f mP({ 2.3464675f, 0, 0, 0, + 0, 2.4142134f, 0, 0, + 0, 0, -1.0002f, -1, + 0, 0, -20.002f, 0 }); + const Recti viewport(0, 0, 1000, 1000); + const float pick[] = { 250, 250, 0.5f }; + + const Vec3f expected(-4.2612f, -4.1417f, 180.002f ); + Vec3f result; + Mat4f tmp; + + // gluUnProject + bool res = Mat4f::mapWinToObj(pick[0], pick[1], pick[2], + mMv, mP, viewport, + result, tmp); + + REQUIRE( true == res ); + COMPARE_NARRAYS_EPS(expected.cbegin(), result.cbegin(), 3, 0.0001f); +} + |