nav_mesh_queries_3d.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**************************************************************************/
  2. /* nav_mesh_queries_3d.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. #pragma once
  31. #include "../nav_utils_3d.h"
  32. #include "core/templates/a_hash_map.h"
  33. #include "servers/navigation/navigation_globals.h"
  34. #include "servers/navigation/navigation_path_query_parameters_3d.h"
  35. #include "servers/navigation/navigation_path_query_result_3d.h"
  36. #include "servers/navigation/navigation_utilities.h"
  37. using namespace NavigationUtilities;
  38. class NavMap3D;
  39. struct NavMapIteration3D;
  40. class NavMeshQueries3D {
  41. public:
  42. struct PathQuerySlot {
  43. LocalVector<Nav3D::NavigationPoly> path_corridor;
  44. Heap<Nav3D::NavigationPoly *, Nav3D::NavPolyTravelCostGreaterThan, Nav3D::NavPolyHeapIndexer> traversable_polys;
  45. bool in_use = false;
  46. uint32_t slot_index = 0;
  47. AHashMap<const Nav3D::Polygon *, uint32_t> poly_to_id;
  48. };
  49. struct NavMeshPathQueryTask3D {
  50. enum TaskStatus {
  51. QUERY_STARTED,
  52. QUERY_FINISHED,
  53. QUERY_FAILED,
  54. CALLBACK_DISPATCHED,
  55. CALLBACK_FAILED,
  56. };
  57. // Parameters.
  58. Vector3 start_position;
  59. Vector3 target_position;
  60. uint32_t navigation_layers;
  61. BitField<PathMetadataFlags> metadata_flags = PathMetadataFlags::PATH_INCLUDE_ALL;
  62. PathfindingAlgorithm pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
  63. PathPostProcessing path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
  64. bool simplify_path = false;
  65. real_t simplify_epsilon = 0.0;
  66. bool exclude_regions = false;
  67. bool include_regions = false;
  68. LocalVector<RID> excluded_regions;
  69. LocalVector<RID> included_regions;
  70. float path_return_max_length = 0.0;
  71. float path_return_max_radius = 0.0;
  72. int path_search_max_polygons = NavigationDefaults3D::path_search_max_polygons;
  73. float path_search_max_distance = 0.0;
  74. // Path building.
  75. Vector3 begin_position;
  76. Vector3 end_position;
  77. const Nav3D::Polygon *begin_polygon = nullptr;
  78. const Nav3D::Polygon *end_polygon = nullptr;
  79. uint32_t least_cost_id = 0;
  80. // Map.
  81. Vector3 map_up;
  82. NavMap3D *map = nullptr;
  83. PathQuerySlot *path_query_slot = nullptr;
  84. // Path points.
  85. LocalVector<Vector3> path_points;
  86. LocalVector<int32_t> path_meta_point_types;
  87. LocalVector<RID> path_meta_point_rids;
  88. LocalVector<int64_t> path_meta_point_owners;
  89. float path_length = 0.0;
  90. Ref<NavigationPathQueryParameters3D> query_parameters;
  91. Ref<NavigationPathQueryResult3D> query_result;
  92. Callable callback;
  93. NavMeshPathQueryTask3D::TaskStatus status = NavMeshPathQueryTask3D::TaskStatus::QUERY_STARTED;
  94. void path_clear() {
  95. path_points.clear();
  96. path_meta_point_types.clear();
  97. path_meta_point_rids.clear();
  98. path_meta_point_owners.clear();
  99. }
  100. void path_reverse() {
  101. path_points.reverse();
  102. path_meta_point_types.reverse();
  103. path_meta_point_rids.reverse();
  104. path_meta_point_owners.reverse();
  105. }
  106. };
  107. static bool emit_callback(const Callable &p_callback);
  108. static Vector3 polygons_get_random_point(const LocalVector<Nav3D::Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly);
  109. static Vector3 polygons_get_closest_point_to_segment(const LocalVector<Nav3D::Polygon> &p_polygons, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision);
  110. static Vector3 polygons_get_closest_point(const LocalVector<Nav3D::Polygon> &p_polygons, const Vector3 &p_point);
  111. static Vector3 polygons_get_closest_point_normal(const LocalVector<Nav3D::Polygon> &p_polygons, const Vector3 &p_point);
  112. static Nav3D::ClosestPointQueryResult polygons_get_closest_point_info(const LocalVector<Nav3D::Polygon> &p_polygons, const Vector3 &p_point);
  113. static RID polygons_get_closest_point_owner(const LocalVector<Nav3D::Polygon> &p_polygons, const Vector3 &p_point);
  114. static Vector3 map_iteration_get_closest_point_to_segment(const NavMapIteration3D &p_map_iteration, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision);
  115. static Vector3 map_iteration_get_closest_point(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point);
  116. static Vector3 map_iteration_get_closest_point_normal(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point);
  117. static RID map_iteration_get_closest_point_owner(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point);
  118. static Nav3D::ClosestPointQueryResult map_iteration_get_closest_point_info(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point);
  119. static Vector3 map_iteration_get_random_point(const NavMapIteration3D &p_map_iteration, uint32_t p_navigation_layers, bool p_uniformly);
  120. static void map_query_path(NavMap3D *map, const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result, const Callable &p_callback);
  121. static void query_task_map_iteration_get_path(NavMeshPathQueryTask3D &p_query_task, const NavMapIteration3D &p_map_iteration);
  122. static void _query_task_push_back_point_with_metadata(NavMeshPathQueryTask3D &p_query_task, const Vector3 &p_point, const Nav3D::Polygon *p_point_polygon);
  123. static void _query_task_find_start_end_positions(NavMeshPathQueryTask3D &p_query_task, const NavMapIteration3D &p_map_iteration);
  124. static void _query_task_build_path_corridor(NavMeshPathQueryTask3D &p_query_task, const NavMapIteration3D &p_map_iteration);
  125. static void _query_task_post_process_corridorfunnel(NavMeshPathQueryTask3D &p_query_task);
  126. static void _query_task_post_process_edgecentered(NavMeshPathQueryTask3D &p_query_task);
  127. static void _query_task_post_process_nopostprocessing(NavMeshPathQueryTask3D &p_query_task);
  128. static void _query_task_clip_path(NavMeshPathQueryTask3D &p_query_task, const Nav3D::NavigationPoly *from_poly, const Vector3 &p_to_point, const Nav3D::NavigationPoly *p_to_poly);
  129. static void _query_task_simplified_path_points(NavMeshPathQueryTask3D &p_query_task);
  130. static bool _query_task_is_connection_owner_usable(const NavMeshPathQueryTask3D &p_query_task, const NavBaseIteration3D *p_owner);
  131. static void _query_task_process_path_result_limits(NavMeshPathQueryTask3D &p_query_task);
  132. static void _query_task_search_polygon_connections(NavMeshPathQueryTask3D &p_query_task, const Nav3D::Connection &p_connection, uint32_t p_least_cost_id, const Nav3D::NavigationPoly &p_least_cost_poly, real_t p_poly_enter_cost, const Vector3 &p_end_point);
  133. static void 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);
  134. static LocalVector<uint32_t> get_simplified_path_indices(const LocalVector<Vector3> &p_path, real_t p_epsilon);
  135. static float _calculate_path_length(const LocalVector<Vector3> &p_path, uint32_t p_start_index, uint32_t p_end_index);
  136. };