Debian Codegraph @

modules/navigation_2d/nav_utils_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* nav_utils_2d.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
31 #pragma once
32
33 #include "core/math/vector3.h"
34 #include "core/object/ref_counted.h"
35 #include "core/templates/hash_map.h"
36 #include "core/templates/hashfuncs.h"
37 #include "core/templates/local_vector.h"
38 #include "servers/navigation_2d/navigation_constants_2d.h"
39
40 class NavBaseIteration2D;
41
42 namespace Nav2D {
43 struct Polygon;
44
45 union PointKey {
46 struct {
47 int64_t x : 32;
48 int64_t y : 32;
49 };
50
51 uint64_t key = 0;
52 };
53
54 struct EdgeKey {
55 PointKey a;
56 PointKey b;
57
58 static uint32_t hash(const EdgeKey &p_val) {
59 return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);
60 }
61
62 bool operator==(const EdgeKey &p_key) const {
63 return (a.key == p_key.a.key) && (b.key == p_key.b.key);
64 }
65
66 EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
67 a(p_a),
68 b(p_b) {
69 if (a.key > b.key) {
70 SWAP(a, b);
71 }
72 }
73 };
74
75 struct ConnectableEdge {
76 EdgeKey ek;
77 uint32_t polygon_index;
78 int edge = -1;
79 Vector2 pathway_start;
80 Vector2 pathway_end;
81 };
82
83 struct Connection {
84 /// Polygon that this connection leads to.
85 Polygon *polygon = nullptr;
86
87 /// Edge of the source polygon where this connection starts from.
88 int edge = -1;
89
90 /// Point on the edge where the gateway leading to the poly starts.
91 Vector2 pathway_start;
92
93 /// Point on the edge where the gateway leading to the poly ends.
94 Vector2 pathway_end;
95 };
96
97 struct Polygon {
98 uint32_t id = UINT32_MAX;
99
100 /// Navigation region or link that contains this polygon.
101 const NavBaseIteration2D *owner = nullptr;
102
103 LocalVector<Vector2> vertices;
104
105 real_t surface_area = 0.0;
106 };
107
108 struct NavigationPoly {
109 /// This poly.
110 const Polygon *poly = nullptr;
111
112 /// Index in the heap of traversable polygons.
113 uint32_t traversable_poly_index = UINT32_MAX;
114
115 /// Those 4 variables are used to travel the path backwards.
116 int back_navigation_poly_id = -1;
117 int back_navigation_edge = -1;
118 Vector2 back_navigation_edge_pathway_start;
119 Vector2 back_navigation_edge_pathway_end;
120
121 /// The entry position of this poly.
122 Vector2 entry;
123 /// The distance traveled until now (g cost).
124 real_t traveled_distance = 0.0;
125 /// The distance to the destination (h cost).
126 real_t distance_to_destination = 0.0;
127
128 /// The total travel cost (f cost).
129 real_t total_travel_cost() const {
130 return traveled_distance + distance_to_destination;
131 }
132
133 bool operator==(const NavigationPoly &p_other) const {
134 return poly == p_other.poly;
135 }
136
137 bool operator!=(const NavigationPoly &p_other) const {
138 return !(*this == p_other);
139 }
140
141 void reset() {
142 poly = nullptr;
143 traversable_poly_index = UINT32_MAX;
144 back_navigation_poly_id = -1;
145 back_navigation_edge = -1;
146 traveled_distance = FLT_MAX;
147 distance_to_destination = 0.0;
148 }
149 };
150
151 struct NavPolyTravelCostGreaterThan {
152 // Returns `true` if the travel cost of `a` is higher than that of `b`.
153 bool operator()(const NavigationPoly *p_poly_a, const NavigationPoly *p_poly_b) const {
154 real_t f_cost_a = p_poly_a->total_travel_cost();
155 real_t h_cost_a = p_poly_a->distance_to_destination;
156 real_t f_cost_b = p_poly_b->total_travel_cost();
157 real_t h_cost_b = p_poly_b->distance_to_destination;
158
159 if (f_cost_a != f_cost_b) {
160 return f_cost_a > f_cost_b;
161 } else {
162 return h_cost_a > h_cost_b;
163 }
164 }
165 };
166
167 struct NavPolyHeapIndexer {
168 void operator()(NavigationPoly *p_poly, uint32_t p_heap_index) const {
169 p_poly->traversable_poly_index = p_heap_index;
170 }
171 };
172
173 struct ClosestPointQueryResult {
174 Vector2 point;
175 RID owner;
176 };
177
178 struct EdgeConnectionPair {
179 Connection connections[2];
180 int size = 0;
181 };
182
183 struct PerformanceData {
184 int pm_region_count = 0;
185 int pm_agent_count = 0;
186 int pm_link_count = 0;
187 int pm_polygon_count = 0;
188 int pm_edge_count = 0;
189 int pm_edge_merge_count = 0;
190 int pm_edge_connection_count = 0;
191 int pm_edge_free_count = 0;
192 int pm_obstacle_count = 0;
193
194 void reset() {
195 pm_region_count = 0;
196 pm_agent_count = 0;
197 pm_link_count = 0;
198 pm_polygon_count = 0;
199 pm_edge_count = 0;
200 pm_edge_merge_count = 0;
201 pm_edge_connection_count = 0;
202 pm_edge_free_count = 0;
203 pm_obstacle_count = 0;
204 }
205 };
206
207 } //namespace Nav2D