Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_height_map_shape_3d.cpp
45991 views
1
/**************************************************************************/
2
/* test_height_map_shape_3d.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_height_map_shape_3d)
34
35
#ifndef PHYSICS_3D_DISABLED
36
37
#include "core/io/image.h"
38
#include "scene/resources/3d/height_map_shape_3d.h"
39
40
namespace TestHeightMapShape3D {
41
42
TEST_CASE("[SceneTree][HeightMapShape3D] Constructor") {
43
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
44
CHECK(height_map_shape->get_map_width() == 2);
45
CHECK(height_map_shape->get_map_depth() == 2);
46
CHECK(height_map_shape->get_map_data().size() == 4);
47
CHECK(height_map_shape->get_min_height() == 0.0);
48
CHECK(height_map_shape->get_max_height() == 0.0);
49
}
50
51
TEST_CASE("[SceneTree][HeightMapShape3D] set_map_width and get_map_width") {
52
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
53
height_map_shape->set_map_width(10);
54
CHECK(height_map_shape->get_map_width() == 10);
55
}
56
57
TEST_CASE("[SceneTree][HeightMapShape3D] set_map_depth and get_map_depth") {
58
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
59
height_map_shape->set_map_depth(15);
60
CHECK(height_map_shape->get_map_depth() == 15);
61
}
62
63
TEST_CASE("[SceneTree][HeightMapShape3D] set_map_data and get_map_data") {
64
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
65
Vector<real_t> map_data;
66
map_data.push_back(1.0);
67
map_data.push_back(2.0);
68
height_map_shape->set_map_data(map_data);
69
CHECK(height_map_shape->get_map_data().size() == 4.0);
70
CHECK(height_map_shape->get_map_data()[0] == 0.0);
71
CHECK(height_map_shape->get_map_data()[1] == 0.0);
72
}
73
74
TEST_CASE("[SceneTree][HeightMapShape3D] get_min_height") {
75
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
76
height_map_shape->set_map_width(3);
77
height_map_shape->set_map_depth(1);
78
height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });
79
CHECK(height_map_shape->get_min_height() == 0.5);
80
}
81
82
TEST_CASE("[SceneTree][HeightMapShape3D] get_max_height") {
83
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
84
height_map_shape->set_map_width(3);
85
height_map_shape->set_map_depth(1);
86
height_map_shape->set_map_data(Vector<real_t>{ 1.0, 2.0, 0.5 });
87
CHECK(height_map_shape->get_max_height() == 2.0);
88
}
89
90
TEST_CASE("[SceneTree][HeightMapShape3D] update_map_data_from_image") {
91
// Create a HeightMapShape3D instance.
92
Ref<HeightMapShape3D> height_map_shape = memnew(HeightMapShape3D);
93
94
// Create a mock image with FORMAT_R8 and set its data.
95
Vector<uint8_t> image_data;
96
image_data.push_back(0);
97
image_data.push_back(128);
98
image_data.push_back(255);
99
image_data.push_back(64);
100
101
Ref<Image> image = memnew(Image);
102
image->set_data(2, 2, false, Image::FORMAT_R8, image_data);
103
104
height_map_shape->update_map_data_from_image(image, 0.0, 10.0);
105
106
// Check the map data.
107
Vector<real_t> expected_map_data = { 0.0, 5.0, 10.0, 2.5 };
108
Vector<real_t> actual_map_data = height_map_shape->get_map_data();
109
real_t tolerance = 0.1;
110
111
for (int i = 0; i < expected_map_data.size(); ++i) {
112
CHECK(Math::abs(actual_map_data[i] - expected_map_data[i]) < tolerance);
113
}
114
115
// Check the min and max heights.
116
CHECK(height_map_shape->get_min_height() == 0.0);
117
CHECK(height_map_shape->get_max_height() == 10.0);
118
}
119
120
} // namespace TestHeightMapShape3D
121
122
#endif // PHYSICS_3D_DISABLED
123
124