Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_basis.h
20952 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("[Basis] Set axis angle") {
193
Vector3 axis;
194
real_t angle;
195
real_t pi = (real_t)Math::PI;
196
197
// Testing the singularity when the angle is 0°.
198
Basis identity(1, 0, 0, 0, 1, 0, 0, 0, 1);
199
identity.get_axis_angle(axis, angle);
200
CHECK(angle == 0);
201
202
// Testing the singularity when the angle is 180°.
203
Basis singularityPi(-1, 0, 0, 0, 1, 0, 0, 0, -1);
204
singularityPi.get_axis_angle(axis, angle);
205
CHECK(angle == doctest::Approx(pi));
206
207
// Testing reversing the an axis (of an 30° angle).
208
float cos30deg = Math::cos(Math::deg_to_rad((real_t)30.0));
209
Basis z_positive(cos30deg, -0.5, 0, 0.5, cos30deg, 0, 0, 0, 1);
210
Basis z_negative(cos30deg, 0.5, 0, -0.5, cos30deg, 0, 0, 0, 1);
211
212
z_positive.get_axis_angle(axis, angle);
213
CHECK(angle == doctest::Approx(Math::deg_to_rad((real_t)30.0)));
214
CHECK(axis == Vector3(0, 0, 1));
215
216
z_negative.get_axis_angle(axis, angle);
217
CHECK(angle == doctest::Approx(Math::deg_to_rad((real_t)30.0)));
218
CHECK(axis == Vector3(0, 0, -1));
219
220
// Testing a rotation of 90° on x-y-z.
221
Basis x90deg(1, 0, 0, 0, 0, -1, 0, 1, 0);
222
x90deg.get_axis_angle(axis, angle);
223
CHECK(angle == doctest::Approx(pi / (real_t)2));
224
CHECK(axis == Vector3(1, 0, 0));
225
226
Basis y90deg(0, 0, 1, 0, 1, 0, -1, 0, 0);
227
y90deg.get_axis_angle(axis, angle);
228
CHECK(axis == Vector3(0, 1, 0));
229
230
Basis z90deg(0, -1, 0, 1, 0, 0, 0, 0, 1);
231
z90deg.get_axis_angle(axis, angle);
232
CHECK(axis == Vector3(0, 0, 1));
233
234
// Regression test: checks that the method returns a small angle (not 0).
235
Basis tiny(1, 0, 0, 0, 0.9999995, -0.001, 0, 001, 0.9999995); // The min angle possible with float is 0.001rad.
236
tiny.get_axis_angle(axis, angle);
237
CHECK(angle == doctest::Approx(0.001).epsilon(0.0001));
238
239
// Regression test: checks that the method returns an angle which is a number (not NaN)
240
Basis bugNan(1.00000024, 0, 0.000100001693, 0, 1, 0, -0.000100009143, 0, 1.00000024);
241
bugNan.get_axis_angle(axis, angle);
242
CHECK(!Math::is_nan(angle));
243
}
244
245
TEST_CASE("[Basis] Finite number checks") {
246
constexpr Vector3 x(0, 1, 2);
247
constexpr Vector3 infinite(Math::NaN, Math::NaN, Math::NaN);
248
249
CHECK_MESSAGE(
250
Basis(x, x, x).is_finite(),
251
"Basis with all components finite should be finite");
252
253
CHECK_FALSE_MESSAGE(
254
Basis(infinite, x, x).is_finite(),
255
"Basis with one component infinite should not be finite.");
256
CHECK_FALSE_MESSAGE(
257
Basis(x, infinite, x).is_finite(),
258
"Basis with one component infinite should not be finite.");
259
CHECK_FALSE_MESSAGE(
260
Basis(x, x, infinite).is_finite(),
261
"Basis with one component infinite should not be finite.");
262
263
CHECK_FALSE_MESSAGE(
264
Basis(infinite, infinite, x).is_finite(),
265
"Basis with two components infinite should not be finite.");
266
CHECK_FALSE_MESSAGE(
267
Basis(infinite, x, infinite).is_finite(),
268
"Basis with two components infinite should not be finite.");
269
CHECK_FALSE_MESSAGE(
270
Basis(x, infinite, infinite).is_finite(),
271
"Basis with two components infinite should not be finite.");
272
273
CHECK_FALSE_MESSAGE(
274
Basis(infinite, infinite, infinite).is_finite(),
275
"Basis with three components infinite should not be finite.");
276
}
277
278
TEST_CASE("[Basis] Is conformal checks") {
279
CHECK_MESSAGE(
280
Basis().is_conformal(),
281
"Identity Basis should be conformal.");
282
283
CHECK_MESSAGE(
284
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_conformal(),
285
"Basis with only rotation should be conformal.");
286
287
CHECK_MESSAGE(
288
Basis::from_scale(Vector3(-1, -1, -1)).is_conformal(),
289
"Basis with only a flip should be conformal.");
290
291
CHECK_MESSAGE(
292
Basis::from_scale(Vector3(1.2, 1.2, 1.2)).is_conformal(),
293
"Basis with only uniform scale should be conformal.");
294
295
CHECK_MESSAGE(
296
Basis(Vector3(3, 4, 0), Vector3(4, -3, 0.0), Vector3(0, 0, 5)).is_conformal(),
297
"Basis with a flip, rotation, and uniform scale should be conformal.");
298
299
CHECK_FALSE_MESSAGE(
300
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_conformal(),
301
"Basis with non-uniform scale should not be conformal.");
302
303
CHECK_FALSE_MESSAGE(
304
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_conformal(),
305
"Basis with the X axis skewed 45 degrees should not be conformal.");
306
307
CHECK_MESSAGE(
308
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_conformal(),
309
"Edge case: Basis with all zeroes should return true for is_conformal (because a 0 scale is uniform).");
310
}
311
312
TEST_CASE("[Basis] Is orthogonal checks") {
313
CHECK_MESSAGE(
314
Basis().is_orthogonal(),
315
"Identity Basis should be orthogonal.");
316
317
CHECK_MESSAGE(
318
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_orthogonal(),
319
"Basis with only rotation should be orthogonal.");
320
321
CHECK_MESSAGE(
322
Basis::from_scale(Vector3(-1, -1, -1)).is_orthogonal(),
323
"Basis with only a flip should be orthogonal.");
324
325
CHECK_MESSAGE(
326
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_orthogonal(),
327
"Basis with only scale should be orthogonal.");
328
329
CHECK_MESSAGE(
330
Basis(Vector3(3, 4, 0), Vector3(4, -3, 0), Vector3(0, 0, 5)).is_orthogonal(),
331
"Basis with a flip, rotation, and uniform scale should be orthogonal.");
332
333
CHECK_FALSE_MESSAGE(
334
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_orthogonal(),
335
"Basis with the X axis skewed 45 degrees should not be orthogonal.");
336
337
CHECK_MESSAGE(
338
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_orthogonal(),
339
"Edge case: Basis with all zeroes should return true for is_orthogonal, since zero vectors are orthogonal to all vectors.");
340
}
341
342
TEST_CASE("[Basis] Is orthonormal checks") {
343
CHECK_MESSAGE(
344
Basis().is_orthonormal(),
345
"Identity Basis should be orthonormal.");
346
347
CHECK_MESSAGE(
348
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_orthonormal(),
349
"Basis with only rotation should be orthonormal.");
350
351
CHECK_MESSAGE(
352
Basis::from_scale(Vector3(-1, -1, -1)).is_orthonormal(),
353
"Basis with only a flip should be orthonormal.");
354
355
CHECK_FALSE_MESSAGE(
356
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_orthonormal(),
357
"Basis with only scale should not be orthonormal.");
358
359
CHECK_FALSE_MESSAGE(
360
Basis(Vector3(3, 4, 0), Vector3(4, -3, 0), Vector3(0, 0, 5)).is_orthonormal(),
361
"Basis with a flip, rotation, and uniform scale should not be orthonormal.");
362
363
CHECK_FALSE_MESSAGE(
364
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_orthonormal(),
365
"Basis with the X axis skewed 45 degrees should not be orthonormal.");
366
367
CHECK_FALSE_MESSAGE(
368
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_orthonormal(),
369
"Edge case: Basis with all zeroes should return false for is_orthonormal, since the vectors do not have a length of 1.");
370
}
371
372
TEST_CASE("[Basis] Is rotation checks") {
373
CHECK_MESSAGE(
374
Basis().is_rotation(),
375
"Identity Basis should be a rotation (a rotation of zero).");
376
377
CHECK_MESSAGE(
378
Basis::from_euler(Vector3(1.2, 3.4, 5.6)).is_rotation(),
379
"Basis with only rotation should be a rotation.");
380
381
CHECK_FALSE_MESSAGE(
382
Basis::from_scale(Vector3(-1, -1, -1)).is_rotation(),
383
"Basis with only a flip should not be a rotation.");
384
385
CHECK_FALSE_MESSAGE(
386
Basis::from_scale(Vector3(1.2, 3.4, 5.6)).is_rotation(),
387
"Basis with only scale should not be a rotation.");
388
389
CHECK_FALSE_MESSAGE(
390
Basis(Vector3(2, 0, 0), Vector3(0, 0.5, 0), Vector3(0, 0, 1)).is_rotation(),
391
"Basis with a squeeze should not be a rotation.");
392
393
CHECK_FALSE_MESSAGE(
394
Basis(Vector3(Math::SQRT12, Math::SQRT12, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)).is_rotation(),
395
"Basis with the X axis skewed 45 degrees should not be a rotation.");
396
397
CHECK_FALSE_MESSAGE(
398
Basis(0, 0, 0, 0, 0, 0, 0, 0, 0).is_rotation(),
399
"Edge case: Basis with all zeroes should return false for is_rotation, because it is not just a rotation (has a scale of 0).");
400
}
401
402
} // namespace TestBasis
403
404