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