Eclipse SUMO - Simulation of Urban MObility
libsumo/Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
23 // C++ TraCI client API implementation
24 /****************************************************************************/
25 #include <config.h>
26 
27 #include <microsim/MSNet.h>
28 #include <microsim/MSLane.h>
29 #include <microsim/MSEdge.h>
30 #include <microsim/MSVehicle.h>
31 #include <libsumo/TraCIConstants.h>
32 #include "Lane.h"
33 
34 
35 namespace libsumo {
36 // ===========================================================================
37 // static member initializations
38 // ===========================================================================
39 SubscriptionResults Lane::mySubscriptionResults;
40 ContextSubscriptionResults Lane::myContextSubscriptionResults;
41 
42 
43 // ===========================================================================
44 // static member definitions
45 // ===========================================================================
46 std::vector<std::string>
47 Lane::getIDList() {
48  std::vector<std::string> ids;
49  MSLane::insertIDs(ids);
50  return ids;
51 }
52 
53 
54 int
55 Lane::getIDCount() {
56  return (int)getIDList().size();
57 }
58 
59 
60 std::string
61 Lane::getEdgeID(std::string laneID) {
62  return getLane(laneID)->getEdge().getID();
63 }
64 
65 
66 double
67 Lane::getLength(std::string laneID) {
68  return getLane(laneID)->getLength();
69 }
70 
71 
72 double
73 Lane::getMaxSpeed(std::string laneID) {
74  return getLane(laneID)->getSpeedLimit();
75 }
76 
77 
78 int
79 Lane::getLinkNumber(std::string laneID) {
80  return (int)getLane(laneID)->getLinkCont().size();
81 }
82 
83 
84 std::vector<TraCIConnection>
85 Lane::getLinks(std::string laneID) {
86  std::vector<TraCIConnection> v;
87  const MSLane* const lane = getLane(laneID);
88  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
89  for (const MSLink* const link : lane->getLinkCont()) {
90  const std::string approachedLane = link->getLane() != nullptr ? link->getLane()->getID() : "";
91  const bool hasPrio = link->havePriority();
92  const double speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
93  const bool isOpen = link->opened(currTime, speed, speed, SUMOVTypeParameter::getDefault().length,
95  const bool hasFoe = link->hasApproachingFoe(currTime, currTime, 0, SUMOVTypeParameter::getDefaultDecel());
96  const std::string approachedInternal = link->getViaLane() != nullptr ? link->getViaLane()->getID() : "";
97  const std::string state = SUMOXMLDefinitions::LinkStates.getString(link->getState());
98  const std::string direction = SUMOXMLDefinitions::LinkDirections.getString(link->getDirection());
99  const double length = link->getLength();
100  v.push_back(TraCIConnection(approachedLane, hasPrio, isOpen, hasFoe, approachedInternal, state, direction, length));
101  }
102  return v;
103 }
104 
105 
106 std::vector<std::string>
107 Lane::getAllowed(std::string laneID) {
108  SVCPermissions permissions = getLane(laneID)->getPermissions();
109  if (permissions == SVCAll) { // special case: write nothing
110  permissions = 0;
111  }
112  return getVehicleClassNamesList(permissions);
113 }
114 
115 
116 std::vector<std::string>
117 Lane::getDisallowed(std::string laneID) {
118  return getVehicleClassNamesList(invertPermissions((getLane(laneID)->getPermissions()))); // negation yields disallowed
119 }
120 
121 
123 Lane::getShape(std::string laneID) {
125  const PositionVector& shp = getLane(laneID)->getShape();
126  for (PositionVector::const_iterator pi = shp.begin(); pi != shp.end(); ++pi) {
127  TraCIPosition p;
128  p.x = pi->x();
129  p.y = pi->y();
130  p.z = pi->z();
131  pv.push_back(p);
132  }
133  return pv;
134 }
135 
136 
137 double
138 Lane::getWidth(std::string laneID) {
139  return getLane(laneID)->getWidth();
140 }
141 
142 
143 double
144 Lane::getCO2Emission(std::string laneID) {
145  return getLane(laneID)->getCO2Emissions();
146 }
147 
148 
149 double
150 Lane::getCOEmission(std::string laneID) {
151  return getLane(laneID)->getCOEmissions();
152 }
153 
154 
155 double
156 Lane::getHCEmission(std::string laneID) {
157  return getLane(laneID)->getHCEmissions();
158 }
159 
160 
161 double
162 Lane::getPMxEmission(std::string laneID) {
163  return getLane(laneID)->getPMxEmissions();
164 }
165 
166 
167 double
168 Lane::getNOxEmission(std::string laneID) {
169  return getLane(laneID)->getNOxEmissions();
170 }
171 
172 double
173 Lane::getFuelConsumption(std::string laneID) {
174  return getLane(laneID)->getFuelConsumption();
175 }
176 
177 
178 double
179 Lane::getNoiseEmission(std::string laneID) {
180  return getLane(laneID)->getHarmonoise_NoiseEmissions();
181 }
182 
183 
184 double
185 Lane::getElectricityConsumption(std::string laneID) {
186  return getLane(laneID)->getElectricityConsumption();
187 }
188 
189 
190 double
191 Lane::getLastStepMeanSpeed(std::string laneID) {
192  return getLane(laneID)->getMeanSpeed();
193 }
194 
195 
196 double
197 Lane::getLastStepOccupancy(std::string laneID) {
198  return getLane(laneID)->getNettoOccupancy();
199 }
200 
201 
202 double
203 Lane::getLastStepLength(std::string laneID) {
204  const MSLane* lane = getLane(laneID);
205  double length = 0;
206  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
207  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
208  length += (*j)->getVehicleType().getLength();
209  }
210  if (vehs.size() > 0) {
211  length = length / (double)vehs.size();
212  }
213  lane->releaseVehicles();
214  return length;
215 }
216 
217 
218 double
219 Lane::getWaitingTime(std::string laneID) {
220  return getLane(laneID)->getWaitingSeconds();
221 }
222 
223 
224 double
225 Lane::getTraveltime(std::string laneID) {
226  const MSLane* lane = getLane(laneID);
227  double meanSpeed = lane->getMeanSpeed();
228  if (meanSpeed != 0) {
229  return lane->getLength() / meanSpeed;
230  } else {
231  return 1000000.;
232  }
233 }
234 
235 
236 int
237 Lane::getLastStepVehicleNumber(std::string laneID) {
238  return (int)getLane(laneID)->getVehicleNumber();
239 }
240 
241 int
242 Lane::getLastStepHaltingNumber(std::string laneID) {
243  const MSLane* lane = getLane(laneID);
244  int halting = 0;
245  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
246  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
247  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
248  ++halting;
249  }
250  }
251  lane->releaseVehicles();
252  return halting;
253 }
254 
255 
256 std::vector<std::string>
257 Lane::getLastStepVehicleIDs(std::string laneID) {
258  const MSLane* lane = getLane(laneID);
259  std::vector<std::string> vehIDs;
260  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
261  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
262  vehIDs.push_back((*j)->getID());
263  }
264  lane->releaseVehicles();
265  return vehIDs;
266 }
267 
268 
269 std::vector<std::string>
270 Lane::getFoes(const std::string& laneID, const std::string& toLaneID) {
271  std::vector<std::string> foeIDs;
272  const MSLink* const link = getLane(laneID)->getLinkTo(getLane(toLaneID));
273  if (link == nullptr) {
274  throw TraCIException("No connection from lane '" + laneID + "' to lane '" + toLaneID + "'");
275  }
276  for (const MSLink* foe : link->getFoeLinks()) {
277  foeIDs.push_back(foe->getLaneBefore()->getID());
278  }
279  return foeIDs;
280 }
281 
282 
283 std::vector<std::string>
284 Lane::getInternalFoes(const std::string& laneID) {
285  const MSLane* lane = getLane(laneID);
286  const std::vector<const MSLane*>* foeLanes;
287  std::vector<const MSLane*>::const_iterator it;
288  std::vector<std::string> foeIDs;
289 
290  if (lane->isInternal() && lane->getLinkCont().size() > 0) {
291  MSLink* link = lane->getLinkCont().front();
292  foeLanes = &link->getFoeLanes();
293 
294  for (it = foeLanes->begin(); foeLanes->end() != it; ++it) {
295  foeIDs.push_back((*it)->getID());
296  }
297  }
298  return foeIDs;
299 }
300 
301 
302 void
303 Lane::setAllowed(std::string laneID, std::string allowedClass) {
304  setAllowed(laneID, std::vector<std::string>({allowedClass}));
305 }
306 
307 
308 void
309 Lane::setAllowed(std::string laneID, std::vector<std::string> allowedClasses) {
310  MSLane* l = const_cast<MSLane*>(getLane(laneID));
313  for (MSEdge* const pred : l->getEdge().getPredecessors()) {
314  pred->rebuildAllowedTargets();
315  }
316 }
317 
318 
319 void
320 Lane::setDisallowed(std::string laneID, std::vector<std::string> disallowedClasses) {
321  MSLane* l = const_cast<MSLane*>(getLane(laneID));
322  l->setPermissions(invertPermissions(parseVehicleClasses(disallowedClasses)), MSLane::CHANGE_PERMISSIONS_PERMANENT); // negation yields allowed
324  for (MSEdge* const pred : l->getEdge().getPredecessors()) {
325  pred->rebuildAllowedTargets();
326  }
327 }
328 
329 
330 void
331 Lane::setMaxSpeed(std::string laneID, double speed) {
332  MSLane* l = const_cast<MSLane*>(getLane(laneID));
333  l->setMaxSpeed(speed);
334 }
335 
336 
337 void
338 Lane::setLength(std::string laneID, double length) {
339  MSLane* l = const_cast<MSLane*>(getLane(laneID));
340  l->setLength(length);
341 }
342 
343 
344 std::string
345 Lane::getParameter(const std::string& laneID, const std::string& param) {
346  return getLane(laneID)->getParameter(param, "");
347 }
348 
349 
351 
352 
353 void
354 Lane::setParameter(const std::string& laneID, const std::string& key, const std::string& value) {
355  MSLane* l = const_cast<MSLane*>(getLane(laneID));
356  l->setParameter(key, value);
357 }
358 
359 
361 
362 
363 const MSLane*
364 Lane::getLane(const std::string& id) {
365  const MSLane* r = MSLane::dictionary(id);
366  if (r == nullptr) {
367  throw TraCIException("Lane '" + id + "' is not known");
368  }
369  return r;
370 }
371 
372 
373 void
374 Lane::storeShape(const std::string& id, PositionVector& shape) {
375  shape = getLane(id)->getShape();
376 }
377 
378 
379 std::shared_ptr<VariableWrapper>
380 Lane::makeWrapper() {
381  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
382 }
383 
384 
385 bool
386 Lane::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
387  switch (variable) {
388  case TRACI_ID_LIST:
389  return wrapper->wrapStringList(objID, variable, getIDList());
390  case ID_COUNT:
391  return wrapper->wrapInt(objID, variable, getIDCount());
392  case LANE_LINK_NUMBER:
393  return wrapper->wrapInt(objID, variable, getLinkNumber(objID));
394  case LANE_EDGE_ID:
395  return wrapper->wrapString(objID, variable, getEdgeID(objID));
396  case VAR_LENGTH:
397  return wrapper->wrapDouble(objID, variable, getLength(objID));
398  case VAR_MAXSPEED:
399  return wrapper->wrapDouble(objID, variable, getMaxSpeed(objID));
400  case LANE_ALLOWED:
401  return wrapper->wrapStringList(objID, variable, getAllowed(objID));
402  case LANE_DISALLOWED:
403  return wrapper->wrapStringList(objID, variable, getDisallowed(objID));
404  case VAR_CO2EMISSION:
405  return wrapper->wrapDouble(objID, variable, getCO2Emission(objID));
406  case VAR_COEMISSION:
407  return wrapper->wrapDouble(objID, variable, getCOEmission(objID));
408  case VAR_HCEMISSION:
409  return wrapper->wrapDouble(objID, variable, getHCEmission(objID));
410  case VAR_PMXEMISSION:
411  return wrapper->wrapDouble(objID, variable, getPMxEmission(objID));
412  case VAR_NOXEMISSION:
413  return wrapper->wrapDouble(objID, variable, getNOxEmission(objID));
414  case VAR_FUELCONSUMPTION:
415  return wrapper->wrapDouble(objID, variable, getFuelConsumption(objID));
416  case VAR_NOISEEMISSION:
417  return wrapper->wrapDouble(objID, variable, getNoiseEmission(objID));
419  return wrapper->wrapDouble(objID, variable, getElectricityConsumption(objID));
421  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
423  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
425  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
426  case LAST_STEP_OCCUPANCY:
427  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
429  return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
430  case LAST_STEP_LENGTH:
431  return wrapper->wrapDouble(objID, variable, getLastStepLength(objID));
432  case VAR_WAITING_TIME:
433  return wrapper->wrapDouble(objID, variable, getWaitingTime(objID));
435  return wrapper->wrapDouble(objID, variable, getTraveltime(objID));
436  case VAR_WIDTH:
437  return wrapper->wrapDouble(objID, variable, getWidth(objID));
438  default:
439  return false;
440  }
441 }
442 }
443 
444 
445 /****************************************************************************/
long long int SUMOTime
Definition: SUMOTime.h:31
const SVCPermissions SVCAll
all VClasses are allowed
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
const std::vector< std::string > & getVehicleClassNamesList(SVCPermissions permissions)
Returns the ids of the given classes, divided using a ' '.
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
T MIN2(T a, T b)
Definition: StdDefs.h:73
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:60
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:57
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:104
C++ TraCI client API implementation.
A road/street connecting two junctions.
Definition: MSEdge.h:77
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:383
void rebuildAllowedLanes()
Definition: MSEdge.cpp:250
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.h:640
static void insertIDs(std::vector< std::string > &into)
Adds the ids of all stored lanes into the given vector.
Definition: MSLane.cpp:1940
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:426
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:531
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:92
static const long CHANGE_PERMISSIONS_PERMANENT
Definition: MSLane.h:1239
double getLength() const
Returns the lane's length.
Definition: MSLane.h:539
void setPermissions(SVCPermissions permissions, long long transientID)
Sets the permissions to the given value. If a transientID is given, the permissions are recored as te...
Definition: MSLane.cpp:3728
void setMaxSpeed(double val)
Sets a new maximum speed for the lane (used by TraCI and MSCalibrator)
Definition: MSLane.cpp:2168
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1908
bool isInternal() const
Definition: MSLane.cpp:2036
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:673
void setLength(double val)
Sets a new length for the lane (used by TraCI only)
Definition: MSLane.cpp:2175
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:456
double getMeanSpeed() const
Returns the mean speed on this lane.
Definition: MSLane.cpp:2736
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
A list of positions.
static double getDefaultDecel(const SUMOVehicleClass vc=SVC_IGNORING)
Returns the default deceleration for the given vehicle class This needs to be a function because the ...
static const SUMOVTypeParameter & getDefault()
return the default parameters, this is a function due to the http://www.parashift....
static StringBijection< LinkState > LinkStates
link states
static StringBijection< LinkDirection > LinkDirections
link directions
const std::string & getString(const T key) const
TRACI_CONST int LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int VAR_NOXEMISSION
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_WAITING_TIME
TRACI_CONST int LANE_LINK_NUMBER
TRACI_CONST int LAST_STEP_LENGTH
TRACI_CONST int LANE_EDGE_ID
TRACI_CONST int VAR_PMXEMISSION
TRACI_CONST int VAR_COEMISSION
TRACI_CONST int VAR_WIDTH
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int LAST_STEP_MEAN_SPEED
TRACI_CONST int VAR_CO2EMISSION
TRACI_CONST int VAR_FUELCONSUMPTION
TRACI_CONST int LAST_STEP_VEHICLE_HALTING_NUMBER
TRACI_CONST int VAR_LENGTH
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:250
TRACI_CONST int VAR_HCEMISSION
TRACI_CONST int ID_COUNT
TRACI_CONST int LAST_STEP_OCCUPANCY
TRACI_CONST int VAR_NOISEEMISSION
TRACI_CONST int LANE_DISALLOWED
TRACI_CONST int VAR_CURRENT_TRAVELTIME
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251
TRACI_CONST int LANE_ALLOWED
TRACI_CONST int VAR_ELECTRICITYCONSUMPTION
A list of positions.