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 /****************************************************************************/
Shape::setShapeType
void setShapeType(const std::string &type)
Sets a new type.
Definition: Shape.h:123
libsumo::POI::getIDList
static std::vector< std::string > getIDList()
Definition: POI.cpp:50
libsumo::VAR_HEIGHT
TRACI_CONST int VAR_HEIGHT
Definition: TraCIConstants.h:770
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:74
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:140
libsumo::VAR_POSITION
TRACI_CONST int VAR_POSITION
Definition: TraCIConstants.h:616
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:247
NamedObjectCont::insertIDs
void insertIDs(std::vector< std::string > &into) const
Definition: NamedObjectCont.h:124
libsumo::POI::getWidth
static double getWidth(const std::string &poiID)
Definition: POI.cpp:83
Shape::getShapeNaviDegree
double getShapeNaviDegree() const
Returns the angle of the Shape in navigational degrees.
Definition: Shape.h:97
libsumo::TraCIPosition
A 3D-position.
Definition: TraCIDefs.h:110
libsumo::POI::getPosition
static TraCIPosition getPosition(const std::string &poiID, const bool includeZ=false)
Definition: POI.cpp:77
MSNet::getShapeContainer
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:460
libsumo::VariableWrapper
Definition: Subscription.h:114
libsumo::VAR_COLOR
TRACI_CONST int VAR_COLOR
Definition: TraCIConstants.h:628
ShapeContainer::movePOI
virtual void movePOI(const std::string &id, const Position &pos)
Assigns a new position to the named PoI.
Definition: ShapeContainer.cpp:170
libsumo::Helper::makeRGBColor
static RGBColor makeRGBColor(const TraCIColor &color)
Definition: Helper.cpp:258
PointOfInterest::getHeight
double getHeight() const
Returns the image height of the POI.
Definition: PointOfInterest.h:95
libsumo::VAR_WIDTH
TRACI_CONST int VAR_WIDTH
Definition: TraCIConstants.h:664
libsumo::POI
Definition: POI.h:53
ShapeContainer
Storage for geometrical objects.
Definition: ShapeContainer.h:50
libsumo::ContextSubscriptionResults
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:205
Shape::setShapeImgFile
void setShapeImgFile(const std::string &imgFile)
Sets a new imgFile.
Definition: Shape.h:158
libsumo::POI::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: POI.cpp:276
Shape::getShapeImgFile
const std::string & getShapeImgFile() const
Returns the imgFile of the Shape.
Definition: Shape.h:104
libsumo::Helper::makeTraCIPosition
static TraCIPosition makeTraCIPosition(const Position &position, const bool includeZ=false)
Definition: Helper.cpp:264
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:136
PositionVector
A list of positions.
Definition: PositionVector.h:46
libsumo::POI::getHeight
static double getHeight(const std::string &poiID)
Definition: POI.cpp:89
libsumo::POI::getIDCount
static int getIDCount()
Definition: POI.cpp:59
libsumo::POI::getTree
static LIBSUMO_SUBSCRIPTION_API NamedRTree * getTree()
Returns a tree filled with PoI instances.
Definition: POI.cpp:251
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:270
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:71
libsumo
Definition: Edge.cpp:30
GeomHelper::makeRing
static PositionVector makeRing(const double radius1, const double radius2, const Position &center, unsigned int nPoints)
Definition: GeomHelper.cpp:255
libsumo::POI::setHeight
static void setHeight(const std::string &poiID, double height)
Definition: POI.cpp:139
libsumo::POI::setPosition
static void setPosition(const std::string &poiID, double x, double y)
Definition: POI.cpp:119
libsumo::POI::setAngle
static void setAngle(const std::string &poiID, double angle)
Definition: POI.cpp:145
Shape::DEFAULT_RELATIVEPATH
static const bool DEFAULT_RELATIVEPATH
Definition: Shape.h:49
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:101
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:64
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:157
TraCIConstants.h
libsumo::VAR_ANGLE
TRACI_CONST int VAR_ANGLE
Definition: TraCIConstants.h:622
NamedRTree::Insert
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:82
Shape::getShapeLayer
double getShapeLayer() const
Returns the layer of the Shape.
Definition: Shape.h:90
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:133
PointOfInterest::setWidth
void setWidth(double width)
set the image width of the POI
Definition: PointOfInterest.h:110
libsumo::POI::getType
static std::string getType(const std::string &poiID)
Definition: POI.cpp:65
libsumo::POI::setType
static void setType(const std::string &poiID, const std::string &setType)
Definition: POI.cpp:113
Helper.h
PointOfInterest::setHeight
void setHeight(double height)
set the image height of the POI
Definition: PointOfInterest.h:115
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
libsumo::Helper::makeTraCIPositionVector
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:224
libsumo::TRACI_ID_LIST
TRACI_CONST int TRACI_ID_LIST
Definition: TraCIConstants.h:496
libsumo::POI::getPoI
static PointOfInterest * getPoI(const std::string &id)
Definition: POI.cpp:241
Shape::getShapeType
const std::string & getShapeType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:76
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:51
libsumo::POI::getAngle
static double getAngle(const std::string &poiID)
Definition: POI.cpp:95
libsumo::ID_COUNT
TRACI_CONST int ID_COUNT
Definition: TraCIConstants.h:499
libsumo::TraCIException
Definition: TraCIDefs.h:90
libsumo::POI::setWidth
static void setWidth(const std::string &poiID, double width)
Definition: POI.cpp:133
ShapeContainer::getPOIs
const POIs & getPOIs() const
Returns all pois.
Definition: ShapeContainer.h:155
libsumo::POI::mySubscriptionResults
static SubscriptionResults mySubscriptionResults
Definition: POI.h:100
libsumo::POI::getColor
static TraCIColor getColor(const std::string &poiID)
Definition: POI.cpp:71
Polygon.h
libsumo::VAR_TYPE
TRACI_CONST int VAR_TYPE
Definition: TraCIConstants.h:670
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
PointOfInterest.h
libsumo::POI::setColor
static void setColor(const std::string &poiID, const TraCIColor &c)
Definition: POI.cpp:127
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
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:295
libsumo::POI::getParameter
static std::string getParameter(const std::string &poiID, const std::string &key)
Definition: POI.cpp:107
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:264
POI.h
libsumo::POI::setImageFile
static void setImageFile(const std::string &poiID, const std::string &imageFile)
Definition: POI.cpp:151
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:99
libsumo::VAR_POSITION3D
TRACI_CONST int VAR_POSITION3D
Definition: TraCIConstants.h:619
Parameterised::setParameter
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
Definition: Parameterised.cpp:45
NamedRTree
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:64
PointOfInterest::getWidth
double getWidth() const
Returns the image width of the POI.
Definition: PointOfInterest.h:90
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:146
config.h
ShapeContainer.h
Shape::setShapeNaviDegree
void setShapeNaviDegree(const double angle)
Sets a new angle in navigational degrees.
Definition: Shape.h:151
GeomHelper.h
PointOfInterest
A point-of-interest.
Definition: PointOfInterest.h:44
ShapeContainer::removePOI
virtual bool removePOI(const std::string &id)
Removes a PoI from the container.
Definition: ShapeContainer.cpp:164
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:694
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
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:176
libsumo::POI::setParameter
static void setParameter(const std::string &poiID, const std::string &key, const std::string &value)
Definition: POI.cpp:230
libsumo::SubscriptionResults
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:204
Shape::setShapeColor
void setShapeColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:130
libsumo::POI::myContextSubscriptionResults
static ContextSubscriptionResults myContextSubscriptionResults
Definition: POI.h:101
libsumo::POI::remove
static bool remove(const std::string &poiID, int layer=0)
Definition: POI.cpp:169