HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
extent2.hpp
Go to the documentation of this file.
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
9#pragma once
10
11#include "vector2.hpp"
12#include "../utility/utility.hpp"
13#include "../macros.hpp"
14#include <hikocpu/hikocpu.hpp>
15#include <compare>
16#include <concepts>
17#include <format>
18#include <ostream>
19#include <exception>
20
21hi_export_module(hikogui.geometry : extent2);
22
23hi_export namespace hi { inline namespace v1 {
24
32class extent2 {
33public:
34 using array_type = simd<float, 4>;
35 using value_type = array_type::value_type;
36
37 constexpr extent2(extent2 const&) noexcept = default;
38 constexpr extent2(extent2&&) noexcept = default;
39 constexpr extent2& operator=(extent2 const&) noexcept = default;
40 constexpr extent2& operator=(extent2&&) noexcept = default;
41
42 [[nodiscard]] constexpr explicit extent2(array_type const& other) noexcept : _v(other)
43 {
44 hi_axiom(holds_invariant());
45 }
46
49 [[nodiscard]] constexpr explicit operator array_type() const noexcept
50 {
51 return _v;
52 }
53
54 [[nodiscard]] constexpr explicit extent2(vector2 const &other) noexcept : _v(f32x4{other})
55 {
56 hi_axiom(holds_invariant());
57 }
58
59 [[nodiscard]] constexpr explicit operator vector2() const noexcept
60 {
61 return vector2{static_cast<array_type>(*this)};
62 }
63
66 [[nodiscard]] constexpr extent2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
67
73 [[nodiscard]] constexpr extent2(float width, float height) noexcept : _v(width, height, 0.0f, 0.0f)
74 {
75 hi_axiom(holds_invariant());
76 }
77
78 [[nodiscard]] constexpr static extent2 infinity() noexcept
79 {
81 }
82
83 [[nodiscard]] constexpr static extent2 large() noexcept
84 {
85 return extent2{large_number_v<float>, large_number_v<float>};
86 }
87
88 [[nodiscard]] constexpr static extent2 nan() noexcept
89 {
90 auto r = extent2{};
93 return r;
94 }
95
96 [[nodiscard]] constexpr explicit operator bool() const noexcept
97 {
98 return _v.x() != 0.0f or _v.y() != 0.0f or _v.z() != 0.0f;
99 }
100
107 [[nodiscard]] constexpr float& width() noexcept
108 {
109 return _v.x();
110 }
111
118 [[nodiscard]] constexpr float& height() noexcept
119 {
120 return _v.y();
121 }
122
129 [[nodiscard]] constexpr float width() const noexcept
130 {
131 return _v.x();
132 }
133
140 [[nodiscard]] constexpr float height() const noexcept
141 {
142 return _v.y();
143 }
144
145 [[nodiscard]] constexpr vector2 right() const noexcept
146 {
147 return vector2{_v.x000()};
148 }
149
150 [[nodiscard]] constexpr vector2 up() const noexcept
151 {
152 return vector2{_v._0y00()};
153 }
154
155 constexpr extent2& operator+=(extent2 const& rhs) noexcept
156 {
157 return *this = *this + rhs;
158 }
159
165 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, extent2 const& rhs) noexcept
166 {
167 return extent2{lhs._v + rhs._v};
168 }
169
175 [[nodiscard]] constexpr friend extent2 operator-(extent2 const& lhs, extent2 const& rhs) noexcept
176 {
177 return extent2{lhs._v - rhs._v};
178 }
179
185 [[nodiscard]] constexpr friend extent2 operator*(extent2 const& lhs, float const& rhs) noexcept
186 {
187 return extent2{lhs._v * array_type::broadcast(rhs)};
188 }
189
190 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, vector2 const& rhs) noexcept
191 {
192 return extent2{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
193 }
194
195 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, extent2 const& rhs) noexcept
196 {
197 return vector2{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
198 }
199
205 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, float const& rhs) noexcept
206 {
207 auto r = extent2{};
208 for (std::size_t i = 0; i != 2; ++i) {
209 r._v[i] = lhs._v[i] + rhs;
210 }
211
212 return r;
213 }
214
220 [[nodiscard]] constexpr friend extent2 operator*(float const& lhs, extent2 const& rhs) noexcept
221 {
222 return extent2{array_type::broadcast(lhs) * rhs._v};
223 }
224
230 [[nodiscard]] constexpr friend bool operator==(extent2 const& lhs, extent2 const& rhs) noexcept
231 {
232 return equal(lhs._v, rhs._v);
233 }
234
235 [[nodiscard]] constexpr friend std::partial_ordering operator<=>(extent2 const& lhs, extent2 const& rhs) noexcept
236 {
237 constexpr std::size_t mask = 0b0011;
238
239 auto const equal = (lhs._v == rhs._v).mask() & mask;
240 if (equal == mask) {
241 // Only equivalent if all elements are equal.
242 return std::partial_ordering::equivalent;
243 }
244
245 auto const less = (lhs._v < rhs._v).mask() & mask;
246 if ((less | equal) == mask) {
247 // If one or more elements is less (but none are greater) then the ordering is less.
248 return std::partial_ordering::less;
249 }
250
251 auto const greater = (lhs._v > rhs._v).mask() & mask;
252 if ((greater | equal) == mask) {
253 // If one or more elements is greater (but none are less) then the ordering is greater.
254 return std::partial_ordering::greater;
255 }
256
257 // Some elements are less and others are greater, we don't have an ordering.
258 return std::partial_ordering::unordered;
259 }
260
265 [[nodiscard]] hi_force_inline constexpr friend float squared_hypot(extent2 const& rhs) noexcept
266 {
267 return dot<0b0011>(rhs._v, rhs._v).x();
268 }
269
274 [[nodiscard]] friend float hypot(extent2 const& rhs) noexcept
275 {
276 return hypot<0b0011>(rhs._v).x();
277 }
278
283 [[nodiscard]] constexpr friend float rcp_hypot(extent2 const& rhs) noexcept
284 {
285 return rhypot<0b0011>(rhs._v).x();
286 }
287
292 [[nodiscard]] constexpr friend extent2 normalize(extent2 const& rhs) noexcept
293 {
294 return extent2{normalize<0b0011>(rhs._v)};
295 }
296
297 [[nodiscard]] constexpr friend extent2 ceil(extent2 const& rhs) noexcept
298 {
299 return extent2{ceil(array_type{rhs})};
300 }
301
302 [[nodiscard]] constexpr friend extent2 floor(extent2 const& rhs) noexcept
303 {
304 return extent2{floor(static_cast<array_type>(rhs))};
305 }
306
307 [[nodiscard]] constexpr friend extent2 round(extent2 const& rhs) noexcept
308 {
309 return extent2{round(static_cast<array_type>(rhs))};
310 }
311
312 [[nodiscard]] constexpr friend extent2 min(extent2 const& lhs, extent2 const& rhs) noexcept
313 {
314 return extent2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
315 }
316
317 [[nodiscard]] constexpr friend extent2 max(extent2 const& lhs, extent2 const& rhs) noexcept
318 {
319 return extent2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
320 }
321
322 [[nodiscard]] constexpr friend extent2 clamp(extent2 const& value, extent2 const& min, extent2 const& max) noexcept
323 {
324 return extent2{clamp(static_cast<array_type>(value), static_cast<array_type>(min), static_cast<array_type>(max))};
325 }
326
331 [[nodiscard]] constexpr bool holds_invariant() const noexcept
332 {
333 return _v.x() >= 0.0f and _v.y() >= 0.0f and _v.z() == 0.0f and _v.w() == 0.0f;
334 }
335
336 [[nodiscard]] friend std::string to_string(extent2 const& rhs) noexcept
337 {
338 return std::format("[{}, {}]", rhs._v.x(), rhs._v.y());
339 }
340
341 friend std::ostream& operator<<(std::ostream& lhs, extent2 const& rhs) noexcept
342 {
343 return lhs << to_string(rhs);
344 }
345
346private:
347 array_type _v;
348};
349
350}} // namespace hi::v1
351
352// XXX #617 MSVC bug does not handle partial specialization in modules.
353hi_export template<>
354struct std::formatter<hi::extent2, char> : std::formatter<std::string, char> {
355 auto format(hi::extent2 const& t, auto& fc) const
356 {
357 return std::formatter<std::string, char>::format(std::format("[{}, {}]", t.width(), t.height()), fc);
358 }
359};
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition simd_intf.hpp:18
A high-level geometric extent.
Definition extent2.hpp:32
constexpr float width() const noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:129
friend float hypot(extent2 const &rhs) noexcept
Get the length of the extent.
Definition extent2.hpp:274
constexpr friend extent2 operator*(float const &lhs, extent2 const &rhs) noexcept
Scale the extent by a scaler.
Definition extent2.hpp:220
constexpr float height() const noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:140
hi_force_inline constexpr friend float squared_hypot(extent2 const &rhs) noexcept
Get the squared length of the extent.
Definition extent2.hpp:265
constexpr friend extent2 operator+(extent2 const &lhs, float const &rhs) noexcept
Add a scaler to the extent.
Definition extent2.hpp:205
constexpr friend extent2 operator*(extent2 const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent2.hpp:185
constexpr friend bool operator==(extent2 const &lhs, extent2 const &rhs) noexcept
Compare if two extents are equal.
Definition extent2.hpp:230
constexpr friend extent2 operator-(extent2 const &lhs, extent2 const &rhs) noexcept
Subtract two extents from each other.
Definition extent2.hpp:175
constexpr friend float rcp_hypot(extent2 const &rhs) noexcept
Get the length of the extent.
Definition extent2.hpp:283
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:107
constexpr extent2() noexcept
Construct a empty extent / zero length.
Definition extent2.hpp:66
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:118
constexpr friend extent2 operator+(extent2 const &lhs, extent2 const &rhs) noexcept
Add two extents from each other.
Definition extent2.hpp:165
constexpr bool holds_invariant() const noexcept
Check if the extent is valid.
Definition extent2.hpp:331
constexpr friend extent2 normalize(extent2 const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent2.hpp:292
constexpr extent2(float width, float height) noexcept
Construct a 3D extent from width, height and depth.
Definition extent2.hpp:73
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
T infinity(T... args)
T signaling_NaN(T... args)