Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/Polygon.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2026 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/****************************************************************************/
18// C++ TraCI client API implementation
19/****************************************************************************/
20#include <microsim/MSNet.h>
30
31#include "Polygon.h"
32#include "Helper.h"
33
34
35namespace libsumo {
36// ===========================================================================
37// static member initializations
38// ===========================================================================
39SubscriptionResults Polygon::mySubscriptionResults;
40ContextSubscriptionResults Polygon::myContextSubscriptionResults;
41NamedRTree* Polygon::myTree(nullptr);
42
43
44// ===========================================================================
45// static member definitions
46// ===========================================================================
47std::vector<std::string>
48Polygon::getIDList() {
49 std::vector<std::string> ids;
51 return ids;
52}
53
54
55int
56Polygon::getIDCount() {
57 return (int)getIDList().size();
58}
59
60
61std::string
62Polygon::getType(const std::string& polygonID) {
63 return getPolygon(polygonID)->getShapeType();
64}
65
66
67TraCIPositionVector
68Polygon::getShape(const std::string& polygonID) {
69 SUMOPolygon* p = getPolygon(polygonID);
71}
72
73
74bool
75Polygon::getFilled(const std::string& polygonID) {
76 return getPolygon(polygonID)->getFill();
77}
78
79
80double
81Polygon::getLineWidth(const std::string& polygonID) {
82 return getPolygon(polygonID)->getLineWidth();
83}
84
85
86double
87Polygon::getHeight(const std::string& polygonID) {
88 return getPolygon(polygonID)->getHeight();
89}
90
91
92TraCIColor
93Polygon::getColor(const std::string& polygonID) {
94 SUMOPolygon* p = getPolygon(polygonID);
96}
97
98
99std::string
100Polygon::getParameter(const std::string& polygonID, const std::string& key) {
101 return getPolygon(polygonID)->getParameter(key, "");
102}
103
104
106
107
108void
109Polygon::setType(const std::string& polygonID, const std::string& polygonType) {
110 getPolygon(polygonID)->setShapeType(polygonType);
111}
112
113
114void
115Polygon::setShape(const std::string& polygonID, const TraCIPositionVector& shape) {
116 PositionVector positionVector = Helper::makePositionVector(shape);
117 getPolygon(polygonID); // just to check whether it exists
119 shapeCont.reshapePolygon(polygonID, positionVector);
120}
121
122
123void
124Polygon::setColor(const std::string& polygonID, const TraCIColor& c) {
125 getPolygon(polygonID)->setShapeColor(Helper::makeRGBColor(c));
126}
127
128
129void
130Polygon::add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& color, bool fill, const std::string& polygonType, int layer, double lineWidth) {
133 RGBColor col = Helper::makeRGBColor(color);
134 if (!shapeCont.addPolygon(polygonID, polygonType, col, (double)layer, Shape::DEFAULT_ANGLE, Shape::DEFAULT_IMG_FILE, pShape, false, fill, lineWidth)) {
135 throw TraCIException("Could not add polygon '" + polygonID + "'");
136 }
137 if (myTree != nullptr) {
138 SUMOPolygon* p = shapeCont.getPolygons().get(polygonID);
140 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
141 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
142 myTree->Insert(cmin, cmax, p);
143 }
144}
145
146
147void
148Polygon::addHighlightPolygon(const std::string& objectID, const int type, const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& color, bool fill, const std::string& polygonType, int layer, double lineWidth) {
149 add(polygonID, shape, color, fill, polygonType, layer, lineWidth);
150 MSNet::getInstance()->getShapeContainer().registerHighlight(objectID, type, polygonID);
151}
152
153
154void
155Polygon::addDynamics(const std::string& polygonID, const std::string& trackedObjectID, const std::vector<double>& timeSpan, const std::vector<double>& alphaSpan, bool looped, bool rotate) {
156 if (timeSpan.empty()) {
157 if (trackedObjectID == "") {
158 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': dynamics underspecified (either a tracked object ID or a time span have to be provided).");
159 }
160 if (looped) {
161 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': looped==true requires time line of positive length.");
162 }
163 }
164 if (timeSpan.size() == 1) {
165 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': time span cannot have length one.");
166 } else if (timeSpan.size() > 0 && timeSpan[0] != 0.0) {
167 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': first element of time span must be zero.");
168 }
169 if (timeSpan.size() != alphaSpan.size() && alphaSpan.size() != 0) {
170 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': alpha span must have length zero or equal to time span length.");
171 }
172 if (timeSpan.size() >= 2) {
173 for (unsigned int i = 1; i < timeSpan.size(); ++i) {
174 if (timeSpan[i - 1] > timeSpan[i]) {
175 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': entries of time span must be ordered ascendingly.");
176 }
177 }
178 }
179
180 SUMOTrafficObject* obj = getTrafficObject(trackedObjectID);
182 PolygonDynamics* pd = shapeCont.addPolygonDynamics(SIMTIME, polygonID, obj, timeSpan, alphaSpan, looped, rotate);
183 if (pd == nullptr) {
184 throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': polygon doesn't exist.");
185 }
186 // Ensure existence of a DynamicShapeUpdater
187 if (MSNet::getInstance()->getDynamicShapeUpdater() == nullptr) {
190 }
191
192 // Schedule the regular polygon update
194 shapeCont.addPolygonUpdateCommand(pd->getPolygonID(), cmd);
196}
197
198
199void
200Polygon::remove(const std::string& polygonID, int /* layer */) {
201 // !!! layer not used yet (shouldn't the id be enough?)
203 if (myTree != nullptr) {
204 SUMOPolygon* p = shapeCont.getPolygons().get(polygonID);
205 if (p != nullptr) {
207 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
208 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
209 myTree->Remove(cmin, cmax, p);
210 }
211 }
212 if (!shapeCont.removePolygon(polygonID)) {
213 throw TraCIException("Could not remove polygon '" + polygonID + "'");
214 }
215}
216
217
218void
219Polygon::setFilled(std::string polygonID, bool filled) {
220 getPolygon(polygonID)->setFill(filled);
221}
222
223
224void
225Polygon::setLineWidth(std::string polygonID, double lineWidth) {
226 getPolygon(polygonID)->setLineWidth(lineWidth);
227}
228
229
230void
231Polygon::setHeight(std::string polygonID, double height) {
232 getPolygon(polygonID)->setHeight(height);
233}
234
235
237Polygon::getPolygon(const std::string& id) {
239 if (p == nullptr) {
240 throw TraCIException("Polygon '" + id + "' is not known");
241 }
242 return p;
243}
244
245
247Polygon::getTrafficObject(const std::string& id) {
248 if (id == "") {
249 return nullptr;
250 }
251 MSNet* net = MSNet::getInstance();
252 // First try to find a vehicle with the given id
253 SUMOVehicle* sumoVehicle = net->getVehicleControl().getVehicle(id);
254 if (sumoVehicle != nullptr) {
255 return static_cast<SUMOTrafficObject*>(sumoVehicle);
256 }
257 MSTransportable* transportable = net->getPersonControl().get(id);
258 if (transportable != nullptr) {
259 return static_cast<SUMOTrafficObject*>(transportable);
260 } else {
261 throw TraCIException("Traffic object '" + id + "' is not known");
262 }
263}
264
265
266void
267Polygon::setParameter(const std::string& polygonID, const std::string& key, const std::string& value) {
268 SUMOPolygon* p = getPolygon(polygonID);
269 p->setParameter(key, value);
270}
271
272
274
275
277Polygon::getTree() {
278 if (myTree == nullptr) {
279 myTree = new NamedRTree();
281 for (const auto& i : shapeCont.getPolygons()) {
282 Boundary b = i.second->getShape().getBoxBoundary();
283 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
284 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
285 myTree->Insert(cmin, cmax, i.second);
286 }
287 }
288 return myTree;
289}
290
291void
292Polygon::cleanup() {
293 delete myTree;
294 myTree = nullptr;
295}
296
297void
298Polygon::storeShape(const std::string& id, PositionVector& shape) {
299 shape = getPolygon(id)->getShape();
300}
301
302
303std::shared_ptr<VariableWrapper>
304Polygon::makeWrapper() {
305 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
306}
307
308
309bool
310Polygon::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
311 switch (variable) {
312 case TRACI_ID_LIST:
313 return wrapper->wrapStringList(objID, variable, getIDList());
314 case ID_COUNT:
315 return wrapper->wrapInt(objID, variable, getIDCount());
316 case VAR_TYPE:
317 return wrapper->wrapString(objID, variable, getType(objID));
318 case VAR_COLOR:
319 return wrapper->wrapColor(objID, variable, getColor(objID));
320 case VAR_FILL:
321 return wrapper->wrapInt(objID, variable, getFilled(objID));
322 case VAR_WIDTH:
323 return wrapper->wrapDouble(objID, variable, getLineWidth(objID));
324 case VAR_HEIGHT:
325 return wrapper->wrapDouble(objID, variable, getHeight(objID));
326 case VAR_SHAPE:
327 return wrapper->wrapPositionVector(objID, variable, getShape(objID));
329 paramData->readUnsignedByte();
330 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
332 paramData->readUnsignedByte();
333 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
334 default:
335 return false;
336 }
337}
338
339
340bool
341Polygon::exists(std::string polyID) {
343 return p != nullptr;
344}
345}
346
347
348/****************************************************************************/
#define SIMSTEP
Definition SUMOTime.h:64
#define SIMTIME
Definition SUMOTime.h:65
#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
PositionVector getShape(const bool closeShape) const
get position vector (shape) based on this boundary
Definition Boundary.cpp:444
double ymax() const
Returns maximum y-coordinate.
Definition Boundary.cpp:133
double xmax() const
Returns maximum x-coordinate.
Definition Boundary.cpp:121
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Interface for objects listening to vehicle state changes.
Definition MSNet.h:669
The simulated network and simulation perfomer.
Definition MSNet.h:89
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:199
MSEventControl * getEndOfTimestepEvents()
Returns the event control for events executed at the end of a time step.
Definition MSNet.h:505
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
Definition MSNet.cpp:1362
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition MSNet.h:525
MSDynamicShapeUpdater * makeDynamicShapeUpdater()
Creates and returns a dynamic shapes updater.
Definition MSNet.cpp:1311
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:402
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition MSNet.cpp:1294
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
T get(const std::string &id) const
Retrieves an item.
void insertIDs(std::vector< std::string > &into) const
A RT-tree for efficient storing of SUMO's Named objects.
Definition NamedRTree.h:61
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
A wrapper for a Command function with parameter.
const std::string & getPolygonID() const
A 2D- or 3D-polygon.
A list of positions.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
const PositionVector & getShape() const
Returns the shape of the polygon.
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition SUMOVehicle.h:63
Storage for geometrical objects.
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
virtual void reshapePolygon(const std::string &id, const PositionVector &shape)
Assigns a shape to the named polygon.
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
const Polygons & getPolygons() const
Returns all polygons.
virtual PolygonDynamics * addPolygonDynamics(double simtime, std::string polyID, SUMOTrafficObject *trackedObject, const std::vector< double > &timeSpan, const std::vector< double > &alphaSpan, bool looped, bool rotate)
Adds dynamics (animation / tracking) to the given polygon.
virtual void addPolygonUpdateCommand(std::string polyID, ParametrisedWrappingCommand< ShapeContainer, PolygonDynamics * > *cmd)
Register update command (for descheduling at removal)
virtual void registerHighlight(const std::string &objectID, const int type, const std::string &polygonID)
register highlight of the specified type if the given id
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, double layer, double angle, const std::string &imgFile, const PositionVector &shape, bool geo, bool fill, double lineWidth, bool ignorePruning=false, const std::string &name=Shape::DEFAULT_NAME)
Builds a polygon using the given values and adds it to the container.
static const std::string DEFAULT_IMG_FILE
Definition Shape.h:47
static const double DEFAULT_ANGLE
Definition Shape.h:46
const RGBColor & getShapeColor() const
Returns the color of the Shape.
Definition Shape.cpp:105
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition Helper.cpp:363
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition Helper.cpp:353
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition Helper.cpp:376
static RGBColor makeRGBColor(const TraCIColor &color)
Definition Helper.cpp:387
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_TYPE
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:379
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:378
TRACI_CONST int VAR_SHAPE
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int VAR_FILL