Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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-2025 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
29#include <microsim/MSNet.h>
31#include "Polygon.h"
32#include "POI.h"
33#include "Helper.h"
34
35
36namespace libsumo {
37// ===========================================================================
38// static member initializations
39// ===========================================================================
40SubscriptionResults POI::mySubscriptionResults;
41ContextSubscriptionResults POI::myContextSubscriptionResults;
42NamedRTree* POI::myTree(nullptr);
43
44
45// ===========================================================================
46// static member definitions
47// ===========================================================================
48std::vector<std::string>
49POI::getIDList() {
50 std::vector<std::string> ids;
52 return ids;
53}
54
55
56int
57POI::getIDCount() {
58 return (int)getIDList().size();
59}
60
61
62std::string
63POI::getType(const std::string& poiID) {
64 return getPoI(poiID)->getShapeType();
65}
66
67
68TraCIColor
69POI::getColor(const std::string& poiID) {
70 return Helper::makeTraCIColor(getPoI(poiID)->getShapeColor());
71}
72
73
74TraCIPosition
75POI::getPosition(const std::string& poiID, const bool includeZ) {
76 return Helper::makeTraCIPosition(*getPoI(poiID), includeZ);
77}
78
79
80double
81POI::getWidth(const std::string& poiID) {
82 return getPoI(poiID)->getWidth();
83}
84
85
86double
87POI::getHeight(const std::string& poiID) {
88 return getPoI(poiID)->getHeight();
89}
90
91
92double
93POI::getAngle(const std::string& poiID) {
94 return getPoI(poiID)->getShapeNaviDegree();
95}
96
97
98std::string
99POI::getImageFile(const std::string& poiID) {
100 return getPoI(poiID)->getShapeImgFile();
101}
102
103
104std::string
105POI::getParameter(const std::string& poiID, const std::string& key) {
106 return getPoI(poiID)->getParameter(key, "");
107}
108
109
111
112
113void
114POI::setType(const std::string& poiID, const std::string& poiType) {
115 getPoI(poiID)->setShapeType(poiType);
116}
117
118
119void
120POI::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
127void
128POI::setColor(const std::string& poiID, const TraCIColor& c) {
129 getPoI(poiID)->setShapeColor(Helper::makeRGBColor(c));
130}
131
132
133void
134POI::setWidth(const std::string& poiID, double width) {
135 getPoI(poiID)->setWidth(width);
136}
137
138
139void
140POI::setHeight(const std::string& poiID, double height) {
141 getPoI(poiID)->setHeight(height);
142}
143
144
145void
146POI::setAngle(const std::string& poiID, double angle) {
147 getPoI(poiID)->setShapeNaviDegree(angle);
148}
149
150
151void
152POI::setImageFile(const std::string& poiID, const std::string& imageFile) {
153 getPoI(poiID)->setShapeImgFile(imageFile);
154}
155
156
157bool
158POI::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, width, height);
164 if (ok && myTree != nullptr) {
165 PointOfInterest* p = shapeCont.getPOIs().get(poiID);
166 const float cmin[2] = {(float)p->x(), (float)p->y()};
167 const float cmax[2] = {(float)p->x(), (float)p->y()};
168 myTree->Insert(cmin, cmax, p);
169 }
170 return ok;
171}
172
173
174bool
175POI::remove(const std::string& poiID, int /* layer */) {
177 PointOfInterest* p = shapeCont.getPOIs().get(poiID);
178 if (p != nullptr && myTree != nullptr) {
179 const float cmin[2] = {(float)p->x(), (float)p->y()};
180 const float cmax[2] = {(float)p->x(), (float)p->y()};
181 myTree->Remove(cmin, cmax, p);
182 }
183 return shapeCont.removePOI(poiID);
184}
185
186
187void
188POI::highlight(const std::string& poiID, const TraCIColor& col, double size, const int alphaMax, const double duration, const int type) {
189 // NOTE: Code is duplicated in large parts in Vehicle.cpp
190 PointOfInterest* poi = getPoI(poiID);
191
192 // Center of the highlight circle
193 Position* center = dynamic_cast<Position*>(poi);
194 // Size of the highlight circle
195 if (size <= 0) {
196 size = sqrt(poi->getHeight() * poi->getHeight() + poi->getWidth() * poi->getWidth()) * 0.7;
197 }
198 // Make polygon shape
199 const unsigned int nPoints = 34;
200 const PositionVector circlePV = GeomHelper::makeRing(size, size + 1., *center, nPoints);
201 TraCIPositionVector circle = Helper::makeTraCIPositionVector(circlePV);
202
203#ifdef DEBUG_DYNAMIC_SHAPES
204 std::cout << SIMTIME << " Vehicle::highlight() for vehicle '" << vehicleID << "'\n"
205 << " circle: " << circlePV << std::endl;
206#endif
207
208 // Find a free polygon id
209 int i = 0;
210 std::string polyID = poi->getID() + "_hl" + toString(i);
211 while (Polygon::exists(polyID)) {
212 polyID = poi->getID() + "_hl" + toString(++i);
213 }
214 // Line width
215 double lw = 0.;
216 // Layer
217 double lyr = 0.;
218 if (MSNet::getInstance()->isGUINet()) {
219 lyr = poi->getShapeLayer();
220 lyr += (type + 1) / 257.;
221 }
222 // Make Polygon
223 Polygon::addHighlightPolygon(poiID, type, polyID, circle, col, true, "highlight", (int)lyr, lw);
224
225 // Animation time line
226 double maxAttack = 1.0; // maximal fade-in time
227 std::vector<double> timeSpan;
228 if (duration > 0.) {
229 timeSpan = {0, MIN2(maxAttack, duration / 3.), 2.*duration / 3., duration};
230 }
231 // Alpha time line
232 std::vector<double> alphaSpan;
233 if (alphaMax > 0.) {
234 alphaSpan = {0., (double) alphaMax, ((double) alphaMax) / 3., 0.};
235 }
236 // Attach dynamics
237 Polygon::addDynamics(polyID, "", timeSpan, alphaSpan, false, false);
238}
239
240
241void
242POI::setParameter(const std::string& poiID, const std::string& key, const std::string& value) {
243 PointOfInterest* p = getPoI(poiID);
244 p->setParameter(key, value);
245}
246
247
248
250
251
253POI::getPoI(const std::string& id) {
255 if (sumoPoi == nullptr) {
256 throw TraCIException("POI '" + id + "' is not known");
257 }
258 return sumoPoi;
259}
260
261
263POI::getTree() {
264 if (myTree == nullptr) {
265 myTree = new NamedRTree();
267 for (const auto& i : shapeCont.getPOIs()) {
268 const float cmin[2] = {(float)i.second->x(), (float)i.second->y()};
269 const float cmax[2] = {(float)i.second->x(), (float)i.second->y()};
270 myTree->Insert(cmin, cmax, i.second);
271 }
272 }
273 return myTree;
274}
275
276void
277POI::cleanup() {
278 delete myTree;
279 myTree = nullptr;
280}
281
282
283void
284POI::storeShape(const std::string& id, PositionVector& shape) {
285 shape.push_back(*getPoI(id));
286}
287
288
289std::shared_ptr<VariableWrapper>
290POI::makeWrapper() {
291 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
292}
293
294
295bool
296POI::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
297 switch (variable) {
298 case TRACI_ID_LIST:
299 return wrapper->wrapStringList(objID, variable, getIDList());
300 case ID_COUNT:
301 return wrapper->wrapInt(objID, variable, getIDCount());
302 case VAR_TYPE:
303 return wrapper->wrapString(objID, variable, getType(objID));
304 case VAR_COLOR:
305 return wrapper->wrapColor(objID, variable, getColor(objID));
306 case VAR_POSITION:
307 return wrapper->wrapPosition(objID, variable, getPosition(objID));
308 case VAR_POSITION3D:
309 return wrapper->wrapPosition(objID, variable, getPosition(objID, true));
310 case VAR_WIDTH:
311 return wrapper->wrapDouble(objID, variable, getWidth(objID));
312 case VAR_HEIGHT:
313 return wrapper->wrapDouble(objID, variable, getHeight(objID));
314 case VAR_ANGLE:
315 return wrapper->wrapDouble(objID, variable, getAngle(objID));
316 case VAR_IMAGEFILE:
317 return wrapper->wrapString(objID, variable, getImageFile(objID));
319 paramData->readUnsignedByte();
320 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
322 paramData->readUnsignedByte();
323 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
324 default:
325 return false;
326 }
327}
328
329
330}
331
332
333/****************************************************************************/
#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:77
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:124
static PositionVector makeRing(const double radius1, const double radius2, const Position &center, unsigned int nPoints)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition MSNet.h:506
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.
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:52
double y() const
Returns the y-position.
Definition Position.h:57
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.
const POIs & getPOIs() const
Returns all pois.
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, double width, double height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
double getShapeLayer() const
Returns the layer of the Shape.
Definition Shape.h:89
static TraCIPosition makeTraCIPosition(const Position &position, const bool includeZ=false)
Definition Helper.cpp:393
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition Helper.cpp:353
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition Helper.cpp:376
static RGBColor makeRGBColor(const TraCIColor &color)
Definition Helper.cpp:387
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:379
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:378
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