HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
iaarect.hpp
1// Copyright 2020 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Foundation/ivec.hpp"
7
8namespace tt {
9
12class iaarect {
18 ivec v;
19
20public:
21 tt_force_inline iaarect() noexcept : v() {}
22 tt_force_inline iaarect(iaarect const &rhs) noexcept = default;
23 tt_force_inline iaarect &operator=(iaarect const &rhs) noexcept = default;
24 tt_force_inline iaarect(iaarect &&rhs) noexcept = default;
25 tt_force_inline iaarect &operator=(iaarect &&rhs) noexcept = default;
26
27 iaarect(__m128i rhs) noexcept :
28 v(rhs) {}
29
30 iaarect &operator=(__m128i rhs) noexcept {
31 v = rhs;
32 return *this;
33 }
34
35 operator __m128i () const noexcept {
36 return v;
37 }
38
46 template<typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
47 tt_force_inline iaarect(T x, T y, T width, T height) noexcept :
48 iaarect(ivec(x, y, x + width, y + height)) {}
49
55 tt_force_inline iaarect(ivec const &offset, ivec const &extent) noexcept :
56 iaarect(offset.xyxy() + extent.zwxy()) {}
57
58 [[nodiscard]] tt_force_inline static iaarect p0p3(ivec const &p1, ivec const &p2) noexcept {
59 return _mm_blend_epi16(p1, p2.xyxy(), 0b11'11'00'00);
60 }
61
67 iaarect &operator|=(iaarect const &rhs) noexcept {
68 return *this = *this | rhs;
69 }
70
75 iaarect &operator+=(ivec const &rhs) noexcept {
76 return *this = *this + rhs;
77 }
78
83 iaarect &operator-=(ivec const &rhs) noexcept {
84 return *this = *this - rhs;
85 }
86
92 template<size_t I>
93 [[nodiscard]] tt_force_inline ivec corner() const noexcept {
94 static_assert(I <= 3);
95 if constexpr (I == 0) {
96 return v.xy01();
97 } else if constexpr (I == 1) {
98 return v.zy01();
99 } else if constexpr (I == 2) {
100 return v.xw01();
101 } else {
102 return v.zw01();
103 }
104 }
105
110 [[nodiscard]] tt_force_inline ivec offset() const noexcept { return corner<0>(); }
111
112
117 [[nodiscard]] ivec extent() const noexcept {
118 return corner<3>() - corner<0>();
119 }
120
121 [[nodiscard]] tt_force_inline int x1() const noexcept { return v.x(); }
122 [[nodiscard]] tt_force_inline int y1() const noexcept { return v.y(); }
123 [[nodiscard]] tt_force_inline int x2() const noexcept { return v.z(); }
124 [[nodiscard]] tt_force_inline int y2() const noexcept { return v.w(); }
125 [[nodiscard]] tt_force_inline int width() const noexcept { return extent().x(); }
126 [[nodiscard]] tt_force_inline int height() const noexcept { return extent().y(); }
127
132 [[nodiscard]] bool contains(ivec const &rhs) const noexcept {
133 return
134 (((rhs >= v) & 0x00ff) == 0x00ff) &&
135 (((rhs <= v) & 0xff00) == 0xff00);
136 }
137
138 [[nodiscard]] friend bool operator==(iaarect const &lhs, iaarect const &rhs) noexcept {
139 return lhs.v == rhs.v;
140 }
141
142 [[nodiscard]] friend bool operator!=(iaarect const &lhs, iaarect const &rhs) noexcept {
143 return !(lhs == rhs);
144 }
145
146 [[nodiscard]] friend iaarect operator|(iaarect const &lhs, iaarect const &rhs) noexcept {
147 return _mm_blend_epi16(min(lhs.v, rhs.v), max(lhs.v, rhs.v), 0b11'11'00'00);
148 }
149
150 [[nodiscard]] friend iaarect operator+(iaarect const &lhs, ivec const &rhs) noexcept {
151 return static_cast<__m128i>(lhs.v + rhs.xyxy());
152 }
153
154 [[nodiscard]] friend iaarect operator-(iaarect const &lhs, ivec const &rhs) noexcept {
155 return static_cast<__m128i>(lhs.v - rhs.xyxy());
156 }
157
164 template<typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
165 [[nodiscard]] friend iaarect expand(iaarect const &lhs, T rhs) noexcept {
166 ttlet _0000 = _mm_setzero_si128();
167 ttlet _000r = _mm_insert_epi32(_0000, rhs, 0);
168 ttlet _00rr = ivec{_mm_shuffle_epi32(_000r, _MM_SHUFFLE(1,1,0,0))};
169 ttlet _rr00 = ivec{_mm_shuffle_epi32(_000r, _MM_SHUFFLE(0,0,1,1))};
170 return static_cast<__m128i>((lhs.v - _00rr) + _rr00);
171 }
172};
173
174}
175
Class which represents an axis-aligned rectangle.
Definition iaarect.hpp:12
tt_force_inline ivec offset() const noexcept
Get coordinate of the bottom-left corner.
Definition iaarect.hpp:110
ivec extent() const noexcept
Get size of the rectangle.
Definition iaarect.hpp:117
tt_force_inline iaarect(ivec const &offset, ivec const &extent) noexcept
Create a box from the position and size.
Definition iaarect.hpp:55
bool contains(ivec const &rhs) const noexcept
Check if a 2D coordinate is inside the rectangle.
Definition iaarect.hpp:132
tt_force_inline iaarect(T x, T y, T width, T height) noexcept
Create a box from the position and size.
Definition iaarect.hpp:47
iaarect & operator-=(ivec const &rhs) noexcept
Translate the box to a new position.
Definition iaarect.hpp:83
iaarect & operator|=(iaarect const &rhs) noexcept
Expand the current rectangle to include the new rectangle.
Definition iaarect.hpp:67
tt_force_inline ivec corner() const noexcept
Get coordinate of a corner.
Definition iaarect.hpp:93
friend iaarect expand(iaarect const &lhs, T rhs) noexcept
Expand the rectangle for the same amount in all directions.
Definition iaarect.hpp:165
iaarect & operator+=(ivec const &rhs) noexcept
Translate the box to a new position.
Definition iaarect.hpp:75
A 4D vector.
Definition ivec.hpp:37
T max(T... args)
T min(T... args)