Debian Codegraph @

tests/core/math/test_transform_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_transform_3d.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_3d.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestTransform3D {
38
39 Transform3D create_dummy_transform() {
40 return Transform3D(Basis(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9)), Vector3(10, 11, 12));
41 }
42
43 Transform3D identity() {
44 return Transform3D();
45 }
46
47 TEST_CASE("[Transform3D] translation") {
48 constexpr Vector3 offset = Vector3(1, 2, 3);
49
50 // Both versions should give the same result applied to identity.
51 CHECK(identity().translated(offset) == identity().translated_local(offset));
52
53 // Check both versions against left and right multiplications.
54 Transform3D orig = create_dummy_transform();
55 Transform3D T = identity().translated(offset);
56 CHECK(orig.translated(offset) == T * orig);
57 CHECK(orig.translated_local(offset) == orig * T);
58 }
59
60 TEST_CASE("[Transform3D] scaling") {
61 constexpr Vector3 scaling = Vector3(1, 2, 3);
62
63 // Both versions should give the same result applied to identity.
64 CHECK(identity().scaled(scaling) == identity().scaled_local(scaling));
65
66 // Check both versions against left and right multiplications.
67 Transform3D orig = create_dummy_transform();
68 Transform3D S = identity().scaled(scaling);
69 CHECK(orig.scaled(scaling) == S * orig);
70 CHECK(orig.scaled_local(scaling) == orig * S);
71 }
72
73 TEST_CASE("[Transform3D] rotation") {
74 const Vector3 axis = Vector3(1, 2, 3).normalized();
75 constexpr real_t phi = 1.0;
76
77 // Both versions should give the same result applied to identity.
78 CHECK(identity().rotated(axis, phi) == identity().rotated_local(axis, phi));
79
80 // Check both versions against left and right multiplications.
81 Transform3D orig = create_dummy_transform();
82 Transform3D R = identity().rotated(axis, phi);
83 CHECK(orig.rotated(axis, phi) == R * orig);
84 CHECK(orig.rotated_local(axis, phi) == orig * R);
85 }
86
87 TEST_CASE("[Transform3D] Finite number checks") {
88 constexpr Vector3 y(0, 1, 2);
89 constexpr Vector3 infinite_vec(Math::NaN, Math::NaN, Math::NaN);
90 constexpr Basis x(y, y, y);
91 constexpr Basis infinite_basis(infinite_vec, infinite_vec, infinite_vec);
92
93 CHECK_MESSAGE(
94 Transform3D(x, y).is_finite(),
95 "Transform3D with all components finite should be finite");
96
97 CHECK_FALSE_MESSAGE(
98 Transform3D(x, infinite_vec).is_finite(),
99 "Transform3D with one component infinite should not be finite.");
100 CHECK_FALSE_MESSAGE(
101 Transform3D(infinite_basis, y).is_finite(),
102 "Transform3D with one component infinite should not be finite.");
103
104 CHECK_FALSE_MESSAGE(
105 Transform3D(infinite_basis, infinite_vec).is_finite(),
106 "Transform3D with two components infinite should not be finite.");
107 }
108
109 TEST_CASE("[Transform3D] Rotate around global origin") {
110 // Start with the default orientation, but not centered on the origin.
111 // Rotating should rotate both our basis and the origin.
112 Transform3D transform = Transform3D();
113 transform.origin = Vector3(0, 0, 1);
114
115 Transform3D expected = Transform3D();
116 expected.origin = Vector3(0, 0, -1);
117 expected.basis[0] = Vector3(-1, 0, 0);
118 expected.basis[2] = Vector3(0, 0, -1);
119
120 const Transform3D rotated_transform = transform.rotated(Vector3(0, 1, 0), Math::PI);
121 CHECK_MESSAGE(rotated_transform.is_equal_approx(expected), "The rotated transform should have a new orientation and basis.");
122 }
123
124 TEST_CASE("[Transform3D] Rotate in-place (local rotation)") {
125 // Start with the default orientation.
126 // Local rotation should not change the origin, only the basis.
127 Transform3D transform = Transform3D();
128 transform.origin = Vector3(1, 2, 3);
129
130 Transform3D expected = Transform3D();
131 expected.origin = Vector3(1, 2, 3);
132 expected.basis[0] = Vector3(-1, 0, 0);
133 expected.basis[2] = Vector3(0, 0, -1);
134
135 const Transform3D rotated_transform = Transform3D(transform.rotated_local(Vector3(0, 1, 0), Math::PI));
136 CHECK_MESSAGE(rotated_transform.is_equal_approx(expected), "The rotated transform should have a new orientation but still be based on the same origin.");
137 }
138 } // namespace TestTransform3D