6#include "TTauri/Foundation/vec.hpp"
7#include "TTauri/Foundation/attributes.hpp"
25 tt_force_inline
aarect() noexcept : v() {}
26 tt_force_inline
aarect(
aarect const &rhs)
noexcept =
default;
27 tt_force_inline
aarect &operator=(
aarect const &rhs)
noexcept =
default;
29 tt_force_inline
aarect &operator=(
aarect &&rhs)
noexcept =
default;
39 template<
typename X,
typename Y,
typename W=float,
typename H=float,
40 std::enable_if_t<std::is_arithmetic_v<X> && std::is_arithmetic_v<Y> && std::is_arithmetic_v<W> && std::is_arithmetic_v<H>,
int> = 0>
41 tt_force_inline
aarect(X x, Y y, W width, H height) noexcept :
43 numeric_cast<float>(x),
44 numeric_cast<float>(y),
45 numeric_cast<float>(x) + numeric_cast<float>(width),
46 numeric_cast<float>(y) + numeric_cast<float>(height)
55 v(position.xyxy() +
extent._00xy()) {
56 tt_assume(position.is_point());
57 tt_assume(position.z() == 0.0);
58 tt_assume(
extent.is_vector());
59 tt_assume(
extent.z() == 0.0);
68 tt_assume(
extent.is_vector());
69 tt_assume(
extent.z() == 0.0);
75 [[nodiscard]] tt_force_inline
static aarect p0p3(
vec const &v)
noexcept {
81 [[nodiscard]] tt_force_inline
static aarect p0p3(
vec const &
p0,
vec const &p3)
noexcept {
82 tt_assume(
p0.is_point());
83 tt_assume(p3.is_point());
87 operator bool () const noexcept {
88 return v.xyxy() != v.zwzw();
97 return *
this = *
this | rhs;
106 return *
this = *
this | rhs;
115 return *
this = *
this + rhs;
123 return *
this = *
this - rhs;
131 return *
this = *
this * rhs;
140 [[nodiscard]] tt_force_inline
vec corner() const noexcept {
141 static_assert(I <= 3);
142 if constexpr (I == 0) {
144 }
else if constexpr (I == 1) {
146 }
else if constexpr (I == 2) {
164 [[nodiscard]] tt_force_inline
vec p0() const noexcept {
168 [[nodiscard]] tt_force_inline
vec p3() const noexcept {
177 [[nodiscard]] tt_force_inline
vec offset() const noexcept {
186 return (v.zwzw() - v).xy00();
189 [[nodiscard]] tt_force_inline
float x() const noexcept {
193 [[nodiscard]] tt_force_inline
float y() const noexcept {
197 [[nodiscard]] tt_force_inline
float width() const noexcept {
198 return (v.zwzw() - v).x();
201 [[nodiscard]] tt_force_inline
float height() const noexcept {
202 return (v.zwzw() - v).y();
205 template<
typename T, std::enable_if_t<std::is_arithmetic_v<T>,
int> = 0>
206 tt_force_inline aarect &width(T newWidth)
noexcept {
207 v = v.xyxw() + vec::make_z(newWidth);
211 template<
typename T, std::enable_if_t<std::is_arithmetic_v<T>,
int> = 0>
212 tt_force_inline aarect &height(T newHeight)
noexcept {
213 v = v.xyzy() + vec::make_w(newHeight);
222 return (rhs.xyxy() >= v) == 0b0011;
233 if (alignment == HorizontalAlignment::Left) {
234 x = haystack.p0().x();
236 }
else if (alignment == HorizontalAlignment::Right) {
237 x = haystack.p3().x() - needle.width();
239 }
else if (alignment == HorizontalAlignment::Center) {
240 x = (haystack.p0().x() + (haystack.width() * 0.5f)) - (needle.width() * 0.5f);
247 if (alignment == VerticalAlignment::Bottom) {
248 y = haystack.p0().y();
250 }
else if (alignment == VerticalAlignment::Top) {
251 y = haystack.p3().y() - needle.height();
253 }
else if (alignment == VerticalAlignment::Middle) {
254 y = (haystack.p0().y() + (haystack.height() * 0.5f)) - (needle.height() * 0.5f);
266 return align(outside, inside, alignment);
269 [[nodiscard]]
friend bool operator==(
aarect const &lhs,
aarect const &rhs)
noexcept {
270 return lhs.v == rhs.v;
273 [[nodiscard]]
friend bool operator!=(aarect
const &lhs, aarect
const &rhs)
noexcept {
274 return !(lhs == rhs);
277 [[nodiscard]]
friend bool overlaps(aarect
const &lhs, aarect
const &rhs)
noexcept {
278 ttlet rhs_swap = rhs.v.zwxy();
279 if (((lhs.v > rhs_swap) & 0x0011) != 0) {
282 if (((lhs.v < rhs_swap) & 0x1100) != 0) {
288 [[nodiscard]]
friend aarect operator|(aarect
const &lhs, aarect
const &rhs)
noexcept {
292 [[nodiscard]]
friend aarect operator|(aarect
const &lhs, vec
const &rhs)
noexcept {
293 tt_assume(rhs.is_point());
297 [[nodiscard]]
friend aarect operator+(aarect
const &lhs, vec
const &rhs)
noexcept {
301 [[nodiscard]]
friend aarect operator-(aarect
const &lhs, vec
const &rhs)
noexcept {
305 [[nodiscard]]
friend aarect operator*(aarect
const &lhs,
float rhs)
noexcept {
315 ttlet
extent = lhs.extent();
316 ttlet scaled_extent =
extent * rhs;
317 ttlet diff_extent = scaled_extent -
extent;
318 ttlet half_diff_extent = diff_extent * 0.5f;
320 ttlet p1 = lhs.p0() - half_diff_extent;
321 ttlet p2 = lhs.p3() + half_diff_extent;
346 [[nodiscard]]
friend aarect round(
aarect const &rhs)
noexcept {
Class which represents an axis-aligned rectangle.
Definition aarect.hpp:13
aarect & operator*=(float rhs) noexcept
Scale the box by moving the positions (scaling the vectors).
Definition aarect.hpp:130
tt_force_inline vec corner() const noexcept
Get coordinate of a corner.
Definition aarect.hpp:140
friend aarect align(aarect haystack, aarect needle, Alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition aarect.hpp:231
tt_force_inline vec p0() const noexcept
Get coordinate of a corner.
Definition aarect.hpp:164
aarect & operator-=(vec const &rhs) noexcept
Translate the box to a new position.
Definition aarect.hpp:122
aarect & operator|=(aarect const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition aarect.hpp:96
aarect & operator|=(vec const &rhs) noexcept
Expand the current rectangle to include the new point.
Definition aarect.hpp:105
tt_force_inline vec offset() const noexcept
Get vector from origin to the bottom-left corner.
Definition aarect.hpp:177
friend aarect scale(aarect const &lhs, float rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarect.hpp:314
friend aarect expand(aarect const &lhs, float rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition aarect.hpp:332
bool contains(vec const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition aarect.hpp:221
tt_force_inline aarect(X x, Y y, W width, H height) noexcept
Create a box from the position and size.
Definition aarect.hpp:41
vec extent() const noexcept
Get size of the rectangle.
Definition aarect.hpp:185
aarect & operator+=(vec const &rhs) noexcept
Translate the box to a new position.
Definition aarect.hpp:114
tt_force_inline aarect(vec const &position, vec const &extent) noexcept
Create a rectangle from the position and size.
Definition aarect.hpp:54
static aarect _align(aarect outside, aarect inside, Alignment alignment) noexcept
Need to call the hiden friend function from within another class.
Definition aarect.hpp:265
friend aarect shrink(aarect const &lhs, float rhs) noexcept
Shrink the rectangle for the same amount in all directions.
Definition aarect.hpp:342
tt_force_inline aarect(vec const &extent) noexcept
Create a rectangle from the size.
Definition aarect.hpp:66
static tt_force_inline aarect p0p3(vec const &v) noexcept
Create aarect from packed p0p3 coordinates.
Definition aarect.hpp:75
Definition R32G32B32A32SFloat.hpp:13
A 4D vector.
Definition vec.hpp:37
static tt_force_inline vec point(float x=0.0f, float y=0.0f, float z=0.0f) noexcept
Create a point out of 2 to 4 values.
Definition vec.hpp:180