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