sandbox
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
25#ifndef LIBSBX_MATH_VECTOR_HPP_
26#define LIBSBX_MATH_VECTOR_HPP_
27
28#include <algorithm>
29#include <array>
30#include <cmath>
31#include <concepts>
32#include <ranges>
33#include <utility>
34
35#include <libsbx/utility/make_array.hpp>
36#include <libsbx/utility/assert.hpp>
37#include <libsbx/utility/zip.hpp>
39
41#include <libsbx/math/traits.hpp>
44
45namespace sbx::math {
46
53template<std::size_t Size, scalar Type>
54requires (Size > 1u)
56
57 template<std::size_t S, scalar T>
58 requires (S > 1u)
59 friend class basic_vector;
60
61public:
62
63 using value_type = Type;
64 using reference = value_type&;
65 using const_reference = const value_type&;
66 using size_type = std::size_t;
67 using length_type = std::float_t;
68
76 template<scalar Other = value_type>
77 constexpr basic_vector(Other value = Other{0}) noexcept;
78
86 template<scalar Other = value_type>
87 constexpr basic_vector(const basic_vector<Size, Other>& other) noexcept;
88
89 constexpr basic_vector(const basic_vector& other) noexcept = default;
90 constexpr basic_vector(basic_vector&& other) noexcept = default;
91
92 auto operator=(const basic_vector& other) noexcept -> basic_vector& = default;
93 auto operator=(basic_vector&& other) noexcept -> basic_vector& = default;
94
102 static constexpr auto min(const basic_vector& vector) noexcept -> value_type;
103
115 template<scalar Lhs = value_type, scalar Rhs = value_type>
116 static constexpr auto min(const basic_vector<Size, Lhs>& lhs, const basic_vector<Size, Rhs>& rhs) noexcept -> basic_vector;
117
125 static constexpr auto max(const basic_vector& vector) noexcept -> value_type;
126
138 template<scalar Lhs = value_type, scalar Rhs = value_type>
139 static constexpr auto max(const basic_vector<Size, Lhs>& lhs, const basic_vector<Size, Rhs>& rhs) noexcept -> basic_vector;
140
150 template<scalar Lhs = value_type>
151 static constexpr auto abs(const basic_vector<Size, Lhs>& vector) noexcept -> basic_vector;
152
163 template<size_type Axis, scalar Other = value_type>
164 requires (Axis < Size)
165 [[nodiscard]] static constexpr auto splat(const basic_vector<Size, Other>& vector) noexcept -> basic_vector<Size, Other>;
166
176 [[nodiscard]] static constexpr auto lerp(const basic_vector& x, const basic_vector& y, const value_type a) noexcept -> basic_vector;
177
183 constexpr auto data() noexcept -> value_type*;
184
190 constexpr auto data() const noexcept -> const value_type*;
191
199 [[nodiscard]] constexpr auto operator[](size_type index) noexcept -> reference;
200
208 [[nodiscard]] constexpr auto operator[](size_type index) const noexcept -> const_reference;
209
219 template<scalar Other>
220 constexpr auto operator+=(const basic_vector<Size, Other>& other) noexcept -> basic_vector&;
221
231 template<scalar Other>
232 constexpr auto operator-=(const basic_vector<Size, Other>& other) noexcept -> basic_vector&;
233
243 template<scalar Other>
244 constexpr auto operator*=(Other scalar) noexcept -> basic_vector&;
245
255 template<scalar Other>
256 constexpr auto operator*=(const basic_vector<Size, Other>& other) noexcept -> basic_vector&;
257
267 template<scalar Other>
268 constexpr auto operator/=(Other scalar) noexcept -> basic_vector&;
269
275 [[nodiscard]] constexpr auto length_squared() const noexcept -> length_type;
276
282 [[nodiscard]] constexpr auto length() const noexcept -> length_type;
283
289 constexpr auto normalize() noexcept -> basic_vector&;
290
291protected:
292
293 template<std::convertible_to<value_type>... Args>
294 requires (sizeof...(Args) == Size)
295 constexpr basic_vector(Args&&... args) noexcept;
296
297 constexpr basic_vector(std::array<value_type, Size>&& components) noexcept;
298
299 template<scalar Other>
300 [[nodiscard]] static constexpr auto fill(Other value) noexcept -> basic_vector;
301
302 template<std::size_t Index, scalar Other>
303 [[nodiscard]] static constexpr auto axis(Other value) noexcept -> basic_vector;
304
305private:
306
307 std::array<Type, Size> _components;
308
309}; // class basic_vector
310
323template<std::size_t Size, scalar Lhs, scalar Rhs>
324[[nodiscard]] constexpr auto operator==(const basic_vector<Size, Lhs>& lhs, const basic_vector<Size, Rhs>& rhs) noexcept -> bool;
325
338template<std::size_t Size, scalar Lhs, scalar Rhs>
339[[nodiscard]] constexpr auto operator+(basic_vector<Size, Lhs> lhs, const basic_vector<Size, Rhs>& rhs) noexcept -> basic_vector<Size, Lhs>;
340
353template<std::size_t Size, scalar Lhs, scalar Rhs>
354[[nodiscard]] constexpr auto operator-(basic_vector<Size, Lhs> lhs, const basic_vector<Size, Rhs>& rhs) noexcept -> basic_vector<Size, Lhs>;
355
368template<std::size_t Size, scalar Lhs, scalar Rhs>
369[[nodiscard]] constexpr auto operator*(basic_vector<Size, Lhs> lhs, Rhs scalar) noexcept -> basic_vector<Size, Lhs>;
370
383template<std::size_t Size, scalar Lhs, scalar Rhs>
384[[nodiscard]] constexpr auto operator/(basic_vector<Size, Lhs> lhs, Rhs scalar) noexcept -> basic_vector<Size, Lhs>;
385
386} // namespace sbx::math
387
388#include <libsbx/math/vector.ipp>
389
390#endif // LIBSBX_MATH_VECTOR_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
Fixed-size vector type.
Definition: vector.hpp:55
Concept for scalar numeric types.
Definition: concepts.hpp:150
Mathematical constants.
Generic math algorithms and helpers.
constexpr auto abs(const Type value) -> Type
Computes the absolute value.
Definition: algorithm.ipp:12
Core numeric concepts and type traits.