HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
extent.hpp
1// Copyright Take Vos 2021.
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 "vector.hpp"
8#include "../rapid/numeric_array.hpp"
9
10namespace tt {
11namespace geo {
12
19template<int D>
20class extent {
21public:
22 static_assert(D == 2 || D == 3, "Only 2D or 3D extents are supported");
23
24 constexpr extent(extent const &) noexcept = default;
25 constexpr extent(extent &&) noexcept = default;
26 constexpr extent &operator=(extent const &) noexcept = default;
27 constexpr extent &operator=(extent &&) noexcept = default;
28
31 template<int E>
32 requires(E < D) [[nodiscard]] constexpr extent(extent<E> const &other) noexcept : _v(static_cast<f32x4>(other))
33 {
34 tt_axiom(is_valid());
35 }
36
39 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
40 {
41 return _v;
42 }
43
46 [[nodiscard]] constexpr explicit extent(f32x4 const &other) noexcept : _v(other)
47 {
48 tt_axiom(is_valid());
49 }
50
51 template<int E>
52 [[nodiscard]] constexpr explicit operator vector<E>() const noexcept requires(E >= D)
53 {
54 tt_axiom(is_valid());
55 return vector<E>{static_cast<f32x4>(*this)};
56 }
57
60 [[nodiscard]] constexpr extent() noexcept : _v(0.0f, 0.0f, 0.0f, 0.0f)
61 {
62 tt_axiom(is_valid());
63 }
64
69 [[nodiscard]] constexpr extent(float width, float height) noexcept requires(D == 2) : _v(width, height, 0.0f, 0.0f)
70 {
71 tt_axiom(is_valid());
72 }
73
79 [[nodiscard]] constexpr extent(float width, float height, float depth = 0.0f) noexcept requires(D == 3) :
80 _v(width, height, depth, 0.0f)
81 {
82 tt_axiom(is_valid());
83 }
84
85 [[nodiscard]] static constexpr extent infinity() noexcept requires(D == 2)
86 {
88 }
89
90 [[nodiscard]] static constexpr extent infinity() noexcept requires(D == 3)
91 {
92 return extent{
96 }
97
98 [[nodiscard]] static constexpr extent large() noexcept requires(D == 2)
99 {
100 return extent{32767.0f, 32767.0f};
101 }
102
103 [[nodiscard]] static constexpr extent large() noexcept requires(D == 3)
104 {
105 return extent{32767.0f, 32767.0f, 32767.0f};
106 }
107
108 [[nodiscard]] static constexpr extent nan() noexcept requires(D == 2)
109 {
110 auto r = extent{};
113 return r;
114 }
115
116 [[nodiscard]] static constexpr extent nan() noexcept requires(D == 3)
117 {
118 auto r = extent{};
122 return r;
123 }
124
131 [[nodiscard]] constexpr float &width() noexcept
132 {
133 return _v.x();
134 }
135
142 [[nodiscard]] constexpr float &height() noexcept
143 {
144 return _v.y();
145 }
146
153 [[nodiscard]] constexpr float &depth() noexcept requires(D == 3)
154 {
155 return _v.z();
156 }
157
164 [[nodiscard]] constexpr float const &width() const noexcept
165 {
166 return _v.x();
167 }
168
175 [[nodiscard]] constexpr float const &height() const noexcept
176 {
177 return _v.y();
178 }
179
186 [[nodiscard]] constexpr float const &depth() const noexcept requires(D == 3)
187 {
188 return _v.z();
189 }
190
191 [[nodiscard]] constexpr vector<D> right() const noexcept
192 {
193 return vector<D>{_v.x000()};
194 }
195
196 [[nodiscard]] constexpr vector<D> up() const noexcept
197 {
198 return vector<D>{_v._0y00()};
199 }
200
201 constexpr extent &operator+=(extent const &rhs) noexcept
202 {
203 return *this = *this + rhs;
204 }
205
211 [[nodiscard]] constexpr friend extent operator+(extent const &lhs, extent const &rhs) noexcept
212 {
213 tt_axiom(lhs.is_valid() && rhs.is_valid());
214 return extent{lhs._v + rhs._v};
215 }
216
222 [[nodiscard]] constexpr friend extent operator-(extent const &lhs, extent const &rhs) noexcept
223 {
224 tt_axiom(lhs.is_valid() && rhs.is_valid());
225 return extent{lhs._v - rhs._v};
226 }
227
233 [[nodiscard]] constexpr friend extent operator*(extent const &lhs, float const &rhs) noexcept
234 {
235 tt_axiom(lhs.is_valid());
236 return extent{lhs._v * rhs};
237 }
238
239 template<int E>
240 [[nodiscard]] constexpr friend auto operator+(extent const &lhs, vector<E> const &rhs) noexcept
241 {
242 tt_axiom(lhs.is_valid());
243 tt_axiom(rhs.is_valid());
244
245 return extent<std::max(D, E)>{static_cast<f32x4>(lhs) + static_cast<f32x4>(rhs)};
246 }
247
248 template<int E>
249 [[nodiscard]] constexpr friend auto operator+(vector<E> const &lhs, extent const &rhs) noexcept
250 {
251 tt_axiom(lhs.is_valid());
252 tt_axiom(rhs.is_valid());
253
254 return vector<std::max(D, E)>{static_cast<f32x4>(lhs) + static_cast<f32x4>(rhs)};
255 }
256
262 [[nodiscard]] constexpr friend extent operator+(extent const &lhs, float const &rhs) noexcept
263 {
264 tt_axiom(lhs.is_valid());
265
266 auto r = extent{};
267 for (size_t i = 0; i != D; ++i) {
268 r._v[i] = lhs._v[i] + rhs;
269 }
270
271 return r;
272 }
273
279 [[nodiscard]] constexpr friend extent operator*(float const &lhs, extent const &rhs) noexcept
280 {
281 tt_axiom(rhs.is_valid());
282 return extent{lhs * rhs._v};
283 }
284
290 [[nodiscard]] constexpr friend bool operator==(extent const &lhs, extent const &rhs) noexcept
291 {
292 tt_axiom(lhs.is_valid() && rhs.is_valid());
293 return lhs._v == rhs._v;
294 }
295
298 [[nodiscard]] constexpr friend bool operator<(extent const &lhs, extent const &rhs) noexcept requires(D == 2)
299 {
300 return lhs.width() < rhs.width() && lhs.height() < rhs.height();
301 }
302
305 [[nodiscard]] constexpr friend bool operator<(extent const &lhs, extent const &rhs) noexcept requires(D == 3)
306 {
307 return lhs.width() < rhs.width() && lhs.height() < rhs.height() && lhs.depth() < rhs.depth();
308 }
309
312 [[nodiscard]] constexpr friend bool operator<=(extent const &lhs, extent const &rhs) noexcept requires(D == 2)
313 {
314 return lhs.width() <= rhs.width() && lhs.height() <= rhs.height();
315 }
316
319 [[nodiscard]] constexpr friend bool operator<=(extent const &lhs, extent const &rhs) noexcept requires(D == 3)
320 {
321 return lhs.width() <= rhs.width() && lhs.height() <= rhs.height() && lhs.depth() <= rhs.depth();
322 }
323
326 [[nodiscard]] constexpr friend bool operator>(extent const &lhs, extent const &rhs) noexcept requires(D == 2)
327 {
328 return lhs.width() > rhs.width() && lhs.height() > rhs.height();
329 }
330
333 [[nodiscard]] constexpr friend bool operator>(extent const &lhs, extent const &rhs) noexcept requires(D == 3)
334 {
335 return lhs.width() > rhs.width() && lhs.height() > rhs.height() && lhs.depth() > rhs.depth();
336 }
337
340 [[nodiscard]] constexpr friend bool operator>=(extent const &lhs, extent const &rhs) noexcept requires(D == 2)
341 {
342 return lhs.width() >= rhs.width() && lhs.height() >= rhs.height();
343 }
344
347 [[nodiscard]] constexpr friend bool operator>=(extent const &lhs, extent const &rhs) noexcept requires(D == 3)
348 {
349 return lhs.width() >= rhs.width() && lhs.height() >= rhs.height() && lhs.depth() >= rhs.depth();
350 }
351
356 [[nodiscard]] constexpr friend float squared_hypot(extent const &rhs) noexcept
357 {
358 tt_axiom(rhs.is_valid());
359 return squared_hypot<element_mask>(rhs._v);
360 }
361
366 [[nodiscard]] constexpr friend float hypot(extent const &rhs) noexcept
367 {
368 tt_axiom(rhs.is_valid());
369 return hypot<element_mask>(rhs._v);
370 }
371
376 [[nodiscard]] constexpr friend float rcp_hypot(extent const &rhs) noexcept
377 {
378 tt_axiom(rhs.is_valid());
379 return rcp_hypot<element_mask>(rhs._v);
380 }
381
386 [[nodiscard]] constexpr friend extent normalize(extent const &rhs) noexcept
387 {
388 tt_axiom(rhs.is_valid());
389 return extent{normalize<element_mask>(rhs._v)};
390 }
391
392 [[nodiscard]] constexpr friend extent ceil(extent const &rhs) noexcept
393 {
394 tt_axiom(rhs.is_valid());
395 return extent{ceil(static_cast<f32x4>(rhs))};
396 }
397
398 [[nodiscard]] constexpr friend extent floor(extent const &rhs) noexcept
399 {
400 tt_axiom(rhs.is_valid());
401 return extent{floor(static_cast<f32x4>(rhs))};
402 }
403
404 [[nodiscard]] constexpr friend extent min(extent const &lhs, extent const &rhs) noexcept
405 {
406 return extent{min(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
407 }
408
409 [[nodiscard]] constexpr friend extent max(extent const &lhs, extent const &rhs) noexcept
410 {
411 return extent{max(static_cast<f32x4>(lhs), static_cast<f32x4>(rhs))};
412 }
413
414 [[nodiscard]] constexpr friend extent clamp(extent const &value, extent const &min, extent const &max) noexcept
415 {
416 return extent{clamp(static_cast<f32x4>(value), static_cast<f32x4>(min), static_cast<f32x4>(max))};
417 }
418
423 [[nodiscard]] constexpr bool is_valid() const noexcept
424 {
425 return _v.x() >= 0.0f && _v.y() >= 0.0f && _v.z() >= 0.0f && _v.w() == 0.0f && (D == 3 || _v.z() == 0.0f);
426 }
427
428 [[nodiscard]] friend std::string to_string(extent const &rhs) noexcept
429 {
430 if constexpr (D == 2) {
431 return std::format("[{}, {}]", rhs._v.x(), rhs._v.y());
432 } else if constexpr (D == 3) {
433 return std::format("[{}, {}, {}]", rhs._v.x(), rhs._v.y(), rhs._v.z());
434 } else {
435 tt_static_no_default();
436 }
437 }
438
439 friend std::ostream &operator<<(std::ostream &lhs, extent const &rhs) noexcept
440 {
441 return lhs << to_string(rhs);
442 }
443
444private:
445 f32x4 _v;
446
447 static constexpr size_t element_mask = (1_uz << D) - 1;
448};
449
450} // namespace geo
451
452using extent2 = geo::extent<2>;
453using extent3 = geo::extent<3>;
454
455} // namespace tt
456
457namespace std {
458
459template<typename CharT>
460struct formatter<tt::geo::extent<2>, CharT> {
461 auto parse(auto &pc)
462 {
463 return pc.end();
464 }
465
466 auto format(tt::geo::extent<2> const &t, auto &fc)
467 {
468 return std::vformat_to(fc.out(), "[{}, {}]", std::make_format_args(t.width(), t.height()));
469 }
470};
471
472template<typename CharT>
473struct formatter<tt::geo::extent<3>, CharT> : formatter<float, CharT> {
474 auto parse(auto &pc)
475 {
476 return pc.end();
477 }
478
479 auto format(tt::geo::extent<3> const &t, auto &fc)
480 {
481 return std::vformat_to(fc.out(), "[{}, {}, {}]", std::make_format_args(t.width(), t.height(), t.depth()));
482 }
483};
484
485} // namespace std
STL namespace.
A high-level geometric extent.
Definition extent.hpp:20
constexpr float const & height() const noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:175
constexpr float const & width() const noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:164
constexpr friend bool operator>(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:326
constexpr friend bool operator<(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:298
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:131
constexpr friend bool operator<(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:305
constexpr friend extent operator*(float const &lhs, extent const &rhs) noexcept
Scale the extent by a scaler.
Definition extent.hpp:279
constexpr bool is_valid() const noexcept
Check if the extent is valid.
Definition extent.hpp:423
constexpr friend float squared_hypot(extent const &rhs) noexcept
Get the squared length of the extent.
Definition extent.hpp:356
constexpr friend extent operator-(extent const &lhs, extent const &rhs) noexcept
Subtract two extents from each other.
Definition extent.hpp:222
constexpr friend extent operator*(extent const &lhs, float const &rhs) noexcept
Scale the extent by a scaler.
Definition extent.hpp:233
constexpr friend bool operator<=(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:312
constexpr friend float rcp_hypot(extent const &rhs) noexcept
Get the length of the extent.
Definition extent.hpp:376
constexpr friend bool operator<=(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:319
constexpr friend extent normalize(extent const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent.hpp:386
constexpr extent(float width, float height, float depth=0.0f) noexcept
Construct a 3D extent from x, y and z elements.
Definition extent.hpp:79
constexpr extent(extent< E > const &other) noexcept
Construct a extent from a lower dimension extent.
Definition extent.hpp:32
constexpr extent(float width, float height) noexcept
Construct a 2D extent from x and y elements.
Definition extent.hpp:69
constexpr float & depth() noexcept
Access the z-as-depth element from the extent.
Definition extent.hpp:153
constexpr friend float hypot(extent const &rhs) noexcept
Get the length of the extent.
Definition extent.hpp:366
constexpr friend extent operator+(extent const &lhs, float const &rhs) noexcept
Add a scaler to the extent.
Definition extent.hpp:262
constexpr float const & depth() const noexcept
Access the z-as-depth element from the extent.
Definition extent.hpp:186
constexpr friend bool operator>(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:333
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:142
constexpr extent(f32x4 const &other) noexcept
Construct a extent from a f32x4-numeric_array.
Definition extent.hpp:46
constexpr friend bool operator==(extent const &lhs, extent const &rhs) noexcept
Compare if two extents are equal.
Definition extent.hpp:290
constexpr extent() noexcept
Construct a empty extent / zero length.
Definition extent.hpp:60
constexpr friend bool operator>=(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:340
constexpr friend extent operator+(extent const &lhs, extent const &rhs) noexcept
Add two extents from each other.
Definition extent.hpp:211
constexpr friend bool operator>=(extent const &lhs, extent const &rhs) noexcept
Compare the size of the extents.
Definition extent.hpp:347
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
T infinity(T... args)
T max(T... args)
T signaling_NaN(T... args)