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-2022 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 <microsim/MSLink.h>
33 #include <libsumo/TraCIConstants.h>
34 #include "Lane.h"
35 
36 
37 namespace libsumo {
38 // ===========================================================================
39 // static member initializations
40 // ===========================================================================
41 SubscriptionResults Lane::mySubscriptionResults;
42 ContextSubscriptionResults Lane::myContextSubscriptionResults;
43 
44 
45 // ===========================================================================
46 // static member definitions
47 // ===========================================================================
48 std::vector<std::string>
49 Lane::getIDList() {
50  std::vector<std::string> ids;
51  MSLane::insertIDs(ids);
52  return ids;
53 }
54 
55 
56 int
57 Lane::getIDCount() {
58  return (int)getIDList().size();
59 }
60 
61 
62 std::string
63 Lane::getEdgeID(std::string laneID) {
64  return getLane(laneID)->getEdge().getID();
65 }
66 
67 
68 double
69 Lane::getLength(std::string laneID) {
70  return getLane(laneID)->getLength();
71 }
72 
73 
74 double
75 Lane::getMaxSpeed(std::string laneID) {
76  return getLane(laneID)->getSpeedLimit();
77 }
78 
79 
80 int
81 Lane::getLinkNumber(std::string laneID) {
82  return (int)getLane(laneID)->getLinkCont().size();
83 }
84 
85 
86 std::vector<TraCIConnection>
87 Lane::getLinks(std::string laneID) {
88  std::vector<TraCIConnection> v;
89  const MSLane* const lane = getLane(laneID);
90  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
91  for (const MSLink* const link : lane->getLinkCont()) {
92  const std::string approachedLane = link->getLane() != nullptr ? link->getLane()->getID() : "";
93  const bool hasPrio = link->havePriority();
94  const double speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
95  const bool isOpen = link->opened(currTime, speed, speed, SUMOVTypeParameter::getDefault().length,
97  const bool hasFoe = link->hasApproachingFoe(currTime, currTime, 0, SUMOVTypeParameter::getDefaultDecel());
98  const std::string approachedInternal = link->getViaLane() != nullptr ? link->getViaLane()->getID() : "";
99  const std::string state = SUMOXMLDefinitions::LinkStates.getString(link->getState());
100  const std::string direction = SUMOXMLDefinitions::LinkDirections.getString(link->getDirection());
101  const double length = link->getLength();
102  v.push_back(TraCIConnection(approachedLane, hasPrio, isOpen, hasFoe, approachedInternal, state, direction, length));
103  }
104  return v;
105 }
106 
107 
108 std::vector<std::string>
109 Lane::getAllowed(std::string laneID) {
110  SVCPermissions permissions = getLane(laneID)->getPermissions();
111  if (permissions == SVCAll) { // special case: write nothing
112  permissions = 0;
113  }
114  return getVehicleClassNamesList(permissions);
115 }
116 
117 
118 std::vector<std::string>
119 Lane::getDisallowed(std::string laneID) {
120  return getVehicleClassNamesList(invertPermissions((getLane(laneID)->getPermissions()))); // negation yields disallowed
121 }
122 
123 
124 TraCIPositionVector
125 Lane::getShape(std::string laneID) {
126  TraCIPositionVector pv;
127  const PositionVector& shp = getLane(laneID)->getShape();
128  for (PositionVector::const_iterator pi = shp.begin(); pi != shp.end(); ++pi) {
129  TraCIPosition p;
130  p.x = pi->x();
131  p.y = pi->y();
132  p.z = pi->z();
133  pv.value.push_back(p);
134  }
135  return pv;
136 }
137 
138 
139 double
140 Lane::getWidth(std::string laneID) {
141  return getLane(laneID)->getWidth();
142 }
143 
144 
145 double
146 Lane::getCO2Emission(std::string laneID) {
147  return getLane(laneID)->getCO2Emissions();
148 }
149 
150 
151 double
152 Lane::getCOEmission(std::string laneID) {
153  return getLane(laneID)->getCOEmissions();
154 }
155 
156 
157 double
158 Lane::getHCEmission(std::string laneID) {
159  return getLane(laneID)->getHCEmissions();
160 }
161 
162 
163 double
164 Lane::getPMxEmission(std::string laneID) {
165  return getLane(laneID)->getPMxEmissions();
166 }
167 
168 
169 double
170 Lane::getNOxEmission(std::string laneID) {
171  return getLane(laneID)->getNOxEmissions();
172 }
173 
174 double
175 Lane::getFuelConsumption(std::string laneID) {
176  return getLane(laneID)->getFuelConsumption();
177 }
178 
179 
180 double
181 Lane::getNoiseEmission(std::string laneID) {
182  return getLane(laneID)->getHarmonoise_NoiseEmissions();
183 }
184 
185 
186 double
187 Lane::getElectricityConsumption(std::string laneID) {
188  return getLane(laneID)->getElectricityConsumption();
189 }
190 
191 
192 double
193 Lane::getLastStepMeanSpeed(std::string laneID) {
194  return getLane(laneID)->getMeanSpeed();
195 }
196 
197 
198 double
199 Lane::getLastStepOccupancy(std::string laneID) {
200  return getLane(laneID)->getNettoOccupancy();
201 }
202 
203 
204 double
205 Lane::getLastStepLength(std::string laneID) {
206  const MSLane* lane = getLane(laneID);
207  double length = 0;
208  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
209  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
210  length += (*j)->getVehicleType().getLength();
211  }
212  if (vehs.size() > 0) {
213  length = length / (double)vehs.size();
214  }
215  lane->releaseVehicles();
216  return length;
217 }
218 
219 
220 double
221 Lane::getWaitingTime(std::string laneID) {
222  return getLane(laneID)->getWaitingSeconds();
223 }
224 
225 
226 double
227 Lane::getTraveltime(std::string laneID) {
228  const MSLane* lane = getLane(laneID);
229  double meanSpeed = lane->getMeanSpeed();
230  if (meanSpeed != 0) {
231  return lane->getLength() / meanSpeed;
232  } else {
233  return 1000000.;
234  }
235 }
236 
237 
238 int
239 Lane::getLastStepVehicleNumber(std::string laneID) {
240  return (int)getLane(laneID)->getVehicleNumber();
241 }
242 
243 int
244 Lane::getLastStepHaltingNumber(std::string laneID) {
245  const MSLane* lane = getLane(laneID);
246  int halting = 0;
247  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
248  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
249  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
250  ++halting;
251  }
252  }
253  lane->releaseVehicles();
254  return halting;
255 }
256 
257 
258 std::vector<std::string>
259 Lane::getLastStepVehicleIDs(std::string laneID) {
260  const MSLane* lane = getLane(laneID);
261  std::vector<std::string> vehIDs;
262  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
263  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
264  vehIDs.push_back((*j)->getID());
265  }
266  lane->releaseVehicles();
267  return vehIDs;
268 }
269 
270 
271 std::vector<std::string>
272 Lane::getFoes(const std::string& laneID, const std::string& toLaneID) {
273  std::vector<std::string> foeIDs;
274  const MSLink* const link = getLane(laneID)->getLinkTo(getLane(toLaneID));
275  if (link == nullptr) {
276  throw TraCIException("No connection from lane '" + laneID + "' to lane '" + toLaneID + "'");
277  }
278  for (const MSLink* foe : link->getFoeLinks()) {
279  foeIDs.push_back(foe->getLaneBefore()->getID());
280  }
281  return foeIDs;
282 }
283 
284 
285 std::vector<std::string>
286 Lane::getInternalFoes(const std::string& laneID) {
287  const MSLane* lane = getLane(laneID);
288  const std::vector<const MSLane*>* foeLanes;
289  std::vector<const MSLane*>::const_iterator it;
290  std::vector<std::string> foeIDs;
291 
292  if (lane->isInternal() && lane->getLinkCont().size() > 0) {
293  MSLink* link = lane->getLinkCont().front();
294  foeLanes = &link->getFoeLanes();
295 
296  for (it = foeLanes->begin(); foeLanes->end() != it; ++it) {
297  foeIDs.push_back((*it)->getID());
298  }
299  }
300  return foeIDs;
301 }
302 
303 
304 const std::vector<std::string>
305 Lane::getPendingVehicles(const std::string& laneID) {
306  getLane(laneID); // validate laneID
307  std::vector<std::string> vehIDs;
308  for (const SUMOVehicle* veh : MSNet::getInstance()->getInsertionControl().getPendingVehicles()) {
309  if (veh->getLane() != nullptr && veh->getLane()->getID() == laneID) {
310  vehIDs.push_back(veh->getID());
311  }
312  }
313  return vehIDs;
314 }
315 
316 void
317 Lane::setAllowed(std::string laneID, std::string allowedClass) {
318  setAllowed(laneID, std::vector<std::string>({allowedClass}));
319 }
320 
321 
322 void
323 Lane::setAllowed(std::string laneID, std::vector<std::string> allowedClasses) {
324  MSLane* l = const_cast<MSLane*>(getLane(laneID));
327 }
328 
329 
330 void
331 Lane::setDisallowed(std::string laneID, std::vector<std::string> disallowedClasses) {
332  MSLane* l = const_cast<MSLane*>(getLane(laneID));
333  l->setPermissions(invertPermissions(parseVehicleClasses(disallowedClasses)), MSLane::CHANGE_PERMISSIONS_PERMANENT); // negation yields allowed
335 }
336 
337 
338 void
339 Lane::setMaxSpeed(std::string laneID, double speed) {
340  MSLane* l = const_cast<MSLane*>(getLane(laneID));
341  l->setMaxSpeed(speed);
342 }
343 
344 
345 void
346 Lane::setLength(std::string laneID, double length) {
347  MSLane* l = const_cast<MSLane*>(getLane(laneID));
348  l->setLength(length);
349 }
350 
351 
352 std::string
353 Lane::getParameter(const std::string& laneID, const std::string& param) {
354  return getLane(laneID)->getParameter(param, "");
355 }
356 
357 
359 
360 
361 void
362 Lane::setParameter(const std::string& laneID, const std::string& key, const std::string& value) {
363  MSLane* l = const_cast<MSLane*>(getLane(laneID));
364  l->setParameter(key, value);
365 }
366 
367 
369 
370 
371 const MSLane*
372 Lane::getLane(const std::string& id) {
373  const MSLane* r = MSLane::dictionary(id);
374  if (r == nullptr) {
375  throw TraCIException("Lane '" + id + "' is not known");
376  }
377  return r;
378 }
379 
380 
381 void
382 Lane::storeShape(const std::string& id, PositionVector& shape) {
383  shape = getLane(id)->getShape();
384 }
385 
386 
387 std::shared_ptr<VariableWrapper>
388 Lane::makeWrapper() {
389  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
390 }
391 
392 
393 bool
394 Lane::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
395  switch (variable) {
396  case TRACI_ID_LIST:
397  return wrapper->wrapStringList(objID, variable, getIDList());
398  case ID_COUNT:
399  return wrapper->wrapInt(objID, variable, getIDCount());
400  case LANE_LINK_NUMBER:
401  return wrapper->wrapInt(objID, variable, getLinkNumber(objID));
402  case LANE_EDGE_ID:
403  return wrapper->wrapString(objID, variable, getEdgeID(objID));
404  case VAR_LENGTH:
405  return wrapper->wrapDouble(objID, variable, getLength(objID));
406  case VAR_MAXSPEED:
407  return wrapper->wrapDouble(objID, variable, getMaxSpeed(objID));
408  case LANE_ALLOWED:
409  return wrapper->wrapStringList(objID, variable, getAllowed(objID));
410  case LANE_DISALLOWED:
411  return wrapper->wrapStringList(objID, variable, getDisallowed(objID));
412  case VAR_CO2EMISSION:
413  return wrapper->wrapDouble(objID, variable, getCO2Emission(objID));
414  case VAR_COEMISSION:
415  return wrapper->wrapDouble(objID, variable, getCOEmission(objID));
416  case VAR_HCEMISSION:
417  return wrapper->wrapDouble(objID, variable, getHCEmission(objID));
418  case VAR_PMXEMISSION:
419  return wrapper->wrapDouble(objID, variable, getPMxEmission(objID));
420  case VAR_NOXEMISSION:
421  return wrapper->wrapDouble(objID, variable, getNOxEmission(objID));
422  case VAR_FUELCONSUMPTION:
423  return wrapper->wrapDouble(objID, variable, getFuelConsumption(objID));
424  case VAR_NOISEEMISSION:
425  return wrapper->wrapDouble(objID, variable, getNoiseEmission(objID));
427  return wrapper->wrapDouble(objID, variable, getElectricityConsumption(objID));
429  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
431  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
433  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
434  case LAST_STEP_OCCUPANCY:
435  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
437  return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
438  case LAST_STEP_LENGTH:
439  return wrapper->wrapDouble(objID, variable, getLastStepLength(objID));
440  case VAR_WAITING_TIME:
441  return wrapper->wrapDouble(objID, variable, getWaitingTime(objID));
443  return wrapper->wrapDouble(objID, variable, getTraveltime(objID));
444  case VAR_WIDTH:
445  return wrapper->wrapDouble(objID, variable, getWidth(objID));
446  case VAR_SHAPE:
447  return wrapper->wrapPositionVector(objID, variable, getShape(objID));
449  return wrapper->wrapStringList(objID, variable, getPendingVehicles(objID));
451  paramData->readUnsignedByte();
452  return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
454  paramData->readUnsignedByte();
455  return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
456  default:
457  return false;
458  }
459 }
460 }
461 
462 
463 /****************************************************************************/
long long int SUMOTime
Definition: SUMOTime.h:32
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:74
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:61
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:69
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:115
C++ TraCI client API implementation.
Definition: Lane.h:33
void rebuildAllowedLanes()
Definition: MSEdge.cpp:271
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:641
static void insertIDs(std::vector< std::string > &into)
Adds the ids of all stored lanes into the given vector.
Definition: MSLane.cpp:2023
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:428
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:533
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:92
static const long CHANGE_PERMISSIONS_PERMANENT
Definition: MSLane.h:1256
double getLength() const
Returns the lane's length.
Definition: MSLane.h:541
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:3938
void setMaxSpeed(double val)
Sets a new maximum speed for the lane (used by TraCI and MSCalibrator)
Definition: MSLane.cpp:2254
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1991
bool isInternal() const
Definition: MSLane.cpp:2122
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:674
void setLength(double val)
Sets a new length for the lane (used by TraCI only)
Definition: MSLane.cpp:2261
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:458
double getMeanSpeed() const
Returns the mean speed on this lane.
Definition: MSLane.cpp:2831
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:174
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:318
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....
Representation of a vehicle.
Definition: SUMOVehicle.h:60
static StringBijection< LinkState > LinkStates
link states
static StringBijection< LinkDirection > LinkDirections
link directions
const std::string & getString(const T key) const
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
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
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:279
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_PENDING_VEHICLES
TRACI_CONST int VAR_FUELCONSUMPTION
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:278
TRACI_CONST int VAR_SHAPE
TRACI_CONST int LAST_STEP_VEHICLE_HALTING_NUMBER
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_HCEMISSION
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int LAST_STEP_OCCUPANCY
TRACI_CONST int VAR_NOISEEMISSION
TRACI_CONST int LANE_DISALLOWED
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_CURRENT_TRAVELTIME
TRACI_CONST int LANE_ALLOWED
TRACI_CONST int VAR_ELECTRICITYCONSUMPTION