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