sandbox
Loading...
Searching...
No Matches
vector3.hpp
1#ifndef LIBSBX_MATH_VECTOR3_HPP_
2#define LIBSBX_MATH_VECTOR3_HPP_
3
4#include <concepts>
5#include <cstddef>
6#include <cmath>
7#include <fstream>
8#include <ostream>
9#include <type_traits>
10
11#include <yaml-cpp/yaml.h>
12
13#include <fmt/format.h>
14
15#include <libsbx/math/concepts.hpp>
16#include <libsbx/math/vector.hpp>
17#include <libsbx/math/vector2.hpp>
18
19namespace sbx::math {
20
21template<scalar Type>
22class basic_vector3 : public basic_vector<3u, Type> {
23
25
26 inline static constexpr auto x_axis = std::size_t{0u};
27 inline static constexpr auto y_axis = std::size_t{1u};
28 inline static constexpr auto z_axis = std::size_t{2u};
29
30public:
31
32 using value_type = base_type::value_type;
33 using reference = base_type::reference;
34 using const_reference = base_type::const_reference;
35 using size_type = base_type::size_type;
36 using length_type = base_type::length_type;
37
38 inline static constexpr basic_vector3 zero{base_type::fill(value_type{0})};
39 inline static constexpr basic_vector3 one{base_type::fill(value_type{1})};
40 inline static constexpr basic_vector3 right{base_type::template axis<x_axis>(value_type{1})};
41 inline static constexpr basic_vector3 left{base_type::template axis<x_axis>(value_type{-1})};
42 inline static constexpr basic_vector3 up{base_type::template axis<y_axis>(value_type{1})};
43 inline static constexpr basic_vector3 down{base_type::template axis<y_axis>(value_type{-1})};
44 inline static constexpr basic_vector3 forward{base_type::template axis<z_axis>(value_type{-1})};
45 inline static constexpr basic_vector3 backward{base_type::template axis<z_axis>(value_type{1})};
46
47 using base_type::base_type;
48
49 constexpr basic_vector3(const base_type& base) noexcept;
50
51 template<scalar X, scalar Y, scalar Z>
52 constexpr basic_vector3(X x, Y y, Z z) noexcept;
53
54 template<scalar Other, scalar Scalar = Other>
55 constexpr basic_vector3(const basic_vector2<Other>& vector, Scalar z = Scalar{0}) noexcept;
56
57 [[nodiscard]] static constexpr auto cross(const basic_vector3& lhs, const basic_vector3& rhs) noexcept -> basic_vector3;
58
59 [[nodiscard]] static constexpr auto dot(const basic_vector3& lhs, const basic_vector3& rhs) noexcept -> length_type;
60
61 [[nodiscard]] static constexpr auto normalized(const basic_vector3& vector) noexcept -> basic_vector3;
62
63 [[nodiscard]] static constexpr auto reflect(const basic_vector3& vector, const basic_vector3& normal) noexcept -> basic_vector3;
64
65 [[nodiscard]] static constexpr auto abs(const basic_vector3& vector) noexcept -> basic_vector3;
66
67 [[nodiscard]] constexpr operator basic_vector2<Type>() const noexcept;
68
69 [[nodiscard]] constexpr auto x() noexcept -> reference;
70
71 [[nodiscard]] constexpr auto x() const noexcept -> const_reference;
72
73 [[nodiscard]] constexpr auto y() noexcept -> reference;
74
75 [[nodiscard]] constexpr auto y() const noexcept -> const_reference;
76
77 [[nodiscard]] constexpr auto z() noexcept -> reference;
78
79 [[nodiscard]] constexpr auto z() const noexcept -> const_reference;
80
81 constexpr auto normalize() noexcept -> basic_vector3&;
82
83}; // template<scalar Type>
84
85template<scalar Lhs, scalar Rhs>
86[[nodiscard]] constexpr auto operator+(basic_vector3<Lhs> lhs, const basic_vector3<Rhs>& rhs) noexcept -> basic_vector3<Lhs>;
87
88template<scalar Lhs, scalar Rhs>
89[[nodiscard]] constexpr auto operator-(basic_vector3<Lhs> lhs, const basic_vector3<Rhs>& rhs) noexcept -> basic_vector3<Lhs>;
90
91template<scalar Type>
92[[nodiscard]] constexpr auto operator-(basic_vector3<Type> vector) noexcept -> basic_vector3<Type>;
93
94template<scalar Lhs, scalar Rhs>
95[[nodiscard]] constexpr auto operator*(basic_vector3<Lhs> lhs, Rhs scalar) noexcept -> basic_vector3<Lhs>;
96
97template<scalar Lhs, std::convertible_to<Lhs> Rhs>
98requires (!is_scalar_v<Rhs>)
99[[nodiscard]] constexpr auto operator*(basic_vector3<Lhs> lhs, const Rhs& rhs) noexcept -> basic_vector3<Lhs>;
100
101template<scalar Lhs, scalar Rhs>
102[[nodiscard]] constexpr auto operator/(basic_vector3<Lhs> lhs, Rhs scalar) noexcept -> basic_vector3<Lhs>;
103
104template<scalar Lhs, std::convertible_to<Lhs> Rhs>
105requires (!is_scalar_v<Rhs>)
106[[nodiscard]] constexpr auto operator/(basic_vector3<Lhs> lhs, const Rhs& rhs) noexcept -> basic_vector3<Lhs>;
107
109
111
113
114using vector3 = vector3f;
115
116} // namespace sbx::math
117
118template<sbx::math::scalar Type>
119struct std::hash<sbx::math::basic_vector3<Type>> {
120
121 inline auto operator()(const sbx::math::basic_vector3<Type>& vector) const noexcept -> std::size_t;
122
123}; // struct std::hash<sbx::math::basic_vector3<Type>>
124
125template<sbx::math::scalar Type>
126struct fmt::formatter<sbx::math::basic_vector3<Type>> {
127
128 template<typename ParseContext>
129 constexpr auto parse(ParseContext& context) noexcept -> decltype(context.begin());
130
131 template<typename FormatContext>
132 auto format(const sbx::math::basic_vector3<Type>& vector, FormatContext& context) noexcept -> decltype(context.out());
133
134}; // struct fmt::formatter<sbx::math::basic_vector3<Type>>
135
136template<sbx::math::scalar Type>
137struct YAML::convert<sbx::math::basic_vector3<Type>> {
138
139 static auto encode(const sbx::math::basic_vector3<Type>& rhs) -> YAML::Node;
140
141 static auto decode(const YAML::Node& node, sbx::math::basic_vector3<Type>& rhs) -> bool;
142
143}; // struct YAML::convert<sbx::math::basic_vector3<Type>>
144
145#include <libsbx/math/vector3.ipp>
146
147#endif // LIBSBX_MATH_VECTOR3_HPP_
148
A vector in two-dimensional space.
Definition: vector2.hpp:27
Definition: vector3.hpp:22
Definition: vector.hpp:19