12#include "rectangle.hpp"
13#include "../macros.hpp"
18hi_export_module(hikogui.geometry : quad);
20hi_export
namespace hi::inline
v1 {
29 constexpr quad() noexcept : p0(), p1(), p2(), p3() {}
33 constexpr quad(quad
const &)
noexcept =
default;
34 constexpr quad(quad &&) noexcept = default;
35 constexpr quad &operator=(quad const &) noexcept = default;
36 constexpr quad &operator=(quad &&) noexcept = default;
38 constexpr quad(aarectangle const &rhs) noexcept
40 auto const tmp = f32x4{rhs};
41 p0 = point3{tmp.xy01()};
42 p1 = point3{tmp.zy01()};
43 p2 = point3{tmp.xw01()};
44 p3 = point3{tmp.zw01()};
47 constexpr quad(rectangle
const &rhs) noexcept : p0(get<0>(rhs)), p1(get<1>(rhs)), p2(get<2>(rhs)), p3(get<3>(rhs)) {}
51 [[nodiscard]]
constexpr vector3
bottom() const noexcept
58 [[nodiscard]]
constexpr vector3
top() const noexcept
65 [[nodiscard]]
constexpr vector3
left() const noexcept
72 [[nodiscard]]
constexpr vector3
right() const noexcept
83 auto const[x, y, z, zeros] = transpose(f32x4{bottom()}, f32x4{left()}, f32x4{top()}, f32x4{right()});
84 return sqrt(x * x + y * y + z * z);
94 default: hi_no_default();
98 [[nodiscard]]
constexpr point3
const &operator[](
std::size_t index)
const noexcept
105 default: hi_no_default();
109 template<std::
size_t I>
110 [[nodiscard]]
constexpr friend point3
const &get(quad
const &rhs)
noexcept
112 static_assert(I < 4,
"Index out of range.");
114 if constexpr (I == 0) {
116 }
else if constexpr (I == 1) {
118 }
else if constexpr (I == 2) {
125 template<std::
size_t I>
126 [[nodiscard]]
constexpr friend point3 &get(quad &rhs)
noexcept
128 static_assert(I < 4,
"Index out of range.");
130 if constexpr (I == 0) {
132 }
else if constexpr (I == 1) {
134 }
else if constexpr (I == 2) {
151 auto const t = f32x4{lhs.top()};
152 auto const l = f32x4{lhs.left()};
153 auto const b = f32x4{lhs.bottom()};
154 auto const r = f32x4{lhs.right()};
156 auto const[x, y, z, ones] = transpose(t, l, b, r);
157 auto const square_lengths = x * x + y * y + z * z;
158 auto const inv_lengths = rsqrt(square_lengths);
159 auto const norm_t = t * inv_lengths.xxxx();
160 auto const norm_l = l * inv_lengths.yyyy();
161 auto const norm_b = b * inv_lengths.zzzz();
162 auto const norm_r = r * inv_lengths.wwww();
164 auto const extra_width = rhs.xxxx();
165 auto const extra_height = rhs.yyyy();
167 auto const top_extra = vector3{norm_t * extra_width};
168 auto const left_extra = vector3{norm_l * extra_height};
169 auto const bottom_extra = vector3{norm_b * extra_width};
170 auto const right_extra = vector3{norm_r * extra_height};
172 auto const lengths = rcp(inv_lengths);
174 auto const rhs_times_2 = rhs + rhs;
178 lhs.
p0 - bottom_extra - left_extra,
179 lhs.p1 + bottom_extra - right_extra,
180 lhs.p2 - top_extra + left_extra,
181 lhs.p3 + top_extra + right_extra},
182 lengths + rhs_times_2.xyxy()};
195 return expand_and_edge_hypots(lhs, f32x4{rhs});
208 return expand_and_edge_hypots(lhs, -f32x4{rhs});
221 auto const[expanded_quad, new_lengths] = expand_and_edge_hypots(lhs, rhs);
222 return expanded_quad;
225 [[nodiscard]]
friend constexpr quad operator+(
quad const &lhs,
float rhs)
noexcept
227 return lhs + extent2{rhs, rhs};
240 auto const[expanded_quad, new_lengths] = shrink_and_edge_hypots(lhs, rhs);
241 return expanded_quad;
244 [[nodiscard]]
friend constexpr quad operator-(
quad const &lhs,
float rhs)
noexcept
246 return lhs - extent2{rhs, rhs};
249 [[nodiscard]]
friend constexpr aarectangle bounding_rectangle(quad
const &rhs)
noexcept
254 min_p = min(min_p, rhs.p1);
255 max_p = max(max_p, rhs.p1);
256 min_p = min(min_p, rhs.p2);
257 max_p = max(max_p, rhs.p2);
258 min_p = min(min_p, rhs.p3);
259 max_p = max(max_p, rhs.p3);
260 return aarectangle{point2{min_p}, point2{max_p}};
263 constexpr quad &operator+=(extent2
const &rhs)
noexcept
265 return *
this = *
this + rhs;
268 [[nodiscard]]
friend constexpr bool operator==(quad
const &lhs, quad
const &rhs)
noexcept =
default;
Defined the geo::extent, extent2 and extent3 types.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point3.hpp:29
friend constexpr std::pair< quad, f32x4 > expand_and_edge_hypots(quad const &lhs, f32x4 const &rhs) noexcept
Add a border around the quad.
Definition quad.hpp:149
point3 p3
Right-top.
Definition quad.hpp:27
constexpr vector3 right() const noexcept
The vector from right-bottom to right-top.
Definition quad.hpp:72
friend constexpr std::pair< quad, f32x4 > shrink_and_edge_hypots(quad const &lhs, extent2 const &rhs) noexcept
Subtract a border from the quad.
Definition quad.hpp:206
point3 p1
Right-bottom.
Definition quad.hpp:25
friend constexpr quad operator+(quad const &lhs, extent2 const &rhs) noexcept
Add a border around the quad.
Definition quad.hpp:219
constexpr vector3 left() const noexcept
The vector from left-bottom to left-top.
Definition quad.hpp:65
friend constexpr quad operator-(quad const &lhs, extent2 const &rhs) noexcept
Add a border around the quad.
Definition quad.hpp:238
point3 p0
Left-bottom.
Definition quad.hpp:24
constexpr vector3 bottom() const noexcept
The vector from left-bottom to right-bottom.
Definition quad.hpp:51
constexpr vector3 top() const noexcept
The vector from left-top to right-top.
Definition quad.hpp:58
point3 p2
Left-top.
Definition quad.hpp:26
constexpr f32x4 edge_hypots() const noexcept
Return the length of each edge.
Definition quad.hpp:81
friend constexpr std::pair< quad, f32x4 > expand_and_edge_hypots(quad const &lhs, extent2 const &rhs) noexcept
Add a border around the quad.
Definition quad.hpp:193