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/matrix3x3.hpp>
20#include <libsbx/math/angle.hpp>
21
22namespace sbx::math {
23
24template<scalar Type>
25class basic_matrix4x4 : public basic_matrix<4u, 4u, Type> {
26
28
29 template<scalar Other>
31
32 inline static constexpr auto x_axis = std::size_t{0u};
33 inline static constexpr auto y_axis = std::size_t{1u};
34 inline static constexpr auto z_axis = std::size_t{2u};
35 inline static constexpr auto w_axis = std::size_t{3u};
36
37public:
38
39 using value_type = base_type::value_type;
40 using reference = base_type::reference;
41 using const_reference = base_type::const_reference;
42 using size_type = base_type::size_type;
44
45 inline static constexpr basic_matrix4x4 identity{base_type::identity()};
46
47 inline static constexpr basic_matrix4x4 zero{base_type{value_type{0}}};
48
49 using base_type::base_type;
50
51 constexpr basic_matrix4x4(const base_type& base) noexcept;
52
53 template<scalar Other>
54 constexpr basic_matrix4x4(
55 const column_type_for<Other>& column0,
56 const column_type_for<Other>& column1,
57 const column_type_for<Other>& column2,
58 const column_type_for<Other>& column3
59 ) noexcept;
60
61 template<scalar Other>
62 constexpr basic_matrix4x4(
63 Other x0, Other x1, Other x2, Other x3,
64 Other y0, Other y1, Other y2, Other y3,
65 Other z0, Other z1, Other z2, Other z3,
66 Other w0, Other w1, Other w2, Other w3
67 ) noexcept;
68
69 template<scalar Other>
70 constexpr basic_matrix4x4(const Other v00, const Other v11, const Other v22, const Other v33) noexcept;
71
72 template<scalar Other>
73 constexpr basic_matrix4x4(const basic_matrix3x3<Other>& other) noexcept
74 : base_type{column_type{other[0], 0}, column_type{other[1], 0}, column_type{other[2], 0}, column_type{0, 0, 0, 1}} { }
75
76 constexpr basic_matrix4x4(const basic_matrix4x4& other) noexcept = default;
77
78 constexpr basic_matrix4x4(basic_matrix4x4&& other) noexcept = default;
79
80 auto operator=(const basic_matrix4x4& other) noexcept -> basic_matrix4x4& = default;
81
82 auto operator=(basic_matrix4x4&& other) noexcept -> basic_matrix4x4& = default;
83
84 // -- Static member functions --
85
86 [[nodiscard]] constexpr static auto transposed(const basic_matrix4x4& matrix) noexcept -> basic_matrix4x4;
87
88 [[nodiscard]] constexpr static auto inverted(const basic_matrix4x4& matrix) -> basic_matrix4x4;
89
90 [[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;
91
92 [[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;
93
94 [[nodiscard]] constexpr static auto orthographic(const value_type left, const value_type right, const value_type bottom, const value_type top) noexcept -> basic_matrix4x4;
95
96 [[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;
97
98 [[nodiscard]] constexpr static auto translated(const basic_matrix4x4& matrix, const basic_vector3<value_type>& vector) noexcept -> basic_matrix4x4;
99
100 [[nodiscard]] constexpr static auto scaled(const basic_matrix4x4& matrix, const basic_vector3<value_type>& vector) noexcept -> basic_matrix4x4;
101
102 [[nodiscard]] constexpr static auto rotated(const basic_matrix4x4& matrix, const basic_vector3<value_type>& axis, const basic_angle<value_type>& angle) noexcept -> basic_matrix4x4;
103
104 [[nodiscard]] constexpr static auto rotation_from_euler_angles(const basic_vector3<value_type>& euler_angles) noexcept -> basic_matrix4x4;
105
106 constexpr auto operator[](size_type index) const noexcept -> const column_type&;
107
108 constexpr auto operator[](size_type index) noexcept -> column_type&;
109
110}; // class basic_matrix4x4
111
112template<scalar Lhs, scalar Rhs>
113[[nodiscard]] constexpr auto operator+(basic_matrix4x4<Lhs> lhs, const basic_matrix4x4<Rhs>& rhs) noexcept -> basic_matrix4x4<Lhs>;
114
115template<scalar Lhs, scalar Rhs>
116[[nodiscard]] constexpr auto operator-(basic_matrix4x4<Lhs> lhs, const basic_matrix4x4<Rhs>& rhs) noexcept -> basic_matrix4x4<Lhs>;
117
118template<scalar Lhs, scalar Rhs>
119[[nodiscard]] constexpr auto operator*(basic_matrix4x4<Lhs> lhs, Rhs scalar) noexcept -> basic_matrix4x4<Lhs>;
120
121template<scalar Lhs, scalar Rhs>
122[[nodiscard]] constexpr auto operator*(basic_matrix4x4<Lhs> lhs, const basic_vector4<Rhs>& rhs) noexcept -> basic_vector4<Lhs>;
123
124template<scalar Lhs, scalar Rhs>
125[[nodiscard]] constexpr auto operator*(basic_matrix4x4<Lhs> lhs, const basic_matrix4x4<Rhs>& rhs) noexcept -> basic_matrix4x4<Lhs>;
126
127template<scalar Lhs, scalar Rhs>
128[[nodiscard]] constexpr auto operator/(basic_matrix4x4<Lhs> lhs, Rhs scalar) noexcept -> basic_matrix4x4<Lhs>;
129
131
133
134using matrix4x4 = matrix4x4f;
135
136} // namespace sbx::math
137
138template<sbx::math::scalar Type>
139struct fmt::formatter<sbx::math::basic_matrix4x4<Type>> {
140
141 template<typename ParseContext>
142 constexpr auto parse(ParseContext& context) -> decltype(context.begin()) {
143 return context.begin();
144 }
145
146 template<typename FormatContext>
147 auto format(const sbx::math::basic_matrix4x4<Type>& matrix, FormatContext& context) -> decltype(context.out()) {
148 if constexpr (sbx::math::is_floating_point_v<Type>) {
149 return fmt::format_to(context.out(),
150 "\n{:.2f}, {:.2f}, {:.2f}, {:.2f}\n{:.2f}, {:.2f}, {:.2f}, {:.2f}\n{:.2f}, {:.2f}, {:.2f}, {:.2f}\n{:.2f}, {:.2f}, {:.2f}, {:.2f}",
151 matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
152 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
153 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
154 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]
155 );
156 } else {
157 return fmt::format_to(context.out(),
158 "\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}",
159 matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
160 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
161 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
162 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]
163 );
164 }
165 }
166
167}; // struct fmt::formatter
168
169#include <libsbx/math/matrix4x4.ipp>
170
171#endif // LIBSBX_MATH_MATRIX4X4_HPP_
Definition: angle.hpp:271
Definition: matrix3x3.hpp:24
Definition: matrix4x4.hpp:25
Definition: matrix.hpp:13
Definition: vector3.hpp:22
Definition: vector4.hpp:22