Eclipse SUMO - Simulation of Urban MObility
POI.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 /****************************************************************************/
22 // C++ TraCI client API implementation
23 /****************************************************************************/
24 #include <config.h>
25 
28 #include <utils/geom/GeomHelper.h>
29 #include <microsim/MSNet.h>
30 #include <libsumo/TraCIConstants.h>
31 #include "Polygon.h"
32 #include "POI.h"
33 #include "Helper.h"
34 
35 
36 namespace libsumo {
37 // ===========================================================================
38 // static member initializations
39 // ===========================================================================
42 NamedRTree* POI::myTree(nullptr);
43 
44 
45 // ===========================================================================
46 // static member definitions
47 // ===========================================================================
48 std::vector<std::string>
49 POI::getIDList() {
50  std::vector<std::string> ids;
52  shapeCont.getPOIs().insertIDs(ids);
53  return ids;
54 }
55 
56 
57 int
58 POI::getIDCount() {
59  return (int)getIDList().size();
60 }
61 
62 
63 std::string
64 POI::getType(const std::string& poiID) {
65  return getPoI(poiID)->getShapeType();
66 }
67 
68 
70 POI::getColor(const std::string& poiID) {
71  return Helper::makeTraCIColor(getPoI(poiID)->getShapeColor());
72 }
73 
74 
76 POI::getPosition(const std::string& poiID, const bool includeZ) {
77  return Helper::makeTraCIPosition(*getPoI(poiID), includeZ);
78 }
79 
80 
81 double
82 POI::getWidth(const std::string& poiID) {
83  return getPoI(poiID)->getWidth();
84 }
85 
86 
87 double
88 POI::getHeight(const std::string& poiID) {
89  return getPoI(poiID)->getHeight();
90 }
91 
92 
93 double
94 POI::getAngle(const std::string& poiID) {
95  return getPoI(poiID)->getShapeNaviDegree();
96 }
97 
98 
99 std::string
100 POI::getImageFile(const std::string& poiID) {
101  return getPoI(poiID)->getShapeImgFile();
102 }
103 
104 
105 std::string
106 POI::getParameter(const std::string& poiID, const std::string& key) {
107  return getPoI(poiID)->getParameter(key, "");
108 }
109 
110 
112 
113 
114 void
115 POI::setType(const std::string& poiID, const std::string& type) {
116  getPoI(poiID)->setShapeType(type);
117 }
118 
119 
120 void
121 POI::setPosition(const std::string& poiID, double x, double y) {
122  // try to retrieve so that the correct error is generated for unknown poiIDs
123  getPoI(poiID);
125 }
126 
127 
128 void
129 POI::setColor(const std::string& poiID, const TraCIColor& c) {
131 }
132 
133 
134 void
135 POI::setWidth(const std::string& poiID, double width) {
136  getPoI(poiID)->setWidth(width);
137 }
138 
139 
140 void
141 POI::setHeight(const std::string& poiID, double height) {
142  getPoI(poiID)->setHeight(height);
143 }
144 
145 
146 void
147 POI::setAngle(const std::string& poiID, double angle) {
148  getPoI(poiID)->setShapeNaviDegree(angle);
149 }
150 
151 
152 void
153 POI::setImageFile(const std::string& poiID, const std::string& imageFile) {
154  getPoI(poiID)->setShapeImgFile(imageFile);
155 }
156 
157 
158 bool
159 POI::add(const std::string& poiID, double x, double y, const TraCIColor& color, const std::string& poiType, int layer, const std::string& imgFile, double width, double height, double angle) {
161  bool ok = shapeCont.addPOI(poiID, poiType,
162  Helper::makeRGBColor(color),
163  Position(x, y), false, "", 0, 0, (double)layer,
164  angle,
165  imgFile,
167  width,
168  height);
169  if (ok && myTree != nullptr) {
170  PointOfInterest* p = shapeCont.getPOIs().get(poiID);
171  const float cmin[2] = {(float)p->x(), (float)p->y()};
172  const float cmax[2] = {(float)p->x(), (float)p->y()};
173  myTree->Insert(cmin, cmax, p);
174  }
175  return ok;
176 }
177 
178 
179 bool
180 POI::remove(const std::string& poiID, int /* layer */) {
182  PointOfInterest* p = shapeCont.getPOIs().get(poiID);
183  if (p != nullptr && myTree != nullptr) {
184  const float cmin[2] = {(float)p->x(), (float)p->y()};
185  const float cmax[2] = {(float)p->x(), (float)p->y()};
186  myTree->Remove(cmin, cmax, p);
187  }
188  return shapeCont.removePOI(poiID);
189 }
190 
191 
192 void
193 POI::highlight(const std::string& poiID, const TraCIColor& col, double size, const int alphaMax, const double duration, const int type) {
194  // NOTE: Code is duplicated in large parts in Vehicle.cpp
195  PointOfInterest* poi = getPoI(poiID);
196 
197  // Center of the highlight circle
198  Position* center = dynamic_cast<Position*>(poi);
199  // Size of the highlight circle
200  if (size <= 0) {
201  size = sqrt(poi->getHeight() * poi->getHeight() + poi->getWidth() * poi->getWidth()) * 0.7;
202  }
203  // Make polygon shape
204  const unsigned int nPoints = 34;
205  const PositionVector circlePV = GeomHelper::makeRing(size, size + 1., *center, nPoints);
207 
208 #ifdef DEBUG_DYNAMIC_SHAPES
209  std::cout << SIMTIME << " Vehicle::highlight() for vehicle '" << vehicleID << "'\n"
210  << " circle: " << circlePV << std::endl;
211 #endif
212 
213  // Find a free polygon id
214  int i = 0;
215  std::string polyID = poi->getID() + "_hl" + toString(i);
216  while (Polygon::exists(polyID)) {
217  polyID = poi->getID() + "_hl" + toString(++i);
218  }
219  // Line width
220  double lw = 0.;
221  // Layer
222  double lyr = 0.;
223  if (MSNet::getInstance()->isGUINet()) {
224  lyr = poi->getShapeLayer();
225  lyr += (type + 1) / 257.;
226  }
227  // Make Polygon
228  Polygon::addHighlightPolygon(poiID, type, polyID, circle, col, true, "highlight", (int)lyr, lw);
229 
230  // Animation time line
231  double maxAttack = 1.0; // maximal fade-in time
232  std::vector<double> timeSpan;
233  if (duration > 0.) {
234  timeSpan = {0, MIN2(maxAttack, duration / 3.), 2.*duration / 3., duration};
235  }
236  // Alpha time line
237  std::vector<double> alphaSpan;
238  if (alphaMax > 0.) {
239  alphaSpan = {0., (double) alphaMax, ((double) alphaMax) / 3., 0.};
240  }
241  // Attach dynamics
242  Polygon::addDynamics(polyID, "", timeSpan, alphaSpan, false, false);
243 }
244 
245 
246 void
247 POI::setParameter(const std::string& poiID, const std::string& key, const std::string& value) {
248  PointOfInterest* p = getPoI(poiID);
249  p->setParameter(key, value);
250 }
251 
252 
253 
255 
256 
258 POI::getPoI(const std::string& id) {
260  if (sumoPoi == nullptr) {
261  throw TraCIException("POI '" + id + "' is not known");
262  }
263  return sumoPoi;
264 }
265 
266 
267 NamedRTree*
269  if (myTree == nullptr) {
270  myTree = new NamedRTree();
272  for (const auto& i : shapeCont.getPOIs()) {
273  const float cmin[2] = {(float)i.second->x(), (float)i.second->y()};
274  const float cmax[2] = {(float)i.second->x(), (float)i.second->y()};
275  myTree->Insert(cmin, cmax, i.second);
276  }
277  }
278  return myTree;
279 }
280 
281 void
283  delete myTree;
284  myTree = nullptr;
285 }
286 
287 
288 void
289 POI::storeShape(const std::string& id, PositionVector& shape) {
290  shape.push_back(*getPoI(id));
291 }
292 
293 
294 std::shared_ptr<VariableWrapper>
296  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
297 }
298 
299 
300 bool
301 POI::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
302  switch (variable) {
303  case TRACI_ID_LIST:
304  return wrapper->wrapStringList(objID, variable, getIDList());
305  case ID_COUNT:
306  return wrapper->wrapInt(objID, variable, getIDCount());
307  case VAR_TYPE:
308  return wrapper->wrapString(objID, variable, getType(objID));
309  case VAR_COLOR:
310  return wrapper->wrapColor(objID, variable, getColor(objID));
311  case VAR_POSITION:
312  return wrapper->wrapPosition(objID, variable, getPosition(objID));
313  case VAR_POSITION3D:
314  return wrapper->wrapPosition(objID, variable, getPosition(objID, true));
315  case VAR_WIDTH:
316  return wrapper->wrapDouble(objID, variable, getWidth(objID));
317  case VAR_HEIGHT:
318  return wrapper->wrapDouble(objID, variable, getHeight(objID));
319  case VAR_ANGLE:
320  return wrapper->wrapDouble(objID, variable, getAngle(objID));
321  case VAR_IMAGEFILE:
322  return wrapper->wrapString(objID, variable, getImageFile(objID));
323  default:
324  return false;
325  }
326 }
327 
328 
329 }
330 
331 
332 /****************************************************************************/
static void setAngle(const std::string &poiID, double angle)
Definition: POI.cpp:147
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
static void highlight(const std::string &poiID, const TraCIColor &col=TraCIColor(255, 0, 0, 255), double size=-1, const int alphaMax=-1, const double duration=-1, const int type=0)
Definition: POI.cpp:193
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:371
static NamedRTree * getTree()
Returns a tree filled with PoI instances.
Definition: POI.cpp:268
const std::string & getShapeImgFile() const
Returns the imgFile of the Shape.
Definition: Shape.h:101
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:104
TRACI_CONST int VAR_COLOR
static bool remove(const std::string &poiID, int layer=0)
Definition: POI.cpp:180
TRACI_CONST int VAR_WIDTH
static void setColor(const std::string &poiID, const TraCIColor &color)
Definition: POI.cpp:129
void setShapeColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:127
static TraCIPosition makeTraCIPosition(const Position &position, const bool includeZ=false)
Definition: Helper.cpp:388
void setShapeType(const std::string &type)
Sets a new type.
Definition: Shape.h:120
double y() const
Returns the y-position.
Definition: Position.h:59
T get(const std::string &id) const
Retrieves an item.
static PositionVector makeRing(const double radius1, const double radius2, const Position &center, unsigned int nPoints)
Definition: GeomHelper.cpp:253
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
static void setHeight(const std::string &poiID, double height)
Definition: POI.cpp:141
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:60
double x() const
Returns the x-position.
Definition: Position.h:54
TRACI_CONST int VAR_POSITION
static void setWidth(const std::string &poiID, double width)
Definition: POI.cpp:135
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251
Storage for geometrical objects.
TRACI_CONST int VAR_ANGLE
static NamedRTree * myTree
Definition: POI.h:98
const std::string & getID() const
Returns the id.
Definition: Named.h:73
double getHeight() const
Returns the image height of the POI.
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
static double getHeight(const std::string &poiID)
Definition: POI.cpp:88
static double getAngle(const std::string &poiID)
Definition: POI.cpp:94
void insertIDs(std::vector< std::string > &into) const
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:348
static std::shared_ptr< VariableWrapper > makeWrapper()
Definition: POI.cpp:295
#define SIMTIME
Definition: SUMOTime.h:60
const std::string & getShapeType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:73
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
void setShapeImgFile(const std::string &imgFile)
Sets a new imgFile.
Definition: Shape.h:155
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 double getWidth(const std::string &poiID)
Definition: POI.cpp:82
virtual void movePOI(const std::string &id, const Position &pos)
Assigns a new position to the named PoI.
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
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:494
static std::string getImageFile(const std::string &poiID)
Definition: POI.cpp:100
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
virtual bool removePOI(const std::string &id)
Removes a PoI from the container.
A list of positions.
static bool add(const std::string &poiID, double x, double y, const TraCIColor &color, const std::string &poiType="", int layer=0, const std::string &imgFile=Shape::DEFAULT_IMG_FILE, double width=Shape::DEFAULT_IMG_WIDTH, double height=Shape::DEFAULT_IMG_HEIGHT, double angle=Shape::DEFAULT_ANGLE)
Definition: POI.cpp:159
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: POI.cpp:301
static void setImageFile(const std::string &poiID, const std::string &imageFile)
Definition: POI.cpp:153
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:46
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, const Position &pos, bool geo, const std::string &lane, double posOverLane, double posLat, double layer, double angle, const std::string &imgFile, bool relativePath, double width, double height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
T MIN2(T a, T b)
Definition: StdDefs.h:73
static SubscriptionResults mySubscriptionResults
Definition: POI.h:96
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
LIBSUMO_ID_PARAMETER_API static LIBSUMO_SUBSCRIPTION_API void setType(const std::string &poiID, const std::string &setType)
Definition: POI.cpp:115
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:57
TRACI_CONST int VAR_POSITION3D
double getShapeLayer() const
Returns the layer of the Shape.
Definition: Shape.h:87
double getWidth() const
Returns the image width of the POI.
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
virtual bool wrapPosition(const std::string &objID, const int variable, const TraCIPosition &value)=0
virtual void setShapeNaviDegree(const double angle)
Sets a new angle in navigational degrees.
Definition: Shape.h:148
TRACI_CONST int VAR_TYPE
void setHeight(double height)
set the image height of the POI
virtual bool wrapColor(const std::string &objID, const int variable, const TraCIColor &value)=0
void setWidth(double width)
set the image width of the POI
static ContextSubscriptionResults myContextSubscriptionResults
Definition: POI.h:97
static TraCIPosition getPosition(const std::string &poiID, const bool includeZ=false)
Definition: POI.cpp:76
TRACI_CONST int VAR_IMAGEFILE
static std::string getType(const std::string &poiID)
Definition: POI.cpp:64
static PointOfInterest * getPoI(const std::string &id)
Definition: POI.cpp:258
TRACI_CONST int VAR_HEIGHT
A point-of-interest.
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 bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
A 3D-position.
Definition: TraCIDefs.h:141
static void cleanup()
Definition: POI.cpp:282
static TraCIColor getColor(const std::string &poiID)
Definition: POI.cpp:70
static void storeShape(const std::string &id, PositionVector &shape)
Saves the shape of the requested object in the given container.
Definition: POI.cpp:289
A list of positions.
static void setPosition(const std::string &poiID, double x, double y)
Definition: POI.cpp:121
double getShapeNaviDegree() const
Returns the angle of the Shape in navigational degrees.
Definition: Shape.h:94
const POIs & getPOIs() const
Returns all pois.