15#include "../SIMD/module.hpp"
16#include "../utility/utility.hpp"
17#include "../concurrency/concurrency.hpp"
18#include "../macros.hpp"
24namespace hi {
inline namespace v1 {
31 using array_type = simd<float, 4>;
32 using value_type = array_type::value_type;
42 [[nodiscard]]
constexpr static aarectangle
large() noexcept
44 return {point2{-large_number_v<float>, -large_number_v<float>}, point2{large_number_v<float>, large_number_v<float>}};
59 constexpr aarectangle(
float x,
float y,
float width,
float height) noexcept :
60 v{x, y, x + width, y + height}
69 constexpr explicit aarectangle(
extent2 const& extent) noexcept : v(
static_cast<array_type
>(extent)._00xy())
78 constexpr aarectangle(point2
const& p0, point2
const& p3) noexcept :
79 v(
static_cast<array_type
>(p0).xy00() +
static_cast<array_type
>(p3)._00xy())
81 hi_axiom(p0.holds_invariant());
82 hi_axiom(p3.holds_invariant());
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
131 constexpr aarectangle&
operator|=(aarectangle
const& rhs)
noexcept
133 return *
this = *
this | rhs;
141 constexpr aarectangle&
operator|=(point2
const& rhs)
noexcept
143 return *
this = *
this | rhs;
146 [[nodiscard]]
constexpr point2 operator[](
std::size_t i)
const noexcept
150 return point2{v.xy01()};
152 return point2{v.zy01()};
154 return point2{v.xw01()};
156 return point2{v.zw01()};
163 [[nodiscard]]
constexpr friend point2 get(aarectangle
const &rhs)
noexcept
165 if constexpr (I == 0) {
166 return point2{rhs.v.xy01()};
167 }
else if constexpr (I == 1) {
168 return point2{rhs.v.zy01()};
169 }
else if constexpr (I == 2) {
170 return point2{rhs.v.xw01()};
171 }
else if constexpr (I == 3) {
172 return point2{rhs.v.zw01()};
174 hi_static_no_default();
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};
243 [[nodiscard]]
constexpr friend point2
midpoint(aarectangle
const& rhs)
noexcept
245 return midpoint(get<0>(rhs), get<3>(rhs));
248 constexpr aarectangle& set_width(value_type newWidth)
noexcept
250 v = v.xyxw() + array_type{value_type{0}, value_type{0}, newWidth, value_type{0}};
254 constexpr aarectangle& 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(point2
const& rhs)
const noexcept
267 return (
static_cast<array_type
>(rhs).xyxy() >= v).mask() == 0b0011;
275 [[nodiscard]]
constexpr bool contains(point3
const& rhs)
const noexcept
289 auto x = value_type{0};
294 x = haystack.right() - needle.width();
297 x = haystack.center() - needle.width() / value_type{2};
303 auto y = value_type{0};
305 y = haystack.bottom();
308 y = haystack.top() - needle.height();
311 y = haystack.middle() - needle.height() / value_type{2};
317 return {point2{x, y}, needle};
340 return equal(lhs.v, rhs.v);
345 if (lhs.empty() or rhs.
empty()) {
349 hilet rhs_swap = rhs.v.zwxy();
352 if (((lhs.v > rhs_swap).mask() & 0b0011) != 0) {
357 if (((lhs.v < rhs_swap).mask() & 0b1100) != 0) {
364 [[nodiscard]]
friend constexpr aarectangle operator|(aarectangle
const& lhs, aarectangle
const& rhs)
noexcept
371 return aarectangle{
min(get<0>(lhs), get<0>(rhs)),
max(get<3>(lhs), get<3>(rhs))};
375 [[nodiscard]]
friend constexpr aarectangle operator|(aarectangle
const& lhs, point2
const& rhs)
noexcept
378 return aarectangle{rhs, rhs};
380 return aarectangle{
min(get<0>(lhs), rhs),
max(get<3>(lhs), rhs)};
389 [[nodiscard]]
friend constexpr aarectangle
operator*(aarectangle
const& lhs, value_type rhs)
noexcept
391 hilet new_extent = lhs.size() * rhs;
393 hilet offset = diff * 0.5f;
395 hilet p0 = get<0>(lhs) - offset;
396 hilet p3 = max(get<3>(lhs) + offset, p0);
397 return aarectangle{p0, p3};
406 [[nodiscard]]
friend constexpr aarectangle
operator+(aarectangle
const& lhs, value_type rhs)
noexcept
408 return aarectangle{lhs.v + neg<0b0011>(array_type::broadcast(rhs))};
417 [[nodiscard]]
friend constexpr aarectangle
operator-(aarectangle
const& lhs, value_type rhs)
noexcept
424 hilet p0 = round(get<0>(rhs));
431 [[nodiscard]]
friend constexpr aarectangle
ceil(aarectangle
const& rhs)
noexcept
433 hilet p0 =
floor(get<0>(rhs));
434 hilet p3 =
ceil(get<3>(rhs));
435 return aarectangle{p0, p3};
440 [[nodiscard]]
friend constexpr aarectangle
ceil(aarectangle
const& lhs,
extent2 const& rhs)
noexcept
442 hilet p0 =
floor(get<0>(lhs), rhs);
443 hilet p3 =
ceil(get<3>(lhs), rhs);
444 return aarectangle{p0, p3};
449 [[nodiscard]]
friend constexpr aarectangle
floor(aarectangle
const& rhs)
noexcept
451 hilet p0 =
ceil(get<0>(rhs));
452 hilet p3 =
floor(get<3>(rhs));
453 return aarectangle{p0, p3};
464 [[nodiscard]]
friend constexpr aarectangle
intersect(aarectangle
const& lhs, aarectangle
const& rhs)
noexcept
466 hilet p0 = max(get<0>(lhs), get<0>(rhs));
467 hilet p3 = min(get<3>(lhs), get<3>(rhs));
468 if (p0.x() < p3.x() && p0.y() < p3.y()) {
475 [[nodiscard]]
friend value_type distance(
aarectangle const& lhs, point2
const& rhs)
noexcept
477 hilet lhs_ =
static_cast<array_type
>(lhs);
478 hilet rhs_ =
static_cast<array_type
>(rhs);
480 hilet closest_point = max(min(rhs_, lhs_.zwzw()), lhs_);
481 hilet v_closest_point = closest_point - rhs_;
482 return hypot<0b0011>(v_closest_point);
500 constexpr static bool is_always_lock_free =
false;
502 constexpr atomic()
noexcept =
default;
503 atomic(atomic
const&) =
delete;
504 atomic(atomic&&) =
delete;
505 atomic&
operator=(atomic
const&) =
delete;
508 constexpr atomic(value_type
const& rhs) noexcept : _value(rhs) {}
509 atomic&
operator=(value_type
const& rhs)
noexcept
515 operator value_type()
const noexcept
522 return is_always_lock_free;
525 void store(value_type desired, std::memory_order = std::memory_order_seq_cst)
noexcept
527 hilet
lock = std::scoped_lock(_mutex);
531 value_type
load(std::memory_order = std::memory_order_seq_cst)
const noexcept
533 hilet
lock = std::scoped_lock(_mutex);
537 value_type
exchange(value_type desired, std::memory_order = std::memory_order_seq_cst)
noexcept
539 hilet
lock = std::scoped_lock(_mutex);
540 return std::exchange(_value, desired);
543 bool compare_exchange_weak(value_type& expected, value_type desired, std::memory_order, std::memory_order)
noexcept
545 hilet
lock = std::scoped_lock(_mutex);
546 if (_value == expected) {
556 value_type& expected,
558 std::memory_order success,
559 std::memory_order failure)
noexcept
565 compare_exchange_weak(value_type& expected, value_type desired, std::memory_order order = std::memory_order_seq_cst)
noexcept
571 value_type& expected,
573 std::memory_order order = std::memory_order_seq_cst)
noexcept
578 value_type
fetch_or(value_type arg, std::memory_order = std::memory_order_seq_cst)
noexcept
580 hilet
lock = std::scoped_lock(_mutex);
586 value_type
operator|=(value_type arg)
noexcept
588 hilet
lock = std::scoped_lock(_mutex);
589 return _value |= arg;
594 mutable hi::unfair_mutex _mutex;
597template<
typename CharT>
598struct std::formatter<
hi::aarectangle, CharT> {
606 return std::vformat_to(fc.out(),
"{}:{}", std::make_format_args(get<0>(t), t.
size()));
types and utilities for alignment.
Defined the geo::extent, extent2 and extent3 types.
@ middle
Align to the vertical-middle.
Definition alignment.hpp:33
@ bottom
Align to the bottom.
Definition alignment.hpp:37
@ top
Align to the top.
Definition alignment.hpp:29
@ right
Align the text to the right side.
Definition alignment.hpp:137
@ left
Align the text to the left side.
Definition alignment.hpp:119
@ center
Align the text in the center.
Definition alignment.hpp:125
@ other
The gui_event does not have associated data.
Definition gui_event_variant.hpp:22
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
The HikoGUI API version 1.
Definition lookahead_iterator.hpp:6
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context.hpp:34
@ outside
The border is drawn outside the edge of a quad.
Definition draw_context.hpp:38
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:29
friend constexpr aarectangle ceil(aarectangle const &lhs, extent2 const &rhs) noexcept
Round rectangle by expanding to a certain granularity.
Definition aarectangle.hpp:440
constexpr extent2 size() const noexcept
Get size of the rectangle.
Definition aarectangle.hpp:182
friend constexpr aarectangle intersect(aarectangle const &lhs, aarectangle const &rhs) noexcept
Return the overlapping part of two rectangles.
Definition aarectangle.hpp:464
static constexpr aarectangle large() noexcept
Create a large axis aligned rectangle.
Definition aarectangle.hpp:42
friend constexpr aarectangle operator*(aarectangle const &lhs, value_type rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarectangle.hpp:389
constexpr friend point2 midpoint(aarectangle const &rhs) noexcept
Get the center of the rectangle.
Definition aarectangle.hpp:243
constexpr aarectangle(extent2 const &extent) noexcept
Create a rectangle from the size.
Definition aarectangle.hpp:69
friend constexpr aarectangle operator-(aarectangle const &lhs, value_type rhs) noexcept
Shrink the rectangle for the same amount in all directions.
Definition aarectangle.hpp:417
constexpr bool empty() const noexcept
Check if the rectangle has no area.
Definition aarectangle.hpp:114
constexpr bool contains(point3 const &rhs) const noexcept
Check if a 3D coordinate is inside the rectangle.
Definition aarectangle.hpp:275
constexpr bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarectangle.hpp:264
static constexpr aarectangle _align(aarectangle outside, aarectangle inside, alignment alignment) noexcept
Need to call the hidden friend function from within another class.
Definition aarectangle.hpp:333
friend constexpr aarectangle floor(aarectangle const &rhs) noexcept
Round rectangle by shrinking to pixel edge.
Definition aarectangle.hpp:449
constexpr bool holds_invariant() const noexcept
Make sure p0 is left/bottom from p3.
Definition aarectangle.hpp:107
friend constexpr aarectangle align(aarectangle haystack, extent2 needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition aarectangle.hpp:287
friend constexpr aarectangle align(aarectangle haystack, aarectangle needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition aarectangle.hpp:326
friend constexpr aarectangle operator+(aarectangle const &lhs, value_type rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarectangle.hpp:406
constexpr value_type center() const noexcept
The center on the x-axis between left and right.
Definition aarectangle.hpp:236
constexpr aarectangle & operator|=(point2 const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition aarectangle.hpp:141
constexpr aarectangle(float x, float y, float width, float height) noexcept
Create a box from the position and size.
Definition aarectangle.hpp:59
friend constexpr aarectangle ceil(aarectangle const &rhs) noexcept
Round rectangle by expanding to pixel edge.
Definition aarectangle.hpp:431
constexpr aarectangle(point2 const &p0, point2 const &p3) noexcept
Create a rectangle from the left-bottom and right-top points.
Definition aarectangle.hpp:78
constexpr aarectangle & operator|=(aarectangle const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition aarectangle.hpp:131
constexpr value_type middle() const noexcept
The middle on the y-axis between bottom and top.
Definition aarectangle.hpp:229
constexpr aarectangle(point2 const &p0, extent2 const &extent) noexcept
Create a rectangle from the size.
Definition aarectangle.hpp:93
Horizontal/Vertical alignment combination.
Definition alignment.hpp:242
A high-level geometric extent.
Definition extent2.hpp:29
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:19
T compare_exchange_weak(T... args)
T is_lock_free(T... args)
hi::aarectangle operator hi::aarectangle(hi::aarectangle... args)