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 
RORouteDef::addAlternative
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
RORoutable::myRoutingSuccess
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:182
ToString.h
ARRIVAL_POS_GIVEN
The arrival position is given.
Definition: SUMOVehicleParameter.h:228
RORouteDef::buildCurrentRoute
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
ROVehicle::getDepartureTime
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:95
SUMOVehicleParameter::Stop::lane
std::string lane
The lane to stop at.
Definition: SUMOVehicleParameter.h:580
ARRIVAL_LANE_GIVEN
The arrival lane is given.
Definition: SUMOVehicleParameter.h:212
SUMO_ATTR_TOLONLAT
Definition: SUMOXMLDefinitions.h:640
ROHelper.h
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
ROVehicle::addStop
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:73
SUMO_ATTR_TOXY
Definition: SUMOXMLDefinitions.h:642
SUMO_ATTR_VIAXY
Definition: SUMOXMLDefinitions.h:722
OptionsCont.h
RONet::getEdge
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:153
SUMOVehicleParameter::departPosProcedure
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
Definition: SUMOVehicleParameter.h:491
MsgHandler.h
RORouteDef::getFirstRoute
const RORoute * getFirstRoute() const
Definition: RORouteDef.h:101
OutputDevice::setPrecision
void setPrecision(int precision=gPrecision)
Sets the precison or resets it to default.
Definition: OutputDevice.cpp:222
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
RORoute::getEdgeVector
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:155
SUMO_ATTR_VIALONLAT
Definition: SUMOXMLDefinitions.h:721
OptionsCont::exists
bool exists(const std::string &name) const
Returns the information whether the named option is known.
Definition: OptionsCont.cpp:130
GeoConvHelper.h
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:223
RORouteDef
Base class for a vehicle's route definition.
Definition: RORouteDef.h:56
SUMOVTypeParameter::saved
bool saved
Information whether this type was already saved (needed by routers)
Definition: SUMOVTypeParameter.h:311
RONet
The router's network representation.
Definition: RONet.h:64
RORoutable::getType
const SUMOVTypeParameter * getType() const
Returns the type of the routable.
Definition: RORoutable.h:85
SUMOVehicleParameter
Structure representing possible vehicle parameter.
Definition: SUMOVehicleParameter.h:291
ROEdge::getLanes
const std::vector< ROLane * > & getLanes() const
Returns this edge's lanes.
Definition: ROEdge.h:475
ROVehicle.h
RORouteDef.h
PositionVector
A list of positions.
Definition: PositionVector.h:46
RONet::getInstance
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:56
RORoute.h
GeoConvHelper::usingGeoProjection
bool usingGeoProjection() const
Returns whether a transformation from geo to metric coordinates will be performed.
Definition: GeoConvHelper.cpp:282
RORoute::size
int size() const
Returns the number of edges in this route.
Definition: RORoute.h:146
gPrecisionGeo
int gPrecisionGeo
Definition: StdDefs.cpp:28
Parameterised::writeParams
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
Definition: Parameterised.cpp:111
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:254
RORoute::recheckForLoops
void recheckForLoops(const ConstROEdgeVector &mandatory)
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:78
SUMO_ATTR_TO
Definition: SUMOXMLDefinitions.h:638
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
SUMO_ATTR_FROMLONLAT
Definition: SUMOXMLDefinitions.h:639
ROVehicle::ROVehicle
ROVehicle(const SUMOVehicleParameter &pars, RORouteDef *route, const SUMOVTypeParameter *type, const RONet *net, MsgHandler *errorHandler=0)
Constructor.
Definition: ROVehicle.cpp:47
MsgHandler
Definition: MsgHandler.h:44
SUMOVehicleParameter::arrivalLaneProcedure
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
Definition: SUMOVehicleParameter.h:513
GeoConvHelper::getFinal
static const GeoConvHelper & getFinal()
the coordinate transformation for writing the location element and for tracking the original coordina...
Definition: GeoConvHelper.h:106
RONet.h
SUMOVTypeParameter
Structure representing possible vehicle parameter.
Definition: SUMOVTypeParameter.h:86
DEPART_LANE_GIVEN
The lane is given.
Definition: SUMOVehicleParameter.h:116
ROVehicle::getMandatoryEdges
ConstROEdgeVector getMandatoryEdges(const ROEdge *requiredStart, const ROEdge *requiredEnd) const
compute mandatory edges
Definition: ROVehicle.cpp:166
RORoutable::getParameter
const SUMOVehicleParameter & getParameter() const
Returns the definition of the vehicle / person parameter.
Definition: RORoutable.h:74
STOP_INDEX_FIT
const int STOP_INDEX_FIT
Definition: SUMOVehicleParameter.h:72
RORoutable::getID
const std::string & getID() const
Returns the id of the routable.
Definition: RORoutable.h:94
RORouteDef::writeXMLDefinition
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:357
ROVehicle::getDepartEdge
const ROEdge * getDepartEdge() const
Returns the first edge the vehicle takes.
Definition: ROVehicle.cpp:120
OutputDevice.h
ROVehicle::getRouteDefinition
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:76
SUMOVehicleParameter::arrivalPosProcedure
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
Definition: SUMOVehicleParameter.h:519
GeoConvHelper::cartesian2geo
void cartesian2geo(Position &cartesian) const
Converts the given cartesian (shifted) position to its geo (lat/long) representation.
Definition: GeoConvHelper.cpp:294
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
RORoute
A complete router's route.
Definition: RORoute.h:55
RouterProvider::getVehicleRouter
SUMOAbstractRouter< E, V > & getVehicleRouter() const
Definition: RouterProvider.h:50
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
SUMOVehicleParameter::Stop::endPos
double endPos
The stopping position end.
Definition: SUMOVehicleParameter.h:598
ROEdge::getNormalBefore
const ROEdge * getNormalBefore() const
if this edge is an internal edge, return its first normal predecessor, otherwise the edge itself
Definition: ROEdge.cpp:263
RouterProvider
Definition: RouterProvider.h:38
SUMOVTypeParameter::write
void write(OutputDevice &dev) const
Writes the vtype.
Definition: SUMOVTypeParameter.cpp:301
SUMO_TAG_VEHICLE
description of a vehicle
Definition: SUMOXMLDefinitions.h:120
SUMO_ATTR_FROMXY
Definition: SUMOXMLDefinitions.h:641
SUMOVehicleParameter::write
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.
Definition: SUMOVehicleParameter.cpp:66
ROVehicle::getStopEdges
const ConstROEdgeVector & getStopEdges() const
Definition: ROVehicle.h:100
SUMO_ATTR_FROM
Definition: SUMOXMLDefinitions.h:637
SUMOAbstractRouter< ROEdge, ROVehicle >
RORoutable
A routable thing such as a vehicle or person.
Definition: RORoutable.h:55
StringUtils.h
SUMOVehicleParameter::departLaneProcedure
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
Definition: SUMOVehicleParameter.h:485
SUMO_ATTR_VIA
Definition: SUMOXMLDefinitions.h:720
ROEdge::prohibits
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:262
SUMOVehicleParameter::via
std::vector< std::string > via
List of the via-edges the vehicle must visit.
Definition: SUMOVehicleParameter.h:641
SUMOVehicleParameter::Stop::index
int index
at which position in the stops list
Definition: SUMOVehicleParameter.h:631
RORoute::getFirst
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:94
STOP_INDEX_END
const int STOP_INDEX_END
Definition: SUMOVehicleParameter.h:71
ROEdge
A basic edge for routing applications.
Definition: ROEdge.h:73
config.h
ROLane.h
ROVehicle::computeRoute
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROVehicle.cpp:126
ROEdge::getNormalAfter
const ROEdge * getNormalAfter() const
if this edge is an internal edge, return its first normal successor, otherwise the edge itself
Definition: ROEdge.cpp:274
ROVehicle::~ROVehicle
virtual ~ROVehicle()
Destructor.
Definition: ROVehicle.cpp:116
gPrecision
int gPrecision
the precision for floating point outputs
Definition: StdDefs.cpp:27
ROVehicle::myRoute
RORouteDef *const myRoute
The route the vehicle takes.
Definition: ROVehicle.h:141
DEPART_POS_GIVEN
The position is given.
Definition: SUMOVehicleParameter.h:140
SUMOVehicleParameter::stops
std::vector< Stop > stops
List of the stops the vehicle will make, TraCI may add entries here.
Definition: SUMOVehicleParameter.h:638
SUMOVTypeParameter.h
RONet::getEdgeForLaneID
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:163
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
ROVehicle::myStopEdges
ConstROEdgeVector myStopEdges
The edges where the vehicle stops.
Definition: ROVehicle.h:144
SUMO_TAG_TRIP
a single trip definition (used by router)
Definition: SUMOXMLDefinitions.h:146
ConstROEdgeVector
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:57
SUMOVehicleParameter::Stop
Definition of vehicle stop (position and duration)
Definition: SUMOVehicleParameter.h:566
ROVehicle::saveAsXML
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:196
RORoute::getStops
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:184