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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
14 // C++ TraCI client API implementation
15 /****************************************************************************/
16 
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <microsim/MSNet.h>
26 #include <libsumo/TraCIConstants.h>
31 
32 #include "Polygon.h"
33 #include "Helper.h"
34 
35 
36 namespace libsumo {
37 // ===========================================================================
38 // static member initializations
39 // ===========================================================================
42 
43 
44 // ===========================================================================
45 // static member definitions
46 // ===========================================================================
47 std::vector<std::string>
49  std::vector<std::string> ids;
51  shapeCont.getPolygons().insertIDs(ids);
52  return ids;
53 }
54 
55 
56 int
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 
98 void
99 Polygon::setType(const std::string& polygonID, const std::string& setType) {
100  SUMOPolygon* p = getPolygon(polygonID);
101  p->setShapeType(setType);
102 }
103 
104 
105 void
106 Polygon::setShape(const std::string& polygonID, const TraCIPositionVector& shape) {
107  PositionVector positionVector = Helper::makePositionVector(shape);
108  getPolygon(polygonID); // just to check whether it exists
110  shapeCont.reshapePolygon(polygonID, positionVector);
111 }
112 
113 
114 void
115 Polygon::setColor(const std::string& polygonID, const TraCIColor& c) {
117 }
118 
119 
120 void
121 Polygon::add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& color, bool fill, const std::string& polygonType, int layer, double lineWidth) {
124  RGBColor col = Helper::makeRGBColor(color);
125  if (!shapeCont.addPolygon(polygonID, polygonType, col, (double)layer, Shape::DEFAULT_ANGLE, Shape::DEFAULT_IMG_FILE, Shape::DEFAULT_RELATIVEPATH, pShape, false, fill, lineWidth)) {
126  throw TraCIException("Could not add polygon '" + polygonID + "'");
127  }
128 }
129 
130 
131 void
132 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) {
133  add(polygonID, shape, color, fill, polygonType, layer, lineWidth);
134  MSNet::getInstance()->getShapeContainer().registerHighlight(objectID, type, polygonID);
135 }
136 
137 
138 void
139 Polygon::addDynamics(const std::string& polygonID, const std::string& trackedID, const std::vector<double>& timeSpan, const std::vector<double>& alphaSpan, bool looped, bool rotate) {
140  if (timeSpan.empty()) {
141  if (trackedID == "") {
142  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).");
143  }
144  if (looped) {
145  throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': looped==true requires time line of positive length.");
146  }
147  }
148  if (timeSpan.size() == 1) {
149  throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': time span cannot have length one.");
150  } else if (timeSpan.size() > 0 && timeSpan[0] != 0.0) {
151  throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': first element of time span must be zero.");
152  }
153  if (timeSpan.size() != alphaSpan.size() && alphaSpan.size() != 0) {
154  throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': alpha span must have length zero or equal to time span length.");
155  }
156  if (timeSpan.size() >= 2) {
157  for (unsigned int i = 1; i < timeSpan.size(); ++i) {
158  if (timeSpan[i - 1] > timeSpan[i]) {
159  throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': entries of time span must be ordered ascendingly.");
160  }
161  }
162  }
163 
164  SUMOTrafficObject* obj = getTrafficObject(trackedID);
166  PolygonDynamics* pd = shapeCont.addPolygonDynamics(SIMTIME, polygonID, obj, timeSpan, alphaSpan, looped, rotate);
167  if (pd == nullptr) {
168  throw TraCIException("Could not add polygon dynamics for polygon '" + polygonID + "': polygon doesn't exist.");
169  }
170  // Ensure existence of a DynamicShapeUpdater
171  if (MSNet::getInstance()->getDynamicShapeUpdater() == nullptr) {
172  MSNet::VehicleStateListener* listener = dynamic_cast<MSNet::VehicleStateListener*>(MSNet::getInstance()->makeDynamicShapeUpdater());
174  }
175 
176  // Schedule the regular polygon update
178  shapeCont.addPolygonUpdateCommand(pd->getPolygonID(), cmd);
180 }
181 
182 
183 void
184 Polygon::remove(const std::string& polygonID, int /* layer */) {
185  // !!! layer not used yet (shouldn't the id be enough?)
187  if (!shapeCont.removePolygon(polygonID)) {
188  throw TraCIException("Could not remove polygon '" + polygonID + "'");
189  }
190 }
191 
192 
193 void
194 Polygon::setFilled(std::string polygonID, bool filled) {
195  SUMOPolygon* p = getPolygon(polygonID);
196  p->setFill(filled);
197 }
198 
199 void
200 Polygon::setLineWidth(std::string polygonID, double lineWidth) {
201  SUMOPolygon* p = getPolygon(polygonID);
202  p->setLineWidth(lineWidth);
203 }
204 
205 
207 Polygon::getPolygon(const std::string& id) {
209  if (p == nullptr) {
210  throw TraCIException("Polygon '" + id + "' is not known");
211  }
212  return p;
213 }
214 
215 
217 Polygon::getTrafficObject(const std::string& id) {
218  if (id == "") {
219  return nullptr;
220  }
221  MSNet* net = MSNet::getInstance();
222  // First try to find a vehicle with the given id
223  SUMOVehicle* sumoVehicle = net->getVehicleControl().getVehicle(id);
224  if (sumoVehicle != nullptr) {
225  return static_cast<SUMOTrafficObject*>(sumoVehicle);
226  }
227  MSTransportable* transportable = net->getPersonControl().get(id);
228  if (transportable != nullptr) {
229  return static_cast<SUMOTrafficObject*>(transportable);
230  } else {
231  throw TraCIException("Traffic object '" + id + "' is not known");
232  }
233 }
234 
235 
236 void
237 Polygon::setParameter(const std::string& polygonID, const std::string& key, const std::string& value) {
238  SUMOPolygon* p = getPolygon(polygonID);
239  p->setParameter(key, value);
240 }
241 
242 
244 
245 
246 NamedRTree*
248  NamedRTree* t = new NamedRTree();
250  for (const auto& i : shapeCont.getPolygons()) {
251  Boundary b = i.second->getShape().getBoxBoundary();
252  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
253  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
254  t->Insert(cmin, cmax, i.second);
255  }
256  return t;
257 }
258 
259 
260 void
261 Polygon::storeShape(const std::string& id, PositionVector& shape) {
262  shape = getPolygon(id)->getShape();
263 }
264 
265 
266 std::shared_ptr<VariableWrapper>
268  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
269 }
270 
271 
272 bool
273 Polygon::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
274  switch (variable) {
275  case TRACI_ID_LIST:
276  return wrapper->wrapStringList(objID, variable, getIDList());
277  case ID_COUNT:
278  return wrapper->wrapInt(objID, variable, getIDCount());
279  case VAR_TYPE:
280  return wrapper->wrapString(objID, variable, getType(objID));
281  case VAR_COLOR:
282  return wrapper->wrapColor(objID, variable, getColor(objID));
283  case VAR_FILL:
284  return wrapper->wrapInt(objID, variable, getFilled(objID));
285  case VAR_WIDTH:
286  return wrapper->wrapDouble(objID, variable, getLineWidth(objID));
287  default:
288  return false;
289  }
290 }
291 
292 
293 bool
294 Polygon::exists(std::string polyID) {
296  return p != nullptr;
297 }
298 
299 }
300 
301 
302 /****************************************************************************/
Shape::setShapeType
void setShapeType(const std::string &type)
Sets a new type.
Definition: Shape.h:122
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
PolygonDynamics.h
libsumo::Polygon::setColor
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:115
libsumo::Polygon::add
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:121
TraCIPositionVector
A list of positions.
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
MSNet.h
libsumo::Polygon::getTree
static NamedRTree * getTree()
Returns a tree filled with polygon instances.
Definition: Polygon.cpp:247
libsumo::Polygon::addDynamics
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:139
libsumo::VariableWrapper::wrapString
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
libsumo::Polygon::getIDList
static std::vector< std::string > getIDList()
Definition: Polygon.cpp:48
libsumo::Helper::makeTraCIColor
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:292
Boundary::ymin
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
NamedObjectCont::insertIDs
void insertIDs(std::vector< std::string > &into) const
Definition: NamedObjectCont.h:123
MSNet::getShapeContainer
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:459
libsumo::VariableWrapper
Definition: Subscription.h:132
ShapeContainer::polygonDynamicsUpdate
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
Definition: ShapeContainer.cpp:217
libsumo::Polygon
Definition: Polygon.h:46
libsumo::VAR_COLOR
TRACI_CONST int VAR_COLOR
Definition: TraCIConstants.h:630
MSNet::VehicleStateListener
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:568
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:91
libsumo::Helper::makePositionVector
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition: Helper.cpp:279
libsumo::Helper::makeRGBColor
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:303
libsumo::Polygon::getPolygon
static SUMOPolygon * getPolygon(const std::string &id)
Definition: Polygon.cpp:207
libsumo::VAR_WIDTH
TRACI_CONST int VAR_WIDTH
Definition: TraCIConstants.h:666
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
libsumo::Polygon::getParameter
static std::string getParameter(const std::string &polygonID, const std::string &key)
Definition: Polygon.cpp:93
libsumo::Polygon::remove
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:184
Boundary::xmax
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:124
ShapeContainer
Storage for geometrical objects.
Definition: ShapeContainer.h:49
libsumo::ContextSubscriptionResults
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:204
ShapeContainer::reshapePolygon
virtual void reshapePolygon(const std::string &id, const PositionVector &shape)
Assigns a shape to the named polygon.
Definition: ShapeContainer.cpp:178
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:135
MSTransportable
Definition: MSTransportable.h:58
PositionVector
A list of positions.
Definition: PositionVector.h:45
PolygonDynamics::getPolygonID
const std::string & getPolygonID() const
Definition: PolygonDynamics.h:54
libsumo::Polygon::myContextSubscriptionResults
static ContextSubscriptionResults myContextSubscriptionResults
Definition: Polygon.h:99
Shape::getShapeColor
const RGBColor & getShapeColor() const
Returns the color of the Shape.
Definition: Shape.h:82
libsumo::Polygon::getIDCount
static int getIDCount()
Definition: Polygon.cpp:57
MSDynamicShapeUpdater.h
libsumo::Polygon::setShape
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:106
libsumo::VariableWrapper::wrapColor
virtual bool wrapColor(const std::string &objID, const int variable, const TraCIColor &value)=0
Parameterised::getParameter
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Definition: Parameterised.cpp:72
libsumo
Definition: Edge.cpp:29
libsumo::Polygon::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Polygon.cpp:273
MSTransportableControl::get
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
Definition: MSTransportableControl.cpp:75
Shape::DEFAULT_RELATIVEPATH
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:48
RGBColor
Definition: RGBColor.h:39
libsumo::VariableWrapper::wrapDouble
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
libsumo::Polygon::getTrafficObject
static SUMOTrafficObject * getTrafficObject(const std::string &id)
Obtain a traffic object with the given id if one exists.
Definition: Polygon.cpp:217
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:63
MSVehicleControl::getVehicle
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
Definition: MSVehicleControl.cpp:240
TraCIConstants.h
Boundary::xmin
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:118
NamedRTree::Insert
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:81
MSTransportableControl.h
MSNet::getEndOfTimestepEvents
MSEventControl * getEndOfTimestepEvents()
Returns the event control for events executed at the end of a time step.
Definition: MSNet.h:439
libsumo::Polygon::getColor
static TraCIColor getColor(const std::string &polygonID)
Definition: Polygon.cpp:86
libsumo::VariableWrapper::wrapStringList
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
libsumo::Polygon::addHighlightPolygon
static LIBSUMO_SUBSCRIPTION_API 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:132
libsumo::Polygon::getLineWidth
static double getLineWidth(const std::string &polygonID)
Definition: Polygon.cpp:81
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
libsumo::Polygon::storeShape
static void storeShape(const std::string &id, PositionVector &shape)
Saves the shape of the requested object in the given container.
Definition: Polygon.cpp:261
MSNet::addVehicleStateListener
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
Definition: MSNet.cpp:877
ShapeContainer::addPolygonDynamics
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.
Definition: ShapeContainer.cpp:73
Helper.h
libsumo::Polygon::setFilled
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:194
libsumo::Helper::makeTraCIPositionVector
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:269
libsumo::TRACI_ID_LIST
TRACI_CONST int TRACI_ID_LIST
Definition: TraCIConstants.h:498
SIMSTEP
#define SIMSTEP
Definition: SUMOTime.h:62
Shape::getShapeType
const std::string & getShapeType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:75
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:50
libsumo::ID_COUNT
TRACI_CONST int ID_COUNT
Definition: TraCIConstants.h:501
SUMOPolygon::getShape
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:81
SUMOPolygon::setFill
void setFill(bool fill)
Sets whether the polygon shall be filled.
Definition: SUMOPolygon.h:108
libsumo::TraCIException
Definition: TraCIDefs.h:89
libsumo::Polygon::getType
static std::string getType(const std::string &polygonID)
Definition: Polygon.cpp:63
ShapeContainer::addPolygon
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.
Definition: ShapeContainer.cpp:64
Polygon.h
libsumo::VAR_TYPE
TRACI_CONST int VAR_TYPE
Definition: TraCIConstants.h:672
ShapeContainer::registerHighlight
virtual void registerHighlight(const std::string &objectID, const int type, const std::string &polygonID)
register highlight of the specified type if the given id
Definition: ShapeContainer.cpp:229
SUMOPolygon
Definition: SUMOPolygon.h:46
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
libsumo::Polygon::exists
static bool exists(std::string polyID)
Checks if a polygon of the given name exists already in the simulation.
Definition: Polygon.cpp:294
MSNet::getPersonControl
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:810
libsumo::Polygon::setParameter
static void setParameter(const std::string &polygonID, const std::string &key, const std::string &value)
Definition: Polygon.cpp:237
libsumo::Polygon::setType
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:99
SUMOPolygon::setLineWidth
void setLineWidth(double lineWidth)
Definition: SUMOPolygon.h:112
libsumo::VariableWrapper::wrapInt
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
ParametrisedWrappingCommand
A wrapper for a Command function with parameter.
Definition: ParametrisedWrappingCommand.h:32
NamedObjectCont::get
T get(const std::string &id) const
Retrieves an item.
Definition: NamedObjectCont.h:98
libsumo::Polygon::getShape
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:69
Parameterised::setParameter
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
Definition: Parameterised.cpp:46
NamedRTree
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:63
libsumo::Polygon::getFilled
static bool getFilled(const std::string &polygonID)
Definition: Polygon.cpp:76
ShapeContainer::removePolygon
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
Definition: ShapeContainer.cpp:153
PolygonDynamics
Definition: PolygonDynamics.h:29
Shape::DEFAULT_ANGLE
static const double DEFAULT_ANGLE
Definition: Shape.h:46
ShapeContainer.h
ParametrisedWrappingCommand.h
libsumo::Polygon::makeWrapper
static std::shared_ptr< VariableWrapper > makeWrapper()
Definition: Polygon.cpp:267
MSEventControl.h
SUMOPolygon::getFill
bool getFill() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:89
ShapeContainer::getPolygons
const Polygons & getPolygons() const
Returns all polygons.
Definition: ShapeContainer.h:149
Shape::DEFAULT_IMG_FILE
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:47
SUMOPolygon.h
libsumo::Polygon::mySubscriptionResults
static SubscriptionResults mySubscriptionResults
Definition: Polygon.h:98
libsumo::Polygon::setLineWidth
static void setLineWidth(std::string polygonID, double lineWidth)
Definition: Polygon.cpp:200
libsumo::VAR_FILL
TRACI_CONST int VAR_FILL
Definition: TraCIConstants.h:693
MSVehicleControl.h
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:336
libsumo::SubscriptionResults
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:203
Shape::setShapeColor
void setShapeColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:129
SUMOPolygon::getLineWidth
double getLineWidth() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:96
Boundary::ymax
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136
ShapeContainer::addPolygonUpdateCommand
virtual void addPolygonUpdateCommand(std::string polyID, ParametrisedWrappingCommand< ShapeContainer, PolygonDynamics * > *cmd)
Register update command (for descheduling at removal)
Definition: ShapeContainer.cpp:280