libosmscout  1.1.1
RouteInstructionAgent.h
Go to the documentation of this file.
1 #ifndef LIBOSMSCOUT_ROUTEINSTRUCTIONAGENT_H
2 #define LIBOSMSCOUT_ROUTEINSTRUCTIONAGENT_H
3 
4 /*
5  This source is part of the libosmscout library
6  Copyright (C) 2019 Lukas Karas
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 
26 
27 namespace osmscout {
28 
29 template <typename RouteInstruction>
30 struct RouteInstructionsMessage CLASS_FINAL : public NavigationMessage
31 {
32 public:
33  std::list<RouteInstruction> instructions;
34 
35  inline RouteInstructionsMessage(const Timestamp& timestamp, const std::list<RouteInstruction> &instructions):
36  NavigationMessage(timestamp), instructions(instructions)
37  {}
38 };
39 
40 template <typename RouteInstruction>
41 struct NextRouteInstructionsMessage CLASS_FINAL : public NavigationMessage
42 {
43 public:
44  RouteInstruction nextRouteInstruction;
45 
46  inline NextRouteInstructionsMessage(const Timestamp& timestamp, const RouteInstruction &nextRouteInstruction):
47  NavigationMessage(timestamp), nextRouteInstruction(nextRouteInstruction)
48  {}
49 };
50 
51 template <typename RouteInstruction, typename RouteInstructionBuilder>
52 class RouteInstructionAgent CLASS_FINAL : public NavigationAgent
53 {
54 private:
55  RouteDescriptionRef prevRoute;
56  std::list<RouteInstruction> instructions;
57 
58 public:
59  RouteInstructionAgent();
60 
61  std::list<NavigationMessageRef> Process(const NavigationMessageRef& message) override;
62 
63 };
64 
65 template <typename RouteInstruction, typename RouteInstructionBuilder>
66 RouteInstructionAgent<RouteInstruction, RouteInstructionBuilder>::RouteInstructionAgent() = default;
67 
68 template <typename RouteInstruction, typename RouteInstructionBuilder>
69 std::list<NavigationMessageRef> RouteInstructionAgent<RouteInstruction, RouteInstructionBuilder>
70  ::Process(const NavigationMessageRef& message)
71 {
72  std::list<NavigationMessageRef> result;
73 
74  auto* positionMessage = dynamic_cast<PositionAgent::PositionMessage*>(message.get());
75  if (positionMessage==nullptr) {
76  return result;
77  }
78 
79  if (!positionMessage->route){
80  // we don't have route description yet
81  return result;
82  }
83  if (positionMessage->position.state != PositionAgent::PositionState::OnRoute &&
84  positionMessage->position.state != PositionAgent::PositionState::EstimateInTunnel) {
85  // what route instruction we should show?
86  return result;
87  }
88 
89  RouteInstructionBuilder builder;
90 
91  Timestamp now = positionMessage->timestamp;
92  if (prevRoute != positionMessage->route){
93  instructions=builder.GenerateRouteInstructions(positionMessage->position.routeNode,
94  positionMessage->route->Nodes().end());
95  result.push_back(std::make_shared<RouteInstructionsMessage<RouteInstruction>>(now,instructions));
96  }
97 
98  // remove instructions behind our back (pop from the front of the list)
99  bool updated=false;
100  while (!instructions.empty() &&
101  positionMessage->position.routeNode != positionMessage->route->Nodes().end() &&
102  instructions.front().GetDistance() <= positionMessage->position.routeNode->GetDistance()){
103 
104  instructions.pop_front();
105  updated=true;
106  }
107  if (updated){
108  result.push_back(std::make_shared<RouteInstructionsMessage<RouteInstruction>>(now,instructions));
109  }
110 
111  // next route instruction
112  RouteInstruction nextInstruction = builder.GenerateNextRouteInstruction(positionMessage->position.routeNode,
113  positionMessage->route->Nodes().end(),
114  positionMessage->position.coord);
115  result.push_back(std::make_shared<NextRouteInstructionsMessage<RouteInstruction>>(now,nextInstruction));
116 
117  prevRoute=positionMessage->route;
118 
119  return result;
120 }
121 
122 }
123 
124 #endif //LIBOSMSCOUT_ROUTEINSTRUCTIONAGENT_H
std::chrono::system_clock::time_point Timestamp
Definition: Time.h:27
Definition: Area.h:38
#define CLASS_FINAL
Definition: Compiler.h:26
std::list< RouteInstruction > instructions
Definition: RouteInstructionAgent.h:33
RouteInstructionsMessage(const Timestamp &timestamp, const std::list< RouteInstruction > &instructions)
Definition: RouteInstructionAgent.h:35
std::shared_ptr< RouteDescription > RouteDescriptionRef
Definition: RouteDescription.h:814
RouteInstruction nextRouteInstruction
Definition: RouteInstructionAgent.h:44
std::shared_ptr< NavigationMessage > NavigationMessageRef
Definition: Engine.h:56
NextRouteInstructionsMessage(const Timestamp &timestamp, const RouteInstruction &nextRouteInstruction)
Definition: RouteInstructionAgent.h:46
Definition: Engine.h:48