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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <hikocpu/hikocpu.hpp>
10#include <exception>
11#include <format>
12#include <ostream>
13#include <compare>
14
15hi_export_module(hikogui.geometry : vector2);
16
17hi_export namespace hi {
18inline namespace v1 {
19
27class vector2 {
28public:
30 using value_type = array_type::value_type;
31
32 constexpr vector2(vector2 const&) noexcept = default;
33 constexpr vector2(vector2&&) noexcept = default;
34 constexpr vector2& operator=(vector2 const&) noexcept = default;
35 constexpr vector2& operator=(vector2&&) noexcept = default;
36
39 [[nodiscard]] constexpr explicit operator array_type() const noexcept
40 {
41 return _v;
42 }
43
46 [[nodiscard]] constexpr explicit vector2(array_type const& other) noexcept : _v(other)
47 {
48 hi_axiom(holds_invariant());
49 }
50
53 [[nodiscard]] constexpr vector2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
54
60 [[nodiscard]] constexpr vector2(float x, float y) noexcept : _v(x, y, 0.0f, 0.0f) {}
61
65 [[nodiscard]] constexpr float& x() noexcept
66 {
67 return _v.x();
68 }
69
73 [[nodiscard]] constexpr float& y() noexcept
74 {
75 return _v.y();
76 }
77
81 [[nodiscard]] constexpr float x() const noexcept
82 {
83 return _v.x();
84 }
85
89 [[nodiscard]] constexpr float y() const noexcept
90 {
91 return _v.y();
92 }
93
97 [[nodiscard]] constexpr vector2 operator-() const noexcept
98 {
99 return vector2{-_v};
100 }
101
102 constexpr vector2& operator+=(vector2 const& rhs) noexcept
103 {
104 return *this = *this + rhs;
105 }
106
107 constexpr vector2& operator-=(vector2 const& rhs) noexcept
108 {
109 return *this = *this - rhs;
110 }
111
112 constexpr vector2& operator*=(float const& rhs) noexcept
113 {
114 return *this = *this * rhs;
115 }
116
122 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, vector2 const& rhs) noexcept
123 {
124 return vector2{lhs._v + rhs._v};
125 }
126
132 [[nodiscard]] constexpr friend vector2 operator-(vector2 const& lhs, vector2 const& rhs) noexcept
133 {
134 return vector2{lhs._v - rhs._v};
135 }
136
142 [[nodiscard]] constexpr friend vector2 operator*(vector2 const& lhs, float const& rhs) noexcept
143 {
144 return vector2{lhs._v * array_type::broadcast(rhs)};
145 }
146
152 [[nodiscard]] constexpr friend vector2 operator*(float const& lhs, vector2 const& rhs) noexcept
153 {
154 return vector2{array_type::broadcast(lhs) * rhs._v};
155 }
156
162 [[nodiscard]] constexpr friend bool operator==(vector2 const& lhs, vector2 const& rhs) noexcept
163 {
164 return equal(lhs._v, rhs._v);
165 }
166
171 [[nodiscard]] constexpr friend float squared_hypot(vector2 const& rhs) noexcept
172 {
173 return dot<0b0011>(rhs._v, rhs._v).x();
174 }
175
180 [[nodiscard]] friend float hypot(vector2 const& rhs) noexcept
181 {
182 return hypot<0b0011>(rhs._v).x();
183 }
184
189 [[nodiscard]] constexpr friend float rcp_hypot(vector2 const& rhs) noexcept
190 {
191 return rhypot<0b0011>(rhs._v).x();
192 }
193
198 [[nodiscard]] constexpr friend vector2 normalize(vector2 const& rhs) noexcept
199 {
200 return vector2{normalize<0b0011>(rhs._v)};
201 }
202
208 [[nodiscard]] constexpr friend float dot(vector2 const& lhs, vector2 const& rhs) noexcept
209 {
210 return dot<0b0011>(lhs._v, rhs._v).x();
211 }
212
218 [[nodiscard]] constexpr friend float det(vector2 const& lhs, vector2 const& rhs) noexcept
219 {
220 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
221 }
222
227 [[nodiscard]] constexpr friend vector2 cross(vector2 const& rhs) noexcept
228 {
229 return vector2{-rhs._v.y(), rhs._v.x()};
230 }
231
240 [[nodiscard]] constexpr friend float cross(vector2 const& lhs, vector2 const& rhs) noexcept
241 {
242 auto const tmp1 = rhs._v.yxwz();
243 auto const tmp2 = lhs._v * tmp1;
244 auto const tmp3 = hsub(tmp2, tmp2);
245 return tmp3.x();
246 }
247
252 [[nodiscard]] constexpr friend vector2 normal(vector2 const& rhs) noexcept
253 {
254 return normalize(cross(rhs));
255 }
256
262 [[nodiscard]] friend constexpr vector2 min(vector2 const& lhs, vector2 const& rhs) noexcept
263 {
264 return vector2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
265 }
266
272 [[nodiscard]] friend constexpr vector2 max(vector2 const& lhs, vector2 const& rhs) noexcept
273 {
274 return vector2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
275 }
276
279 [[nodiscard]] friend constexpr vector2 round(vector2 const& rhs) noexcept
280 {
281 return vector2{round(static_cast<array_type>(rhs))};
282 }
283
286 [[nodiscard]] friend constexpr vector2 ceil(vector2 const& rhs) noexcept
287 {
288 return vector2{ceil(static_cast<array_type>(rhs))};
289 }
290
293 [[nodiscard]] friend constexpr vector2 floor(vector2 const& rhs) noexcept
294 {
295 return vector2{floor(static_cast<array_type>(rhs))};
296 }
297
301 [[nodiscard]] constexpr bool holds_invariant() const noexcept
302 {
303 return _v.z() == 0.0f and _v.w() == 0.0f;
304 }
305
306 [[nodiscard]] friend std::string to_string(vector2 const& rhs) noexcept
307 {
308 return std::format("({}, {})", rhs._v.x(), rhs._v.y());
309 }
310
311 friend std::ostream& operator<<(std::ostream& lhs, vector2 const& rhs) noexcept
312 {
313 return lhs << to_string(rhs);
314 }
315
316private:
317 array_type _v;
318};
319
320} // namespace v1
321} // namespace hi::v1
322
323// XXX #617 MSVC bug does not handle partial specialization in modules.
324hi_export template<>
325struct std::formatter<hi::vector2, char> : std::formatter<std::string, char> {
326 auto format(hi::vector2 const& t, auto& fc) const
327 {
328 return std::formatter<std::string, char>::format(std::format("({}, {})", t.x(), t.y()), fc);
329 }
330};
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
constexpr vector2() noexcept
Construct a empty vector / zero length.
Definition vector2.hpp:53
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:65
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector2.hpp:89
constexpr vector2(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector2.hpp:46
friend constexpr vector2 round(vector2 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector2.hpp:279
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector2.hpp:301
constexpr friend vector2 normalize(vector2 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector2.hpp:198
constexpr vector2(float x, float y) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector2.hpp:60
constexpr friend vector2 cross(vector2 const &rhs) noexcept
Get the cross product of one 2D vectors.
Definition vector2.hpp:227
constexpr friend float dot(vector2 const &lhs, vector2 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector2.hpp:208
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:272
friend constexpr vector2 ceil(vector2 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector2.hpp:286
friend constexpr vector2 floor(vector2 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector2.hpp:293
constexpr friend vector2 normal(vector2 const &rhs) noexcept
Get the normal on a 2D vector.
Definition vector2.hpp:252
friend float hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:180
constexpr friend bool operator==(vector2 const &lhs, vector2 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector2.hpp:162
constexpr friend vector2 operator*(float const &lhs, vector2 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:152
constexpr friend float cross(vector2 const &lhs, vector2 const &rhs) noexcept
Get the cross product between two 2D vectors.
Definition vector2.hpp:240
constexpr friend vector2 operator+(vector2 const &lhs, vector2 const &rhs) noexcept
Add two vectors from each other.
Definition vector2.hpp:122
constexpr friend vector2 operator-(vector2 const &lhs, vector2 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector2.hpp:132
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector2.hpp:73
constexpr friend float det(vector2 const &lhs, vector2 const &rhs) noexcept
Get the determinate between two vectors.
Definition vector2.hpp:218
constexpr friend float rcp_hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:189
constexpr friend float squared_hypot(vector2 const &rhs) noexcept
Get the squared length of the vector.
Definition vector2.hpp:171
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector2.hpp:81
constexpr friend vector2 operator*(vector2 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:142
constexpr vector2 operator-() const noexcept
Mirror this vector.
Definition vector2.hpp:97
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:262