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 "../SIMD/module.hpp"
13#include "../utility/module.hpp"
14#include <compare>
15#include <concepts>
16
17namespace hi { inline namespace v1 {
18
26class extent2 {
27public:
28 using array_type = simd<float, 4>;
29 using value_type = array_type::value_type;
30
31 constexpr extent2(extent2 const&) noexcept = default;
32 constexpr extent2(extent2&&) noexcept = default;
33 constexpr extent2& operator=(extent2 const&) noexcept = default;
34 constexpr extent2& operator=(extent2&&) noexcept = default;
35
36 [[nodiscard]] constexpr explicit extent2(array_type const& other) noexcept : _v(other)
37 {
39 }
40
43 [[nodiscard]] constexpr explicit operator array_type() const noexcept
44 {
45 return _v;
46 }
47
48 [[nodiscard]] constexpr explicit extent2(vector2 const &other) noexcept : _v(f32x4{other})
49 {
51 }
52
53 [[nodiscard]] constexpr explicit operator vector2() const noexcept
54 {
55 return vector2{static_cast<array_type>(*this)};
56 }
57
60 [[nodiscard]] constexpr extent2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
61
67 [[nodiscard]] constexpr extent2(float width, float height) noexcept : _v(width, height, 0.0f, 0.0f)
68 {
70 }
71
72 [[nodiscard]] static constexpr extent2 infinity() noexcept
73 {
75 }
76
77 [[nodiscard]] static constexpr extent2 large() noexcept
78 {
79 return extent2{large_number_v<float>, large_number_v<float>};
80 }
81
82 [[nodiscard]] static constexpr extent2 nan() noexcept
83 {
84 auto r = extent2{};
87 return r;
88 }
89
90 [[nodiscard]] constexpr explicit operator bool() const noexcept
91 {
92 return _v.x() != 0.0f or _v.y() != 0.0f or _v.z() != 0.0f;
93 }
94
101 [[nodiscard]] constexpr float& width() noexcept
102 {
103 return _v.x();
104 }
105
112 [[nodiscard]] constexpr float& height() noexcept
113 {
114 return _v.y();
115 }
116
123 [[nodiscard]] constexpr float width() const noexcept
124 {
125 return _v.x();
126 }
127
134 [[nodiscard]] constexpr float height() const noexcept
135 {
136 return _v.y();
137 }
138
139 [[nodiscard]] constexpr vector2 right() const noexcept
140 {
141 return vector2{_v.x000()};
142 }
143
144 [[nodiscard]] constexpr vector2 up() const noexcept
145 {
146 return vector2{_v._0y00()};
147 }
148
149 constexpr extent2& operator+=(extent2 const& rhs) noexcept
150 {
151 return *this = *this + rhs;
152 }
153
159 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, extent2 const& rhs) noexcept
160 {
161 return extent2{lhs._v + rhs._v};
162 }
163
169 [[nodiscard]] constexpr friend extent2 operator-(extent2 const& lhs, extent2 const& rhs) noexcept
170 {
171 return extent2{lhs._v - rhs._v};
172 }
173
179 [[nodiscard]] constexpr friend extent2 operator*(extent2 const& lhs, float const& rhs) noexcept
180 {
181 return extent2{lhs._v * rhs};
182 }
183
184 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, vector2 const& rhs) noexcept
185 {
186 return extent2{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
187 }
188
189 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, extent2 const& rhs) noexcept
190 {
191 return vector2{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
192 }
193
199 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, float const& rhs) noexcept
200 {
201 auto r = extent2{};
202 for (std::size_t i = 0; i != 2; ++i) {
203 r._v[i] = lhs._v[i] + rhs;
204 }
205
206 return r;
207 }
208
214 [[nodiscard]] constexpr friend extent2 operator*(float const& lhs, extent2 const& rhs) noexcept
215 {
216 return extent2{lhs * rhs._v};
217 }
218
224 [[nodiscard]] constexpr friend bool operator==(extent2 const& lhs, extent2 const& rhs) noexcept
225 {
226 return equal(lhs._v, rhs._v);
227 }
228
229 [[nodiscard]] constexpr friend std::partial_ordering operator<=>(extent2 const& lhs, extent2 const& rhs) noexcept
230 {
231 constexpr std::size_t mask = 0b0011;
232
233 hilet equal = (lhs._v == rhs._v).mask() & mask;
234 if (equal == mask) {
235 // Only equivalent if all elements are equal.
236 return std::partial_ordering::equivalent;
237 }
238
239 hilet less = (lhs._v < rhs._v).mask() & mask;
240 if ((less | equal) == mask) {
241 // If one or more elements is less (but none are greater) then the ordering is less.
242 return std::partial_ordering::less;
243 }
244
245 hilet greater = (lhs._v > rhs._v).mask() & mask;
246 if ((greater | equal) == mask) {
247 // If one or more elements is greater (but none are less) then the ordering is greater.
248 return std::partial_ordering::greater;
249 }
250
251 // Some elements are less and others are greater, we don't have an ordering.
252 return std::partial_ordering::unordered;
253 }
254
259 [[nodiscard]] hi_force_inline constexpr friend float squared_hypot(extent2 const& rhs) noexcept
260 {
261 return squared_hypot<0b0011>(rhs._v);
262 }
263
268 [[nodiscard]] friend float hypot(extent2 const& rhs) noexcept
269 {
270 return hypot<0b0011>(rhs._v);
271 }
272
277 [[nodiscard]] constexpr friend float rcp_hypot(extent2 const& rhs) noexcept
278 {
279 return rcp_hypot<0b0011>(rhs._v);
280 }
281
286 [[nodiscard]] constexpr friend extent2 normalize(extent2 const& rhs) noexcept
287 {
288 return extent2{normalize<0b0011>(rhs._v)};
289 }
290
291 [[nodiscard]] constexpr friend extent2 ceil(extent2 const& rhs) noexcept
292 {
293 return extent2{ceil(array_type{rhs})};
294 }
295
296 [[nodiscard]] constexpr friend extent2 floor(extent2 const& rhs) noexcept
297 {
298 return extent2{floor(static_cast<array_type>(rhs))};
299 }
300
301 [[nodiscard]] constexpr friend extent2 round(extent2 const& rhs) noexcept
302 {
303 return extent2{round(static_cast<array_type>(rhs))};
304 }
305
306 [[nodiscard]] constexpr friend extent2 min(extent2 const& lhs, extent2 const& rhs) noexcept
307 {
308 return extent2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
309 }
310
311 [[nodiscard]] constexpr friend extent2 max(extent2 const& lhs, extent2 const& rhs) noexcept
312 {
313 return extent2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
314 }
315
316 [[nodiscard]] constexpr friend extent2 clamp(extent2 const& value, extent2 const& min, extent2 const& max) noexcept
317 {
318 return extent2{clamp(static_cast<array_type>(value), static_cast<array_type>(min), static_cast<array_type>(max))};
319 }
320
325 [[nodiscard]] constexpr bool holds_invariant() const noexcept
326 {
327 return _v.x() >= 0.0f and _v.y() >= 0.0f and _v.z() == 0.0f and _v.w() == 0.0f;
328 }
329
330 [[nodiscard]] friend std::string to_string(extent2 const& rhs) noexcept
331 {
332 return std::format("[{}, {}]", rhs._v.x(), rhs._v.y());
333 }
334
335 friend std::ostream& operator<<(std::ostream& lhs, extent2 const& rhs) noexcept
336 {
337 return lhs << to_string(rhs);
338 }
339
340private:
341 array_type _v;
342};
343
344}} // namespace hi::v1
345
346template<typename CharT>
347struct std::formatter<hi::extent2, CharT> {
348 auto parse(auto& pc)
349 {
350 return pc.end();
351 }
352
353 auto format(hi::extent2 const& t, auto& fc)
354 {
355 return std::vformat_to(fc.out(), "[{}, {}]", std::make_format_args(t.width(), t.height()));
356 }
357};
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ 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 extent.
Definition extent2.hpp:26
constexpr float width() const noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:123
friend float hypot(extent2 const &rhs) noexcept
Get the length of the extent.
Definition extent2.hpp:268
constexpr friend extent2 operator*(float const &lhs, extent2 const &rhs) noexcept
Scale the extent by a scaler.
Definition extent2.hpp:214
constexpr float height() const noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:134
hi_force_inline constexpr friend float squared_hypot(extent2 const &rhs) noexcept
Get the squared length of the extent.
Definition extent2.hpp:259
constexpr friend extent2 operator+(extent2 const &lhs, float const &rhs) noexcept
Add a scaler to the extent.
Definition extent2.hpp:199
constexpr friend extent2 operator*(extent2 const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent2.hpp:179
constexpr friend bool operator==(extent2 const &lhs, extent2 const &rhs) noexcept
Compare if two extents are equal.
Definition extent2.hpp:224
constexpr friend extent2 operator-(extent2 const &lhs, extent2 const &rhs) noexcept
Subtract two extents from each other.
Definition extent2.hpp:169
constexpr friend float rcp_hypot(extent2 const &rhs) noexcept
Get the length of the extent.
Definition extent2.hpp:277
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:101
constexpr extent2() noexcept
Construct a empty extent / zero length.
Definition extent2.hpp:60
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:112
constexpr friend extent2 operator+(extent2 const &lhs, extent2 const &rhs) noexcept
Add two extents from each other.
Definition extent2.hpp:159
constexpr bool holds_invariant() const noexcept
Check if the extent is valid.
Definition extent2.hpp:325
constexpr friend extent2 normalize(extent2 const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent2.hpp:286
constexpr extent2(float width, float height) noexcept
Construct a 3D extent from width, height and depth.
Definition extent2.hpp:67
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:18
T infinity(T... args)
T signaling_NaN(T... args)