Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/LaneArea.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2012-2025 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/****************************************************************************/
21// C++ TraCI client API implementation
22/****************************************************************************/
23#include <config.h>
24
27#include <microsim/MSNet.h>
28#include <libsumo/Helper.h>
30#include "LaneArea.h"
31
32
33namespace libsumo {
34// ===========================================================================
35// static member initializations
36// ===========================================================================
37SubscriptionResults LaneArea::mySubscriptionResults;
38ContextSubscriptionResults LaneArea::myContextSubscriptionResults;
39NamedRTree* LaneArea::myTree(nullptr);
40
41
42// ===========================================================================
43// static member definitions
44// ===========================================================================
45std::vector<std::string>
46LaneArea::getIDList() {
47 std::vector<std::string> ids;
49 return ids;
50}
51
52
53int
54LaneArea::getIDCount() {
55 std::vector<std::string> ids;
57}
58
59
60int
61LaneArea::getJamLengthVehicle(const std::string& detID) {
62 return getDetector(detID)->getCurrentJamLengthInVehicles();
63}
64
65
66double
67LaneArea::getJamLengthMeters(const std::string& detID) {
68 return getDetector(detID)->getCurrentJamLengthInMeters();
69}
70
71
72double
73LaneArea::getLastStepMeanSpeed(const std::string& detID) {
74 return getDetector(detID)->getCurrentMeanSpeed();
75}
76
77
78std::vector<std::string>
79LaneArea::getLastStepVehicleIDs(const std::string& detID) {
80 return getDetector(detID)->getCurrentVehicleIDs();
81}
82
83
84double
85LaneArea::getLastStepOccupancy(const std::string& detID) {
86 return getDetector(detID)->getCurrentOccupancy();
87}
88
89
90double
91LaneArea::getPosition(const std::string& detID) {
92 return getDetector(detID)->getStartPos();
93}
94
95
96std::string
97LaneArea::getLaneID(const std::string& detID) {
98 return getDetector(detID)->getLane()->getID();
99}
100
101
102double
103LaneArea::getLength(const std::string& detID) {
104 const MSE2Collector* const e2 = getDetector(detID);
105 return e2->getLength();
106}
107
108
109int
110LaneArea::getLastStepVehicleNumber(const std::string& detID) {
111 return getDetector(detID)->getCurrentVehicleNumber();
112}
113
114
115int
116LaneArea::getLastStepHaltingNumber(const std::string& detID) {
117 return getDetector(detID)->getCurrentHaltingNumber();
118}
119
120
121double
122LaneArea::getIntervalOccupancy(const std::string& detID) {
123 return getDetector(detID)->getIntervalOccupancy();
124}
125
126double
127LaneArea::getIntervalMeanSpeed(const std::string& detID) {
128 return getDetector(detID)->getIntervalMeanSpeed();
129}
130
131double
132LaneArea::getIntervalMeanTimeLoss(const std::string& detID) {
133 return getDetector(detID)->getIntervalMeanTimeLoss();
134}
135
136double
137LaneArea::getIntervalMaxJamLengthInMeters(const std::string& detID) {
138 return getDetector(detID)->getIntervalMaxJamLengthInMeters();
139}
140
141int
142LaneArea::getIntervalVehicleNumber(const std::string& detID) {
143 return getDetector(detID)->getIntervalVehicleNumber();
144}
145
146double
147LaneArea::getLastIntervalOccupancy(const std::string& detID) {
148 return getDetector(detID)->getLastIntervalOccupancy();
149}
150
151double
152LaneArea::getLastIntervalMeanSpeed(const std::string& detID) {
153 return getDetector(detID)->getLastIntervalMeanSpeed();
154}
155
156double
157LaneArea::getLastIntervalMeanTimeLoss(const std::string& detID) {
158 return getDetector(detID)->getLastIntervalMeanTimeLoss();
159}
160
161double
162LaneArea::getLastIntervalMaxJamLengthInMeters(const std::string& detID) {
163 return getDetector(detID)->getLastIntervalMaxJamLengthInMeters();
164}
165
166int
167LaneArea::getLastIntervalVehicleNumber(const std::string& detID) {
168 return getDetector(detID)->getLastIntervalVehicleNumber();
169}
170
171
172std::string
173LaneArea::getParameter(const std::string& detID, const std::string& param) {
174 return getDetector(detID)->getParameter(param, "");
175}
176
177
179
180
181void
182LaneArea::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
183 getDetector(detID)->setParameter(name, value);
184}
185
186
188
189
191LaneArea::getDetector(const std::string& id) {
193 if (e2 == nullptr) {
194 throw TraCIException("Lane area detector '" + id + "' is not known");
195 }
196 return e2;
197}
198
199
201LaneArea::getTree() {
202 if (myTree == nullptr) {
203 myTree = new NamedRTree();
204 for (const std::string& id : getIDList()) {
205 PositionVector shape;
206 storeShape(id, shape);
207 Boundary b = shape.getBoxBoundary();
208 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
209 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
210 myTree->Insert(cmin, cmax, getDetector(id));
211 }
212 }
213 return myTree;
214}
215
216
217void
218LaneArea::cleanup() {
219 delete myTree;
220 myTree = nullptr;
221}
222
223
224void
225LaneArea::storeShape(const std::string& id, PositionVector& shape) {
226 MSE2Collector* const det = getDetector(id);
227 shape.push_back(det->getLanes().front()->getShape().positionAtOffset(det->getStartPos()));
228 shape.push_back(det->getLanes().back()->getShape().positionAtOffset(det->getEndPos()));
229}
230
231
232std::shared_ptr<VariableWrapper>
233LaneArea::makeWrapper() {
234 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
235}
236
237
238void
239LaneArea::overrideVehicleNumber(const std::string& detID, int vehNum) {
240 getDetector(detID)->overrideVehicleNumber(vehNum);
241}
242
243
244bool
245LaneArea::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
246 switch (variable) {
247 case TRACI_ID_LIST:
248 return wrapper->wrapStringList(objID, variable, getIDList());
249 case ID_COUNT:
250 return wrapper->wrapInt(objID, variable, getIDCount());
252 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
254 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
256 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
258 return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
260 return wrapper->wrapInt(objID, variable, getJamLengthVehicle(objID));
262 return wrapper->wrapDouble(objID, variable, getJamLengthMeters(objID));
264 return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
265 case VAR_POSITION:
266 return wrapper->wrapDouble(objID, variable, getPosition(objID));
267 case VAR_LANE_ID:
268 return wrapper->wrapString(objID, variable, getLaneID(objID));
269 case VAR_LENGTH:
270 return wrapper->wrapDouble(objID, variable, getLength(objID));
272 return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
274 return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
276 return wrapper->wrapDouble(objID, variable, getIntervalMeanTimeLoss(objID));
278 return wrapper->wrapDouble(objID, variable, getIntervalMaxJamLengthInMeters(objID));
280 return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
282 return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
284 return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
286 return wrapper->wrapDouble(objID, variable, getLastIntervalMeanTimeLoss(objID));
288 return wrapper->wrapDouble(objID, variable, getLastIntervalMaxJamLengthInMeters(objID));
290 return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
292 paramData->readUnsignedByte();
293 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
295 paramData->readUnsignedByte();
296 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
297 default:
298 return false;
299 }
300}
301
302
303}
304
305
306/****************************************************************************/
@ SUMO_TAG_LANE_AREA_DETECTOR
alternative tag for e2 detector
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:77
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:124
A class that stores a 2D geometrical boundary.
Definition Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition Boundary.cpp:127
double xmin() const
Returns minimum x-coordinate.
Definition Boundary.cpp:115
double ymax() const
Returns maximum y-coordinate.
Definition Boundary.cpp:133
double xmax() const
Returns maximum x-coordinate.
Definition Boundary.cpp:121
C++ TraCI client API implementation.
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
An areal detector corresponding to a sequence of consecutive lanes.
std::vector< MSLane * > getLanes()
Returns a vector containing pointers to the lanes covered by the detector ordered from its first to i...
double getStartPos() const
Returns the begin position of the detector.
double getEndPos() const
Returns the end position of the detector.
double getLength() const
Returns the length of the detector.
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition MSNet.h:455
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
T get(const std::string &id) const
Retrieves an item.
void insertIDs(std::vector< std::string > &into) const
int size() const
Returns the number of stored items within the container.
A RT-tree for efficient storing of SUMO's Named objects.
Definition NamedRTree.h:61
A list of positions.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
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 VAR_INTERVAL_TIMELOSS
TRACI_CONST int VAR_INTERVAL_NUMBER
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_LAST_INTERVAL_NUMBER
TRACI_CONST int VAR_LAST_INTERVAL_MAX_JAM_LENGTH_METERS
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:379
TRACI_CONST int VAR_INTERVAL_MAX_JAM_LENGTH_METERS
TRACI_CONST int VAR_POSITION
TRACI_CONST int LAST_STEP_MEAN_SPEED
TRACI_CONST int VAR_LAST_INTERVAL_OCCUPANCY
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:378
TRACI_CONST int JAM_LENGTH_METERS
TRACI_CONST int LAST_STEP_VEHICLE_HALTING_NUMBER
TRACI_CONST int VAR_LAST_INTERVAL_TIMELOSS
TRACI_CONST int VAR_LENGTH
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_LANE_ID
TRACI_CONST int LAST_STEP_OCCUPANCY
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_INTERVAL_SPEED
TRACI_CONST int VAR_LAST_INTERVAL_SPEED
TRACI_CONST int VAR_INTERVAL_OCCUPANCY
TRACI_CONST int JAM_LENGTH_VEHICLE