libosmscout  1.1.1
RoutingService.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_ROUTINGSERVICE_H
2 #define OSMSCOUT_ROUTINGSERVICE_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2012 Tim Teulings
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
23 #include <atomic>
24 #include <functional>
25 #include <list>
26 #include <memory>
27 #include <set>
28 #include <unordered_map>
29 #include <unordered_set>
30 
31 #include <osmscout/CoreFeatures.h>
32 
33 #include <osmscout/Point.h>
34 
35 #include <osmscout/TypeConfig.h>
36 
38 
39 // Datafiles
40 #include <osmscout/DataFile.h>
41 #include <osmscout/Database.h>
43 
44 // Routing
45 #include <osmscout/Intersection.h>
51 
52 #include <osmscout/util/Breaker.h>
53 #include <osmscout/util/Cache.h>
54 
56 
57 namespace osmscout {
58 
64  class OSMSCOUT_API RoutePosition CLASS_FINAL
65  {
66  private:
67  ObjectFileRef object;
68  size_t nodeIndex;
69  DatabaseId database;
70 
71  public:
72  RoutePosition();
73  RoutePosition(const ObjectFileRef& object,
74  size_t nodeIndex,
75  DatabaseId database);
76 
77  bool IsValid() const
78  {
79  return object.Valid();
80  }
81 
82  ObjectFileRef GetObjectFileRef() const
83  {
84  return object;
85  }
86 
87  size_t GetNodeIndex() const
88  {
89  return nodeIndex;
90  }
91 
93  {
94  return database;
95  }
96  };
97 
98  class OSMSCOUT_API RoutePositionResult CLASS_FINAL
99  {
100  private:
101  RoutePosition routePosition;
102  Distance distance;
103 
104  public:
105  RoutePositionResult();
106 
107  RoutePositionResult(const RoutePosition &routePosition, const Distance &distance);
108 
109  RoutePosition GetRoutePosition() const
110  {
111  return routePosition;
112  }
113 
114  Distance GetDistance() const
115  {
116  return distance;
117  }
118 
119  bool IsValid() const
120  {
121  return routePosition.IsValid();
122  }
123  };
124 
125 
135  class OSMSCOUT_API RouterParameter CLASS_FINAL
136  {
137  private:
138  bool debugPerformance;
139 
140  public:
141  RouterParameter();
142 
143  void SetDebugPerformance(bool debug);
144 
145  bool IsDebugPerformance() const;
146  };
147 
154  {
155  public:
156  virtual ~RoutingProgress() = default;
157 
161  virtual void Reset() = 0;
162 
170  virtual void Progress(const Distance &currentMaxDistance,
171  const Distance &overallDistance) = 0;
172  };
173 
177  using RoutingProgressRef = std::shared_ptr<RoutingProgress>;
178 
185  class OSMSCOUT_API RoutingParameter CLASS_FINAL
186  {
187  private:
188  BreakerRef breaker;
189  RoutingProgressRef progress;
190 
191  public:
192  void SetBreaker(const BreakerRef& breaker);
193  void SetProgress(const RoutingProgressRef& progress);
194 
196  {
197  return breaker;
198  }
199 
201  {
202  return progress;
203  }
204  };
205 
212  {
213  protected:
220  struct RNode
221  {
225  ObjectFileRef object;
226 
227  double currentCost=0;
228  double estimateCost=0;
229  double overallCost=0;
230 
231  bool access=true;
232 
233  RNode() = default;
234 
235  RNode(const DBId& id,
236  const RouteNodeRef& node,
237  const ObjectFileRef& object)
238  : id(id),
239  node(node),
240  object(object)
241  {
242  // no code
243  }
244 
245  RNode(const DBId& id,
246  const RouteNodeRef& node,
247  const ObjectFileRef& object,
248  const DBId& prev)
249  : id(id),
250  node(node),
251  prev(prev),
252  object(object),
253  currentCost(0),
254  estimateCost(0),
255  overallCost(0),
256  access(true)
257  {
258  // no code
259  }
260 
261  bool operator==(const RNode& other) const
262  {
263  return id==other.id;
264  }
265 
266  bool operator<(const RNode& other) const
267  {
268  return id<other.id;
269  }
270  };
271 
272  using RNodeRef = std::shared_ptr<RNode>;
273 
275  {
276  bool operator()(const RNodeRef& a,
277  const RNodeRef& b) const
278  {
279  if (a->overallCost==b->overallCost) {
280  return a->id<b->id;
281  }
282 
283  return a->overallCost<b->overallCost;
284  }
285  };
286 
298  struct VNode
299  {
302  ObjectFileRef object;
303 
312  bool operator==(const VNode& other) const
313  {
314  return currentNode==other.currentNode;
315  }
316 
324  explicit VNode(const DBId& currentNode)
325  : currentNode(currentNode)
326  {
327  // no code
328  }
329 
340  VNode(const DBId& currentNode,
341  const ObjectFileRef& object,
342  const DBId& previousNode)
343  : currentNode(currentNode),
344  previousNode(previousNode),
345  object(object)
346  {
347  // no code
348  }
349  };
350 
356  {
357  size_t operator()(const VNode& node) const
358  {
359  return std::hash<Id>()(node.currentNode.id) ^
360  std::hash<DatabaseId>()(node.currentNode.database);
361  }
362  };
363 
364  using OpenList = std::set<RNodeRef, RNodeCostCompare>;
365  using OpenListRef = std::set<RNodeRef, RNodeCostCompare>::iterator;
366 
367  using OpenMap = std::unordered_map<DBId, OpenListRef>;
368  using ClosedSet = std::unordered_set<VNode, ClosedNodeHasher>;
369 
370  public:
372  static const char* const FILENAME_INTERSECTIONS_DAT;
374  static const char* const FILENAME_INTERSECTIONS_IDX;
375 
377  static const char* const DEFAULT_FILENAME_BASE;
378 
379  static std::string GetDataFilename(const std::string& filenamebase);
380  static std::string GetData2Filename(const std::string& filenamebase);
381  static std::string GetIndexFilename(const std::string& filenamebase);
382 
383  public:
384  RoutingService();
385  virtual ~RoutingService();
386  };
387 
388 }
389 
390 #endif /* OSMSCOUT_ROUTINGSERVICE_H */
391 
Distance GetDistance() const
Definition: RoutingService.h:114
bool operator<(const RNode &other) const
Definition: RoutingService.h:266
bool operator==(const RNode &other) const
Definition: RoutingService.h:261
static const char *const FILENAME_INTERSECTIONS_IDX
Relative filename of the intersection index file.
Definition: RoutingService.h:374
Definition: RoutingService.h:220
std::unordered_set< VNode, ClosedNodeHasher > ClosedSet
Definition: RoutingService.h:368
bool operator==(const VNode &other) const
Definition: RoutingService.h:312
DBId id
The file offset of the current route node.
Definition: RoutingService.h:222
std::shared_ptr< Breaker > BreakerRef
Definition: Breaker.h:65
ObjectFileRef GetObjectFileRef() const
Definition: RoutingService.h:82
Definition: RoutingService.h:153
Definition: Area.h:86
size_t operator()(const VNode &node) const
Definition: RoutingService.h:357
bool operator()(const RNodeRef &a, const RNodeRef &b) const
Definition: RoutingService.h:276
ObjectFileRef object
The object (way/area) visited from the current route node.
Definition: RoutingService.h:302
RoutingProgressRef GetProgress() const
Definition: RoutingService.h:200
DBId previousNode
FileOffset of the previous route node.
Definition: RoutingService.h:301
Definition: RoutingService.h:274
std::set< RNodeRef, RNodeCostCompare >::iterator OpenListRef
Definition: RoutingService.h:365
RouteNodeRef node
The current route node.
Definition: RoutingService.h:223
std::shared_ptr< RoutingProgress > RoutingProgressRef
Definition: RoutingService.h:177
Definition: RoutingService.h:355
BreakerRef GetBreaker() const
Definition: RoutingService.h:195
Definition: Area.h:38
DatabaseId database
Definition: DBFileOffset.h:40
std::unordered_map< DBId, OpenListRef > OpenMap
Definition: RoutingService.h:367
DBId prev
The file offset of the previous route node.
Definition: RoutingService.h:224
#define CLASS_FINAL
Definition: Compiler.h:26
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
static const char *const FILENAME_INTERSECTIONS_DAT
Relative filename of the intersection data file.
Definition: RoutingService.h:372
std::shared_ptr< RNode > RNodeRef
Definition: RoutingService.h:272
ObjectFileRef object
The object (way/area) visited from the current route node.
Definition: RoutingService.h:225
VNode(const DBId &currentNode)
Definition: RoutingService.h:324
RoutePosition GetRoutePosition() const
Definition: RoutingService.h:109
Definition: RoutingService.h:211
std::shared_ptr< RouteNode > RouteNodeRef
Definition: RouteNode.h:160
std::set< RNodeRef, RNodeCostCompare > OpenList
Definition: RoutingService.h:364
bool IsValid() const
Definition: RoutingService.h:77
VNode(const DBId &currentNode, const ObjectFileRef &object, const DBId &previousNode)
Definition: RoutingService.h:340
Id id
Definition: DBFileOffset.h:41
size_t GetNodeIndex() const
Definition: RoutingService.h:87
static const char *const DEFAULT_FILENAME_BASE
Relative filebase name for touting data as generated by default by the importer.
Definition: RoutingService.h:377
RNode(const DBId &id, const RouteNodeRef &node, const ObjectFileRef &object, const DBId &prev)
Definition: RoutingService.h:245
Definition: DBFileOffset.h:38
DatabaseId GetDatabaseId() const
Definition: RoutingService.h:92
DBId currentNode
FileOffset of this route node.
Definition: RoutingService.h:300
Definition: Progress.h:34
Definition: RoutingService.h:298
RNode(const DBId &id, const RouteNodeRef &node, const ObjectFileRef &object)
Definition: RoutingService.h:235
uint32_t DatabaseId
Definition: DBFileOffset.h:30