14#include "rectangle.hpp"
20#include "../color/color.hpp"
36 static_assert(D == 2 || D == 3,
"Only 2D or 3D rotation-matrices are supported");
40 constexpr matrix& operator=(
matrix const&)
noexcept =
default;
41 constexpr matrix& operator=(
matrix&&)
noexcept =
default;
47 hilet a = f32x4::broadcast(1.0f);
61 constexpr matrix(f32x4 col0, f32x4 col1, f32x4 col2, f32x4 col3 = f32x4{0.0f, 0.0f, 0.0f, 1.0f})
noexcept :
62 _col0(col0), _col1(col1), _col2(col2), _col3(col3)
73 constexpr matrix(vector3 col0, vector3 col1, vector3 col2, vector3 col3 = vector3{})
noexcept
76 _col0(static_cast<f32x4>(col0)),
77 _col1(static_cast<f32x4>(col1)),
78 _col2(static_cast<f32x4>(col2)),
79 _col3(static_cast<f32x4>(col3).xyz1())
110 _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 _col0(c0r0, c0r1, c0r2, c0r3), _col1(c1r0, c1r1, c1r2, c1r3), _col2(c2r0, c2r1, c2r2, c2r3), _col3(c3r0, c3r1, c3r2, c3r3)
186 return {_col0, _col1, _col2, _col3};
198 [[nodiscard]]
constexpr static matrix
207 [[nodiscard]]
friend constexpr f32x4
const&
get(
matrix const& rhs)
noexcept
209 if constexpr (I == 0) {
211 }
else if constexpr (I == 1) {
213 }
else if constexpr (I == 2) {
215 }
else if constexpr (I == 3) {
228 [[nodiscard]]
friend constexpr f32x4&
get(
matrix& rhs)
noexcept
230 if constexpr (I == 0) {
232 }
else if constexpr (I == 1) {
234 }
else if constexpr (I == 2) {
236 }
else if constexpr (I == 3) {
243 [[nodiscard]]
constexpr bool holds_invariant() const noexcept
253 [[nodiscard]]
constexpr f32x4
operator*(f32x4
const& rhs)
const noexcept
255 return {_col0 * rhs.xxxx() + _col1 * rhs.yyyy() + _col2 * rhs.zzzz() + _col3 * rhs.wwww()};
263 [[nodiscard]]
constexpr float operator*(
float const& rhs)
const noexcept
266 auto abs_scale = hypot<D>(_col0 * f32x4::broadcast(rhs));
279 return {*
this * get<0>(rhs), *
this * get<1>(rhs), *
this * get<2>(rhs), *
this * get<3>(rhs)};
290 [[nodiscard]]
constexpr auto operator*(vector<float, E>
const& rhs)
const noexcept
293 return vector<float,
std::max(D, E)>{
294 _col0 *
static_cast<f32x4
>(rhs).xxxx() + _col1 *
static_cast<f32x4
>(rhs).yyyy() +
295 _col2 *
static_cast<f32x4
>(rhs).zzzz()};
310 _col0 *
static_cast<f32x4
>(rhs).xxxx() + _col1 *
static_cast<f32x4
>(rhs).yyyy() +
311 _col2 *
static_cast<f32x4
>(rhs).zzzz()};
320 [[nodiscard]]
constexpr auto operator*(point<float, E>
const& rhs)
const noexcept
323 return point<float,
std::max(D, E)>{
324 _col0 *
static_cast<f32x4
>(rhs).xxxx() + _col1 *
static_cast<f32x4
>(rhs).yyyy() +
325 _col2 *
static_cast<f32x4
>(rhs).zzzz() + _col3 *
static_cast<f32x4
>(rhs).wwww()};
350 return rectangle{*
this * rhs.origin, *
this * rhs.right, *
this * rhs.up};
358 [[nodiscard]]
constexpr quad
operator*(quad
const& rhs)
const noexcept
360 return quad{*
this * rhs.p0, *
this * rhs.p1, *
this * rhs.p2, *
this * rhs.p3};
370 return circle{*
this * midpoint(rhs), *
this * rhs.radius()};
380 return line_segment{*
this * rhs.origin(), *
this * rhs.direction()};
391 [[nodiscard]]
constexpr auto operator*(color
const& rhs)
const noexcept
395 _col0 *
static_cast<f32x4
>(rhs).xxxx() + _col1 *
static_cast<f32x4
>(rhs).yyyy() +
396 _col2 *
static_cast<f32x4
>(rhs).zzzz() + _col3};
406 return matrix{*
this * get<0>(rhs), *
this * get<1>(rhs), *
this * get<2>(rhs), *
this * get<3>(rhs)};
413 auto tmp =
transpose(rhs._col0, rhs._col1, rhs._col2, rhs._col3);
414 return {std::get<0>(tmp), std::get<1>(tmp), std::get<2>(tmp), std::get<3>(tmp)};
451 template<
char DstX,
char DstY,
char DstZ,
char DstW = 'w'>
455 return matrix{reflect_column<DstX>(), reflect_column<DstY>(), reflect_column<DstZ>(), reflect_column<DstW>()} * rhs;
466 return _col0 == rhs._col0 && _col1 == rhs._col1 && _col2 == rhs._col2 && _col3 == rhs._col3;
478 hilet s0c0 = _col0 * _col1.yxwz();
484 hilet s1c1 = _col0 * _col2.yxwz();
485 hilet s0c0s1c1 = hsub(s0c0, s1c1);
491 hilet s2c2 = _col0 * _col3.yxwz();
497 hilet s3c3 = _col1 * _col2.yxwz();
498 hilet s2c2s3c3 = hsub(s2c2, s3c3);
504 hilet s4c4 = _col1 * _col3.yxwz();
510 hilet s5c5 = _col2 * _col3.yxwz();
511 hilet s4c4s5c5 = hsub(s4c4, s5c5);
519 hilet s0123 = s0c0s1c1.xz00() + s2c2s3c3._00xz();
520 hilet s45__ = s4c4s5c5.xz00();
522 hilet c5432 = s4c4s5c5.wy00() + s2c2s3c3._00wy();
523 hilet c10__ = s0c0s1c1.wy00();
525 hilet det_prod_half0 = neg<0b0010>(s0123 * c5432);
526 hilet det_prod_half1 = neg<0b0001>(s45__ * c10__);
528 hilet det_sum0 = hadd(det_prod_half0, det_prod_half1);
529 hilet det_sum1 = hadd(det_sum0, det_sum0);
530 hilet det = hadd(det_sum1, det_sum1).xxxx();
532 if (det.x() == 0.0f) {
536 hilet invdet = rcp(det);
545 auto tmp_c5543 = neg<0b1010>(c5432.xxyz());
546 auto tmp_c4221 = neg<0b0101>(c5432.yww0() + c10__._000x());
547 auto tmp_c3100 = neg<0b1010>(c5432.z000() + c10__._0xyy());
548 hilet inv_col0 = ((t._col1.yxxx() * tmp_c5543) + (t._col1.zzyy() * tmp_c4221) + (t._col1.wwwz() * tmp_c3100)) * invdet;
554 tmp_c5543 = -tmp_c5543;
555 tmp_c4221 = -tmp_c4221;
556 tmp_c3100 = -tmp_c3100;
557 hilet inv_col1 = ((t._col0.yxxx() * tmp_c5543) + (t._col0.zzyy() * tmp_c4221) + (t._col0.wwwz() * tmp_c3100)) * invdet;
563 auto tmp_s5543 = neg<0b1010>(s45__.yyx0() + s0123._000w());
564 auto tmp_s4221 = neg<0b0101>(s45__.x000() + s0123._0zzy());
565 auto tmp_s3100 = neg<0b1010>(s0123.wyxx());
566 hilet inv_col2 = ((t._col3.yxxx() * tmp_s5543) + (t._col3.zzyy() * tmp_s4221) + (t._col3.wwwz() * tmp_s3100)) * invdet;
572 tmp_s5543 = -tmp_s5543;
573 tmp_s4221 = -tmp_s4221;
574 tmp_s3100 = -tmp_s3100;
575 hilet inv_col3 = ((t._col2.yxxx() * tmp_s5543) + (t._col2.zzyy() * tmp_s4221) + (t._col2.wwwz() * tmp_s3100)) * invdet;
577 return {inv_col0, inv_col1, inv_col2, inv_col3};
587 [[nodiscard]]
static constexpr f32x4 reflect_column() noexcept
589 if constexpr (Axis ==
'x') {
590 return f32x4{1.0f, 0.0f, 0.0f, 0.0f};
591 }
else if constexpr (Axis ==
'X') {
592 return f32x4{-1.0f, 0.0f, 0.0f, 0.0f};
593 }
else if constexpr (Axis ==
'y') {
594 return f32x4{0.0f, 1.0f, 0.0f, 0.0f};
595 }
else if constexpr (Axis ==
'Y') {
596 return f32x4{0.0f, -1.0f, 0.0f, 0.0f};
597 }
else if constexpr (Axis ==
'z') {
598 return f32x4{0.0f, 0.0f, 1.0f, 0.0f};
599 }
else if constexpr (Axis ==
'Z') {
600 return f32x4{0.0f, 0.0f, -1.0f, 0.0f};
601 }
else if constexpr (Axis ==
'w') {
602 return f32x4{0.0f, 0.0f, 0.0f, 1.0f};
603 }
else if constexpr (Axis ==
'W') {
604 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:181
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
Defined the geo::extent, extent2 and extent3 types.
Defined the corner_radii type.
@ other
The gui_event does not have associated data.
@ rectangle
The gui_event has rectangle data.
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
geo::matrix< 2 > matrix2
A 2D homogenious transformation matrix.
Definition matrix.hpp:615
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:28
A type defining a 2D circle.
Definition circle.hpp:19
The 4 radiuses of the corners of a quad or rectangle.
Definition corner_radii.hpp:17
A high-level geometric extent.
Definition extent.hpp:31
Line segment.
Definition line_segment.hpp:19
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix.hpp:34
friend constexpr f32x4 const & get(matrix const &rhs) noexcept
Get a column.
Definition matrix.hpp:207
friend constexpr matrix reflect(matrix const &rhs) noexcept
Reflect axis of a matrix.
Definition matrix.hpp:452
constexpr bool operator==(matrix< E > const &rhs) const noexcept
Compare two matrices potentially of different dimensions.
Definition matrix.hpp:464
constexpr matrix(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 matrix.hpp:98
constexpr float operator*(float const &rhs) const noexcept
Transform a float by the scaling factor of the matrix.
Definition matrix.hpp:263
constexpr auto operator*(color const &rhs) const noexcept
Transform a color by a color matrix.
Definition matrix.hpp:391
constexpr matrix(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 matrix.hpp:61
constexpr auto operator*(vector< float, E > const &rhs) const noexcept
Transform a vector by the matrix.
Definition matrix.hpp:290
constexpr matrix operator~() const
Invert matrix.
Definition matrix.hpp:471
static constexpr matrix 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 scale.hpp:211
constexpr auto operator*(matrix const &rhs) const noexcept
Matrix/Matrix multiplication.
Definition matrix.hpp:404
constexpr rectangle operator*(rectangle const &rhs) const noexcept
Transform a rectangle by the matrix.
Definition matrix.hpp:348
constexpr rectangle operator*(aarectangle const &rhs) const noexcept
Transform an axis-aligned rectangle by the matrix.
Definition matrix.hpp:337
friend constexpr f32x4 & get(matrix &rhs) noexcept
Get a column.
Definition matrix.hpp:228
constexpr corner_radii operator*(corner_radii const &rhs) const noexcept
Transform a float by the scaling factor of the matrix.
Definition matrix.hpp:277
constexpr matrix(matrix< E > const &other) noexcept
Copy-construct a matrix from a smaller matrix.
Definition matrix.hpp:163
constexpr circle operator*(circle const &rhs) const noexcept
Transform a circle by the matrix.
Definition matrix.hpp:368
constexpr matrix(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 matrix.hpp:136
constexpr auto operator*(point< float, E > const &rhs) const noexcept
Transform a point by the matrix.
Definition matrix.hpp:320
constexpr auto operator*(extent< float, E > const &rhs) const noexcept
Transform a extent by the matrix.
Definition matrix.hpp:306
constexpr matrix() noexcept
Constructs an identity matrix.
Definition matrix.hpp:45
constexpr f32x4 operator*(f32x4 const &rhs) const noexcept
Transform a f32x4 numeric array by the matrix.
Definition matrix.hpp:253
constexpr line_segment operator*(line_segment const &rhs) const noexcept
Transform a line-segment by the matrix.
Definition matrix.hpp:378
friend constexpr matrix transpose(matrix const &rhs) noexcept
Matrix transpose.
Definition matrix.hpp:411
constexpr quad operator*(quad const &rhs) const noexcept
Transform a quad by the matrix.
Definition matrix.hpp:358
constexpr matrix(vector3 col0, vector3 col1, vector3 col2, vector3 col3=vector3{}) noexcept
Construct a matrix from four vectors.
Definition matrix.hpp:73