Path: blob/master/modules/navigation_3d/nav_map_3d.h
10277 views
/**************************************************************************/1/* nav_map_3d.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "3d/nav_map_iteration_3d.h"33#include "3d/nav_mesh_queries_3d.h"34#include "nav_rid_3d.h"35#include "nav_utils_3d.h"3637#include "core/math/math_defs.h"38#include "core/object/worker_thread_pool.h"39#include "servers/navigation/navigation_globals.h"4041#include <KdTree2d.h>42#include <KdTree3d.h>43#include <RVOSimulator2d.h>44#include <RVOSimulator3d.h>4546class NavLink3D;47class NavRegion3D;48class NavAgent3D;49class NavObstacle3D;5051class NavMap3D : public NavRid3D {52/// Map Up53Vector3 up = Vector3(0, 1, 0);5455/// To find the polygons edges the vertices are displaced in a grid where56/// each cell has the following cell_size and cell_height.57real_t cell_size = NavigationDefaults3D::NAV_MESH_CELL_SIZE;58real_t cell_height = NavigationDefaults3D::NAV_MESH_CELL_HEIGHT;5960// For the inter-region merging to work, internal rasterization is performed.61Vector3 merge_rasterizer_cell_size = Vector3(cell_size, cell_height, cell_size);6263// This value is used to control sensitivity of internal rasterizer.64float merge_rasterizer_cell_scale = 1.0;6566bool use_edge_connections = true;67/// This value is used to detect the near edges to connect.68real_t edge_connection_margin = NavigationDefaults3D::EDGE_CONNECTION_MARGIN;6970/// This value is used to limit how far links search to find polygons to connect to.71real_t link_connection_radius = NavigationDefaults3D::LINK_CONNECTION_RADIUS;7273bool map_settings_dirty = true;7475/// Map regions76LocalVector<NavRegion3D *> regions;7778/// Map links79LocalVector<NavLink3D *> links;8081/// RVO avoidance worlds82RVO2D::RVOSimulator2D rvo_simulation_2d;83RVO3D::RVOSimulator3D rvo_simulation_3d;8485/// avoidance controlled agents86LocalVector<NavAgent3D *> active_2d_avoidance_agents;87LocalVector<NavAgent3D *> active_3d_avoidance_agents;8889/// dirty flag when one of the agent's arrays are modified90bool agents_dirty = true;9192/// All the Agents (even the controlled one)93LocalVector<NavAgent3D *> agents;9495/// All the avoidance obstacles (both static and dynamic)96LocalVector<NavObstacle3D *> obstacles;9798/// Are rvo obstacles modified?99bool obstacles_dirty = true;100101/// Change the id each time the map is updated.102uint32_t iteration_id = 0;103104bool use_threads = true;105bool avoidance_use_multiple_threads = true;106bool avoidance_use_high_priority_threads = true;107108// Performance Monitor109Nav3D::PerformanceData performance_data;110111struct {112struct {113RWLock rwlock;114SelfList<NavRegion3D>::List list;115} regions;116struct {117RWLock rwlock;118SelfList<NavLink3D>::List list;119} links;120struct {121RWLock rwlock;122SelfList<NavAgent3D>::List list;123} agents;124struct {125RWLock rwlock;126SelfList<NavObstacle3D>::List list;127} obstacles;128} sync_dirty_requests;129130struct {131struct {132RWLock rwlock;133SelfList<NavRegion3D>::List list;134} regions;135} async_dirty_requests;136137int path_query_slots_max = 4;138139bool use_async_iterations = true;140141uint32_t iteration_slot_index = 0;142LocalVector<NavMapIteration3D> iteration_slots;143mutable RWLock iteration_slot_rwlock;144145NavMapIterationBuild3D iteration_build;146WorkerThreadPool::TaskID iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;147static void _build_iteration_threaded(void *p_arg);148149bool iteration_dirty = true;150bool iteration_building = false;151bool iteration_ready = false;152153void _build_iteration();154void _sync_iteration();155156public:157NavMap3D();158~NavMap3D();159160uint32_t get_iteration_id() const { return iteration_id; }161162void set_up(Vector3 p_up);163Vector3 get_up() const {164return up;165}166167void set_cell_size(real_t p_cell_size);168real_t get_cell_size() const {169return cell_size;170}171172void set_cell_height(real_t p_cell_height);173real_t get_cell_height() const { return cell_height; }174175void set_merge_rasterizer_cell_scale(float p_value);176float get_merge_rasterizer_cell_scale() const {177return merge_rasterizer_cell_scale;178}179180void set_use_edge_connections(bool p_enabled);181bool get_use_edge_connections() const {182return use_edge_connections;183}184185void set_edge_connection_margin(real_t p_edge_connection_margin);186real_t get_edge_connection_margin() const {187return edge_connection_margin;188}189190void set_link_connection_radius(real_t p_link_connection_radius);191real_t get_link_connection_radius() const {192return link_connection_radius;193}194195Nav3D::PointKey get_point_key(const Vector3 &p_pos) const;196const Vector3 &get_merge_rasterizer_cell_size() const;197198void query_path(NavMeshQueries3D::NavMeshPathQueryTask3D &p_query_task);199200Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;201Vector3 get_closest_point(const Vector3 &p_point) const;202Vector3 get_closest_point_normal(const Vector3 &p_point) const;203Nav3D::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;204RID get_closest_point_owner(const Vector3 &p_point) const;205206void add_region(NavRegion3D *p_region);207void remove_region(NavRegion3D *p_region);208const LocalVector<NavRegion3D *> &get_regions() const {209return regions;210}211212void add_link(NavLink3D *p_link);213void remove_link(NavLink3D *p_link);214const LocalVector<NavLink3D *> &get_links() const {215return links;216}217218bool has_agent(NavAgent3D *agent) const;219void add_agent(NavAgent3D *agent);220void remove_agent(NavAgent3D *agent);221const LocalVector<NavAgent3D *> &get_agents() const {222return agents;223}224225void set_agent_as_controlled(NavAgent3D *agent);226void remove_agent_as_controlled(NavAgent3D *agent);227228bool has_obstacle(NavObstacle3D *obstacle) const;229void add_obstacle(NavObstacle3D *obstacle);230void remove_obstacle(NavObstacle3D *obstacle);231const LocalVector<NavObstacle3D *> &get_obstacles() const {232return obstacles;233}234235Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;236237void sync();238void step(double p_delta_time);239void dispatch_callbacks();240241// Performance Monitor242int get_pm_region_count() const { return performance_data.pm_region_count; }243int get_pm_agent_count() const { return performance_data.pm_agent_count; }244int get_pm_link_count() const { return performance_data.pm_link_count; }245int get_pm_polygon_count() const { return performance_data.pm_polygon_count; }246int get_pm_edge_count() const { return performance_data.pm_edge_count; }247int get_pm_edge_merge_count() const { return performance_data.pm_edge_merge_count; }248int get_pm_edge_connection_count() const { return performance_data.pm_edge_connection_count; }249int get_pm_edge_free_count() const { return performance_data.pm_edge_free_count; }250int get_pm_obstacle_count() const { return performance_data.pm_obstacle_count; }251252int get_region_connections_count(NavRegion3D *p_region) const;253Vector3 get_region_connection_pathway_start(NavRegion3D *p_region, int p_connection_id) const;254Vector3 get_region_connection_pathway_end(NavRegion3D *p_region, int p_connection_id) const;255256void add_region_async_thread_join_request(SelfList<NavRegion3D> *p_async_request);257void remove_region_async_thread_join_request(SelfList<NavRegion3D> *p_async_request);258259void add_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request);260void add_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request);261void add_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request);262void add_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request);263264void remove_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request);265void remove_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request);266void remove_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request);267void remove_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request);268269void set_use_async_iterations(bool p_enabled);270bool get_use_async_iterations() const;271272private:273void _sync_dirty_map_update_requests();274void _sync_dirty_avoidance_update_requests();275void _sync_async_tasks();276277void compute_single_step(uint32_t index, NavAgent3D **agent);278279void compute_single_avoidance_step_2d(uint32_t index, NavAgent3D **agent);280void compute_single_avoidance_step_3d(uint32_t index, NavAgent3D **agent);281282void _sync_avoidance();283void _update_rvo_simulation();284void _update_rvo_obstacles_tree_2d();285void _update_rvo_agents_tree_2d();286void _update_rvo_agents_tree_3d();287288void _update_merge_rasterizer_cell_dimensions();289};290291292