Overseer
Graph.h
1 #ifndef _OVERSEER_GRAPH_H_
2 #define _OVERSEER_GRAPH_H_
3 
4 #include "Map.h"
5 #include "ChokePoint.h"
6 #include "Region.h"
7 #include <algorithm>
8 
9 namespace Overseer{
10 
11  class ChokePoint;
12 
13  typedef std::vector<ChokePoint> CPPath;
14  typedef std::tuple<size_t, size_t, size_t> ChokePointId;
15 
20  struct AstarNode {
27  AstarNode(ChokePointId cp_id, float f);
28 
29  ChokePointId choke_point_id;
30  float f_score;
31 
35  bool operator==(const AstarNode& rhs) const;
36  };
37 
42  struct LowerScore {
43  bool operator()(const AstarNode& lhs, const AstarNode& rhs) const;
44  };
45 
50  class Graph {
51 
52  public:
53 
57  Graph();
63  Graph(Map* map);
64 
72  std::vector<ChokePoint> getChokePoints(size_t region_id_a, size_t region_id_b) const;
73 
79  std::vector<ChokePoint> getChokePoints() const;
80 
87  ChokePoint getChokePoint(ChokePointId cp_id) const;
88 
96  CPPath& getPath(ChokePoint cp_a, ChokePoint cp_b);
97 
101  void createChokePoints();
102 
106  void computePaths();
107 
113  void setMap(Map *map);
114 
115  private:
116  size_t num_regions;
117  Map *p_map;
118  std::vector<std::vector<std::vector<ChokePoint>>> m_ChokePointsMatrix;
119  std::map<ChokePointId, std::map<ChokePointId, float>> m_ChokePointDistanceMap;
120  std::map<ChokePointId, std::vector<ChokePoint>> m_ChokePointNeighbors;
121  std::map<size_t, std::vector<ChokePoint>> m_RegionChokePoints;
122  std::map<ChokePointId, std::map<ChokePointId, CPPath>> m_ChokePointPaths;
123 
124  // Check if region id is valid
125  bool validId(size_t id_arg) const;
126  // Cluster the point within a chokepoint
127  std::vector<std::deque<TilePosition>>* createClustersFromFrontiers(std::pair<std::pair<size_t,size_t>, std::vector<TilePosition>> frontierByRegionPair);
128  // Caculate the distances between adjacent chokepoints
129  void initializeChokePointDistanceMap();
130  // Sets the distance between cp_a and cp_b, if not adjacent distance -> infinity.
131  void setChokePointDistance(ChokePointId cp_a, ChokePointId cp_b, float d);
132  // Gets the distance between chokepoints cp_a and cp_b
133  float getChokePointDistance(ChokePoint cp_a, ChokePoint cp_b);
134  // Sets the CPPath path to the chokepoints cp_a and cp_b
135  void setChokePointPath(ChokePoint cp_a, ChokePoint cp_b, CPPath path);
136  // Uses A* to calculate the shortest path between Chokepoints a and b
137  CPPath aStar(ChokePoint cp_a, ChokePoint cp_b);
138  // calculate the shortest euclidean distance between cp_a and cp_b
139  float bestHeuristic(ChokePoint cp_a, ChokePoint cp_b);
140  // Reconstructs the path from A* output
141  CPPath reconstructPath(std::map<ChokePointId, ChokePointId> came_from, ChokePoint current);
142  // Returns the chokepoints id is the output from A*
143  std::vector<ChokePointId> extractKeys(std::map<ChokePointId, ChokePointId> const& input_map);
144  };
145 }
146 
147 #endif /* _GRAPH_H_ */
comparator for priority queue lowest fscore at the top.
Definition: Graph.h:42
Definition: ChokePoint.cpp:3
AstarNode(ChokePointId cp_id, float f)
constructor sets chokepoint id and fscore.
Definition: Graph.cpp:10
Definition: Graph.h:20
is the overseer map that holds the most "important" functionality for a outside user.
Definition: Map.h:22
Class that is used as a chokepoint container with size and positioning on the map.
Definition: ChokePoint.h:17
bool operator==(const AstarNode &rhs) const
comparator for A* node is equal if chokepoint ids are equal
Definition: Graph.cpp:15
Is a mathematical "graphmap" for analysing.
Definition: Graph.h:50