15#include "../utility/utility.hpp"
16#include "../concurrency/concurrency.hpp"
19#include "../macros.hpp"
20#include <hikocpu/hikocpu.hpp>
26hi_export_module(hikogui.geometry : aarectangle);
28hi_export
namespace hi {
inline namespace v1 {
36 using value_type = array_type::value_type;
48 return {point2{-large_number_v<float>, -large_number_v<float>}, point2{large_number_v<float>, large_number_v<float>}};
63 constexpr aarectangle(
float x,
float y,
float width,
float height) noexcept :
64 v{x, y, x + width, y + height}
82 constexpr aarectangle(point2
const& p0, point2
const& p3) noexcept :
85 hi_axiom(p0.holds_invariant());
86 hi_axiom(p3.holds_invariant());
103 constexpr explicit operator array_type() const noexcept
113 return (v <= v.zwzw()).mask() == 0b1111;
118 [[nodiscard]]
constexpr bool empty() const noexcept
120 return (v == v.zwxy()).mask() == 0b1111;
125 [[nodiscard]]
constexpr explicit operator bool() const noexcept
137 return *
this = *
this | rhs;
147 return *
this = *
this | rhs;
150 [[nodiscard]]
constexpr point2 operator[](
std::size_t i)
const noexcept
154 return point2{v.xy01()};
156 return point2{v.zy01()};
158 return point2{v.xw01()};
160 return point2{v.zw01()};
167 [[nodiscard]]
constexpr friend point2 get(aarectangle
const &rhs)
noexcept
169 if constexpr (I == 0) {
170 return point2{rhs.v.xy01()};
171 }
else if constexpr (I == 1) {
172 return point2{rhs.v.zy01()};
173 }
else if constexpr (I == 2) {
174 return point2{rhs.v.xw01()};
175 }
else if constexpr (I == 3) {
176 return point2{rhs.v.zw01()};
178 hi_static_no_default();
191 [[nodiscard]]
constexpr value_type x() const noexcept
196 [[nodiscard]]
constexpr value_type y() const noexcept
201 [[nodiscard]]
constexpr value_type width() const noexcept
203 return (v.zwzw() - v).x();
206 [[nodiscard]]
constexpr value_type height() const noexcept
208 return (v.zwzw() - v).y();
211 [[nodiscard]]
constexpr value_type bottom() const noexcept
216 [[nodiscard]]
constexpr value_type top() const noexcept
221 [[nodiscard]]
constexpr value_type left() const noexcept
226 [[nodiscard]]
constexpr value_type right() const noexcept
233 [[nodiscard]]
constexpr value_type
middle() const noexcept
235 return (bottom() + top()) / value_type{2};
240 [[nodiscard]]
constexpr value_type
center() const noexcept
242 return (left() + right()) / value_type{2};
249 return midpoint(get<0>(rhs), get<3>(rhs));
252 constexpr aarectangle& set_width(value_type newWidth)
noexcept
254 v = v.xyxw() + array_type{value_type{0}, value_type{0}, newWidth, value_type{0}};
258 constexpr aarectangle& set_height(value_type newHeight)
noexcept
260 v = v.xyzy() + array_type{value_type{0}, value_type{0}, value_type{0}, newHeight};
268 [[nodiscard]]
constexpr bool contains(point2
const& rhs)
const noexcept
271 return (
static_cast<array_type>(rhs).xyxy() >= v).mask() == 0b0011;
279 [[nodiscard]]
constexpr bool contains(point3
const& rhs)
const noexcept
293 auto x = value_type{0};
298 x = haystack.right() - needle.width();
301 x = haystack.center() - needle.width() / value_type{2};
307 auto y = value_type{0};
309 y = haystack.bottom();
312 y = haystack.top() - needle.height();
315 y = haystack.middle() - needle.height() / value_type{2};
321 return {point2{x, y}, needle};
344 return equal(lhs.v, rhs.v);
349 if (lhs.empty() or rhs.
empty()) {
353 auto const rhs_swap = rhs.v.zwxy();
356 if (((lhs.v > rhs_swap).mask() & 0b0011) != 0) {
361 if (((lhs.v < rhs_swap).mask() & 0b1100) != 0) {
368 [[nodiscard]]
friend constexpr aarectangle operator|(aarectangle
const& lhs, aarectangle
const& rhs)
noexcept
375 return aarectangle{min(get<0>(lhs), get<0>(rhs)), max(get<3>(lhs), get<3>(rhs))};
379 [[nodiscard]]
friend constexpr aarectangle operator|(aarectangle
const& lhs, point2
const& rhs)
noexcept
382 return aarectangle{rhs, rhs};
384 return aarectangle{min(get<0>(lhs), rhs), max(get<3>(lhs), rhs)};
395 auto const new_extent = lhs.
size() * rhs;
397 auto const offset = diff * 0.5f;
399 auto const p0 = get<0>(lhs) - offset;
400 auto const p3 = max(get<3>(lhs) + offset, p0);
412 return aarectangle{lhs.v + neg_mask<0b0011>(array_type::broadcast(rhs))};
417 return lhs = lhs + rhs;
433 return lhs = lhs - rhs;
438 auto const p0 = round(get<0>(rhs));
439 auto const size = round(rhs.
size());
447 auto const p0 =
floor(get<0>(rhs));
448 auto const p3 =
ceil(get<3>(rhs));
456 auto const p0 =
floor(get<0>(lhs), rhs);
457 auto const p3 =
ceil(get<3>(lhs), rhs);
465 auto const p0 =
ceil(get<0>(rhs));
466 auto const p3 =
floor(get<3>(rhs));
480 auto const p0 = max(get<0>(lhs), get<0>(rhs));
481 auto const p3 = min(get<3>(lhs), get<3>(rhs));
482 if (p0.x() < p3.x() && p0.y() < p3.y()) {
489 [[nodiscard]]
friend value_type distance(
aarectangle const& lhs, point2
const& rhs)
noexcept
491 auto const lhs_ =
static_cast<array_type
>(lhs);
492 auto const rhs_ =
static_cast<array_type
>(rhs);
494 auto const closest_point = max(min(rhs_, lhs_.zwzw()), lhs_);
495 auto const v_closest_point = closest_point - rhs_;
496 return hypot<0b0011>(v_closest_point).x();
514 constexpr static bool is_always_lock_free =
false;
516 constexpr atomic()
noexcept =
default;
536 return is_always_lock_free;
539 void store(
value_type desired, std::memory_order = std::memory_order_seq_cst)
noexcept
541 auto const lock = std::scoped_lock(_mutex);
545 value_type load(std::memory_order = std::memory_order_seq_cst)
const noexcept
547 auto const lock = std::scoped_lock(_mutex);
553 auto const lock = std::scoped_lock(_mutex);
554 return std::exchange(_value, desired);
559 auto const lock = std::scoped_lock(_mutex);
560 if (_value == expected) {
572 std::memory_order success,
573 std::memory_order failure)
noexcept
587 std::memory_order order = std::memory_order_seq_cst)
noexcept
594 auto const lock = std::scoped_lock(_mutex);
602 auto const lock = std::scoped_lock(_mutex);
603 return _value |= arg;
613struct std::formatter<
hi::aarectangle, char> : std::formatter<std::string, char> {
616 return std::formatter<std::string, char>::format(std::format(
"{}:{}", get<0>(t), t.
size()), fc);
Functions and types for accessing operating system threads.
Definition of the unfair_mutex.
types and utilities for alignment.
Defined the geo::extent, extent2 and extent3 types.
@ middle
Align to the vertical-middle.
@ bottom
Align to the bottom.
@ right
Align the text to the right side.
@ left
Align the text to the left side.
@ center
Align the text in the center.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
@ inside
The border is drawn inside the edge of a quad.
@ outside
The border is drawn outside the edge of a quad.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition simd_intf.hpp:18
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
friend constexpr aarectangle ceil(aarectangle const &lhs, extent2 const &rhs) noexcept
Round rectangle by expanding to a certain granularity.
Definition aarectangle.hpp:454
constexpr extent2 size() const noexcept
Get size of the rectangle.
Definition aarectangle.hpp:186
friend constexpr aarectangle intersect(aarectangle const &lhs, aarectangle const &rhs) noexcept
Return the overlapping part of two rectangles.
Definition aarectangle.hpp:478
static constexpr aarectangle large() noexcept
Create a large axis aligned rectangle.
Definition aarectangle.hpp:46
friend constexpr aarectangle operator*(aarectangle const &lhs, value_type rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarectangle.hpp:393
constexpr friend point2 midpoint(aarectangle const &rhs) noexcept
Get the center of the rectangle.
Definition aarectangle.hpp:247
constexpr aarectangle(extent2 const &extent) noexcept
Create a rectangle from the size.
Definition aarectangle.hpp:73
friend constexpr aarectangle operator-(aarectangle const &lhs, value_type rhs) noexcept
Shrink the rectangle for the same amount in all directions.
Definition aarectangle.hpp:426
constexpr bool empty() const noexcept
Check if the rectangle has no area.
Definition aarectangle.hpp:118
constexpr bool contains(point3 const &rhs) const noexcept
Check if a 3D coordinate is inside the rectangle.
Definition aarectangle.hpp:279
constexpr bool contains(point2 const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarectangle.hpp:268
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:337
friend constexpr aarectangle floor(aarectangle const &rhs) noexcept
Round rectangle by shrinking to pixel edge.
Definition aarectangle.hpp:463
constexpr bool holds_invariant() const noexcept
Make sure p0 is left/bottom from p3.
Definition aarectangle.hpp:111
friend constexpr aarectangle align(aarectangle haystack, extent2 needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition aarectangle.hpp:291
friend constexpr aarectangle align(aarectangle haystack, aarectangle needle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition aarectangle.hpp:330
friend constexpr aarectangle operator+(aarectangle const &lhs, value_type rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarectangle.hpp:410
constexpr value_type center() const noexcept
The center on the x-axis between left and right.
Definition aarectangle.hpp:240
constexpr aarectangle & operator|=(point2 const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition aarectangle.hpp:145
constexpr aarectangle(float x, float y, float width, float height) noexcept
Create a box from the position and size.
Definition aarectangle.hpp:63
friend constexpr aarectangle ceil(aarectangle const &rhs) noexcept
Round rectangle by expanding to pixel edge.
Definition aarectangle.hpp:445
constexpr aarectangle(point2 const &p0, point2 const &p3) noexcept
Create a rectangle from the left-bottom and right-top points.
Definition aarectangle.hpp:82
constexpr aarectangle & operator|=(aarectangle const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition aarectangle.hpp:135
constexpr value_type middle() const noexcept
The middle on the y-axis between bottom and top.
Definition aarectangle.hpp:233
constexpr aarectangle(point2 const &p0, extent2 const &extent) noexcept
Create a rectangle from the size.
Definition aarectangle.hpp:97
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
A high-level geometric extent.
Definition extent2.hpp:32
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector2.hpp:27
T compare_exchange_weak(T... args)
T is_lock_free(T... args)