sandbox
Loading...
Searching...
No Matches
quaternion.hpp
1#ifndef LIBSBX_MATH_QUATERNION_HPP_
2#define LIBSBX_MATH_QUATERNION_HPP_
3
4#include <cstddef>
5#include <concepts>
6#include <cmath>
7#include <type_traits>
8
9#include <yaml-cpp/yaml.h>
10
11#include <fmt/format.h>
12
13#include <libsbx/math/concepts.hpp>
14#include <libsbx/math/vector3.hpp>
15#include <libsbx/math/vector4.hpp>
16#include <libsbx/math/matrix4x4.hpp>
17#include <libsbx/math/angle.hpp>
18
19namespace sbx::math {
20
21template<floating_point Type>
23
24 template<floating_point Other>
26
27 template<floating_point Other>
29
30 template<floating_point Other>
32
33 template<floating_point Other>
35
36public:
37
38 using value_type = Type;
39 using reference = value_type&;
40 using const_reference = const value_type&;
41 using size_type = std::size_t;
42 using length_type = std::float_t;
46
47 inline static constexpr basic_quaternion identity{vector_type::zero, value_type{1}};
48
49 template<floating_point Other = value_type>
50 constexpr basic_quaternion(Other value = Other{0}) noexcept;
51
52 template<floating_point Complex = value_type, floating_point Scalar = value_type>
53 constexpr basic_quaternion(const vector_type_for<Complex>& complex, Scalar scalar) noexcept;
54
55 template<floating_point Other = value_type>
56 constexpr basic_quaternion(const vector_type_for<Other>& euler_angles) noexcept;
57
58 template<floating_point Other = value_type>
59 constexpr basic_quaternion(Other x, Other y, Other z, Other w) noexcept;
60
61 template<floating_point Complex = value_type, floating_point Scalar = value_type>
62 constexpr basic_quaternion(const vector_type_for<Complex>& axis, const basic_angle<Scalar>& angle) noexcept;
63
64 template<floating_point Other = value_type>
65 constexpr basic_quaternion(const matrix_type_for<Other>& matrix) noexcept;
66
67 [[nodiscard]] constexpr operator matrix_type() const noexcept;
68
69 [[nodiscard]] constexpr auto to_matrix() const noexcept -> matrix_type;
70
71 template<floating_point Other = value_type>
72 constexpr auto operator+=(const basic_quaternion<Other>& other) noexcept -> basic_quaternion&;
73
74 template<floating_point Other = value_type>
75 constexpr auto operator-=(const basic_quaternion<Other>& other) noexcept -> basic_quaternion&;
76
77 template<floating_point Other = value_type>
78 constexpr auto operator*=(Other value) noexcept -> basic_quaternion&;
79
80 template<floating_point Other = value_type>
81 constexpr auto operator*=(const basic_quaternion<Other>& other) noexcept -> basic_quaternion&;
82
83 template<floating_point Other = value_type>
84 constexpr auto operator/=(Other value) noexcept -> basic_quaternion&;
85
86 [[nodiscard]] constexpr auto x() noexcept -> reference;
87
88 [[nodiscard]] constexpr auto x() const noexcept -> const_reference;
89
90 [[nodiscard]] constexpr auto y() noexcept -> reference;
91
92 [[nodiscard]] constexpr auto y() const noexcept -> const_reference;
93
94 [[nodiscard]] constexpr auto z() noexcept -> reference;
95
96 [[nodiscard]] constexpr auto z() const noexcept -> const_reference;
97
98 [[nodiscard]] constexpr auto w() noexcept -> reference;
99
100 [[nodiscard]] constexpr auto w() const noexcept -> const_reference;
101
102 [[nodiscard]] constexpr auto complex() noexcept -> vector_type&;
103
104 [[nodiscard]] constexpr auto complex() const noexcept -> const vector_type&;
105
106 [[nodiscard]] constexpr auto scalar() noexcept -> reference;
107
108 [[nodiscard]] constexpr auto scalar() const noexcept -> const_reference;
109
110 [[nodiscard]] constexpr auto length_squared() const noexcept -> length_type;
111
112 [[nodiscard]] constexpr auto length() const noexcept -> length_type;
113
114 constexpr auto normalize() noexcept -> basic_quaternion&;
115
116private:
117
118 vector_type _complex;
119 value_type _scalar;
120
121}; // class basic_quaternion
122
123template<floating_point Lhs, floating_point Rhs>
124[[nodiscard]] constexpr auto operator==(const basic_quaternion<Lhs>& lhs, const basic_quaternion<Rhs>& rhs) noexcept -> bool;
125
126template<floating_point Lhs, floating_point Rhs>
127[[nodiscard]] constexpr auto operator+(basic_quaternion<Lhs> lhs, const basic_quaternion<Rhs>& rhs) noexcept -> basic_quaternion<Lhs>;
128
129template<floating_point Lhs, floating_point Rhs>
130[[nodiscard]] constexpr auto operator-(basic_quaternion<Lhs> lhs, const basic_quaternion<Rhs>& rhs) noexcept -> basic_quaternion<Lhs>;
131
132template<floating_point Type>
133[[nodiscard]] constexpr auto operator-(basic_quaternion<Type> quaternion) noexcept -> basic_quaternion<Type>;
134
135template<floating_point Lhs, floating_point Rhs>
136[[nodiscard]] constexpr auto operator*(basic_quaternion<Lhs> lhs, Rhs scalar) noexcept -> basic_quaternion<Lhs>;
137
138template<floating_point Lhs, floating_point Rhs>
139[[nodiscard]] constexpr auto operator*(basic_quaternion<Lhs> lhs, const basic_quaternion<Rhs>& rhs) noexcept -> basic_quaternion<Lhs>;
140
141template<floating_point Lhs, floating_point Rhs>
142[[nodiscard]] constexpr auto operator/(basic_quaternion<Lhs> lhs, Rhs scalar) noexcept -> basic_quaternion<Lhs>;
143
146
148using quaternion = quaternionf;
149
150} // namespace sbx::math
151
152template<sbx::math::floating_point Type>
153struct std::hash<sbx::math::basic_quaternion<Type>> {
154
155 auto operator()(const sbx::math::basic_quaternion<Type>& quaternion) const noexcept -> std::size_t;
156
157}; // struct std::hash
158
159template<sbx::math::floating_point Type>
160struct YAML::convert<sbx::math::basic_quaternion<Type>> {
161
162 static auto encode(const sbx::math::basic_quaternion<Type>& quaternion) -> YAML::Node;
163
164 static auto decode(const YAML::Node& node, sbx::math::basic_quaternion<Type>& quaternion) -> bool;
165
166}; // struct YAML::convert
167
168template<sbx::math::floating_point Type>
169struct fmt::formatter<sbx::math::basic_quaternion<Type>> {
170
171 template<typename ParseContext>
172 constexpr auto parse(ParseContext& context) -> decltype(context.begin());
173
174 template<typename FormatContext>
175 auto format(const sbx::math::basic_quaternion<Type>& quaternion, FormatContext& context) -> decltype(context.out());
176
177}; // struct fmt::formatter
178
179#include <libsbx/math/quaternion.ipp>
180
181#endif // LIBSBX_MATH_QUATERNION_HPP_
Definition: angle.hpp:251
Definition: matrix4x4.hpp:24
Definition: quaternion.hpp:22
Definition: vector3.hpp:22