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 "../SIMD/SIMD.hpp"
9#include "../macros.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 { inline namespace v1 {
18
26class vector2 {
27public:
29 using value_type = array_type::value_type;
30
31 constexpr vector2(vector2 const&) noexcept = default;
32 constexpr vector2(vector2&&) noexcept = default;
33 constexpr vector2& operator=(vector2 const&) noexcept = default;
34 constexpr vector2& operator=(vector2&&) noexcept = default;
35
38 [[nodiscard]] constexpr explicit operator array_type() const noexcept
39 {
40 return _v;
41 }
42
45 [[nodiscard]] constexpr explicit vector2(array_type const& other) noexcept : _v(other)
46 {
47 hi_axiom(holds_invariant());
48 }
49
52 [[nodiscard]] constexpr vector2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
53
59 [[nodiscard]] constexpr vector2(float x, float y) noexcept : _v(x, y, 0.0f, 0.0f) {}
60
64 [[nodiscard]] constexpr float& x() noexcept
65 {
66 return _v.x();
67 }
68
72 [[nodiscard]] constexpr float& y() noexcept
73 {
74 return _v.y();
75 }
76
80 [[nodiscard]] constexpr float x() const noexcept
81 {
82 return _v.x();
83 }
84
88 [[nodiscard]] constexpr float y() const noexcept
89 {
90 return _v.y();
91 }
92
97 {
98 return vector2{-_v};
99 }
100
101 constexpr vector2& operator+=(vector2 const& rhs) noexcept
102 {
103 return *this = *this + rhs;
104 }
105
106 constexpr vector2& operator-=(vector2 const& rhs) noexcept
107 {
108 return *this = *this - rhs;
109 }
110
111 constexpr vector2& operator*=(float const& rhs) noexcept
112 {
113 return *this = *this * rhs;
114 }
115
121 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, vector2 const& rhs) noexcept
122 {
123 return vector2{lhs._v + rhs._v};
124 }
125
131 [[nodiscard]] constexpr friend vector2 operator-(vector2 const& lhs, vector2 const& rhs) noexcept
132 {
133 return vector2{lhs._v - rhs._v};
134 }
135
141 [[nodiscard]] constexpr friend vector2 operator*(vector2 const& lhs, float const& rhs) noexcept
142 {
143 return vector2{lhs._v * array_type::broadcast(rhs)};
144 }
145
151 [[nodiscard]] constexpr friend vector2 operator*(float const& lhs, vector2 const& rhs) noexcept
152 {
153 return vector2{array_type::broadcast(lhs) * rhs._v};
154 }
155
161 [[nodiscard]] constexpr friend bool operator==(vector2 const& lhs, vector2 const& rhs) noexcept
162 {
163 return equal(lhs._v, rhs._v);
164 }
165
170 [[nodiscard]] constexpr friend float squared_hypot(vector2 const& rhs) noexcept
171 {
172 return dot<0b0011>(rhs._v, rhs._v).x();
173 }
174
179 [[nodiscard]] friend float hypot(vector2 const& rhs) noexcept
180 {
181 return hypot<0b0011>(rhs._v).x();
182 }
183
188 [[nodiscard]] constexpr friend float rcp_hypot(vector2 const& rhs) noexcept
189 {
190 return rhypot<0b0011>(rhs._v).x();
191 }
192
197 [[nodiscard]] constexpr friend vector2 normalize(vector2 const& rhs) noexcept
198 {
199 return vector2{normalize<0b0011>(rhs._v)};
200 }
201
207 [[nodiscard]] constexpr friend float dot(vector2 const& lhs, vector2 const& rhs) noexcept
208 {
209 return dot<0b0011>(lhs._v, rhs._v).x();
210 }
211
217 [[nodiscard]] constexpr friend float det(vector2 const& lhs, vector2 const& rhs) noexcept
218 {
219 return lhs.x() * rhs.y() - lhs.y() * rhs.x();
220 }
221
226 [[nodiscard]] constexpr friend vector2 cross(vector2 const& rhs) noexcept
227 {
228 return vector2{-rhs._v.y(), rhs._v.x()};
229 }
230
239 [[nodiscard]] constexpr friend float cross(vector2 const& lhs, vector2 const& rhs) noexcept
240 {
241 auto const tmp1 = rhs._v.yxwz();
242 auto const tmp2 = lhs._v * tmp1;
243 auto const tmp3 = hsub(tmp2, tmp2);
244 return tmp3.x();
245 }
246
251 [[nodiscard]] constexpr friend vector2 normal(vector2 const& rhs) noexcept
252 {
253 return normalize(cross(rhs));
254 }
255
261 [[nodiscard]] friend constexpr vector2 min(vector2 const& lhs, vector2 const& rhs) noexcept
262 {
263 return vector2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
264 }
265
271 [[nodiscard]] friend constexpr vector2 max(vector2 const& lhs, vector2 const& rhs) noexcept
272 {
273 return vector2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
274 }
275
278 [[nodiscard]] friend constexpr vector2 round(vector2 const& rhs) noexcept
279 {
280 return vector2{round(static_cast<array_type>(rhs))};
281 }
282
285 [[nodiscard]] friend constexpr vector2 ceil(vector2 const& rhs) noexcept
286 {
287 return vector2{ceil(static_cast<array_type>(rhs))};
288 }
289
292 [[nodiscard]] friend constexpr vector2 floor(vector2 const& rhs) noexcept
293 {
294 return vector2{floor(static_cast<array_type>(rhs))};
295 }
296
301 {
302 return _v.z() == 0.0f and _v.w() == 0.0f;
303 }
304
305 [[nodiscard]] friend std::string to_string(vector2 const& rhs) noexcept
306 {
307 return std::format("({}, {})", rhs._v.x(), rhs._v.y());
308 }
309
310 friend std::ostream& operator<<(std::ostream& lhs, vector2 const& rhs) noexcept
311 {
312 return lhs << to_string(rhs);
313 }
314
315private:
316 array_type _v;
317};
318
319}} // namespace hi::v1
320
321// XXX #617 MSVC bug does not handle partial specialization in modules.
322hi_export template<>
323struct std::formatter<hi::vector2, char> {
324 auto parse(auto& pc)
325 {
326 return pc.end();
327 }
328
329 auto format(hi::vector2 const& t, auto& fc) const
330 {
331 return std::vformat_to(fc.out(), "({}, {})", std::make_format_args(t.x(), t.y()));
332 }
333};
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:26
constexpr vector2() noexcept
Construct a empty vector / zero length.
Definition vector2.hpp:52
constexpr float & x() noexcept
Access the x element from the vector.
Definition vector2.hpp:64
constexpr float y() const noexcept
Access the y element from the vector.
Definition vector2.hpp:88
constexpr vector2(array_type const &other) noexcept
Construct a vector from a array_type-simd.
Definition vector2.hpp:45
friend constexpr vector2 round(vector2 const &rhs) noexcept
Round the elements of the vector toward nearest integer.
Definition vector2.hpp:278
constexpr bool holds_invariant() const noexcept
Check if the vector is valid.
Definition vector2.hpp:300
constexpr friend vector2 normalize(vector2 const &rhs) noexcept
Normalize a vector to a unit vector.
Definition vector2.hpp:197
constexpr vector2(float x, float y) noexcept
Construct a 3D vector from x, y and z elements.
Definition vector2.hpp:59
constexpr friend vector2 cross(vector2 const &rhs) noexcept
Get the cross product of one 2D vectors.
Definition vector2.hpp:226
constexpr friend float dot(vector2 const &lhs, vector2 const &rhs) noexcept
Get the dot product between two vectors.
Definition vector2.hpp:207
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:271
friend constexpr vector2 ceil(vector2 const &rhs) noexcept
Round the elements of the vector toward upward and to the right.
Definition vector2.hpp:285
friend constexpr vector2 floor(vector2 const &rhs) noexcept
Round the elements of the vector toward downward and to the left.
Definition vector2.hpp:292
constexpr friend vector2 normal(vector2 const &rhs) noexcept
Get the normal on a 2D vector.
Definition vector2.hpp:251
friend float hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:179
constexpr friend bool operator==(vector2 const &lhs, vector2 const &rhs) noexcept
Compare if two vectors are equal.
Definition vector2.hpp:161
constexpr friend vector2 operator*(float const &lhs, vector2 const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:151
constexpr friend float cross(vector2 const &lhs, vector2 const &rhs) noexcept
Get the cross product between two 2D vectors.
Definition vector2.hpp:239
constexpr friend vector2 operator+(vector2 const &lhs, vector2 const &rhs) noexcept
Add two vectors from each other.
Definition vector2.hpp:121
constexpr friend vector2 operator-(vector2 const &lhs, vector2 const &rhs) noexcept
Subtract two vectors from each other.
Definition vector2.hpp:131
constexpr float & y() noexcept
Access the y element from the vector.
Definition vector2.hpp:72
constexpr friend float det(vector2 const &lhs, vector2 const &rhs) noexcept
Get the determinate between two vectors.
Definition vector2.hpp:217
constexpr friend float rcp_hypot(vector2 const &rhs) noexcept
Get the length of the vector.
Definition vector2.hpp:188
constexpr friend float squared_hypot(vector2 const &rhs) noexcept
Get the squared length of the vector.
Definition vector2.hpp:170
constexpr float x() const noexcept
Access the x element from the vector.
Definition vector2.hpp:80
constexpr friend vector2 operator*(vector2 const &lhs, float const &rhs) noexcept
Scale the vector by a scaler.
Definition vector2.hpp:141
constexpr vector2 operator-() const noexcept
Mirror this vector.
Definition vector2.hpp:96
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:261