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-2018 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>
26 
27 #include "Polygon.h"
28 #include "Helper.h"
29 
30 
31 namespace libsumo {
32 // ===========================================================================
33 // static member initializations
34 // ===========================================================================
37 
38 
39 // ===========================================================================
40 // static member definitions
41 // ===========================================================================
42 std::vector<std::string>
44  std::vector<std::string> ids;
46  shapeCont.getPolygons().insertIDs(ids);
47  return ids;
48 }
49 
50 
51 int
53  return (int)getIDList().size();
54 }
55 
56 
57 std::string
58 Polygon::getType(const std::string& polygonID) {
59  return getPolygon(polygonID)->getShapeType();
60 }
61 
62 
64 Polygon::getShape(const std::string& polygonID) {
65  SUMOPolygon* p = getPolygon(polygonID);
67 }
68 
69 
70 bool
71 Polygon::getFilled(const std::string& polygonID) {
72  return getPolygon(polygonID)->getFill();
73 }
74 
75 
77 Polygon::getColor(const std::string& polygonID) {
78  SUMOPolygon* p = getPolygon(polygonID);
80 }
81 
82 
83 std::string
84 Polygon::getParameter(const std::string& polygonID, const std::string& paramName) {
85  return getPolygon(polygonID)->getParameter(paramName, "");
86 }
87 
88 
89 void
90 Polygon::setType(const std::string& polygonID, const std::string& setType) {
91  SUMOPolygon* p = getPolygon(polygonID);
92  p->setShapeType(setType);
93 }
94 
95 
96 void
97 Polygon::setShape(const std::string& polygonID, const TraCIPositionVector& shape) {
98  PositionVector positionVector = Helper::makePositionVector(shape);
99  getPolygon(polygonID); // just to check whether it exists
101  shapeCont.reshapePolygon(polygonID, positionVector);
102 }
103 
104 
105 void
106 Polygon::setColor(const std::string& polygonID, const TraCIColor& c) {
108 }
109 
110 
111 void
112 Polygon::add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& color, bool fill, const std::string& polygonType, int layer) {
115  RGBColor col = Helper::makeRGBColor(color);
116  if (!shapeCont.addPolygon(polygonID, polygonType, col, (double)layer, Shape::DEFAULT_ANGLE, Shape::DEFAULT_IMG_FILE, Shape::DEFAULT_RELATIVEPATH, pShape, false, fill)) {
117  throw TraCIException("Could not add polygon '" + polygonID + "'");
118  }
119 }
120 
121 
122 void
123 Polygon::remove(const std::string& polygonID, int /* layer */) {
124  // !!! layer not used yet (shouldn't the id be enough?)
126  if (!shapeCont.removePolygon(polygonID)) {
127  throw TraCIException("Could not remove polygon '" + polygonID + "'");
128  }
129 }
130 
131 
132 void
133 Polygon::setFilled(std::string polygonID, bool filled) {
134  SUMOPolygon* p = getPolygon(polygonID);
135  p->setFill(filled);
136 }
137 
138 
140 Polygon::getPolygon(const std::string& id) {
142  if (p == 0) {
143  throw TraCIException("Polygon '" + id + "' is not known");
144  }
145  return p;
146 }
147 
148 
149 void
150 Polygon::setParameter(std::string& id, std::string& name, std::string& value) {
151  SUMOPolygon* p = getPolygon(id);
152  p->setParameter(name, value);
153 }
154 
155 
157 
158 
159 NamedRTree*
161  NamedRTree* t = new NamedRTree();
163  for (const auto& i : shapeCont.getPolygons()) {
164  Boundary b = i.second->getShape().getBoxBoundary();
165  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
166  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
167  t->Insert(cmin, cmax, i.second);
168  }
169  return t;
170 }
171 
172 
173 void
174 Polygon::storeShape(const std::string& id, PositionVector& shape) {
175  shape = getPolygon(id)->getShape();
176 }
177 
178 
179 std::shared_ptr<VariableWrapper>
181  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
182 }
183 
184 
185 bool
186 Polygon::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
187  switch (variable) {
188  case ID_LIST:
189  return wrapper->wrapStringList(objID, variable, getIDList());
190  case ID_COUNT:
191  return wrapper->wrapInt(objID, variable, getIDCount());
192  case VAR_TYPE:
193  return wrapper->wrapString(objID, variable, getType(objID));
194  case VAR_COLOR:
195  return wrapper->wrapColor(objID, variable, getColor(objID));
196  case VAR_FILL:
197  return wrapper->wrapInt(objID, variable, getFilled(objID));
198  default:
199  return false;
200  }
201 }
202 
203 
204 }
205 
206 
207 /****************************************************************************/
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:106
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:83
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:199
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition: Helper.cpp:198
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:208
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:47
void setShapeColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:129
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:174
static SubscriptionResults mySubscriptionResults
Definition: Polygon.h:85
void setShapeType(const std::string &type)
Sets a new type.
Definition: Shape.h:122
T get(const std::string &id) const
Retrieves an item.
#define VAR_TYPE
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:65
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:133
#define VAR_COLOR
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:80
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:200
Storage for geometrical objects.
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &color, bool fill=false, const std::string &polygonType="", int layer=0)
Definition: Polygon.cpp:112
void insertIDs(std::vector< std::string > &into) const
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:188
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
const std::string & getShapeType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:75
static std::string getType(const std::string &polygonID)
Definition: Polygon.cpp:58
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:219
virtual bool removePolygon(const std::string &id)
Removes a polygon from the container.
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:432
A list of positions.
static LIBSUMO_SUBSCRIPTION_API NamedRTree * getTree()
Returns a tree filled with polygon instances.
Definition: Polygon.cpp:160
const RGBColor & getShapeColor() const
Returns the color of the Shape.
Definition: Shape.h:82
static std::string getParameter(const std::string &polygonID, const std::string &paramName)
Definition: Polygon.cpp:84
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:48
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:123
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:64
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:97
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:52
bool getFill() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:88
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:180
Definition: Edge.cpp:30
static void setParameter(std::string &name, std::string &value, std::string &string)
Definition: Polygon.cpp:150
static std::vector< std::string > getIDList()
Definition: Polygon.cpp:43
virtual bool wrapColor(const std::string &objID, const int variable, const TraCIColor &value)=0
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Polygon.cpp:186
static TraCIColor getColor(const std::string &polygonID)
Definition: Polygon.cpp:77
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static bool getFilled(const std::string &polygonID)
Definition: Polygon.cpp:71
static SUMOPolygon * getPolygon(const std::string &id)
Definition: Polygon.cpp:140
#define VAR_FILL
#define ID_COUNT
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:137
#define ID_LIST
static ContextSubscriptionResults myContextSubscriptionResults
Definition: Polygon.h:86
static int getIDCount()
Definition: Polygon.cpp:52
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, bool ignorePruning=false)
Builds a polygon using the given values and adds it to the container.
A list of positions.
void setFill(bool fill)
Sets whether the polygon shall be filled.
Definition: SUMOPolygon.h:100
static const double DEFAULT_ANGLE
Definition: Shape.h:46
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:90