2#ifndef LIBSBX_MATH_VECTOR3_HPP_
3#define LIBSBX_MATH_VECTOR3_HPP_
12#include <yaml-cpp/yaml.h>
14#include <fmt/format.h>
18#include <libsbx/math/vector2.hpp>
27 inline static constexpr auto x_axis = std::size_t{0u};
28 inline static constexpr auto y_axis = std::size_t{1u};
29 inline static constexpr auto z_axis = std::size_t{2u};
33 using value_type = base_type::value_type;
34 using reference = base_type::reference;
35 using const_reference = base_type::const_reference;
36 using size_type = base_type::size_type;
37 using length_type = base_type::length_type;
39 inline static constexpr basic_vector3 zero{base_type::fill(value_type{0})};
40 inline static constexpr basic_vector3 one{base_type::fill(value_type{1})};
41 inline static constexpr basic_vector3 right{base_type::template axis<x_axis>(value_type{1})};
42 inline static constexpr basic_vector3 left{base_type::template axis<x_axis>(value_type{-1})};
43 inline static constexpr basic_vector3 up{base_type::template axis<y_axis>(value_type{1})};
44 inline static constexpr basic_vector3 down{base_type::template axis<y_axis>(value_type{-1})};
45 inline static constexpr basic_vector3 forward{base_type::template axis<z_axis>(value_type{-1})};
46 inline static constexpr basic_vector3 backward{base_type::template axis<z_axis>(value_type{1})};
48 using base_type::base_type;
52 template<scalar X, scalar Y, scalar Z>
55 template<scalar Other, scalar Scalar = Other>
68 [[nodiscard]]
static constexpr auto distance_squared(
const basic_vector3& lhs,
const basic_vector3& rhs)
noexcept -> value_type;
74 return {1.0f, 0.0f, 0.0f};
77 const auto ax = std::abs(vector.x());
78 const auto ay = std::abs(vector.y());
79 const auto az = std::abs(vector.z());
84 auto orthogonal = cross(vector, other);
87 if (orthogonal.length_squared() < 1e-12f) {
89 orthogonal = cross(vector, other);
92 return normalized(orthogonal);
96 return base_type::template splat<x_axis>(vector);
100 return base_type::template splat<y_axis>(vector);
104 return base_type::template splat<z_axis>(vector);
123 [[nodiscard]]
constexpr auto x()
noexcept -> reference;
125 [[nodiscard]]
constexpr auto x()
const noexcept -> const_reference;
127 [[nodiscard]]
constexpr auto y()
noexcept -> reference;
129 [[nodiscard]]
constexpr auto y()
const noexcept -> const_reference;
131 [[nodiscard]]
constexpr auto z()
noexcept -> reference;
133 [[nodiscard]]
constexpr auto z()
const noexcept -> const_reference;
139template<scalar Lhs, scalar Rhs>
142template<scalar Lhs, scalar Rhs>
148template<scalar Lhs, scalar Rhs>
151template<scalar Lhs, scalar Rhs>
154template<scalar Lhs, std::convertible_to<Lhs> Rhs>
155requires (!is_scalar_v<Rhs>)
158template<scalar Lhs, scalar Rhs>
161template<scalar Lhs, scalar Rhs>
164template<scalar Lhs, std::convertible_to<Lhs> Rhs>
165requires (!is_scalar_v<Rhs>)
178template<sbx::math::scalar Type>
179struct std::hash<sbx::math::basic_vector3<Type>> {
185template<sbx::math::scalar Type>
186struct fmt::formatter<sbx::math::basic_vector3<Type>> {
188 template<
typename ParseContext>
189 constexpr auto parse(ParseContext& context)
noexcept ->
decltype(context.begin());
191 template<
typename FormatContext>
196template<sbx::math::scalar Type>
197struct YAML::convert<sbx::math::basic_vector3<Type>> {
202 node.SetStyle(YAML::EmitterStyle::Flow);
216 rhs.x() =
node[
"x"].as<Type>();
217 rhs.y() =
node[
"y"].as<Type>();
218 rhs.z() =
node[
"z"].as<Type>();
225template<sbx::math::scalar Type>
227 return out << YAML::convert<sbx::math::basic_vector3<Type>>::encode(vector);
230#include <libsbx/math/vector3.ipp>
constexpr auto operator/(basic_degree< Type > lhs, const Other rhs) noexcept -> basic_degree< Type >
Divides a degree value by a scalar factor.
Definition: angle.ipp:112
constexpr auto operator+(basic_degree< Type > lhs, const basic_degree< Other > &rhs) noexcept -> basic_degree< Type >
Adds two degree values.
Definition: angle.ipp:90
constexpr auto operator*(basic_degree< Type > lhs, const Other rhs) noexcept -> basic_degree< Type >
Multiplies a degree value by a scalar factor.
Definition: angle.ipp:105
A vector in two-dimensional space.
Definition: vector2.hpp:28
Definition: vector3.hpp:23
Fixed-size vector type.
Definition: vector.hpp:55
constexpr auto length_squared() const noexcept -> length_type
Returns the squared length of the vector.
Definition: vector.ipp:190
Concept for scalar numeric types.
Definition: concepts.hpp:150
Core numeric concepts and type traits.
Generic fixed-size vector type.