jaulib v1.5.0
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_math_mat4f_10_project01.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 <thread>
25#include <cassert>
26#include <cinttypes>
27#include <cstring>
28
29#include <jau/test/catch2_ext.hpp>
30
32
33using namespace jau;
34using namespace jau::math;
35using namespace jau::math::util;
36
37/**
38 * PMVMatrix w/ separate P + Mv vs Mat4f::mapObjToWin() w/ single PMv
39 *
40 * Both using same Mat4f::mapObjToWin(..).
41 */
42TEST_CASE( "Test 01 Project PMVMatrixToMatrix4f", "[project][mat4f][linear_algebra][math]" ) {
43 // Simple 10 x 10 view port
44 const Recti viewport(0,0,10,10);
45
46 Vec3f winA00, winA01, winA10, winA11;
47 Vec3f winB00, winB01, winB10, winB11;
48
49 PMVMat4f m;
50 Mat4f mat4PMv;
51 m.getMulPMv(mat4PMv);
52 // std::cout << mat4PMv.toString(null, "mat4PMv", "%10.5f"));
53
54 m.mapObjToWin(Vec3f(1, 0, 0), viewport, winA00); // separate P + Mv
55 std::cout << "A.0.0 - Project 1,0 -->" << winA00 << std::endl;
56 Mat4f::mapObjToWin(Vec3f(1, 0, 0), mat4PMv, viewport, winB00); // single PMv
57 std::cout << "B.0.0 - Project 1,0 -->" << winB00 << std::endl;
58
59 m.mapObjToWin(Vec3f(0, 0, 0), viewport, winA01);
60 std::cout << "A.0.1 - Project 0,0 -->" << winA01 << std::endl;
61 Mat4f::mapObjToWin(Vec3f(0, 0, 0), mat4PMv, viewport, winB01);
62 std::cout << "B.0.1 - Project 0,0 -->" << winB01 << std::endl;
63
64 m.orthoP(0, 10, 0, 10, 1, -1);
65 std::cout << "MATRIX - Ortho 0,0,10,10 - Locate the origin in the bottom left and scale" << std::endl;
66 std::cout << m << std::endl;
67 m.getMulPMv(mat4PMv);
68 std::cout << mat4PMv.toString("mat4PMv", "%10.5f") << std::endl;
69
70 m.mapObjToWin(Vec3f(1, 0, 0), viewport, winA10);
71 std::cout << "A.1.0 - Project 1,0 -->" << winA10 << std::endl;
72 Mat4f::mapObjToWin(Vec3f(1, 0, 0), mat4PMv, viewport, winB10);
73 std::cout << "B.1.0 - Project 1,0 -->" << winB10 << std::endl;
74
75 m.mapObjToWin(Vec3f(0, 0, 0), viewport, winA11);
76 std::cout << "A.1.1 - Project 0,0 -->" << winA11 << std::endl;
77 Mat4f::mapObjToWin(Vec3f(0, 0, 0), mat4PMv, viewport, winB11);
78 std::cout << "B.1.1 - Project 0,0 -->" << winB11 << std::endl;
79
80 REQUIRE_MSG("A/B 0.0 Project 1,0 failure", winB00 == winA00);
81 REQUIRE_MSG("A/B 0.1 Project 0,0 failure", winB01 == winA01);
82 REQUIRE_MSG("A/B 1.0 Project 1,0 failure", winB10 == winA10);
83 REQUIRE_MSG("A/B 1.1 Project 0,0 failure", winB11 == winA11);
84}
85
86/**
87 * PMVMatrix vs Mat4f::mapObjToWin(), both w/ separate P + Mv
88 *
89 * Both using same Mat4f::mapObjToWin().
90 */
91TEST_CASE( "Test 02 Project PMVMatrixToMatrix4f 2", "[project][mat4f][linear_algebra][math]" ) {
92 // Simple 10 x 10 view port
93 const Recti viewport(0,0,10,10);
94
95 Vec3f winA00, winA01, winA10, winA11;
96 Vec3f winB00, winB01, winB10, winB11;
97
98 PMVMat4f m;
99 Mat4f mat4Mv, mat4P;
100
101 float mat4Mv_f16[16];
102 float mat4P_f16[16];
103
104 m.getMv().get(mat4Mv_f16);
105 m.getP().get(mat4P_f16);
106
107 std::cout << m.getMv().toString("mat4Mv") << std::endl;
108 std::cout << m.getP().toString("mat4P") << std::endl;
109 mat4Mv.load( mat4Mv_f16 );
110 mat4P.load( mat4P_f16 );
111 REQUIRE( Mat4f(mat4Mv_f16) == mat4Mv);
112 REQUIRE( Mat4f(mat4P_f16) == mat4P);
113 REQUIRE( m.getMv() == mat4Mv);
114 REQUIRE( m.getP() == mat4P);
115
116 m.mapObjToWin(Vec3f(1, 0, 0), viewport, winA00);
117 std::cout << "A.0.0 - Project 1,0 -->" << winA00 << std::endl;
118 Mat4f::mapObjToWin(Vec3f(1, 0, 0), mat4Mv, mat4P, viewport, winB00);
119 std::cout << "B.0.0 - Project 1,0 -->" << winB00 << std::endl;
120
121 m.mapObjToWin(Vec3f(0, 0, 0), viewport, winA01);
122 std::cout << "A.0.1 - Project 0,0 -->" << winA01 << std::endl;
123 Mat4f::mapObjToWin(Vec3f(0, 0, 0), mat4Mv, mat4P, viewport, winB01);
124 std::cout << "B.0.1 - Project 0,0 -->" << winB01 << std::endl;
125
126 m.orthoP(0, 10, 0, 10, 1, -1);
127 std::cout << "MATRIX - Ortho 0,0,10,10 - Locate the origin in the bottom left and scale" << std::endl;
128 std::cout << m << std::endl;
129 m.getMv().get(mat4Mv_f16);
130 m.getP().get(mat4P_f16);
131 std::cout << m.getMv().toString("mat4Mv") << std::endl;
132 std::cout << m.getP().toString("mat4P") << std::endl;
133 mat4Mv.load( mat4Mv_f16 );
134 mat4P.load( mat4P_f16 );
135 REQUIRE( Mat4f(mat4Mv_f16) == mat4Mv);
136 REQUIRE( Mat4f(mat4P_f16) == mat4P);
137 REQUIRE( m.getMv() == mat4Mv);
138 REQUIRE( m.getP() == mat4P);
139
140 m.mapObjToWin(Vec3f(1, 0, 0), viewport, winA10);
141 std::cout << "A.1.0 - Project 1,0 -->" << winA10 << std::endl;
142 Mat4f::mapObjToWin(Vec3f(1, 0, 0), mat4Mv, mat4P, viewport, winB10);
143 std::cout << "B.1.0 - Project 1,0 -->" << winB10 << std::endl;
144
145 m.mapObjToWin(Vec3f(0, 0, 0), viewport, winA11);
146 std::cout << "A.1.1 - Project 0,0 -->" << winA11 << std::endl;
147 Mat4f::mapObjToWin(Vec3f(0, 0, 0), mat4Mv, mat4P, viewport, winB11);
148 std::cout << "B.1.1 - Project 0,0 -->" << winB11 << std::endl;
149
150 REQUIRE_MSG("A/B 0.0 Project 1,0 failure", winB00 == winA00);
151 REQUIRE_MSG("A/B 0.1 Project 0,0 failure", winB01 == winA01);
152 REQUIRE_MSG("A/B 1.0 Project 1,0 failure", winB10 == winA10);
153 REQUIRE_MSG("A/B 1.1 Project 0,0 failure", winB11 == winA11);
154}
155
156TEST_CASE( "Test 10 Project Matrix4f 1", "[project][mat4f][linear_algebra][math]" ) {
157 Vec3f winHas;
158 Vec2f winExp( 297, 360 );
159
160 Recti viewport(0, 0, 1280, 720);
161
162 Mat4f mat4Mv ({
163 0.40000000596046450000f, 0.00000000000000000000f, 0.00000000000000000000f, 0.00000000000000000000f,
164 0.00000000000000000000f, 0.40000000596046450000f, 0.00000000000000000000f, 0.00000000000000000000f,
165 0.00000000000000000000f, 0.00000000000000000000f, 1.00000000000000000000f, 0.00000000000000000000f,
166 -0.09278385341167450000f, -0.00471283448860049250f, -0.20000000298023224000f, 1.00000000000000000000f });
167
168 Mat4f mat4P ({
169 1.35799503326416020000f, 0.00000000000000000000f, 0.00000000000000000000f, 0.00000000000000000000f,
170 0.00000000000000000000f, 2.41421341896057130000f, 0.00000000000000000000f, 0.00000000000000000000f,
171 0.00000000000000000000f, 0.00000000000000000000f, -1.00002861022949220000f, -1.00000000000000000000f,
172 0.00000000000000000000f, 0.00000000000000000000f, -0.20000286400318146000f, 0.00000000000000000000f });
173
174 Vec3f objPos(0.02945519052445888500f, 0.01178207620978355400f, -0.00499999988824129100f);
175
176 std::cout << "pMv" << std::endl;
177 std::cout << mat4Mv.toString("", "%25.20f") << std::endl;
178 std::cout << "pP" << std::endl;
179 std::cout << mat4P.toString("", "%25.20f") << std::endl;
180
181 Mat4f::mapObjToWin(objPos, mat4Mv, mat4P, viewport, winHas);
182 std::cout << "B.0.0 - Project 1,0 -->" << winHas << std::endl;
183
184 REQUIRE_THAT( winExp.x, Catch::Matchers::WithinAbs(std::round(winHas.x), EPSILON<float>) );
185 REQUIRE_THAT( winExp.y, Catch::Matchers::WithinAbs(std::round(winHas.y), EPSILON<float>) );
186}
187
188TEST_CASE( "Test 11 Project Matrix4f 2", "[project][mat4f][linear_algebra][math]" ) {
189 Vec3f winHas;
190 Vec2f winExp( 136, 360 );
191
192 Recti viewport(0, 0, 1280, 720);
193
194 // m30 (row 3, column 0) differs from test01
195 Mat4f mat4Mv({
196 0.40000000596046450000f, 0.00000000000000000000f, 0.00000000000000000000f, 0.00000000000000000000f,
197 0.00000000000000000000f, 0.40000000596046450000f, 0.00000000000000000000f, 0.00000000000000000000f,
198 0.00000000000000000000f, 0.00000000000000000000f, 1.00000000000000000000f, 0.00000000000000000000f,
199 -0.13065303862094880000f, -0.00471283448860049250f, -0.20000000298023224000f, 1.00000000000000000000f });
200
201 Mat4f mat4P({
202 1.35799503326416020000f, 0.00000000000000000000f, 0.00000000000000000000f, 0.00000000000000000000f,
203 0.00000000000000000000f, 2.41421341896057130000f, 0.00000000000000000000f, 0.00000000000000000000f,
204 0.00000000000000000000f, 0.00000000000000000000f, -1.00002861022949220000f, -1.00000000000000000000f,
205 0.00000000000000000000f, 0.00000000000000000000f, -0.20000286400318146000f, 0.00000000000000000000f });
206
207 Vec3f objPos(0.02945519052445888500f, 0.01178207620978355400f, -0.00499999988824129100f);
208
209 std::cout << "pMv" << std::endl;
210 std::cout << mat4Mv.toString("", "%25.20ff") << std::endl;
211 std::cout << "pP" << std::endl;
212 std::cout << mat4P.toString("", "%25.20ff") << std::endl;
213
214 Mat4f::mapObjToWin(objPos, mat4Mv, mat4P, viewport, winHas);
215 std::cout << "B.0.0 - Project 1,0 -->" << winHas << std::endl;
216
217 REQUIRE_THAT( winExp.x, Catch::Matchers::WithinAbs(std::round(winHas.x), EPSILON<float>) );
218 REQUIRE_THAT( winExp.y, Catch::Matchers::WithinAbs(std::round(winHas.y), EPSILON<float>) );
219}
static bool mapObjToWin(const Vec3 &obj, const Matrix4 &mPMv, const Recti &viewport, Vec3 &winPos) noexcept
Definition mat4f.hpp:1506
constexpr value_type get(const jau::nsize_t i) const noexcept
Returns the ith component of the given column-major order matrix, 0 <= i < 16, w/o boundary check.
Definition mat4f.hpp:295
constexpr Matrix4 & load(const_iterator src) noexcept
Load the values of the given matrix src to this matrix w/o boundary check.
Definition mat4f.hpp:253
std::string toString(const std::string &rowPrefix, const std::string_view f) const noexcept
Returns a formatted string representation of this matrix.
Definition mat4f.hpp:1932
value_type x
Definition vec2f.hpp:62
value_type y
Definition vec2f.hpp:63
value_type x
Definition vec3f.hpp:66
value_type y
Definition vec3f.hpp:67
constexpr Mat4 & getP() noexcept
Returns the projection matrix (P).
Definition pmvmat4.hpp:263
constexpr Mat4 & getMulPMv(Mat4 &result) noexcept
Returns multiplication result of P and Mv matrix, i.e.
Definition pmvmat4.hpp:447
constexpr void orthoP(const float left, const float right, const float bottom, const float top, const float zNear, const float zFar) noexcept
Multiply the projection matrix with the orthogonal matrix.
Definition pmvmat4.hpp:838
constexpr Mat4 & getMv() noexcept
Returns the modelview matrix (Mv).
Definition pmvmat4.hpp:275
bool mapObjToWin(const Vec3 &objPos, const Recti &viewport, Vec3 &winPos) const noexcept
Map object coordinates to window coordinates.
Definition pmvmat4.hpp:921
constexpr T EPSILON
Alias for epsilon constant, i.e. std::numeric_limits<T>::epsilon()
Matrix4< float > Mat4f
Definition mat4f.hpp:1968
Vector2F< float > Vec2f
Definition vec2f.hpp:404
RectI< int > Recti
Definition recti.hpp:146
Vector3F< float > Vec3f
Definition vec3f.hpp:422
PMVMatrix4< float > PMVMat4f
Definition pmvmat4.hpp:1463
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
TEST_CASE("Test 01 Project PMVMatrixToMatrix4f", "[project][mat4f][linear_algebra][math]")
PMVMatrix w/ separate P + Mv vs Mat4f::mapObjToWin() w/ single PMv.