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