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 :
71 _v(static_cast<f32x4>(other).xyz0())
72 {
73 tt_axiom(is_valid());
74 }
75
76 [[nodiscard]] constexpr translate(float x, float y) noexcept requires(D == 2) : _v(x, y, 0.0, 0.0) {}
77
78 [[nodiscard]] constexpr translate(float x, float y, float z = 0.0) noexcept requires(D == 3) : _v(x, y, z, 0.0) {}
79
86 [[nodiscard]] constexpr static translate
87 align(aarectangle src_rectangle, aarectangle dst_rectangle, alignment alignment) noexcept
88 {
89 auto x = 0.0f;
90 if (alignment == horizontal_alignment::left) {
91 x = dst_rectangle.left();
92
93 } else if (alignment == horizontal_alignment::right) {
94 x = dst_rectangle.right() - src_rectangle.width();
95
96 } else if (alignment == horizontal_alignment::center) {
97 x = dst_rectangle.center() - src_rectangle.width() * 0.5f;
98
99 } else {
100 tt_no_default();
101 }
102
103 auto y = 0.0f;
104 if (alignment == vertical_alignment::bottom) {
105 y = dst_rectangle.bottom();
106
107 } else if (alignment == vertical_alignment::top) {
108 y = dst_rectangle.top() - src_rectangle.height();
109
110 } else if (alignment == vertical_alignment::middle) {
111 y = dst_rectangle.middle() - src_rectangle.height() * 0.5f;
112
113 } else {
114 tt_no_default();
115 }
116
117 return translate{x - src_rectangle.left(), y - src_rectangle.bottom()};
118 }
119
120 template<int E>
121 [[nodiscard]] constexpr vector<E> operator*(vector<E> const &rhs) const noexcept
122 {
123 // Vectors are not translated.
124 tt_axiom(is_valid() && rhs.is_valid());
125 return rhs;
126 }
127
128 template<int E>
129 [[nodiscard]] constexpr point<std::max(D, E)> operator*(point<E> const &rhs) const noexcept
130 {
131 tt_axiom(is_valid() && rhs.is_valid());
132 return point<std::max(D, E)>{_v + static_cast<f32x4>(rhs)};
133 }
134
135 [[nodiscard]] constexpr aarectangle operator*(aarectangle const &rhs) const noexcept requires(D == 2)
136 {
137 return aarectangle{*this * get<0>(rhs), *this * get<3>(rhs)};
138 }
139
140 [[nodiscard]] constexpr rectangle operator*(rectangle const &rhs) const noexcept
141 {
142 return rectangle{*this * get<0>(rhs), *this * get<1>(rhs), *this * get<2>(rhs), *this * get<3>(rhs)};
143 }
144
145 [[nodiscard]] constexpr translate operator*(identity const &) const noexcept
146 {
147 tt_axiom(is_valid());
148 return *this;
149 }
150
151 template<int E>
152 [[nodiscard]] constexpr auto operator*(matrix<E> const &rhs) const noexcept
153 {
154 tt_axiom(is_valid() && rhs.is_valid());
155 return matrix<std::max(D, E)>{get<0>(rhs), get<1>(rhs), get<2>(rhs), get<3>(rhs) + _v};
156 }
157
158 template<int E>
159 [[nodiscard]] constexpr auto operator*(translate<E> const &rhs) const noexcept
160 {
161 tt_axiom(is_valid() && rhs.is_valid());
162 return translate<std::max(D, E)>{_v + static_cast<f32x4>(rhs)};
163 }
164
165 template<int E>
166 [[nodiscard]] constexpr bool operator==(translate<E> const &rhs) const noexcept
167 {
168 tt_axiom(is_valid() && rhs.is_valid());
169 return _v == static_cast<f32x4>(rhs);
170 }
171
172 [[nodiscard]] constexpr translate operator~() const noexcept
173 {
174 return translate{-_v};
175 }
176
177 [[nodiscard]] constexpr bool is_valid() const noexcept
178 {
179 return _v.w() == 0.0f && (D == 3 || _v.z() == 0.0f);
180 }
181
182 [[nodiscard]] friend constexpr translate round(translate const &rhs) noexcept
183 {
184 return translate{round(rhs._v)};
185 }
186
187private:
188 f32x4 _v;
189};
190
191} // namespace geo
192
193using translate2 = geo::translate<2>;
194using translate3 = geo::translate<3>;
195
196constexpr translate3 translate_z(float z) noexcept
197{
198 return translate3{0.0f, 0.0f, z};
199}
200
201} // namespace tt
alignment
Vertical and horizontal alignment.
Definition alignment.hpp:47
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
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:22
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:87
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
T max(T... args)