Path: blob/master/modules/navigation_3d/nav_utils_3d.h
10277 views
/**************************************************************************/1/* nav_utils_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 "core/math/vector3.h"33#include "core/object/ref_counted.h"34#include "core/templates/hash_map.h"35#include "core/templates/hashfuncs.h"36#include "servers/navigation/nav_heap.h"37#include "servers/navigation/navigation_utilities.h"3839class NavBaseIteration3D;4041namespace Nav3D {42struct Polygon;4344union PointKey {45struct {46int64_t x : 21;47int64_t y : 22;48int64_t z : 21;49};5051uint64_t key = 0;52};5354struct EdgeKey {55PointKey a;56PointKey b;5758static uint32_t hash(const EdgeKey &p_val) {59return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);60}6162bool operator==(const EdgeKey &p_key) const {63return (a.key == p_key.a.key) && (b.key == p_key.b.key);64}6566EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :67a(p_a),68b(p_b) {69if (a.key > b.key) {70SWAP(a, b);71}72}73};7475struct ConnectableEdge {76EdgeKey ek;77uint32_t polygon_index;78int edge = -1;79Vector3 pathway_start;80Vector3 pathway_end;81};8283struct Connection {84/// Polygon that this connection leads to.85Polygon *polygon = nullptr;8687/// Edge of the source polygon where this connection starts from.88int edge = -1;8990/// Point on the edge where the gateway leading to the poly starts.91Vector3 pathway_start;9293/// Point on the edge where the gateway leading to the poly ends.94Vector3 pathway_end;95};9697struct Polygon {98uint32_t id = UINT32_MAX;99100/// Navigation region or link that contains this polygon.101const NavBaseIteration3D *owner = nullptr;102103LocalVector<Vector3> vertices;104105real_t surface_area = 0.0;106};107108struct NavigationPoly {109/// This poly.110const Polygon *poly = nullptr;111112/// Index in the heap of traversable polygons.113uint32_t traversable_poly_index = UINT32_MAX;114115/// Those 4 variables are used to travel the path backwards.116int back_navigation_poly_id = -1;117int back_navigation_edge = -1;118Vector3 back_navigation_edge_pathway_start;119Vector3 back_navigation_edge_pathway_end;120121/// The entry position of this poly.122Vector3 entry;123/// The distance traveled until now (g cost).124real_t traveled_distance = 0.0;125/// The distance to the destination (h cost).126real_t distance_to_destination = 0.0;127128/// The total travel cost (f cost).129real_t total_travel_cost() const {130return traveled_distance + distance_to_destination;131}132133bool operator==(const NavigationPoly &p_other) const {134return poly == p_other.poly;135}136137bool operator!=(const NavigationPoly &p_other) const {138return !(*this == p_other);139}140141void reset() {142poly = nullptr;143traversable_poly_index = UINT32_MAX;144back_navigation_poly_id = -1;145back_navigation_edge = -1;146traveled_distance = FLT_MAX;147distance_to_destination = 0.0;148}149};150151struct NavPolyTravelCostGreaterThan {152// Returns `true` if the travel cost of `a` is higher than that of `b`.153bool operator()(const NavigationPoly *p_poly_a, const NavigationPoly *p_poly_b) const {154real_t f_cost_a = p_poly_a->total_travel_cost();155real_t h_cost_a = p_poly_a->distance_to_destination;156real_t f_cost_b = p_poly_b->total_travel_cost();157real_t h_cost_b = p_poly_b->distance_to_destination;158159if (f_cost_a != f_cost_b) {160return f_cost_a > f_cost_b;161} else {162return h_cost_a > h_cost_b;163}164}165};166167struct NavPolyHeapIndexer {168void operator()(NavigationPoly *p_poly, uint32_t p_heap_index) const {169p_poly->traversable_poly_index = p_heap_index;170}171};172173struct ClosestPointQueryResult {174Vector3 point;175Vector3 normal;176RID owner;177};178179struct EdgeConnectionPair {180Connection connections[2];181int size = 0;182};183184struct PerformanceData {185int pm_region_count = 0;186int pm_agent_count = 0;187int pm_link_count = 0;188int pm_polygon_count = 0;189int pm_edge_count = 0;190int pm_edge_merge_count = 0;191int pm_edge_connection_count = 0;192int pm_edge_free_count = 0;193int pm_obstacle_count = 0;194195void reset() {196pm_region_count = 0;197pm_agent_count = 0;198pm_link_count = 0;199pm_polygon_count = 0;200pm_edge_count = 0;201pm_edge_merge_count = 0;202pm_edge_connection_count = 0;203pm_edge_free_count = 0;204pm_obstacle_count = 0;205}206};207208} // namespace Nav3D209210211