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 double
76 Polygon::getLineWidth(const std::string& polygonID) {
77  return getPolygon(polygonID)->getLineWidth();
78 }
79 
81 Polygon::getColor(const std::string& polygonID) {
82  SUMOPolygon* p = getPolygon(polygonID);
84 }
85 
86 
87 std::string
88 Polygon::getParameter(const std::string& polygonID, const std::string& key) {
89  return getPolygon(polygonID)->getParameter(key, "");
90 }
91 
92 
93 void
94 Polygon::setType(const std::string& polygonID, const std::string& setType) {
95  SUMOPolygon* p = getPolygon(polygonID);
96  p->setShapeType(setType);
97 }
98 
99 
100 void
101 Polygon::setShape(const std::string& polygonID, const TraCIPositionVector& shape) {
102  PositionVector positionVector = Helper::makePositionVector(shape);
103  getPolygon(polygonID); // just to check whether it exists
105  shapeCont.reshapePolygon(polygonID, positionVector);
106 }
107 
108 
109 void
110 Polygon::setColor(const std::string& polygonID, const TraCIColor& c) {
112 }
113 
114 
115 void
116 Polygon::add(const std::string& polygonID, const TraCIPositionVector& shape, const TraCIColor& color, bool fill, double lineWidth, const std::string& polygonType, int layer) {
119  RGBColor col = Helper::makeRGBColor(color);
120  if (!shapeCont.addPolygon(polygonID, polygonType, col, (double)layer, Shape::DEFAULT_ANGLE, Shape::DEFAULT_IMG_FILE, Shape::DEFAULT_RELATIVEPATH, pShape, false, fill, lineWidth)) {
121  throw TraCIException("Could not add polygon '" + polygonID + "'");
122  }
123 }
124 
125 
126 void
127 Polygon::remove(const std::string& polygonID, int /* layer */) {
128  // !!! layer not used yet (shouldn't the id be enough?)
130  if (!shapeCont.removePolygon(polygonID)) {
131  throw TraCIException("Could not remove polygon '" + polygonID + "'");
132  }
133 }
134 
135 
136 void
137 Polygon::setFilled(std::string polygonID, bool filled) {
138  SUMOPolygon* p = getPolygon(polygonID);
139  p->setFill(filled);
140 }
141 
142 void
143 Polygon::setLineWidth(std::string polygonID, double lineWidth) {
144  SUMOPolygon* p = getPolygon(polygonID);
145  p->setLineWidth(lineWidth);
146 }
147 
148 
150 Polygon::getPolygon(const std::string& id) {
152  if (p == nullptr) {
153  throw TraCIException("Polygon '" + id + "' is not known");
154  }
155  return p;
156 }
157 
158 
159 void
160 Polygon::setParameter(const std::string& polygonID, const std::string& key, const std::string& value) {
161  SUMOPolygon* p = getPolygon(polygonID);
162  p->setParameter(key, value);
163 }
164 
165 
167 
168 
169 NamedRTree*
171  NamedRTree* t = new NamedRTree();
173  for (const auto& i : shapeCont.getPolygons()) {
174  Boundary b = i.second->getShape().getBoxBoundary();
175  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
176  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
177  t->Insert(cmin, cmax, i.second);
178  }
179  return t;
180 }
181 
182 
183 void
184 Polygon::storeShape(const std::string& id, PositionVector& shape) {
185  shape = getPolygon(id)->getShape();
186 }
187 
188 
189 std::shared_ptr<VariableWrapper>
191  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
192 }
193 
194 
195 bool
196 Polygon::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
197  switch (variable) {
198  case TRACI_ID_LIST:
199  return wrapper->wrapStringList(objID, variable, getIDList());
200  case ID_COUNT:
201  return wrapper->wrapInt(objID, variable, getIDCount());
202  case VAR_TYPE:
203  return wrapper->wrapString(objID, variable, getType(objID));
204  case VAR_COLOR:
205  return wrapper->wrapColor(objID, variable, getColor(objID));
206  case VAR_FILL:
207  return wrapper->wrapInt(objID, variable, getFilled(objID));
208  case VAR_WIDTH:
209  return wrapper->wrapDouble(objID, variable, getLineWidth(objID));
210  default:
211  return false;
212  }
213 }
214 
215 
216 }
217 
218 
219 /****************************************************************************/
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:110
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 void setLineWidth(std::string polygonID, double lineWidth)
Definition: Polygon.cpp:143
static std::string getParameter(const std::string &polygonID, const std::string &key)
Definition: Polygon.cpp:88
static PositionVector makePositionVector(const TraCIPositionVector &vector)
Definition: Helper.cpp:220
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:230
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:48
void setLineWidth(double lineWidth)
Definition: SUMOPolygon.h:112
void setShapeColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:130
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:184
static SubscriptionResults mySubscriptionResults
Definition: Polygon.h:87
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &color, bool fill=false, double lineWidth=1, const std::string &polygonType="", int layer=0)
Definition: Polygon.cpp:116
void setShapeType(const std::string &type)
Sets a new type.
Definition: Shape.h:123
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:137
#define VAR_COLOR
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:81
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
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.
void insertIDs(std::vector< std::string > &into) const
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:210
double getLineWidth() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:96
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:76
static std::string getType(const std::string &polygonID)
Definition: Polygon.cpp:58
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:241
#define TRACI_ID_LIST
virtual bool removePolygon(const std::string &id)
Removes a polygon from the container.
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:439
A list of positions.
static LIBSUMO_SUBSCRIPTION_API NamedRTree * getTree()
Returns a tree filled with polygon instances.
Definition: Polygon.cpp:170
const RGBColor & getShapeColor() const
Returns the color of the Shape.
Definition: Shape.h:83
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:49
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:127
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:64
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:101
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:89
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:190
Definition: Edge.cpp:30
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
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:196
static TraCIColor getColor(const std::string &polygonID)
Definition: Polygon.cpp:81
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:150
#define VAR_FILL
#define ID_COUNT
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
static void setParameter(const std::string &polygonID, const std::string &key, const std::string &value)
Definition: Polygon.cpp:160
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
static ContextSubscriptionResults myContextSubscriptionResults
Definition: Polygon.h:88
static int getIDCount()
Definition: Polygon.cpp:52
A list of positions.
void setFill(bool fill)
Sets whether the polygon shall be filled.
Definition: SUMOPolygon.h:108
static const double DEFAULT_ANGLE
Definition: Shape.h:47
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:94
static double getLineWidth(const std::string &polygonID)
Definition: Polygon.cpp:76
#define VAR_WIDTH