VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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>
18 
19 #include <vrfutil/asyncJob.h>
20 #include <vrfutil/kinematicTools.h>
21 #include <vrfutil/railSegment.h>
23 
24 #include <boost/graph/adjacency_list.hpp>
25 #include <boost/graph/properties.hpp>
26 #include <boost/graph/graph_traits.hpp>
27 #include <boost/function.hpp>
28 #include <boost/unordered_set.hpp>
29 
30 #include <tbb/queuing_mutex.h>
31 #include <tbb/queuing_rw_mutex.h>
32 #include <tbb/atomic.h>
33 #include <tbb/compat/condition_variable>
34 #include <tbb/mutex.h>
35 
36 #include <list>
37 #include <map>
38 
39 namespace MAKVRinTerra
40 {
41  class DtAStarGraphDebugger;
42 
43  //Properties of each vertex
45  {
46  explicit VertexInfo();
47  explicit VertexInfo(const DtPoint& point);
48 
50 
51  //DtFeatureGeometry::Point geometry() const;
52  };
53 
54  //Properties on each edge
56  {
57  explicit EdgeInfo(const DtPathFeature& p, const DtFeatureGeometry::Path&);
58  explicit EdgeInfo(const DtPathFeature::CPtr& p, const DtFeatureGeometry::Path&);
59 
60  typedef std::vector<DtPathFeature::CPtr> FeatureVector;
61 
62  DtFeatureGeometry::Path edgeGeometry() const;
63  double distance() const;
64  double width() const;
65  bool isOneway() const;
66  bool isDeleted() const;
67  FeatureVector features() const;
68  DtPathFeature::CPtr firstValidFeature() const;
69  void addFeature(const DtPathFeature::CPtr&);
70  size_t hash() const;
71 
72  private:
73  friend class DtAStarGraph;
74  struct Data;
75 
76  boost::shared_ptr<Data> myData;
77  };
78 
79  //DtAStarGraph processes path features to make a graph for path planning.
81  {
82  class BoostGraphVisitor;
83  friend class BoostGraphVisitor;
84 
85  struct RedrawCallback;
86  friend struct RedrawCallback;
87 
91 
92  public:
93  typedef boost::shared_ptr<DtAStarGraph> Ptr;
94 
95  Ptr shared_from_this();
96 
97  typedef boost::adjacency_list< //graph is adjacency list (vs adjacency matrix)
98  boost::setS, //per-vertex edge-list is a set
99  boost::vecS, //graph's vertex-list is a vector
100  boost::directedS, //graph is directed
101  VertexInfo, //VertexInfo struct is property on edges
102  EdgeInfo, //EdgeInfo struct is property on edges
103  boost::no_property, //no property on graph
104  boost::vecS> //graph's edge-list is a vector
106 
107  typedef boost::graph_traits<DtBoostGraph> GraphTraits;
108 
109  typedef DtBoostGraph::vertex_descriptor vertex_descriptor;
110  typedef DtBoostGraph::edge_descriptor edge_descriptor;
111 
112  typedef GraphTraits::vertex_iterator vertex_iterator;
113  typedef GraphTraits::edge_iterator edge_iterator;
114 
115  typedef std::list<vertex_descriptor> VertexList;
116 
117  //Weight is a property on edges.
118  typedef boost::property<boost::edge_weight_t, double> Weight;
119 
125  {
126  typedef std::list<PathVertex> List;
127 
128  static double Distance(const List&);
129 
130  explicit PathVertex(
131  const DtPoint& l,
132  vertex_descriptor v = GraphTraits::null_vertex(),
134 
138  };
139 
141  {
142  typedef std::list<MultiPathVertex> List;
143 
144  explicit MultiPathVertex(
145  const DtPoint& l,
146  vertex_descriptor v = GraphTraits::null_vertex(),
148 
149  explicit MultiPathVertex(const PathVertex&);
150 
151  PathVertex::List getSinglePath() const;
152  void addPath(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
153  void addPathToFront(const PathVertex::List&);
154  void addPathToEnd(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
155  void addPathToEnd(const MultiPathVertex&);
156 
157  void draw(
159  const DtProj& localProj) const;
160 
163  };
164 
166  {
167  public:
168  typedef std::vector<Path> Sequence;
169 
170  explicit Path(const std::string& = std::string());
171 
172  PathVertex::List getSinglePath() const;
173  void addPath(const PathVertex::List&);
174  void addPath(const MultiPathVertex::List&);
175  void addPath(const Path&);
176  void addPathToFront(const PathVertex::List&);
177  void addPathToEnd(const PathVertex::List&);
178 
179  bool empty() const { return myPathVertices.empty(); }
180  size_t numPaths() const { return myPathVertices.size(); }
181 
182  std::string description() const { return myDescription; }
183 
184  const MultiPathVertex::List& pathVertices() const { return myPathVertices; }
185  MultiPathVertex::List& pathVertices() { return myPathVertices; }
186 
187  void draw(
189  const DtProj& localProj) const;
190 
191  private:
193  std::string myDescription;
194  };
195 
196  template <typename T>
198 
200 
201  typedef std::pair<DtQuery, double> DtWeightFactor;
202  typedef std::list<DtWeightFactor> DtWeightFactorsList;
203 
204  typedef boost::function<double (edge_descriptor)> EdgeWeightAdjustor;
205 
206  typedef boost::function<bool ()> CancelCheck;
207 
209  static DtRailSegmentList* DtPath2RailSegmentList(const Path& path);
210 
211  static Ptr Make(
212  std::auto_ptr<DtFeatureSet> input,
213  DtProj::CPtr localProj,
214  const DtTerrainInterfaceConfig& config,
215  std::string label = std::string());
216 
217  virtual ~DtAStarGraph();
218 
219  virtual void shutdown();
220  virtual bool isShutdown() const;
221 
232  Path makePath(
233  const DtPoint& startLocation,
234  const DtPoint& endLocation,
235  double maxSearchRadius,
236  double startProximityThreshold,
237  double stopProximityThreshold,
238  const DtWeightFactorsList& weightFactors,
239  const CancelCheck& cancelCheck);
240 
241  struct Results;
242 
244  {
247  boost::optional<DtFeatureGeometry::Area> searchArea;
252 
254  const DtFeatureGeometry::Point& start,
255  const DtFeatureGeometry::Point& end);
256 
257  void setMaxSearchRadius(double radius);
258 
259  bool cancel() const;
260  };
261 
262  Path makePath(
263  SearchParameters params,
264  Results* results = 0,
265  unsigned recursionLevel = 0);
266 
269  bool onPathFeature(const DtPoint& location, double offset = 0.5);
270 
271  std::auto_ptr<DtPathFeatureSet> paths() const;
272 
273  const DtFeatureSet& inputFeatureSet() const;
274 
275  DtProj::CPtr localProj() const;
276 
286  DtClosestPathFeatureFinder closestFeatureOnNetwork(
287  const DtPoint& startingLocation,
288  const double& searchRadius,
289  bool* dataAvailable = NULL);
290 
291  bool isDrawing() const;
292  void setDrawing(bool);
293 
294  bool isSearchDrawing() const;
295  void setSearchDrawing(bool);
296 
297  bool isLoadDrawing() const;
298  void setLoadDrawing(bool);
299 
300  std::string label() const;
301 
302  virtual DtFeaturesDebugger::Record& addToDebugger(DtFeaturesDebugger&);
303 
305  void addFeatureToGraph(
306  const DtFeature & r,
307  bool joinToIntersectingEdges,
308  const boost::optional<DtFeatureGeometry>& searchArea = boost::none_t(),
310 
311  void addFeatureToGraph(
312  const DtFeature&,
313  const boost::optional<DtFeatureGeometry>& searchArea = boost::none_t(),
315 
316  void addGeometryToGraph(DtFeatureGeometry g);
317 
318  bool loadTile(const DtPoint& localLocation, double timeout = 0);
319  bool loadTile(const DtFeatureGeometry& geom, double timeout = 0);
320  bool loadTileAndBlock(
321  const DtPoint& localLocation, const CancelCheck& = CancelCheck());
322  bool loadTileAndBlock(
323  const DtFeatureGeometry& geom, const CancelCheck& = CancelCheck());
324 
325  void redraw();
326 
327  protected:
328  DtAStarGraph(
329  std::auto_ptr<DtFeatureSet> input,
330  DtProj::CPtr localProj,
331  const DtTerrainInterfaceConfig& config,
332  std::string label);
333 
334  void draw();
335 
336  void drawLoop();
337 
339  boost::scoped_ptr<DtFeatureSet> myInputFeatureSet;
340 
343 
344  typedef tbb::queuing_rw_mutex GraphMutex;
345 
348 
351 
352  struct GridTile : public boost::enable_shared_from_this<GridTile>
353  {
354  typedef boost::shared_ptr<GridTile> Ptr;
355  typedef std::vector<Ptr> PtrVector;
356 
357  struct EdgeRecord
358  {
359  struct Hash
360  {
361  size_t operator()(const EdgeRecord& edge) const;
362  };
363 
364  bool operator==(const EdgeRecord& other) const;
365 
368 
369  explicit EdgeRecord(edge_descriptor, const EdgeInfo&);
370  };
371 
373  typedef std::vector<EdgeRecord> EdgeSet;
374  //typedef boost::unordered_set<EdgeRecord, EdgeRecord::Hash> EdgeSet;
375 
377  const unsigned level;
378  const unsigned maxLevel;
380 
381  explicit GridTile(const GridTile& parent, unsigned x, unsigned y);
382  explicit GridTile(DtAStarGraph&, const DtProj::Bounds&, unsigned maxLevel);
383 
384  ~GridTile();
385 
386  bool load(const DtFeatureGeometry&, double timeout);
387  void find(const DtFeatureGeometry&, PtrVector&);
388  void getTiles(PtrVector&);
389 
391 
392  void addEdge(edge_descriptor, const EdgeInfo&);
393  void getEdges(const DtFeatureGeometry&, EdgeSet&,
395  void getEdgesWithWidth(const DtFeatureGeometry&, EdgeSet&);
396 
397  private:
398  void loadAndGet();
399  bool loaded(double timeout) const;
400 
402 
403  typedef tbb::mutex LoadingMutex;
404 
406  enum State { None, Loading, Done, Error };
407  tbb::atomic<State> state;
408 
409  typedef tbb::queuing_rw_mutex Mutex;
410 
411  mutable Mutex mutex;
414  boost::shared_ptr<GridTile> subtiles[2][2];
415 
416  mutable Mutex edgesMutex;
418  };
419 
420  friend struct GridTile;
421 
424 
426 
427  tbb::atomic<bool> myShutdownFlag;
428 
429  typedef tbb::queuing_mutex DrawerMutex;
432  std::auto_ptr<tbb::tbb_thread> myDrawThread;
433 
434  typedef tbb::interface5::condition_variable Condition;
435  typedef tbb::mutex ConditionMutex;
436  typedef tbb::interface5::unique_lock<ConditionMutex> ConditionLock;
441 
442  tbb::atomic<bool> myDrawSearchFlag;
443  tbb::atomic<bool> myDrawLoadingFlag;
444 
445  std::string myLabel;
446  };
447 
448  template <typename T>
450  {
451  typedef std::vector<T> Sequence;
452 
453  static T CreateDefault(T def, size_t) { return def; }
454 
455  public:
456  typedef size_t key_type;
457  typedef typename Sequence::value_type value_type;
458  typedef typename Sequence::reference reference;
459  typedef boost::lvalue_property_map_tag category;
460 
461  typedef boost::function<T (size_t)> DefaultFactory;
462 
463  explicit DynamicPropertyMap(const DefaultFactory& def, size_t size = 0)
464  : myDefaultFactory(def)
465  , myValues(new Sequence(size))
466  {
467  for (size_t i=0; i != size; ++i)
468  (*myValues)[i] = myDefaultFactory(i);
469  }
470 
471  explicit DynamicPropertyMap(T def, size_t size = 0)
472  : myDefaultFactory(boost::bind(&CreateDefault, def, _1))
473  , myValues(new Sequence(size, def))
474  {
475  }
476 
477  T& operator[](size_t index)
478  {
479  while (index >= size())
480  myValues->push_back(myDefaultFactory(index));
481 
482  return (*myValues)[index];
483  }
484 
485  T& operator[](size_t index) const
486  {
487  while (index >= size())
488  myValues->push_back(myDefaultFactory(index));
489 
490  return (*myValues)[index];
491  }
492 
493  size_t size() const
494  {
495  return myValues->size();
496  }
497 
498  void clear()
499  {
500  myValues->clear();
501  }
502 
503  private:
505  boost::shared_ptr<Sequence> myValues;
506  };
507 
508  template <typename T>
509  T get(const DtAStarGraph::DynamicPropertyMap<T>& map, size_t index)
510  {
511  return map[index];
512  }
513 
519 
520  template <typename T>
521  void put(DtAStarGraph::DynamicPropertyMap<T>& map, size_t index, const T& value)
522  {
523  map[index] = value;
524  }
525 
531 
533  {
534  public:
536  };
537 
539  {
540  public:
542  const DtAStarGraph::Ptr& pathFinder,
543  const DtPoint& localStart,
544  const DtPoint& localDestination,
545  double maxSearchRadius,
546  double startProximityThreshold,
547  double stopProximityThreshold,
548  const DtAStarGraph::DtWeightFactorsList& factors);
549 
550  task* onExecute();
551  task* onException();
552 
553  public:
561  };
562 }
563 

Document ID: Generated on Tue Mar 8 22:13:38 EST 2016 from SVN revision 162938
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)