1#ifndef LIBSBX_MATH_MATRIX_CAST_HPP_
2#define LIBSBX_MATH_MATRIX_CAST_HPP_
8#include <libsbx/math/fwd.hpp>
9#include <libsbx/math/concepts.hpp>
10#include <libsbx/math/matrix.hpp>
11#include <libsbx/math/matrix3x3.hpp>
12#include <libsbx/math/matrix4x4.hpp>
13#include <libsbx/math/quaternion.hpp>
17template<
typename Type,
typename Return,
typename... Args>
18concept dispatcher_for =
requires() {
19 { std::remove_cvref_t<Type>::invoke(std::declval<Args>()...) } -> std::same_as<Return>;
22template<
typename Type,
typename Fallback,
typename =
void>
27template<
typename Type,
typename Fallback>
28struct value_type_or<Type, Fallback, std::void_t<typename std::remove_cvref_t<Type>::value_type>> {
29 using type =
typename std::remove_cvref_t<Type>::value_type;
32template<
typename Type,
typename Fallback>
33using value_type_or_t =
typename value_type_or<Type, Fallback>::type;
35template<
typename Type>
36using value_type_t = value_type_or_t<Type, std::float_t>;
44 static_cast<Type
>(matrix[0][0]),
static_cast<Type
>(matrix[1][0]),
static_cast<Type
>(matrix[2][0]),
45 static_cast<Type
>(matrix[0][1]),
static_cast<Type
>(matrix[1][1]),
static_cast<Type
>(matrix[2][1]),
46 static_cast<Type
>(matrix[0][2]),
static_cast<Type
>(matrix[1][2]),
static_cast<Type
>(matrix[2][2])
55 static_cast<Type
>(matrix[0][0]),
static_cast<Type
>(matrix[1][0]),
static_cast<Type
>(matrix[2][0]),
static_cast<Type
>(0),
56 static_cast<Type
>(matrix[0][1]),
static_cast<Type
>(matrix[1][1]),
static_cast<Type
>(matrix[2][1]),
static_cast<Type
>(0),
57 static_cast<Type
>(matrix[0][2]),
static_cast<Type
>(matrix[1][2]),
static_cast<Type
>(matrix[2][2]),
static_cast<Type
>(0),
58 static_cast<Type
>(0),
static_cast<Type
>(0),
static_cast<Type
>(0),
static_cast<Type
>(1),
78 matrix[0][0] = 1.0f - 2.0f * (yy + zz);
79 matrix[0][1] = 2.0f * (xy + wz);
80 matrix[0][2] = 2.0f * (xz - wy);
82 matrix[1][0] = 2.0f * (xy - wz);
83 matrix[1][1] = 1.0f - 2.0f * (xx + zz);
84 matrix[1][2] = 2.0f * (yz + wx);
86 matrix[2][0] = 2.0f * (xz + wy);
87 matrix[2][1] = 2.0f * (yz - wx);
88 matrix[2][2] = 1.0f - 2.0f * (xx + yy);
113template<std::
size_t Columns, std::
size_t Rows,
typename From>
114requires (dispatcher_for<detail::matrix_cast_impl<Columns, Rows, std::remove_cvref_t<From>>, concrete_matrix_t<Columns, Rows, value_type_t<From>>, From>)
115[[nodiscard]]
constexpr auto matrix_cast(
const From& from) -> concrete_matrix_t<Columns, Rows, value_type_t<From>> {
129 result.position = vector3{matrix[3][0], matrix[3][1], matrix[3][2]};
132 result.scale.x() = vector3{matrix[0][0], matrix[0][1], matrix[0][2]}.length();
133 result.scale.y() = vector3{matrix[1][0], matrix[1][1], matrix[1][2]}.length();
134 result.scale.z() = vector3{matrix[2][0], matrix[2][1], matrix[2][2]}.length();
137 auto rotation_matrix = matrix4x4{matrix};
138 rotation_matrix[0][0] /= result.scale.x();
139 rotation_matrix[0][1] /= result.scale.x();
140 rotation_matrix[0][2] /= result.scale.x();
142 rotation_matrix[1][0] /= result.scale.y();
143 rotation_matrix[1][1] /= result.scale.y();
144 rotation_matrix[1][2] /= result.scale.y();
146 rotation_matrix[2][0] /= result.scale.z();
147 rotation_matrix[2][1] /= result.scale.z();
148 rotation_matrix[2][2] /= result.scale.z();
150 result.rotation = quaternion{rotation_matrix};
Definition: matrix3x3.hpp:25
Definition: matrix4x4.hpp:26
Definition: matrix.hpp:13
Definition: quaternion.hpp:24
Definition: vector3.hpp:22
Definition: matrix_cast.hpp:119
Definition: matrix_cast.hpp:23