Path: blob/master/modules/navigation_2d/nav_utils_2d.h
10277 views
/**************************************************************************/1/* nav_utils_2d.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 NavBaseIteration2D;4041namespace Nav2D {42struct Polygon;4344union PointKey {45struct {46int64_t x : 32;47int64_t y : 32;48};4950uint64_t key = 0;51};5253struct EdgeKey {54PointKey a;55PointKey b;5657static uint32_t hash(const EdgeKey &p_val) {58return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);59}6061bool operator==(const EdgeKey &p_key) const {62return (a.key == p_key.a.key) && (b.key == p_key.b.key);63}6465EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :66a(p_a),67b(p_b) {68if (a.key > b.key) {69SWAP(a, b);70}71}72};7374struct ConnectableEdge {75EdgeKey ek;76uint32_t polygon_index;77int edge = -1;78Vector2 pathway_start;79Vector2 pathway_end;80};8182struct Connection {83/// Polygon that this connection leads to.84Polygon *polygon = nullptr;8586/// Edge of the source polygon where this connection starts from.87int edge = -1;8889/// Point on the edge where the gateway leading to the poly starts.90Vector2 pathway_start;9192/// Point on the edge where the gateway leading to the poly ends.93Vector2 pathway_end;94};9596struct Polygon {97uint32_t id = UINT32_MAX;9899/// Navigation region or link that contains this polygon.100const NavBaseIteration2D *owner = nullptr;101102LocalVector<Vector2> vertices;103104real_t surface_area = 0.0;105};106107struct NavigationPoly {108/// This poly.109const Polygon *poly = nullptr;110111/// Index in the heap of traversable polygons.112uint32_t traversable_poly_index = UINT32_MAX;113114/// Those 4 variables are used to travel the path backwards.115int back_navigation_poly_id = -1;116int back_navigation_edge = -1;117Vector2 back_navigation_edge_pathway_start;118Vector2 back_navigation_edge_pathway_end;119120/// The entry position of this poly.121Vector2 entry;122/// The distance traveled until now (g cost).123real_t traveled_distance = 0.0;124/// The distance to the destination (h cost).125real_t distance_to_destination = 0.0;126127/// The total travel cost (f cost).128real_t total_travel_cost() const {129return traveled_distance + distance_to_destination;130}131132bool operator==(const NavigationPoly &p_other) const {133return poly == p_other.poly;134}135136bool operator!=(const NavigationPoly &p_other) const {137return !(*this == p_other);138}139140void reset() {141poly = nullptr;142traversable_poly_index = UINT32_MAX;143back_navigation_poly_id = -1;144back_navigation_edge = -1;145traveled_distance = FLT_MAX;146distance_to_destination = 0.0;147}148};149150struct NavPolyTravelCostGreaterThan {151// Returns `true` if the travel cost of `a` is higher than that of `b`.152bool operator()(const NavigationPoly *p_poly_a, const NavigationPoly *p_poly_b) const {153real_t f_cost_a = p_poly_a->total_travel_cost();154real_t h_cost_a = p_poly_a->distance_to_destination;155real_t f_cost_b = p_poly_b->total_travel_cost();156real_t h_cost_b = p_poly_b->distance_to_destination;157158if (f_cost_a != f_cost_b) {159return f_cost_a > f_cost_b;160} else {161return h_cost_a > h_cost_b;162}163}164};165166struct NavPolyHeapIndexer {167void operator()(NavigationPoly *p_poly, uint32_t p_heap_index) const {168p_poly->traversable_poly_index = p_heap_index;169}170};171172struct ClosestPointQueryResult {173Vector2 point;174RID owner;175};176177struct EdgeConnectionPair {178Connection connections[2];179int size = 0;180};181182struct PerformanceData {183int pm_region_count = 0;184int pm_agent_count = 0;185int pm_link_count = 0;186int pm_polygon_count = 0;187int pm_edge_count = 0;188int pm_edge_merge_count = 0;189int pm_edge_connection_count = 0;190int pm_edge_free_count = 0;191int pm_obstacle_count = 0;192193void reset() {194pm_region_count = 0;195pm_agent_count = 0;196pm_link_count = 0;197pm_polygon_count = 0;198pm_edge_count = 0;199pm_edge_merge_count = 0;200pm_edge_connection_count = 0;201pm_edge_free_count = 0;202pm_obstacle_count = 0;203}204};205206} //namespace Nav2D207208209