jaulib v1.3.8
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_math_mat4f_11_unproject.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cstring>
26
27#include <jau/test/catch2_ext.hpp>
28
30
31using namespace jau;
32using namespace jau::math;
33using namespace jau::math::util;
34
35static const float NaN = std::numeric_limits<float>::quiet_NaN();
36
37TEST_CASE( "Test 01 Unproject NaN", "[unproject][mat4f][linear_algebra][math]" ) {
38 const Mat4f mMv({1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1});
39 const Mat4f mP({1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1});
40 const Recti viewport(0,0,800,600);
41 const float pick[] = { 400, 300, 0 };
42
43 Vec3f objPos(NaN, NaN, NaN);
44
45 // gluUnProject
46 bool res = Mat4f::mapWinToObj(pick[0], pick[1], pick[2],
47 mMv, mP, viewport,
48 objPos);
49
50 REQUIRE( false == std::isnan(objPos.x) );
51 REQUIRE( false == std::isnan(objPos.y) );
52 REQUIRE( false == std::isnan(objPos.z) );
53
54 REQUIRE( true == res );
55}
56
57TEST_CASE( "Test 10 Unproject Pick 1", "[unproject][mat4f][linear_algebra][math]" ) {
58 const Mat4f mMv({ 1, 0, 0, 0,
59 0, 1, 0, 0,
60 0, 0, 1, 0,
61 0, 0, 0, 1 });
62 const Mat4f mP({ 2.3464675f, 0, 0, 0,
63 0, 2.4142134f, 0, 0,
64 0, 0, -1.0002f, -1,
65 0, 0, -20.002f, 0 });
66 const Recti viewport(0, 0, 1000, 1000);
67 const float pick[] = { 250, 250, 0.5f };
68
69 const Vec3f expObj(-4.2612f, -4.1417f, -19.9980f );
70 Vec3f result;
71
72 // gluUnProject
73 bool res = Mat4f::mapWinToObj(pick[0], pick[1], pick[2],
74 mMv, mP, viewport,
75 result);
76
77 REQUIRE( true == res );
78 COMPARE_NARRAYS_EPS(expObj.cbegin(), result.cbegin(), 3, 0.0001f);
79
80 const Vec3f expView = mMv * expObj;
81 res = Mat4f::mapWinToView(pick[0], pick[1], pick[2],
82 mP, viewport,
83 result);
84 REQUIRE( true == res );
85 COMPARE_NARRAYS_EPS(expView.cbegin(), result.cbegin(), 3, 0.0001f);
86}
87
88TEST_CASE( "Test 11 Unproject Pick 2", "[unproject][mat4f][linear_algebra][math]" ) {
89 const Mat4f mMv({ 1, 0, 0, 0,
90 0, 1, 0, 0,
91 0, 0, 1, 0,
92 0, 0, -200, 1 });
93 const Mat4f mP({ 2.3464675f, 0, 0, 0,
94 0, 2.4142134f, 0, 0,
95 0, 0, -1.0002f, -1,
96 0, 0, -20.002f, 0 });
97 const Recti viewport(0, 0, 1000, 1000);
98 const float pick[] = { 250, 250, 0.5f };
99
100 const Vec3f expObj(-4.2612f, -4.1417f, 180.002f );
101 Vec3f result;
102
103 // gluUnProject
104 bool res = Mat4f::mapWinToObj(pick[0], pick[1], pick[2],
105 mMv, mP, viewport,
106 result);
107
108 REQUIRE( true == res );
109 COMPARE_NARRAYS_EPS(expObj.cbegin(), result.cbegin(), 3, 0.0001f);
110
111 const Vec3f expView = mMv * expObj;
112 res = Mat4f::mapWinToView(pick[0], pick[1], pick[2],
113 mP, viewport,
114 result);
115 REQUIRE( true == res );
116 COMPARE_NARRAYS_EPS(expView.cbegin(), result.cbegin(), 3, 0.0001f);
117}
118
static bool mapWinToView(const value_type winx, const value_type winy, const value_type winz, const Matrix4 &mP, const Recti &viewport, Vec3 &viewPos) noexcept
Definition mat4f.hpp:1583
static bool mapWinToObj(const value_type winx, const value_type winy, const value_type winz, const Matrix4 &mMv, const Matrix4 &mP, const Recti &viewport, Vec3 &objPos) noexcept
Definition mat4f.hpp:1558
value_type x
Definition vec3f.hpp:69
constexpr const_iterator cbegin() const noexcept
Definition vec3f.hpp:105
value_type y
Definition vec3f.hpp:70
value_type z
Definition vec3f.hpp:71
Matrix4< float > Mat4f
Definition mat4f.hpp:1928
RectI< int > Recti
Definition recti.hpp:139
Vector3F< float > Vec3f
Definition vec3f.hpp:433
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
static const float NaN
TEST_CASE("Test 01 Unproject NaN", "[unproject][mat4f][linear_algebra][math]")