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/utility.hpp"
14#include "../macros.hpp"
15#include <compare>
16#include <concepts>
17
18
19
20namespace hi { inline namespace v1 {
21
29class extent2 {
30public:
31 using array_type = simd<float, 4>;
32 using value_type = array_type::value_type;
33
34 constexpr extent2(extent2 const&) noexcept = default;
35 constexpr extent2(extent2&&) noexcept = default;
36 constexpr extent2& operator=(extent2 const&) noexcept = default;
37 constexpr extent2& operator=(extent2&&) noexcept = default;
38
39 [[nodiscard]] constexpr explicit extent2(array_type const& other) noexcept : _v(other)
40 {
41 hi_axiom(holds_invariant());
42 }
43
46 [[nodiscard]] constexpr explicit operator array_type() const noexcept
47 {
48 return _v;
49 }
50
51 [[nodiscard]] constexpr explicit extent2(vector2 const &other) noexcept : _v(f32x4{other})
52 {
53 hi_axiom(holds_invariant());
54 }
55
56 [[nodiscard]] constexpr explicit operator vector2() const noexcept
57 {
58 return vector2{static_cast<array_type>(*this)};
59 }
60
63 [[nodiscard]] constexpr extent2() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f) {}
64
70 [[nodiscard]] constexpr extent2(float width, float height) noexcept : _v(width, height, 0.0f, 0.0f)
71 {
72 hi_axiom(holds_invariant());
73 }
74
75 [[nodiscard]] constexpr static extent2 infinity() noexcept
76 {
78 }
79
80 [[nodiscard]] constexpr static extent2 large() noexcept
81 {
83 }
84
85 [[nodiscard]] constexpr static extent2 nan() noexcept
86 {
87 auto r = extent2{};
90 return r;
91 }
92
93 [[nodiscard]] constexpr explicit operator bool() const noexcept
94 {
95 return _v.x() != 0.0f or _v.y() != 0.0f or _v.z() != 0.0f;
96 }
97
104 [[nodiscard]] constexpr float& width() noexcept
105 {
106 return _v.x();
107 }
108
115 [[nodiscard]] constexpr float& height() noexcept
116 {
117 return _v.y();
118 }
119
126 [[nodiscard]] constexpr float width() const noexcept
127 {
128 return _v.x();
129 }
130
137 [[nodiscard]] constexpr float height() const noexcept
138 {
139 return _v.y();
140 }
141
142 [[nodiscard]] constexpr vector2 right() const noexcept
143 {
144 return vector2{_v.x000()};
145 }
146
147 [[nodiscard]] constexpr vector2 up() const noexcept
148 {
149 return vector2{_v._0y00()};
150 }
151
152 constexpr extent2& operator+=(extent2 const& rhs) noexcept
153 {
154 return *this = *this + rhs;
155 }
156
162 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, extent2 const& rhs) noexcept
163 {
164 return extent2{lhs._v + rhs._v};
165 }
166
172 [[nodiscard]] constexpr friend extent2 operator-(extent2 const& lhs, extent2 const& rhs) noexcept
173 {
174 return extent2{lhs._v - rhs._v};
175 }
176
182 [[nodiscard]] constexpr friend extent2 operator*(extent2 const& lhs, float const& rhs) noexcept
183 {
184 return extent2{lhs._v * rhs};
185 }
186
187 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, vector2 const& rhs) noexcept
188 {
189 return extent2{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
190 }
191
192 [[nodiscard]] constexpr friend vector2 operator+(vector2 const& lhs, extent2 const& rhs) noexcept
193 {
194 return vector2{static_cast<array_type>(lhs) + static_cast<array_type>(rhs)};
195 }
196
202 [[nodiscard]] constexpr friend extent2 operator+(extent2 const& lhs, float const& rhs) noexcept
203 {
204 auto r = extent2{};
205 for (std::size_t i = 0; i != 2; ++i) {
206 r._v[i] = lhs._v[i] + rhs;
207 }
208
209 return r;
210 }
211
217 [[nodiscard]] constexpr friend extent2 operator*(float const& lhs, extent2 const& rhs) noexcept
218 {
219 return extent2{lhs * rhs._v};
220 }
221
227 [[nodiscard]] constexpr friend bool operator==(extent2 const& lhs, extent2 const& rhs) noexcept
228 {
229 return equal(lhs._v, rhs._v);
230 }
231
232 [[nodiscard]] constexpr friend std::partial_ordering operator<=>(extent2 const& lhs, extent2 const& rhs) noexcept
233 {
234 constexpr std::size_t mask = 0b0011;
235
236 hilet equal = (lhs._v == rhs._v).mask() & mask;
237 if (equal == mask) {
238 // Only equivalent if all elements are equal.
239 return std::partial_ordering::equivalent;
240 }
241
242 hilet less = (lhs._v < rhs._v).mask() & mask;
243 if ((less | equal) == mask) {
244 // If one or more elements is less (but none are greater) then the ordering is less.
245 return std::partial_ordering::less;
246 }
247
248 hilet greater = (lhs._v > rhs._v).mask() & mask;
249 if ((greater | equal) == mask) {
250 // If one or more elements is greater (but none are less) then the ordering is greater.
251 return std::partial_ordering::greater;
252 }
253
254 // Some elements are less and others are greater, we don't have an ordering.
255 return std::partial_ordering::unordered;
256 }
257
262 [[nodiscard]] hi_force_inline constexpr friend float squared_hypot(extent2 const& rhs) noexcept
263 {
264 return squared_hypot<0b0011>(rhs._v);
265 }
266
271 [[nodiscard]] friend float hypot(extent2 const& rhs) noexcept
272 {
273 return hypot<0b0011>(rhs._v);
274 }
275
280 [[nodiscard]] constexpr friend float rcp_hypot(extent2 const& rhs) noexcept
281 {
282 return rcp_hypot<0b0011>(rhs._v);
283 }
284
289 [[nodiscard]] constexpr friend extent2 normalize(extent2 const& rhs) noexcept
290 {
291 return extent2{normalize<0b0011>(rhs._v)};
292 }
293
294 [[nodiscard]] constexpr friend extent2 ceil(extent2 const& rhs) noexcept
295 {
296 return extent2{ceil(array_type{rhs})};
297 }
298
299 [[nodiscard]] constexpr friend extent2 floor(extent2 const& rhs) noexcept
300 {
301 return extent2{floor(static_cast<array_type>(rhs))};
302 }
303
304 [[nodiscard]] constexpr friend extent2 round(extent2 const& rhs) noexcept
305 {
306 return extent2{round(static_cast<array_type>(rhs))};
307 }
308
309 [[nodiscard]] constexpr friend extent2 min(extent2 const& lhs, extent2 const& rhs) noexcept
310 {
311 return extent2{min(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
312 }
313
314 [[nodiscard]] constexpr friend extent2 max(extent2 const& lhs, extent2 const& rhs) noexcept
315 {
316 return extent2{max(static_cast<array_type>(lhs), static_cast<array_type>(rhs))};
317 }
318
319 [[nodiscard]] constexpr friend extent2 clamp(extent2 const& value, extent2 const& min, extent2 const& max) noexcept
320 {
321 return extent2{clamp(static_cast<array_type>(value), static_cast<array_type>(min), static_cast<array_type>(max))};
322 }
323
329 {
330 return _v.x() >= 0.0f and _v.y() >= 0.0f and _v.z() == 0.0f and _v.w() == 0.0f;
331 }
332
333 [[nodiscard]] friend std::string to_string(extent2 const& rhs) noexcept
334 {
335 return std::format("[{}, {}]", rhs._v.x(), rhs._v.y());
336 }
337
338 friend std::ostream& operator<<(std::ostream& lhs, extent2 const& rhs) noexcept
339 {
340 return lhs << to_string(rhs);
341 }
342
343private:
344 array_type _v;
345};
346
347}} // namespace hi::v1
348
349template<typename CharT>
350struct std::formatter<hi::extent2, CharT> {
351 auto parse(auto& pc)
352 {
353 return pc.end();
354 }
355
356 auto format(hi::extent2 const& t, auto& fc) const
357 {
358 return std::vformat_to(fc.out(), "[{}, {}]", std::make_format_args(t.width(), t.height()));
359 }
360};
@ 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 extent.
Definition extent2.hpp:29
constexpr float width() const noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:126
friend float hypot(extent2 const &rhs) noexcept
Get the length of the extent.
Definition extent2.hpp:271
constexpr friend extent2 operator*(float const &lhs, extent2 const &rhs) noexcept
Scale the extent by a scaler.
Definition extent2.hpp:217
constexpr float height() const noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:137
hi_force_inline constexpr friend float squared_hypot(extent2 const &rhs) noexcept
Get the squared length of the extent.
Definition extent2.hpp:262
constexpr friend extent2 operator+(extent2 const &lhs, float const &rhs) noexcept
Add a scaler to the extent.
Definition extent2.hpp:202
constexpr friend extent2 operator*(extent2 const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent2.hpp:182
constexpr friend bool operator==(extent2 const &lhs, extent2 const &rhs) noexcept
Compare if two extents are equal.
Definition extent2.hpp:227
constexpr friend extent2 operator-(extent2 const &lhs, extent2 const &rhs) noexcept
Subtract two extents from each other.
Definition extent2.hpp:172
constexpr friend float rcp_hypot(extent2 const &rhs) noexcept
Get the length of the extent.
Definition extent2.hpp:280
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:104
constexpr extent2() noexcept
Construct a empty extent / zero length.
Definition extent2.hpp:63
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:115
constexpr friend extent2 operator+(extent2 const &lhs, extent2 const &rhs) noexcept
Add two extents from each other.
Definition extent2.hpp:162
constexpr bool holds_invariant() const noexcept
Check if the extent is valid.
Definition extent2.hpp:328
constexpr friend extent2 normalize(extent2 const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent2.hpp:289
constexpr extent2(float width, float height) noexcept
Construct a 3D extent from width, height and depth.
Definition extent2.hpp:70
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:19
T infinity(T... args)
T signaling_NaN(T... args)