SUMO - Simulation of Urban MObility
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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
22 // C++ TraCI client API implementation
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <microsim/MSNet.h>
36 #include <microsim/MSLane.h>
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSVehicle.h>
39 #include "Lane.h"
40 
41 
42 // ===========================================================================
43 // member definitions
44 // ===========================================================================
45 namespace libsumo {
46 std::vector<std::string>
48  std::vector<std::string> ids;
49  MSLane::insertIDs(ids);
50  return ids;
51 }
52 
53 
54 int
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* lane = getLane(laneID);
88  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
89  const MSLinkCont& links = lane->getLinkCont();
90  for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
91  MSLink* link = (*i);
92  const std::string approachedLane = link->getLane() != 0 ? link->getLane()->getID() : "";
93  const bool hasPrio = link->havePriority() ? 1 : 0;
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()) ? 1 : 0;
98  const std::string approachedInternal = link->getViaLane() != 0 ? 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 
125 Lane::getShape(std::string laneID) {
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.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 std::vector<std::string>
271 Lane::getFoes(const std::string& laneID, const std::string& toLaneID) {
272  std::vector<std::string> foeIDs;
273  const MSLane* from = getLane(laneID);
274  const MSLane* to = getLane(toLaneID);
275  const MSLink* link = MSLinkContHelper::getConnectingLink(*from, *to);
276  if (link == 0) {
277  throw TraCIException("No connection from lane '" + laneID + "' to lane '" + toLaneID + "'");
278  }
279  for (MSLink* foe : link->getFoeLinks()) {
280  foeIDs.push_back(foe->getLaneBefore()->getID());
281  }
282  return foeIDs;
283 }
284 
285 
286 std::vector<std::string>
287 Lane::getInternalFoes(const std::string& laneID) {
288  const MSLane* lane = getLane(laneID);
289  const std::vector<const MSLane*>* foeLanes;
290  std::vector<const MSLane*>::const_iterator it;
291  std::vector<std::string> foeIDs;
292 
293  if (lane->isInternal()) {
294  MSLink* link = lane->getLinkCont().front();
295  foeLanes = &link->getFoeLanes();
296 
297  for (it = foeLanes->begin(); foeLanes->end() != it; ++it) {
298  foeIDs.push_back((*it)->getID());
299  }
300  }
301  return foeIDs;
302 }
303 
304 
305 void
306 Lane::setAllowed(std::string laneID, std::vector<std::string> allowedClasses) {
307  MSLane* l = const_cast<MSLane*>(getLane(laneID));
310 }
311 
312 
313 void
314 Lane::setDisallowed(std::string laneID, std::vector<std::string> disallowedClasses) {
315  MSLane* l = const_cast<MSLane*>(getLane(laneID));
316  l->setPermissions(invertPermissions(parseVehicleClasses(disallowedClasses)), MSLane::CHANGE_PERMISSIONS_PERMANENT); // negation yields allowed
318 }
319 
320 
321 void
322 Lane::setMaxSpeed(std::string laneID, double speed) {
323  MSLane* l = const_cast<MSLane*>(getLane(laneID));
324  l->setMaxSpeed(speed);
325 }
326 
327 
328 void
329 Lane::setLength(std::string laneID, double length) {
330  MSLane* l = const_cast<MSLane*>(getLane(laneID));
331  l->setLength(length);
332 }
333 
334 
335 std::string
336 Lane::getParameter(const std::string& laneID, const std::string& param) {
337  return getLane(laneID)->getParameter(param, "");
338 }
339 
340 
341 void
342 Lane::setParameter(const std::string& laneID, const std::string& key, const std::string& value) {
343  MSLane* l = const_cast<MSLane*>(getLane(laneID));
344  l->setParameter(key, value);
345 }
346 
347 
348 const MSLane*
349 Lane::getLane(const std::string& id) {
350  const MSLane* r = MSLane::dictionary(id);
351  if (r == 0) {
352  throw TraCIException("Lane '" + id + "' is not known");
353  }
354  return r;
355 }
356 }
357 
358 
359 /****************************************************************************/
static double getCO2Emission(std::string laneID)
Definition: Lane.cpp:146
static double getLength(std::string laneID)
Definition: Lane.cpp:67
static double getNOxEmission(std::string laneID)
Definition: Lane.cpp:170
static void setAllowed(std::string laneID, std::vector< std::string > allowedClasses)
Definition: Lane.cpp:306
static std::vector< std::string > getFoes(const std::string &laneID, const std::string &toLaneID)
Definition: Lane.cpp:271
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: Lane.cpp:85
double getNOxEmissions() const
Returns the sum of last step NOx emissions.
Definition: MSLane.cpp:2478
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:607
static std::string getEdgeID(std::string laneID)
Definition: Lane.cpp:61
static void insertIDs(std::vector< std::string > &into)
Adds the ids of all stored lanes into the given vector.
Definition: MSLane.cpp:1671
double getFuelConsumption() const
Returns the sum of last step fuel consumption.
Definition: MSLane.cpp:2502
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Lane.cpp:342
void setMaxSpeed(double val)
Sets a new maximum speed for the lane (used by TraCI and MSCalibrator)
Definition: MSLane.cpp:1919
const std::string & getString(const T key) const
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
std::vector< std::string > getVehicleClassNamesList(SVCPermissions permissions)
Returns the ids of the given classes, divided using a &#39; &#39;.
static std::vector< std::string > getAllowed(std::string laneID)
Definition: Lane.cpp:109
static std::vector< std::string > getInternalFoes(const std::string &laneID)
Definition: Lane.cpp:287
static double getElectricityConsumption(std::string laneID)
Definition: Lane.cpp:187
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
static double getFuelConsumption(std::string laneID)
Definition: Lane.cpp:175
double getPMxEmissions() const
Returns the sum of last step PMx emissions.
Definition: MSLane.cpp:2466
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:497
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:392
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:439
static int getIDCount()
Definition: Lane.cpp:55
static int getLastStepHaltingNumber(std::string laneID)
Definition: Lane.cpp:244
static double getWidth(std::string laneID)
Definition: Lane.cpp:140
static int getLastStepVehicleNumber(std::string laneID)
Definition: Lane.cpp:239
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static StringBijection< LinkState > LinkStates
link states
const SVCPermissions SVCAll
all VClasses are allowed
static double getMaxSpeed(std::string laneID)
Definition: Lane.cpp:73
double getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:513
static double getPMxEmission(std::string laneID)
Definition: Lane.cpp:164
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
static StringBijection< LinkDirection > LinkDirections
link directions
bool isInternal() const
Definition: MSLane.cpp:1774
static double getLastStepLength(std::string laneID)
Definition: Lane.cpp:205
void rebuildAllowedLanes()
Definition: MSEdge.cpp:246
static double getHCEmission(std::string laneID)
Definition: Lane.cpp:158
double getCO2Emissions() const
Returns the sum of last step CO2 emissions.
Definition: MSLane.cpp:2442
void setLength(double val)
Sets a new length for the lane (used by TraCI only)
Definition: MSLane.cpp:1926
A list of positions.
static double getLastStepMeanSpeed(std::string laneID)
Definition: Lane.cpp:193
int getVehicleNumber() const
Returns the number of vehicles on this lane (for which this lane is responsible)
Definition: MSLane.h:365
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:505
static TraCIPositionVector getShape(std::string laneID)
Definition: Lane.cpp:125
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:489
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
static void setMaxSpeed(std::string laneID, double speed)
Definition: Lane.cpp:322
T MIN2(T a, T b)
Definition: StdDefs.h:67
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: Lane.cpp:314
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 ...
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
double getHCEmissions() const
Returns the sum of last step HC emissions.
Definition: MSLane.cpp:2490
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:89
static const MSLane * getLane(const std::string &id)
Definition: Lane.cpp:349
Definition: Edge.cpp:31
static std::string getParameter(const std::string &laneID, const std::string &param)
Definition: Lane.cpp:336
void setPermissions(SVCPermissions permissions, long transientID)
Sets the permissions to the given value. If a transientID is given, the permissions are recored as te...
Definition: MSLane.cpp:3119
double getElectricityConsumption() const
Returns the sum of last step electricity consumption.
Definition: MSLane.cpp:2514
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1639
double getCOEmissions() const
Returns the sum of last step CO emissions.
Definition: MSLane.cpp:2454
static std::vector< std::string > getLastStepVehicleIDs(std::string laneID)
Definition: Lane.cpp:259
static double getWaitingTime(std::string laneID)
Definition: Lane.cpp:221
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
double getHarmonoise_NoiseEmissions() const
Returns the sum of last step noise emissions.
Definition: MSLane.cpp:2526
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:57
static double getTraveltime(std::string laneID)
Definition: Lane.cpp:227
static void setLength(std::string laneID, double length)
Definition: Lane.cpp:329
long long int SUMOTime
Definition: TraCIDefs.h:51
double getMeanSpeed() const
Returns the mean speed on this lane.
Definition: MSLane.cpp:2426
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1874
static const long CHANGE_PERMISSIONS_PERMANENT
Definition: MSLane.h:1073
static const SUMOVTypeParameter & getDefault()
return the default parameters, this is a function due to the http://www.parashift.com/c++-faq/static-init-order.html
A 3D-position.
Definition: TraCIDefs.h:71
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:419
double getNettoOccupancy() const
Returns the netto (excluding minGaps) occupancy of this lane during the last step (including minGaps)...
Definition: MSLane.cpp:2398
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
A list of positions.
double getWaitingSeconds() const
Returns the overall waiting time on this lane.
Definition: MSLane.cpp:2413
static double getNoiseEmission(std::string laneID)
Definition: Lane.cpp:181
static double getLastStepOccupancy(std::string laneID)
Definition: Lane.cpp:199
static std::vector< std::string > getDisallowed(std::string laneID)
Definition: Lane.cpp:119
static int getLinkNumber(std::string laneID)
Definition: Lane.cpp:79
static double getCOEmission(std::string laneID)
Definition: Lane.cpp:152
static std::vector< std::string > getIDList()
Definition: Lane.cpp:47