Debian Codegraph @

tests/core/math/test_transform_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_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/transform_2d.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestTransform2D {
38
39 Transform2D create_dummy_transform() {
40 return Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
41 }
42
43 Transform2D identity() {
44 return Transform2D();
45 }
46
47 TEST_CASE("[Transform2D] Default constructor") {
48 Transform2D default_constructor = Transform2D();
49 CHECK(default_constructor == Transform2D(Vector2(1, 0), Vector2(0, 1), Vector2(0, 0)));
50 }
51
52 TEST_CASE("[Transform2D] Copy constructor") {
53 Transform2D T = create_dummy_transform();
54 Transform2D copy_constructor = Transform2D(T);
55 CHECK(T == copy_constructor);
56 }
57
58 TEST_CASE("[Transform2D] Constructor from angle and position") {
59 constexpr float ROTATION = Math::PI / 4;
60 constexpr Vector2 TRANSLATION = Vector2(20, -20);
61
62 const Transform2D test = Transform2D(ROTATION, TRANSLATION);
63 const Transform2D expected = Transform2D().rotated(ROTATION).translated(TRANSLATION);
64 CHECK(test == expected);
65 }
66
67 TEST_CASE("[Transform2D] Constructor from angle, scale, skew and position") {
68 constexpr float ROTATION = Math::PI / 2;
69 constexpr Vector2 SCALE = Vector2(2, 0.5);
70 constexpr float SKEW = Math::PI / 4;
71 constexpr Vector2 TRANSLATION = Vector2(30, 0);
72
73 const Transform2D test = Transform2D(ROTATION, SCALE, SKEW, TRANSLATION);
74 Transform2D expected = Transform2D().scaled(SCALE).rotated(ROTATION).translated(TRANSLATION);
75 expected.set_skew(SKEW);
76
77 CHECK(test.is_equal_approx(expected));
78 }
79
80 TEST_CASE("[Transform2D] Constructor from raw values") {
81 constexpr Transform2D test = Transform2D(1, 2, 3, 4, 5, 6);
82 constexpr Transform2D expected = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
83 static_assert(test == expected);
84 }
85
86 TEST_CASE("[Transform2D] xform") {
87 constexpr Vector2 v = Vector2(2, 3);
88 constexpr Transform2D T = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
89 constexpr Vector2 expected = Vector2(1 * 2 + 3 * 3 + 5 * 1, 2 * 2 + 4 * 3 + 6 * 1);
90 CHECK(T.xform(v) == expected);
91 }
92
93 TEST_CASE("[Transform2D] Basis xform") {
94 constexpr Vector2 v = Vector2(2, 2);
95 constexpr Transform2D T1 = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(0, 0));
96
97 // Both versions should be the same when the origin is (0,0).
98 CHECK(T1.basis_xform(v) == T1.xform(v));
99
100 constexpr Transform2D T2 = Transform2D(Vector2(1, 2), Vector2(3, 4), Vector2(5, 6));
101
102 // Each version should be different when the origin is not (0,0).
103 CHECK_FALSE(T2.basis_xform(v) == T2.xform(v));
104 }
105
106 TEST_CASE("[Transform2D] Affine inverse") {
107 const Transform2D orig = create_dummy_transform();
108 const Transform2D affine_inverted = orig.affine_inverse();
109 const Transform2D affine_inverted_again = affine_inverted.affine_inverse();
110 CHECK(affine_inverted_again == orig);
111 }
112
113 TEST_CASE("[Transform2D] Orthonormalized") {
114 const Transform2D T = create_dummy_transform();
115 const Transform2D orthonormalized_T = T.orthonormalized();
116
117 // Check each basis has length 1.
118 CHECK(Math::is_equal_approx(orthonormalized_T[0].length_squared(), 1));
119 CHECK(Math::is_equal_approx(orthonormalized_T[1].length_squared(), 1));
120
121 const Vector2 vx = Vector2(orthonormalized_T[0].x, orthonormalized_T[1].x);
122 const Vector2 vy = Vector2(orthonormalized_T[0].y, orthonormalized_T[1].y);
123
124 // Check the basis are orthogonal.
125 CHECK(Math::is_equal_approx(orthonormalized_T.tdotx(vx), 1));
126 CHECK(Math::is_equal_approx(orthonormalized_T.tdotx(vy), 0));
127 CHECK(Math::is_equal_approx(orthonormalized_T.tdoty(vx), 0));
128 CHECK(Math::is_equal_approx(orthonormalized_T.tdoty(vy), 1));
129 }
130
131 TEST_CASE("[Transform2D] translation") {
132 constexpr Vector2 offset = Vector2(1, 2);
133
134 // Both versions should give the same result applied to identity.
135 CHECK(identity().translated(offset) == identity().translated_local(offset));
136
137 // Check both versions against left and right multiplications.
138 const Transform2D orig = create_dummy_transform();
139 const Transform2D T = identity().translated(offset);
140 CHECK(orig.translated(offset) == T * orig);
141 CHECK(orig.translated_local(offset) == orig * T);
142 }
143
144 TEST_CASE("[Transform2D] scaling") {
145 constexpr Vector2 scaling = Vector2(1, 2);
146
147 // Both versions should give the same result applied to identity.
148 CHECK(identity().scaled(scaling) == identity().scaled_local(scaling));
149
150 // Check both versions against left and right multiplications.
151 const Transform2D orig = create_dummy_transform();
152 const Transform2D S = identity().scaled(scaling);
153 CHECK(orig.scaled(scaling) == S * orig);
154 CHECK(orig.scaled_local(scaling) == orig * S);
155 }
156
157 TEST_CASE("[Transform2D] rotation") {
158 constexpr real_t phi = 1.0;
159
160 // Both versions should give the same result applied to identity.
161 CHECK(identity().rotated(phi) == identity().rotated_local(phi));
162
163 // Check both versions against left and right multiplications.
164 const Transform2D orig = create_dummy_transform();
165 const Transform2D R = identity().rotated(phi);
166 CHECK(orig.rotated(phi) == R * orig);
167 CHECK(orig.rotated_local(phi) == orig * R);
168 }
169
170 TEST_CASE("[Transform2D] Interpolation") {
171 const Transform2D rotate_scale_skew_pos = Transform2D(Math::deg_to_rad(170.0), Vector2(3.6, 8.0), Math::deg_to_rad(20.0), Vector2(2.4, 6.8));
172 const Transform2D rotate_scale_skew_pos_halfway = Transform2D(Math::deg_to_rad(85.0), Vector2(2.3, 4.5), Math::deg_to_rad(10.0), Vector2(1.2, 3.4));
173 Transform2D interpolated = Transform2D().interpolate_with(rotate_scale_skew_pos, 0.5);
174 CHECK(interpolated.get_origin().is_equal_approx(rotate_scale_skew_pos_halfway.get_origin()));
175 CHECK(interpolated.get_rotation() == doctest::Approx(rotate_scale_skew_pos_halfway.get_rotation()));
176 CHECK(interpolated.get_scale().is_equal_approx(rotate_scale_skew_pos_halfway.get_scale()));
177 CHECK(interpolated.get_skew() == doctest::Approx(rotate_scale_skew_pos_halfway.get_skew()));
178 CHECK(interpolated.is_equal_approx(rotate_scale_skew_pos_halfway));
179 interpolated = rotate_scale_skew_pos.interpolate_with(Transform2D(), 0.5);
180 CHECK(interpolated.is_equal_approx(rotate_scale_skew_pos_halfway));
181 }
182
183 TEST_CASE("[Transform2D] Finite number checks") {
184 constexpr Vector2 x = Vector2(0, 1);
185 constexpr Vector2 infinite = Vector2(Math::NaN, Math::NaN);
186
187 CHECK_MESSAGE(
188 Transform2D(x, x, x).is_finite(),
189 "Transform2D with all components finite should be finite");
190
191 CHECK_FALSE_MESSAGE(
192 Transform2D(infinite, x, x).is_finite(),
193 "Transform2D with one component infinite should not be finite.");
194 CHECK_FALSE_MESSAGE(
195 Transform2D(x, infinite, x).is_finite(),
196 "Transform2D with one component infinite should not be finite.");
197 CHECK_FALSE_MESSAGE(
198 Transform2D(x, x, infinite).is_finite(),
199 "Transform2D with one component infinite should not be finite.");
200
201 CHECK_FALSE_MESSAGE(
202 Transform2D(infinite, infinite, x).is_finite(),
203 "Transform2D with two components infinite should not be finite.");
204 CHECK_FALSE_MESSAGE(
205 Transform2D(infinite, x, infinite).is_finite(),
206 "Transform2D with two components infinite should not be finite.");
207 CHECK_FALSE_MESSAGE(
208 Transform2D(x, infinite, infinite).is_finite(),
209 "Transform2D with two components infinite should not be finite.");
210
211 CHECK_FALSE_MESSAGE(
212 Transform2D(infinite, infinite, infinite).is_finite(),
213 "Transform2D with three components infinite should not be finite.");
214 }
215
216 TEST_CASE("[Transform2D] Is conformal checks") {
217 CHECK_MESSAGE(
218 Transform2D().is_conformal(),
219 "Identity Transform2D should be conformal.");
220
221 CHECK_MESSAGE(
222 Transform2D(1.2, Vector2()).is_conformal(),
223 "Transform2D with only rotation should be conformal.");
224
225 CHECK_MESSAGE(
226 Transform2D(Vector2(1, 0), Vector2(0, -1), Vector2()).is_conformal(),
227 "Transform2D with only a flip should be conformal.");
228
229 CHECK_MESSAGE(
230 Transform2D(Vector2(1.2, 0), Vector2(0, 1.2), Vector2()).is_conformal(),
231 "Transform2D with only uniform scale should be conformal.");
232
233 CHECK_MESSAGE(
234 Transform2D(Vector2(1.2, 3.4), Vector2(3.4, -1.2), Vector2()).is_conformal(),
235 "Transform2D with a flip, rotation, and uniform scale should be conformal.");
236
237 CHECK_FALSE_MESSAGE(
238 Transform2D(Vector2(1.2, 0), Vector2(0, 3.4), Vector2()).is_conformal(),
239 "Transform2D with non-uniform scale should not be conformal.");
240
241 CHECK_FALSE_MESSAGE(
242 Transform2D(Vector2(Math::SQRT12, Math::SQRT12), Vector2(0, 1), Vector2()).is_conformal(),
243 "Transform2D with the X axis skewed 45 degrees should not be conformal.");
244 }
245
246 } // namespace TestTransform2D