NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
NavMesh.cpp
Go to the documentation of this file.
1
#include "
Engine/AI/Navigation/NavMesh.h
"
2
3
#include <unordered_set>
4
#include <unordered_map>
5
6
namespace
NoJob
7
{
8
NavVertexID
NavMesh::AddVertex
(
const
glm::vec3& position)
9
{
10
if
(m_Vertices.size() >=
InvalidNavVertexID
)
11
{
12
return
InvalidNavVertexID
;
13
}
14
15
const
NavVertexID
id
=
16
static_cast<
NavVertexID
>
(m_Vertices.size());
17
18
m_Vertices.push_back({ position });
19
20
return
id;
21
}
22
23
NavPolygonID
NavMesh::AddPolygon
(
24
const
std::vector<NavVertexID>& vertices,
25
float
cost)
26
{
27
// A polygon needs at least three vertices.
28
if
(vertices.size() < 3)
29
{
30
return
InvalidNavPolygonID
;
31
}
32
33
// Negative traversal penalties are not supported.
34
// Cost 0 means that the polygon adds no terrain penalty.
35
if
(cost < 0.0f)
36
{
37
return
InvalidNavPolygonID
;
38
}
39
40
// The polygon ID is its position inside m_Polygons.
41
// Keep the maximum uint32_t value reserved as the invalid sentinel.
42
if
(m_Polygons.size() >=
InvalidNavPolygonID
)
43
{
44
return
InvalidNavPolygonID
;
45
}
46
47
// Every referenced vertex must already exist and each vertex
48
// can only appear once in the polygon.
49
std::unordered_set<NavVertexID> uniqueVertices;
50
51
for
(
NavVertexID
vertexID : vertices)
52
{
53
if
(vertexID >= m_Vertices.size())
54
{
55
return
InvalidNavPolygonID
;
56
}
57
58
if
(!uniqueVertices.insert(vertexID).second)
59
{
60
return
InvalidNavPolygonID
;
61
}
62
}
63
64
NavPolygon
polygon;
65
polygon.
Vertices
= vertices;
66
polygon.
Cost
= cost;
67
68
polygon.
Edges
.reserve(vertices.size());
69
70
glm::vec3 center{ 0.0f };
71
72
for
(
size_t
i = 0; i < vertices.size(); ++i)
73
{
74
const
NavVertexID
startVertex = vertices[i];
75
const
NavVertexID
endVertex =
76
vertices[(i + 1) % vertices.size()];
77
78
NavEdge
edge;
79
edge.
StartVertex
= startVertex;
80
edge.
EndVertex
= endVertex;
81
82
polygon.
Edges
.push_back(edge);
83
84
center += m_Vertices[startVertex].Position;
85
}
86
87
polygon.
Center
=
88
center /
static_cast<
float
>
(vertices.size());
89
90
const
NavPolygonID
id
=
91
static_cast<
NavPolygonID
>
(m_Polygons.size());
92
93
m_Polygons.push_back(std::move(polygon));
94
95
return
id;
96
}
97
98
void
NavMesh::BuildAdjacency
()
99
{
100
// O(E) edge lookup instead of comparing every polygon pair.
101
std::unordered_map<std::uint64_t, std::pair<NavPolygonID, size_t>> edges;
102
for
(
NavPolygonID
id
= 0;
id
< m_Polygons.size(); ++id)
103
{
104
auto
& polygon = m_Polygons[id];
105
for
(
size_t
i = 0; i < polygon.Edges.size(); ++i)
106
{
107
auto
& edge = polygon.Edges[i];
108
edge.NeighborPolygon =
InvalidNavPolygonID
;
109
const
std::uint64_t reverse =
110
(std::uint64_t(edge.EndVertex) << 32) | edge.StartVertex;
111
auto
it = edges.find(reverse);
112
if
(it != edges.end())
113
{
114
edge.NeighborPolygon = it->second.first;
115
m_Polygons[it->second.first].Edges[it->second.second].NeighborPolygon = id;
116
}
117
else
118
{
119
const
std::uint64_t key =
120
(std::uint64_t(edge.StartVertex) << 32) | edge.EndVertex;
121
edges.emplace(key, std::make_pair(
id
, i));
122
}
123
}
124
}
125
}
126
127
const
NavVertex
*
NavMesh::GetVertex
(
NavVertexID
id
)
const
128
{
129
if
(
id
>= m_Vertices.size())
130
{
131
return
nullptr
;
132
}
133
134
return
&m_Vertices[id];
135
}
136
137
const
NavPolygon
*
NavMesh::GetPolygon
(
NavPolygonID
id
)
const
138
{
139
if
(
id
>= m_Polygons.size())
140
{
141
return
nullptr
;
142
}
143
144
return
&m_Polygons[id];
145
}
146
void
NavMesh::ReplacePolygons
(std::vector<NavPolygon> polygons)
147
{
148
m_Polygons = std::move(polygons);
149
BuildAdjacency
();
150
}
151
}
NavMesh.h
NavMesh declarations for the Navigation subsystem.
NoJob::NavMesh::GetVertex
const NavVertex * GetVertex(NavVertexID id) const
Definition
NavMesh.cpp:127
NoJob::NavMesh::AddVertex
NavVertexID AddVertex(const glm::vec3 &position)
Definition
NavMesh.cpp:8
NoJob::NavMesh::ReplacePolygons
void ReplacePolygons(std::vector< NavPolygon > polygons)
Definition
NavMesh.cpp:146
NoJob::NavMesh::AddPolygon
NavPolygonID AddPolygon(const std::vector< NavVertexID > &vertices, float cost=1.0f)
Definition
NavMesh.cpp:23
NoJob::NavMesh::BuildAdjacency
void BuildAdjacency()
Definition
NavMesh.cpp:98
NoJob::NavMesh::GetPolygon
const NavPolygon * GetPolygon(NavPolygonID id) const
Definition
NavMesh.cpp:137
NoJob
Definition
BTActionNode.h:13
NoJob::InvalidNavPolygonID
constexpr NavPolygonID InvalidNavPolygonID
Definition
NavMesh.h:23
NoJob::NavVertexID
uint32_t NavVertexID
Definition
NavMesh.h:17
NoJob::NavPolygonID
uint32_t NavPolygonID
Definition
NavMesh.h:18
NoJob::InvalidNavVertexID
constexpr NavVertexID InvalidNavVertexID
Definition
NavMesh.h:20
NoJob::NavEdge
Definition
NavMesh.h:32
NoJob::NavEdge::EndVertex
NavVertexID EndVertex
Definition
NavMesh.h:34
NoJob::NavEdge::StartVertex
NavVertexID StartVertex
Definition
NavMesh.h:33
NoJob::NavPolygon
Definition
NavMesh.h:43
NoJob::NavPolygon::Edges
std::vector< NavEdge > Edges
Definition
NavMesh.h:45
NoJob::NavPolygon::Vertices
std::vector< NavVertexID > Vertices
Definition
NavMesh.h:44
NoJob::NavPolygon::Cost
float Cost
Definition
NavMesh.h:49
NoJob::NavPolygon::Center
glm::vec3 Center
Definition
NavMesh.h:47
NoJob::NavVertex
Definition
NavMesh.h:27
Engine
AI
Navigation
NavMesh.cpp
Generated by
1.18.0