7#include "../SIMD/module.hpp"
9namespace hi::inline
v1 {
20template<
typename T,
int D>
24 using array_type = simd<value_type, 4>;
26 static_assert(D == 2 || D == 3,
"Only 2D or 3D vectors are supported");
30 constexpr vector& operator=(
vector const&)
noexcept =
default;
31 constexpr vector& operator=(
vector&&)
noexcept =
default;
50 _v[i] = value_type{0};
57 [[nodiscard]]
constexpr explicit operator array_type() const noexcept
64 [[nodiscard]]
constexpr explicit vector(array_type
const& other) noexcept : _v(other)
71 [[nodiscard]]
constexpr vector() noexcept : _v(value_type{0}, value_type{0}, value_type{0}, value_type{0}) {}
77 [[nodiscard]]
constexpr vector(value_type x, value_type y)
noexcept
79 : _v(x, y, value_type{0}, value_type{0})
88 [[nodiscard]]
constexpr vector(value_type x, value_type y, value_type z = value_type{0})
noexcept
90 : _v(x, y, z, value_type{0})
94 [[nodiscard]]
constexpr static vector infinity() noexcept
100 [[nodiscard]]
constexpr static vector infinity() noexcept
112 [[nodiscard]]
constexpr value_type&
x() noexcept
120 [[nodiscard]]
constexpr value_type&
y() noexcept
128 [[nodiscard]]
constexpr value_type&
z() noexcept
137 [[nodiscard]]
constexpr value_type
x() const noexcept
145 [[nodiscard]]
constexpr value_type
y() const noexcept
153 [[nodiscard]]
constexpr value_type
z() const noexcept
172 hi_axiom(holds_invariant() && rhs.holds_invariant());
173 _v = _v +
static_cast<array_type
>(rhs);
184 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
185 return vector{lhs._v + rhs._v};
195 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
196 return vector{lhs._v - rhs._v};
207 return vector{lhs._v * rhs};
218 return vector{array_type::broadcast(lhs) * rhs._v};
228 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
229 return equal(lhs._v, rhs._v);
239 return squared_hypot<element_mask>(rhs._v);
246 [[nodiscard]]
constexpr friend value_type
hypot(
vector const& rhs)
noexcept
247 requires std::is_floating_point_v<value_type>
250 return hypot<element_mask>(rhs._v);
260 return rcp_hypot<element_mask>(rhs._v);
270 return vector{normalize<element_mask>(rhs._v)};
278 [[nodiscard]]
constexpr friend value_type
dot(
vector const& lhs,
vector const& rhs)
noexcept
280 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
281 return dot<element_mask>(lhs._v, rhs._v);
289 [[nodiscard]]
constexpr friend value_type
det(
vector const& lhs,
vector const& rhs)
noexcept
292 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
293 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
303 return vector{min(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
313 return vector{max(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
320 return vector{round(
static_cast<array_type
>(rhs))};
327 return vector{ceil(
static_cast<array_type
>(rhs))};
334 return vector{floor(
static_cast<array_type
>(rhs))};
342 return _v.w() == value_type{0} && (D == 3 || _v.z() == value_type{0});
347 if constexpr (D == 2) {
348 return std::format(
"({}, {})", rhs._v.x(), rhs._v.y());
349 }
else if constexpr (D == 3) {
350 return std::format(
"({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
364 static constexpr std::size_t element_mask = (1_uz << D) - 1;
371[[nodiscard]]
constexpr vector<float, 2> cross(vector<float, 2>
const& rhs)
noexcept
374 return vector<float, 2>{cross_2D(
static_cast<f32x4
>(rhs))};
381[[nodiscard]]
constexpr vector<float, 2> normal(vector<float, 2>
const& rhs)
noexcept
384 return normalize(cross(rhs));
392[[nodiscard]]
constexpr vector<float, 3> normal(vector<float, 3>
const& rhs,
float angle)
noexcept
394 if (angle !=
float{0}) {
397 return normal(vector<float, 2>{f32x4{rhs}.xy00()});
408[[nodiscard]]
constexpr float cross(vector<float, 2>
const& lhs, vector<float, 2>
const& rhs)
noexcept
410 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
411 return cross_2D(
static_cast<f32x4
>(lhs),
static_cast<f32x4
>(rhs));
419[[nodiscard]]
constexpr vector<float, 3> cross(vector<float, 3>
const& lhs, vector<float, 3>
const& rhs)
noexcept
421 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
422 return vector<float, 3>{cross_3D(
static_cast<f32x4
>(lhs),
static_cast<f32x4
>(rhs))};
427using vector2 = geo::vector<float, 2>;
428using vector3 = geo::vector<float, 3>;
429using vector2i = geo::vector<int, 2>;
430using vector3i = geo::vector<int, 3>;
434template<
typename CharT>
435struct std::formatter<
hi::geo::vector<float, 2>, CharT> {
441 auto format(hi::geo::vector<float, 2>
const& t,
auto& fc)
443 return std::vformat_to(fc.out(),
"({}, {})", std::make_format_args(t.x(), t.y()));
447template<
typename CharT>
448struct std::formatter<
hi::geo::vector<float, 3>, CharT> {
454 auto format(hi::geo::vector<float, 3>
const& t,
auto& fc)
456 return std::vformat_to(fc.out(),
"({}, {}, {})", std::make_format_args(t.x(), t.y(), t.z()));
460template<
typename CharT>
461struct std::formatter<
hi::geo::vector<int, 2>, CharT> {
467 auto format(hi::geo::vector<int, 2>
const& t,
auto& fc)
469 return std::vformat_to(fc.out(),
"({}, {})", std::make_format_args(t.x(), t.y()));
473template<
typename CharT>
474struct std::formatter<
hi::geo::vector<int, 3>, CharT> {
480 auto format(hi::geo::vector<int, 3>
const& t,
auto& fc)
482 return std::vformat_to(fc.out(),
"({}, {}, {})", std::make_format_args(t.x(), t.y(), t.z()));
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:323
#define hi_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:335
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:21
constexpr friend value_type det(vector const &lhs, vector const &rhs) noexcept
Get the determinate between two vectors.
Definition vector.hpp:289
friend constexpr vector min(vector const &lhs, vector const &rhs) noexcept
Mix the two vectors and get the lowest value of each element.
Definition vector.hpp:301
constexpr friend value_type squared_hypot(vector const &rhs) noexcept
Get the squared length of the vector.
Definition vector.hpp:236
constexpr vector() noexcept
Construct a empty vector / zero length.
Definition vector.hpp:71
constexpr vector(vector< value_type, E > const &other) noexcept
Construct a vector from a higher dimension vector.
Definition vector.hpp:47
constexpr friend value_type rcp_hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:257
constexpr friend bool operator==(vector const &lhs, vector const &rhs) noexcept
Compare if two vectors are equal.
Definition vector.hpp:226
friend constexpr vector round(vector const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector.hpp:318
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector.hpp:340
constexpr friend vector normalize(vector const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector.hpp:267
constexpr value_type & y() noexcept
Access the y element from the vector.
Definition vector.hpp:120
constexpr value_type y() const noexcept
Access the y element from the vector.
Definition vector.hpp:145
constexpr friend value_type dot(vector const &lhs, vector const &rhs) noexcept
Get the dot product between two vectors.
Definition vector.hpp:278
constexpr friend vector operator*(vector const &lhs, value_type const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:204
constexpr friend vector operator-(vector const &lhs, vector const &rhs) noexcept
Subtract two vectors from each other.
Definition vector.hpp:193
constexpr value_type & x() noexcept
Access the x element from the vector.
Definition vector.hpp:112
constexpr vector operator-() const noexcept
Mirror this vector.
Definition vector.hpp:162
friend constexpr vector floor(vector const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector.hpp:332
constexpr friend value_type hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:246
constexpr value_type z() const noexcept
Access the z element from the vector.
Definition vector.hpp:153
constexpr value_type & z() noexcept
Access the z element from the vector.
Definition vector.hpp:128
constexpr friend vector operator*(value_type const &lhs, vector const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:215
constexpr vector(value_type x, value_type y) noexcept
Construct a 2D vector from x and y elements.
Definition vector.hpp:77
friend constexpr vector ceil(vector const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector.hpp:325
constexpr vector(value_type x, value_type y, value_type z=value_type{0}) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector.hpp:88
constexpr vector(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector.hpp:64
constexpr vector(vector< value_type, E > const &other) noexcept
Construct a vector from a lower dimension vector.
Definition vector.hpp:37
friend constexpr vector max(vector const &lhs, vector const &rhs) noexcept
Mix the two vectors and get the highest value of each element.
Definition vector.hpp:311
constexpr friend vector operator+(vector const &lhs, vector const &rhs) noexcept
Add two vectors from each other.
Definition vector.hpp:182
constexpr value_type x() const noexcept
Access the x element from the vector.
Definition vector.hpp:137