7#include "numeric_array.hpp"
22 static_assert(D == 2 || D == 3,
"Only 2D or 3D vectors are supported");
26 constexpr vector &operator=(
vector const &)
noexcept =
default;
27 constexpr vector &operator=(
vector &&)
noexcept =
default;
32 requires(E < D) [[nodiscard]]
constexpr vector(
vector<E> const &other) noexcept : _v(
static_cast<f32x4>(other))
41 requires(E > D) [[nodiscard]]
constexpr explicit vector(
vector<E> const &other) noexcept : _v(
static_cast<f32x4>(other))
43 for (
size_t i = D; i != E; ++i) {
51 [[nodiscard]]
constexpr explicit operator f32x4() const noexcept
58 [[nodiscard]]
constexpr explicit vector(
f32x4 const &other) noexcept : _v(other)
65 [[nodiscard]]
constexpr vector() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
71 [[nodiscard]]
constexpr vector(
float x,
float y)
noexcept requires(D == 2) : _v(
x,
y, 0.0f, 0.0f) {}
78 [[nodiscard]]
constexpr vector(
float x,
float y,
float z = 0.0f) noexcept requires(D == 3) : _v(
x,
y,
z, 0.0f) {}
83 [[nodiscard]]
constexpr float &
x() noexcept
91 [[nodiscard]]
constexpr float &
y() noexcept
99 [[nodiscard]]
constexpr float &
z() noexcept requires(D == 3)
107 [[nodiscard]]
constexpr float const &
x() const noexcept
115 [[nodiscard]]
constexpr float const &
y() const noexcept
123 [[nodiscard]]
constexpr float const &
z() const noexcept requires(D == 3)
137 template<
int E>
requires (E <= D)
141 _v = _v +
static_cast<f32x4>(rhs);
152 tt_axiom(lhs.is_valid() && rhs.
is_valid());
153 return vector{lhs._v + rhs._v};
163 tt_axiom(lhs.is_valid() && rhs.
is_valid());
164 return vector{lhs._v - rhs._v};
174 tt_axiom(lhs.is_valid());
175 return vector{lhs._v * rhs};
186 return vector{lhs * rhs._v};
196 tt_axiom(lhs.is_valid() && rhs.
is_valid());
197 return lhs._v == rhs._v;
207 return squared_hypot<element_mask>(rhs._v);
214 [[nodiscard]]
constexpr friend float hypot(
vector const &rhs)
noexcept
217 return hypot<element_mask>(rhs._v);
227 return rcp_hypot<element_mask>(rhs._v);
237 return vector{normalize<element_mask>(rhs._v)};
245 [[nodiscard]]
constexpr friend float dot(
vector const &lhs,
vector const &rhs)
noexcept
247 tt_axiom(lhs.is_valid() && rhs.
is_valid());
248 return dot<element_mask>(lhs._v, rhs._v);
297 [[nodiscard]]
constexpr bool is_valid() const noexcept
299 return _v.w() == 0.0f && (D == 3 || _v.z() == 0.0f);
304 if constexpr (D == 2) {
305 return fmt::format(
"({}, {})", rhs._v.x(), rhs._v.y());
306 }
else if constexpr (D == 3) {
307 return fmt::format(
"({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
309 tt_static_no_default();
315 return lhs << to_string(rhs);
321 static constexpr size_t element_mask = (1_uz << D) - 1;
328[[nodiscard]]
constexpr vector<2> cross(vector<2>
const &rhs)
noexcept
331 return vector<2>{cross_2D(
static_cast<f32x4
>(rhs))};
338[[nodiscard]]
constexpr vector<2> normal(vector<2>
const &rhs)
noexcept
341 return normalize(cross(rhs));
352[[nodiscard]]
constexpr float cross(vector<2>
const &lhs, vector<2>
const &rhs)
noexcept
354 tt_axiom(lhs.is_valid() && rhs.
is_valid());
355 return cross_2D(
static_cast<f32x4
>(lhs),
static_cast<f32x4
>(rhs));
363[[nodiscard]]
constexpr vector<3> cross(vector<3>
const &lhs, vector<3>
const &rhs)
noexcept
365 tt_axiom(lhs.is_valid() && rhs.
is_valid());
366 return vector<3>{cross_3D(
static_cast<f32x4
>(lhs),
static_cast<f32x4
>(rhs))};
371using vector2 = geo::vector<2>;
372using vector3 = geo::vector<3>;
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
constexpr float const & x() const noexcept
Access the x element from the vector.
Definition vector.hpp:107
constexpr vector(f32x4 const &other) noexcept
Construct a vector from a f32x4-numeric_array.
Definition vector.hpp:58
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector.hpp:83
constexpr vector(float x, float y) noexcept
Construct a 2D vector from x and y elements.
Definition vector.hpp:71
constexpr friend float hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:214
constexpr vector(float x, float y, float z=0.0f) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector.hpp:78
friend constexpr auto min(vector const &lhs, vector< E > const &rhs) noexcept
Mix the two vectors and get the lowest value of each element.
Definition vector.hpp:257
constexpr friend bool operator==(vector const &lhs, vector const &rhs) noexcept
Compare if two vectors are equal.
Definition vector.hpp:194
constexpr float const & y() const noexcept
Access the y element from the vector.
Definition vector.hpp:115
friend constexpr vector round(vector const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector.hpp:275
constexpr vector(vector< E > const &other) noexcept
Construct a vector from a higher dimension vector.
Definition vector.hpp:41
constexpr friend vector normalize(vector const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector.hpp:234
constexpr friend vector operator*(float const &lhs, vector const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:183
constexpr vector() noexcept
Construct a empty vector / zero length.
Definition vector.hpp:65
constexpr friend vector operator-(vector const &lhs, vector const &rhs) noexcept
Subtract two vectors from each other.
Definition vector.hpp:161
constexpr float & z() noexcept
Access the z element from the vector.
Definition vector.hpp:99
friend constexpr vector floor(vector const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector.hpp:289
constexpr vector operator-() const noexcept
Mirror this vector.
Definition vector.hpp:131
constexpr vector(vector< E > const &other) noexcept
Construct a vector from a lower dimension vector.
Definition vector.hpp:32
constexpr friend vector operator*(vector const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:172
constexpr float const & z() const noexcept
Access the z element from the vector.
Definition vector.hpp:123
constexpr friend float squared_hypot(vector const &rhs) noexcept
Get the squared length of the vector.
Definition vector.hpp:204
constexpr bool is_valid() const noexcept
Check if the vector is valid.
Definition vector.hpp:297
friend constexpr vector ceil(vector const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector.hpp:282
constexpr friend float dot(vector const &lhs, vector const &rhs) noexcept
Get the dot product between two vectors.
Definition vector.hpp:245
constexpr friend float rcp_hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:224
constexpr friend vector operator+(vector const &lhs, vector const &rhs) noexcept
Add two vectors from each other.
Definition vector.hpp:150
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector.hpp:91
friend constexpr auto max(vector const &lhs, vector< E > const &rhs) noexcept
Mix the two vectors and get the highest value of each element.
Definition vector.hpp:268