Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_plane.cpp
23450 views
1
/**************************************************************************/
2
/* test_plane.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_plane)
34
35
#include "core/math/plane.h"
36
37
namespace TestPlane {
38
39
TEST_CASE("[Plane] Constructor methods") {
40
constexpr Plane plane = Plane(32, 22, 16, 3);
41
constexpr Plane plane_vector = Plane(Vector3(32, 22, 16), 3);
42
constexpr Plane plane_copy_plane = Plane(plane);
43
44
static_assert(
45
plane == plane_vector,
46
"Planes created with same values but different methods should be equal.");
47
48
static_assert(
49
plane == plane_copy_plane,
50
"Planes created with same values but different methods should be equal.");
51
}
52
53
TEST_CASE("[Plane] Basic getters") {
54
constexpr Plane plane = Plane(32, 22, 16, 3);
55
constexpr Plane plane_normalized = Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42);
56
57
CHECK_MESSAGE(
58
plane.get_normal().is_equal_approx(Vector3(32, 22, 16)),
59
"get_normal() should return the expected value.");
60
61
CHECK_MESSAGE(
62
plane.normalized().is_equal_approx(plane_normalized),
63
"normalized() should return a copy of the normalized value.");
64
}
65
66
TEST_CASE("[Plane] Basic setters") {
67
Plane plane = Plane(32, 22, 16, 3);
68
plane.set_normal(Vector3(4, 2, 3));
69
70
CHECK_MESSAGE(
71
plane.is_equal_approx(Plane(4, 2, 3, 3)),
72
"set_normal() should result in the expected plane.");
73
74
plane = Plane(32, 22, 16, 3);
75
plane.normalize();
76
77
CHECK_MESSAGE(
78
plane.is_equal_approx(Plane(32.0 / 42, 22.0 / 42, 16.0 / 42, 3.0 / 42)),
79
"normalize() should result in the expected plane.");
80
}
81
82
TEST_CASE("[Plane] Plane-point operations") {
83
constexpr Plane plane = Plane(32, 22, 16, 3);
84
constexpr Plane y_facing_plane = Plane(0, 1, 0, 4);
85
86
CHECK_MESSAGE(
87
plane.get_center().is_equal_approx(Vector3(32 * 3, 22 * 3, 16 * 3)),
88
"get_center() should return a vector pointing to the center of the plane.");
89
90
CHECK_MESSAGE(
91
y_facing_plane.is_point_over(Vector3(0, 5, 0)),
92
"is_point_over() should return the expected result.");
93
94
CHECK_MESSAGE(
95
y_facing_plane.get_any_perpendicular_normal().is_equal_approx(Vector3(1, 0, 0)),
96
"get_any_perpendicular_normal() should return the expected result.");
97
98
// TODO distance_to()
99
}
100
101
TEST_CASE("[Plane] Has point") {
102
constexpr Plane x_facing_plane = Plane(1, 0, 0, 0);
103
constexpr Plane y_facing_plane = Plane(0, 1, 0, 0);
104
constexpr Plane z_facing_plane = Plane(0, 0, 1, 0);
105
106
constexpr Vector3 x_axis_point = Vector3(10, 0, 0);
107
constexpr Vector3 y_axis_point = Vector3(0, 10, 0);
108
constexpr Vector3 z_axis_point = Vector3(0, 0, 10);
109
110
constexpr Plane x_facing_plane_with_d_offset = Plane(1, 0, 0, 1);
111
constexpr Vector3 y_axis_point_with_d_offset = Vector3(1, 10, 0);
112
113
CHECK_MESSAGE(
114
x_facing_plane.has_point(y_axis_point),
115
"has_point() with contained Vector3 should return the expected result.");
116
CHECK_MESSAGE(
117
x_facing_plane.has_point(z_axis_point),
118
"has_point() with contained Vector3 should return the expected result.");
119
120
CHECK_MESSAGE(
121
y_facing_plane.has_point(x_axis_point),
122
"has_point() with contained Vector3 should return the expected result.");
123
CHECK_MESSAGE(
124
y_facing_plane.has_point(z_axis_point),
125
"has_point() with contained Vector3 should return the expected result.");
126
127
CHECK_MESSAGE(
128
z_facing_plane.has_point(y_axis_point),
129
"has_point() with contained Vector3 should return the expected result.");
130
CHECK_MESSAGE(
131
z_facing_plane.has_point(x_axis_point),
132
"has_point() with contained Vector3 should return the expected result.");
133
134
CHECK_MESSAGE(
135
x_facing_plane_with_d_offset.has_point(y_axis_point_with_d_offset),
136
"has_point() with passed Vector3 should return the expected result.");
137
}
138
139
TEST_CASE("[Plane] Intersection") {
140
constexpr Plane x_facing_plane = Plane(1, 0, 0, 1);
141
constexpr Plane y_facing_plane = Plane(0, 1, 0, 2);
142
constexpr Plane z_facing_plane = Plane(0, 0, 1, 3);
143
144
Vector3 vec_out;
145
146
CHECK_MESSAGE(
147
x_facing_plane.intersect_3(y_facing_plane, z_facing_plane, &vec_out),
148
"intersect_3() should return the expected result.");
149
CHECK_MESSAGE(
150
vec_out.is_equal_approx(Vector3(1, 2, 3)),
151
"intersect_3() should modify vec_out to the expected result.");
152
153
CHECK_MESSAGE(
154
x_facing_plane.intersects_ray(Vector3(0, 1, 1), Vector3(2, 0, 0), &vec_out),
155
"intersects_ray() should return the expected result.");
156
CHECK_MESSAGE(
157
vec_out.is_equal_approx(Vector3(1, 1, 1)),
158
"intersects_ray() should modify vec_out to the expected result.");
159
160
CHECK_MESSAGE(
161
x_facing_plane.intersects_segment(Vector3(0, 1, 1), Vector3(2, 1, 1), &vec_out),
162
"intersects_segment() should return the expected result.");
163
CHECK_MESSAGE(
164
vec_out.is_equal_approx(Vector3(1, 1, 1)),
165
"intersects_segment() should modify vec_out to the expected result.");
166
}
167
168
TEST_CASE("[Plane] Finite number checks") {
169
constexpr Vector3 x(0, 1, 2);
170
constexpr Vector3 infinite_vec(Math::NaN, Math::NaN, Math::NaN);
171
constexpr real_t y = 0;
172
constexpr real_t infinite_y = Math::NaN;
173
174
CHECK_MESSAGE(
175
Plane(x, y).is_finite(),
176
"Plane with all components finite should be finite");
177
178
CHECK_FALSE_MESSAGE(
179
Plane(x, infinite_y).is_finite(),
180
"Plane with one component infinite should not be finite.");
181
CHECK_FALSE_MESSAGE(
182
Plane(infinite_vec, y).is_finite(),
183
"Plane with one component infinite should not be finite.");
184
185
CHECK_FALSE_MESSAGE(
186
Plane(infinite_vec, infinite_y).is_finite(),
187
"Plane with two components infinite should not be finite.");
188
}
189
190
} // namespace TestPlane
191
192