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]] static constexpr auto distance(const basic_vector3& lhs, const basic_vector3& rhs) noexcept -> value_type;
68
69 [[nodiscard]] static constexpr auto splat_x(const basic_vector3& vector) noexcept -> basic_vector3 {
70 return base_type::template splat<x_axis>(vector);
71 }
72
73 [[nodiscard]] static constexpr auto splat_y(const basic_vector3& vector) noexcept -> basic_vector3 {
74 return base_type::template splat<y_axis>(vector);
75 }
76
77 [[nodiscard]] static constexpr auto splat_z(const basic_vector3& vector) noexcept -> basic_vector3 {
78 return base_type::template splat<z_axis>(vector);
79 }
80
81 // /**
82 // * @brief Linearly interpolates between two vectors.
83 // *
84 // * @param start The starting vector.
85 // * @param end The ending vector.
86 // * @param t The interpolation factor [0.0f, 1.0f].
87 // *
88 // * @return A new vector that is the result of the linear interpolation.
89 // */
90 // [[nodiscard]] static constexpr auto lerp(const basic_vector3& start, const basic_vector3& end, const value_type t) noexcept -> basic_vector3 {
91 // utility::assert_that(t >= 0.0f && t <= 1.0f, "Interpolation factor out of bounds in vector3 lerp");
92 // return start * (1.0f - t) + end * t;
93 // }
94
95 [[nodiscard]] constexpr operator basic_vector2<Type>() const noexcept;
96
97 [[nodiscard]] constexpr auto x() noexcept -> reference;
98
99 [[nodiscard]] constexpr auto x() const noexcept -> const_reference;
100
101 [[nodiscard]] constexpr auto y() noexcept -> reference;
102
103 [[nodiscard]] constexpr auto y() const noexcept -> const_reference;
104
105 [[nodiscard]] constexpr auto z() noexcept -> reference;
106
107 [[nodiscard]] constexpr auto z() const noexcept -> const_reference;
108
109 constexpr auto normalize() noexcept -> basic_vector3&;
110
111}; // template<scalar Type>
112
113template<scalar Lhs, scalar Rhs>
114[[nodiscard]] constexpr auto operator+(basic_vector3<Lhs> lhs, const basic_vector3<Rhs>& rhs) noexcept -> basic_vector3<Lhs>;
115
116template<scalar Lhs, scalar Rhs>
117[[nodiscard]] constexpr auto operator-(basic_vector3<Lhs> lhs, const basic_vector3<Rhs>& rhs) noexcept -> basic_vector3<Lhs>;
118
119template<scalar Type>
120[[nodiscard]] constexpr auto operator-(basic_vector3<Type> vector) noexcept -> basic_vector3<Type>;
121
122template<scalar Lhs, scalar Rhs>
123[[nodiscard]] constexpr auto operator*(basic_vector3<Lhs> lhs, Rhs scalar) noexcept -> basic_vector3<Lhs>;
124
125template<scalar Lhs, scalar Rhs>
126[[nodiscard]] constexpr auto operator*(Lhs scalar, basic_vector3<Rhs> rhs) noexcept -> basic_vector3<Rhs>;
127
128template<scalar Lhs, std::convertible_to<Lhs> Rhs>
129requires (!is_scalar_v<Rhs>)
130[[nodiscard]] constexpr auto operator*(basic_vector3<Lhs> lhs, const Rhs& rhs) noexcept -> basic_vector3<Lhs>;
131
132template<scalar Lhs, scalar Rhs>
133[[nodiscard]] constexpr auto operator*(basic_vector3<Lhs> lhs, const basic_vector3<Rhs>& rhs) noexcept -> basic_vector3<Lhs>;
134
135template<scalar Lhs, scalar Rhs>
136[[nodiscard]] constexpr auto operator/(basic_vector3<Lhs> lhs, Rhs scalar) noexcept -> basic_vector3<Lhs>;
137
138template<scalar Lhs, std::convertible_to<Lhs> Rhs>
139requires (!is_scalar_v<Rhs>)
140[[nodiscard]] constexpr auto operator/(basic_vector3<Lhs> lhs, const Rhs& rhs) noexcept -> basic_vector3<Lhs>;
141
143
145
147
148using vector3 = vector3f;
149
150} // namespace sbx::math
151
152template<sbx::math::scalar Type>
153struct std::hash<sbx::math::basic_vector3<Type>> {
154
155 inline auto operator()(const sbx::math::basic_vector3<Type>& vector) const noexcept -> std::size_t;
156
157}; // struct std::hash<sbx::math::basic_vector3<Type>>
158
159template<sbx::math::scalar Type>
160struct fmt::formatter<sbx::math::basic_vector3<Type>> {
161
162 template<typename ParseContext>
163 constexpr auto parse(ParseContext& context) noexcept -> decltype(context.begin());
164
165 template<typename FormatContext>
166 auto format(const sbx::math::basic_vector3<Type>& vector, FormatContext& context) noexcept -> decltype(context.out());
167
168}; // struct fmt::formatter<sbx::math::basic_vector3<Type>>
169
170template<sbx::math::scalar Type>
171struct YAML::convert<sbx::math::basic_vector3<Type>> {
172
173 static auto encode(const sbx::math::basic_vector3<Type>& rhs) -> YAML::Node;
174
175 static auto decode(const YAML::Node& node, sbx::math::basic_vector3<Type>& rhs) -> bool;
176
177}; // struct YAML::convert<sbx::math::basic_vector3<Type>>
178
179#include <libsbx/math/vector3.ipp>
180
181#endif // LIBSBX_MATH_VECTOR3_HPP_
182
Definition: tests.cpp:5
A vector in two-dimensional space.
Definition: vector2.hpp:27
Definition: vector3.hpp:22
Definition: vector.hpp:22