nav_mesh_queries_3d.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /**************************************************************************/
  2. /* nav_mesh_queries_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. #ifndef _3D_DISABLED
  31. #include "nav_mesh_queries_3d.h"
  32. #include "../nav_base.h"
  33. #include "../nav_map.h"
  34. #include "core/math/geometry_3d.h"
  35. #include "servers/navigation/navigation_utilities.h"
  36. #define THREE_POINTS_CROSS_PRODUCT(m_a, m_b, m_c) (((m_c) - (m_a)).cross((m_b) - (m_a)))
  37. bool NavMeshQueries3D::emit_callback(const Callable &p_callback) {
  38. ERR_FAIL_COND_V(!p_callback.is_valid(), false);
  39. Callable::CallError ce;
  40. Variant result;
  41. p_callback.callp(nullptr, 0, result, ce);
  42. return ce.error == Callable::CallError::CALL_OK;
  43. }
  44. Vector3 NavMeshQueries3D::polygons_get_random_point(const LocalVector<gd::Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly) {
  45. const LocalVector<gd::Polygon> &region_polygons = p_polygons;
  46. if (region_polygons.is_empty()) {
  47. return Vector3();
  48. }
  49. if (p_uniformly) {
  50. real_t accumulated_area = 0;
  51. RBMap<real_t, uint32_t> region_area_map;
  52. for (uint32_t rp_index = 0; rp_index < region_polygons.size(); rp_index++) {
  53. const gd::Polygon &region_polygon = region_polygons[rp_index];
  54. real_t polyon_area = region_polygon.surface_area;
  55. if (polyon_area == 0.0) {
  56. continue;
  57. }
  58. region_area_map[accumulated_area] = rp_index;
  59. accumulated_area += polyon_area;
  60. }
  61. if (region_area_map.is_empty() || accumulated_area == 0) {
  62. // All polygons have no real surface / no area.
  63. return Vector3();
  64. }
  65. real_t region_area_map_pos = Math::random(real_t(0), accumulated_area);
  66. RBMap<real_t, uint32_t>::Iterator region_E = region_area_map.find_closest(region_area_map_pos);
  67. ERR_FAIL_COND_V(!region_E, Vector3());
  68. uint32_t rrp_polygon_index = region_E->value;
  69. ERR_FAIL_UNSIGNED_INDEX_V(rrp_polygon_index, region_polygons.size(), Vector3());
  70. const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
  71. real_t accumulated_polygon_area = 0;
  72. RBMap<real_t, uint32_t> polygon_area_map;
  73. for (uint32_t rpp_index = 2; rpp_index < rr_polygon.points.size(); rpp_index++) {
  74. real_t face_area = Face3(rr_polygon.points[0].pos, rr_polygon.points[rpp_index - 1].pos, rr_polygon.points[rpp_index].pos).get_area();
  75. if (face_area == 0.0) {
  76. continue;
  77. }
  78. polygon_area_map[accumulated_polygon_area] = rpp_index;
  79. accumulated_polygon_area += face_area;
  80. }
  81. if (polygon_area_map.is_empty() || accumulated_polygon_area == 0) {
  82. // All faces have no real surface / no area.
  83. return Vector3();
  84. }
  85. real_t polygon_area_map_pos = Math::random(real_t(0), accumulated_polygon_area);
  86. RBMap<real_t, uint32_t>::Iterator polygon_E = polygon_area_map.find_closest(polygon_area_map_pos);
  87. ERR_FAIL_COND_V(!polygon_E, Vector3());
  88. uint32_t rrp_face_index = polygon_E->value;
  89. ERR_FAIL_UNSIGNED_INDEX_V(rrp_face_index, rr_polygon.points.size(), Vector3());
  90. const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
  91. Vector3 face_random_position = face.get_random_point_inside();
  92. return face_random_position;
  93. } else {
  94. uint32_t rrp_polygon_index = Math::random(int(0), region_polygons.size() - 1);
  95. const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
  96. uint32_t rrp_face_index = Math::random(int(2), rr_polygon.points.size() - 1);
  97. const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
  98. Vector3 face_random_position = face.get_random_point_inside();
  99. return face_random_position;
  100. }
  101. }
  102. void NavMeshQueries3D::_query_task_create_same_polygon_two_point_path(NavMeshPathQueryTask3D &p_query_task, const gd::Polygon *begin_poly, Vector3 begin_point, const gd::Polygon *end_poly, Vector3 end_point) {
  103. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) {
  104. p_query_task.path_meta_point_types.resize(2);
  105. p_query_task.path_meta_point_types[0] = begin_poly->owner->get_type();
  106. p_query_task.path_meta_point_types[1] = end_poly->owner->get_type();
  107. }
  108. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) {
  109. p_query_task.path_meta_point_rids.resize(2);
  110. p_query_task.path_meta_point_rids[0] = begin_poly->owner->get_self();
  111. p_query_task.path_meta_point_rids[1] = end_poly->owner->get_self();
  112. }
  113. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) {
  114. p_query_task.path_meta_point_owners.resize(2);
  115. p_query_task.path_meta_point_owners[0] = begin_poly->owner->get_owner_id();
  116. p_query_task.path_meta_point_owners[1] = end_poly->owner->get_owner_id();
  117. }
  118. p_query_task.path_points.resize(2);
  119. p_query_task.path_points[0] = begin_point;
  120. p_query_task.path_points[1] = end_point;
  121. }
  122. void NavMeshQueries3D::_query_task_push_back_point_with_metadata(NavMeshPathQueryTask3D &p_query_task, Vector3 p_point, const gd::Polygon *p_point_polygon) {
  123. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) {
  124. p_query_task.path_meta_point_types.push_back(p_point_polygon->owner->get_type());
  125. }
  126. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) {
  127. p_query_task.path_meta_point_rids.push_back(p_point_polygon->owner->get_self());
  128. }
  129. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) {
  130. p_query_task.path_meta_point_owners.push_back(p_point_polygon->owner->get_owner_id());
  131. }
  132. p_query_task.path_points.push_back(p_point);
  133. }
  134. void NavMeshQueries3D::map_query_path(NavMap *map, const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result, const Callable &p_callback) {
  135. ERR_FAIL_NULL(map);
  136. ERR_FAIL_COND(p_query_parameters.is_null());
  137. ERR_FAIL_COND(p_query_result.is_null());
  138. using namespace NavigationUtilities;
  139. NavMeshQueries3D::NavMeshPathQueryTask3D query_task;
  140. query_task.start_position = p_query_parameters->get_start_position();
  141. query_task.target_position = p_query_parameters->get_target_position();
  142. query_task.navigation_layers = p_query_parameters->get_navigation_layers();
  143. query_task.callback = p_callback;
  144. switch (p_query_parameters->get_pathfinding_algorithm()) {
  145. case NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR: {
  146. query_task.pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  147. } break;
  148. default: {
  149. WARN_PRINT("No match for used PathfindingAlgorithm - fallback to default");
  150. query_task.pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  151. } break;
  152. }
  153. switch (p_query_parameters->get_path_postprocessing()) {
  154. case NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL: {
  155. query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  156. } break;
  157. case NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED: {
  158. query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED;
  159. } break;
  160. case NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_NONE: {
  161. query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_NONE;
  162. } break;
  163. default: {
  164. WARN_PRINT("No match for used PathPostProcessing - fallback to default");
  165. query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  166. } break;
  167. }
  168. query_task.metadata_flags = (int64_t)p_query_parameters->get_metadata_flags();
  169. query_task.simplify_path = p_query_parameters->get_simplify_path();
  170. query_task.simplify_epsilon = p_query_parameters->get_simplify_epsilon();
  171. query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_STARTED;
  172. map->query_path(query_task);
  173. const uint32_t path_point_size = query_task.path_points.size();
  174. Vector<Vector3> path_points;
  175. Vector<int32_t> path_meta_point_types;
  176. TypedArray<RID> path_meta_point_rids;
  177. Vector<int64_t> path_meta_point_owners;
  178. {
  179. path_points.resize(path_point_size);
  180. Vector3 *w = path_points.ptrw();
  181. const Vector3 *r = query_task.path_points.ptr();
  182. for (uint32_t i = 0; i < path_point_size; i++) {
  183. w[i] = r[i];
  184. }
  185. }
  186. if (query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) {
  187. path_meta_point_types.resize(path_point_size);
  188. int32_t *w = path_meta_point_types.ptrw();
  189. const int32_t *r = query_task.path_meta_point_types.ptr();
  190. for (uint32_t i = 0; i < path_point_size; i++) {
  191. w[i] = r[i];
  192. }
  193. }
  194. if (query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) {
  195. path_meta_point_rids.resize(path_point_size);
  196. for (uint32_t i = 0; i < path_point_size; i++) {
  197. path_meta_point_rids[i] = query_task.path_meta_point_rids[i];
  198. }
  199. }
  200. if (query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) {
  201. path_meta_point_owners.resize(path_point_size);
  202. int64_t *w = path_meta_point_owners.ptrw();
  203. const int64_t *r = query_task.path_meta_point_owners.ptr();
  204. for (uint32_t i = 0; i < path_point_size; i++) {
  205. w[i] = r[i];
  206. }
  207. }
  208. p_query_result->set_path(path_points);
  209. p_query_result->set_path_types(path_meta_point_types);
  210. p_query_result->set_path_rids(path_meta_point_rids);
  211. p_query_result->set_path_owner_ids(path_meta_point_owners);
  212. if (query_task.callback.is_valid()) {
  213. if (emit_callback(query_task.callback)) {
  214. query_task.status = NavMeshPathQueryTask3D::TaskStatus::CALLBACK_DISPATCHED;
  215. } else {
  216. query_task.status = NavMeshPathQueryTask3D::TaskStatus::CALLBACK_FAILED;
  217. }
  218. }
  219. }
  220. void NavMeshQueries3D::query_task_polygons_get_path(NavMeshPathQueryTask3D &p_query_task, const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_map_up, uint32_t p_link_polygons_size) {
  221. p_query_task.path_points.clear();
  222. p_query_task.path_meta_point_types.clear();
  223. p_query_task.path_meta_point_rids.clear();
  224. p_query_task.path_meta_point_owners.clear();
  225. // Find begin polyon and begin position closest to start position and
  226. // end polyon and end position closest to target position on the map.
  227. const gd::Polygon *begin_poly = nullptr;
  228. const gd::Polygon *end_poly = nullptr;
  229. Vector3 begin_point;
  230. Vector3 end_point;
  231. _query_task_find_start_end_positions(p_query_task, p_polygons, &begin_poly, begin_point, &end_poly, end_point);
  232. // Check for trivial cases
  233. if (!begin_poly || !end_poly) {
  234. p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FAILED;
  235. return;
  236. }
  237. if (begin_poly == end_poly) {
  238. _query_task_create_same_polygon_two_point_path(p_query_task, begin_poly, begin_point, end_poly, end_point);
  239. return;
  240. }
  241. _query_task_build_path_corridor(p_query_task, p_polygons, p_map_up, p_link_polygons_size, begin_poly, begin_point, end_poly, end_point);
  242. // Post-Process path.
  243. switch (p_query_task.path_postprocessing) {
  244. case PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL: {
  245. _path_corridor_post_process_corridorfunnel(p_query_task, p_query_task.least_cost_id, begin_poly, begin_point, end_poly, end_point, p_map_up);
  246. } break;
  247. case PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED: {
  248. _path_corridor_post_process_edgecentered(p_query_task, p_query_task.least_cost_id, begin_poly, begin_point, end_poly, end_point);
  249. } break;
  250. case PathPostProcessing::PATH_POSTPROCESSING_NONE: {
  251. _path_corridor_post_process_nopostprocessing(p_query_task, p_query_task.least_cost_id, begin_poly, begin_point, end_poly, end_point);
  252. } break;
  253. default: {
  254. WARN_PRINT("No match for used PathPostProcessing - fallback to default");
  255. _path_corridor_post_process_corridorfunnel(p_query_task, p_query_task.least_cost_id, begin_poly, begin_point, end_poly, end_point, p_map_up);
  256. } break;
  257. }
  258. p_query_task.path_points.invert();
  259. p_query_task.path_meta_point_types.invert();
  260. p_query_task.path_meta_point_rids.invert();
  261. p_query_task.path_meta_point_owners.invert();
  262. if (p_query_task.simplify_path) {
  263. _query_task_simplified_path_points(p_query_task);
  264. }
  265. #ifdef DEBUG_ENABLED
  266. // Ensure post conditions as path meta arrays if used MUST match in array size with the path points.
  267. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) {
  268. DEV_ASSERT(p_query_task.path_points.size() == p_query_task.path_meta_point_types.size());
  269. }
  270. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) {
  271. DEV_ASSERT(p_query_task.path_points.size() == p_query_task.path_meta_point_rids.size());
  272. }
  273. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) {
  274. DEV_ASSERT(p_query_task.path_points.size() == p_query_task.path_meta_point_owners.size());
  275. }
  276. #endif // DEBUG_ENABLED
  277. p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED;
  278. }
  279. void NavMeshQueries3D::_query_task_build_path_corridor(NavMeshPathQueryTask3D &p_query_task, const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_map_up, uint32_t p_link_polygons_size, const gd::Polygon *begin_poly, Vector3 begin_point, const gd::Polygon *end_poly, Vector3 end_point) {
  280. // List of all reachable navigation polys.
  281. LocalVector<gd::NavigationPoly> &navigation_polys = p_query_task.path_query_slot->path_corridor;
  282. for (gd::NavigationPoly &polygon : navigation_polys) {
  283. polygon.reset();
  284. }
  285. DEV_ASSERT(navigation_polys.size() == p_polygons.size() + p_link_polygons_size);
  286. // Initialize the matching navigation polygon.
  287. gd::NavigationPoly &begin_navigation_poly = navigation_polys[begin_poly->id];
  288. begin_navigation_poly.poly = begin_poly;
  289. begin_navigation_poly.entry = begin_point;
  290. begin_navigation_poly.back_navigation_edge_pathway_start = begin_point;
  291. begin_navigation_poly.back_navigation_edge_pathway_end = begin_point;
  292. // Heap of polygons to travel next.
  293. gd::Heap<gd::NavigationPoly *, gd::NavPolyTravelCostGreaterThan, gd::NavPolyHeapIndexer>
  294. &traversable_polys = p_query_task.path_query_slot->traversable_polys;
  295. traversable_polys.clear();
  296. traversable_polys.reserve(p_polygons.size() * 0.25);
  297. // This is an implementation of the A* algorithm.
  298. p_query_task.least_cost_id = begin_poly->id;
  299. int prev_least_cost_id = -1;
  300. bool found_route = false;
  301. const gd::Polygon *reachable_end = nullptr;
  302. real_t distance_to_reachable_end = FLT_MAX;
  303. bool is_reachable = true;
  304. while (true) {
  305. // Takes the current least_cost_poly neighbors (iterating over its edges) and compute the traveled_distance.
  306. for (const gd::Edge &edge : navigation_polys[p_query_task.least_cost_id].poly->edges) {
  307. // Iterate over connections in this edge, then compute the new optimized travel distance assigned to this polygon.
  308. for (uint32_t connection_index = 0; connection_index < edge.connections.size(); connection_index++) {
  309. const gd::Edge::Connection &connection = edge.connections[connection_index];
  310. // Only consider the connection to another polygon if this polygon is in a region with compatible layers.
  311. if ((p_query_task.navigation_layers & connection.polygon->owner->get_navigation_layers()) == 0) {
  312. continue;
  313. }
  314. const gd::NavigationPoly &least_cost_poly = navigation_polys[p_query_task.least_cost_id];
  315. real_t poly_enter_cost = 0.0;
  316. real_t poly_travel_cost = least_cost_poly.poly->owner->get_travel_cost();
  317. if (prev_least_cost_id != -1 && navigation_polys[prev_least_cost_id].poly->owner->get_self() != least_cost_poly.poly->owner->get_self()) {
  318. poly_enter_cost = least_cost_poly.poly->owner->get_enter_cost();
  319. }
  320. prev_least_cost_id = p_query_task.least_cost_id;
  321. Vector3 pathway[2] = { connection.pathway_start, connection.pathway_end };
  322. const Vector3 new_entry = Geometry3D::get_closest_point_to_segment(least_cost_poly.entry, pathway);
  323. const real_t new_traveled_distance = least_cost_poly.entry.distance_to(new_entry) * poly_travel_cost + poly_enter_cost + least_cost_poly.traveled_distance;
  324. // Check if the neighbor polygon has already been processed.
  325. gd::NavigationPoly &neighbor_poly = navigation_polys[connection.polygon->id];
  326. if (neighbor_poly.poly != nullptr) {
  327. // If the neighbor polygon hasn't been traversed yet and the new path leading to
  328. // it is shorter, update the polygon.
  329. if (neighbor_poly.traversable_poly_index < traversable_polys.size() &&
  330. new_traveled_distance < neighbor_poly.traveled_distance) {
  331. neighbor_poly.back_navigation_poly_id = p_query_task.least_cost_id;
  332. neighbor_poly.back_navigation_edge = connection.edge;
  333. neighbor_poly.back_navigation_edge_pathway_start = connection.pathway_start;
  334. neighbor_poly.back_navigation_edge_pathway_end = connection.pathway_end;
  335. neighbor_poly.traveled_distance = new_traveled_distance;
  336. neighbor_poly.distance_to_destination =
  337. new_entry.distance_to(end_point) *
  338. neighbor_poly.poly->owner->get_travel_cost();
  339. neighbor_poly.entry = new_entry;
  340. // Update the priority of the polygon in the heap.
  341. traversable_polys.shift(neighbor_poly.traversable_poly_index);
  342. }
  343. } else {
  344. // Initialize the matching navigation polygon.
  345. neighbor_poly.poly = connection.polygon;
  346. neighbor_poly.back_navigation_poly_id = p_query_task.least_cost_id;
  347. neighbor_poly.back_navigation_edge = connection.edge;
  348. neighbor_poly.back_navigation_edge_pathway_start = connection.pathway_start;
  349. neighbor_poly.back_navigation_edge_pathway_end = connection.pathway_end;
  350. neighbor_poly.traveled_distance = new_traveled_distance;
  351. neighbor_poly.distance_to_destination =
  352. new_entry.distance_to(end_point) *
  353. neighbor_poly.poly->owner->get_travel_cost();
  354. neighbor_poly.entry = new_entry;
  355. // Add the polygon to the heap of polygons to traverse next.
  356. traversable_polys.push(&neighbor_poly);
  357. }
  358. }
  359. }
  360. // When the heap of traversable polygons is empty at this point it means the end polygon is
  361. // unreachable.
  362. if (traversable_polys.is_empty()) {
  363. // Thus use the further reachable polygon
  364. ERR_BREAK_MSG(is_reachable == false, "It's not expect to not find the most reachable polygons");
  365. is_reachable = false;
  366. if (reachable_end == nullptr) {
  367. // The path is not found and there is not a way out.
  368. break;
  369. }
  370. // Set as end point the furthest reachable point.
  371. end_poly = reachable_end;
  372. real_t end_d = FLT_MAX;
  373. for (size_t point_id = 2; point_id < end_poly->points.size(); point_id++) {
  374. Face3 f(end_poly->points[0].pos, end_poly->points[point_id - 1].pos, end_poly->points[point_id].pos);
  375. Vector3 spoint = f.get_closest_point_to(p_query_task.target_position);
  376. real_t dpoint = spoint.distance_to(p_query_task.target_position);
  377. if (dpoint < end_d) {
  378. end_point = spoint;
  379. end_d = dpoint;
  380. }
  381. }
  382. // Search all faces of start polygon as well.
  383. bool closest_point_on_start_poly = false;
  384. for (size_t point_id = 2; point_id < begin_poly->points.size(); point_id++) {
  385. Face3 f(begin_poly->points[0].pos, begin_poly->points[point_id - 1].pos, begin_poly->points[point_id].pos);
  386. Vector3 spoint = f.get_closest_point_to(p_query_task.target_position);
  387. real_t dpoint = spoint.distance_to(p_query_task.target_position);
  388. if (dpoint < end_d) {
  389. end_point = spoint;
  390. end_d = dpoint;
  391. closest_point_on_start_poly = true;
  392. }
  393. }
  394. if (closest_point_on_start_poly) {
  395. _query_task_create_same_polygon_two_point_path(p_query_task, begin_poly, begin_point, end_poly, end_point);
  396. return;
  397. }
  398. for (gd::NavigationPoly &nav_poly : navigation_polys) {
  399. nav_poly.poly = nullptr;
  400. }
  401. navigation_polys[begin_poly->id].poly = begin_poly;
  402. p_query_task.least_cost_id = begin_poly->id;
  403. prev_least_cost_id = -1;
  404. reachable_end = nullptr;
  405. continue;
  406. }
  407. // Pop the polygon with the lowest travel cost from the heap of traversable polygons.
  408. p_query_task.least_cost_id = traversable_polys.pop()->poly->id;
  409. // Store the farthest reachable end polygon in case our goal is not reachable.
  410. if (is_reachable) {
  411. real_t distance = navigation_polys[p_query_task.least_cost_id].entry.distance_to(p_query_task.target_position);
  412. if (distance_to_reachable_end > distance) {
  413. distance_to_reachable_end = distance;
  414. reachable_end = navigation_polys[p_query_task.least_cost_id].poly;
  415. }
  416. }
  417. // Check if we reached the end
  418. if (navigation_polys[p_query_task.least_cost_id].poly == end_poly) {
  419. found_route = true;
  420. break;
  421. }
  422. }
  423. // We did not find a route but we have both a start polygon and an end polygon at this point.
  424. // Usually this happens because there was not a single external or internal connected edge, e.g. our start polygon is an isolated, single convex polygon.
  425. if (!found_route) {
  426. real_t end_d = FLT_MAX;
  427. // Search all faces of the start polygon for the closest point to our target position.
  428. for (size_t point_id = 2; point_id < begin_poly->points.size(); point_id++) {
  429. Face3 f(begin_poly->points[0].pos, begin_poly->points[point_id - 1].pos, begin_poly->points[point_id].pos);
  430. Vector3 spoint = f.get_closest_point_to(p_query_task.target_position);
  431. real_t dpoint = spoint.distance_to(p_query_task.target_position);
  432. if (dpoint < end_d) {
  433. end_point = spoint;
  434. end_d = dpoint;
  435. }
  436. }
  437. _query_task_create_same_polygon_two_point_path(p_query_task, begin_poly, begin_point, begin_poly, end_point);
  438. return;
  439. }
  440. }
  441. void NavMeshQueries3D::_query_task_simplified_path_points(NavMeshPathQueryTask3D &p_query_task) {
  442. if (!p_query_task.simplify_path || p_query_task.path_points.size() <= 2) {
  443. return;
  444. }
  445. const LocalVector<uint32_t> &simplified_path_indices = NavMeshQueries3D::get_simplified_path_indices(p_query_task.path_points, p_query_task.simplify_epsilon);
  446. uint32_t index_count = simplified_path_indices.size();
  447. {
  448. Vector3 *points_ptr = p_query_task.path_points.ptr();
  449. for (uint32_t i = 0; i < index_count; i++) {
  450. points_ptr[i] = points_ptr[simplified_path_indices[i]];
  451. }
  452. p_query_task.path_points.resize(index_count);
  453. }
  454. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) {
  455. int32_t *types_ptr = p_query_task.path_meta_point_types.ptr();
  456. for (uint32_t i = 0; i < index_count; i++) {
  457. types_ptr[i] = types_ptr[simplified_path_indices[i]];
  458. }
  459. p_query_task.path_meta_point_types.resize(index_count);
  460. }
  461. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) {
  462. RID *rids_ptr = p_query_task.path_meta_point_rids.ptr();
  463. for (uint32_t i = 0; i < index_count; i++) {
  464. rids_ptr[i] = rids_ptr[simplified_path_indices[i]];
  465. }
  466. p_query_task.path_meta_point_rids.resize(index_count);
  467. }
  468. if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) {
  469. int64_t *owners_ptr = p_query_task.path_meta_point_owners.ptr();
  470. for (uint32_t i = 0; i < index_count; i++) {
  471. owners_ptr[i] = owners_ptr[simplified_path_indices[i]];
  472. }
  473. p_query_task.path_meta_point_owners.resize(index_count);
  474. }
  475. }
  476. void NavMeshQueries3D::_path_corridor_post_process_corridorfunnel(NavMeshPathQueryTask3D &p_query_task, int p_least_cost_id, const gd::Polygon *p_begin_poly, Vector3 p_begin_point, const gd::Polygon *p_end_polygon, Vector3 p_end_point, const Vector3 &p_map_up) {
  477. LocalVector<gd::NavigationPoly> &p_path_corridor = p_query_task.path_query_slot->path_corridor;
  478. // Set the apex poly/point to the end point
  479. gd::NavigationPoly *apex_poly = &p_path_corridor[p_least_cost_id];
  480. Vector3 back_pathway[2] = { apex_poly->back_navigation_edge_pathway_start, apex_poly->back_navigation_edge_pathway_end };
  481. const Vector3 back_edge_closest_point = Geometry3D::get_closest_point_to_segment(p_end_point, back_pathway);
  482. if (p_end_point.is_equal_approx(back_edge_closest_point)) {
  483. // The end point is basically on top of the last crossed edge, funneling around the corners would at best do nothing.
  484. // At worst it would add an unwanted path point before the last point due to precision issues so skip to the next polygon.
  485. if (apex_poly->back_navigation_poly_id != -1) {
  486. apex_poly = &p_path_corridor[apex_poly->back_navigation_poly_id];
  487. }
  488. }
  489. Vector3 apex_point = p_end_point;
  490. gd::NavigationPoly *left_poly = apex_poly;
  491. Vector3 left_portal = apex_point;
  492. gd::NavigationPoly *right_poly = apex_poly;
  493. Vector3 right_portal = apex_point;
  494. gd::NavigationPoly *p = apex_poly;
  495. _query_task_push_back_point_with_metadata(p_query_task, p_end_point, p_end_polygon);
  496. while (p) {
  497. // Set left and right points of the pathway between polygons.
  498. Vector3 left = p->back_navigation_edge_pathway_start;
  499. Vector3 right = p->back_navigation_edge_pathway_end;
  500. if (THREE_POINTS_CROSS_PRODUCT(apex_point, left, right).dot(p_map_up) < 0) {
  501. SWAP(left, right);
  502. }
  503. bool skip = false;
  504. if (THREE_POINTS_CROSS_PRODUCT(apex_point, left_portal, left).dot(p_map_up) >= 0) {
  505. //process
  506. if (left_portal == apex_point || THREE_POINTS_CROSS_PRODUCT(apex_point, left, right_portal).dot(p_map_up) > 0) {
  507. left_poly = p;
  508. left_portal = left;
  509. } else {
  510. clip_path(p_query_task, p_path_corridor, apex_poly, right_portal, right_poly, p_map_up);
  511. apex_point = right_portal;
  512. p = right_poly;
  513. left_poly = p;
  514. apex_poly = p;
  515. left_portal = apex_point;
  516. right_portal = apex_point;
  517. _query_task_push_back_point_with_metadata(p_query_task, apex_point, apex_poly->poly);
  518. skip = true;
  519. }
  520. }
  521. if (!skip && THREE_POINTS_CROSS_PRODUCT(apex_point, right_portal, right).dot(p_map_up) <= 0) {
  522. //process
  523. if (right_portal == apex_point || THREE_POINTS_CROSS_PRODUCT(apex_point, right, left_portal).dot(p_map_up) < 0) {
  524. right_poly = p;
  525. right_portal = right;
  526. } else {
  527. clip_path(p_query_task, p_path_corridor, apex_poly, left_portal, left_poly, p_map_up);
  528. apex_point = left_portal;
  529. p = left_poly;
  530. right_poly = p;
  531. apex_poly = p;
  532. right_portal = apex_point;
  533. left_portal = apex_point;
  534. _query_task_push_back_point_with_metadata(p_query_task, apex_point, apex_poly->poly);
  535. }
  536. }
  537. // Go to the previous polygon.
  538. if (p->back_navigation_poly_id != -1) {
  539. p = &p_path_corridor[p->back_navigation_poly_id];
  540. } else {
  541. // The end
  542. p = nullptr;
  543. }
  544. }
  545. // If the last point is not the begin point, add it to the list.
  546. if (p_query_task.path_points[p_query_task.path_points.size() - 1] != p_begin_point) {
  547. _query_task_push_back_point_with_metadata(p_query_task, p_begin_point, p_begin_poly);
  548. }
  549. }
  550. void NavMeshQueries3D::_path_corridor_post_process_edgecentered(NavMeshPathQueryTask3D &p_query_task, int p_least_cost_id, const gd::Polygon *p_begin_poly, Vector3 p_begin_point, const gd::Polygon *p_end_polygon, Vector3 p_end_point) {
  551. LocalVector<gd::NavigationPoly> &p_path_corridor = p_query_task.path_query_slot->path_corridor;
  552. _query_task_push_back_point_with_metadata(p_query_task, p_end_point, p_end_polygon);
  553. // Add mid points.
  554. int np_id = p_least_cost_id;
  555. while (np_id != -1 && p_path_corridor[np_id].back_navigation_poly_id != -1) {
  556. if (p_path_corridor[np_id].back_navigation_edge != -1) {
  557. int prev = p_path_corridor[np_id].back_navigation_edge;
  558. int prev_n = (p_path_corridor[np_id].back_navigation_edge + 1) % p_path_corridor[np_id].poly->points.size();
  559. Vector3 point = (p_path_corridor[np_id].poly->points[prev].pos + p_path_corridor[np_id].poly->points[prev_n].pos) * 0.5;
  560. _query_task_push_back_point_with_metadata(p_query_task, point, p_path_corridor[np_id].poly);
  561. } else {
  562. _query_task_push_back_point_with_metadata(p_query_task, p_path_corridor[np_id].entry, p_path_corridor[np_id].poly);
  563. }
  564. np_id = p_path_corridor[np_id].back_navigation_poly_id;
  565. }
  566. _query_task_push_back_point_with_metadata(p_query_task, p_begin_point, p_begin_poly);
  567. }
  568. void NavMeshQueries3D::_path_corridor_post_process_nopostprocessing(NavMeshPathQueryTask3D &p_query_task, int p_least_cost_id, const gd::Polygon *p_begin_poly, Vector3 p_begin_point, const gd::Polygon *p_end_polygon, Vector3 p_end_point) {
  569. LocalVector<gd::NavigationPoly> &p_path_corridor = p_query_task.path_query_slot->path_corridor;
  570. _query_task_push_back_point_with_metadata(p_query_task, p_end_point, p_end_polygon);
  571. // Add mid points.
  572. int np_id = p_least_cost_id;
  573. while (np_id != -1 && p_path_corridor[np_id].back_navigation_poly_id != -1) {
  574. _query_task_push_back_point_with_metadata(p_query_task, p_path_corridor[np_id].entry, p_path_corridor[np_id].poly);
  575. np_id = p_path_corridor[np_id].back_navigation_poly_id;
  576. }
  577. _query_task_push_back_point_with_metadata(p_query_task, p_begin_point, p_begin_poly);
  578. }
  579. void NavMeshQueries3D::_query_task_find_start_end_positions(NavMeshPathQueryTask3D &p_query_task, const LocalVector<gd::Polygon> &p_polygons, const gd::Polygon **r_begin_poly, Vector3 &r_begin_point, const gd::Polygon **r_end_poly, Vector3 &r_end_point) {
  580. real_t begin_d = FLT_MAX;
  581. real_t end_d = FLT_MAX;
  582. // Find the initial poly and the end poly on this map.
  583. for (const gd::Polygon &p : p_polygons) {
  584. // Only consider the polygon if it in a region with compatible layers.
  585. if ((p_query_task.navigation_layers & p.owner->get_navigation_layers()) == 0) {
  586. continue;
  587. }
  588. // For each face check the distance between the origin/destination.
  589. for (size_t point_id = 2; point_id < p.points.size(); point_id++) {
  590. const Face3 face(p.points[0].pos, p.points[point_id - 1].pos, p.points[point_id].pos);
  591. Vector3 point = face.get_closest_point_to(p_query_task.start_position);
  592. real_t distance_to_point = point.distance_to(p_query_task.start_position);
  593. if (distance_to_point < begin_d) {
  594. begin_d = distance_to_point;
  595. *r_begin_poly = &p;
  596. r_begin_point = point;
  597. }
  598. point = face.get_closest_point_to(p_query_task.target_position);
  599. distance_to_point = point.distance_to(p_query_task.target_position);
  600. if (distance_to_point < end_d) {
  601. end_d = distance_to_point;
  602. *r_end_poly = &p;
  603. r_end_point = point;
  604. }
  605. }
  606. }
  607. }
  608. Vector3 NavMeshQueries3D::polygons_get_closest_point_to_segment(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) {
  609. bool use_collision = p_use_collision;
  610. Vector3 closest_point;
  611. real_t closest_point_distance = FLT_MAX;
  612. for (const gd::Polygon &polygon : p_polygons) {
  613. // For each face check the distance to the segment.
  614. for (size_t point_id = 2; point_id < polygon.points.size(); point_id += 1) {
  615. const Face3 face(polygon.points[0].pos, polygon.points[point_id - 1].pos, polygon.points[point_id].pos);
  616. Vector3 intersection_point;
  617. if (face.intersects_segment(p_from, p_to, &intersection_point)) {
  618. const real_t d = p_from.distance_to(intersection_point);
  619. if (!use_collision) {
  620. closest_point = intersection_point;
  621. use_collision = true;
  622. closest_point_distance = d;
  623. } else if (closest_point_distance > d) {
  624. closest_point = intersection_point;
  625. closest_point_distance = d;
  626. }
  627. }
  628. // If segment does not itersect face, check the distance from segment's endpoints.
  629. else if (!use_collision) {
  630. const Vector3 p_from_closest = face.get_closest_point_to(p_from);
  631. const real_t d_p_from = p_from.distance_to(p_from_closest);
  632. if (closest_point_distance > d_p_from) {
  633. closest_point = p_from_closest;
  634. closest_point_distance = d_p_from;
  635. }
  636. const Vector3 p_to_closest = face.get_closest_point_to(p_to);
  637. const real_t d_p_to = p_to.distance_to(p_to_closest);
  638. if (closest_point_distance > d_p_to) {
  639. closest_point = p_to_closest;
  640. closest_point_distance = d_p_to;
  641. }
  642. }
  643. }
  644. // Finally, check for a case when shortest distance is between some point located on a face's edge and some point located on a line segment.
  645. if (!use_collision) {
  646. for (size_t point_id = 0; point_id < polygon.points.size(); point_id += 1) {
  647. Vector3 a, b;
  648. Geometry3D::get_closest_points_between_segments(
  649. p_from,
  650. p_to,
  651. polygon.points[point_id].pos,
  652. polygon.points[(point_id + 1) % polygon.points.size()].pos,
  653. a,
  654. b);
  655. const real_t d = a.distance_to(b);
  656. if (d < closest_point_distance) {
  657. closest_point_distance = d;
  658. closest_point = b;
  659. }
  660. }
  661. }
  662. }
  663. return closest_point;
  664. }
  665. Vector3 NavMeshQueries3D::polygons_get_closest_point(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  666. gd::ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point);
  667. return cp.point;
  668. }
  669. Vector3 NavMeshQueries3D::polygons_get_closest_point_normal(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  670. gd::ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point);
  671. return cp.normal;
  672. }
  673. gd::ClosestPointQueryResult NavMeshQueries3D::polygons_get_closest_point_info(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  674. gd::ClosestPointQueryResult result;
  675. real_t closest_point_distance_squared = FLT_MAX;
  676. for (const gd::Polygon &polygon : p_polygons) {
  677. Vector3 plane_normal = (polygon.points[1].pos - polygon.points[0].pos).cross(polygon.points[2].pos - polygon.points[0].pos);
  678. Vector3 closest_on_polygon;
  679. real_t closest = FLT_MAX;
  680. bool inside = true;
  681. Vector3 previous = polygon.points[polygon.points.size() - 1].pos;
  682. for (size_t point_id = 0; point_id < polygon.points.size(); ++point_id) {
  683. Vector3 edge = polygon.points[point_id].pos - previous;
  684. Vector3 to_point = p_point - previous;
  685. Vector3 edge_to_point_pormal = edge.cross(to_point);
  686. bool clockwise = edge_to_point_pormal.dot(plane_normal) > 0;
  687. // If we are not clockwise, the point will never be inside the polygon and so the closest point will be on an edge.
  688. if (!clockwise) {
  689. inside = false;
  690. real_t point_projected_on_edge = edge.dot(to_point);
  691. real_t edge_square = edge.length_squared();
  692. if (point_projected_on_edge > edge_square) {
  693. real_t distance = polygon.points[point_id].pos.distance_squared_to(p_point);
  694. if (distance < closest) {
  695. closest_on_polygon = polygon.points[point_id].pos;
  696. closest = distance;
  697. }
  698. } else if (point_projected_on_edge < 0.f) {
  699. real_t distance = previous.distance_squared_to(p_point);
  700. if (distance < closest) {
  701. closest_on_polygon = previous;
  702. closest = distance;
  703. }
  704. } else {
  705. // If we project on this edge, this will be the closest point.
  706. real_t percent = point_projected_on_edge / edge_square;
  707. closest_on_polygon = previous + percent * edge;
  708. break;
  709. }
  710. }
  711. previous = polygon.points[point_id].pos;
  712. }
  713. if (inside) {
  714. Vector3 plane_normalized = plane_normal.normalized();
  715. real_t distance = plane_normalized.dot(p_point - polygon.points[0].pos);
  716. real_t distance_squared = distance * distance;
  717. if (distance_squared < closest_point_distance_squared) {
  718. closest_point_distance_squared = distance_squared;
  719. result.point = p_point - plane_normalized * distance;
  720. result.normal = plane_normal;
  721. result.owner = polygon.owner->get_self();
  722. if (Math::is_zero_approx(distance)) {
  723. break;
  724. }
  725. }
  726. } else {
  727. real_t distance = closest_on_polygon.distance_squared_to(p_point);
  728. if (distance < closest_point_distance_squared) {
  729. closest_point_distance_squared = distance;
  730. result.point = closest_on_polygon;
  731. result.normal = plane_normal;
  732. result.owner = polygon.owner->get_self();
  733. }
  734. }
  735. }
  736. return result;
  737. }
  738. RID NavMeshQueries3D::polygons_get_closest_point_owner(const LocalVector<gd::Polygon> &p_polygons, const Vector3 &p_point) {
  739. gd::ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point);
  740. return cp.owner;
  741. }
  742. void NavMeshQueries3D::clip_path(NavMeshPathQueryTask3D &p_query_task, const LocalVector<gd::NavigationPoly> &p_navigation_polys, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly, const Vector3 &p_map_up) {
  743. Vector3 from = p_query_task.path_points[p_query_task.path_points.size() - 1];
  744. if (from.is_equal_approx(p_to_point)) {
  745. return;
  746. }
  747. Plane cut_plane;
  748. cut_plane.normal = (from - p_to_point).cross(p_map_up);
  749. if (cut_plane.normal == Vector3()) {
  750. return;
  751. }
  752. cut_plane.normal.normalize();
  753. cut_plane.d = cut_plane.normal.dot(from);
  754. while (from_poly != p_to_poly) {
  755. Vector3 pathway_start = from_poly->back_navigation_edge_pathway_start;
  756. Vector3 pathway_end = from_poly->back_navigation_edge_pathway_end;
  757. ERR_FAIL_COND(from_poly->back_navigation_poly_id == -1);
  758. from_poly = &p_navigation_polys[from_poly->back_navigation_poly_id];
  759. if (!pathway_start.is_equal_approx(pathway_end)) {
  760. Vector3 inters;
  761. if (cut_plane.intersects_segment(pathway_start, pathway_end, &inters)) {
  762. if (!inters.is_equal_approx(p_to_point) && !inters.is_equal_approx(p_query_task.path_points[p_query_task.path_points.size() - 1])) {
  763. _query_task_push_back_point_with_metadata(p_query_task, inters, from_poly->poly);
  764. }
  765. }
  766. }
  767. }
  768. }
  769. LocalVector<uint32_t> NavMeshQueries3D::get_simplified_path_indices(const LocalVector<Vector3> &p_path, real_t p_epsilon) {
  770. p_epsilon = MAX(0.0, p_epsilon);
  771. real_t squared_epsilon = p_epsilon * p_epsilon;
  772. LocalVector<uint32_t> simplified_path_indices;
  773. simplified_path_indices.reserve(p_path.size());
  774. simplified_path_indices.push_back(0);
  775. simplify_path_segment(0, p_path.size() - 1, p_path, squared_epsilon, simplified_path_indices);
  776. simplified_path_indices.push_back(p_path.size() - 1);
  777. return simplified_path_indices;
  778. }
  779. void NavMeshQueries3D::simplify_path_segment(int p_start_inx, int p_end_inx, const LocalVector<Vector3> &p_points, real_t p_epsilon, LocalVector<uint32_t> &r_simplified_path_indices) {
  780. Vector3 path_segment[2] = { p_points[p_start_inx], p_points[p_end_inx] };
  781. real_t point_max_distance = 0.0;
  782. int point_max_index = 0;
  783. for (int i = p_start_inx; i < p_end_inx; i++) {
  784. const Vector3 &checked_point = p_points[i];
  785. const Vector3 closest_point = Geometry3D::get_closest_point_to_segment(checked_point, path_segment);
  786. real_t distance_squared = closest_point.distance_squared_to(checked_point);
  787. if (distance_squared > point_max_distance) {
  788. point_max_index = i;
  789. point_max_distance = distance_squared;
  790. }
  791. }
  792. if (point_max_distance > p_epsilon) {
  793. simplify_path_segment(p_start_inx, point_max_index, p_points, p_epsilon, r_simplified_path_indices);
  794. r_simplified_path_indices.push_back(point_max_index);
  795. simplify_path_segment(point_max_index, p_end_inx, p_points, p_epsilon, r_simplified_path_indices);
  796. }
  797. }
  798. #endif // _3D_DISABLED