Eclipse SUMO - Simulation of Urban MObility
libsumo/LaneArea.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2012-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/****************************************************************************/
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
121std::string
122LaneArea::getParameter(const std::string& detID, const std::string& param) {
123 return getDetector(detID)->getParameter(param, "");
124}
125
126
128
129
130void
131LaneArea::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
132 getDetector(detID)->setParameter(name, value);
133}
134
135
137
138
140LaneArea::getDetector(const std::string& id) {
142 if (e2 == nullptr) {
143 throw TraCIException("Lane area detector '" + id + "' is not known");
144 }
145 return e2;
146}
147
148
150LaneArea::getTree() {
151 if (myTree == nullptr) {
152 myTree = new NamedRTree();
153 for (const std::string& id : getIDList()) {
154 PositionVector shape;
155 storeShape(id, shape);
156 Boundary b = shape.getBoxBoundary();
157 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
158 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
159 myTree->Insert(cmin, cmax, getDetector(id));
160 }
161 }
162 return myTree;
163}
164
165
166void
167LaneArea::cleanup() {
168 delete myTree;
169 myTree = nullptr;
170}
171
172
173void
174LaneArea::storeShape(const std::string& id, PositionVector& shape) {
175 MSE2Collector* const det = getDetector(id);
176 shape.push_back(det->getLanes().front()->getShape().positionAtOffset(det->getStartPos()));
177 shape.push_back(det->getLanes().back()->getShape().positionAtOffset(det->getEndPos()));
178}
179
180
181std::shared_ptr<VariableWrapper>
182LaneArea::makeWrapper() {
183 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
184}
185
186
187void
188LaneArea::overrideVehicleNumber(const std::string& detID, int num) {
189 getDetector(detID)->overrideVehicleNumber(num);
190}
191
192
193bool
194LaneArea::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
195 switch (variable) {
196 case TRACI_ID_LIST:
197 return wrapper->wrapStringList(objID, variable, getIDList());
198 case ID_COUNT:
199 return wrapper->wrapInt(objID, variable, getIDCount());
201 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
203 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
205 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
207 return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
209 return wrapper->wrapInt(objID, variable, getJamLengthVehicle(objID));
211 return wrapper->wrapDouble(objID, variable, getJamLengthMeters(objID));
213 return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
214 case VAR_POSITION:
215 return wrapper->wrapDouble(objID, variable, getPosition(objID));
216 case VAR_LANE_ID:
217 return wrapper->wrapString(objID, variable, getLaneID(objID));
218 case VAR_LENGTH:
219 return wrapper->wrapDouble(objID, variable, getLength(objID));
221 paramData->readUnsignedByte();
222 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
224 paramData->readUnsignedByte();
225 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
226 default:
227 return false;
228 }
229}
230
231
232}
233
234
235/****************************************************************************/
@ SUMO_TAG_LANE_AREA_DETECTOR
alternative tag for e2 detector
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition: TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:118
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:124
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.
Definition: MSE2Collector.h:79
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:442
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
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 LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:301
TRACI_CONST int VAR_POSITION
TRACI_CONST int LAST_STEP_MEAN_SPEED
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:300
TRACI_CONST int JAM_LENGTH_METERS
TRACI_CONST int LAST_STEP_VEHICLE_HALTING_NUMBER
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 JAM_LENGTH_VEHICLE