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