VR-Forces Developer's Guide
 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 
138  {
139  typedef std::list<MultiPathVertex> List;
140 
141  explicit MultiPathVertex(
142  const DtPoint& l,
143  vertex_descriptor v = GraphTraits::null_vertex(),
145 
146  explicit MultiPathVertex(const PathVertex&);
147 
148  PathVertex::List getSinglePath() const;
149  void addPath(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
150  void addPathToFront(const PathVertex::List&);
151  void addPathToEnd(PathVertex::List::const_iterator, PathVertex::List::const_iterator);
152  void addPathToEnd(const MultiPathVertex&);
153 
156  };
157 
159  {
160  public:
161  typedef std::vector<Path> Sequence;
162 
163  explicit Path(const std::string& = std::string());
164 
165  PathVertex::List getSinglePath() const;
166  void addPath(const PathVertex::List&);
167  void addPath(const MultiPathVertex::List&);
168  void addPath(const Path&);
169  void addPathToFront(const PathVertex::List&);
170  void addPathToEnd(const PathVertex::List&);
171 
172  bool empty() const { return myPathVertices.empty(); }
173  size_t numPaths() const { return myPathVertices.size(); }
174 
175  std::string description() const { return myDescription; }
176 
177  const MultiPathVertex::List& pathVertices() const { return myPathVertices; }
178  MultiPathVertex::List& pathVertices() { return myPathVertices; }
179 
180  private:
182  std::string myDescription;
183  };
184 
185  template <typename T>
187 
189 
190  typedef std::pair<DtQuery, double> DtWeightFactor;
191  typedef std::list<DtWeightFactor> DtWeightFactorsList;
192 
193  typedef boost::function<double (edge_descriptor)> EdgeWeightAdjustor;
194 
195  typedef boost::function<bool ()> CancelCheck;
196 
198  static DtRailSegmentList* DtPath2RailSegmentList(const Path& path);
199 
200  static Ptr Make(
201  std::auto_ptr<DtFeatureSet> input,
202  DtProj::CPtr localProj,
203  const DtTerrainInterfaceConfig& config,
204  std::string label = std::string());
205 
206  virtual ~DtAStarGraph();
207 
208  virtual void shutdown();
209  virtual bool isShutdown() const;
210 
221  Path makePath(
222  const DtPoint& startLocation,
223  const DtPoint& endLocation,
224  double maxSearchRadius,
225  double startProximityThreshold,
226  double stopProximityThreshold,
227  const DtWeightFactorsList& weightFactors,
228  const CancelCheck& cancelCheck);
229 
230  struct Results;
231 
233  {
236  boost::optional<DtFeatureGeometry::Area> searchArea;
241 
243  const DtFeatureGeometry::Point& start,
244  const DtFeatureGeometry::Point& end);
245 
246  void setMaxSearchRadius(double radius);
247  void setSearchRadius(double maxRadius, double minRadius);
248 
249  bool cancel() const;
250  };
251 
252  Path makePath(
253  SearchParameters params,
254  Results* results = 0,
255  unsigned recursionLevel = 0);
256 
259  bool onPathFeature(const DtPoint& location, double offset = 0.5);
260 
261  std::auto_ptr<DtPathFeatureSet> paths() const;
262 
263  const DtFeatureSet& inputFeatureSet() const;
264 
265  DtProj::CPtr localProj() const;
266 
276  DtClosestPathFeatureFinder closestFeatureOnNetwork(
277  const DtPoint& startingLocation,
278  const double& searchRadius,
279  bool* dataAvailable = NULL);
280 
281  std::string label() const;
282 
283  virtual DtFeaturesDebugger::Record& addToDebugger(DtFeaturesDebugger&);
284 
289  void addFeatureToGraph(
290  const DtFeature & r,
291  bool joinToIntersectingEdges,
292  const boost::optional<DtFeatureGeometry>& searchArea = boost::none);
293 
296  void addFeatureToGraph(
297  const DtFeature&,
298  const boost::optional<DtFeatureGeometry>& searchArea = boost::none);
299 
300  void addGeometryToGraph(DtFeatureGeometry g);
301 
308  bool loadTile(const DtPoint& localLocation, double timeout = 0);
309  bool loadTile(const DtFeatureGeometry& geom, double timeout = 0);
310 
311  bool loadTileAndBlock(
312  const DtPoint& localLocation, const CancelCheck& = CancelCheck());
313  bool loadTileAndBlock(
314  const DtFeatureGeometry& geom, const CancelCheck& = CancelCheck());
315 
316  protected:
317 
325  void maybeAddEdgeToGraph(const DtPoint& vA, const vertex_descriptor vAIndex,
326  const DtPoint& vB, const vertex_descriptor vBIndex,
327  boost::shared_ptr<DtPathFeature> path,
328  unsigned& numAddedEdges, unsigned& numDuplicates);
329 
337  bool maybeAddVertexToGraph(const DtPoint& vLocation, const VertexBoolMap& existingVertices,
338  const double tolerance,
339  vertex_descriptor& vIndex);
340 
342 
343  protected:
346  DtAStarGraph(
347  std::auto_ptr<DtFeatureSet> input,
348  DtProj::CPtr localProj,
349  const DtTerrainInterfaceConfig& config,
350  std::string label);
351 
353  boost::scoped_ptr<DtFeatureSet> myInputFeatureSet;
354 
357 
358  typedef tbb::queuing_rw_mutex GraphMutex;
359 
362 
365 
366  struct GridTile : public boost::enable_shared_from_this<GridTile>
367  {
368  typedef boost::shared_ptr<GridTile> TilePtr;
369  typedef std::vector<TilePtr> TilePtrVector;
370 
371  struct EdgeRecord
372  {
373  struct Hash
374  {
375  size_t operator()(const EdgeRecord& edge) const;
376  };
377 
378  bool operator==(const EdgeRecord& other) const;
379 
382 
383  explicit EdgeRecord(edge_descriptor, const EdgeInfo&);
384  };
385 
387  typedef std::vector<EdgeRecord> EdgeSet;
388  //typedef boost::unordered_set<EdgeRecord, EdgeRecord::Hash> EdgeSet;
389 
391  const unsigned myLevel;
392  const unsigned myMaxLevel;
394 
402  explicit GridTile(const GridTile& parent, unsigned x, unsigned y);
403 
406  explicit GridTile(DtAStarGraph&, const DtProj::Bounds&, unsigned maxLevel);
407 
408  ~GridTile();
409 
413  bool loadAllLeafSubTiles(const DtFeatureGeometry&, double timeout);
414 
419  void findIntersectingSubTiles(const DtFeatureGeometry&, TilePtrVector& tileList);
420 
423  void getTiles(TilePtrVector&);
424 
427  void addEdge(edge_descriptor, const EdgeInfo&);
428 
433  void getEdges(const DtFeatureGeometry&, EdgeSet& allIntersectingEdges);
434 
437  void getEdgesWithWidth(const DtFeatureGeometry&, EdgeSet& allIntersectingEdges);
438 
439  private:
442  void loadAndGet();
443 
444  bool loaded(double timeout) const;
445 
447 
448  typedef tbb::mutex LoadingMutex;
449 
451  enum State { None, Loading, Done, Error };
452  tbb::atomic<State> myState;
453 
454  typedef tbb::queuing_rw_mutex Mutex;
455 
456  mutable Mutex myMutex;
460 
462  boost::shared_ptr<GridTile> mySubtiles[2][2];
463 
466  };
467 
468  friend struct GridTile;
469 
474 
476 
477  tbb::atomic<bool> myShutdownFlag;
478 
479  std::string myLabel;
480 
483  };
484 
485  template <typename T>
487  {
488  typedef std::vector<T> Sequence;
489 
490  static T CreateDefault(T def, size_t) { return def; }
491 
492  public:
493  typedef size_t key_type;
494  typedef typename Sequence::value_type value_type;
495  typedef typename Sequence::reference reference;
496  typedef boost::lvalue_property_map_tag category;
497 
498  typedef boost::function<T (size_t)> DefaultFactory;
499 
500  explicit DynamicPropertyMap(const DefaultFactory& def, size_t size = 0)
501  : myDefaultFactory(def)
502  , myValues(new Sequence(size))
503  {
504  for (size_t i=0; i != size; ++i)
505  (*myValues)[i] = myDefaultFactory(i);
506  }
507 
508  explicit DynamicPropertyMap(T def, size_t size = 0)
509  : myDefaultFactory(boost::bind(&CreateDefault, def, _1))
510  , myValues(new Sequence(size, def))
511  {
512  }
513 
514  T& operator[](size_t index)
515  {
516  while (index >= size())
517  myValues->push_back(myDefaultFactory(index));
518 
519  return (*myValues)[index];
520  }
521 
522  T& operator[](size_t index) const
523  {
524  while (index >= size())
525  myValues->push_back(myDefaultFactory(index));
526 
527  return (*myValues)[index];
528  }
529 
530  size_t size() const
531  {
532  return myValues->size();
533  }
534 
535  void clear()
536  {
537  myValues->clear();
538  }
539 
540  private:
542  boost::shared_ptr<Sequence> myValues;
543  };
544 
545  template <typename T>
546  T get(const DtAStarGraph::DynamicPropertyMap<T>& map, size_t index)
547  {
548  return map[index];
549  }
550 
556 
557  template <typename T>
558  void put(DtAStarGraph::DynamicPropertyMap<T>& map, size_t index, const T& value)
559  {
560  map[index] = value;
561  }
562 
568 
570  {
571  public:
573  };
574 
576  {
577  public:
579  const DtAStarGraph::Ptr& pathFinder,
580  const DtPoint& localStart,
581  const DtPoint& localDestination,
582  double maxSearchRadius,
583  double startProximityThreshold,
584  double stopProximityThreshold,
585  const DtAStarGraph::DtWeightFactorsList& factors);
586 
587  task* onExecute();
588  task* onException();
589 
590  public:
598  };
599 }
600 
Represents a set of DtFeature objects or others which conform to the DtFeature concept.
Definition: featureSet.h:101
Concrete job class which manages a weak pointer to it&#39;s job map entry. Using this job will require th...
Definition: asyncJob.h:92
double startProximityThreshold
Definition: aStarGraph.h:237
State
Definition: aStarGraph.h:451
GraphTraits::edge_iterator edge_iterator
Definition: aStarGraph.h:109
#define DT_DLL_features
stop complaining about multiple independent base classes for boost::noncopyable this should probably ...
Definition: featuresDefines.h:41
const unsigned myMaxLevel
Definition: aStarGraph.h:392
DtFeatureGeometry::Point endPoint
Definition: aStarGraph.h:235
Class DtTerrainInterfaceConfig is a readable-writable class holding terrain interface configuration o...
Definition: terrainInterfaceConfig.h:32
boost::graph_traits< DtBoostGraph > GraphTraits
Definition: aStarGraph.h:103
Definition: aStarGraph.h:575
DtFeatureGeometry::Point startPoint
Definition: aStarGraph.h:234
boost::function< void()> DetachCallback
Callback type for disconnecting other callbacks. Functions which register callbacks return a DetachCa...
Definition: featureSet.h:125
FeaturePtr addFeatureRef(const Feature &f)
Definition: featureSetUtils.h:308
DtAStarGraph & myAStarGraph
Definition: aStarGraph.h:446
boost::shared_ptr< Sequence > myValues
Definition: aStarGraph.h:542
int myDebuggingGraphicIndex
For debugging output.
Definition: aStarGraph.h:482
Definition: aStarGraph.h:54
tbb::atomic< bool > myShutdownFlag
Definition: aStarGraph.h:477
tbb::atomic< State > myState
Definition: aStarGraph.h:452
std::list< PathVertex > List
Definition: aStarGraph.h:122
std::list< DtRailSegment * > DtRailSegmentList
Definition: railSegment.h:197
DtBoostGraph myGraph
Boost graph for path planning.
Definition: aStarGraph.h:364
std::vector< Path > Sequence
Definition: aStarGraph.h:161
boost::lvalue_property_map_tag category
Definition: aStarGraph.h:496
DynamicPropertyMap(T def, size_t size=0)
Definition: aStarGraph.h:508
DtFeatureSet::DetachCallback myDetacherCallbackFn
Definition: aStarGraph.h:461
DtFeatureSetWrapper< DtFeatureSet, boost::shared_ptr > myFeatureSet
A feature set that is the AStarGraph&#39;s input feature set, but clipped to the geometry of this tile...
Definition: aStarGraph.h:459
std::vector< TilePtr > TilePtrVector
Definition: aStarGraph.h:369
void put(DtAStarGraph::DynamicPropertyMap< T > &map, size_t index, const T &value)
template &lt;typename t&gt;=&quot;&quot;&gt; T get(const DtAStarGraph::DynamicPropertyMap&lt;T&gt;* map, size_t index) { retur...
Definition: aStarGraph.h:558
Vertex data passed back from makePath. location is the location of the current vertex in local coordi...
Definition: aStarGraph.h:120
tbb::mutex LoadingMutex
Definition: aStarGraph.h:448
voidpf void uLong size
Definition: ioapi.h:39
GraphTraits::vertex_iterator vertex_iterator
Definition: aStarGraph.h:108
FeaturePtr addFeature(Feature *f)
these functions make it easier to take the address of addFeature
Definition: featureSetUtils.h:306
tbb::queuing_rw_mutex GraphMutex
Definition: aStarGraph.h:358
double stopProximityThreshold
Definition: aStarGraph.h:238
DT_DLL_terrainCS bool operator==(const DtDegMinSec &lhs, const DtDegMinSec &rhs)
MultiPathVertex::List myPathVertices
Definition: aStarGraph.h:181
boost::shared_ptr< DtAStarGraph > Ptr
Definition: aStarGraph.h:89
Mutex myEdgesMutex
Definition: aStarGraph.h:464
List nextVertices
Definition: aStarGraph.h:155
Definition: featureGeometry.h:60
Mutex myMutex
Definition: aStarGraph.h:456
virtual FeaturePtr addFeaturePtr(Feature *feature)
These functions are used to add features to the feature sets. The argument is the feature being added...
Definition: proj.h:68
Definition: featureSet.h:60
DtProj::CPtr myLocalProj
For converting local coordinates passed in from the user.
Definition: aStarGraph.h:356
Definition: aStarGraph.h:79
boost::function< bool()> CancelCheck
Definition: aStarGraph.h:195
GridTile::TilePtr myLoadedTiles
Points to one top-level (level = 0) tile. This tile will have 4 subtiles, one for each quadrant...
Definition: aStarGraph.h:473
double myStartProximityThreshold
Definition: aStarGraph.h:595
std::list< DtWeightFactor > DtWeightFactorsList
Definition: aStarGraph.h:191
Entry for a feature set in the debugger.
Definition: featuresDebugger.h:55
edge_descriptor myEdgeDescriptor
Definition: aStarGraph.h:380
const unsigned myLevel
Definition: aStarGraph.h:391
size_t size() const
Definition: aStarGraph.h:530
std::list< vertex_descriptor > VertexList
Definition: aStarGraph.h:111
boost::shared_ptr< GridTile > TilePtr
Definition: aStarGraph.h:368
MAKVRinTerra::DtAStarGraph::Path path
Definition: aStarGraph.h:572
DtPoint localLocation
Definition: aStarGraph.h:131
std::string description() const
Definition: aStarGraph.h:175
DtWeightFactorsList weightFactors
Definition: aStarGraph.h:239
DtTerrainInterfaceConfig myTerrainInterfaceConfig
Definition: aStarGraph.h:475
std::string myLabel
Definition: aStarGraph.h:479
Handle< PointType > Point
Definition: featureGeometry.h:69
Definition: aStarGraph.h:158
DT_DLL_VRVCORE double distance(const makVrv::DtCoordinateSystem &, const DtVector &from, const DtVector &to)
Returns the distance from the two points. Coordinates are in local database coordinates The coordinat...
binderNoArgs< _Fn > bind(const _Fn &_Func, const _Ty &_Left)
Definition: DtSTLUtilities.h:76
EdgeSet myEdgeSet
Definition: aStarGraph.h:465
Sequence::reference reference
Definition: aStarGraph.h:495
boost::shared_ptr< const DtPathFeature > CPtr
Definition: pathFeature.h:21
DtPoint myLocalStart
Definition: aStarGraph.h:592
Definition: aStarGraph.h:43
Definition: aStarGraph.h:366
A MultiPathVertex is a PathVertex with a list of next vertices.
Definition: aStarGraph.h:137
DefaultFactory myDefaultFactory
Definition: aStarGraph.h:541
MultiPathVertex::List & pathVertices()
Definition: aStarGraph.h:178
boost::shared_ptr< const DtProj > CPtr
Definition: proj.h:48
Handle< PathType > Path
Definition: featureGeometry.h:70
DtBoostGraph::edge_descriptor edge_descriptor
Definition: aStarGraph.h:106
size_t key_type
Definition: aStarGraph.h:493
Base class for result of job.
Definition: asyncJobServer.h:43
template &lt;typename t&gt;=&quot;&quot;&gt; void put(DtAStarGraph::DynamicPropertyMap&lt;T&gt;* map, size_t index...
Definition: aStarGraph.h:569
DtPathFeature::CPtr nextEdgeFeature
Definition: aStarGraph.h:133
boost::optional< DtFeatureGeometry::Area > searchArea
Definition: aStarGraph.h:236
std::vector< DtPathFeature::CPtr > FeatureVector
Definition: aStarGraph.h:59
DtPoint myLocalDestination
Definition: aStarGraph.h:593
void clear()
Definition: aStarGraph.h:535
EdgeInfo myEdgeInfo
Definition: aStarGraph.h:381
LoadingMutex myLoadingMutex
Definition: aStarGraph.h:450
Definition: pathFeature.h:106
double myStopProximityThreshold
Definition: aStarGraph.h:596
boost::function< T(size_t)> DefaultFactory
Definition: aStarGraph.h:498
Definition: pathFeature.h:17
const MultiPathVertex::List & pathVertices() const
Definition: aStarGraph.h:177
const DtProj::Bounds myBounds
Definition: aStarGraph.h:390
double myMaxSearchRadius
Definition: aStarGraph.h:594
DynamicPropertyMap(const DefaultFactory &def, size_t size=0)
Definition: aStarGraph.h:500
boost::shared_ptr< Data > myData
Definition: aStarGraph.h:73
Source feature sets are used to create your own feature sets with your own features.
Definition: featureSetUtils.h:286
std::vector< EdgeRecord > EdgeSet
fixme change to unordered set
Definition: aStarGraph.h:387
boost::function< double(edge_descriptor)> EdgeWeightAdjustor
Definition: aStarGraph.h:193
boost::scoped_ptr< DtFeatureSet > myInputFeatureSet
Feature source for this graph.
Definition: aStarGraph.h:353
DynamicPropertyMap< unsigned > VertexBoolMap
Definition: aStarGraph.h:186
GraphMutex myGraphMutex
Synchronize access to boost graph.
Definition: aStarGraph.h:361
const DtFeatureGeometry::Area myBoundsGeometry
Definition: aStarGraph.h:393
DtPoint location
Definition: aStarGraph.h:48
T & operator[](size_t index)
Definition: aStarGraph.h:514
Sequence::value_type value_type
Definition: aStarGraph.h:494
tbb::queuing_rw_mutex Mutex
Definition: aStarGraph.h:454
vertex_descriptor vertex
Definition: aStarGraph.h:132
PathVertex pathVertex
Definition: aStarGraph.h:154
T & operator[](size_t index) const
Definition: aStarGraph.h:522
Represents a GIS feature. Features consist of a DtFeatureGeometry (2.5D geometric vector data) and a ...
Definition: feature.h:37
bool empty() const
Definition: aStarGraph.h:172
Definition: featuresDebugger.h:33
std::list< MultiPathVertex > List
Definition: aStarGraph.h:139
voidpf uLong offset
Definition: ioapi.h:42
std::pair< DtQuery, double > DtWeightFactor
Definition: aStarGraph.h:190
DtAStarGraph::Ptr myPathFinder
Definition: aStarGraph.h:591
std::string myDescription
Definition: aStarGraph.h:182
std::vector< T > Sequence
Definition: aStarGraph.h:488
static T CreateDefault(T def, size_t)
Definition: aStarGraph.h:490
Represents a feature geometry.
Definition: featureGeometry.h:39
size_t numPaths() const
Definition: aStarGraph.h:173
Definition: point.h:34
DtAStarGraph::DtWeightFactorsList myWeightFactors
Definition: aStarGraph.h:597
boost::property< boost::edge_weight_t, double > Weight
Definition: aStarGraph.h:114
CancelCheck cancelCheck
Definition: aStarGraph.h:240
boost::adjacency_list< boost::setS, boost::vecS, boost::directedS, VertexInfo, EdgeInfo, boost::no_property, boost::vecS > DtBoostGraph
Definition: aStarGraph.h:101
DtBoostGraph::vertex_descriptor vertex_descriptor
Definition: aStarGraph.h:105

Document ID: Generated on Tue Sep 24 19:28:17 EDT 2024 from SVN revision 269799
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)