Eclipse SUMO - Simulation of Urban MObility
ROVehicle.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // A vehicle as used by router
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <iostream>
30 #include <utils/common/ToString.h>
36 #include "RORouteDef.h"
37 #include "RORoute.h"
38 #include "ROHelper.h"
39 #include "RONet.h"
40 #include "ROLane.h"
41 #include "ROVehicle.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  RORouteDef* route, const SUMOVTypeParameter* type,
49  const RONet* net, MsgHandler* errorHandler)
50  : RORoutable(pars, type), myRoute(route) {
51  getParameter().stops.clear();
52  if (route != nullptr && route->getFirstRoute() != nullptr) {
53  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = route->getFirstRoute()->getStops().begin(); s != route->getFirstRoute()->getStops().end(); ++s) {
54  addStop(*s, net, errorHandler);
55  }
56  }
57  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = pars.stops.begin(); s != pars.stops.end(); ++s) {
58  addStop(*s, net, errorHandler);
59  }
60  if (pars.via.size() != 0) {
61  // via takes precedence over stop edges
62  // XXX check for inconsistencies #2275
63  myStopEdges.clear();
64  for (std::vector<std::string>::const_iterator it = pars.via.begin(); it != pars.via.end(); ++it) {
65  assert(net->getEdge(*it) != 0);
66  myStopEdges.push_back(net->getEdge(*it));
67  }
68  }
69 }
70 
71 
72 void
73 ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
74  const ROEdge* stopEdge = net->getEdgeForLaneID(stopPar.lane);
75  assert(stopEdge != 0); // was checked when parsing the stop
76  if (stopEdge->prohibits(this)) {
77  if (errorHandler != nullptr) {
78  errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
79  }
80  return;
81  }
82  // where to insert the stop
83  std::vector<SUMOVehicleParameter::Stop>::iterator iter = getParameter().stops.begin();
84  ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
85  if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(getParameter().stops.size())) {
86  if (getParameter().stops.size() > 0) {
87  iter = getParameter().stops.end();
88  edgeIter = myStopEdges.end();
89  }
90  } else {
91  if (stopPar.index == STOP_INDEX_FIT) {
93  ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
94  if (stopEdgeIt == edges.end()) {
95  iter = getParameter().stops.end();
96  edgeIter = myStopEdges.end();
97  } else {
98  while (iter != getParameter().stops.end()) {
99  if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
100  break;
101  }
102  ++iter;
103  ++edgeIter;
104  }
105  }
106  } else {
107  iter += stopPar.index;
108  edgeIter += stopPar.index;
109  }
110  }
111  getParameter().stops.insert(iter, stopPar);
112  myStopEdges.insert(edgeIter, stopEdge);
113 }
114 
115 
117 
118 
119 const ROEdge*
121  return myRoute->getFirstRoute()->getFirst();
122 }
123 
124 
125 void
127  const bool removeLoops, MsgHandler* errorHandler) {
129  std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
130  RORouteDef* const routeDef = getRouteDefinition();
131  // check if the route definition is valid
132  if (routeDef == nullptr) {
133  errorHandler->inform(noRouteMsg);
134  myRoutingSuccess = false;
135  return;
136  }
137  RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
138  if (current == nullptr || current->size() == 0) {
139  delete current;
140  errorHandler->inform(noRouteMsg);
141  myRoutingSuccess = false;
142  return;
143  }
144  // check whether we have to evaluate the route for not containing loops
145  if (removeLoops) {
146  const ROEdge* requiredStart = (getParameter().departPosProcedure == DEPART_POS_GIVEN
147  || getParameter().departLaneProcedure == DEPART_LANE_GIVEN ? current->getEdgeVector().front() : 0);
148  const ROEdge* requiredEnd = (getParameter().arrivalPosProcedure == ARRIVAL_POS_GIVEN
149  || getParameter().arrivalLaneProcedure == ARRIVAL_LANE_GIVEN ? current->getEdgeVector().back() : 0);
150  current->recheckForLoops(getMandatoryEdges(requiredStart, requiredEnd));
151  // check whether the route is still valid
152  if (current->size() == 0) {
153  delete current;
154  errorHandler->inform(noRouteMsg + " (after removing loops)");
155  myRoutingSuccess = false;
156  return;
157  }
158  }
159  // add built route
160  routeDef->addAlternative(router, this, current, getDepartureTime());
161  myRoutingSuccess = true;
162 }
163 
164 
166 ROVehicle::getMandatoryEdges(const ROEdge* requiredStart, const ROEdge* requiredEnd) const {
167  ConstROEdgeVector mandatory;
168  if (requiredStart) {
169  mandatory.push_back(requiredStart);
170  }
171  for (const ROEdge* e : getStopEdges()) {
172  if (e->isInternal()) {
173  // the edges before and after the internal edge are mandatory
174  const ROEdge* before = e->getNormalBefore();
175  const ROEdge* after = e->getNormalAfter();
176  if (mandatory.size() == 0 || after != mandatory.back()) {
177  mandatory.push_back(before);
178  mandatory.push_back(after);
179  }
180  } else {
181  if (mandatory.size() == 0 || e != mandatory.back()) {
182  mandatory.push_back(e);
183  }
184  }
185  }
186  if (requiredEnd) {
187  if (mandatory.size() < 2 || mandatory.back() != requiredEnd) {
188  mandatory.push_back(requiredEnd);
189  }
190  }
191  return mandatory;
192 }
193 
194 
195 void
196 ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
197  if (typeos != nullptr && getType() != nullptr && !getType()->saved) {
198  getType()->write(*typeos);
199  getType()->saved = true;
200  }
201  if (getType() != nullptr && !getType()->saved) {
202  getType()->write(os);
203  getType()->saved = asAlternatives;
204  }
205 
206  const bool writeTrip = options.exists("write-trips") && options.getBool("write-trips");
207  const bool writeGeoTrip = writeTrip && options.getBool("write-trips.geo");
208  // write the vehicle (new style, with included routes)
209  getParameter().write(os, options, writeTrip ? SUMO_TAG_TRIP : SUMO_TAG_VEHICLE);
210 
211  // save the route
212  if (writeTrip) {
214  const ROEdge* from = nullptr;
215  const ROEdge* to = nullptr;
216  if (edges.size() > 0) {
217  if (edges.front()->isTazConnector()) {
218  if (edges.size() > 1) {
219  from = edges[1];
220  }
221  } else {
222  from = edges[0];
223  }
224  if (edges.back()->isTazConnector()) {
225  if (edges.size() > 1) {
226  to = edges[edges.size() - 2];
227  }
228  } else {
229  to = edges[edges.size() - 1];
230  }
231  }
232  if (from != nullptr) {
233  if (writeGeoTrip) {
234  Position fromPos = from->getLanes()[0]->getShape().positionAtOffset2D(0);
235  if (GeoConvHelper::getFinal().usingGeoProjection()) {
238  os.writeAttr(SUMO_ATTR_FROMLONLAT, fromPos);
240  } else {
241  os.writeAttr(SUMO_ATTR_FROMXY, fromPos);
242  }
243  } else {
244  os.writeAttr(SUMO_ATTR_FROM, from->getID());
245  }
246  }
247  if (to != nullptr) {
248  if (writeGeoTrip) {
249  Position toPos = to->getLanes()[0]->getShape().positionAtOffset2D(to->getLanes()[0]->getShape().length2D());
253  os.writeAttr(SUMO_ATTR_TOLONLAT, toPos);
255  } else {
256  os.writeAttr(SUMO_ATTR_TOXY, toPos);
257  }
258  } else {
259  os.writeAttr(SUMO_ATTR_TO, to->getID());
260  }
261  }
262  if (getParameter().via.size() > 0) {
263  if (writeGeoTrip) {
264  PositionVector viaPositions;
265  for (const std::string& viaID : getParameter().via) {
266  const ROEdge* viaEdge = RONet::getInstance()->getEdge(viaID);
267  assert(viaEdge != nullptr);
268  Position viaPos = viaEdge->getLanes()[0]->getShape().positionAtOffset2D(viaEdge->getLanes()[0]->getShape().length2D() / 2);
269  viaPositions.push_back(viaPos);
270  }
271  if (GeoConvHelper::getFinal().usingGeoProjection()) {
272  for (int i = 0; i < (int)viaPositions.size(); i++) {
273  GeoConvHelper::getFinal().cartesian2geo(viaPositions[i]);
274  }
276  os.writeAttr(SUMO_ATTR_VIALONLAT, viaPositions);
278  } else {
279  os.writeAttr(SUMO_ATTR_VIAXY, viaPositions);
280  }
281 
282  } else {
284  }
285  }
286  } else {
287  myRoute->writeXMLDefinition(os, this, asAlternatives, options.getBool("exit-times"));
288  }
289  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator stop = getParameter().stops.begin(); stop != getParameter().stops.end(); ++stop) {
290  stop->write(os);
291  }
293  os.closeTag();
294 }
295 
296 
297 /****************************************************************************/
298 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
std::string lane
The lane to stop at.
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:357
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:163
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
Structure representing possible vehicle parameter.
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:27
const SUMOVehicleParameter & getParameter() const
Returns the definition of the vehicle / person parameter.
Definition: RORoutable.h:74
bool saved
Information whether this type was already saved (needed by routers)
void addAlternative(SUMOAbstractRouter< ROEdge, ROVehicle > &router, const ROVehicle *const, RORoute *current, SUMOTime begin)
Adds an alternative to the list of routes.
Definition: RORouteDef.cpp:264
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
const RORoute * getFirstRoute() const
Definition: RORouteDef.h:101
The position is given.
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:262
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:94
const int STOP_INDEX_FIT
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:57
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:182
const std::string & getID() const
Returns the id.
Definition: Named.h:77
The arrival position is given.
ROVehicle(const SUMOVehicleParameter &pars, RORouteDef *route, const SUMOVTypeParameter *type, const RONet *net, MsgHandler *errorHandler=0)
Constructor.
Definition: ROVehicle.cpp:47
RORouteDef *const myRoute
The route the vehicle takes.
Definition: ROVehicle.h:141
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:56
The lane is given.
std::vector< Stop > stops
List of the stops the vehicle will make, TraCI may add entries here.
RORoute * buildCurrentRoute(SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime begin, const ROVehicle &veh) const
Triggers building of the complete route (via preComputeCurrentRoute) or returns precomputed route...
Definition: RORouteDef.cpp:84
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:73
A routable thing such as a vehicle or person.
Definition: RORoutable.h:55
const ROEdge * getDepartEdge() const
Returns the first edge the vehicle takes.
Definition: ROVehicle.cpp:120
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
const ROEdge * getNormalAfter() const
if this edge is an internal edge, return its first normal successor, otherwise the edge itself ...
Definition: ROEdge.cpp:274
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
int gPrecisionGeo
Definition: StdDefs.cpp:28
A list of positions.
ConstROEdgeVector getMandatoryEdges(const ROEdge *requiredStart, const ROEdge *requiredEnd) const
compute mandatory edges
Definition: ROVehicle.cpp:166
const int STOP_INDEX_END
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:184
bool exists(const std::string &name) const
Returns the information whether the named option is known.
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE, const std::string &typeID="") const
Writes the parameters as a beginning element.
const ConstROEdgeVector & getStopEdges() const
Definition: ROVehicle.h:100
double endPos
The stopping position end.
const std::string & getID() const
Returns the id of the routable.
Definition: RORoutable.h:94
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:95
A basic edge for routing applications.
Definition: ROEdge.h:73
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:76
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
Definition of vehicle stop (position and duration)
int index
at which position in the stops list
std::vector< std::string > via
List of the via-edges the vehicle must visit.
SUMOAbstractRouter< E, V > & getVehicleRouter() const
int size() const
Returns the number of edges in this route.
Definition: RORoute.h:146
void write(OutputDevice &dev) const
Writes the vtype.
The router&#39;s network representation.
Definition: RONet.h:64
Structure representing possible vehicle parameter.
const SUMOVTypeParameter * getType() const
Returns the type of the routable.
Definition: RORoutable.h:85
ConstROEdgeVector myStopEdges
The edges where the vehicle stops.
Definition: ROVehicle.h:144
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:155
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
A storage for options typed value containers)
Definition: OptionsCont.h:90
The arrival lane is given.
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROVehicle.cpp:126
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:56
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
void recheckForLoops(const ConstROEdgeVector &mandatory)
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:78
const std::vector< ROLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: ROEdge.h:475
description of a vehicle
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
a single trip definition (used by router)
const ROEdge * getNormalBefore() const
if this edge is an internal edge, return its first normal predecessor, otherwise the edge itself ...
Definition: ROEdge.cpp:263
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:153
virtual ~ROVehicle()
Destructor.
Definition: ROVehicle.cpp:116
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:196
A complete router&#39;s route.
Definition: RORoute.h:55
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.