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#include "../macros.hpp"
9
10namespace hi { inline namespace v1 {
11
19class vector2 {
20public:
21 using array_type = simd<float, 4>;
22 using value_type = array_type::value_type;
23
24 constexpr vector2(vector2 const&) noexcept = default;
25 constexpr vector2(vector2&&) noexcept = default;
26 constexpr vector2& operator=(vector2 const&) noexcept = default;
27 constexpr vector2& operator=(vector2&&) noexcept = default;
28
31 [[nodiscard]] constexpr explicit operator array_type() const noexcept
32 {
33 return _v;
34 }
35
38 [[nodiscard]] constexpr explicit vector2(array_type const& other) noexcept : _v(other)
39 {
40 hi_axiom(holds_invariant());
41 }
42
45 [[nodiscard]] constexpr vector2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
46
52 [[nodiscard]] constexpr vector2(float x, float y) noexcept : _v(x, y, 0.0f, 0.0f) {}
53
57 [[nodiscard]] constexpr float& x() noexcept
58 {
59 return _v.x();
60 }
61
65 [[nodiscard]] constexpr float& y() noexcept
66 {
67 return _v.y();
68 }
69
73 [[nodiscard]] constexpr float x() const noexcept
74 {
75 return _v.x();
76 }
77
81 [[nodiscard]] constexpr float y() const noexcept
82 {
83 return _v.y();
84 }
85
90 {
91 return vector2{-_v};
92 }
93
94 constexpr vector2& operator+=(vector2 const& rhs) noexcept
95 {
96 return *this = *this + rhs;
97 }
98
99 constexpr vector2& operator-=(vector2 const& rhs) noexcept
100 {
101 return *this = *this - rhs;
102 }
103
104 constexpr vector2& operator*=(float const& rhs) noexcept
105 {
106 return *this = *this * rhs;
107 }
108
114 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, vector2 const& rhs) noexcept
115 {
116 return vector2{lhs._v + rhs._v};
117 }
118
124 [[nodiscard]] constexpr friend vector2 operator-(vector2 const& lhs, vector2 const& rhs) noexcept
125 {
126 return vector2{lhs._v - rhs._v};
127 }
128
134 [[nodiscard]] constexpr friend vector2 operator*(vector2 const& lhs, float const& rhs) noexcept
135 {
136 return vector2{lhs._v * rhs};
137 }
138
144 [[nodiscard]] constexpr friend vector2 operator*(float const& lhs, vector2 const& rhs) noexcept
145 {
146 return vector2{array_type::broadcast(lhs) * rhs._v};
147 }
148
154 [[nodiscard]] constexpr friend bool operator==(vector2 const& lhs, vector2 const& rhs) noexcept
155 {
156 return equal(lhs._v, rhs._v);
157 }
158
163 [[nodiscard]] constexpr friend float squared_hypot(vector2 const& rhs) noexcept
164 {
165 return squared_hypot<0b0011>(rhs._v);
166 }
167
172 [[nodiscard]] friend float hypot(vector2 const& rhs) noexcept
173 {
174 return hypot<0b0011>(rhs._v);
175 }
176
181 [[nodiscard]] constexpr friend float rcp_hypot(vector2 const& rhs) noexcept
182 {
183 return rcp_hypot<0b0011>(rhs._v);
184 }
185
190 [[nodiscard]] constexpr friend vector2 normalize(vector2 const& rhs) noexcept
191 {
192 return vector2{normalize<0b0011>(rhs._v)};
193 }
194
200 [[nodiscard]] constexpr friend float dot(vector2 const& lhs, vector2 const& rhs) noexcept
201 {
202 return dot<0b0011>(lhs._v, rhs._v);
203 }
204
210 [[nodiscard]] constexpr friend float det(vector2 const& lhs, vector2 const& rhs) noexcept
211 {
212 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
213 }
214
219 [[nodiscard]] constexpr friend vector2 cross(vector2 const& rhs) noexcept
220 {
221 return vector2{cross_2D(static_cast<f32x4>(rhs))};
222 }
223
232 [[nodiscard]] constexpr friend float cross(vector2 const& lhs, vector2 const& rhs) noexcept
233 {
234 return cross_2D(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs));
235 }
236
241 [[nodiscard]] constexpr friend vector2 normal(vector2 const& rhs) noexcept
242 {
243 return normalize(cross(rhs));
244 }
245
251 [[nodiscard]] friend constexpr vector2 min(vector2 const& lhs, vector2 const& rhs) noexcept
252 {
253 return vector2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
254 }
255
261 [[nodiscard]] friend constexpr vector2 max(vector2 const& lhs, vector2 const& rhs) noexcept
262 {
263 return vector2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
264 }
265
268 [[nodiscard]] friend constexpr vector2 round(vector2 const& rhs) noexcept
269 {
270 return vector2{round(static_cast<array_type>(rhs))};
271 }
272
275 [[nodiscard]] friend constexpr vector2 ceil(vector2 const& rhs) noexcept
276 {
277 return vector2{ceil(static_cast<array_type>(rhs))};
278 }
279
282 [[nodiscard]] friend constexpr vector2 floor(vector2 const& rhs) noexcept
283 {
284 return vector2{floor(static_cast<array_type>(rhs))};
285 }
286
291 {
292 return _v.z() == 0.0f and _v.w() == 0.0f;
293 }
294
295 [[nodiscard]] friend std::string to_string(vector2 const& rhs) noexcept
296 {
297 return std::format("({}, {})", rhs._v.x(), rhs._v.y());
298 }
299
300 friend std::ostream& operator<<(std::ostream& lhs, vector2 const& rhs) noexcept
301 {
302 return lhs << to_string(rhs);
303 }
304
305private:
306 array_type _v;
307};
308
309}} // namespace hi::v1
310
311template<typename CharT>
312struct std::formatter<hi::vector2, CharT> {
313 auto parse(auto& pc)
314 {
315 return pc.end();
316 }
317
318 auto format(hi::vector2 const& t, auto& fc) const
319 {
320 return std::vformat_to(fc.out(), "({}, {})", std::make_format_args(t.x(), t.y()));
321 }
322};
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:19
constexpr vector2() noexcept
Construct a empty vector / zero length.
Definition vector2.hpp:45
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:57
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector2.hpp:81
constexpr vector2(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector2.hpp:38
friend constexpr vector2 round(vector2 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector2.hpp:268
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector2.hpp:290
constexpr friend vector2 normalize(vector2 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector2.hpp:190
constexpr vector2(float x, float y) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector2.hpp:52
constexpr friend vector2 cross(vector2 const &rhs) noexcept
Get the cross product of one 2D vectors.
Definition vector2.hpp:219
constexpr friend float dot(vector2 const &lhs, vector2 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector2.hpp:200
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:261
friend constexpr vector2 ceil(vector2 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector2.hpp:275
friend constexpr vector2 floor(vector2 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector2.hpp:282
constexpr friend vector2 normal(vector2 const &rhs) noexcept
Get the normal on a 2D vector.
Definition vector2.hpp:241
friend float hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:172
constexpr friend bool operator==(vector2 const &lhs, vector2 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector2.hpp:154
constexpr friend vector2 operator*(float const &lhs, vector2 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:144
constexpr friend float cross(vector2 const &lhs, vector2 const &rhs) noexcept
Get the cross product between two 2D vectors.
Definition vector2.hpp:232
constexpr friend vector2 operator+(vector2 const &lhs, vector2 const &rhs) noexcept
Add two vectors from each other.
Definition vector2.hpp:114
constexpr friend vector2 operator-(vector2 const &lhs, vector2 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector2.hpp:124
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector2.hpp:65
constexpr friend float det(vector2 const &lhs, vector2 const &rhs) noexcept
Get the determinate between two vectors.
Definition vector2.hpp:210
constexpr friend float rcp_hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:181
constexpr friend float squared_hypot(vector2 const &rhs) noexcept
Get the squared length of the vector.
Definition vector2.hpp:163
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector2.hpp:73
constexpr friend vector2 operator*(vector2 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:134
constexpr vector2 operator-() const noexcept
Mirror this vector.
Definition vector2.hpp:89
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:251