VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
aStarGraph.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2012 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
9 
10 #pragma once
11 
14 #include <features/pathFeature.h>
17 
18 #include <vrfutil/asyncJob.h>
19 #include <vrfutil/kinematicTools.h>
20 #include <vrfutil/railSegment.h>
22 
23 #include <boost/graph/adjacency_list.hpp>
24 #include <boost/graph/properties.hpp>
25 #include <boost/graph/graph_traits.hpp>
26 #include <boost/function.hpp>
27 #include <boost/unordered_set.hpp>
28 
29 #include <tbb/queuing_mutex.h>
30 #include <tbb/queuing_rw_mutex.h>
31 #include <tbb/atomic.h>
32 #include <tbb/compat/condition_variable>
33 #include <tbb/mutex.h>
34 
35 #include <list>
36 #include <map>
37 
38 namespace MAKVRinTerra
39 {
40  class DtAStarGraphDebugger;
41 
42  //Properties of each vertex
44  {
45  explicit VertexInfo();
46  explicit VertexInfo(const DtPoint& point);
47 
49 
50  //DtFeatureGeometry::Point geometry() const;
51  };
52 
53  //Properties on each edge
55  {
56  explicit EdgeInfo(const DtPathFeature& p, const DtFeatureGeometry::Path&);
57  explicit EdgeInfo(const DtPathFeature::CPtr& p, const DtFeatureGeometry::Path&);
58 
59  typedef std::vector<DtPathFeature::CPtr> FeatureVector;
60 
61  DtFeatureGeometry::Path edgeGeometry() const;
62  double distance() const;
63  double width() const;
64  bool isOneway() const;
65  bool isDeleted() const;
66  FeatureVector features() const;
67  DtPathFeature::CPtr firstValidFeature() const;
68  void addFeature(const DtPathFeature::CPtr&);
69  size_t hash() const;
70 
71  private:
72  friend class DtAStarGraph;
73  struct Data;
74 
75  boost::shared_ptr<Data> myData;
76  };
77 
78  //DtAStarGraph processes path features to make a graph for path planning.
80  {
81  class BoostGraphVisitor;
82  friend class BoostGraphVisitor;
83 
87 
88  public:
89  typedef boost::shared_ptr<DtAStarGraph> Ptr;
90 
91  Ptr shared_from_this();
92 
93  typedef boost::adjacency_list< //graph is adjacency list (vs adjacency matrix)
94  boost::setS, //per-vertex edge-list is a set
95  boost::vecS, //graph's vertex-list is a vector
96  boost::directedS, //graph is directed
97  VertexInfo, //VertexInfo struct is property on edges
98  EdgeInfo, //EdgeInfo struct is property on edges
99  boost::no_property, //no property on graph
100  boost::vecS> //graph's edge-list is a vector
102 
103  typedef boost::graph_traits<DtBoostGraph> GraphTraits;
104 
105  typedef DtBoostGraph::vertex_descriptor vertex_descriptor;
106  typedef DtBoostGraph::edge_descriptor edge_descriptor;
107 
108  typedef GraphTraits::vertex_iterator vertex_iterator;
109  typedef GraphTraits::edge_iterator edge_iterator;
110 
111  typedef std::list<vertex_descriptor> VertexList;
112 
113  //Weight is a property on edges.
114  typedef boost::property<boost::edge_weight_t, double> Weight;
115 
121  {
122  typedef std::list<PathVertex> List;
123 
124  static double Distance(const List&);
125 
126  explicit PathVertex(
127  const DtPoint& l,
128  vertex_descriptor v = GraphTraits::null_vertex(),
130 
134  };
135 
137  {
138  typedef std::list<MultiPathVertex> List;
139 
140  explicit MultiPathVertex(
141  const DtPoint& l,
142  vertex_descriptor v = GraphTraits::null_vertex(),
144 
145  explicit MultiPathVertex(const PathVertex&);
146 
147  PathVertex::List getSinglePath() const;
148  void addPath(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
149  void addPathToFront(const PathVertex::List&);
150  void addPathToEnd(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
151  void addPathToEnd(const MultiPathVertex&);
152 
155  };
156 
158  {
159  public:
160  typedef std::vector<Path> Sequence;
161 
162  explicit Path(const std::string& = std::string());
163 
164  PathVertex::List getSinglePath() const;
165  void addPath(const PathVertex::List&);
166  void addPath(const MultiPathVertex::List&);
167  void addPath(const Path&);
168  void addPathToFront(const PathVertex::List&);
169  void addPathToEnd(const PathVertex::List&);
170 
171  bool empty() const { return myPathVertices.empty(); }
172  size_t numPaths() const { return myPathVertices.size(); }
173 
174  std::string description() const { return myDescription; }
175 
176  const MultiPathVertex::List& pathVertices() const { return myPathVertices; }
177  MultiPathVertex::List& pathVertices() { return myPathVertices; }
178 
179  private:
181  std::string myDescription;
182  };
183 
184  template <typename T>
186 
188 
189  typedef std::pair<DtQuery, double> DtWeightFactor;
190  typedef std::list<DtWeightFactor> DtWeightFactorsList;
191 
192  typedef boost::function<double (edge_descriptor)> EdgeWeightAdjustor;
193 
194  typedef boost::function<bool ()> CancelCheck;
195 
197  static DtRailSegmentList* DtPath2RailSegmentList(const Path& path);
198 
199  static Ptr Make(
200  std::auto_ptr<DtFeatureSet> input,
201  DtProj::CPtr localProj,
202  const DtTerrainInterfaceConfig& config,
203  std::string label = std::string());
204 
205  virtual ~DtAStarGraph();
206 
207  virtual void shutdown();
208  virtual bool isShutdown() const;
209 
220  Path makePath(
221  const DtPoint& startLocation,
222  const DtPoint& endLocation,
223  double maxSearchRadius,
224  double startProximityThreshold,
225  double stopProximityThreshold,
226  const DtWeightFactorsList& weightFactors,
227  const CancelCheck& cancelCheck);
228 
229  struct Results;
230 
232  {
235  boost::optional<DtFeatureGeometry::Area> searchArea;
240 
242  const DtFeatureGeometry::Point& start,
243  const DtFeatureGeometry::Point& end);
244 
245  void setMaxSearchRadius(double radius);
246  void setSearchRadius(double maxRadius, double minRadius);
247 
248  bool cancel() const;
249  };
250 
251  Path makePath(
252  SearchParameters params,
253  Results* results = 0,
254  unsigned recursionLevel = 0);
255 
258  bool onPathFeature(const DtPoint& location, double offset = 0.5);
259 
260  std::auto_ptr<DtPathFeatureSet> paths() const;
261 
262  const DtFeatureSet& inputFeatureSet() const;
263 
264  DtProj::CPtr localProj() const;
265 
275  DtClosestPathFeatureFinder closestFeatureOnNetwork(
276  const DtPoint& startingLocation,
277  const double& searchRadius,
278  bool* dataAvailable = NULL);
279 
280  std::string label() const;
281 
282  virtual DtFeaturesDebugger::Record& addToDebugger(DtFeaturesDebugger&);
283 
285  void addFeatureToGraph(
286  const DtFeature & r,
287  bool joinToIntersectingEdges,
288  const boost::optional<DtFeatureGeometry>& searchArea = boost::none_t());
289 
290  void addFeatureToGraph(
291  const DtFeature&,
292  const boost::optional<DtFeatureGeometry>& searchArea = boost::none_t());
293 
294  void addGeometryToGraph(DtFeatureGeometry g);
295 
296  bool loadTile(const DtPoint& localLocation, double timeout = 0);
297  bool loadTile(const DtFeatureGeometry& geom, double timeout = 0);
298  bool loadTileAndBlock(
299  const DtPoint& localLocation, const CancelCheck& = CancelCheck());
300  bool loadTileAndBlock(
301  const DtFeatureGeometry& geom, const CancelCheck& = CancelCheck());
302 
303  protected:
304  DtAStarGraph(
305  std::auto_ptr<DtFeatureSet> input,
306  DtProj::CPtr localProj,
307  const DtTerrainInterfaceConfig& config,
308  std::string label);
309 
311  boost::scoped_ptr<DtFeatureSet> myInputFeatureSet;
312 
315 
316  typedef tbb::queuing_rw_mutex GraphMutex;
317 
320 
323 
324  struct GridTile : public boost::enable_shared_from_this<GridTile>
325  {
326  typedef boost::shared_ptr<GridTile> Ptr;
327  typedef std::vector<Ptr> PtrVector;
328 
329  struct EdgeRecord
330  {
331  struct Hash
332  {
333  size_t operator()(const EdgeRecord& edge) const;
334  };
335 
336  bool operator==(const EdgeRecord& other) const;
337 
340 
341  explicit EdgeRecord(edge_descriptor, const EdgeInfo&);
342  };
343 
345  typedef std::vector<EdgeRecord> EdgeSet;
346  //typedef boost::unordered_set<EdgeRecord, EdgeRecord::Hash> EdgeSet;
347 
349  const unsigned level;
350  const unsigned maxLevel;
352 
353  explicit GridTile(const GridTile& parent, unsigned x, unsigned y);
354  explicit GridTile(DtAStarGraph&, const DtProj::Bounds&, unsigned maxLevel);
355 
356  ~GridTile();
357 
358  bool load(const DtFeatureGeometry&, double timeout);
359  void find(const DtFeatureGeometry&, PtrVector&);
360  void getTiles(PtrVector&);
361 
362  void addEdge(edge_descriptor, const EdgeInfo&);
363  void getEdges(const DtFeatureGeometry&, EdgeSet&);
364  void getEdgesWithWidth(const DtFeatureGeometry&, EdgeSet&);
365 
366  private:
367  void loadAndGet();
368  bool loaded(double timeout) const;
369 
371 
372  typedef tbb::mutex LoadingMutex;
373 
375  enum State { None, Loading, Done, Error };
376  tbb::atomic<State> state;
377 
378  typedef tbb::queuing_rw_mutex Mutex;
379 
380  mutable Mutex mutex;
383  boost::shared_ptr<GridTile> subtiles[2][2];
384 
385  mutable Mutex edgesMutex;
387  };
388 
389  friend struct GridTile;
390 
393 
395 
396  tbb::atomic<bool> myShutdownFlag;
397 
398  std::string myLabel;
399  };
400 
401  template <typename T>
403  {
404  typedef std::vector<T> Sequence;
405 
406  static T CreateDefault(T def, size_t) { return def; }
407 
408  public:
409  typedef size_t key_type;
410  typedef typename Sequence::value_type value_type;
411  typedef typename Sequence::reference reference;
412  typedef boost::lvalue_property_map_tag category;
413 
414  typedef boost::function<T (size_t)> DefaultFactory;
415 
416  explicit DynamicPropertyMap(const DefaultFactory& def, size_t size = 0)
417  : myDefaultFactory(def)
418  , myValues(new Sequence(size))
419  {
420  for (size_t i=0; i != size; ++i)
421  (*myValues)[i] = myDefaultFactory(i);
422  }
423 
424  explicit DynamicPropertyMap(T def, size_t size = 0)
425  : myDefaultFactory(boost::bind(&CreateDefault, def, _1))
426  , myValues(new Sequence(size, def))
427  {
428  }
429 
430  T& operator[](size_t index)
431  {
432  while (index >= size())
433  myValues->push_back(myDefaultFactory(index));
434 
435  return (*myValues)[index];
436  }
437 
438  T& operator[](size_t index) const
439  {
440  while (index >= size())
441  myValues->push_back(myDefaultFactory(index));
442 
443  return (*myValues)[index];
444  }
445 
446  size_t size() const
447  {
448  return myValues->size();
449  }
450 
451  void clear()
452  {
453  myValues->clear();
454  }
455 
456  private:
458  boost::shared_ptr<Sequence> myValues;
459  };
460 
461  template <typename T>
462  T get(const DtAStarGraph::DynamicPropertyMap<T>& map, size_t index)
463  {
464  return map[index];
465  }
466 
472 
473  template <typename T>
474  void put(DtAStarGraph::DynamicPropertyMap<T>& map, size_t index, const T& value)
475  {
476  map[index] = value;
477  }
478 
484 
486  {
487  public:
489  };
490 
492  {
493  public:
495  const DtAStarGraph::Ptr& pathFinder,
496  const DtPoint& localStart,
497  const DtPoint& localDestination,
498  double maxSearchRadius,
499  double startProximityThreshold,
500  double stopProximityThreshold,
501  const DtAStarGraph::DtWeightFactorsList& factors);
502 
503  task* onExecute();
504  task* onException();
505 
506  public:
514  };
515 }
516 

Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)