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:
1 of 2, 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 28 [[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 28 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.
107 [[nodiscard]] float get(int row, int col) const;
108
109 /// Get a transformed vec4.
110 [[nodiscard]] v4 get_transformed(const v4 &p) const;
111
112 /// Get a transformed vec3 assuming it's a point
113 [[nodiscard]] v3 get_transformed_point(const v3 &p) const;
114
115 /// Get a transformed vec3, assuming it's a normal vector
116 [[nodiscard]] v3 get_transformed_vec(const v3 &p) const;
117
118 /// Get a transformed unit3
119 [[nodiscard]] n3 get_transformed_vec(const n3 &p) const;
120
121 /// Get a row as a vec4.
122 [[nodiscard]] v4 get_row(int r) const;
123
124 /// Get a column as vec4.
125 [[nodiscard]] v4 get_column(int c) const;
126
127 /// Combine this with a translation matrix.
128 [[nodiscard]] m4 get_translated(const v3 &t) const;
129
130 /// Combine this with a rotation matrix.
131 [[nodiscard]] m4 get_rotated(const AA &aa) const;
132
133 /// Get the current translation of the transformation matrix.
134 [[nodiscard]] v3 get_translation() const;
135
136 /// Get the major vector.
137 [[nodiscard]] v4 get_major() const;
138
139 /// Get the local X axis.
140 [[nodiscard]] n3 get_x_axis() const;
141
142 /// Get the local Y axis.
143 [[nodiscard]] n3 get_y_axis() const;
144
145 /// Get the local Z azis.
146 [[nodiscard]] n3 get_z_axis() const;
147
148 /// Gets the transpose of a matrix.
149 /// If the matrix is a rotation matrix, then the transpose is guaranteed to be the inverse of the matrix.
150 [[nodiscard]] m4 get_transposed() const;
151
152
153 void operator+=(const m4 &rhs);
154 void operator-=(const m4 &rhs);
155
156 private:
157 /// stored in column major
158 float data[16];
159
160 m4() = default;
161
162 41 constexpr m4(
163 float t00, float t01, float t02, float t03,
164 float t10, float t11, float t12, float t13,
165 float t20, float t21, float t22, float t23,
166 float t30, float t31, float t32, float t33)
167 41 : data{
168 t00, t01, t02, t03,
169 t10, t11, t12, t13,
170 t20, t21, t22, t23,
171 t30, t31, t32, t33}
172 {
173 41 }
174 };
175
176 /// The identity matrix.
177 constexpr m4 m4_identity = m4::from_scalar(1);
178
179 /// Convert a matrix to a string representation, prefer fmt.
180 std::string string_from(const m4 &m);
181
182 m4 operator+(const m4 &lhs, const m4 &rhs);
183 m4 operator-(const m4 &lhs, const m4 &rhs);
184 m4 operator*(const m4 &lhs, const m4 &rhs);
185 v4 operator*(const m4 &lhs, const v4 &rhs);
186
187 /** @}*/
188
189 ADD_CATCH_FORMATTER_DEF(m4)
190 }
191
192
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 17 not taken.
2 ADD_DEFAULT_FORMATTER(eu::m4, std::string, eu::string_from);
193