HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
vector.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../rapid/numeric_array.hpp"
8
9namespace hi::inline v1 {
10namespace geo {
11
19template<int D>
20class vector {
21public:
22 static_assert(D == 2 || D == 3, "Only 2D or 3D vectors are supported");
23
24 constexpr vector(vector const&) noexcept = default;
25 constexpr vector(vector&&) noexcept = default;
26 constexpr vector& operator=(vector const&) noexcept = default;
27 constexpr vector& operator=(vector&&) noexcept = default;
28
31 template<int E>
32 requires(E < D) [[nodiscard]] constexpr vector(vector<E> const& other) noexcept : _v(static_cast<f32x4>(other))
33 {
34 hi_axiom(holds_invariant());
35 }
36
40 template<int E>
41 requires(E > D) [[nodiscard]] constexpr explicit vector(vector<E> const& other) noexcept : _v(static_cast<f32x4>(other))
42 {
43 for (std::size_t i = D; i != E; ++i) {
44 _v[i] = 0.0f;
45 }
46 hi_axiom(holds_invariant());
47 }
48
51 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
52 {
53 return _v;
54 }
55
58 [[nodiscard]] constexpr explicit vector(f32x4 const& other) noexcept : _v(other)
59 {
60 hi_axiom(holds_invariant());
61 }
62
65 [[nodiscard]] constexpr vector() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
66
71 [[nodiscard]] constexpr vector(float x, float y) noexcept requires(D == 2) : _v(x, y, 0.0f, 0.0f) {}
72
78 [[nodiscard]] constexpr vector(float x, float y, float z = 0.0f) noexcept requires(D == 3) : _v(x, y, z, 0.0f) {}
79
83 [[nodiscard]] constexpr float& x() noexcept
84 {
85 return _v.x();
86 }
87
91 [[nodiscard]] constexpr float& y() noexcept
92 {
93 return _v.y();
94 }
95
99 [[nodiscard]] constexpr float& z() noexcept requires(D == 3)
100 {
101 return _v.z();
102 }
103
107 [[nodiscard]] constexpr float const& x() const noexcept
108 {
109 return _v.x();
110 }
111
115 [[nodiscard]] constexpr float const& y() const noexcept
116 {
117 return _v.y();
118 }
119
123 [[nodiscard]] constexpr float const& z() const noexcept requires(D == 3)
124 {
125 return _v.z();
126 }
127
131 [[nodiscard]] constexpr vector operator-() const noexcept
132 {
133 hi_axiom(holds_invariant());
134 return vector{-_v};
135 }
136
137 template<int E>
138 requires(E <= D) constexpr vector& operator+=(vector<E> const& rhs) noexcept
139 {
140 hi_axiom(holds_invariant() && rhs.holds_invariant());
141 _v = _v + static_cast<f32x4>(rhs);
142 return *this;
143 }
144
150 [[nodiscard]] constexpr friend vector operator+(vector const& lhs, vector const& rhs) noexcept
151 {
152 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
153 return vector{lhs._v + rhs._v};
154 }
155
161 [[nodiscard]] constexpr friend vector operator-(vector const& lhs, vector const& rhs) noexcept
162 {
163 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
164 return vector{lhs._v - rhs._v};
165 }
166
172 [[nodiscard]] constexpr friend vector operator*(vector const& lhs, float const& rhs) noexcept
173 {
174 hi_axiom(lhs.holds_invariant());
175 return vector{lhs._v * rhs};
176 }
177
183 [[nodiscard]] constexpr friend vector operator*(float const& lhs, vector const& rhs) noexcept
184 {
185 hi_axiom(rhs.holds_invariant());
186 return vector{f32x4::broadcast(lhs) * rhs._v};
187 }
188
194 [[nodiscard]] constexpr friend bool operator==(vector const& lhs, vector const& rhs) noexcept
195 {
196 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
197 return lhs._v == rhs._v;
198 }
199
204 [[nodiscard]] constexpr friend float squared_hypot(vector const& rhs) noexcept
205 {
206 hi_axiom(rhs.holds_invariant());
207 return squared_hypot<element_mask>(rhs._v);
208 }
209
214 [[nodiscard]] constexpr friend float hypot(vector const& rhs) noexcept
215 {
216 hi_axiom(rhs.holds_invariant());
217 return hypot<element_mask>(rhs._v);
218 }
219
224 [[nodiscard]] constexpr friend float rcp_hypot(vector const& rhs) noexcept
225 {
226 hi_axiom(rhs.holds_invariant());
227 return rcp_hypot<element_mask>(rhs._v);
228 }
229
234 [[nodiscard]] constexpr friend vector normalize(vector const& rhs) noexcept
235 {
236 hi_axiom(rhs.holds_invariant());
237 return vector{normalize<element_mask>(rhs._v)};
238 }
239
245 [[nodiscard]] constexpr friend float dot(vector const& lhs, vector const& rhs) noexcept
246 {
247 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
248 return dot<element_mask>(lhs._v, rhs._v);
249 }
250
256 [[nodiscard]] constexpr friend float det(vector const& lhs, vector const& rhs) noexcept requires (D == 2)
257 {
258 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
259 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
260 }
261
267 template<int E>
268 [[nodiscard]] friend constexpr auto min(vector const& lhs, vector<E> const& rhs) noexcept
269 {
270 return vector<std::max(D, E)>{min(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
271 }
272
278 template<int E>
279 [[nodiscard]] friend constexpr auto max(vector const& lhs, vector<E> const& rhs) noexcept
280 {
281 return vector<std::max(D, E)>{max(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
282 }
283
286 [[nodiscard]] friend constexpr vector round(vector const& rhs) noexcept
287 {
288 return vector{round(static_cast<f32x4>(rhs))};
289 }
290
293 [[nodiscard]] friend constexpr vector ceil(vector const& rhs) noexcept
294 {
295 return vector{ceil(static_cast<f32x4>(rhs))};
296 }
297
300 [[nodiscard]] friend constexpr vector floor(vector const& rhs) noexcept
301 {
302 return vector{floor(static_cast<f32x4>(rhs))};
303 }
304
308 [[nodiscard]] constexpr bool holds_invariant() const noexcept
309 {
310 return _v.w() == 0.0f && (D == 3 || _v.z() == 0.0f);
311 }
312
313 [[nodiscard]] friend std::string to_string(vector const& rhs) noexcept
314 {
315 if constexpr (D == 2) {
316 return std::format("({}, {})", rhs._v.x(), rhs._v.y());
317 } else if constexpr (D == 3) {
318 return std::format("({}, {}, {})", rhs._v.x(), rhs._v.y(), rhs._v.z());
319 } else {
320 hi_static_no_default();
321 }
322 }
323
324 friend std::ostream& operator<<(std::ostream& lhs, vector const& rhs) noexcept
325 {
326 return lhs << to_string(rhs);
327 }
328
329private:
330 f32x4 _v;
331
332 static constexpr std::size_t element_mask = (1_uz << D) - 1;
333};
334
339[[nodiscard]] constexpr vector<2> cross(vector<2> const& rhs) noexcept
340{
341 hi_axiom(rhs.holds_invariant());
342 return vector<2>{cross_2D(static_cast<f32x4>(rhs))};
343}
344
349[[nodiscard]] constexpr vector<2> normal(vector<2> const& rhs) noexcept
350{
351 hi_axiom(rhs.holds_invariant());
352 return normalize(cross(rhs));
353}
354
360[[nodiscard]] constexpr vector<3> normal(vector<3> const& rhs, float angle) noexcept
361{
362 if (angle != 0.0f) {
363 hi_not_implemented();
364 }
365 return normal(vector<2>{f32x4{rhs}.xy00()});
366}
367
376[[nodiscard]] constexpr float cross(vector<2> const& lhs, vector<2> const& rhs) noexcept
377{
378 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
379 return cross_2D(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs));
380}
381
387[[nodiscard]] constexpr vector<3> cross(vector<3> const& lhs, vector<3> const& rhs) noexcept
388{
389 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
390 return vector<3>{cross_3D(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
391}
392
393} // namespace geo
394
395using vector2 = geo::vector<2>;
396using vector3 = geo::vector<3>;
397
398} // namespace hi::inline v1
399
400template<typename CharT>
401struct std::formatter<hi::geo::vector<2>, CharT> {
402 auto parse(auto& pc)
403 {
404 return pc.end();
405 }
406
407 auto format(hi::geo::vector<2> const& t, auto& fc)
408 {
409 return std::vformat_to(fc.out(), "({}, {})", std::make_format_args(t.x(), t.y()));
410 }
411};
412
413template<typename CharT>
414struct std::formatter<hi::geo::vector<3>, CharT> : std::formatter<float, CharT> {
415 auto parse(auto& pc)
416 {
417 return pc.end();
418 }
419
420 auto format(hi::geo::vector<3> const& t, auto& fc)
421 {
422 return std::vformat_to(fc.out(), "({}, {}, {})", std::make_format_args(t.x(), t.y(), t.z()));
423 }
424};
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
constexpr vector(float x, float y) noexcept
Construct a 2D vector from x and y elements.
Definition vector.hpp:71
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
constexpr friend float hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:214
constexpr vector(vector< E > const &other) noexcept
Construct a vector from a higher dimension vector.
Definition vector.hpp:41
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:268
constexpr friend bool operator==(vector const &lhs, vector const &rhs) noexcept
Compare if two vectors are equal.
Definition vector.hpp:194
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector.hpp:308
friend constexpr vector round(vector const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector.hpp:286
constexpr vector() noexcept
Construct a empty vector / zero length.
Definition vector.hpp:65
constexpr vector(f32x4 const &other) noexcept
Construct a vector from a f32x4-numeric_array.
Definition vector.hpp:58
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 float & z() noexcept
Access the z element from the vector.
Definition vector.hpp:99
constexpr friend vector operator-(vector const &lhs, vector const &rhs) noexcept
Subtract two vectors from each other.
Definition vector.hpp:161
friend constexpr vector floor(vector const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector.hpp:300
constexpr vector operator-() const noexcept
Mirror this vector.
Definition vector.hpp:131
constexpr friend vector operator*(vector const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector.hpp:172
constexpr float const & y() const noexcept
Access the y element from the vector.
Definition vector.hpp:115
constexpr friend float squared_hypot(vector const &rhs) noexcept
Get the squared length of the vector.
Definition vector.hpp:204
constexpr friend float det(vector const &lhs, vector const &rhs) noexcept
Get the determinate between two vectors.
Definition vector.hpp:256
friend constexpr vector ceil(vector const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector.hpp:293
constexpr friend float dot(vector const &lhs, vector const &rhs) noexcept
Get the dot product between two vectors.
Definition vector.hpp:245
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector.hpp:91
constexpr vector(vector< E > const &other) noexcept
Construct a vector from a lower dimension vector.
Definition vector.hpp:32
constexpr friend float rcp_hypot(vector const &rhs) noexcept
Get the length of the vector.
Definition vector.hpp:224
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector.hpp:83
constexpr friend vector operator+(vector const &lhs, vector const &rhs) noexcept
Add two vectors from each other.
Definition vector.hpp:150
constexpr float const & x() const noexcept
Access the x element from the vector.
Definition vector.hpp:107
constexpr float const & z() const noexcept
Access the z element from the vector.
Definition vector.hpp:123
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:279
T max(T... args)
T to_string(T... args)