jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_math_mat4f_01.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
29#include <jau/math/mat4f.hpp>
31
32using namespace jau;
33using namespace jau::math;
34
35static const float EPSILON = std::numeric_limits<float>::epsilon();
36
37static const float mI_0[] = { 1, 0, 0, 0,
38 0, 1, 0, 0,
39 0, 0, 1, 0,
40 0, 0, 0, 1 };
41static const Mat4f mI(mI_0);
42
43static const float m1_0[] = { 1, 3, 4, 0,
44 6, 7, 8, 5,
45 98, 7, 6, 9,
46 54, 3, 2, 5 };
47static const Mat4f m1(m1_0);
48
49static const float m1T_0[] = { 1, 6, 98, 54,
50 3, 7, 7, 3,
51 4, 8, 6, 2,
52 0, 5, 9, 5 };
53static const Mat4f m1T(m1T_0);
54
55static const float m2_0[] = { 1, 6, 98, 54,
56 3, 7, 7, 3,
57 4, 8, 6, 2,
58 0, 5, 9, 5 };
59static const Mat4f m2(m2_0);
60
61static const float m2xm1_0[] = { 26, 59, 143, 71,
62 59, 174, 730, 386,
63 143, 730, 9770, 5370,
64 71, 386, 5370, 2954 };
65static const Mat4f m2xm1(m2xm1_0);
66
67static const float m1xm2_0[] = {12557, 893, 748, 1182,
68 893, 116, 116, 113,
69 748, 116, 120, 104,
70 1182, 113, 104, 131 };
71static const Mat4f m1xm2(m1xm2_0);
72
73TEST_CASE( "Test 00 Load Get", "[mat4f][linear_algebra][math]" ) {
74 {
75 Mat4f m;
76 REQUIRE(mI == m);
77 }
78 {
79 float f16[16];
80 m1.get(f16);
81 COMPARE_NARRAYS_EPS(m1_0, f16, 16, EPSILON);
82
83 Mat4f m;
84 m.load(f16);
85 REQUIRE(m1 == m);
86 }
87}
88
89TEST_CASE( "Test 01 Mul", "[mat4f][linear_algebra][math]" ) {
90 {
91 REQUIRE(m1xm2 == m1 * m2);
92 Mat4f m; m.mul(m1, m2);
93 REQUIRE(m1xm2 == m);
94 }
95 {
96 REQUIRE(m2xm1 == m2 * m1);
97 Mat4f m; m.mul(m2, m1);
98 REQUIRE(m2xm1 == m);
99 }
100}
101
102TEST_CASE( "Test 02 Transpose", "[mat4f][linear_algebra][math]" ) {
103 REQUIRE(m1T == Mat4f(m1).transpose());
104 REQUIRE(m1T == Mat4f().transpose(m1));
105}
106
107TEST_CASE( "Test 10 LookAtNegZ", "[mat4f][linear_algebra][math]" ) {
108 Mat4f tmp;
109 Mat4f m;
110 // Look towards -z
111 m.setToLookAt(
112 Vec3f(0, 0, 0), // eye
113 Vec3f(0, 0, -1), // center
114 Vec3f(0, 1, 0), // up
115 tmp);
116
117 /**
118 * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
119 */
120 Mat4f exp( { 1, 0, 0, 0,
121 0, 1, 0, 0,
122 0, 0, 1, 0,
123 0, 0, 0, 1 } );
124
125 REQUIRE(exp == m);
126}
127
128TEST_CASE( "Test 11 LookAtPosY", "[mat4f][linear_algebra][math]" ) {
129 Mat4f tmp;
130 Mat4f m;
131 // Look towards -z
132 m.setToLookAt(
133 Vec3f(0, 0, 0), // eye
134 Vec3f(0, 1, 0), // center
135 Vec3f(0, 0, 1), // up
136 tmp);
137
138 /**
139 * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
140 */
141 Mat4f exp( { 1, 0, 0, 0,
142 0, 0, -1, 0,
143 0, 1, 0, 0,
144 0, 0, 0, 1
145 } );
146
147 REQUIRE(exp == m);
148}
149
150TEST_CASE( "Test 20 Float16Stack", "[stack][mat4f][math]" ) {
152 Mat4f m10( { 1.0f, 2.0f, 3.0f, 4.0f, // column 0
153 5.0f, 6.0f, 7.0f, 8.0f, // column 1
154 9.0f, 10.0f, 11.0f, 12.0f, // column 2
155 13.0f, 14.0f, 15.0f, 16.0f // column 3
156 } );
157 Mat4f m20 = m10 * 2.0f;
158 std::cout << "mat4 m10 " << m10 << std::endl;
159 std::cout << "mat4 m20 " << m20 << std::endl;
160 s1.push(m10.cbegin());
161 s1.push(m20.cbegin());
162 Mat4f m22, m12;
163 s1.pop(m22.begin());
164 s1.pop(m12.begin());
165 REQUIRE( m22 == m20 );
166 REQUIRE( m12 == m10 );
167}
168
169TEST_CASE( "Test 21 Mat4fStack", "[stack][mat4f][math]" ) {
171 Mat4f m10( { 1.0f, 2.0f, 3.0f, 4.0f, // column 0
172 5.0f, 6.0f, 7.0f, 8.0f, // column 1
173 9.0f, 10.0f, 11.0f, 12.0f, // column 2
174 13.0f, 14.0f, 15.0f, 16.0f // column 3
175 } );
176 Mat4f m20 = m10 * 2.0f;
177 std::cout << "mat4 m10 " << m10 << std::endl;
178 std::cout << "mat4 m20 " << m20 << std::endl;
179 s1.push(m10);
180 s1.push(m20);
181 Mat4f m22, m12;
182 s1.pop(m22);
183 s1.pop(m12);
184 REQUIRE( m22 == m20 );
185 REQUIRE( m12 == m10 );
186}
constexpr iterator begin() noexcept
Definition mat4f.hpp:238
constexpr Matrix4 & setToLookAt(const Vec3 &eye, const Vec3 &center, const Vec3 &up, Matrix4 &tmp) noexcept
Set this matrix to the look-at matrix based on given parameters.
Definition mat4f.hpp:1303
constexpr Matrix4 & mul(const Matrix4 &b) noexcept
Multiply matrix: [this] = [this] x [b].
Definition mat4f.hpp:721
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:265
constexpr_cxx20 void pop(matrix_t &dest) noexcept
Definition sstack.hpp:154
constexpr_cxx20 void push(const matrix_t &src) noexcept
Definition sstack.hpp:149
constexpr_cxx20 void push(const value_type *src) noexcept
Definition sstack.hpp:88
constexpr_cxx20 void pop(value_type *dest) noexcept
Definition sstack.hpp:95
SimpleStack< float, 16 > Stack16f
4x4 float matrix stack based on single float elements
Definition sstack.hpp:109
Matrix4< float > Mat4f
Definition mat4f.hpp:1885
MatrixStack< float > Mat4fStack
4x4 float matrix stack
Definition sstack.hpp:166
Vector3F< float > Vec3f
Definition vec3f.hpp:404
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
static const Mat4f mI(mI_0)
static const float mI_0[]
static const Mat4f m1T(m1T_0)
static const Mat4f m1(m1_0)
static const float m2xm1_0[]
static const float m2_0[]
TEST_CASE("Test 00 Load Get", "[mat4f][linear_algebra][math]")
static const float m1_0[]
static const Mat4f m2(m2_0)
static const float EPSILON
static const float m1xm2_0[]
static const Mat4f m1xm2(m1xm2_0)
static const float m1T_0[]
static const Mat4f m2xm1(m2xm1_0)