HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
rect.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "numeric_array.hpp"
8#include "aarect.hpp"
9#include "alignment.hpp"
10#include <array>
11
12namespace tt {
13
16class rect {
19 std::array<f32x4,4> corners;
20
21public:
22 rect() noexcept : corners({f32x4{}, f32x4{}, f32x4{}, f32x4{}}) {}
23 rect(rect const &rhs) noexcept = default;
24 rect &operator=(rect const &rhs) noexcept = default;
25 rect(rect &&rhs) noexcept = default;
26 rect &operator=(rect &&rhs) noexcept = default;
27
28 rect(f32x4 corner0, f32x4 corner1, f32x4 corner2, f32x4 corner3) noexcept :
29 corners({corner0, corner1, corner2, corner3}) {}
30
31 rect(aarect rhs) noexcept :
32 corners({rhs.corner<0>(), rhs.corner<1>(), rhs.corner<2>(), rhs.corner<3>()}) {}
33
34 rect &operator=(aarect rhs) noexcept {
35 corners[0] = rhs.corner<0>();
36 corners[1] = rhs.corner<1>();
37 corners[2] = rhs.corner<2>();
38 corners[3] = rhs.corner<3>();
39 return *this;
40 }
41
42 rect(f32x4 corner0, f32x4 extent) noexcept :
43 corners({
44 corner0,
45 corner0 + extent.x000(),
46 corner0 + extent._0y00(),
47 corner0 + extent.xy00()
48 })
49 {
50 tt_axiom(corner0.is_point());
51 tt_axiom(extent.is_vector());
52 tt_axiom(extent.z() == 0.0);
53 }
54
57 f32x4 right_vector() const noexcept {
58 return corner<1>() - corner<0>();
59 }
60
63 f32x4 up_vector() const noexcept {
64 return corner<2>() - corner<0>();
65 }
66
67 float width() const noexcept {
68 return hypot<0b0111>(right_vector());
69 }
70
71 float height() const noexcept {
72 return hypot<0b0111>(up_vector());
73 }
74
75 f32x4 extent() const noexcept {
76 return {width(), height()};
77 }
78
79 aarect aabb() const noexcept {
80 // XXX - Should actually check maximum and minimums of all points.
81 return aarect::p0p3(std::get<0>(corners).xy01(), std::get<3>(corners).xy01());
82 }
83
89 template<size_t I>
90 [[nodiscard]] f32x4 corner() const noexcept {
91 static_assert(I <= 3);
92 return std::get<I>(corners);
93 }
94
95 [[nodiscard]] friend rect expand(rect const &lhs, float rhs) noexcept {
96 ttlet rightDirection = normalize<0b0111>(lhs.right_vector());
97 ttlet upDirection = normalize<0b0111>(lhs.up_vector());
98
99 return {
100 lhs.corner<0>() + rhs * -rightDirection + rhs * -upDirection,
101 lhs.corner<1>() + rhs * rightDirection + rhs * -upDirection,
102 lhs.corner<2>() + rhs * -rightDirection + rhs * upDirection,
103 lhs.corner<3>() + rhs * rightDirection + rhs * upDirection
104 };
105 }
106};
107
108}
109
static axis_aligned_rectangle p0p3(numeric_array< float, 4 > const &v) noexcept
Create axis_aligned_rectangle from packed p0p3 coordinates.
Definition aarect.hpp:86
Class which represents an rectangle.
Definition rect.hpp:16
f32x4 right_vector() const noexcept
Get the right vector of a rectangle.
Definition rect.hpp:57
f32x4 up_vector() const noexcept
Get the up vector of a rectangle.
Definition rect.hpp:63
f32x4 corner() const noexcept
Get coordinate of a corner.
Definition rect.hpp:90