Line data Source code
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 : /****************************************************************************/
12 : /// @file TraCI_Lane.cpp
13 : /// @author Daniel Krajzewicz
14 : /// @author Mario Krumnow
15 : /// @author Jakob Erdmann
16 : /// @author Michael Behrisch
17 : /// @author Robert Hilbrich
18 : /// @author Leonhard Luecken
19 : /// @date 30.05.2012
20 : /// @version $Id$
21 : ///
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 "TraCI_Lane.h"
40 :
41 :
42 : // ===========================================================================
43 : // member definitions
44 : // ===========================================================================
45 : std::vector<std::string>
46 12 : TraCI_Lane::getIDList() {
47 12 : std::vector<std::string> ids;
48 12 : MSLane::insertIDs(ids);
49 12 : return ids;
50 : }
51 :
52 :
53 : int
54 6 : TraCI_Lane::getIDCount() {
55 6 : return (int)getIDList().size();
56 : }
57 :
58 :
59 : std::string
60 5 : TraCI_Lane::getEdgeID(std::string laneID) {
61 5 : return getLane(laneID)->getEdge().getID();
62 : }
63 :
64 :
65 : double
66 327 : TraCI_Lane::getLength(std::string laneID) {
67 327 : return getLane(laneID)->getLength();
68 : }
69 :
70 :
71 : double
72 329 : TraCI_Lane::getMaxSpeed(std::string laneID) {
73 329 : return getLane(laneID)->getSpeedLimit();
74 : }
75 :
76 :
77 : int
78 6 : TraCI_Lane::getLinkNumber(std::string laneID) {
79 6 : return (int) getLane(laneID)->getLinkCont().size();
80 : }
81 :
82 :
83 : std::vector<TraCIConnection>
84 9 : TraCI_Lane::getLinks(std::string laneID) {
85 9 : std::vector<TraCIConnection> v;
86 9 : const MSLane* lane = getLane(laneID);
87 9 : const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
88 9 : const MSLinkCont& links = lane->getLinkCont();
89 37 : for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
90 28 : MSLink* link = (*i);
91 28 : const std::string approachedLane = link->getLane() != 0 ? link->getLane()->getID() : "";
92 28 : const bool hasPrio = link->havePriority() ? 1 : 0;
93 28 : const double speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
94 28 : const bool isOpen = link->opened(currTime, speed, speed, SUMOVTypeParameter::getDefault().length,
95 56 : SUMOVTypeParameter::getDefault().impatience, SUMOVTypeParameter::getDefaultDecel(), 0) ? 1 : 0;
96 28 : const bool hasFoe = link->hasApproachingFoe(currTime, currTime, 0, SUMOVTypeParameter::getDefaultDecel()) ? 1 : 0;
97 56 : const std::string approachedInternal = link->getViaLane() != 0 ? link->getViaLane()->getID() : "";
98 56 : const std::string state = SUMOXMLDefinitions::LinkStates.getString(link->getState());
99 56 : const std::string direction = SUMOXMLDefinitions::LinkDirections.getString(link->getDirection());
100 28 : const double length = link->getLength();
101 28 : v.push_back(TraCIConnection(approachedLane, hasPrio, isOpen, hasFoe, approachedInternal, state, direction, length));
102 28 : }
103 9 : return v;
104 : }
105 :
106 :
107 : std::vector<std::string>
108 17 : TraCI_Lane::getAllowed(std::string laneID) {
109 17 : SVCPermissions permissions = getLane(laneID)->getPermissions();
110 17 : if (permissions == SVCAll) { // special case: write nothing
111 10 : permissions = 0;
112 : }
113 17 : return getVehicleClassNamesList(permissions);
114 : }
115 :
116 :
117 : std::vector<std::string>
118 17 : TraCI_Lane::getDisallowed(std::string laneID) {
119 17 : return getVehicleClassNamesList(invertPermissions((getLane(laneID)->getPermissions()))); // negation yields disallowed
120 : }
121 :
122 :
123 : TraCIPositionVector
124 12017 : TraCI_Lane::getShape(std::string laneID) {
125 12017 : TraCIPositionVector pv;
126 12017 : const PositionVector& shp = getLane(laneID)->getShape();
127 36051 : for (PositionVector::const_iterator pi = shp.begin(); pi != shp.end(); ++pi) {
128 : TraCIPosition p;
129 24034 : p.x = pi->x();
130 24034 : p.y = pi->y();
131 24034 : p.z = pi->z();
132 24034 : pv.push_back(p);
133 : }
134 12017 : return pv;
135 : }
136 :
137 :
138 : double
139 5 : TraCI_Lane::getWidth(std::string laneID) {
140 5 : return getLane(laneID)->getWidth();
141 : }
142 :
143 :
144 : double
145 7 : TraCI_Lane::getCO2Emission(std::string laneID) {
146 7 : return getLane(laneID)->getCO2Emissions();
147 : }
148 :
149 :
150 : double
151 7 : TraCI_Lane::getCOEmission(std::string laneID) {
152 7 : return getLane(laneID)->getCOEmissions();
153 : }
154 :
155 :
156 : double
157 7 : TraCI_Lane::getHCEmission(std::string laneID) {
158 7 : return getLane(laneID)->getHCEmissions();
159 : }
160 :
161 :
162 : double
163 7 : TraCI_Lane::getPMxEmission(std::string laneID) {
164 7 : return getLane(laneID)->getPMxEmissions();
165 : }
166 :
167 :
168 : double
169 7 : TraCI_Lane::getNOxEmission(std::string laneID) {
170 7 : return getLane(laneID)->getNOxEmissions();
171 : }
172 :
173 : double
174 7 : TraCI_Lane::getFuelConsumption(std::string laneID) {
175 7 : return getLane(laneID)->getFuelConsumption();
176 : }
177 :
178 :
179 : double
180 7 : TraCI_Lane::getNoiseEmission(std::string laneID) {
181 7 : return getLane(laneID)->getHarmonoise_NoiseEmissions();
182 : }
183 :
184 :
185 : double
186 3 : TraCI_Lane::getElectricityConsumption(std::string laneID) {
187 3 : return getLane(laneID)->getElectricityConsumption();
188 : }
189 :
190 :
191 : double
192 7 : TraCI_Lane::getLastStepMeanSpeed(std::string laneID) {
193 7 : return getLane(laneID)->getMeanSpeed();
194 : }
195 :
196 :
197 : double
198 7 : TraCI_Lane::getLastStepOccupancy(std::string laneID) {
199 7 : return getLane(laneID)->getNettoOccupancy();
200 : }
201 :
202 :
203 : double
204 7 : TraCI_Lane::getLastStepLength(std::string laneID) {
205 7 : const MSLane* lane = getLane(laneID);
206 7 : double length = 0;
207 7 : const MSLane::VehCont& vehs = lane->getVehiclesSecure();
208 12 : for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
209 5 : length += (*j)->getVehicleType().getLength();
210 : }
211 7 : if (vehs.size() > 0) {
212 5 : length = length / (double) vehs.size();
213 : }
214 7 : lane->releaseVehicles();
215 7 : return length;
216 : }
217 :
218 :
219 : double
220 4 : TraCI_Lane::getWaitingTime(std::string laneID) {
221 4 : return getLane(laneID)->getWaitingSeconds();
222 : }
223 :
224 :
225 : double
226 7 : TraCI_Lane::getTraveltime(std::string laneID) {
227 7 : const MSLane* lane = getLane(laneID);
228 7 : double meanSpeed = lane->getMeanSpeed();
229 7 : if (meanSpeed != 0) {
230 7 : return lane->getLength() / meanSpeed;
231 : } else {
232 0 : return 1000000.;
233 : }
234 : }
235 :
236 :
237 : int
238 19 : TraCI_Lane::getLastStepVehicleNumber(std::string laneID) {
239 19 : return (int) getLane(laneID)->getVehicleNumber();
240 : }
241 :
242 : int
243 7 : TraCI_Lane::getLastStepHaltingNumber(std::string laneID) {
244 7 : const MSLane* lane = getLane(laneID);
245 7 : int halting = 0;
246 7 : const MSLane::VehCont& vehs = lane->getVehiclesSecure();
247 12 : for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
248 5 : if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
249 0 : ++halting;
250 : }
251 : }
252 7 : lane->releaseVehicles();
253 7 : return halting;
254 : }
255 :
256 :
257 : std::vector<std::string>
258 7 : TraCI_Lane::getLastStepVehicleIDs(std::string laneID) {
259 7 : const MSLane* lane = getLane(laneID);
260 7 : std::vector<std::string> vehIDs;
261 7 : const MSLane::VehCont& vehs = lane->getVehiclesSecure();
262 12 : for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
263 5 : vehIDs.push_back((*j)->getID());
264 : }
265 7 : lane->releaseVehicles();
266 7 : return vehIDs;
267 : }
268 :
269 :
270 :
271 :
272 : void
273 4 : TraCI_Lane::setAllowed(std::string laneID, std::vector<std::string> allowedClasses) {
274 4 : MSLane* l = const_cast<MSLane*>(getLane(laneID));
275 4 : l->setPermissions(parseVehicleClasses(allowedClasses), MSLane::CHANGE_PERMISSIONS_PERMANENT);
276 4 : l->getEdge().rebuildAllowedLanes();
277 4 : }
278 :
279 :
280 : void
281 4 : TraCI_Lane::setDisallowed(std::string laneID, std::vector<std::string> disallowedClasses) {
282 4 : MSLane* l = const_cast<MSLane*>(getLane(laneID));
283 4 : l->setPermissions(invertPermissions(parseVehicleClasses(disallowedClasses)), MSLane::CHANGE_PERMISSIONS_PERMANENT); // negation yields allowed
284 4 : l->getEdge().rebuildAllowedLanes();
285 4 : }
286 :
287 :
288 : void
289 4 : TraCI_Lane::setMaxSpeed(std::string laneID, double speed) {
290 4 : MSLane* l = const_cast<MSLane*>(getLane(laneID));
291 4 : l->setMaxSpeed(speed);
292 4 : }
293 :
294 :
295 : void
296 4 : TraCI_Lane::setLength(std::string laneID, double length) {
297 4 : MSLane* l = const_cast<MSLane*>(getLane(laneID));
298 4 : l->setLength(length);
299 4 : }
300 :
301 :
302 : std::string
303 8 : TraCI_Lane::getParameter(const std::string& laneID, const std::string& param) {
304 8 : return getLane(laneID)->getParameter(param, "");
305 : }
306 :
307 :
308 : void
309 4 : TraCI_Lane::setParameter(const std::string& laneID, const std::string& key, const std::string& value) {
310 4 : MSLane* l = const_cast<MSLane*>(getLane(laneID));
311 4 : l->setParameter(key, value);
312 4 : }
313 :
314 :
315 : const MSLane*
316 12877 : TraCI_Lane::getLane(const std::string& id) {
317 12877 : const MSLane* r = MSLane::dictionary(id);
318 12877 : if (r == 0) {
319 0 : throw TraCIException("Lane '" + id + "' is not known");
320 : }
321 12877 : return r;
322 43554 : }
323 :
324 :
325 : /****************************************************************************/
|