12#include "../rapid/numeric_array.hpp"
17namespace hi {
inline namespace v1 {
30template<
typename T,
int D>
34 using array_type = numeric_array<value_type, 4>;
36 static_assert(D == 2 || D == 3,
"Only 2D or 3D extents are supported");
40 constexpr extent& operator=(
extent const&)
noexcept =
default;
41 constexpr extent& operator=(
extent&&)
noexcept =
default;
43 [[nodiscard]]
constexpr static extent large()
noexcept
45 return {large_number_v<value_type>, large_number_v<value_type>};
59 [[nodiscard]]
constexpr explicit operator array_type() const noexcept
64 [[nodiscard]]
constexpr explicit extent(array_type
const&
other) noexcept : _v(
other) {}
66 [[nodiscard]]
constexpr explicit operator bool() const noexcept
68 if constexpr (D == 2) {
69 return _v.x() != value_type{0} or _v.y() != value_type{0};
70 }
else if constexpr (D == 3) {
71 return _v.x() != value_type{0} or _v.y() != value_type{0} or _v.z() != value_type{0};
78 [[nodiscard]]
constexpr explicit operator vector<value_type, E>() const noexcept
82 return vector<value_type, E>{
static_cast<array_type
>(*this)};
87 [[nodiscard]]
constexpr extent() noexcept : _v(value_type{0}, value_type{0}, value_type{0}, value_type{0})
115 [[nodiscard]]
static constexpr extent infinity() noexcept
121 [[nodiscard]]
static constexpr extent infinity() noexcept
130 [[nodiscard]]
static constexpr extent large() noexcept
133 return extent{value_type{16777216}, value_type{16777216}};
136 [[nodiscard]]
static constexpr extent large() noexcept
139 return extent{value_type{16777216}, value_type{16777216}, value_type{16777216}};
142 [[nodiscard]]
static constexpr extent nan() noexcept
143 requires
std::is_same_v<value_type,
float> and (D == 2)
151 [[nodiscard]]
static constexpr extent nan() noexcept
152 requires
std::is_same_v<value_type,
float> and (D == 3)
167 [[nodiscard]]
constexpr value_type&
width() noexcept
178 [[nodiscard]]
constexpr value_type&
height() noexcept
189 [[nodiscard]]
constexpr value_type&
depth() noexcept
201 [[nodiscard]]
constexpr value_type
const&
width() const noexcept
212 [[nodiscard]]
constexpr value_type
const&
height() const noexcept
223 [[nodiscard]]
constexpr value_type
const&
depth() const noexcept
229 [[nodiscard]]
constexpr vector<value_type, D> right() const noexcept
231 return vector<value_type, D>{_v.x000()};
234 [[nodiscard]]
constexpr vector<value_type, D> up() const noexcept
236 return vector<value_type, D>{_v._0y00()};
239 constexpr extent& operator+=(
extent const& rhs)
noexcept
241 return *
this = *
this + rhs;
251 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
252 return extent{lhs._v + rhs._v};
262 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
263 return extent{lhs._v - rhs._v};
276 return extent{lhs._v * rhs};
280 [[nodiscard]]
constexpr friend auto operator+(
extent const& lhs, vector<value_type, E>
const& rhs)
noexcept
285 return extent<value_type,
std::max(D, E)>{
static_cast<array_type
>(lhs) +
static_cast<array_type
>(rhs)};
289 [[nodiscard]]
constexpr friend auto operator+(vector<value_type, E>
const& lhs,
extent const& rhs)
noexcept
294 return vector<value_type,
std::max(D, E)>{
static_cast<array_type
>(lhs) +
static_cast<array_type
>(rhs)};
308 r._v[i] = lhs._v[i] + rhs;
322 return extent{lhs * rhs._v};
332 hi_axiom(lhs.holds_invariant() && rhs.holds_invariant());
333 return lhs._v == rhs._v;
336 [[nodiscard]]
constexpr friend std::partial_ordering operator<=>(
extent const& lhs,
extent const& rhs)
noexcept
341 hilet equal = eq(lhs._v, rhs._v) & mask;
344 return std::partial_ordering::equivalent;
347 hilet less = lt(lhs._v, rhs._v) & mask;
348 if ((less | equal) == mask) {
350 return std::partial_ordering::less;
353 hilet greater = lt(lhs._v, rhs._v) & mask;
354 if ((greater | equal) == mask) {
356 return std::partial_ordering::greater;
360 return std::partial_ordering::unordered;
363 [[nodiscard]]
constexpr friend std::partial_ordering operator<=>(
extent const& lhs,
extent const& rhs)
noexcept
371 return std::partial_ordering::equivalent;
374 hilet less = lt(lhs._v, rhs._v) & mask;
375 if ((less | equal) == mask) {
377 return std::partial_ordering::less;
380 hilet greater = gt(lhs._v, rhs._v) & mask;
381 if ((greater | equal) == mask) {
383 return std::partial_ordering::greater;
387 return std::partial_ordering::unordered;
397 return squared_hypot<element_mask>(rhs._v);
404 [[nodiscard]]
constexpr friend value_type
hypot(
extent const& rhs)
noexcept
407 return hypot<element_mask>(rhs._v);
417 return rcp_hypot<element_mask>(rhs._v);
427 return extent{normalize<element_mask>(rhs._v)};
430 [[nodiscard]]
constexpr friend extent ceil(
extent const& rhs)
noexcept
431 requires std::is_same_v<value_type, float>
434 return extent{ceil(array_type{rhs})};
437 [[nodiscard]]
constexpr friend extent floor(
extent const& rhs)
noexcept
438 requires std::is_same_v<value_type, float>
441 return extent{floor(
static_cast<array_type
>(rhs))};
444 [[nodiscard]]
constexpr friend extent round(
extent const& rhs)
noexcept
445 requires std::is_same_v<value_type, float>
448 return extent{round(
static_cast<array_type
>(rhs))};
451 [[nodiscard]]
constexpr friend extent min(
extent const& lhs,
extent const& rhs)
noexcept
453 return extent{min(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
456 [[nodiscard]]
constexpr friend extent max(
extent const& lhs,
extent const& rhs)
noexcept
458 return extent{max(
static_cast<array_type
>(lhs),
static_cast<array_type
>(rhs))};
463 return extent{clamp(
static_cast<array_type
>(value),
static_cast<array_type
>(min),
static_cast<array_type
>(max))};
472 return _v.x() >= value_type{0} && _v.y() >= value_type{0} && _v.z() >= value_type{0} && _v.w() == value_type{0} &&
473 (D == 3 || _v.z() == value_type{0});
478 if constexpr (D == 2) {
479 return std::format(
"[{}, {}]", rhs._v.x(), rhs._v.y());
480 }
else if constexpr (D == 3) {
481 return std::format(
"[{}, {}, {}]", rhs._v.x(), rhs._v.y(), rhs._v.z());
489 return lhs << to_string(rhs);
495 static constexpr std::size_t element_mask = (1_uz << D) - 1;
521[[nodiscard]]
constexpr extent2i narrow_cast(
extent2 const& rhs)
noexcept
523 return {narrow_cast<int>(rhs.width()), narrow_cast<int>(rhs.height())};
527[[nodiscard]]
constexpr extent2 narrow_cast(
extent2i const& rhs)
noexcept
529 return {narrow_cast<float>(rhs.width()), narrow_cast<float>(rhs.height())};
534template<
typename CharT>
535struct std::formatter<
hi::geo::extent<float, 2>, CharT> {
541 auto format(hi::geo::extent<float, 2>
const& t,
auto& fc)
543 return std::vformat_to(fc.out(),
"[{}, {}]", std::make_format_args(t.width(), t.height()));
547template<
typename CharT>
548struct std::formatter<
hi::geo::extent<float, 3>, CharT> {
554 auto format(hi::geo::extent<float, 3>
const& t,
auto& fc)
556 return std::vformat_to(fc.out(),
"[{}, {}, {}]", std::make_format_args(t.width(), t.height(), t.depth()));
560template<
typename CharT>
561struct std::formatter<
hi::geo::extent<int, 2>, CharT> {
567 auto format(hi::geo::extent<int, 2>
const& t,
auto& fc)
569 return std::vformat_to(fc.out(),
"[{}, {}]", std::make_format_args(t.width(), t.height()));
573template<
typename CharT>
574struct std::formatter<
hi::geo::extent<int, 3>, CharT> {
580 auto format(hi::geo::extent<int, 3>
const& t,
auto& fc)
582 return std::vformat_to(fc.out(),
"[{}, {}, {}]", std::make_format_args(t.width(), t.height(), t.depth()));
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:181
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:148
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
This file contains constants and conversion functions.
geo::extent< int, 3 > extent3i
A 3D extent.
Definition extent.hpp:518
geo::extent< float, 3 > extent3
A 3D extent.
Definition extent.hpp:508
geo::extent< int, 2 > extent2i
A 2D extent.
Definition extent.hpp:513
geo::extent< float, 2 > extent2
A 2D extent.
Definition extent.hpp:503
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
A high-level geometric extent.
Definition extent.hpp:31
hi_force_inline constexpr friend value_type squared_hypot(extent const &rhs) noexcept
Get the squared length of the extent.
Definition extent.hpp:394
constexpr value_type const & depth() const noexcept
Access the z-as-depth element from the extent.
Definition extent.hpp:223
constexpr extent() noexcept
Construct a empty extent / zero length.
Definition extent.hpp:87
constexpr extent(extent< value_type, E > const &other) noexcept
Construct a extent from a lower dimension extent.
Definition extent.hpp:52
constexpr value_type & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:167
constexpr value_type const & height() const noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:212
constexpr friend extent operator*(value_type const &lhs, extent const &rhs) noexcept
Scale the extent by a scaler.
Definition extent.hpp:319
constexpr bool holds_invariant() const noexcept
Check if the extent is valid.
Definition extent.hpp:470
constexpr friend value_type rcp_hypot(extent const &rhs) noexcept
Get the length of the extent.
Definition extent.hpp:414
constexpr friend extent operator-(extent const &lhs, extent const &rhs) noexcept
Subtract two extents from each other.
Definition extent.hpp:260
constexpr friend extent normalize(extent const &rhs) noexcept
Normalize a extent to a unit extent.
Definition extent.hpp:424
constexpr friend extent operator*(extent const &lhs, value_type const &rhs) noexcept
Scale the extent by a scaler.
Definition extent.hpp:273
constexpr value_type const & width() const noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:201
constexpr friend value_type hypot(extent const &rhs) noexcept
Get the length of the extent.
Definition extent.hpp:404
constexpr value_type & depth() noexcept
Access the z-as-depth element from the extent.
Definition extent.hpp:189
constexpr extent(value_type width, value_type height) noexcept
Construct a 2D extent from the width and height.
Definition extent.hpp:96
constexpr friend bool operator==(extent const &lhs, extent const &rhs) noexcept
Compare if two extents are equal.
Definition extent.hpp:330
constexpr extent(value_type width, value_type height, value_type depth=value_type{0}) noexcept
Construct a 3D extent from width, height and depth.
Definition extent.hpp:108
constexpr friend extent operator+(extent const &lhs, value_type const &rhs) noexcept
Add a scaler to the extent.
Definition extent.hpp:302
constexpr friend extent operator+(extent const &lhs, extent const &rhs) noexcept
Add two extents from each other.
Definition extent.hpp:249
constexpr value_type & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:178
T signaling_NaN(T... args)