sandbox
Loading...
Searching...
No Matches
vector4.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_MATH_VECTOR4_HPP_
3#define LIBSBX_MATH_VECTOR4_HPP_
4
5#include <cstddef>
6#include <cmath>
7#include <cstdint>
8#include <concepts>
9#include <fstream>
10#include <ostream>
11#include <type_traits>
12
13#include <fmt/format.h>
14
15#include <yaml-cpp/yaml.h>
16
18#include <libsbx/math/vector2.hpp>
19#include <libsbx/math/vector3.hpp>
20
21namespace sbx::math {
22
23template<scalar Type>
24class basic_vector4 : public basic_vector<4u, Type> {
25
27
28 inline static constexpr auto x_axis = std::size_t{0u};
29 inline static constexpr auto y_axis = std::size_t{1u};
30 inline static constexpr auto z_axis = std::size_t{2u};
31 inline static constexpr auto w_axis = std::size_t{3u};
32
33public:
34
35 using value_type = base_type::value_type;
36 using reference = base_type::reference;
37 using const_reference = base_type::const_reference;
38 using size_type = base_type::size_type;
39 using length_type = base_type::length_type;
40
41 inline static constexpr basic_vector4 zero{base_type::fill(value_type{0})};
42 inline static constexpr basic_vector4 one{base_type::fill(value_type{1})};
43
44 using base_type::base_type;
45
46 constexpr basic_vector4(const base_type& base) noexcept;
47
48 template<scalar XOther, scalar YOther, scalar ZOther, scalar WOther>
49 constexpr basic_vector4(XOther x, YOther y, ZOther z, WOther w) noexcept;
50
51 template<scalar Other, scalar ScalarW = Other>
52 constexpr basic_vector4(const basic_vector3<Other>& vector, ScalarW w = ScalarW{0}) noexcept;
53
54 template<scalar Other, scalar ScalarZ = Other, scalar ScalarW = Other>
55 constexpr basic_vector4(const basic_vector2<Other>& vector, ScalarZ z = ScalarZ{0}, ScalarW w = ScalarW{0}) noexcept;
56
57 [[nodiscard]] static constexpr auto dot(const basic_vector4& lhs, const basic_vector4& rhs) noexcept -> length_type;
58
59 [[nodiscard]] static constexpr auto normalized(const basic_vector4& vector) noexcept -> basic_vector4;
60
61 [[nodiscard]] constexpr operator basic_vector3<Type>() const noexcept;
62
63 [[nodiscard]] constexpr auto x() noexcept -> reference;
64
65 [[nodiscard]] constexpr auto x() const noexcept -> const_reference;
66
67 [[nodiscard]] constexpr auto y() noexcept -> reference;
68
69 [[nodiscard]] constexpr auto y() const noexcept -> const_reference;
70
71 [[nodiscard]] constexpr auto z() noexcept -> reference;
72
73 [[nodiscard]] constexpr auto z() const noexcept -> const_reference;
74
75 [[nodiscard]] constexpr auto w() noexcept -> reference;
76
77 [[nodiscard]] constexpr auto w() const noexcept -> const_reference;
78
79}; // class basic_vector4
80
81template<scalar Lhs, scalar Rhs>
82[[nodiscard]] constexpr auto operator+(basic_vector4<Lhs> lhs, const basic_vector4<Rhs>& rhs) noexcept -> basic_vector4<Lhs>;
83
84template<scalar Lhs, scalar Rhs>
85[[nodiscard]] constexpr auto operator-(basic_vector4<Lhs> lhs, const basic_vector4<Rhs>& rhs) noexcept -> basic_vector4<Lhs>;
86
87template<scalar Type>
88[[nodiscard]] constexpr auto operator-(basic_vector4<Type> vector) noexcept -> basic_vector4<Type>;
89
90template<scalar Lhs, scalar Rhs>
91[[nodiscard]] constexpr auto operator*(basic_vector4<Lhs> lhs, Rhs scalar) noexcept -> basic_vector4<Lhs>;
92
93template<scalar Lhs, scalar Rhs>
94[[nodiscard]] constexpr auto operator*(basic_vector4<Lhs> lhs, const basic_vector4<Rhs>& rhs) noexcept -> basic_vector4<Lhs>;
95
96template<scalar Lhs, scalar Rhs>
97[[nodiscard]] constexpr auto operator/(basic_vector4<Lhs> lhs, Rhs scalar) noexcept -> basic_vector4<Lhs>;
98
100
102
104
105using vector4 = vector4f;
106
107} // namespace sbx::math
108
109template<sbx::math::scalar Type>
110struct std::hash<sbx::math::basic_vector4<Type>> {
111
112 auto operator()(const sbx::math::basic_vector4<Type>& vector) const noexcept -> std::size_t;
113
114}; // struct std::hash
115
116template<sbx::math::scalar Type>
117struct fmt::formatter<sbx::math::basic_vector4<Type>> {
118
119 template<typename ParseContext>
120 constexpr auto parse(ParseContext& context) noexcept -> decltype(context.begin());
121
122 template<typename FormatContext>
123 auto format(const sbx::math::basic_vector4<Type>& vector, FormatContext& context) const noexcept -> decltype(context.out());
124
125}; // struct fmt::formatter
126
127template<sbx::math::scalar Type>
128struct YAML::convert<sbx::math::basic_vector4<Type>> {
129
130 static auto encode(const sbx::math::basic_vector4<Type>& vector) -> Node;
131
132 static auto decode(const Node& node, sbx::math::basic_vector4<Type>& vector) -> bool;
133
134}; // struct YAML::convert
135
136#include <libsbx/math/vector4.ipp>
137
138#endif // LIBSBX_MATH_VECTOR4_HPP_
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
Definition: tests.cpp:6
A vector in two-dimensional space.
Definition: vector2.hpp:28
Definition: vector3.hpp:23
Definition: vector4.hpp:24
Fixed-size vector type.
Definition: vector.hpp:55
Concept for scalar numeric types.
Definition: concepts.hpp:150
Core numeric concepts and type traits.