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