Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_geometry_3d.h
10278 views
1
/**************************************************************************/
2
/* test_geometry_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/geometry_3d.h"
34
#include "tests/test_macros.h"
35
36
namespace TestGeometry3D {
37
TEST_CASE("[Geometry3D] Closest Points Between Segments") {
38
Vector3 ps, qt;
39
Geometry3D::get_closest_points_between_segments(Vector3(1, -1, 1), Vector3(1, 1, -1), Vector3(-1, -2, -1), Vector3(-1, 1, 1), ps, qt);
40
CHECK(ps.is_equal_approx(Vector3(1, -0.2, 0.2)));
41
CHECK(qt.is_equal_approx(Vector3(-1, -0.2, 0.2)));
42
}
43
44
TEST_CASE("[Geometry3D] Closest Distance Between Segments") {
45
CHECK(Geometry3D::get_closest_distance_between_segments(Vector3(1, -2, 0), Vector3(1, 2, 0), Vector3(-1, 2, 0), Vector3(-1, -2, 0)) == 2.0f);
46
}
47
48
TEST_CASE("[Geometry3D] Build Box Planes") {
49
constexpr Vector3 extents = Vector3(5, 5, 20);
50
Vector<Plane> box = Geometry3D::build_box_planes(extents);
51
CHECK(box.size() == 6);
52
CHECK(extents.x == box[0].d);
53
CHECK(box[0].normal == Vector3(1, 0, 0));
54
CHECK(extents.x == box[1].d);
55
CHECK(box[1].normal == Vector3(-1, 0, 0));
56
CHECK(extents.y == box[2].d);
57
CHECK(box[2].normal == Vector3(0, 1, 0));
58
CHECK(extents.y == box[3].d);
59
CHECK(box[3].normal == Vector3(0, -1, 0));
60
CHECK(extents.z == box[4].d);
61
CHECK(box[4].normal == Vector3(0, 0, 1));
62
CHECK(extents.z == box[5].d);
63
CHECK(box[5].normal == Vector3(0, 0, -1));
64
}
65
66
TEST_CASE("[Geometry3D] Build Capsule Planes") {
67
Vector<Plane> capsule = Geometry3D::build_capsule_planes(10, 20, 6, 10);
68
CHECK(capsule.size() == 126);
69
}
70
71
TEST_CASE("[Geometry3D] Build Cylinder Planes") {
72
Vector<Plane> planes = Geometry3D::build_cylinder_planes(3.0f, 10.0f, 10);
73
CHECK(planes.size() == 12);
74
}
75
76
TEST_CASE("[Geometry3D] Build Sphere Planes") {
77
Vector<Plane> planes = Geometry3D::build_sphere_planes(10.0f, 10, 3);
78
CHECK(planes.size() == 63);
79
}
80
81
#if false
82
// This test has been temporarily disabled because it's really fragile and
83
// breaks if calculations change very slightly. For example, it breaks when
84
// using doubles, and it breaks when making Plane calculations more accurate.
85
TEST_CASE("[Geometry3D] Build Convex Mesh") {
86
struct Case {
87
Vector<Plane> object;
88
int want_faces, want_edges, want_vertices;
89
Case(){};
90
Case(Vector<Plane> p_object, int p_want_faces, int p_want_edges, int p_want_vertices) :
91
object(p_object), want_faces(p_want_faces), want_edges(p_want_edges), want_vertices(p_want_vertices){};
92
};
93
Vector<Case> tt;
94
tt.push_back(Case(Geometry3D::build_box_planes(Vector3(5, 10, 5)), 6, 12, 8));
95
tt.push_back(Case(Geometry3D::build_capsule_planes(5, 5, 20, 20, Vector3::Axis()), 820, 7603, 6243));
96
tt.push_back(Case(Geometry3D::build_cylinder_planes(5, 5, 20, Vector3::Axis()), 22, 100, 80));
97
tt.push_back(Case(Geometry3D::build_sphere_planes(5, 5, 20), 220, 1011, 522));
98
for (int i = 0; i < tt.size(); ++i) {
99
Case current_case = tt[i];
100
Geometry3D::MeshData mesh = Geometry3D::build_convex_mesh(current_case.object);
101
CHECK(mesh.faces.size() == current_case.want_faces);
102
CHECK(mesh.edges.size() == current_case.want_edges);
103
CHECK(mesh.vertices.size() == current_case.want_vertices);
104
}
105
}
106
#endif
107
108
TEST_CASE("[Geometry3D] Clip Polygon") {
109
Vector<Plane> box_planes = Geometry3D::build_box_planes(Vector3(5, 10, 5));
110
Vector<Vector3> box = Geometry3D::compute_convex_mesh_points(&box_planes[0], box_planes.size());
111
Vector<Vector3> output = Geometry3D::clip_polygon(box, Plane());
112
CHECK(output == box);
113
output = Geometry3D::clip_polygon(box, Plane(Vector3(0, 1, 0), Vector3(0, 3, 0)));
114
CHECK(output != box);
115
}
116
117
TEST_CASE("[Geometry3D] Compute Convex Mesh Points") {
118
Vector<Vector3> cube;
119
cube.push_back(Vector3(-5, -5, -5));
120
cube.push_back(Vector3(5, -5, -5));
121
cube.push_back(Vector3(-5, 5, -5));
122
cube.push_back(Vector3(5, 5, -5));
123
cube.push_back(Vector3(-5, -5, 5));
124
cube.push_back(Vector3(5, -5, 5));
125
cube.push_back(Vector3(-5, 5, 5));
126
cube.push_back(Vector3(5, 5, 5));
127
Vector<Plane> box_planes = Geometry3D::build_box_planes(Vector3(5, 5, 5));
128
CHECK(Geometry3D::compute_convex_mesh_points(&box_planes[0], box_planes.size()) == cube);
129
}
130
131
TEST_CASE("[Geometry3D] Get Closest Point To Segment") {
132
constexpr Vector3 a = Vector3(1, 1, 1);
133
constexpr Vector3 b = Vector3(5, 5, 5);
134
Vector3 output = Geometry3D::get_closest_point_to_segment(Vector3(2, 1, 4), a, b);
135
CHECK(output.is_equal_approx(Vector3(2.33333, 2.33333, 2.33333)));
136
}
137
138
TEST_CASE("[Geometry3D] Plane and Box Overlap") {
139
CHECK(Geometry3D::planeBoxOverlap(Vector3(3, 4, 2), 5.0f, Vector3(5, 5, 5)) == true);
140
CHECK(Geometry3D::planeBoxOverlap(Vector3(0, 1, 0), -10.0f, Vector3(5, 5, 5)) == false);
141
CHECK(Geometry3D::planeBoxOverlap(Vector3(1, 0, 0), -6.0f, Vector3(5, 5, 5)) == false);
142
}
143
144
TEST_CASE("[Geometry3D] Is Point in Projected Triangle") {
145
CHECK(Geometry3D::point_in_projected_triangle(Vector3(1, 1, 0), Vector3(3, 0, 0), Vector3(0, 3, 0), Vector3(-3, 0, 0)) == true);
146
CHECK(Geometry3D::point_in_projected_triangle(Vector3(5, 1, 0), Vector3(3, 0, 0), Vector3(0, 3, 0), Vector3(-3, 0, 0)) == false);
147
CHECK(Geometry3D::point_in_projected_triangle(Vector3(3, 0, 0), Vector3(3, 0, 0), Vector3(0, 3, 0), Vector3(-3, 0, 0)) == true);
148
}
149
150
TEST_CASE("[Geometry3D] Does Ray Intersect Triangle") {
151
Vector3 result;
152
CHECK(Geometry3D::ray_intersects_triangle(Vector3(0, 1, 1), Vector3(0, 0, -10), Vector3(0, 3, 0), Vector3(-3, 0, 0), Vector3(3, 0, 0), &result) == true);
153
CHECK(Geometry3D::ray_intersects_triangle(Vector3(5, 10, 1), Vector3(0, 0, -10), Vector3(0, 3, 0), Vector3(-3, 0, 0), Vector3(3, 0, 0), &result) == false);
154
CHECK(Geometry3D::ray_intersects_triangle(Vector3(0, 1, 1), Vector3(0, 0, 10), Vector3(0, 3, 0), Vector3(-3, 0, 0), Vector3(3, 0, 0), &result) == false);
155
}
156
157
TEST_CASE("[Geometry3D] Does Segment Intersect Convex") {
158
Vector<Plane> box_planes = Geometry3D::build_box_planes(Vector3(5, 5, 5));
159
Vector3 result, normal;
160
CHECK(Geometry3D::segment_intersects_convex(Vector3(10, 10, 10), Vector3(0, 0, 0), &box_planes[0], box_planes.size(), &result, &normal) == true);
161
CHECK(Geometry3D::segment_intersects_convex(Vector3(10, 10, 10), Vector3(5, 5, 5), &box_planes[0], box_planes.size(), &result, &normal) == true);
162
CHECK(Geometry3D::segment_intersects_convex(Vector3(10, 10, 10), Vector3(6, 5, 5), &box_planes[0], box_planes.size(), &result, &normal) == false);
163
}
164
165
TEST_CASE("[Geometry3D] Segment Intersects Cylinder") {
166
Vector3 result, normal;
167
CHECK(Geometry3D::segment_intersects_cylinder(Vector3(10, 10, 10), Vector3(0, 0, 0), 5, 5, &result, &normal) == true);
168
CHECK(Geometry3D::segment_intersects_cylinder(Vector3(10, 10, 10), Vector3(6, 6, 6), 5, 5, &result, &normal) == false);
169
}
170
171
TEST_CASE("[Geometry3D] Segment Intersects Cylinder") {
172
Vector3 result, normal;
173
CHECK(Geometry3D::segment_intersects_sphere(Vector3(10, 10, 10), Vector3(0, 0, 0), Vector3(0, 0, 0), 5, &result, &normal) == true);
174
CHECK(Geometry3D::segment_intersects_sphere(Vector3(10, 10, 10), Vector3(0, 0, 2.5), Vector3(0, 0, 0), 5, &result, &normal) == true);
175
CHECK(Geometry3D::segment_intersects_sphere(Vector3(10, 10, 10), Vector3(5, 5, 5), Vector3(0, 0, 0), 5, &result, &normal) == false);
176
}
177
178
TEST_CASE("[Geometry3D] Segment Intersects Triangle") {
179
Vector3 result;
180
CHECK(Geometry3D::segment_intersects_triangle(Vector3(1, 1, 1), Vector3(-1, -1, -1), Vector3(-3, 0, 0), Vector3(0, 3, 0), Vector3(3, 0, 0), &result) == true);
181
CHECK(Geometry3D::segment_intersects_triangle(Vector3(1, 1, 1), Vector3(3, 0, 0), Vector3(-3, 0, 0), Vector3(0, 3, 0), Vector3(3, 0, 0), &result) == true);
182
CHECK(Geometry3D::segment_intersects_triangle(Vector3(1, 1, 1), Vector3(10, -1, -1), Vector3(-3, 0, 0), Vector3(0, 3, 0), Vector3(3, 0, 0), &result) == false);
183
}
184
185
TEST_CASE("[Geometry3D] Triangle and Box Overlap") {
186
constexpr Vector3 good_triangle[3] = { Vector3(3, 2, 3), Vector3(2, 2, 1), Vector3(2, 1, 1) };
187
CHECK(Geometry3D::triangle_box_overlap(Vector3(0, 0, 0), Vector3(5, 5, 5), good_triangle) == true);
188
constexpr Vector3 bad_triangle[3] = { Vector3(100, 100, 100), Vector3(-100, -100, -100), Vector3(10, 10, 10) };
189
CHECK(Geometry3D::triangle_box_overlap(Vector3(1000, 1000, 1000), Vector3(1, 1, 1), bad_triangle) == false);
190
}
191
192
TEST_CASE("[Geometry3D] Triangle and Sphere Intersect") {
193
constexpr Vector3 triangle_a = Vector3(3, 0, 0);
194
constexpr Vector3 triangle_b = Vector3(-3, 0, 0);
195
constexpr Vector3 triangle_c = Vector3(0, 3, 0);
196
Vector3 triangle_contact, sphere_contact;
197
CHECK(Geometry3D::triangle_sphere_intersection_test(triangle_a, triangle_b, triangle_c, Vector3(0, -1, 0), Vector3(0, 0, 0), 5, triangle_contact, sphere_contact) == true);
198
CHECK(Geometry3D::triangle_sphere_intersection_test(triangle_a, triangle_b, triangle_c, Vector3(0, 1, 0), Vector3(0, 0, 0), 5, triangle_contact, sphere_contact) == true);
199
CHECK(Geometry3D::triangle_sphere_intersection_test(triangle_a, triangle_b, triangle_c, Vector3(0, 1, 0), Vector3(20, 0, 0), 5, triangle_contact, sphere_contact) == false);
200
}
201
} // namespace TestGeometry3D
202
203