14#include "../SIMD/module.hpp"
15#include "../utility/module.hpp"
16#include "../concurrency/module.hpp"
20namespace hi {
inline namespace v1 {
29 using array_type = simd<value_type, 4>;
42 point<value_type, 2>{-large_number_v<value_type>, -large_number_v<value_type>},
43 point<value_type, 2>{large_number_v<value_type>, large_number_v<value_type>}};
59 v{x, y, x + width, y + height}
69 v(
static_cast<array_type
>(
extent)._00xy())
79 v(
static_cast<array_type
>(p0).xy00() +
static_cast<array_type
>(p3)._00xy())
94 v(
static_cast<array_type
>(p0).xyxy() +
static_cast<array_type
>(
extent)._00xy())
99 constexpr explicit operator array_type() const noexcept
109 return (v <= v.zwzw()).mask() == 0b1111;
114 [[nodiscard]]
constexpr bool empty() const noexcept
116 return (v == v.zwxy()).mask() == 0b1111;
121 [[nodiscard]]
constexpr explicit operator bool() const noexcept
133 return *
this = *
this | rhs;
143 return *
this = *
this | rhs;
146 [[nodiscard]]
constexpr point<value_type, 2> operator[](
std::size_t i)
const noexcept
150 return point<value_type, 2>{v.xy01()};
152 return point<value_type, 2>{v.zy01()};
154 return point<value_type, 2>{v.xw01()};
156 return point<value_type, 2>{v.zw01()};
163 [[nodiscard]]
constexpr friend point<value_type, 2> get(axis_aligned_rectangle
const& rhs)
noexcept
165 if constexpr (I == 0) {
166 return point<value_type, 2>{rhs.v.xy01()};
167 }
else if constexpr (I == 1) {
168 return point<value_type, 2>{rhs.v.zy01()};
169 }
else if constexpr (I == 2) {
170 return point<value_type, 2>{rhs.v.xw01()};
171 }
else if constexpr (I == 3) {
172 return point<value_type, 2>{rhs.v.zw01()};
187 [[nodiscard]]
constexpr value_type x() const noexcept
192 [[nodiscard]]
constexpr value_type y() const noexcept
197 [[nodiscard]]
constexpr value_type width() const noexcept
199 return (v.zwzw() - v).x();
202 [[nodiscard]]
constexpr value_type height() const noexcept
204 return (v.zwzw() - v).y();
207 [[nodiscard]]
constexpr value_type bottom() const noexcept
212 [[nodiscard]]
constexpr value_type top() const noexcept
217 [[nodiscard]]
constexpr value_type left() const noexcept
222 [[nodiscard]]
constexpr value_type right() const noexcept
229 [[nodiscard]]
constexpr value_type
middle() const noexcept
231 return (bottom() + top()) / value_type{2};
236 [[nodiscard]]
constexpr value_type
center() const noexcept
238 return (left() + right()) / value_type{2};
245 return midpoint(get<0>(rhs), get<3>(rhs));
250 v = v.xyxw() + array_type{value_type{0}, value_type{0}, newWidth, value_type{0}};
254 constexpr axis_aligned_rectangle& set_height(value_type newHeight)
noexcept
256 v = v.xyzy() + array_type{value_type{0}, value_type{0}, value_type{0}, newHeight};
264 [[nodiscard]]
constexpr bool contains(point<value_type, 2>
const& rhs)
const noexcept
267 return (
static_cast<array_type
>(rhs).xyxy() >= v).mask() == 0b0011;
275 [[nodiscard]]
constexpr bool contains(point<value_type, 3>
const& rhs)
const noexcept
277 return contains(point<value_type, 2>{rhs});
289 auto x = value_type{0};
290 if (alignment == horizontal_alignment::left) {
293 }
else if (alignment == horizontal_alignment::right) {
294 x = haystack.right() - needle.width();
296 }
else if (alignment == horizontal_alignment::center) {
297 x = haystack.center() - needle.width() / value_type{2};
303 auto y = value_type{0};
304 if (alignment == vertical_alignment::bottom) {
305 y = haystack.bottom();
307 }
else if (alignment == vertical_alignment::top) {
308 y = haystack.top() - needle.height();
310 }
else if (alignment == vertical_alignment::middle) {
311 y = haystack.middle() - needle.height() / value_type{2};
317 return {point<value_type, 2>{x, y}, needle};
329 return align(haystack, needle.size(), alignment);
342 return equal(lhs.v, rhs.v);
345 [[nodiscard]]
friend constexpr bool overlaps(axis_aligned_rectangle
const& lhs, axis_aligned_rectangle
const& rhs)
noexcept
347 if (lhs.empty() or rhs.
empty()) {
351 hilet rhs_swap = rhs.v.zwxy();
354 if (((lhs.v > rhs_swap).mask() & 0b0011) != 0) {
359 if (((lhs.v < rhs_swap).mask() & 0b1100) != 0) {
366 [[nodiscard]]
friend constexpr axis_aligned_rectangle
367 operator|(axis_aligned_rectangle
const& lhs, axis_aligned_rectangle
const& rhs)
noexcept
374 return axis_aligned_rectangle{
min(get<0>(lhs), get<0>(rhs)),
max(get<3>(lhs), get<3>(rhs))};
378 [[nodiscard]]
friend constexpr axis_aligned_rectangle
379 operator|(axis_aligned_rectangle
const& lhs, point<value_type, 2>
const& rhs)
noexcept
382 return axis_aligned_rectangle{rhs, rhs};
384 return axis_aligned_rectangle{
min(get<0>(lhs), rhs),
max(get<3>(lhs), rhs)};
396 hilet diff = vector<value_type, 2>{new_extent} - vector<value_type, 2>{lhs.
size()};
397 hilet offset = diff * 0.5f;
399 hilet p0 = get<0>(lhs) - offset;
400 hilet p3 = max(get<3>(lhs) + offset, p0);
427 requires std::is_same_v<value_type, float>
429 hilet p0 = round(get<0>(rhs));
437 requires std::is_same_v<value_type, float>
457 requires std::is_same_v<value_type, float>
472 [[nodiscard]]
friend constexpr axis_aligned_rectangle
475 hilet p0 = max(get<0>(lhs), get<0>(rhs));
476 hilet p3 = min(get<3>(lhs), get<3>(rhs));
477 if (p0.x() < p3.x() && p0.y() < p3.y()) {
484 [[nodiscard]]
constexpr friend value_type
487 hilet lhs_ =
static_cast<array_type
>(lhs);
488 hilet rhs_ =
static_cast<array_type
>(rhs);
490 hilet closest_point = max(min(rhs_, lhs_.zwzw()), lhs_);
491 hilet v_closest_point = closest_point - rhs_;
492 return hypot<0b0011>(v_closest_point);
506using aarectangle = geo::axis_aligned_rectangle<float>;
507using aarectanglei = geo::axis_aligned_rectangle<int>;
525[[nodiscard]] aarectanglei
fit(aarectanglei
const& bounds, aarectanglei
const&
rectangle)
noexcept;
528[[nodiscard]]
constexpr aarectanglei narrow_cast(
aarectangle const& rhs)
noexcept
530 return {narrow_cast<int>(rhs.x()), narrow_cast<int>(rhs.y()), narrow_cast<int>(rhs.width()), narrow_cast<int>(rhs.height())};
534[[nodiscard]]
constexpr aarectangle narrow_cast(aarectanglei
const& rhs)
noexcept
537 narrow_cast<float>(rhs.x()),
538 narrow_cast<float>(rhs.y()),
539 narrow_cast<float>(rhs.width()),
540 narrow_cast<float>(rhs.height())};
548 using value_type = hi::geo::axis_aligned_rectangle<T>;
549 static constexpr bool is_always_lock_free =
false;
551 constexpr atomic()
noexcept =
default;
557 constexpr atomic(value_type
const& rhs) noexcept : _value(rhs) {}
564 operator value_type()
const noexcept
571 return is_always_lock_free;
574 void store(value_type desired, std::memory_order = std::memory_order_seq_cst)
noexcept
580 value_type
load(std::memory_order = std::memory_order_seq_cst)
const noexcept
586 value_type
exchange(value_type desired, std::memory_order = std::memory_order_seq_cst)
noexcept
589 return std::exchange(_value, desired);
592 bool compare_exchange_weak(value_type& expected, value_type desired, std::memory_order, std::memory_order)
noexcept
595 if (_value == expected) {
605 value_type& expected,
607 std::memory_order success,
608 std::memory_order failure)
noexcept
614 compare_exchange_weak(value_type& expected, value_type desired, std::memory_order order = std::memory_order_seq_cst)
noexcept
620 value_type& expected,
622 std::memory_order order = std::memory_order_seq_cst)
noexcept
627 value_type
fetch_or(value_type arg, std::memory_order = std::memory_order_seq_cst)
noexcept
635 value_type
operator|=(value_type arg)
noexcept
638 return _value |= arg;
646template<
typename CharT>
647struct std::formatter<
hi::geo::axis_aligned_rectangle<float>, CharT> {
653 auto format(hi::geo::axis_aligned_rectangle<float>
const& t,
auto& fc)
655 return std::vformat_to(fc.out(),
"{}:{}", std::make_format_args(get<0>(t), t.size()));
659template<
typename CharT>
660struct std::formatter<
hi::geo::axis_aligned_rectangle<int>, CharT> {
666 auto format(hi::geo::axis_aligned_rectangle<int>
const& t,
auto& fc)
668 return std::vformat_to(fc.out(),
"{}:{}", std::make_format_args(get<0>(t), t.size()));
Defined the geo::extent, extent2 and extent3 types.
types and utilities for alignment.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:308
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:264
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ other
The gui_event does not have associated data.
@ rectangle
The gui_event has rectangle data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
aarectangle fit(aarectangle const &bounds, aarectangle const &rectangle) noexcept
Make a rectangle fit inside bounds.
@ inside
The border is drawn inside the edge of a quad.
@ outside
The border is drawn outside the edge of a quad.
An unfair mutex This is a fast implementation of a mutex which does not fairly arbitrate between mult...
Definition unfair_mutex.hpp:36
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:26
static constexpr axis_aligned_rectangle large() noexcept
Create a large axis aligned rectangle.
Definition axis_aligned_rectangle.hpp:39
friend constexpr axis_aligned_rectangle operator-(axis_aligned_rectangle const &lhs, value_type rhs) noexcept
Shrink the rectangle for the same amount in all directions.
Definition axis_aligned_rectangle.hpp:421
static constexpr axis_aligned_rectangle _align(axis_aligned_rectangle outside, axis_aligned_rectangle inside, alignment alignment) noexcept
Need to call the hidden friend function from within another class.
Definition axis_aligned_rectangle.hpp:335
friend constexpr axis_aligned_rectangle intersect(axis_aligned_rectangle const &lhs, axis_aligned_rectangle const &rhs) noexcept
Return the overlapping part of two rectangles.
Definition axis_aligned_rectangle.hpp:473
constexpr axis_aligned_rectangle(point< value_type, 2 > const &p0, extent< value_type, 2 > const &extent) noexcept
Create a rectangle from the size.
Definition axis_aligned_rectangle.hpp:93
friend constexpr axis_aligned_rectangle align(axis_aligned_rectangle haystack, axis_aligned_rectangle needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition axis_aligned_rectangle.hpp:327
friend constexpr axis_aligned_rectangle ceil(axis_aligned_rectangle const &rhs) noexcept
Round rectangle by expanding to pixel edge.
Definition axis_aligned_rectangle.hpp:436
constexpr bool contains(point< value_type, 3 > const &rhs) const noexcept
Check if a 3D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:275
constexpr extent< value_type, 2 > size() const noexcept
Get size of the rectangle.
Definition axis_aligned_rectangle.hpp:182
constexpr bool empty() const noexcept
Check if the rectangle has no area.
Definition axis_aligned_rectangle.hpp:114
constexpr bool holds_invariant() const noexcept
Make sure p0 is left/bottom from p3.
Definition axis_aligned_rectangle.hpp:107
constexpr bool contains(point< value_type, 2 > const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition axis_aligned_rectangle.hpp:264
friend constexpr axis_aligned_rectangle floor(axis_aligned_rectangle const &rhs) noexcept
Round rectangle by shrinking to pixel edge.
Definition axis_aligned_rectangle.hpp:456
constexpr friend point< value_type, 2 > midpoint(axis_aligned_rectangle const &rhs) noexcept
Get the center of the rectangle.
Definition axis_aligned_rectangle.hpp:243
constexpr value_type center() const noexcept
The center on the x-axis between left and right.
Definition axis_aligned_rectangle.hpp:236
constexpr axis_aligned_rectangle & operator|=(axis_aligned_rectangle const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition axis_aligned_rectangle.hpp:131
constexpr axis_aligned_rectangle(value_type x, value_type y, value_type width, value_type height) noexcept
Create a box from the position and size.
Definition axis_aligned_rectangle.hpp:58
friend constexpr axis_aligned_rectangle operator*(axis_aligned_rectangle const &lhs, value_type rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition axis_aligned_rectangle.hpp:393
friend constexpr axis_aligned_rectangle operator+(axis_aligned_rectangle const &lhs, value_type rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition axis_aligned_rectangle.hpp:410
constexpr value_type middle() const noexcept
The middle on the y-axis between bottom and top.
Definition axis_aligned_rectangle.hpp:229
constexpr axis_aligned_rectangle(point< value_type, 2 > const &p0, point< value_type, 2 > const &p3) noexcept
Create a rectangle from the left-bottom and right-top points.
Definition axis_aligned_rectangle.hpp:78
friend constexpr axis_aligned_rectangle align(axis_aligned_rectangle haystack, extent< value_type, 2 > needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition axis_aligned_rectangle.hpp:287
constexpr axis_aligned_rectangle & operator|=(point< value_type, 2 > const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition axis_aligned_rectangle.hpp:141
constexpr axis_aligned_rectangle(extent< value_type, 2 > const &extent) noexcept
Create a rectangle from the size.
Definition axis_aligned_rectangle.hpp:68
friend constexpr axis_aligned_rectangle ceil(axis_aligned_rectangle const &lhs, extent< value_type, 2 > const &rhs) noexcept
Round rectangle by expanding to a certain granularity.
Definition axis_aligned_rectangle.hpp:447
A high-level geometric extent.
Definition extent.hpp:30
T compare_exchange_weak(T... args)
T is_lock_free(T... args)