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