Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2024 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 : /****************************************************************************/
14 : /// @file LaneArea.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Mario Krumnow
17 : /// @author Jakob Erdmann
18 : /// @author Michael Behrisch
19 : /// @date 30.05.2012
20 : ///
21 : // C++ TraCI client API implementation
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <microsim/output/MSDetectorControl.h>
26 : #include <microsim/output/MSE2Collector.h>
27 : #include <microsim/MSNet.h>
28 : #include <libsumo/Helper.h>
29 : #include <libsumo/TraCIConstants.h>
30 : #include "LaneArea.h"
31 :
32 :
33 : namespace libsumo {
34 : // ===========================================================================
35 : // static member initializations
36 : // ===========================================================================
37 : SubscriptionResults LaneArea::mySubscriptionResults;
38 : ContextSubscriptionResults LaneArea::myContextSubscriptionResults;
39 : NamedRTree* LaneArea::myTree(nullptr);
40 :
41 :
42 : // ===========================================================================
43 : // static member definitions
44 : // ===========================================================================
45 : std::vector<std::string>
46 873 : LaneArea::getIDList() {
47 : std::vector<std::string> ids;
48 873 : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).insertIDs(ids);
49 871 : return ids;
50 2 : }
51 :
52 :
53 : int
54 21 : LaneArea::getIDCount() {
55 : std::vector<std::string> ids;
56 42 : return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).size();
57 21 : }
58 :
59 :
60 : int
61 505 : LaneArea::getJamLengthVehicle(const std::string& detID) {
62 505 : return getDetector(detID)->getCurrentJamLengthInVehicles();
63 : }
64 :
65 :
66 : double
67 504 : LaneArea::getJamLengthMeters(const std::string& detID) {
68 504 : return getDetector(detID)->getCurrentJamLengthInMeters();
69 : }
70 :
71 :
72 : double
73 504 : LaneArea::getLastStepMeanSpeed(const std::string& detID) {
74 504 : return getDetector(detID)->getCurrentMeanSpeed();
75 : }
76 :
77 :
78 : std::vector<std::string>
79 744 : LaneArea::getLastStepVehicleIDs(const std::string& detID) {
80 744 : return getDetector(detID)->getCurrentVehicleIDs();
81 : }
82 :
83 :
84 : double
85 504 : LaneArea::getLastStepOccupancy(const std::string& detID) {
86 504 : return getDetector(detID)->getCurrentOccupancy();
87 : }
88 :
89 :
90 : double
91 505 : LaneArea::getPosition(const std::string& detID) {
92 505 : return getDetector(detID)->getStartPos();
93 : }
94 :
95 :
96 : std::string
97 505 : LaneArea::getLaneID(const std::string& detID) {
98 505 : return getDetector(detID)->getLane()->getID();
99 : }
100 :
101 :
102 : double
103 505 : LaneArea::getLength(const std::string& detID) {
104 505 : const MSE2Collector* const e2 = getDetector(detID);
105 505 : return e2->getLength();
106 : }
107 :
108 :
109 : int
110 1104 : LaneArea::getLastStepVehicleNumber(const std::string& detID) {
111 1104 : return getDetector(detID)->getCurrentVehicleNumber();
112 : }
113 :
114 :
115 : int
116 504 : LaneArea::getLastStepHaltingNumber(const std::string& detID) {
117 504 : return getDetector(detID)->getCurrentHaltingNumber();
118 : }
119 :
120 :
121 : double
122 720 : LaneArea::getIntervalOccupancy(const std::string& detID) {
123 720 : return getDetector(detID)->getIntervalOccupancy();
124 : }
125 :
126 : double
127 720 : LaneArea::getIntervalMeanSpeed(const std::string& detID) {
128 720 : return getDetector(detID)->getIntervalMeanSpeed();
129 : }
130 :
131 : double
132 720 : LaneArea::getIntervalMaxJamLengthInMeters(const std::string& detID) {
133 720 : return getDetector(detID)->getIntervalMaxJamLengthInMeters();
134 : }
135 :
136 : int
137 720 : LaneArea::getIntervalVehicleNumber(const std::string& detID) {
138 720 : return getDetector(detID)->getIntervalVehicleNumber();
139 : }
140 :
141 : double
142 720 : LaneArea::getLastIntervalOccupancy(const std::string& detID) {
143 720 : return getDetector(detID)->getLastIntervalOccupancy();
144 : }
145 :
146 : double
147 720 : LaneArea::getLastIntervalMeanSpeed(const std::string& detID) {
148 720 : return getDetector(detID)->getLastIntervalMeanSpeed();
149 : }
150 :
151 : double
152 720 : LaneArea::getLastIntervalMaxJamLengthInMeters(const std::string& detID) {
153 720 : return getDetector(detID)->getLastIntervalMaxJamLengthInMeters();
154 : }
155 :
156 : int
157 720 : LaneArea::getLastIntervalVehicleNumber(const std::string& detID) {
158 720 : return getDetector(detID)->getLastIntervalVehicleNumber();
159 : }
160 :
161 :
162 : std::string
163 36 : LaneArea::getParameter(const std::string& detID, const std::string& param) {
164 72 : return getDetector(detID)->getParameter(param, "");
165 : }
166 :
167 :
168 0 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(LaneArea)
169 :
170 :
171 : void
172 18 : LaneArea::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
173 18 : getDetector(detID)->setParameter(name, value);
174 18 : }
175 :
176 :
177 272 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(LaneArea, LANEAREA)
178 :
179 :
180 : MSE2Collector*
181 11973 : LaneArea::getDetector(const std::string& id) {
182 23945 : MSE2Collector* e2 = dynamic_cast<MSE2Collector*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).get(id));
183 11973 : if (e2 == nullptr) {
184 2 : throw TraCIException("Lane area detector '" + id + "' is not known");
185 : }
186 11972 : return e2;
187 : }
188 :
189 :
190 : NamedRTree*
191 212 : LaneArea::getTree() {
192 212 : if (myTree == nullptr) {
193 10 : myTree = new NamedRTree();
194 40 : for (const std::string& id : getIDList()) {
195 30 : PositionVector shape;
196 30 : storeShape(id, shape);
197 30 : Boundary b = shape.getBoxBoundary();
198 30 : const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
199 30 : const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
200 30 : myTree->Insert(cmin, cmax, getDetector(id));
201 40 : }
202 : }
203 212 : return myTree;
204 : }
205 :
206 :
207 : void
208 35146 : LaneArea::cleanup() {
209 35146 : delete myTree;
210 35146 : myTree = nullptr;
211 35146 : }
212 :
213 :
214 : void
215 215 : LaneArea::storeShape(const std::string& id, PositionVector& shape) {
216 215 : MSE2Collector* const det = getDetector(id);
217 430 : shape.push_back(det->getLanes().front()->getShape().positionAtOffset(det->getStartPos()));
218 430 : shape.push_back(det->getLanes().back()->getShape().positionAtOffset(det->getEndPos()));
219 215 : }
220 :
221 :
222 : std::shared_ptr<VariableWrapper>
223 263 : LaneArea::makeWrapper() {
224 263 : return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
225 : }
226 :
227 :
228 : void
229 30 : LaneArea::overrideVehicleNumber(const std::string& detID, int vehNum) {
230 30 : getDetector(detID)->overrideVehicleNumber(vehNum);
231 30 : }
232 :
233 :
234 : bool
235 8491 : LaneArea::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
236 8491 : switch (variable) {
237 567 : case TRACI_ID_LIST:
238 567 : return wrapper->wrapStringList(objID, variable, getIDList());
239 15 : case ID_COUNT:
240 15 : return wrapper->wrapInt(objID, variable, getIDCount());
241 856 : case LAST_STEP_VEHICLE_NUMBER:
242 856 : return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
243 336 : case LAST_STEP_MEAN_SPEED:
244 336 : return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
245 496 : case LAST_STEP_VEHICLE_ID_LIST:
246 496 : return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
247 336 : case LAST_STEP_VEHICLE_HALTING_NUMBER:
248 336 : return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
249 337 : case JAM_LENGTH_VEHICLE:
250 337 : return wrapper->wrapInt(objID, variable, getJamLengthVehicle(objID));
251 336 : case JAM_LENGTH_METERS:
252 336 : return wrapper->wrapDouble(objID, variable, getJamLengthMeters(objID));
253 336 : case LAST_STEP_OCCUPANCY:
254 336 : return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
255 337 : case VAR_POSITION:
256 337 : return wrapper->wrapDouble(objID, variable, getPosition(objID));
257 337 : case VAR_LANE_ID:
258 674 : return wrapper->wrapString(objID, variable, getLaneID(objID));
259 337 : case VAR_LENGTH:
260 337 : return wrapper->wrapDouble(objID, variable, getLength(objID));
261 480 : case VAR_INTERVAL_OCCUPANCY:
262 480 : return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
263 480 : case VAR_INTERVAL_SPEED:
264 480 : return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
265 480 : case VAR_INTERVAL_MAX_JAM_LENGTH_METERS:
266 480 : return wrapper->wrapDouble(objID, variable, getIntervalMaxJamLengthInMeters(objID));
267 480 : case VAR_INTERVAL_NUMBER:
268 480 : return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
269 480 : case VAR_LAST_INTERVAL_OCCUPANCY:
270 480 : return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
271 480 : case VAR_LAST_INTERVAL_SPEED:
272 480 : return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
273 480 : case VAR_LAST_INTERVAL_MAX_JAM_LENGTH_METERS:
274 480 : return wrapper->wrapDouble(objID, variable, getLastIntervalMaxJamLengthInMeters(objID));
275 480 : case VAR_LAST_INTERVAL_NUMBER:
276 480 : return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
277 24 : case libsumo::VAR_PARAMETER:
278 24 : paramData->readUnsignedByte();
279 48 : return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
280 0 : case libsumo::VAR_PARAMETER_WITH_KEY:
281 0 : paramData->readUnsignedByte();
282 0 : return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
283 : default:
284 : return false;
285 : }
286 : }
287 :
288 :
289 : }
290 :
291 :
292 : /****************************************************************************/
|