Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_basis.cpp
23450 views
1
/**************************************************************************/
2
/* test_basis.cpp */
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
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_basis)
34
35
#include "core/math/basis.h"
36
37
namespace TestBasis {
38
39
Vector3 deg_to_rad(const Vector3 &p_rotation) {
40
return p_rotation / 180.0 * Math::PI;
41
}
42
43
Vector3 rad2deg(const Vector3 &p_rotation) {
44
return p_rotation / Math::PI * 180.0;
45
}
46
47
String get_rot_order_name(EulerOrder ro) {
48
switch (ro) {
49
case EulerOrder::XYZ:
50
return "XYZ";
51
case EulerOrder::XZY:
52
return "XZY";
53
case EulerOrder::YZX:
54
return "YZX";
55
case EulerOrder::YXZ:
56
return "YXZ";
57
case EulerOrder::ZXY:
58
return "ZXY";
59
case EulerOrder::ZYX:
60
return "ZYX";
61
default:
62
return "[Not supported]";
63
}
64
}
65
66
void test_rotation(Vector3 deg_original_euler, EulerOrder rot_order) {
67
// This test:
68
// 1. Converts the rotation vector from deg to rad.
69
// 2. Converts euler to basis.
70
// 3. Converts the above basis back into euler.
71
// 4. Converts the above euler into basis again.
72
// 5. Compares the basis obtained in step 2 with the basis of step 4
73
//
74
// The conversion "basis to euler", done in the step 3, may be different from
75
// the original euler, even if the final rotation are the same.
76
// This happens because there are more ways to represents the same rotation,
77
// both valid, using eulers.
78
// For this reason is necessary to convert that euler back to basis and finally
79
// compares it.
80
//
81
// In this way we can assert that both functions: basis to euler / euler to basis
82
// are correct.
83
84
// Euler to rotation
85
const Vector3 original_euler = deg_to_rad(deg_original_euler);
86
const Basis to_rotation = Basis::from_euler(original_euler, rot_order);
87
88
// Euler from rotation
89
const Vector3 euler_from_rotation = to_rotation.get_euler(rot_order);
90
const Basis rotation_from_computed_euler = Basis::from_euler(euler_from_rotation, rot_order);
91
92
Basis res = to_rotation.inverse() * rotation_from_computed_euler;
93
94
CHECK_MESSAGE((res.get_column(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.001, vformat("Fail due to X %s\n", String(res.get_column(0))));
95
CHECK_MESSAGE((res.get_column(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.001, vformat("Fail due to Y %s\n", String(res.get_column(1))));
96
CHECK_MESSAGE((res.get_column(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.001, vformat("Fail due to Z %s\n", String(res.get_column(2))));
97
98
// Double check `to_rotation` decomposing with XYZ rotation order.
99
const Vector3 euler_xyz_from_rotation = to_rotation.get_euler(EulerOrder::XYZ);
100
Basis rotation_from_xyz_computed_euler = Basis::from_euler(euler_xyz_from_rotation, EulerOrder::XYZ);
101
102
res = to_rotation.inverse() * rotation_from_xyz_computed_euler;
103
104
CHECK_MESSAGE((res.get_column(0) - Vector3(1.0, 0.0, 0.0)).length() <= 0.001, vformat("Double check with XYZ rot order failed, due to X %s\n", String(res.get_column(0))));
105
CHECK_MESSAGE((res.get_column(1) - Vector3(0.0, 1.0, 0.0)).length() <= 0.001, vformat("Double check with XYZ rot order failed, due to Y %s\n", String(res.get_column(1))));
106
CHECK_MESSAGE((res.get_column(2) - Vector3(0.0, 0.0, 1.0)).length() <= 0.001, vformat("Double check with XYZ rot order failed, due to Z %s\n", String(res.get_column(2))));
107
108
INFO(vformat("Rotation order: %s\n.", get_rot_order_name(rot_order)));
109
INFO(vformat("Original Rotation: %s\n", String(deg_original_euler)));
110
INFO(vformat("Quaternion to rotation order: %s\n", String(rad2deg(euler_from_rotation))));
111
}
112
113
TEST_CASE("[Basis] Euler conversions") {
114
Vector<EulerOrder> euler_order_to_test;
115
euler_order_to_test.push_back(EulerOrder::XYZ);
116
euler_order_to_test.push_back(EulerOrder::XZY);
117
euler_order_to_test.push_back(EulerOrder::YZX);
118
euler_order_to_test.push_back(EulerOrder::YXZ);
119
euler_order_to_test.push_back(EulerOrder::ZXY);
120
euler_order_to_test.push_back(EulerOrder::ZYX);
121
122
Vector<Vector3> vectors_to_test;
123
124
// Test the special cases.
125
vectors_to_test.push_back(Vector3(0.0, 0.0, 0.0));
126
vectors_to_test.push_back(Vector3(0.5, 0.5, 0.5));
127
vectors_to_test.push_back(Vector3(-0.5, -0.5, -0.5));
128
vectors_to_test.push_back(Vector3(40.0, 40.0, 40.0));
129
vectors_to_test.push_back(Vector3(-40.0, -40.0, -40.0));
130
vectors_to_test.push_back(Vector3(0.0, 0.0, -90.0));
131
vectors_to_test.push_back(Vector3(0.0, -90.0, 0.0));
132
vectors_to_test.push_back(Vector3(-90.0, 0.0, 0.0));
133
vectors_to_test.push_back(Vector3(0.0, 0.0, 90.0));
134
vectors_to_test.push_back(Vector3(0.0, 90.0, 0.0));
135
vectors_to_test.push_back(Vector3(90.0, 0.0, 0.0));
136
vectors_to_test.push_back(Vector3(0.0, 0.0, -30.0));
137
vectors_to_test.push_back(Vector3(0.0, -30.0, 0.0));
138
vectors_to_test.push_back(Vector3(-30.0, 0.0, 0.0));
139
vectors_to_test.push_back(Vector3(0.0, 0.0, 30.0));
140
vectors_to_test.push_back(Vector3(0.0, 30.0, 0.0));
141
vectors_to_test.push_back(Vector3(30.0, 0.0, 0.0));
142
vectors_to_test.push_back(Vector3(0.5, 50.0, 20.0));
143
vectors_to_test.push_back(Vector3(-0.5, -50.0, -20.0));
144
vectors_to_test.push_back(Vector3(0.5, 0.0, 90.0));
145
vectors_to_test.push_back(Vector3(0.5, 0.0, -90.0));
146
vectors_to_test.push_back(Vector3(360.0, 360.0, 360.0));
147
vectors_to_test.push_back(Vector3(-360.0, -360.0, -360.0));
148
vectors_to_test.push_back(Vector3(-90.0, 60.0, -90.0));
149
vectors_to_test.push_back(Vector3(90.0, 60.0, -90.0));
150
vectors_to_test.push_back(Vector3(90.0, -60.0, -90.0));
151
vectors_to_test.push_back(Vector3(-90.0, -60.0, -90.0));
152
vectors_to_test.push_back(Vector3(-90.0, 60.0, 90.0));
153
vectors_to_test.push_back(Vector3(90.0, 60.0, 90.0));
154
vectors_to_test.push_back(Vector3(90.0, -60.0, 90.0));
155
vectors_to_test.push_back(Vector3(-90.0, -60.0, 90.0));
156
vectors_to_test.push_back(Vector3(60.0, 90.0, -40.0));
157
vectors_to_test.push_back(Vector3(60.0, -90.0, -40.0));
158
vectors_to_test.push_back(Vector3(-60.0, -90.0, -40.0));
159
vectors_to_test.push_back(Vector3(-60.0, 90.0, 40.0));
160
vectors_to_test.push_back(Vector3(60.0, 90.0, 40.0));
161
vectors_to_test.push_back(Vector3(60.0, -90.0, 40.0));
162
vectors_to_test.push_back(Vector3(-60.0, -90.0, 40.0));
163
vectors_to_test.push_back(Vector3(-90.0, 90.0, -90.0));
164
vectors_to_test.push_back(Vector3(90.0, 90.0, -90.0));
165
vectors_to_test.push_back(Vector3(90.0, -90.0, -90.0));
166
vectors_to_test.push_back(Vector3(-90.0, -90.0, -90.0));
167
vectors_to_test.push_back(Vector3(-90.0, 90.0, 90.0));
168
vectors_to_test.push_back(Vector3(90.0, 90.0, 90.0));
169
vectors_to_test.push_back(Vector3(90.0, -90.0, 90.0));
170
vectors_to_test.push_back(Vector3(20.0, 150.0, 30.0));
171
vectors_to_test.push_back(Vector3(20.0, -150.0, 30.0));
172
vectors_to_test.push_back(Vector3(-120.0, -150.0, 30.0));
173
vectors_to_test.push_back(Vector3(-120.0, -150.0, -130.0));
174
vectors_to_test.push_back(Vector3(120.0, -150.0, -130.0));
175
vectors_to_test.push_back(Vector3(120.0, 150.0, -130.0));
176
vectors_to_test.push_back(Vector3(120.0, 150.0, 130.0));
177
vectors_to_test.push_back(Vector3(89.9, 0.0, 0.0));
178
vectors_to_test.push_back(Vector3(-89.9, 0.0, 0.0));
179
vectors_to_test.push_back(Vector3(0.0, 89.9, 0.0));
180
vectors_to_test.push_back(Vector3(0.0, -89.9, 0.0));
181
vectors_to_test.push_back(Vector3(0.0, 0.0, 89.9));
182
vectors_to_test.push_back(Vector3(0.0, 0.0, -89.9));
183
184
for (int h = 0; h < euler_order_to_test.size(); h += 1) {
185
for (int i = 0; i < vectors_to_test.size(); i += 1) {
186
test_rotation(vectors_to_test[i], euler_order_to_test[h]);
187
}
188
}
189
}
190
191
TEST_CASE("[Basis] Set axis angle") {
192
Vector3 axis;
193
real_t angle;
194
real_t pi = (real_t)Math::PI;
195
196
// Testing the singularity when the angle is 0°.
197
Basis identity(1, 0, 0, 0, 1, 0, 0, 0, 1);
198
identity.get_axis_angle(axis, angle);
199
CHECK(angle == 0);
200
201
// Testing the singularity when the angle is 180°.
202
Basis singularityPi(-1, 0, 0, 0, 1, 0, 0, 0, -1);
203
singularityPi.get_axis_angle(axis, angle);
204
CHECK(angle == doctest::Approx(pi));
205
206
// Testing reversing the an axis (of an 30° angle).
207
float cos30deg = Math::cos(Math::deg_to_rad((real_t)30.0));
208
Basis z_positive(cos30deg, -0.5, 0, 0.5, cos30deg, 0, 0, 0, 1);
209
Basis z_negative(cos30deg, 0.5, 0, -0.5, cos30deg, 0, 0, 0, 1);
210
211
z_positive.get_axis_angle(axis, angle);
212
CHECK(angle == doctest::Approx(Math::deg_to_rad((real_t)30.0)));
213
CHECK(axis == Vector3(0, 0, 1));
214
215
z_negative.get_axis_angle(axis, angle);
216
CHECK(angle == doctest::Approx(Math::deg_to_rad((real_t)30.0)));
217
CHECK(axis == Vector3(0, 0, -1));
218
219
// Testing a rotation of 90° on x-y-z.
220
Basis x90deg(1, 0, 0, 0, 0, -1, 0, 1, 0);
221
x90deg.get_axis_angle(axis, angle);
222
CHECK(angle == doctest::Approx(pi / (real_t)2));
223
CHECK(axis == Vector3(1, 0, 0));
224
225
Basis y90deg(0, 0, 1, 0, 1, 0, -1, 0, 0);
226
y90deg.get_axis_angle(axis, angle);
227
CHECK(axis == Vector3(0, 1, 0));
228
229
Basis z90deg(0, -1, 0, 1, 0, 0, 0, 0, 1);
230
z90deg.get_axis_angle(axis, angle);
231
CHECK(axis == Vector3(0, 0, 1));
232
233
// Regression test: checks that the method returns a small angle (not 0).
234
Basis tiny(1, 0, 0, 0, 0.9999995, -0.001, 0, 001, 0.9999995); // The min angle possible with float is 0.001rad.
235
tiny.get_axis_angle(axis, angle);
236
CHECK(angle == doctest::Approx(0.001).epsilon(0.0001));
237
238
// Regression test: checks that the method returns an angle which is a number (not NaN)
239
Basis bugNan(1.00000024, 0, 0.000100001693, 0, 1, 0, -0.000100009143, 0, 1.00000024);
240
bugNan.get_axis_angle(axis, angle);
241
CHECK(!Math::is_nan(angle));
242
}
243
244
TEST_CASE("[Basis] Finite number checks") {
245
constexpr Vector3 x(0, 1, 2);
246
constexpr Vector3 infinite(Math::NaN, Math::NaN, Math::NaN);
247
248
CHECK_MESSAGE(
249
Basis(x, x, x).is_finite(),
250
"Basis with all components finite should be finite");
251
252
CHECK_FALSE_MESSAGE(
253
Basis(infinite, x, x).is_finite(),
254
"Basis with one component infinite should not be finite.");
255
CHECK_FALSE_MESSAGE(
256
Basis(x, infinite, x).is_finite(),
257
"Basis with one component infinite should not be finite.");
258
CHECK_FALSE_MESSAGE(
259
Basis(x, x, infinite).is_finite(),
260
"Basis with one component infinite should not be finite.");
261
262
CHECK_FALSE_MESSAGE(
263
Basis(infinite, infinite, x).is_finite(),
264
"Basis with two components infinite should not be finite.");
265
CHECK_FALSE_MESSAGE(
266
Basis(infinite, x, infinite).is_finite(),
267
"Basis with two components infinite should not be finite.");
268
CHECK_FALSE_MESSAGE(
269
Basis(x, infinite, infinite).is_finite(),
270
"Basis with two components infinite should not be finite.");
271
272
CHECK_FALSE_MESSAGE(
273
Basis(infinite, infinite, infinite).is_finite(),
274
"Basis with three components infinite should not be finite.");
275
}
276
277
TEST_CASE("[Basis] Is conformal checks") {
278
CHECK_MESSAGE(
279
Basis().is_conformal(),
280
"Identity Basis should be conformal.");
281
282
CHECK_MESSAGE(
283
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_conformal(),
284
"Basis with only rotation should be conformal.");
285
286
CHECK_MESSAGE(
287
Basis::from_scale(Vector3(-1, -1, -1)).is_conformal(),
288
"Basis with only a flip should be conformal.");
289
290
CHECK_MESSAGE(
291
Basis::from_scale(Vector3(1.2, 1.2, 1.2)).is_conformal(),
292
"Basis with only uniform scale should be conformal.");
293
294
CHECK_MESSAGE(
295
Basis(Vector3(3, 4, 0), Vector3(4, -3, 0.0), Vector3(0, 0, 5)).is_conformal(),
296
"Basis with a flip, rotation, and uniform scale should be conformal.");
297
298
CHECK_FALSE_MESSAGE(
299
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_conformal(),
300
"Basis with non-uniform scale should not be conformal.");
301
302
CHECK_FALSE_MESSAGE(
303
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_conformal(),
304
"Basis with the X axis skewed 45 degrees should not be conformal.");
305
306
CHECK_MESSAGE(
307
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_conformal(),
308
"Edge case: Basis with all zeroes should return true for is_conformal (because a 0 scale is uniform).");
309
}
310
311
TEST_CASE("[Basis] Is orthogonal checks") {
312
CHECK_MESSAGE(
313
Basis().is_orthogonal(),
314
"Identity Basis should be orthogonal.");
315
316
CHECK_MESSAGE(
317
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_orthogonal(),
318
"Basis with only rotation should be orthogonal.");
319
320
CHECK_MESSAGE(
321
Basis::from_scale(Vector3(-1, -1, -1)).is_orthogonal(),
322
"Basis with only a flip should be orthogonal.");
323
324
CHECK_MESSAGE(
325
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_orthogonal(),
326
"Basis with only scale should be orthogonal.");
327
328
CHECK_MESSAGE(
329
Basis(Vector3(3, 4, 0), Vector3(4, -3, 0), Vector3(0, 0, 5)).is_orthogonal(),
330
"Basis with a flip, rotation, and uniform scale should be orthogonal.");
331
332
CHECK_FALSE_MESSAGE(
333
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_orthogonal(),
334
"Basis with the X axis skewed 45 degrees should not be orthogonal.");
335
336
CHECK_MESSAGE(
337
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_orthogonal(),
338
"Edge case: Basis with all zeroes should return true for is_orthogonal, since zero vectors are orthogonal to all vectors.");
339
}
340
341
TEST_CASE("[Basis] Is orthonormal checks") {
342
CHECK_MESSAGE(
343
Basis().is_orthonormal(),
344
"Identity Basis should be orthonormal.");
345
346
CHECK_MESSAGE(
347
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_orthonormal(),
348
"Basis with only rotation should be orthonormal.");
349
350
CHECK_MESSAGE(
351
Basis::from_scale(Vector3(-1, -1, -1)).is_orthonormal(),
352
"Basis with only a flip should be orthonormal.");
353
354
CHECK_FALSE_MESSAGE(
355
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_orthonormal(),
356
"Basis with only scale should not be orthonormal.");
357
358
CHECK_FALSE_MESSAGE(
359
Basis(Vector3(3, 4, 0), Vector3(4, -3, 0), Vector3(0, 0, 5)).is_orthonormal(),
360
"Basis with a flip, rotation, and uniform scale should not be orthonormal.");
361
362
CHECK_FALSE_MESSAGE(
363
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_orthonormal(),
364
"Basis with the X axis skewed 45 degrees should not be orthonormal.");
365
366
CHECK_FALSE_MESSAGE(
367
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_orthonormal(),
368
"Edge case: Basis with all zeroes should return false for is_orthonormal, since the vectors do not have a length of 1.");
369
}
370
371
TEST_CASE("[Basis] Is rotation checks") {
372
CHECK_MESSAGE(
373
Basis().is_rotation(),
374
"Identity Basis should be a rotation (a rotation of zero).");
375
376
CHECK_MESSAGE(
377
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_rotation(),
378
"Basis with only rotation should be a rotation.");
379
380
CHECK_FALSE_MESSAGE(
381
Basis::from_scale(Vector3(-1, -1, -1)).is_rotation(),
382
"Basis with only a flip should not be a rotation.");
383
384
CHECK_FALSE_MESSAGE(
385
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_rotation(),
386
"Basis with only scale should not be a rotation.");
387
388
CHECK_FALSE_MESSAGE(
389
Basis(Vector3(2, 0, 0), Vector3(0, 0.5, 0), Vector3(0, 0, 1)).is_rotation(),
390
"Basis with a squeeze should not be a rotation.");
391
392
CHECK_FALSE_MESSAGE(
393
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_rotation(),
394
"Basis with the X axis skewed 45 degrees should not be a rotation.");
395
396
CHECK_FALSE_MESSAGE(
397
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_rotation(),
398
"Edge case: Basis with all zeroes should return false for is_rotation, because it is not just a rotation (has a scale of 0).");
399
}
400
401
} // namespace TestBasis
402
403