Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
VertexPathfinder.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_VERTEXPATHFINDER
19#define INCLUDED_VERTEXPATHFINDER
20
21#include "graphics/Overlay.h"
24
25#include <atomic>
26#include <vector>
27
28// A vertex around the corners of an obstruction
29// (paths will be sequences of these vertexes)
30struct Vertex
31{
32 enum
33 {
37 };
38
43 u8 quadInward : 4; // the quadrant which is inside the shape (or NONE)
44 u8 quadOutward : 4; // the quadrants of the next point on the path which this vertex must be in, given 'pred'
45};
46
47// Obstruction edges (paths will not cross any of these).
48// Defines the two points of the edge.
49struct Edge
50{
52};
53
54// Axis-aligned obstruction squares (paths will not cross any of these).
55// Defines the opposing corners of an axis-aligned square
56// (from which four individual edges can be trivially computed), requiring p0 <= p1
57struct Square
58{
60};
61
62// Axis-aligned obstruction edges.
63// p0 defines one end; c1 is either the X or Y coordinate of the other end,
64// depending on the context in which this is used.
65struct EdgeAA
66{
69};
70
72class CSimContext;
73class SceneCollector;
74
76{
77public:
78 VertexPathfinder(const u16& gridSize, Grid<NavcellData>* const & terrainOnlyGrid) : m_GridSize(gridSize), m_TerrainOnlyGrid(terrainOnlyGrid) {};
81
82 /**
83 * Compute a precise path from the given point to the goal, and return the set of waypoints.
84 * The path is based on the full set of obstructions that pass the filter, such that
85 * a unit of clearance 'clearance' will be able to follow the path with no collisions.
86 * The path is restricted to a box of radius 'range' from the starting point.
87 * Defined in CCmpPathfinder_Vertex.cpp
88 */
89 WaypointPath ComputeShortPath(const ShortPathRequest& request, CmpPtr<ICmpObstructionManager> cmpObstructionManager) const;
90
91private:
92
93 // References to the Pathfinder for convenience.
96
97 // These vectors are expensive to recreate on every call, so we cache them here.
98 // They are made mutable to allow using them in the otherwise const ComputeShortPath.
99
100 mutable std::vector<Edge> m_EdgesUnaligned;
101 mutable std::vector<EdgeAA> m_EdgesLeft;
102 mutable std::vector<EdgeAA> m_EdgesRight;
103 mutable std::vector<EdgeAA> m_EdgesBottom;
104 mutable std::vector<EdgeAA> m_EdgesTop;
105
106 // List of obstruction vertexes (plus start/end points); we'll try to find paths through
107 // the graph defined by these vertexes.
108 mutable std::vector<Vertex> m_Vertexes;
109 // List of collision edges - paths must never cross these.
110 // (Edges are one-sided so intersections are fine in one direction, but not the other direction.)
111 mutable std::vector<Edge> m_Edges;
112 mutable std::vector<Square> m_EdgeSquares; // Axis-aligned squares; equivalent to 4 edges.
113};
114
115/**
116 * There are several vertex pathfinders running asynchronously, so their debug output
117 * might conflict. To remain thread-safe, this single class will handle the debug data.
118 * NB: though threadsafe, the way things are setup means you can have a few
119 * more graphs and edges than you'd expect showing up in the rendered graph.
120 */
122{
123 friend class VertexPathfinder;
124public:
125 void SetDebugOverlay(bool enabled) { m_DebugOverlay = enabled; }
126 void RenderSubmit(SceneCollector& collector);
127protected:
128
129 void DebugRenderGoal(const CSimContext& simContext, const PathGoal& goal);
130 void DebugRenderGraph(const CSimContext& simContext, const std::vector<Vertex>& vertexes, const std::vector<Edge>& edges, const std::vector<Square>& edgeSquares);
131 void DebugRenderEdges(const CSimContext& simContext, bool visible, CFixedVector2D curr, CFixedVector2D npos);
132
133 std::atomic<bool> m_DebugOverlay = false;
134 // The data is double buffered: the first is the 'work-in-progress' state, the second the last RenderSubmit state.
135 std::vector<SOverlayLine> m_DebugOverlayShortPathLines;
136 std::vector<SOverlayLine> m_DebugOverlayShortPathLinesSubmitted;
137};
138
140
141#endif // INCLUDED_VERTEXPATHFINDER
VertexPathfinderDebugOverlay g_VertexPathfinderDebugOverlay
Definition: VertexPathfinder.cpp:53
Definition: FixedVector2D.h:25
A simple fixed-point number class.
Definition: Fixed.h:120
Contains pointers to various 'global' objects that are needed by the simulation code,...
Definition: SimContext.h:33
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:56
Obstruction manager: provides efficient spatial queries over objects in the world.
Definition: ICmpObstructionManager.h:61
Pathfinder goal.
Definition: PathGoal.h:33
This interface accepts renderable objects.
Definition: Scene.h:90
There are several vertex pathfinders running asynchronously, so their debug output might conflict.
Definition: VertexPathfinder.h:122
std::vector< SOverlayLine > m_DebugOverlayShortPathLinesSubmitted
Definition: VertexPathfinder.h:136
void RenderSubmit(SceneCollector &collector)
Definition: VertexPathfinder.cpp:1029
void DebugRenderGoal(const CSimContext &simContext, const PathGoal &goal)
Definition: VertexPathfinder.cpp:894
void SetDebugOverlay(bool enabled)
Definition: VertexPathfinder.h:125
void DebugRenderGraph(const CSimContext &simContext, const std::vector< Vertex > &vertexes, const std::vector< Edge > &edges, const std::vector< Square > &edgeSquares)
Definition: VertexPathfinder.cpp:929
std::atomic< bool > m_DebugOverlay
Definition: VertexPathfinder.h:133
void DebugRenderEdges(const CSimContext &simContext, bool visible, CFixedVector2D curr, CFixedVector2D npos)
Definition: VertexPathfinder.cpp:1006
std::vector< SOverlayLine > m_DebugOverlayShortPathLines
Definition: VertexPathfinder.h:135
Definition: VertexPathfinder.h:76
std::vector< Edge > m_EdgesUnaligned
Definition: VertexPathfinder.h:100
Grid< NavcellData > *const & m_TerrainOnlyGrid
Definition: VertexPathfinder.h:95
std::vector< Square > m_EdgeSquares
Definition: VertexPathfinder.h:112
VertexPathfinder(const u16 &gridSize, Grid< NavcellData > *const &terrainOnlyGrid)
Definition: VertexPathfinder.h:78
const u16 & m_GridSize
Definition: VertexPathfinder.h:94
VertexPathfinder(const VertexPathfinder &)=delete
std::vector< Edge > m_Edges
Definition: VertexPathfinder.h:111
std::vector< EdgeAA > m_EdgesBottom
Definition: VertexPathfinder.h:103
std::vector< EdgeAA > m_EdgesLeft
Definition: VertexPathfinder.h:101
std::vector< EdgeAA > m_EdgesRight
Definition: VertexPathfinder.h:102
std::vector< EdgeAA > m_EdgesTop
Definition: VertexPathfinder.h:104
std::vector< Vertex > m_Vertexes
Definition: VertexPathfinder.h:108
WaypointPath ComputeShortPath(const ShortPathRequest &request, CmpPtr< ICmpObstructionManager > cmpObstructionManager) const
Compute a precise path from the given point to the goal, and return the set of waypoints.
Definition: VertexPathfinder.cpp:534
VertexPathfinder(VertexPathfinder &&o)
Definition: VertexPathfinder.h:80
Definition: VertexPathfinder.h:66
fixed c1
Definition: VertexPathfinder.h:68
CFixedVector2D p0
Definition: VertexPathfinder.h:67
Definition: VertexPathfinder.h:50
CFixedVector2D p1
Definition: VertexPathfinder.h:51
CFixedVector2D p0
Definition: VertexPathfinder.h:51
Definition: Pathfinding.h:44
Definition: VertexPathfinder.h:58
CFixedVector2D p0
Definition: VertexPathfinder.h:59
CFixedVector2D p1
Definition: VertexPathfinder.h:59
Definition: VertexPathfinder.h:31
fixed h
Definition: VertexPathfinder.h:40
u8 status
Definition: VertexPathfinder.h:42
u16 pred
Definition: VertexPathfinder.h:41
u8 quadOutward
Definition: VertexPathfinder.h:44
u8 quadInward
Definition: VertexPathfinder.h:43
CFixedVector2D p
Definition: VertexPathfinder.h:39
fixed g
Definition: VertexPathfinder.h:40
@ UNEXPLORED
Definition: VertexPathfinder.h:34
@ CLOSED
Definition: VertexPathfinder.h:36
@ OPEN
Definition: VertexPathfinder.h:35
Returned path.
Definition: Pathfinding.h:67
uint8_t u8
Definition: types.h:37
uint16_t u16
Definition: types.h:38