3#include "egg/math/Math.hh"
10Matrix34f::Matrix34f() {
11 a.fill(std::numeric_limits<f32>::signaling_NaN());
14Matrix34f::Matrix34f() =
default;
17Matrix34f::Matrix34f(f32 _e00, f32 _e01, f32 _e02, f32 _e03, f32 _e10, f32 _e11, f32 _e12, f32 _e13,
18 f32 _e20, f32 _e21, f32 _e22, f32 _e23) {
36 f32 yy = 2.0f * q.v.y * q.v.y;
37 f32 zz = 2.0f * q.v.z * q.v.z;
38 f32 xx = 2.0f * q.v.x * q.v.x;
39 f32 xy = 2.0f * q.v.x * q.v.y;
40 f32 xz = 2.0f * q.v.x * q.v.z;
41 f32 yz = 2.0f * q.v.y * q.v.z;
42 f32 wz = 2.0f * q.w * q.v.z;
43 f32 wx = 2.0f * q.w * q.v.x;
44 f32 wy = 2.0f * q.w * q.v.y;
46 mtx[0][0] = 1.0f - yy - zz;
51 mtx[1][1] = 1.0f - xx - zz;
56 mtx[2][2] = 1.0f - xx - yy;
66 f32 yy = 2.0f * q.v.y * q.v.y;
67 f32 zz = 2.0f * q.v.z * q.v.z;
68 f32 xx = 2.0f * q.v.x * q.v.x;
69 f32 xy = 2.0f * q.v.x * q.v.y;
70 f32 xz = 2.0f * q.v.x * q.v.z;
71 f32 yz = 2.0f * q.v.y * q.v.z;
72 f32 wz = 2.0f * q.w * q.v.z;
73 f32 wx = 2.0f * q.w * q.v.x;
74 f32 wy = 2.0f * q.w * q.v.y;
76 mtx[0][0] = 1.0f - yy - zz;
81 mtx[1][1] = 1.0f - xx - zz;
86 mtx[2][2] = 1.0f - xx - yy;
99 const f32 c0_c2 = c.x * c.z;
100 const f32 s0_s1 = s.x * s.y;
101 const f32 c0_s2 = c.x * s.z;
103 mtx[0][0] = (c.y * c.z);
104 mtx[1][0] = (c.y * s.z);
107 mtx[0][1] = (s0_s1 * c.z) - c0_s2;
108 mtx[1][1] = (s0_s1 * s.z) + c0_c2;
109 mtx[2][1] = (s.x * c.y);
111 mtx[0][2] = (c0_c2 * s.y) + (s.x * s.z);
112 mtx[1][2] = (c0_s2 * s.y) - (s.x * c.z);
113 mtx[2][2] = (c.x * c.y);
126 const f32 c0_c2 = c.x * c.z;
127 const f32 s0_s1 = s.x * s.y;
128 const f32 c0_s2 = c.x * s.z;
130 mtx[0][0] = (c.y * c.z);
131 mtx[1][0] = (c.y * s.z);
134 mtx[0][1] = (s0_s1 * c.z) - c0_s2;
135 mtx[1][1] = (s0_s1 * s.z) + c0_c2;
136 mtx[2][1] = (s.x * c.y);
138 mtx[0][2] = (c0_c2 * s.y) + (s.x * s.z);
139 mtx[1][2] = (c0_s2 * s.y) - (s.x * c.z);
140 mtx[2][2] = (c.x * c.y);
148void Matrix34f::makeS(
const Vector3f &s) {
183 mtx[rowIdx][0] *= row.x;
184 mtx[rowIdx][1] *= row.y;
185 mtx[rowIdx][2] *= row.z;
190 mtx[0][col] = base.x;
191 mtx[1][col] = base.y;
192 mtx[2][col] = base.z;
200 mat[0, 0] =
fma(rhs[2, 0], mtx[0][2],
fma(rhs[1, 0], mtx[0][1], rhs[0, 0] * mtx[0][0]));
201 mat[0, 1] =
fma(rhs[2, 1], mtx[0][2],
fma(rhs[1, 1], mtx[0][1], rhs[0, 1] * mtx[0][0]));
202 mat[1, 0] =
fma(rhs[2, 0], mtx[1][2],
fma(rhs[1, 0], mtx[1][1], rhs[0, 0] * mtx[1][0]));
203 mat[1, 1] =
fma(rhs[2, 1], mtx[1][2],
fma(rhs[1, 1], mtx[1][1], rhs[0, 1] * mtx[1][0]));
204 mat[0, 2] =
fma(rhs[2, 2], mtx[0][2],
fma(rhs[1, 2], mtx[0][1], rhs[0, 2] * mtx[0][0]));
205 mat[0, 3] =
fma(1.0f, mtx[0][3],
206 fma(rhs[2, 3], mtx[0][2],
fma(rhs[1, 3], mtx[0][1], rhs[0, 3] * mtx[0][0])));
207 mat[1, 2] =
fma(rhs[2, 2], mtx[1][2],
fma(rhs[1, 2], mtx[1][1], rhs[0, 2] * mtx[1][0]));
208 mat[1, 3] =
fma(1.0f, mtx[1][3],
209 fma(rhs[2, 3], mtx[1][2],
fma(rhs[1, 3], mtx[1][1], rhs[0, 3] * mtx[1][0])));
210 mat[2, 0] =
fma(rhs[2, 0], mtx[2][2],
fma(rhs[1, 0], mtx[2][1], rhs[0, 0] * mtx[2][0]));
211 mat[2, 1] =
fma(rhs[2, 1], mtx[2][2],
fma(rhs[1, 1], mtx[2][1], rhs[0, 1] * mtx[2][0]));
212 mat[2, 2] =
fma(rhs[2, 2], mtx[2][2],
fma(rhs[1, 2], mtx[2][1], rhs[0, 2] * mtx[2][0]));
213 mat[2, 3] =
fma(1.0f, mtx[2][3],
214 fma(rhs[2, 3], mtx[2][2],
fma(rhs[1, 3], mtx[2][1], rhs[0, 3] * mtx[2][0])));
223 ret.x = mtx[0][0] * vec.x + mtx[0][3] + mtx[0][1] * vec.y + mtx[0][2] * vec.z;
224 ret.y = mtx[1][0] * vec.x + mtx[1][3] + mtx[1][1] * vec.y + mtx[1][2] * vec.z;
225 ret.z = mtx[2][0] * vec.x + mtx[2][3] + mtx[2][1] * vec.y + mtx[2][2] * vec.z;
235 ret.x =
fma(mtx[0][2], vec.z, mtx[0][0] * vec.x) +
fma(mtx[0][3], 1.0f, mtx[0][1] * vec.y);
236 ret.y =
fma(mtx[1][2], vec.z, mtx[1][0] * vec.x) +
fma(mtx[1][3], 1.0f, mtx[1][1] * vec.y);
237 ret.z =
fma(mtx[2][2], vec.z, mtx[2][0] * vec.x) +
fma(mtx[2][3], 1.0f, mtx[2][1] * vec.y);
246 ret.x = mtx[0][0] * vec.x + mtx[0][1] * vec.y + mtx[0][2] * vec.z;
247 ret.y = mtx[1][0] * vec.x + mtx[1][1] * vec.y + mtx[1][2] * vec.z;
248 ret.z = mtx[2][0] * vec.x + mtx[2][1] * vec.y + mtx[2][2] * vec.z;
258 ret.x =
fma(mtx[0][2], vec.z,
fma(mtx[0][0], vec.x, mtx[0][1] * vec.y));
259 ret.y =
fma(mtx[1][2], vec.z,
fma(mtx[1][0], vec.x, mtx[1][1] * vec.y));
260 ret.z =
fma(mtx[2][2], vec.z,
fma(mtx[2][0], vec.x, mtx[2][1] * vec.y));
270 f32 determinant = ((((mtx[2][1] * (mtx[0][2] * mtx[1][0])) +
271 ((mtx[2][2] * (mtx[0][0] * mtx[1][1])) +
272 (mtx[2][0] * (mtx[0][1] * mtx[1][2])))) -
273 (mtx[0][2] * (mtx[2][0] * mtx[1][1]))) -
274 (mtx[2][2] * (mtx[1][0] * mtx[0][1]))) -
275 (mtx[1][2] * (mtx[0][0] * mtx[2][1]));
277 if (determinant == 0.0f) {
278 out = Matrix34f::ident;
282 f32 invDet = 1.0f / determinant;
284 out[0, 2] = (mtx[0][1] * mtx[1][2] - mtx[1][1] * mtx[0][2]) * invDet;
285 out[1, 2] = -(mtx[0][0] * mtx[1][2] - mtx[0][2] * mtx[1][0]) * invDet;
286 out[2, 1] = -(mtx[0][0] * mtx[2][1] - mtx[2][0] * mtx[0][1]) * invDet;
287 out[2, 2] = (mtx[0][0] * mtx[1][1] - mtx[1][0] * mtx[0][1]) * invDet;
288 out[2, 0] = (mtx[1][0] * mtx[2][1] - mtx[2][0] * mtx[1][1]) * invDet;
289 out[0, 0] = (mtx[1][1] * mtx[2][2] - mtx[2][1] * mtx[1][2]) * invDet;
290 out[0, 1] = -(mtx[0][1] * mtx[2][2] - mtx[2][1] * mtx[0][2]) * invDet;
291 out[1, 0] = -(mtx[1][0] * mtx[2][2] - mtx[2][0] * mtx[1][2]) * invDet;
292 out[1, 1] = (mtx[0][0] * mtx[2][2] - mtx[2][0] * mtx[0][2]) * invDet;
299 f32 fVar14 =
fms(mtx[0][1], mtx[1][2], mtx[1][1] * mtx[0][2]);
300 f32 fVar15 =
fms(mtx[1][1], mtx[2][2], mtx[2][1] * mtx[1][2]);
301 f32 fVar13 =
fms(mtx[2][1], mtx[0][2], mtx[0][1] * mtx[2][2]);
302 f32 determinant =
fma(mtx[2][0], fVar14,
fma(mtx[1][0], fVar13, mtx[0][0] * fVar15));
304 if (determinant == 0.0f) {
308 f32 invDet = 1.0f / determinant;
309 invDet = -
fms(determinant, invDet * invDet, invDet + invDet);
311 out[0, 0] = fVar15 * invDet;
312 out[0, 1] = fVar13 * invDet;
313 out[1, 0] =
fms(mtx[1][2], mtx[2][0], mtx[2][2] * mtx[1][0]) * invDet;
314 out[1, 1] =
fms(mtx[2][2], mtx[0][0], mtx[0][2] * mtx[2][0]) * invDet;
315 out[2, 0] =
fms(mtx[1][0], mtx[2][1], mtx[1][1] * mtx[2][0]) * invDet;
316 out[2, 1] =
fms(mtx[0][1], mtx[2][0], mtx[0][0] * mtx[2][1]) * invDet;
317 out[2, 2] =
fms(mtx[0][0], mtx[1][1], mtx[0][1] * mtx[1][0]) * invDet;
318 out[0, 2] = fVar14 * invDet;
319 out[0, 3] = -
fma(out[0, 2], mtx[2][3],
fma(out[0, 1], mtx[1][3], out[0, 0] * mtx[0][3]));
320 out[1, 2] =
fms(mtx[0][2], mtx[1][0], mtx[1][2] * mtx[0][0]) * invDet;
321 out[1, 3] = -
fma(out[1, 2], mtx[2][3],
fma(out[1, 1], mtx[1][3], out[1, 0] * mtx[0][3]));
322 out[2, 3] = -
fma(out[2, 2], mtx[2][3],
fma(out[2, 1], mtx[1][3], out[2, 0] * mtx[0][3]));
331 ret[0, 1] = mtx[1][0];
332 ret[0, 2] = mtx[2][0];
333 ret[1, 0] = mtx[0][1];
334 ret[1, 2] = mtx[2][1];
335 ret[2, 0] = mtx[0][2];
336 ret[2, 1] = mtx[1][2];
342const Matrix34f Matrix34f::ident(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
345const Matrix34f Matrix34f::zero(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
void makeOrthonormalBasis(const Vector3f &v0, const Vector3f &v1)
Sets a 3x3 orthonormal basis for a local coordinate system.
Matrix34f multiplyTo(const Matrix34f &rhs) const
Multiplies two matrices.
void setBase(size_t col, const Vector3f &base)
Sets one column of a matrix.
void makeZero()
Zeroes every element of the matrix.
void setAxisRotation(f32 angle, const Vector3f &axis)
Rotates the matrix about an axis.
void makeQ(const Quatf &q)
Sets rotation matrix from quaternion.
Vector3f multVector33(const Vector3f &vec) const
Multiplies a 3x3 matrix by a vector.
void inverseTo33(Matrix34f &out) const
Inverts the 3x3 portion of the 3x4 matrix.
void makeR(const Vector3f &r)
Sets 3x3 rotation matrix from a vector of Euler angles.
Vector3f multVector(const Vector3f &vec) const
Multiplies a vector by a matrix.
void makeRT(const Vector3f &r, const Vector3f &t)
Sets rotation-translation matrix.
Matrix34f transpose() const
Transposes the 3x3 portion of the matrix.
Vector3f ps_multVector(const Vector3f &vec) const
Paired-singles impl. of multVector.
void makeQT(const Quatf &q, const Vector3f &t)
Sets matrix from rotation and position.
void mulRow33(size_t rowIdx, const Vector3f &row)
Multiplies one row of a 3x3 matrix by a vector.
bool ps_inverse(Matrix34f &out) const
Vector3f ps_multVector33(const Vector3f &vec) const
Paired-singles impl. of multVector33.
static f32 fma(f32 x, f32 y, f32 z)
Fused multiply-add operation.
static f32 fms(f32 x, f32 y, f32 z)
Fused multiply-subtract operation.
A quaternion, used to represent 3D rotation.
void setAxisRotation(f32 angle, const Vector3f &axis)
Set the quat given angle and axis.
f32 normalise()
Normalizes the vector and returns the original length.