HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
vector2.hpp
1// Copyright Take Vos 2021-2022.
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 "../SIMD/module.hpp"
8
9namespace hi { inline namespace v1 {
10
18class vector2 {
19public:
20 using array_type = simd<float, 4>;
21 using value_type = array_type::value_type;
22
23 constexpr vector2(vector2 const&) noexcept = default;
24 constexpr vector2(vector2&&) noexcept = default;
25 constexpr vector2& operator=(vector2 const&) noexcept = default;
26 constexpr vector2& operator=(vector2&&) noexcept = default;
27
30 [[nodiscard]] constexpr explicit operator array_type() const noexcept
31 {
32 return _v;
33 }
34
37 [[nodiscard]] constexpr explicit vector2(array_type const& other) noexcept : _v(other)
38 {
40 }
41
44 [[nodiscard]] constexpr vector2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
45
51 [[nodiscard]] constexpr vector2(float x, float y) noexcept : _v(x, y, 0.0f, 0.0f) {}
52
56 [[nodiscard]] constexpr float& x() noexcept
57 {
58 return _v.x();
59 }
60
64 [[nodiscard]] constexpr float& y() noexcept
65 {
66 return _v.y();
67 }
68
72 [[nodiscard]] constexpr float x() const noexcept
73 {
74 return _v.x();
75 }
76
80 [[nodiscard]] constexpr float y() const noexcept
81 {
82 return _v.y();
83 }
84
88 [[nodiscard]] constexpr vector2 operator-() const noexcept
89 {
90 return vector2{-_v};
91 }
92
93 constexpr vector2& operator+=(vector2 const& rhs) noexcept
94 {
95 return *this = *this + rhs;
96 }
97
98 constexpr vector2& operator-=(vector2 const& rhs) noexcept
99 {
100 return *this = *this - rhs;
101 }
102
103 constexpr vector2& operator*=(float const& rhs) noexcept
104 {
105 return *this = *this * rhs;
106 }
107
113 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, vector2 const& rhs) noexcept
114 {
115 return vector2{lhs._v + rhs._v};
116 }
117
123 [[nodiscard]] constexpr friend vector2 operator-(vector2 const& lhs, vector2 const& rhs) noexcept
124 {
125 return vector2{lhs._v - rhs._v};
126 }
127
133 [[nodiscard]] constexpr friend vector2 operator*(vector2 const& lhs, float const& rhs) noexcept
134 {
135 return vector2{lhs._v * rhs};
136 }
137
143 [[nodiscard]] constexpr friend vector2 operator*(float const& lhs, vector2 const& rhs) noexcept
144 {
145 return vector2{array_type::broadcast(lhs) * rhs._v};
146 }
147
153 [[nodiscard]] constexpr friend bool operator==(vector2 const& lhs, vector2 const& rhs) noexcept
154 {
155 return equal(lhs._v, rhs._v);
156 }
157
162 [[nodiscard]] constexpr friend float squared_hypot(vector2 const& rhs) noexcept
163 {
164 return squared_hypot<0b0011>(rhs._v);
165 }
166
171 [[nodiscard]] friend float hypot(vector2 const& rhs) noexcept
172 {
173 return hypot<0b0011>(rhs._v);
174 }
175
180 [[nodiscard]] constexpr friend float rcp_hypot(vector2 const& rhs) noexcept
181 {
182 return rcp_hypot<0b0011>(rhs._v);
183 }
184
189 [[nodiscard]] constexpr friend vector2 normalize(vector2 const& rhs) noexcept
190 {
191 return vector2{normalize<0b0011>(rhs._v)};
192 }
193
199 [[nodiscard]] constexpr friend float dot(vector2 const& lhs, vector2 const& rhs) noexcept
200 {
201 return dot<0b0011>(lhs._v, rhs._v);
202 }
203
209 [[nodiscard]] constexpr friend float det(vector2 const& lhs, vector2 const& rhs) noexcept
210 {
211 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
212 }
213
218 [[nodiscard]] constexpr friend vector2 cross(vector2 const& rhs) noexcept
219 {
220 return vector2{cross_2D(static_cast<f32x4>(rhs))};
221 }
222
231 [[nodiscard]] constexpr friend float cross(vector2 const& lhs, vector2 const& rhs) noexcept
232 {
233 return cross_2D(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs));
234 }
235
240 [[nodiscard]] constexpr friend vector2 normal(vector2 const& rhs) noexcept
241 {
242 return normalize(cross(rhs));
243 }
244
250 [[nodiscard]] friend constexpr vector2 min(vector2 const& lhs, vector2 const& rhs) noexcept
251 {
252 return vector2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
253 }
254
260 [[nodiscard]] friend constexpr vector2 max(vector2 const& lhs, vector2 const& rhs) noexcept
261 {
262 return vector2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
263 }
264
267 [[nodiscard]] friend constexpr vector2 round(vector2 const& rhs) noexcept
268 {
269 return vector2{round(static_cast<array_type>(rhs))};
270 }
271
274 [[nodiscard]] friend constexpr vector2 ceil(vector2 const& rhs) noexcept
275 {
276 return vector2{ceil(static_cast<array_type>(rhs))};
277 }
278
281 [[nodiscard]] friend constexpr vector2 floor(vector2 const& rhs) noexcept
282 {
283 return vector2{floor(static_cast<array_type>(rhs))};
284 }
285
289 [[nodiscard]] constexpr bool holds_invariant() const noexcept
290 {
291 return _v.z() == 0.0f and _v.w() == 0.0f;
292 }
293
294 [[nodiscard]] friend std::string to_string(vector2 const& rhs) noexcept
295 {
296 return std::format("({}, {})", rhs._v.x(), rhs._v.y());
297 }
298
299 friend std::ostream& operator<<(std::ostream& lhs, vector2 const& rhs) noexcept
300 {
301 return lhs << to_string(rhs);
302 }
303
304private:
305 array_type _v;
306};
307
308}} // namespace hi::v1
309
310template<typename CharT>
311struct std::formatter<hi::vector2, CharT> {
312 auto parse(auto& pc)
313 {
314 return pc.end();
315 }
316
317 auto format(hi::vector2 const& t, auto& fc)
318 {
319 return std::vformat_to(fc.out(), "({}, {})", std::make_format_args(t.x(), t.y()));
320 }
321};
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
@ other
The gui_event does not have associated data.
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 vector2.hpp:18
constexpr vector2() noexcept
Construct a empty vector / zero length.
Definition vector2.hpp:44
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:56
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector2.hpp:80
constexpr vector2(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector2.hpp:37
friend constexpr vector2 round(vector2 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector2.hpp:267
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector2.hpp:289
constexpr friend vector2 normalize(vector2 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector2.hpp:189
constexpr vector2(float x, float y) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector2.hpp:51
constexpr friend vector2 cross(vector2 const &rhs) noexcept
Get the cross product of one 2D vectors.
Definition vector2.hpp:218
constexpr friend float dot(vector2 const &lhs, vector2 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector2.hpp:199
friend constexpr vector2 max(vector2 const &lhs, vector2 const &rhs) noexcept
Mix the two vectors and get the highest value of each element.
Definition vector2.hpp:260
friend constexpr vector2 ceil(vector2 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector2.hpp:274
friend constexpr vector2 floor(vector2 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector2.hpp:281
constexpr friend vector2 normal(vector2 const &rhs) noexcept
Get the normal on a 2D vector.
Definition vector2.hpp:240
friend float hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:171
constexpr friend bool operator==(vector2 const &lhs, vector2 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector2.hpp:153
constexpr friend vector2 operator*(float const &lhs, vector2 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:143
constexpr friend float cross(vector2 const &lhs, vector2 const &rhs) noexcept
Get the cross product between two 2D vectors.
Definition vector2.hpp:231
constexpr friend vector2 operator+(vector2 const &lhs, vector2 const &rhs) noexcept
Add two vectors from each other.
Definition vector2.hpp:113
constexpr friend vector2 operator-(vector2 const &lhs, vector2 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector2.hpp:123
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector2.hpp:64
constexpr friend float det(vector2 const &lhs, vector2 const &rhs) noexcept
Get the determinate between two vectors.
Definition vector2.hpp:209
constexpr friend float rcp_hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:180
constexpr friend float squared_hypot(vector2 const &rhs) noexcept
Get the squared length of the vector.
Definition vector2.hpp:162
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector2.hpp:72
constexpr friend vector2 operator*(vector2 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:133
constexpr vector2 operator-() const noexcept
Mirror this vector.
Definition vector2.hpp:88
friend constexpr vector2 min(vector2 const &lhs, vector2 const &rhs) noexcept
Mix the two vectors and get the lowest value of each element.
Definition vector2.hpp:250