sandbox
Loading...
Searching...
No Matches
matrix4x4.hpp
1#ifndef LIBSBX_MATH_MATRIX4X4_HPP_
2#define LIBSBX_MATH_MATRIX4X4_HPP_
3
4#include <array>
5#include <cstddef>
6#include <cmath>
7#include <cinttypes>
8#include <concepts>
9#include <fstream>
10#include <ostream>
11#include <type_traits>
12
13#include <fmt/format.h>
14
15#include <libsbx/math/concepts.hpp>
16#include <libsbx/math/vector3.hpp>
17#include <libsbx/math/vector4.hpp>
18#include <libsbx/math/matrix.hpp>
19#include <libsbx/math/angle.hpp>
20
21namespace sbx::math {
22
23template<scalar Type>
24class basic_matrix4x4 : public basic_matrix<4u, 4u, Type> {
25
27
28 template<scalar Other>
30
31 inline static constexpr auto x_axis = std::size_t{0u};
32 inline static constexpr auto y_axis = std::size_t{1u};
33 inline static constexpr auto z_axis = std::size_t{2u};
34 inline static constexpr auto w_axis = std::size_t{3u};
35
36public:
37
38 using value_type = base_type::value_type;
39 using reference = base_type::reference;
40 using const_reference = base_type::const_reference;
41 using size_type = base_type::size_type;
43
44 inline static constexpr basic_matrix4x4 identity{base_type::identity()};
45
46 inline static constexpr basic_matrix4x4 zero{base_type{value_type{0}}};
47
48 using base_type::base_type;
49
50 constexpr basic_matrix4x4(const base_type& base) noexcept;
51
52 template<scalar Other>
53 constexpr basic_matrix4x4(
54 const column_type_for<Other>& column0,
55 const column_type_for<Other>& column1,
56 const column_type_for<Other>& column2,
57 const column_type_for<Other>& column3
58 ) noexcept;
59
60 template<scalar Other>
61 constexpr basic_matrix4x4(
62 Other x0, Other x1, Other x2, Other x3,
63 Other y0, Other y1, Other y2, Other y3,
64 Other z0, Other z1, Other z2, Other z3,
65 Other w0, Other w1, Other w2, Other w3
66 ) noexcept;
67
68// // -- Static member functions --
69
70 [[nodiscard]] constexpr static auto transposed(const basic_matrix4x4& matrix) noexcept -> basic_matrix4x4;
71
72 [[nodiscard]] constexpr static auto inverted(const basic_matrix4x4& matrix) -> basic_matrix4x4;
73
74 [[nodiscard]] constexpr static auto look_at(const basic_vector3<value_type>& position, const basic_vector3<value_type>& target, const basic_vector3<value_type>& up) noexcept -> basic_matrix4x4;
75
76 [[nodiscard]] constexpr static auto perspective(const basic_angle<value_type>& fov, const value_type aspect, const value_type near, const value_type far) noexcept -> basic_matrix4x4;
77
78 [[nodiscard]] constexpr static auto orthographic(const value_type left, const value_type right, const value_type bottom, const value_type top) noexcept -> basic_matrix4x4;
79
80 [[nodiscard]] constexpr static auto orthographic(const value_type left, const value_type right, const value_type bottom, const value_type top, const value_type near, const value_type far) noexcept -> basic_matrix4x4;
81
82 [[nodiscard]] constexpr static auto translated(const basic_matrix4x4& matrix, const basic_vector3<value_type>& vector) noexcept -> basic_matrix4x4;
83
84 [[nodiscard]] constexpr static auto scaled(const basic_matrix4x4& matrix, const basic_vector3<value_type>& vector) noexcept -> basic_matrix4x4;
85
86 [[nodiscard]] constexpr static auto rotated(const basic_matrix4x4& matrix, const basic_vector3<value_type>& axis, const basic_angle<value_type>& angle) noexcept -> basic_matrix4x4;
87
88 [[nodiscard]] constexpr static auto rotation_from_euler_angles(const basic_vector3<value_type>& euler_angles) noexcept -> basic_matrix4x4;
89
90 constexpr auto operator[](size_type index) const noexcept -> const column_type&;
91
92 constexpr auto operator[](size_type index) noexcept -> column_type&;
93
94}; // class basic_matrix4x4
95
96template<scalar Lhs, scalar Rhs>
97[[nodiscard]] constexpr auto operator+(basic_matrix4x4<Lhs> lhs, const basic_matrix4x4<Rhs>& rhs) noexcept -> basic_matrix4x4<Lhs>;
98
99template<scalar Lhs, scalar Rhs>
100[[nodiscard]] constexpr auto operator-(basic_matrix4x4<Lhs> lhs, const basic_matrix4x4<Rhs>& rhs) noexcept -> basic_matrix4x4<Lhs>;
101
102template<scalar Lhs, scalar Rhs>
103[[nodiscard]] constexpr auto operator*(basic_matrix4x4<Lhs> lhs, Rhs scalar) noexcept -> basic_matrix4x4<Lhs>;
104
105template<scalar Lhs, scalar Rhs>
106[[nodiscard]] constexpr auto operator*(basic_matrix4x4<Lhs> lhs, const basic_vector4<Rhs>& rhs) noexcept -> basic_vector4<Lhs>;
107
108template<scalar Lhs, scalar Rhs>
109[[nodiscard]] constexpr auto operator*(basic_matrix4x4<Lhs> lhs, const basic_matrix4x4<Rhs>& rhs) noexcept -> basic_matrix4x4<Lhs>;
110
111template<scalar Lhs, scalar Rhs>
112[[nodiscard]] constexpr auto operator/(basic_matrix4x4<Lhs> lhs, Rhs scalar) noexcept -> basic_matrix4x4<Lhs>;
113
115
117
118using matrix4x4 = matrix4x4f;
119
120} // namespace sbx::math
121
122template<sbx::math::scalar Type>
123struct fmt::formatter<sbx::math::basic_matrix4x4<Type>> {
124
125 template<typename ParseContext>
126 constexpr auto parse(ParseContext& context) -> decltype(context.begin()) {
127 return context.begin();
128 }
129
130 template<typename FormatContext>
131 auto format(const sbx::math::basic_matrix4x4<Type>& matrix, FormatContext& context) -> decltype(context.out()) {
132 if constexpr (sbx::math::is_floating_point_v<Type>) {
133 return fmt::format_to(context.out(),
134 "\n{:.2f}, {:.2f}, {:.2f}, {:.2f}\n{:.2f}, {:.2f}, {:.2f}, {:.2f}\n{:.2f}, {:.2f}, {:.2f}, {:.2f}\n{:.2f}, {:.2f}, {:.2f}, {:.2f}",
135 matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
136 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
137 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
138 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]
139 );
140 } else {
141 return fmt::format_to(context.out(),
142 "\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}",
143 matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
144 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
145 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
146 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]
147 );
148 }
149 }
150
151}; // struct fmt::formatter
152
153#include <libsbx/math/matrix4x4.ipp>
154
155#endif // LIBSBX_MATH_MATRIX4X4_HPP_
Definition: angle.hpp:251
Definition: matrix4x4.hpp:24
Definition: matrix.hpp:13
Definition: vector3.hpp:22
Definition: vector4.hpp:22