GCC Code Coverage Report


./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
6 of 6, 0 excluded
100.0%
Functions:
3 of 3, 0 excluded
100.0%
Branches:
2 of 4, 0 excluded
50.0%

libs/base/src/eu/base/mat4.h
Line Branch Exec Source
1 #pragma once
2
3 #include "eu/base/vec3.h"
4 #include "eu/base/vec4.h"
5 #include "eu/base/angle.h"
6 #include "eu/base/axisangle.h"
7
8
9 namespace eu
10 {
11 /** \addtogroup math
12 * @{
13 */
14
15 struct Q;
16
17 /// 4x4 matrix
18 struct m4
19 {
20 /// Create a new matrix from column major format.
21 [[nodiscard]] static m4 from_col_major(
22 float t00, float t01, float t02, float t03,
23 float t10, float t11, float t12, float t13,
24 float t20, float t21, float t22, float t23,
25 float t30, float t31, float t32, float t33);
26
27 /// Create a new matrix from row major format.
28 651 [[nodiscard]] constexpr static m4 from_row_major(
29 float t00, float t10, float t20, float t30,
30 float t01, float t11, float t21, float t31,
31 float t02, float t12, float t22, float t32,
32 float t03, float t13, float t23, float t33)
33 {
34 return {
35 t00, t01, t02, t03,
36 t10, t11, t12, t13,
37 t20, t21, t22, t23,
38 651 t30, t31, t32, t33};
39 }
40
41 /// Create a matrix from the major, aka the diagonal.
42 /// @see from_scalar
43 [[nodiscard]] static m4 from_major(const v4 &major);
44
45 /// Create a matrix from a single scalar.
46 /// @see from_major
47 [[nodiscard]] constexpr static m4 from_scalar(float scalar)
48 {
49 const float z = 0;
50 return from_row_major(
51 scalar, z, z, z,
52 z, scalar, z, z,
53 z, z, scalar, z,
54 z, z, z, scalar);
55 }
56
57 [[nodiscard]] static m4 from_basis(const n3& a, const n3& b, const n3& c);
58 [[nodiscard]] static m4 from_scale(const v3& s);
59
60 /// Create a translation matrix.
61 [[nodiscard]] static m4 from_translation(const v3 &v);
62
63 /// Create a rotation matrix, around the X axis.
64 [[nodiscard]] static m4 from_rot_x(const An &a);
65
66 /// Create a rotation matrix, around the Y axis.
67 [[nodiscard]] static m4 from_rot_y(const An &a);
68
69 /// Create a rotation matrix, around the Z axis.
70 [[nodiscard]] static m4 from_rot_z(const An &a);
71
72 /// Create a rotation matrix, from an axis angle.
73 [[nodiscard]] static m4 from(const AA &aa);
74
75 /// Create a rotation matrix, from a quaternion.
76 [[nodiscard]] static std::optional<m4> from(const Q& q);
77
78 /// Create an orthographic projection matrix.
79 /// Also known as a `clip_from_view` transformation.
80 /// Clip Space is in OpenGL NDC (-1 to +1) z range.
81 /// @param l the left side
82 /// @param r the right side
83 /// @param t the up side
84 /// @param b the down side
85 /// @param n near
86 /// @param f far
87 [[nodiscard]] static m4 create_ortho_lrud(float l, float r, float t, float b, float n, float f);
88
89 /// Create a perspective projection matrix.
90 [[nodiscard]] static m4 create_perspective(const An &fov, float aspect_ratio, float near, float far);
91
92 /// Get a direct pointer to the data in column major format, for API integration.
93 float *get_column_major_data_ptr();
94
95 /// Get a direct pointer to the const data in column major format, for API integration.
96 [[nodiscard]] const float* get_column_major_data_ptr() const;
97
98 /// Invert the current matrix.
99 /// @see get_inverted
100 bool invert();
101
102 /// Return the inverted matrix
103 /// @see invert
104 [[nodiscard]] m4 get_inverted() const;
105
106 /// Return a single value given a row and column (note: zero/0 based index).
107 [[nodiscard]] float get(int row, int col) const;
108
109 /// Return a single value given a row and column (note: one/1 based index).
110 [[nodiscard]] float get1(int row, int col) const;
111
112 /// Get a transformed vec4.
113 [[nodiscard]] v4 get_transformed(const v4 &p) const;
114
115 /// Get a transformed vec3 assuming it's a point
116 [[nodiscard]] v3 get_transformed_point(const v3 &p) const;
117
118 /// Get a transformed vec3, assuming it's a normal vector
119 [[nodiscard]] v3 get_transformed_vec(const v3 &p) const;
120
121 /// Get a transformed unit3
122 [[nodiscard]] n3 get_transformed_vec(const n3 &p) const;
123
124 /// Get a row as a vec4.
125 [[nodiscard]] v4 get_row(int r) const;
126
127 /// Get a column as vec4.
128 [[nodiscard]] v4 get_column(int c) const;
129
130 /// Combine this with a translation matrix.
131 [[nodiscard]] m4 get_translated(const v3 &t) const;
132
133 /// Combine this with a rotation matrix.
134 [[nodiscard]] m4 get_rotated(const AA &aa) const;
135
136 /// Get the current translation of the transformation matrix.
137 [[nodiscard]] v3 get_translation() const;
138
139 /// Get the major vector.
140 [[nodiscard]] v4 get_major() const;
141
142 /// Get the local X axis.
143 [[nodiscard]] n3 get_x_axis() const;
144
145 /// Get the local Y axis.
146 [[nodiscard]] n3 get_y_axis() const;
147
148 /// Get the local Z azis.
149 [[nodiscard]] n3 get_z_axis() const;
150
151 /// Gets the transpose of a matrix.
152 /// If the matrix is a rotation matrix, then the transpose is guaranteed to be the inverse of the matrix.
153 [[nodiscard]] m4 get_transposed() const;
154
155
156 void operator+=(const m4 &rhs);
157 void operator-=(const m4 &rhs);
158
159 private:
160 /// stored in column major
161 float data[16];
162
163 m4() = default;
164
165 665 constexpr m4(
166 float t00, float t01, float t02, float t03,
167 float t10, float t11, float t12, float t13,
168 float t20, float t21, float t22, float t23,
169 float t30, float t31, float t32, float t33)
170 665 : data{
171 t00, t01, t02, t03,
172 t10, t11, t12, t13,
173 t20, t21, t22, t23,
174 t30, t31, t32, t33}
175 {
176 665 }
177 };
178
179 /// The identity matrix.
180 constexpr m4 m4_identity = m4::from_scalar(1);
181
182 /// Convert a matrix to a string representation, prefer fmt.
183 std::string string_from(const m4 &m);
184
185 m4 operator+(const m4 &lhs, const m4 &rhs);
186 m4 operator-(const m4 &lhs, const m4 &rhs);
187 m4 operator*(const m4 &lhs, const m4 &rhs);
188 v4 operator*(const m4 &lhs, const v4 &rhs);
189
190 /** @}*/
191
192 ADD_CATCH_FORMATTER_DEF(m4)
193 }
194
195
2/4
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 11 not taken.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 9 not taken.
1 ADD_DEFAULT_FORMATTER(eu::m4, std::string, eu::string_from);
196