Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/math/test_triangle_mesh.cpp
23450 views
1
/**************************************************************************/
2
/* test_triangle_mesh.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_triangle_mesh)
34
35
#ifndef _3D_DISABLED
36
37
#include "core/math/triangle_mesh.h"
38
#include "scene/resources/3d/primitive_meshes.h"
39
40
namespace TestTriangleMesh {
41
42
TEST_CASE("[SceneTree][TriangleMesh] BVH creation and intersection") {
43
Ref<BoxMesh> box_mesh;
44
box_mesh.instantiate();
45
46
const Vector<Face3> faces = box_mesh->get_faces();
47
48
Ref<TriangleMesh> triangle_mesh;
49
triangle_mesh.instantiate();
50
CHECK(triangle_mesh->create_from_faces(Variant(faces)));
51
52
const Vector3 begin = Vector3(0.0, 2.0, 0.0);
53
const Vector3 end = Vector3(0.0, -2.0, 0.0);
54
55
{
56
Vector3 point;
57
Vector3 normal;
58
int32_t *surf_index = nullptr;
59
int32_t face_index = -1;
60
const bool has_result = triangle_mesh->intersect_segment(begin, end, point, normal, surf_index, &face_index);
61
CHECK(has_result);
62
CHECK(point.is_equal_approx(Vector3(0.0, 0.5, 0.0)));
63
CHECK(normal.is_equal_approx(Vector3(0.0, 1.0, 0.0)));
64
CHECK(surf_index == nullptr);
65
REQUIRE(face_index != -1);
66
CHECK(face_index == 8);
67
}
68
69
{
70
Vector3 dir = begin.direction_to(end);
71
Vector3 point;
72
Vector3 normal;
73
int32_t *surf_index = nullptr;
74
int32_t face_index = -1;
75
const bool has_result = triangle_mesh->intersect_ray(begin, dir, point, normal, surf_index, &face_index);
76
CHECK(has_result);
77
CHECK(point.is_equal_approx(Vector3(0.0, 0.5, 0.0)));
78
CHECK(normal.is_equal_approx(Vector3(0.0, 1.0, 0.0)));
79
CHECK(surf_index == nullptr);
80
REQUIRE(face_index != -1);
81
CHECK(face_index == 8);
82
}
83
}
84
85
} // namespace TestTriangleMesh
86
87
#endif // _3D_DISABLED
88
89