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