VR-Forces 4.3 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>
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 
48  DtPoint location;
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 
84  struct RedrawCallback;
85  friend struct RedrawCallback;
86 
90 
91  public:
92  typedef boost::shared_ptr<DtAStarGraph> Ptr;
93 
94  Ptr shared_from_this();
95 
96  typedef boost::adjacency_list< //graph is adjacency list (vs adjacency matrix)
97  boost::setS, //per-vertex edge-list is a set
98  boost::vecS, //graph's vertex-list is a vector
99  boost::directedS, //graph is directed
100  VertexInfo, //VertexInfo struct is property on edges
101  EdgeInfo, //EdgeInfo struct is property on edges
102  boost::no_property, //no property on graph
103  boost::vecS> //graph's edge-list is a vector
105 
106  typedef boost::graph_traits<DtBoostGraph> GraphTraits;
107 
108  typedef DtBoostGraph::vertex_descriptor vertex_descriptor;
109  typedef DtBoostGraph::edge_descriptor edge_descriptor;
110 
111  typedef GraphTraits::vertex_iterator vertex_iterator;
112  typedef GraphTraits::edge_iterator edge_iterator;
113 
114  typedef std::list<vertex_descriptor> VertexList;
115 
116  //Weight is a property on edges.
117  typedef boost::property<boost::edge_weight_t, double> Weight;
118 
124  {
125  typedef std::list<PathVertex> List;
126 
127  static double Distance(const List&);
128 
129  explicit PathVertex(
130  const DtPoint& l,
131  vertex_descriptor v = GraphTraits::null_vertex(),
133 
134  DtPoint localLocation;
137  };
138 
140  {
141  typedef std::list<MultiPathVertex> List;
142 
143  explicit MultiPathVertex(
144  const DtPoint& l,
145  vertex_descriptor v = GraphTraits::null_vertex(),
147 
148  explicit MultiPathVertex(const PathVertex&);
149 
150  PathVertex::List getSinglePath() const;
151  void addPath(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
152  void addPathToFront(const PathVertex::List&);
153  void addPathToEnd(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
154  void addPathToEnd(const MultiPathVertex&);
155 
156  void draw(
158  const DtProj& localProj) const;
159 
162  };
163 
165  {
166  public:
167  typedef std::vector<Path> Sequence;
168 
169  explicit Path(const std::string& = std::string());
170 
171  PathVertex::List getSinglePath() const;
172  void addPath(const PathVertex::List&);
173  void addPath(const MultiPathVertex::List&);
174  void addPath(const Path&);
175  void addPathToFront(const PathVertex::List&);
176  void addPathToEnd(const PathVertex::List&);
177 
178  bool empty() const { return myPathVertices.empty(); }
179  size_t numPaths() const { return myPathVertices.size(); }
180 
181  std::string description() const { return myDescription; }
182 
183  const MultiPathVertex::List& pathVertices() const { return myPathVertices; }
184  MultiPathVertex::List& pathVertices() { return myPathVertices; }
185 
186  void draw(
188  const DtProj& localProj) const;
189 
190  private:
192  std::string myDescription;
193  };
194 
195  template <typename T>
197 
199 
200  typedef std::pair<DtQuery, double> DtWeightFactor;
201  typedef std::list<DtWeightFactor> DtWeightFactorsList;
202 
203  typedef boost::function<double (edge_descriptor)> EdgeWeightAdjustor;
204 
205  typedef boost::function<bool ()> CancelCheck;
206 
208  static DtRailSegmentList* DtPath2RailSegmentList(const Path& path);
209 
210  static Ptr Make(
211  std::auto_ptr<DtFeatureSet> input,
212  DtProj::CPtr localProj,
213  const DtTerrainInterfaceConfig& config,
214  std::string label = std::string());
215 
216  virtual ~DtAStarGraph();
217 
218  virtual void shutdown();
219  virtual bool isShutdown() const;
220 
231  Path makePath(
232  const DtPoint& startLocation,
233  const DtPoint& endLocation,
234  double maxSearchRadius,
235  double startProximityThreshold,
236  double stopProximityThreshold,
237  const DtWeightFactorsList& weightFactors,
238  const CancelCheck& cancelCheck);
239 
240  struct Results;
241 
243  {
246  boost::optional<DtFeatureGeometry::Area> searchArea;
251 
253  const DtFeatureGeometry::Point& start,
254  const DtFeatureGeometry::Point& end);
255 
256  void setMaxSearchRadius(double radius);
257 
258  bool cancel() const;
259  };
260 
261  Path makePath(
262  SearchParameters params,
263  Results* results = 0,
264  unsigned recursionLevel = 0);
265 
268  bool onPathFeature(const DtPoint& location, double offset = 0.5);
269 
270  std::auto_ptr<DtPathFeatureSet> paths() const;
271 
272  const DtFeatureSet& inputFeatureSet() const;
273 
274  DtProj::CPtr localProj() const;
275 
285  DtClosestPathFeatureFinder closestFeatureOnNetwork(
286  const DtPoint& startingLocation,
287  const double& searchRadius,
288  bool* dataAvailable = NULL);
289 
290  bool isDrawing() const;
291  void setDrawing(bool);
292 
293  bool isSearchDrawing() const;
294  void setSearchDrawing(bool);
295 
296  bool isLoadDrawing() const;
297  void setLoadDrawing(bool);
298 
299  std::string label() const;
300 
301  virtual DtFeaturesDebugger::Record& addToDebugger(DtFeaturesDebugger&);
302 
304  void addFeatureToGraph(
305  const DtFeature & r,
306  bool joinToIntersectingEdges,
307  const boost::optional<DtFeatureGeometry>& searchArea = boost::none_t(),
309 
310  void addFeatureToGraph(
311  const DtFeature&,
312  const boost::optional<DtFeatureGeometry>& searchArea = boost::none_t(),
314 
315  void addGeometryToGraph(DtFeatureGeometry g);
316 
317  bool loadTile(const DtPoint& localLocation, double timeout = 0);
318  bool loadTile(const DtFeatureGeometry& geom, double timeout = 0);
319  bool loadTileAndBlock(
320  const DtPoint& localLocation, const CancelCheck& = CancelCheck());
321  bool loadTileAndBlock(
322  const DtFeatureGeometry& geom, const CancelCheck& = CancelCheck());
323 
324  void redraw();
325 
326  protected:
327  DtAStarGraph(
328  std::auto_ptr<DtFeatureSet> input,
329  DtProj::CPtr localProj,
330  const DtTerrainInterfaceConfig& config,
331  std::string label);
332 
333  void draw();
334 
335  void drawLoop();
336 
338  boost::scoped_ptr<DtFeatureSet> myInputFeatureSet;
339 
342 
343  typedef tbb::queuing_rw_mutex GraphMutex;
344 
347 
350 
351  struct GridTile : public boost::enable_shared_from_this<GridTile>
352  {
353  typedef boost::shared_ptr<GridTile> Ptr;
354  typedef std::vector<Ptr> PtrVector;
355 
356  struct EdgeRecord
357  {
358  struct Hash
359  {
360  size_t operator()(const EdgeRecord& edge) const;
361  };
362 
363  bool operator==(const EdgeRecord& other) const;
364 
367 
368  explicit EdgeRecord(edge_descriptor, const EdgeInfo&);
369  };
370 
372  typedef std::vector<EdgeRecord> EdgeSet;
373  //typedef boost::unordered_set<EdgeRecord, EdgeRecord::Hash> EdgeSet;
374 
376  const unsigned level;
377  const unsigned maxLevel;
379 
380  explicit GridTile(const GridTile& parent, unsigned x, unsigned y);
381  explicit GridTile(const DtProj::Bounds&, unsigned maxLevel);
382 
383  ~GridTile();
384 
385  bool load(DtAStarGraph&, const DtFeatureGeometry&, double timeout);
386  void find(const DtFeatureGeometry&, PtrVector&);
387  void getTiles(PtrVector&);
388 
390 
391  void addEdge(edge_descriptor, const EdgeInfo&);
392  void getEdges(const DtFeatureGeometry&, EdgeSet&,
394  void getEdgesWithWidth(const DtFeatureGeometry&, EdgeSet&);
395 
396  private:
397  DtFeatureSet* loadAndGet(DtAStarGraph&);
398 
399  enum State { None, Loading, Done, Error };
400  tbb::atomic<State> state;
401 
402  typedef tbb::queuing_rw_mutex Mutex;
403 
404  mutable Mutex mutex;
405  boost::shared_ptr<DtFeatureSet> featureSet;
407  boost::shared_ptr<GridTile> subtiles[2][2];
408 
409  mutable Mutex edgesMutex;
411  };
412 
413  friend struct GridTile;
414 
417 
419 
420  tbb::atomic<bool> myShutdownFlag;
421 
422  typedef tbb::queuing_mutex DrawerMutex;
425  std::auto_ptr<tbb::tbb_thread> myDrawThread;
426 
427  typedef tbb::interface5::condition_variable Condition;
428  typedef tbb::mutex ConditionMutex;
429  typedef tbb::interface5::unique_lock<ConditionMutex> ConditionLock;
434 
435  tbb::atomic<bool> myDrawSearchFlag;
436  tbb::atomic<bool> myDrawLoadingFlag;
437 
438  std::string myLabel;
439  };
440 
441  template <typename T>
443  {
444  typedef std::vector<T> Sequence;
445 
446  static T CreateDefault(T def, size_t) { return def; }
447 
448  public:
449  typedef size_t key_type;
450  typedef typename Sequence::value_type value_type;
451  typedef typename Sequence::reference reference;
452  typedef boost::lvalue_property_map_tag category;
453 
454  typedef boost::function<T (size_t)> DefaultFactory;
455 
456  explicit DynamicPropertyMap(const DefaultFactory& def, size_t size = 0)
457  : myDefaultFactory(def)
458  , myValues(new Sequence(size))
459  {
460  for (size_t i=0; i != size; ++i)
461  (*myValues)[i] = myDefaultFactory(i);
462  }
463 
464  explicit DynamicPropertyMap(T def, size_t size = 0)
465  : myDefaultFactory(boost::bind(&CreateDefault, def, _1))
466  , myValues(new Sequence(size, def))
467  {
468  }
469 
470  T& operator[](size_t index)
471  {
472  while (index >= size())
473  myValues->push_back(myDefaultFactory(index));
474 
475  return (*myValues)[index];
476  }
477 
478  T& operator[](size_t index) const
479  {
480  while (index >= size())
481  myValues->push_back(myDefaultFactory(index));
482 
483  return (*myValues)[index];
484  }
485 
486  size_t size() const
487  {
488  return myValues->size();
489  }
490 
491  void clear()
492  {
493  myValues->clear();
494  }
495 
496  private:
498  boost::shared_ptr<Sequence> myValues;
499  };
500 
501  template <typename T>
502  T get(const DtAStarGraph::DynamicPropertyMap<T>& map, size_t index)
503  {
504  return map[index];
505  }
506 
512 
513  template <typename T>
514  void put(DtAStarGraph::DynamicPropertyMap<T>& map, size_t index, const T& value)
515  {
516  map[index] = value;
517  }
518 
524 
526  {
527  public:
529  };
530 
532  {
533  public:
535  const DtAStarGraph::Ptr& pathFinder,
536  const DtPoint& localStart,
537  const DtPoint& localDestination,
538  double maxSearchRadius,
539  double startProximityThreshold,
540  double stopProximityThreshold,
541  const DtAStarGraph::DtWeightFactorsList& factors);
542 
543  task* onExecute();
544  task* onException();
545 
546  public:
548  DtPoint myLocalStart;
554  };
555 }
556 

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)