jaulib v1.3.0
Jau Support Library (C++, Java, ..)
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 <thread>
25#include <cassert>
26#include <cinttypes>
27#include <cstring>
28
30
31#include <jau/math/mat4f.hpp>
33
34using namespace jau;
35using namespace jau::math;
36
37static const float EPSILON = std::numeric_limits<float>::epsilon();
38
39static const float mI_0[] = { 1, 0, 0, 0,
40 0, 1, 0, 0,
41 0, 0, 1, 0,
42 0, 0, 0, 1 };
43static const Mat4f mI(mI_0);
44
45static const float m1_0[] = { 1, 3, 4, 0,
46 6, 7, 8, 5,
47 98, 7, 6, 9,
48 54, 3, 2, 5 };
49static const Mat4f m1(m1_0);
50
51static const float m1T_0[] = { 1, 6, 98, 54,
52 3, 7, 7, 3,
53 4, 8, 6, 2,
54 0, 5, 9, 5 };
55static const Mat4f m1T(m1T_0);
56
57static const float m2_0[] = { 1, 6, 98, 54,
58 3, 7, 7, 3,
59 4, 8, 6, 2,
60 0, 5, 9, 5 };
61static const Mat4f m2(m2_0);
62
63static const float m2xm1_0[] = { 26, 59, 143, 71,
64 59, 174, 730, 386,
65 143, 730, 9770, 5370,
66 71, 386, 5370, 2954 };
67static const Mat4f m2xm1(m2xm1_0);
68
69static const float m1xm2_0[] = {12557, 893, 748, 1182,
70 893, 116, 116, 113,
71 748, 116, 120, 104,
72 1182, 113, 104, 131 };
73static const Mat4f m1xm2(m1xm2_0);
74
75TEST_CASE( "Test 00 Load Get", "[mat4f][linear_algebra][math]" ) {
76 {
77 Mat4f m;
78 REQUIRE(mI == m);
79 }
80 {
81 float f16[16];
82 m1.get(f16);
84
85 Mat4f m;
86 m.load(f16);
87 REQUIRE(m1 == m);
88 }
89}
90
91TEST_CASE( "Test 01 Mul", "[mat4f][linear_algebra][math]" ) {
92 {
93 REQUIRE(m1xm2 == m1 * m2);
94 Mat4f m; m.mul(m1, m2);
95 REQUIRE(m1xm2 == m);
96 }
97 {
98 REQUIRE(m2xm1 == m2 * m1);
99 Mat4f m; m.mul(m2, m1);
100 REQUIRE(m2xm1 == m);
101 }
102}
103
104TEST_CASE( "Test 02 Transpose", "[mat4f][linear_algebra][math]" ) {
105 REQUIRE(m1T == Mat4f(m1).transpose());
106 REQUIRE(m1T == Mat4f().transpose(m1));
107}
108
109TEST_CASE( "Test 10 LookAtNegZ", "[mat4f][linear_algebra][math]" ) {
110 Mat4f tmp;
111 Mat4f m;
112 // Look towards -z
113 m.setToLookAt(
114 Vec3f(0, 0, 0), // eye
115 Vec3f(0, 0, -1), // center
116 Vec3f(0, 1, 0), // up
117 tmp);
118
119 /**
120 * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
121 */
122 Mat4f exp( { 1, 0, 0, 0,
123 0, 1, 0, 0,
124 0, 0, 1, 0,
125 0, 0, 0, 1 } );
126
127 REQUIRE(exp == m);
128}
129
130TEST_CASE( "Test 11 LookAtPosY", "[mat4f][linear_algebra][math]" ) {
131 Mat4f tmp;
132 Mat4f m;
133 // Look towards -z
134 m.setToLookAt(
135 Vec3f(0, 0, 0), // eye
136 Vec3f(0, 1, 0), // center
137 Vec3f(0, 0, 1), // up
138 tmp);
139
140 /**
141 * The 3 rows of the matrix (= the 3 columns of the array/buffer) should be: side, up, -forward.
142 */
143 Mat4f exp( { 1, 0, 0, 0,
144 0, 0, -1, 0,
145 0, 1, 0, 0,
146 0, 0, 0, 1
147 } );
148
149 REQUIRE(exp == m);
150}
151
152TEST_CASE( "Test 20 Float16Stack", "[stack][mat4f][math]" ) {
154 Mat4f m10( { 1.0f, 2.0f, 3.0f, 4.0f, // column 0
155 5.0f, 6.0f, 7.0f, 8.0f, // column 1
156 9.0f, 10.0f, 11.0f, 12.0f, // column 2
157 13.0f, 14.0f, 15.0f, 16.0f // column 3
158 } );
159 Mat4f m20 = m10 * 2.0f;
160 std::cout << "mat4 m10 " << m10 << std::endl;
161 std::cout << "mat4 m20 " << m20 << std::endl;
162 s1.push(m10.cbegin());
163 s1.push(m20.cbegin());
164 Mat4f m22, m12;
165 s1.pop(m22.begin());
166 s1.pop(m12.begin());
167 REQUIRE( m22 == m20 );
168 REQUIRE( m12 == m10 );
169}
170
171TEST_CASE( "Test 21 Mat4fStack", "[stack][mat4f][math]" ) {
173 Mat4f m10( { 1.0f, 2.0f, 3.0f, 4.0f, // column 0
174 5.0f, 6.0f, 7.0f, 8.0f, // column 1
175 9.0f, 10.0f, 11.0f, 12.0f, // column 2
176 13.0f, 14.0f, 15.0f, 16.0f // column 3
177 } );
178 Mat4f m20 = m10 * 2.0f;
179 std::cout << "mat4 m10 " << m10 << std::endl;
180 std::cout << "mat4 m20 " << m20 << std::endl;
181 s1.push(m10);
182 s1.push(m20);
183 Mat4f m22, m12;
184 s1.pop(m22);
185 s1.pop(m12);
186 REQUIRE( m22 == m20 );
187 REQUIRE( m12 == m10 );
188}
#define COMPARE_NARRAYS_EPS(lhs, rhs, len, eps)
Definition: catch2_ext.hpp:102
Basic 4x4 value_type matrix implementation using fields for intensive use-cases (host operations).
Definition: mat4f.hpp:112
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:306
constexpr iterator begin() noexcept
Definition: mat4f.hpp:237
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:1299
constexpr Matrix4 & mul(const Matrix4 &b) noexcept
Multiply matrix: [this] = [this] x [b].
Definition: mat4f.hpp:717
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:264
A Matrix stack of compounds, each consisting of 16 * T
Definition: sstack.hpp:119
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
A simple stack of compounds, each consisting of element_size * T
Definition: sstack.hpp:55
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
Matrix4< float > Mat4f
Definition: mat4f.hpp:1881
Vector3F< float > Vec3f
Definition: vec3f.hpp:371
__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)