HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
translate.hpp
1// Copyright Take Vos 2021.
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 "matrix.hpp"
8#include "identity.hpp"
9
10namespace tt {
11namespace geo {
12
13template<int D>
14class translate {
15public:
16 static_assert(D == 2 || D == 3, "Only 2D or 3D translation-matrices are supported");
17
18 constexpr translate(translate const &) noexcept = default;
19 constexpr translate(translate &&) noexcept = default;
20 constexpr translate &operator=(translate const &) noexcept = default;
21 constexpr translate &operator=(translate &&) noexcept = default;
22
23 [[nodiscard]] constexpr operator matrix<2>() const noexcept requires(D == 2)
24 {
25 tt_axiom(is_valid());
26 ttlet ones = f32x4::broadcast(1.0);
27 return matrix<2>{ones.x000(), ones._0y00(), ones._00z0(), ones._000w() + _v};
28 }
29
30 [[nodiscard]] constexpr operator matrix<3>() const noexcept
31 {
32 tt_axiom(is_valid());
33 ttlet ones = f32x4::broadcast(1.0);
34 return matrix<3>{ones.x000(), ones._0y00(), ones._00z0(), ones._000w() + _v};
35 }
36
37 [[nodiscard]] constexpr translate() noexcept : _v() {}
38
39 [[nodiscard]] constexpr translate(identity const &) noexcept : translate() {}
40
41 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
42 {
43 tt_axiom(is_valid());
44 return _v;
45 }
46
47 [[nodiscard]] constexpr explicit translate(f32x4 const &other) noexcept : _v(other)
48 {
49 tt_axiom(is_valid());
50 }
51
52 [[nodiscard]] constexpr explicit translate(aarectangle const &other) noexcept : _v(static_cast<f32x4>(get<0>(other)).xy00())
53 {
54 tt_axiom(is_valid());
55 }
56
57 template<int E>
58 requires(E < D) [[nodiscard]] constexpr translate(translate<E> const &other) noexcept : _v(static_cast<f32x4>(other))
59 {
60 tt_axiom(is_valid());
61 }
62
63 template<int E>
64 requires(E <= D) [[nodiscard]] constexpr explicit translate(vector<E> const &other) noexcept : _v(static_cast<f32x4>(other))
65 {
66 tt_axiom(is_valid());
67 }
68
69 template<int E>
70 requires(E <= D) [[nodiscard]] constexpr explicit translate(point<E> const &other) noexcept : _v(static_cast<f32x4>(other).xyz0())
71 {
72 tt_axiom(is_valid());
73 }
74
75 [[nodiscard]] constexpr translate(float x, float y) noexcept requires(D == 2) : _v(x, y, 0.0, 0.0) {}
76
77 [[nodiscard]] constexpr translate(float x, float y, float z = 0.0) noexcept requires(D == 3) : _v(x, y, z, 0.0) {}
78
85 [[nodiscard]] constexpr static translate align(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
86 {
87 auto x = 0.0f;
88 if (alignment == horizontal_alignment::left) {
89 x = dst_rectangle.left();
90
91 } else if (alignment == horizontal_alignment::right) {
92 x = dst_rectangle.right() - src_rectangle.width();
93
94 } else if (alignment == horizontal_alignment::center) {
95 x = dst_rectangle.center() - src_rectangle.width() * 0.5f;
96
97 } else {
98 tt_no_default();
99 }
100
101 auto y = 0.0f;
102 if (alignment == vertical_alignment::bottom) {
103 y = dst_rectangle.bottom();
104
105 } else if (alignment == vertical_alignment::top) {
106 y = dst_rectangle.top() - src_rectangle.height();
107
108 } else if (alignment == vertical_alignment::middle) {
109 y = dst_rectangle.middle() - src_rectangle.height() * 0.5f;
110
111 } else {
112 tt_no_default();
113 }
114
115 return translate{x - src_rectangle.left(), y - src_rectangle.bottom()};
116 }
117
118 template<int E>
119 [[nodiscard]] constexpr vector<E> operator*(vector<E> const &rhs) const noexcept
120 {
121 // Vectors are not translated.
122 tt_axiom(is_valid() && rhs.is_valid());
123 return rhs;
124 }
125
126 template<int E>
127 [[nodiscard]] constexpr point<std::max(D, E)> operator*(point<E> const &rhs) const noexcept
128 {
129 tt_axiom(is_valid() && rhs.is_valid());
130 return point<std::max(D, E)>{_v + static_cast<f32x4>(rhs)};
131 }
132
133 [[nodiscard]] constexpr aarectangle operator*(aarectangle const &rhs) const noexcept requires(D == 2)
134 {
135 return aarectangle{*this * get<0>(rhs), *this * get<3>(rhs)};
136 }
137
138 [[nodiscard]] constexpr rectangle operator*(rectangle const &rhs) const noexcept
139 {
140 return rectangle{*this * get<0>(rhs), *this * get<1>(rhs), *this * get<2>(rhs), *this * get<3>(rhs)};
141 }
142
143 [[nodiscard]] constexpr translate operator*(identity const &) const noexcept
144 {
145 tt_axiom(is_valid());
146 return *this;
147 }
148
149 template<int E>
150 [[nodiscard]] constexpr auto operator*(matrix<E> const &rhs) const noexcept
151 {
152 tt_axiom(is_valid() && rhs.is_valid());
153 return matrix<std::max(D, E)>{get<0>(rhs), get<1>(rhs), get<2>(rhs), get<3>(rhs) + _v};
154 }
155
156 template<int E>
157 [[nodiscard]] constexpr auto operator*(translate<E> const &rhs) const noexcept
158 {
159 tt_axiom(is_valid() && rhs.is_valid());
160 return translate<std::max(D, E)>{_v + static_cast<f32x4>(rhs)};
161 }
162
163 template<int E>
164 [[nodiscard]] constexpr bool operator==(translate<E> const &rhs) const noexcept
165 {
166 tt_axiom(is_valid() && rhs.is_valid());
167 return _v == static_cast<f32x4>(rhs);
168 }
169
170 [[nodiscard]] constexpr translate operator~() const noexcept
171 {
172 return translate{-_v};
173 }
174
175 [[nodiscard]] constexpr bool is_valid() const noexcept
176 {
177 return _v.w() == 0.0f && (D == 3 || _v.z() == 0.0f);
178 }
179
180private:
181 f32x4 _v;
182};
183
184} // namespace geo
185
186using translate2 = geo::translate<2>;
187using translate3 = geo::translate<3>;
188
189constexpr translate3 translate_z(float z) noexcept
190{
191 return translate3{0.0f, 0.0f, z};
192}
193
194} // namespace tt
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:18
Definition identity.hpp:11
Definition matrix.hpp:18
A high-level geometric point Part of the high-level vec, point, mat and color types.
Definition point.hpp:21
Definition translate.hpp:14
static constexpr translate align(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
Align a rectangle within another rectangle.
Definition translate.hpp:85
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
T max(T... args)