Debian Codegraph @

core/math/transform_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* transform_2d.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/math/math_funcs.h"
34 #include "core/math/rect2.h"
35 #include "core/math/vector2.h"
36 #include "core/templates/vector.h"
37
38 class String;
39
40 struct [[nodiscard]] Transform2D {
41 // WARNING: The basis of Transform2D is stored differently from Basis.
42 // In terms of columns array, the basis matrix looks like "on paper":
43 // M = (columns[0][0] columns[1][0])
44 // (columns[0][1] columns[1][1])
45 // This is such that the columns, which can be interpreted as basis vectors
46 // of the coordinate system "painted" on the object, can be accessed as columns[i].
47 // NOTE: This is the opposite of the indices in mathematical texts,
48 // meaning: $M_{12}$ in a math book corresponds to columns[1][0] here.
49 // This requires additional care when working with explicit indices.
50 // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
51
52 // WARNING: Be aware that unlike 3D code, 2D code uses a left-handed coordinate system:
53 // Y-axis points down, and angle is measure from +X to +Y in a clockwise-fashion.
54
55 static const Transform2D FLIP_X;
56 static const Transform2D FLIP_Y;
57
58 Vector2 columns[3] = {
59 { 1, 0 },
60 { 0, 1 },
61 { 0, 0 },
62 };
63
64 _FORCE_INLINE_ real_t tdotx(const Vector2 &p_v) const { return columns[0][0] * p_v.x + columns[1][0] * p_v.y; }
65 _FORCE_INLINE_ real_t tdoty(const Vector2 &p_v) const { return columns[0][1] * p_v.x + columns[1][1] * p_v.y; }
66
67 constexpr const Vector2 &operator[](int p_idx) const { return columns[p_idx]; }
68 constexpr Vector2 &operator[](int p_idx) { return columns[p_idx]; }
69
70 void invert();
71 Transform2D inverse() const;
72
73 void affine_invert();
74 Transform2D affine_inverse() const;
75
76 void set_rotation(real_t p_rot);
77 real_t get_rotation() const;
78 real_t get_skew() const;
79 void set_skew(real_t p_angle);
80 _FORCE_INLINE_ void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
81 _FORCE_INLINE_ void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew);
82 void rotate(real_t p_angle);
83
84 void scale(const Size2 &p_scale);
85 void scale_basis(const Size2 &p_scale);
86 void translate_local(real_t p_tx, real_t p_ty);
87 void translate_local(const Vector2 &p_translation);
88
89 real_t determinant() const;
90
91 Size2 get_scale() const;
92 void set_scale(const Size2 &p_scale);
93
94 _FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; }
95 _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; }
96
97 Transform2D scaled(const Size2 &p_scale) const;
98 Transform2D scaled_local(const Size2 &p_scale) const;
99 Transform2D translated(const Vector2 &p_offset) const;
100 Transform2D translated_local(const Vector2 &p_offset) const;
101 Transform2D rotated(real_t p_angle) const;
102 Transform2D rotated_local(real_t p_angle) const;
103
104 Transform2D untranslated() const;
105
106 void orthonormalize();
107 Transform2D orthonormalized() const;
108 bool is_conformal() const;
109 bool is_equal_approx(const Transform2D &p_transform) const;
110 bool is_same(const Transform2D &p_transform) const;
111 bool is_finite() const;
112
113 Transform2D looking_at(const Vector2 &p_target) const;
114
115 constexpr bool operator==(const Transform2D &p_transform) const;
116 constexpr bool operator!=(const Transform2D &p_transform) const;
117
118 void operator*=(const Transform2D &p_transform);
119 Transform2D operator*(const Transform2D &p_transform) const;
120 constexpr void operator*=(real_t p_val);
121 constexpr Transform2D operator*(real_t p_val) const;
122 constexpr void operator/=(real_t p_val);
123 constexpr Transform2D operator/(real_t p_val) const;
124
125 Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
126
127 _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
128 _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
129 _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
130 _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
131 _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const;
132 _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const;
133 _FORCE_INLINE_ Vector<Vector2> xform(const Vector<Vector2> &p_array) const;
134 _FORCE_INLINE_ Vector<Vector2> xform_inv(const Vector<Vector2> &p_array) const;
135
136 explicit operator String() const;
137
138 constexpr Transform2D(real_t p_xx, real_t p_xy, real_t p_yx, real_t p_yy, real_t p_ox, real_t p_oy) :
139 columns{
140 { p_xx, p_xy },
141 { p_yx, p_yy },
142 { p_ox, p_oy },
143 } {}
144
145 constexpr Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) :
146 columns{ p_x, p_y, p_origin } {}
147
148 Transform2D(real_t p_rot, const Vector2 &p_pos);
149
150 Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos);
151
152 Transform2D() = default;
153 };
154
155 inline constexpr Transform2D Transform2D::FLIP_X = { { -1, 0 }, { 0, 1 }, { 0, 0 } };
156 inline constexpr Transform2D Transform2D::FLIP_Y = { { 1, 0 }, { 0, -1 }, { 0, 0 } };
157
158 constexpr bool Transform2D::operator==(const Transform2D &p_transform) const {
159 for (int i = 0; i < 3; i++) {
160 if (columns[i] != p_transform.columns[i]) {
161 return false;
162 }
163 }
164
165 return true;
166 }
167
168 constexpr bool Transform2D::operator!=(const Transform2D &p_transform) const {
169 for (int i = 0; i < 3; i++) {
170 if (columns[i] != p_transform.columns[i]) {
171 return true;
172 }
173 }
174
175 return false;
176 }
177
178 constexpr void Transform2D::operator*=(real_t p_val) {
179 columns[0] *= p_val;
180 columns[1] *= p_val;
181 columns[2] *= p_val;
182 }
183
184 constexpr Transform2D Transform2D::operator*(real_t p_val) const {
185 Transform2D ret(*this);
186 ret *= p_val;
187 return ret;
188 }
189
190 constexpr void Transform2D::operator/=(real_t p_val) {
191 columns[0] /= p_val;
192 columns[1] /= p_val;
193 columns[2] /= p_val;
194 }
195
196 constexpr Transform2D Transform2D::operator/(real_t p_val) const {
197 Transform2D ret(*this);
198 ret /= p_val;
199 return ret;
200 }
201
202 Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
203 return Vector2(
204 tdotx(p_vec),
205 tdoty(p_vec));
206 }
207
208 Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
209 return Vector2(
210 columns[0].dot(p_vec),
211 columns[1].dot(p_vec));
212 }
213
214 Vector2 Transform2D::xform(const Vector2 &p_vec) const {
215 return Vector2(
216 tdotx(p_vec),
217 tdoty(p_vec)) +
218 columns[2];
219 }
220
221 Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
222 Vector2 v = p_vec - columns[2];
223
224 return Vector2(
225 columns[0].dot(v),
226 columns[1].dot(v));
227 }
228
229 Rect2 Transform2D::xform(const Rect2 &p_rect) const {
230 Vector2 x = columns[0] * p_rect.size.x;
231 Vector2 y = columns[1] * p_rect.size.y;
232 Vector2 pos = xform(p_rect.position);
233
234 Rect2 new_rect;
235 new_rect.position = pos;
236 new_rect.expand_to(pos + x);
237 new_rect.expand_to(pos + y);
238 new_rect.expand_to(pos + x + y);
239 return new_rect;
240 }
241
242 void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
243 columns[0][0] = Math::cos(p_rot) * p_scale.x;
244 columns[1][1] = Math::cos(p_rot) * p_scale.y;
245 columns[1][0] = -Math::sin(p_rot) * p_scale.y;
246 columns[0][1] = Math::sin(p_rot) * p_scale.x;
247 }
248
249 void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, real_t p_skew) {
250 columns[0][0] = Math::cos(p_rot) * p_scale.x;
251 columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
252 columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
253 columns[0][1] = Math::sin(p_rot) * p_scale.x;
254 }
255
256 Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
257 Vector2 ends[4] = {
258 xform_inv(p_rect.position),
259 xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
260 xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
261 xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
262 };
263
264 Rect2 new_rect;
265 new_rect.position = ends[0];
266 new_rect.expand_to(ends[1]);
267 new_rect.expand_to(ends[2]);
268 new_rect.expand_to(ends[3]);
269
270 return new_rect;
271 }
272
273 Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const {
274 Vector<Vector2> array;
275 array.resize(p_array.size());
276
277 const Vector2 *r = p_array.ptr();
278 Vector2 *w = array.ptrw();
279
280 for (int i = 0; i < p_array.size(); ++i) {
281 w[i] = xform(r[i]);
282 }
283 return array;
284 }
285
286 Vector<Vector2> Transform2D::xform_inv(const Vector<Vector2> &p_array) const {
287 Vector<Vector2> array;
288 array.resize(p_array.size());
289
290 const Vector2 *r = p_array.ptr();
291 Vector2 *w = array.ptrw();
292
293 for (int i = 0; i < p_array.size(); ++i) {
294 w[i] = xform_inv(r[i]);
295 }
296 return array;
297 }