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