Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/csg/tests/test_csg.h
10278 views
1
/**************************************************************************/
2
/* test_csg.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 "../csg.h"
34
#include "../csg_shape.h"
35
36
#include "tests/test_macros.h"
37
38
namespace TestCSG {
39
40
TEST_CASE("[SceneTree][CSG] CSGPolygon3D") {
41
SUBCASE("[SceneTree][CSG] CSGPolygon3D: using accurate path tangent for polygon rotation") {
42
const float polygon_radius = 10.0f;
43
44
const Vector3 expected_min_bounds = Vector3(-polygon_radius, -polygon_radius, 0);
45
const Vector3 expected_max_bounds = Vector3(100 + polygon_radius, polygon_radius, 100);
46
const AABB expected_aabb = AABB(expected_min_bounds, expected_max_bounds - expected_min_bounds);
47
48
Ref<Curve3D> curve;
49
curve.instantiate();
50
curve->add_point(
51
// p_position
52
Vector3(0, 0, 0),
53
// p_in
54
Vector3(),
55
// p_out
56
Vector3(0, 0, 60));
57
curve->add_point(
58
// p_position
59
Vector3(100, 0, 100),
60
// p_in
61
Vector3(0, 0, -60),
62
// p_out
63
Vector3());
64
65
Path3D *path = memnew(Path3D);
66
path->set_curve(curve);
67
68
CSGPolygon3D *csg_polygon_3d = memnew(CSGPolygon3D);
69
SceneTree::get_singleton()->get_root()->add_child(csg_polygon_3d);
70
71
csg_polygon_3d->add_child(path);
72
csg_polygon_3d->set_path_node(csg_polygon_3d->get_path_to(path));
73
csg_polygon_3d->set_mode(CSGPolygon3D::Mode::MODE_PATH);
74
75
PackedVector2Array polygon;
76
polygon.append(Vector2(-polygon_radius, 0));
77
polygon.append(Vector2(0, polygon_radius));
78
polygon.append(Vector2(polygon_radius, 0));
79
polygon.append(Vector2(0, -polygon_radius));
80
csg_polygon_3d->set_polygon(polygon);
81
82
csg_polygon_3d->set_path_rotation(CSGPolygon3D::PathRotation::PATH_ROTATION_PATH);
83
csg_polygon_3d->set_path_rotation_accurate(true);
84
85
// Minimize the number of extrusions.
86
// This decreases the number of samples taken from the curve.
87
// Having fewer samples increases the inaccuracy of the line between samples as an approximation of the tangent of the curve.
88
// With correct polygon orientation, the bounding box for the given curve should be independent of the number of extrusions.
89
csg_polygon_3d->set_path_interval_type(CSGPolygon3D::PathIntervalType::PATH_INTERVAL_DISTANCE);
90
csg_polygon_3d->set_path_interval(1000.0f);
91
92
// Call get_brush_faces to force the bounding box to update.
93
csg_polygon_3d->get_brush_faces();
94
95
CHECK(csg_polygon_3d->get_aabb().is_equal_approx(expected_aabb));
96
97
// Perform the bounding box check again with a greater number of extrusions.
98
csg_polygon_3d->set_path_interval(1.0f);
99
csg_polygon_3d->get_brush_faces();
100
101
CHECK(csg_polygon_3d->get_aabb().is_equal_approx(expected_aabb));
102
103
csg_polygon_3d->remove_child(path);
104
SceneTree::get_singleton()->get_root()->remove_child(csg_polygon_3d);
105
106
memdelete(csg_polygon_3d);
107
memdelete(path);
108
}
109
}
110
111
} // namespace TestCSG
112
113