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.org/sumo
3// Copyright (C) 2017-2022 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 shapeCont.getPOIs().insertIDs(ids);
53 return ids;
54}
55
56
57int
58POI::getIDCount() {
59 return (int)getIDList().size();
60}
61
62
63std::string
64POI::getType(const std::string& poiID) {
65 return getPoI(poiID)->getShapeType();
66}
67
68
69TraCIColor
70POI::getColor(const std::string& poiID) {
71 return Helper::makeTraCIColor(getPoI(poiID)->getShapeColor());
72}
73
74
75TraCIPosition
76POI::getPosition(const std::string& poiID, const bool includeZ) {
77 return Helper::makeTraCIPosition(*getPoI(poiID), includeZ);
78}
79
80
81double
82POI::getWidth(const std::string& poiID) {
83 return getPoI(poiID)->getWidth();
84}
85
86
87double
88POI::getHeight(const std::string& poiID) {
89 return getPoI(poiID)->getHeight();
90}
91
92
93double
94POI::getAngle(const std::string& poiID) {
95 return getPoI(poiID)->getShapeNaviDegree();
96}
97
98
99std::string
100POI::getImageFile(const std::string& poiID) {
101 return getPoI(poiID)->getShapeImgFile();
102}
103
104
105std::string
106POI::getParameter(const std::string& poiID, const std::string& key) {
107 return getPoI(poiID)->getParameter(key, "");
108}
109
110
112
113
114void
115POI::setType(const std::string& poiID, const std::string& type) {
116 getPoI(poiID)->setShapeType(type);
117}
118
119
120void
121POI::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
128void
129POI::setColor(const std::string& poiID, const TraCIColor& c) {
130 getPoI(poiID)->setShapeColor(Helper::makeRGBColor(c));
131}
132
133
134void
135POI::setWidth(const std::string& poiID, double width) {
136 getPoI(poiID)->setWidth(width);
137}
138
139
140void
141POI::setHeight(const std::string& poiID, double height) {
142 getPoI(poiID)->setHeight(height);
143}
144
145
146void
147POI::setAngle(const std::string& poiID, double angle) {
148 getPoI(poiID)->setShapeNaviDegree(angle);
149}
150
151
152void
153POI::setImageFile(const std::string& poiID, const std::string& imageFile) {
154 getPoI(poiID)->setShapeImgFile(imageFile);
155}
156
157
158bool
159POI::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,
163 Position(x, y), false, "", 0, false, 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
179bool
180POI::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
192void
193POI::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);
206 TraCIPositionVector circle = Helper::makeTraCIPositionVector(circlePV);
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
246void
247POI::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
258POI::getPoI(const std::string& id) {
260 if (sumoPoi == nullptr) {
261 throw TraCIException("POI '" + id + "' is not known");
262 }
263 return sumoPoi;
264}
265
266
268POI::getTree() {
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
281void
282POI::cleanup() {
283 delete myTree;
284 myTree = nullptr;
285}
286
287
288void
289POI::storeShape(const std::string& id, PositionVector& shape) {
290 shape.push_back(*getPoI(id));
291}
292
293
294std::shared_ptr<VariableWrapper>
295POI::makeWrapper() {
296 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
297}
298
299
300bool
301POI::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
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));
324 paramData->readUnsignedByte();
325 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
327 paramData->readUnsignedByte();
328 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
329 default:
330 return false;
331 }
332}
333
334
335}
336
337
338/****************************************************************************/
#define SIMTIME
Definition: SUMOTime.h:61
T MIN2(T a, T b)
Definition: StdDefs.h:71
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:253
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:502
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: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.
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, 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.
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:374
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:334
static TraCIColor makeTraCIColor(const RGBColor &color)
Definition: Helper.cpp:357
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:368
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:301
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:300
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