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})
97 [[nodiscard]]
constexpr value_type&
x() noexcept
105 [[nodiscard]]
constexpr value_type&
y() noexcept
113 [[nodiscard]]
constexpr value_type&
z() noexcept
122 [[nodiscard]]
constexpr value_type
x() const noexcept
130 [[nodiscard]]
constexpr value_type
y() const noexcept
138 [[nodiscard]]
constexpr value_type
z() const noexcept
157 hi_axiom(holds_invariant() && rhs.holds_invariant());
158 _v = _v +
static_cast<array_type
>(rhs);
169 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
170 return vector{lhs._v + rhs._v};
180 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
181 return vector{lhs._v - rhs._v};
192 return vector{lhs._v * rhs};
203 return vector{array_type::broadcast(lhs) * rhs._v};
213 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
214 return equal(lhs._v, rhs._v);
224 return squared_hypot<element_mask>(rhs._v);
231 [[nodiscard]]
constexpr friend value_type
hypot(
vector const& rhs)
noexcept
232 requires std::is_floating_point_v<value_type>
235 return hypot<element_mask>(rhs._v);
245 return rcp_hypot<element_mask>(rhs._v);
255 return vector{normalize<element_mask>(rhs._v)};
263 [[nodiscard]]
constexpr friend value_type
dot(
vector const& lhs,
vector const& rhs)
noexcept
265 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
266 return dot<element_mask>(lhs._v, rhs._v);
274 [[nodiscard]]
constexpr friend value_type
det(
vector const& lhs,
vector const& rhs)
noexcept
277 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
278 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
289 return vector<value_type,
std::max(D, E)>{min(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
300 return vector<value_type,
std::max(D, E)>{max(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
307 return vector{round(
static_cast<array_type
>(rhs))};
314 return vector{ceil(
static_cast<array_type
>(rhs))};
321 return vector{floor(
static_cast<array_type
>(rhs))};
329 return _v.w() == value_type{0} && (D == 3 || _v.z() == value_type{0});
334 if constexpr (D == 2) {
335 return std::format(
"({}, {})", rhs._v.x(), rhs._v.y());
336 }
else if constexpr (D == 3) {
337 return std::format(
"({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
351 static constexpr std::size_t element_mask = (1_uz << D) - 1;
358[[nodiscard]]
constexpr vector<float, 2> cross(vector<float, 2>
const& rhs)
noexcept
361 return vector<float, 2>{cross_2D(
static_cast<f32x4
>(rhs))};
368[[nodiscard]]
constexpr vector<float, 2> normal(vector<float, 2>
const& rhs)
noexcept
371 return normalize(cross(rhs));
379[[nodiscard]]
constexpr vector<float, 3> normal(vector<float, 3>
const& rhs,
float angle)
noexcept
381 if (angle !=
float{0}) {
384 return normal(vector<float, 2>{f32x4{rhs}.xy00()});
395[[nodiscard]]
constexpr float cross(vector<float, 2>
const& lhs, vector<float, 2>
const& rhs)
noexcept
397 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
398 return cross_2D(
static_cast<f32x4
>(lhs),
static_cast<f32x4
>(rhs));
406[[nodiscard]]
constexpr vector<float, 3> cross(vector<float, 3>
const& lhs, vector<float, 3>
const& rhs)
noexcept
408 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
409 return vector<float, 3>{cross_3D(
static_cast<f32x4
>(lhs),
static_cast<f32x4
>(rhs))};
414using vector2 = geo::vector<float, 2>;
415using vector3 = geo::vector<float, 3>;
416using vector2i = geo::vector<int, 2>;
417using vector3i = geo::vector<int, 3>;
421template<
typename CharT>
422struct std::formatter<
hi::geo::vector<float, 2>, CharT> {
428 auto format(hi::geo::vector<float, 2>
const& t,
auto& fc)
430 return std::vformat_to(fc.out(),
"({}, {})", std::make_format_args(t.x(), t.y()));
434template<
typename CharT>
435struct std::formatter<
hi::geo::vector<float, 3>, CharT> {
441 auto format(hi::geo::vector<float, 3>
const& t,
auto& fc)
443 return std::vformat_to(fc.out(),
"({}, {}, {})", std::make_format_args(t.x(), t.y(), t.z()));
447template<
typename CharT>
448struct std::formatter<
hi::geo::vector<int, 2>, CharT> {
454 auto format(hi::geo::vector<int, 2>
const& t,
auto& fc)
456 return std::vformat_to(fc.out(),
"({}, {})", std::make_format_args(t.x(), t.y()));
460template<
typename CharT>
461struct std::formatter<
hi::geo::vector<int, 3>, CharT> {
467 auto format(hi::geo::vector<int, 3>
const& t,
auto& fc)
469 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:308
#define hi_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:320
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
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:274
constexpr friend value_type squared_hypot(vector const &rhs) noexcept
Get the squared length of the vector.
Definition vector.hpp:221
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:242
constexpr friend bool operator==(vector const &lhs, vector const &rhs) noexcept
Compare if two vectors are equal.
Definition vector.hpp:211
friend constexpr vector round(vector const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector.hpp:305
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector.hpp:327
constexpr friend vector normalize(vector const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector.hpp:252
constexpr value_type & y() noexcept
Access the y element from the vector.
Definition vector.hpp:105
constexpr value_type y() const noexcept
Access the y element from the vector.
Definition vector.hpp:130
friend constexpr auto max(vector const &lhs, vector< value_type, E > const &rhs) noexcept
Mix the two vectors and get the highest value of each element.
Definition vector.hpp:298
constexpr friend value_type dot(vector const &lhs, vector const &rhs) noexcept
Get the dot product between two vectors.
Definition vector.hpp:263
constexpr friend vector operator*(vector const &lhs, value_type const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:189
constexpr friend vector operator-(vector const &lhs, vector const &rhs) noexcept
Subtract two vectors from each other.
Definition vector.hpp:178
constexpr value_type & x() noexcept
Access the x element from the vector.
Definition vector.hpp:97
constexpr vector operator-() const noexcept
Mirror this vector.
Definition vector.hpp:147
friend constexpr vector floor(vector const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector.hpp:319
constexpr friend value_type hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:231
constexpr value_type z() const noexcept
Access the z element from the vector.
Definition vector.hpp:138
constexpr value_type & z() noexcept
Access the z element from the vector.
Definition vector.hpp:113
constexpr friend vector operator*(value_type const &lhs, vector const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:200
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:312
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 auto min(vector const &lhs, vector< value_type, E > const &rhs) noexcept
Mix the two vectors and get the lowest value of each element.
Definition vector.hpp:287
constexpr friend vector operator+(vector const &lhs, vector const &rhs) noexcept
Add two vectors from each other.
Definition vector.hpp:167
constexpr value_type x() const noexcept
Access the x element from the vector.
Definition vector.hpp:122