libosmscout  1.1.1
RoutingProfile.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_ROUTINGPROFILE_H
2 #define OSMSCOUT_ROUTINGPROFILE_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2009 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 <map>
24 #include <memory>
25 #include <vector>
26 #include <iostream>
27 
28 #include <osmscout/OSMScoutTypes.h>
29 #include <osmscout/TypeConfig.h>
30 #include <osmscout/TypeFeatures.h>
31 #include <osmscout/FeatureReader.h>
32 
33 #include <osmscout/Way.h>
34 #include <osmscout/Area.h>
35 
36 #include <osmscout/util/Time.h>
37 #include <osmscout/util/String.h>
38 #include <osmscout/util/Logger.h>
39 
41 
42 namespace osmscout {
43 
48  enum Grade: uint8_t
49  {
55  };
56 
61  struct SpeedVariant
62  {
64  std::array<double,5> speed{NAN, NAN, NAN, NAN, NAN};
65 
66  const double& operator[](Grade grade) const
67  {
68  uint8_t i=static_cast<uint8_t>(grade);
69  assert(i>=1 && i<=5);
70  return speed[i-1];
71  }
72 
73  double& operator[](Grade grade)
74  {
75  uint8_t i=static_cast<uint8_t>(grade);
76  assert(i>=1 && i<=5);
77  return speed[i-1];
78  }
79 
84  void SetupValues()
85  {
86  double speedVal=0;
87  for (size_t i=0; i<5; i++){
88  if (std::isnan(speed[i])){
89  speed[i]=speedVal;
90  } else {
91  speedVal=speed[i];
92  }
93  }
94  }
95 
96  double Min() const
97  {
98  return *std::min_element(speed.begin(), speed.end());
99  }
100 
101  double Max() const
102  {
103  return *std::max_element(speed.begin(), speed.end());
104  }
105 
106  static SpeedVariant Fill(double speed)
107  {
108  return SpeedVariant({speed, speed, speed, speed, speed});
109  }
110  };
118  {
119  public:
120  virtual ~RoutingProfile();
121 
122  virtual Vehicle GetVehicle() const = 0;
123  virtual Distance GetCostLimitDistance() const = 0;
124  virtual double GetCostLimitFactor() const = 0;
125 
126  virtual bool CanUse(const RouteNode& currentNode,
127  const std::vector<ObjectVariantData>& objectVariantData,
128  size_t pathIndex) const = 0;
129  virtual bool CanUse(const Area& area) const = 0;
130  virtual bool CanUse(const Way& way) const = 0;
131  virtual bool CanUseForward(const Way& way) const = 0;
132  virtual bool CanUseBackward(const Way& way) const = 0;
133 
138  virtual double GetCosts(const RouteNode& currentNode,
139  const std::vector<ObjectVariantData>& objectVariantData,
140  size_t inPathIndex,
141  size_t outPathIndex) const = 0;
142 
146  virtual double GetCosts(const Area& area,
147  const Distance &distance) const = 0;
148 
152  virtual double GetCosts(const Way& way,
153  const Distance &distance) const = 0;
154 
158  virtual double GetCosts(const Distance &distance) const = 0;
159 
163  virtual std::string GetCostString(double cost) const = 0;
164 
165  virtual Duration GetTime(const Area& area,
166  const Distance &distance) const = 0;
167  virtual Duration GetTime(const Way& way,
168  const Distance &distance) const = 0;
169  };
170 
171  using RoutingProfileRef = std::shared_ptr<RoutingProfile>;
172 
179  {
180  protected:
189  std::vector<SpeedVariant> speeds;
190  double minSpeed;
191  double maxSpeed;
193 
194  protected:
195  template <typename Obj>
196  Duration GetTime2(const Obj& obj,
197  const Distance &distance) const
198  {
199  double speed=vehicleMaxSpeed;
200 
201  const MaxSpeedFeatureValue *maxSpeedValue=maxSpeedReader.GetValue(obj.GetFeatureValueBuffer());
202 
203  if (maxSpeedValue!=nullptr &&
204  maxSpeedValue->GetMaxSpeed()>0 &&
205  speed > maxSpeedValue->GetMaxSpeed()) {
206  speed=maxSpeedValue->GetMaxSpeed();
207  }
208 
209  Grade grade=SolidGrade;
210  const GradeFeatureValue *gradeValue=gradeReader.GetValue(obj.GetFeatureValueBuffer());
211  if (gradeValue!=nullptr){
212  grade=static_cast<Grade>(gradeValue->GetGrade());
213  }
214 
215  speed=std::min(speed,speeds[obj.GetType()->GetIndex()][grade]);
216 
217  return DurationOfHours(distance.As<Kilometer>()/speed);
218  }
219 
220  public:
221  explicit AbstractRoutingProfile(const TypeConfigRef& typeConfig);
222 
223  void SetVehicle(Vehicle vehicle);
224  void SetVehicleMaxSpeed(double maxSpeed);
225 
226  virtual void ParametrizeForFoot(const TypeConfig& typeConfig,
227  double maxSpeed);
228  virtual void ParametrizeForBicycle(const TypeConfig& typeConfig,
229  double maxSpeed);
230  virtual bool ParametrizeForCar(const TypeConfig& typeConfig,
231  const std::map<std::string,double>& speedMap,
232  double maxSpeed);
233 
234  Vehicle GetVehicle() const override
235  {
236  return vehicle;
237  }
238 
239  double GetVehicleMaxSpeed() const
240  {
241  return vehicleMaxSpeed;
242  }
243 
244  void SetCostLimitDistance(const Distance &costLimitDistance);
245 
246  Distance GetCostLimitDistance() const override
247  {
248  return costLimitDistance;
249  }
250 
251  void SetCostLimitFactor(double costLimitFactor);
252 
253  double GetCostLimitFactor() const override
254  {
255  return costLimitFactor;
256  }
257 
258  std::string GetCostString(double cost) const override
259  {
260  return std::to_string(cost);
261  }
262 
269  void AddType(const TypeInfoRef& type, double speed);
270 
278  void AddType(const TypeInfoRef& type, SpeedVariant speed);
279 
280  bool CanUse(const RouteNode& currentNode,
281  const std::vector<ObjectVariantData>& objectVariantData,
282  size_t pathIndex) const override;
283  bool CanUse(const Area& area) const override;
284  bool CanUse(const Way& way) const override;
285  bool CanUseForward(const Way& way) const override;
286  bool CanUseBackward(const Way& way) const override;
287 
288  Duration GetTime(const Area& area,
289  const Distance &distance) const override
290  {
291  return GetTime2(area,distance);
292  }
293 
294  Duration GetTime(const Way& way,
295  const Distance &distance) const override
296  {
297  return GetTime2(way,distance);
298  }
299  };
300 
306  {
307  public:
308  explicit ShortestPathRoutingProfile(const TypeConfigRef& typeConfig);
309 
310  double GetCosts(const RouteNode& currentNode,
311  const std::vector<ObjectVariantData>& /*objectVariantData*/,
312  size_t /*inPathIndex*/,
313  size_t outPathIndex) const override
314  {
315  return currentNode.paths[outPathIndex].distance.As<Kilometer>();
316  }
317 
318  double GetCosts(const Area& /*area*/,
319  const Distance &distance) const override
320  {
321  return distance.As<Kilometer>();
322  }
323 
324  double GetCosts(const Way& /*way*/,
325  const Distance &distance) const override
326  {
327  return distance.As<Kilometer>();
328  }
329 
330  double GetCosts(const Distance &distance) const override
331  {
332  return distance.As<Kilometer>();
333  }
334 
335  std::string GetCostString(double cost) const override
336  {
337  return Kilometers(cost).AsString();
338  }
339  };
340 
341  using ShortestPathRoutingProfileRef = std::shared_ptr<ShortestPathRoutingProfile>;
342 
349  {
350  protected:
351  bool applyJunctionPenalty=true;
352  Distance penaltySameType=Meters(40);
353  Distance penaltyDifferentType=Meters(250);
354  HourDuration maxPenalty=std::chrono::seconds(10);
355 
356  public:
357  explicit FastestPathRoutingProfile(const TypeConfigRef& typeConfig);
358 
359  void ParametrizeForFoot(const TypeConfig& typeConfig,
360  double maxSpeed) override
361  {
362  applyJunctionPenalty=false;
363  AbstractRoutingProfile::ParametrizeForFoot(typeConfig, maxSpeed);
364  }
365 
369  void ParametrizeForBicycle(const TypeConfig& typeConfig,
370  double maxSpeed) override
371  {
372  applyJunctionPenalty = true;
373  costLimitDistance *= 2;
374  costLimitFactor *= 1.5;
375  AbstractRoutingProfile::ParametrizeForBicycle(typeConfig, maxSpeed);
376  }
377 
381  bool ParametrizeForCar(const TypeConfig& typeConfig,
382  const std::map<std::string,double>& speedMap,
383  double maxSpeed) override
384  {
385  applyJunctionPenalty = true;
386  costLimitDistance *= 2;
387  costLimitFactor *= 1.5;
388  return AbstractRoutingProfile::ParametrizeForCar(typeConfig, speedMap, maxSpeed);
389  }
390 
391  bool HasJunctionPenalty() const
392  {
393  return applyJunctionPenalty;
394  }
395 
396  void SetJunctionPenalty(bool b)
397  {
398  applyJunctionPenalty=b;
399  }
400 
401  Distance GetPenaltySameType() const
402  {
403  return penaltySameType;
404  }
405 
406  void SetPenaltySameType(const Distance &d)
407  {
408  penaltySameType=d;
409  }
410 
411  Distance GetPenaltyDifferentType() const
412  {
413  return penaltyDifferentType;
414  }
415 
416  void SetPenaltyDifferentType(const Distance &d)
417  {
418  penaltyDifferentType=d;
419  }
420 
422  {
423  return maxPenalty;
424  }
425 
427  {
428  maxPenalty=d;
429  }
430 
431  double GetCosts(const RouteNode& currentNode,
432  const std::vector<ObjectVariantData>& objectVariantData,
433  size_t inPathIndex,
434  size_t outPathIndex) const override
435  {
436  assert(currentNode.paths.size() > inPathIndex);
437  assert(currentNode.paths.size() > outPathIndex);
438  auto inObjIndex=currentNode.paths[inPathIndex].objectIndex;
439  auto outObjIndex=currentNode.paths[outPathIndex].objectIndex;
440  auto inVariantIndex=currentNode.objects[inObjIndex].objectVariantIndex;
441  auto outVariantIndex=currentNode.objects[outObjIndex].objectVariantIndex;
442  assert(objectVariantData.size() > inVariantIndex);
443  assert(objectVariantData.size() > outVariantIndex);
444  const ObjectVariantData &inPathVariant=objectVariantData[inVariantIndex];
445  const ObjectVariantData &outPathVariant=objectVariantData[outVariantIndex];
446 
447  auto GetMaxSpeed = [&](const ObjectVariantData &variant) -> double {
448  TypeInfoRef type=variant.type;
449  Grade grade=static_cast<Grade>(variant.grade);
450  double speed=speeds[type->GetIndex()][grade];
451  if (speed<=0){
452  log.Warn() << "Infinite cost for type " << type->GetName();
453  }
454  if (variant.maxSpeed > 0 && speed>variant.maxSpeed) {
455  speed=variant.maxSpeed;
456  }
457  return speed;
458  };
459 
460  // price of ride to target node using outPath
461  double speed=std::min(vehicleMaxSpeed,GetMaxSpeed(outPathVariant));
462  double outPrice = speed <= 0 ?
463  std::numeric_limits<double>::infinity() :
464  currentNode.paths[outPathIndex].distance.As<Kilometer>() / speed;
465 
466  // add penalty for junction
467  // it is estimate without considering real junction geometry
468  double junctionPenalty{0};
469  if (applyJunctionPenalty && inObjIndex!=outObjIndex){
470  auto penaltyDistance = inPathVariant.type != outPathVariant.type ?
471  penaltyDifferentType :
472  penaltySameType;
473 
474  double minSpeed=std::min(GetMaxSpeed(inPathVariant),GetMaxSpeed(outPathVariant));
475  junctionPenalty = minSpeed <= 0 ?
476  std::numeric_limits<double>::infinity() :
477  penaltyDistance.As<Kilometer>() / minSpeed;
478 
479  junctionPenalty = std::min(junctionPenalty, maxPenalty.count());
480 #if defined(DEBUG_ROUTING)
481  std::cout << " Add junction penalty " << GetCostString(junctionPenalty) << std::endl;
482 #endif
483  }
484 
485  return outPrice + junctionPenalty;
486  }
487 
488  double GetCosts(const Area& area,
489  const Distance &distance) const override
490  {
491  auto time=GetTime2(area,distance);
492  return std::chrono::duration_cast<HourDuration>(time).count();
493  }
494 
495  double GetCosts(const Way& way,
496  const Distance &distance) const override
497  {
498  auto time=GetTime2(way,distance);
499  return std::chrono::duration_cast<HourDuration>(time).count();
500  }
501 
502  double GetCosts(const Distance &distance) const override
503  {
504  double speed=maxSpeed;
505 
506  speed=std::min(vehicleMaxSpeed,speed);
507 
508  return distance.As<Kilometer>()/speed;
509  }
510 
511  std::string GetCostString(double cost) const override
512  {
513  return DurationString(std::chrono::duration_cast<Duration>(HourDuration(cost)));
514  }
515 
516  };
517 
518  using FastestPathRoutingProfileRef = std::shared_ptr<FastestPathRoutingProfile>;
519 }
520 
521 #endif
Distance GetPenaltyDifferentType() const
Definition: RoutingProfile.h:411
double GetCosts(const Area &, const Distance &distance) const override
Definition: RoutingProfile.h:318
OSMSCOUT_API Log log
virtual bool ParametrizeForCar(const TypeConfig &typeConfig, const std::map< std::string, double > &speedMap, double maxSpeed)
Duration GetTime(const Area &area, const Distance &distance) const override
Definition: RoutingProfile.h:288
double GetCosts(const RouteNode &currentNode, const std::vector< ObjectVariantData > &, size_t, size_t outPathIndex) const override
Definition: RoutingProfile.h:310
virtual void ParametrizeForFoot(const TypeConfig &typeConfig, double maxSpeed)
std::shared_ptr< ShortestPathRoutingProfile > ShortestPathRoutingProfileRef
Definition: RoutingProfile.h:341
Definition: RoutingProfile.h:348
Definition: RoutingProfile.h:52
bool HasJunctionPenalty() const
Definition: RoutingProfile.h:391
GradeFeatureValueReader gradeReader
Definition: RoutingProfile.h:184
uint8_t vehicleRouteNodeBit
Definition: RoutingProfile.h:186
Distance GetPenaltySameType() const
Definition: RoutingProfile.h:401
double GetCosts(const RouteNode &currentNode, const std::vector< ObjectVariantData > &objectVariantData, size_t inPathIndex, size_t outPathIndex) const override
Definition: RoutingProfile.h:431
Duration GetTime2(const Obj &obj, const Distance &distance) const
Definition: RoutingProfile.h:196
AccessFeatureValueReader accessReader
Definition: RoutingProfile.h:182
Definition: RoutingProfile.h:51
std::vector< SpeedVariant > speeds
maximum vehicle speed on route type and its grade
Definition: RoutingProfile.h:189
uint8_t GetMaxSpeed() const
Definition: TypeFeatures.h:961
std::shared_ptr< RoutingProfile > RoutingProfileRef
Definition: RoutingProfile.h:171
Vehicle
Definition: OSMScoutTypes.h:55
TypeInfoRef type
The type of the object.
Definition: RouteNode.h:45
double & operator[](Grade grade)
Definition: RoutingProfile.h:73
Definition: RoutingProfile.h:54
double GetCosts(const Area &area, const Distance &distance) const override
Definition: RoutingProfile.h:488
void ParametrizeForFoot(const TypeConfig &typeConfig, double maxSpeed) override
Definition: RoutingProfile.h:359
Definition: RoutingProfile.h:53
Definition: TypeFeatures.h:1009
Distance Kilometers(double km)
Definition: Distance.h:362
V * GetValue(const FeatureValueBuffer &buffer) const
Definition: FeatureReader.h:224
Duration GetTime(const Way &way, const Distance &distance) const override
Definition: RoutingProfile.h:294
double vehicleMaxSpeed
Definition: RoutingProfile.h:192
double costLimitFactor
Definition: RoutingProfile.h:188
OSMSCOUT_API std::string DurationString(const Duration &duration)
Definition: Area.h:38
double minSpeed
Definition: RoutingProfile.h:190
Definition: Distance.h:234
std::string GetCostString(double cost) const override
Definition: RoutingProfile.h:511
double GetCosts(const Distance &distance) const override
Definition: RoutingProfile.h:330
const double & operator[](Grade grade) const
Definition: RoutingProfile.h:66
std::chrono::duration< double, std::ratio< 3600 > > HourDuration
Definition: Time.h:31
double GetVehicleMaxSpeed() const
Definition: RoutingProfile.h:239
Definition: RoutingProfile.h:117
Distance GetCostLimitDistance() const override
Definition: RoutingProfile.h:246
virtual void ParametrizeForBicycle(const TypeConfig &typeConfig, double maxSpeed)
#define OSMSCOUT_API
Definition: CoreImportExport.h:45
void ParametrizeForBicycle(const TypeConfig &typeConfig, double maxSpeed) override
Definition: RoutingProfile.h:369
Definition: RoutingProfile.h:178
double maxSpeed
Definition: RoutingProfile.h:191
Definition: RouteNode.h:42
std::string GetCostString(double cost) const override
Definition: RoutingProfile.h:258
Definition: TypeFeatures.h:942
bool ParametrizeForCar(const TypeConfig &typeConfig, const std::map< std::string, double > &speedMap, double maxSpeed) override
Definition: RoutingProfile.h:381
void SetupValues()
Definition: RoutingProfile.h:84
std::string GetCostString(double cost) const override
Definition: RoutingProfile.h:335
std::shared_ptr< FastestPathRoutingProfile > FastestPathRoutingProfileRef
Definition: RoutingProfile.h:518
std::vector< ObjectData > objects
List of objects (ways, areas) that cross this route node.
Definition: RouteNode.h:125
HourDuration GetMaxPenalty() const
Definition: RoutingProfile.h:421
Distance costLimitDistance
Definition: RoutingProfile.h:187
double GetCosts(const Way &way, const Distance &distance) const override
Definition: RoutingProfile.h:495
std::array< double, 5 > speed
speed for each grade, indexed by grade-1.
Definition: RoutingProfile.h:64
Duration DurationOfHours(double hours)
Definition: Time.h:44
TypeConfigRef typeConfig
Definition: RoutingProfile.h:181
Definition: RoutingProfile.h:50
void SetPenaltyDifferentType(const Distance &d)
Definition: RoutingProfile.h:416
double GetCostLimitFactor() const override
Definition: RoutingProfile.h:253
std::shared_ptr< TypeInfo > TypeInfoRef
Definition: TypeConfig.h:58
Definition: RoutingProfile.h:61
Distance Meters(double m)
Definition: Distance.h:358
Log & Warn(bool state)
Definition: Logger.h:462
Vehicle vehicle
Definition: RoutingProfile.h:185
Timestamp::duration Duration
Definition: Time.h:29
MaxSpeedFeatureValueReader maxSpeedReader
Definition: RoutingProfile.h:183
double Max() const
Definition: RoutingProfile.h:101
double Min() const
Definition: RoutingProfile.h:96
Definition: RoutingProfile.h:305
Vehicle GetVehicle() const override
Definition: RoutingProfile.h:234
Grade
Definition: RoutingProfile.h:48
static SpeedVariant Fill(double speed)
Definition: RoutingProfile.h:106
void SetPenaltySameType(const Distance &d)
Definition: RoutingProfile.h:406
double GetCosts(const Distance &distance) const override
Definition: RoutingProfile.h:502
double GetCosts(const Way &, const Distance &distance) const override
Definition: RoutingProfile.h:324
std::vector< Path > paths
List of paths that can in principle be used from this node.
Definition: RouteNode.h:126
std::shared_ptr< TypeConfig > TypeConfigRef
Definition: TypeConfig.h:1227
void SetJunctionPenalty(bool b)
Definition: RoutingProfile.h:396
uint8_t GetGrade() const
Definition: TypeFeatures.h:1028
void SetMaxPenalty(const HourDuration &d)
Definition: RoutingProfile.h:426
Definition: RouteNode.h:61