11#include "translate2.hpp"
15#include "transform_fwd.hpp"
18namespace hi {
inline namespace v1 {
37 hilet a = f32x4::broadcast(1.0f);
51 constexpr matrix2(f32x4 col0, f32x4 col1, f32x4 col2, f32x4 col3 = f32x4{0.0f, 0.0f, 0.0f, 1.0f})
noexcept :
52 _col0(col0), _col1(col1), _col2(col2), _col3(col3)
65 _col0(static_cast<f32x4>(col0)),
66 _col1(static_cast<f32x4>(col1)),
67 _col2(static_cast<f32x4>(col2)),
68 _col3(static_cast<f32x4>(col3).xyz1())
79 _col0(
static_cast<f32x4
>(col0)),
80 _col1(
static_cast<f32x4
>(col1)),
81 _col2(f32x4{0.0f, 0.0f, 1.0f, 0.0f}),
82 _col3(f32x4{0.0f, 0.0f, 0.0f, 1.0f})
111 float c2r2) noexcept :
112 _col0(c0r0, c0r1, c0r2, 0.0f), _col1(c1r0, c1r1, c1r2, 0.0f), _col2(c2r0, c2r1, c2r2, 0.0f), _col3(0.0f, 0.0f, 0.0f, 1.0f)
155 float c3r3) noexcept :
156 _col0(c0r0, c0r1, c0r2, c0r3), _col1(c1r0, c1r1, c1r2, c1r3), _col2(c2r0, c2r1, c2r2, c2r3), _col3(c3r0, c3r1, c3r2, c3r3)
163 hilet ones = f32x4::broadcast(1.0f);
165 _col1 = ones._0y00();
166 _col2 = ones._00z0();
167 _col3 = ones._000w() + f32x4{rhs};
170 [[nodiscard]]
constexpr matrix2(scale2
const& rhs)
noexcept
172 _col0 = f32x4{rhs}.x000();
173 _col1 = f32x4{rhs}._0y00();
174 _col2 = f32x4{rhs}._00z0();
175 _col3 = f32x4{rhs}._000w();
194 hilet x_mul = f32x4{rhs}.xxxx() * f32x4{rhs};
195 hilet y_mul = f32x4{rhs}.yyyy() * f32x4{rhs};
196 hilet z_mul = f32x4{rhs}.zzzz() * f32x4{rhs};
198 auto twos = f32x4{-2.0f, 2.0f, 2.0f, 0.0f};
199 auto one = f32x4{1.0f, 0.0f, 0.0f, 0.0f};
200 _col0 =
one + addsub<0b0011>(z_mul.zwxy(), y_mul.yxwz()) * twos;
203 _col1 =
one + addsub<0b0110>(x_mul.yxwz(), z_mul.wzyx()) * twos;
206 _col2 =
one + addsub<0b0101>(y_mul.wzyx(), x_mul.zwxy()) * twos;
215 return {_col0, _col1, _col2, _col3};
227 [[nodiscard]]
constexpr static matrix2
231 hilet scaled_rectangle = scale * src_rectangle;
233 return translation * scale;
242 [[nodiscard]]
friend constexpr f32x4
const&
get(
matrix2 const& rhs)
noexcept
244 if constexpr (I == 0) {
246 }
else if constexpr (I == 1) {
248 }
else if constexpr (I == 2) {
250 }
else if constexpr (I == 3) {
263 [[nodiscard]]
friend constexpr f32x4&
get(
matrix2& rhs)
noexcept
265 if constexpr (I == 0) {
267 }
else if constexpr (I == 1) {
269 }
else if constexpr (I == 2) {
271 }
else if constexpr (I == 3) {
278 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
280 return _col0.z() == 0.0f and _col0.w() == 0.0f and _col1.z() == 0.0f and _col1.w() == 0.0f and _col2.x() == 0.0f and
281 _col2.y() == 0.0f and _col2.z() == 1.0f and _col2.w() == 0.0f and _col3.z() == 0.0f and _col3.w() == 1.0f;
289 [[nodiscard]]
constexpr f32x4
operator*(f32x4
const& rhs)
const noexcept
291 return {_col0 * rhs.xxxx() + _col1 * rhs.yyyy() + _col2 * rhs.zzzz() + _col3 * rhs.wwww()};
298 auto tmp =
transpose(rhs._col0, rhs._col1, rhs._col2, rhs._col3);
299 return matrix2{std::get<0>(tmp), std::get<1>(tmp), std::get<2>(tmp), std::get<3>(tmp)};
335 template<
char DstX,
char DstY,
char DstZ,
char DstW = 'w'>
338 return matrix2{reflect_column<DstX>(), reflect_column<DstY>(), reflect_column<DstZ>(), reflect_column<DstW>()} * rhs;
348 return equal(lhs._col0, rhs._col0) and equal(lhs._col1, rhs._col1) and equal(lhs._col3, rhs._col3);
360 hilet s0c0 = _col0 * _col1.yxwz();
366 hilet s1c1 = _col0 * _col2.yxwz();
367 hilet s0c0s1c1 = hsub(s0c0, s1c1);
373 hilet s2c2 = _col0 * _col3.yxwz();
379 hilet s3c3 = _col1 * _col2.yxwz();
380 hilet s2c2s3c3 = hsub(s2c2, s3c3);
386 hilet s4c4 = _col1 * _col3.yxwz();
392 hilet s5c5 = _col2 * _col3.yxwz();
393 hilet s4c4s5c5 = hsub(s4c4, s5c5);
401 hilet s0123 = s0c0s1c1.xz00() + s2c2s3c3._00xz();
402 hilet s45__ = s4c4s5c5.xz00();
404 hilet c5432 = s4c4s5c5.wy00() + s2c2s3c3._00wy();
405 hilet c10__ = s0c0s1c1.wy00();
407 hilet det_prod_half0 = neg<0b0010>(s0123 * c5432);
408 hilet det_prod_half1 = neg<0b0001>(s45__ * c10__);
410 hilet det_sum0 = hadd(det_prod_half0, det_prod_half1);
411 hilet det_sum1 = hadd(det_sum0, det_sum0);
412 hilet det = hadd(det_sum1, det_sum1).xxxx();
414 if (det.x() == 0.0f) {
418 hilet invdet = rcp(det);
427 auto tmp_c5543 = neg<0b1010>(c5432.xxyz());
428 auto tmp_c4221 = neg<0b0101>(c5432.yww0() + c10__._000x());
429 auto tmp_c3100 = neg<0b1010>(c5432.z000() + c10__._0xyy());
430 hilet inv_col0 = ((t._col1.yxxx() * tmp_c5543) + (t._col1.zzyy() * tmp_c4221) + (t._col1.wwwz() * tmp_c3100)) * invdet;
436 tmp_c5543 = -tmp_c5543;
437 tmp_c4221 = -tmp_c4221;
438 tmp_c3100 = -tmp_c3100;
439 hilet inv_col1 = ((t._col0.yxxx() * tmp_c5543) + (t._col0.zzyy() * tmp_c4221) + (t._col0.wwwz() * tmp_c3100)) * invdet;
445 auto tmp_s5543 = neg<0b1010>(s45__.yyx0() + s0123._000w());
446 auto tmp_s4221 = neg<0b0101>(s45__.x000() + s0123._0zzy());
447 auto tmp_s3100 = neg<0b1010>(s0123.wyxx());
448 hilet inv_col2 = ((t._col3.yxxx() * tmp_s5543) + (t._col3.zzyy() * tmp_s4221) + (t._col3.wwwz() * tmp_s3100)) * invdet;
454 tmp_s5543 = -tmp_s5543;
455 tmp_s4221 = -tmp_s4221;
456 tmp_s3100 = -tmp_s3100;
457 hilet inv_col3 = ((t._col2.yxxx() * tmp_s5543) + (t._col2.zzyy() * tmp_s4221) + (t._col2.wwwz() * tmp_s3100)) * invdet;
459 return matrix2{inv_col0, inv_col1, inv_col2, inv_col3};
469 [[nodiscard]]
static constexpr f32x4 reflect_column() noexcept
471 if constexpr (Axis ==
'x') {
472 return f32x4{1.0f, 0.0f, 0.0f, 0.0f};
473 }
else if constexpr (Axis ==
'X') {
474 return f32x4{-1.0f, 0.0f, 0.0f, 0.0f};
475 }
else if constexpr (Axis ==
'y') {
476 return f32x4{0.0f, 1.0f, 0.0f, 0.0f};
477 }
else if constexpr (Axis ==
'Y') {
478 return f32x4{0.0f, -1.0f, 0.0f, 0.0f};
479 }
else if constexpr (Axis ==
'z') {
480 return f32x4{0.0f, 0.0f, 1.0f, 0.0f};
481 }
else if constexpr (Axis ==
'Z') {
482 return f32x4{0.0f, 0.0f, -1.0f, 0.0f};
483 }
else if constexpr (Axis ==
'w') {
484 return f32x4{0.0f, 0.0f, 0.0f, 1.0f};
485 }
else if constexpr (Axis ==
'W') {
486 return f32x4{0.0f, 0.0f, 0.0f, -1.0f};
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:323
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
@ one
The number was one, and this means something in the current language.
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:26
Horizontal/Vertical alignment combination.
Definition alignment.hpp:239
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix2.hpp:26
constexpr matrix2(rotate2 const &rhs) noexcept
Convert quaternion to matrix.
Definition matrix2.hpp:181
constexpr matrix2(float c0r0, float c1r0, float c2r0, float c0r1, float c1r1, float c2r1, float c0r2, float c1r2, float c2r2) noexcept
Construct a 3x3 matrix from scalar values.
Definition matrix2.hpp:102
constexpr friend bool operator==(matrix2 const &lhs, matrix2 const &rhs) noexcept
Compare two matrices potentially of different dimensions.
Definition matrix2.hpp:346
constexpr matrix2(vector3 col0, vector3 col1, vector3 col2, vector3 col3=vector3{}) noexcept
Construct a matrix from four vectors.
Definition matrix2.hpp:64
friend constexpr f32x4 & get(matrix2 &rhs) noexcept
Get a column.
Definition matrix2.hpp:263
constexpr matrix2(float c0r0, float c1r0, float c2r0, float c3r0, float c0r1, float c1r1, float c2r1, float c3r1, float c0r2, float c1r2, float c2r2, float c3r2, float c0r3, float c1r3, float c2r3, float c3r3) noexcept
Construct a 4x4 matrix from scalar values.
Definition matrix2.hpp:139
constexpr f32x4 operator*(f32x4 const &rhs) const noexcept
Transform a f32x4 numeric array by the matrix.
Definition matrix2.hpp:289
static constexpr matrix2 uniform(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
Create a transformation matrix to translate and uniformly-scale a src_rectangle to a dst_rectangle.
Definition matrix2.hpp:228
friend constexpr matrix2 transpose(matrix2 const &rhs) noexcept
Matrix transpose.
Definition matrix2.hpp:296
constexpr matrix2 operator~() const
Invert matrix.
Definition matrix2.hpp:353
constexpr matrix2(f32x4 col0, f32x4 col1, f32x4 col2, f32x4 col3=f32x4{0.0f, 0.0f, 0.0f, 1.0f}) noexcept
Construct a matrix from four columns.
Definition matrix2.hpp:51
constexpr matrix2(vector2 col0, vector2 col1) noexcept
Construct a matrix from two vectors.
Definition matrix2.hpp:78
constexpr matrix2() noexcept
Constructs an identity matrix.
Definition matrix2.hpp:35
friend constexpr matrix2 reflect(matrix2 const &rhs) noexcept
Reflect axis of a matrix.
Definition matrix2.hpp:336
friend constexpr f32x4 const & get(matrix2 const &rhs) noexcept
Get a column.
Definition matrix2.hpp:242
static constexpr scale2 uniform(extent2 src_extent, extent2 dst_extent) noexcept
Get a uniform-scale-transform to scale an extent to another extent.
Definition scale2.hpp:45
Definition translate2.hpp:13
static constexpr translate2 align(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition translate2.hpp:79
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:18
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector3.hpp:19