1/**************************************************************************/
2/* nav_region.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 "nav_region.h"
32
33#include "nav_map.h"
34
35void NavRegion::set_map(NavMap *p_map) {
36 if (map == p_map) {
37 return;
38 }
39
40 if (map) {
41 map->remove_region(this);
42 }
43
44 map = p_map;
45 polygons_dirty = true;
46
47 connections.clear();
48
49 if (map) {
50 map->add_region(this);
51 }
52}
53
54void NavRegion::set_enabled(bool p_enabled) {
55 if (enabled == p_enabled) {
56 return;
57 }
58 enabled = p_enabled;
59
60 // TODO: This should not require a full rebuild as the region has not really changed.
61 polygons_dirty = true;
62};
63
64void NavRegion::set_use_edge_connections(bool p_enabled) {
65 if (use_edge_connections != p_enabled) {
66 use_edge_connections = p_enabled;
67 polygons_dirty = true;
68 }
69}
70
71void NavRegion::set_transform(Transform3D p_transform) {
72 if (transform == p_transform) {
73 return;
74 }
75 transform = p_transform;
76 polygons_dirty = true;
77}
78
79void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
80 mesh = p_mesh;
81 polygons_dirty = true;
82}
83
84int NavRegion::get_connections_count() const {
85 if (!map) {
86 return 0;
87 }
88 return connections.size();
89}
90
91Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
92 ERR_FAIL_COND_V(!map, Vector3());
93 ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
94 return connections[p_connection_id].pathway_start;
95}
96
97Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
98 ERR_FAIL_COND_V(!map, Vector3());
99 ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
100 return connections[p_connection_id].pathway_end;
101}
102
103bool NavRegion::sync() {
104 bool something_changed = polygons_dirty /* || something_dirty? */;
105
106 update_polygons();
107
108 return something_changed;
109}
110
111void NavRegion::update_polygons() {
112 if (!polygons_dirty) {
113 return;
114 }
115 polygons.clear();
116 polygons_dirty = false;
117
118 if (map == nullptr) {
119 return;
120 }
121
122 if (mesh.is_null()) {
123 return;
124 }
125
126#ifdef DEBUG_ENABLED
127 if (!Math::is_equal_approx(double(map->get_cell_size()), double(mesh->get_cell_size()))) {
128 ERR_PRINT_ONCE(vformat("Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a `cell_size` of %s while assigned to a navigation map set to a `cell_size` of %s. The cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function. The cell size for default navigation maps can also be changed in the ProjectSettings.", double(map->get_cell_size()), double(mesh->get_cell_size())));
129 }
130
131 if (!Math::is_equal_approx(double(map->get_cell_height()), double(mesh->get_cell_height()))) {
132 ERR_PRINT_ONCE(vformat("Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a `cell_height` of %s while assigned to a navigation map set to a `cell_height` of %s. The cell height for navigation maps can be changed by using the NavigationServer map_set_cell_height() function. The cell height for default navigation maps can also be changed in the ProjectSettings.", double(map->get_cell_height()), double(mesh->get_cell_height())));
133 }
134
135 if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) {
136 ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.");
137 }
138#endif // DEBUG_ENABLED
139
140 Vector<Vector3> vertices = mesh->get_vertices();
141 int len = vertices.size();
142 if (len == 0) {
143 return;
144 }
145
146 const Vector3 *vertices_r = vertices.ptr();
147
148 polygons.resize(mesh->get_polygon_count());
149
150 // Build
151 for (size_t i(0); i < polygons.size(); i++) {
152 gd::Polygon &p = polygons[i];
153 p.owner = this;
154
155 Vector<int> mesh_poly = mesh->get_polygon(i);
156 const int *indices = mesh_poly.ptr();
157 bool valid(true);
158 p.points.resize(mesh_poly.size());
159 p.edges.resize(mesh_poly.size());
160
161 Vector3 center;
162 real_t sum(0);
163
164 for (int j(0); j < mesh_poly.size(); j++) {
165 int idx = indices[j];
166 if (idx < 0 || idx >= len) {
167 valid = false;
168 break;
169 }
170
171 Vector3 point_position = transform.xform(vertices_r[idx]);
172 p.points[j].pos = point_position;
173 p.points[j].key = map->get_point_key(point_position);
174
175 center += point_position; // Composing the center of the polygon
176
177 if (j >= 2) {
178 Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
179 Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
180
181 sum += map->get_up().dot((epb - epa).cross(point_position - epa));
182 }
183 }
184
185 if (!valid) {
186 ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
187 }
188
189 p.clockwise = sum > 0;
190 if (mesh_poly.size() != 0) {
191 p.center = center / real_t(mesh_poly.size());
192 }
193 }
194}
195