Eclipse SUMO - Simulation of Urban MObility
ShapeContainer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2002-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 /****************************************************************************/
21 // Storage for geometrical objects, sorted by the layers they are in
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <fstream>
26 #include <stdlib.h>
27 #include <iostream>
28 #include <utility>
29 #include <string>
30 #include <cmath>
34 #include <utils/common/ToString.h>
35 #include <utils/common/StdDefs.h>
37 #include "PolygonDynamics.h"
38 #include "ShapeContainer.h"
39 
40 
41 // Debug defines
42 //#define DEBUG_DYNAMIC_SHAPES
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48 
50  for (auto& p : myPolygonUpdateCommands) {
51  p.second->deschedule();
52  }
54 
55  for (auto& p : myPolygonDynamics) {
56  delete p.second;
57  }
58  myPolygonDynamics.clear();
59 
60 }
61 
62 bool
63 ShapeContainer::addPolygon(const std::string& id, const std::string& type,
64  const RGBColor& color, double layer,
65  double angle, const std::string& imgFile, bool relativePath,
66  const PositionVector& shape, bool geo, bool fill, double lineWidth, bool ignorePruning,
67  const std::string& name) {
68  return add(new SUMOPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath, name), ignorePruning);
69 }
70 
71 
74  std::string polyID,
75  SUMOTrafficObject* trackedObject,
76  const std::vector<double>& timeSpan,
77  const std::vector<double>& alphaSpan,
78  bool looped,
79  bool rotate) {
80 
81 #ifdef DEBUG_DYNAMIC_SHAPES
82  std::cout << simtime << " ShapeContainer::addPolygonDynamics() called for polygon '" << polyID << "'" << std::endl;
83 #endif
84 
85  SUMOPolygon* p = myPolygons.get(polyID);
86  if (p == nullptr) {
87 #ifdef DEBUG_DYNAMIC_SHAPES
88  std::cout << " polygon '" << polyID << "' doesn't exist!" << std::endl;
89 #endif
90  return nullptr;
91  }
92  // remove eventually existent previously
93  removePolygonDynamics(polyID);
94 
95  // Add new dynamics
96  PolygonDynamics* pd = new PolygonDynamics(simtime, p, trackedObject, timeSpan, alphaSpan, looped, rotate);
97  myPolygonDynamics.insert(std::make_pair(polyID, pd));
98 
99  // Add tracking information
100  if (trackedObject != nullptr) {
101  auto i = myTrackingPolygons.find(pd->getTrackedObjectID());
102  if (i == myTrackingPolygons.end()) {
103  myTrackingPolygons.insert(std::make_pair(pd->getTrackedObjectID(), std::set<const SUMOPolygon*>({p})));
104  } else {
105  i->second.insert(p);
106  }
107  }
108  return pd;
109 }
110 
111 
112 bool
113 ShapeContainer::removePolygonDynamics(const std::string& polyID) {
114  SUMOPolygon* p = myPolygons.get(polyID);
115  if (p == nullptr) {
116  return false;
117  }
118  auto d = myPolygonDynamics.find(polyID);
119  if (d != myPolygonDynamics.end()) {
120 #ifdef DEBUG_DYNAMIC_SHAPES
121  std::cout << " Removing dynamics of polygon '" << polyID << "'" << std::endl;
122 #endif
123  const std::string& trackedObjID = d->second->getTrackedObjectID();
124  if (trackedObjID != "") {
125  // Remove tracking information
126  auto i = myTrackingPolygons.find(trackedObjID);
127  assert(i != myTrackingPolygons.end());
128  assert(i->second.find(p) != i->second.end());
129  i->second.erase(p);
130  // Remove highlighting information
131  clearHighlights(trackedObjID, p);
132  }
133  delete d->second;
134  myPolygonDynamics.erase(d);
135  // Clear existing polygon dynamics commands before adding new dynamics
136  cleanupPolygonDynamics(polyID);
137  return true;
138  } else {
139  return false;
140  }
141 }
142 
143 
144 bool
145 ShapeContainer::addPOI(const std::string& id, const std::string& type, const RGBColor& color, const Position& pos, bool geo,
146  const std::string& lane, double posOverLane, bool friendlyPos, double posLat, const std::string& icon, double layer,
147  double angle, const std::string& imgFile, bool relativePath, double width, double height, bool ignorePruning) {
148  return add(new PointOfInterest(id, type, color, pos, geo, lane, posOverLane, friendlyPos, posLat, icon, layer, angle, imgFile, relativePath, width, height), ignorePruning);
149 }
150 
151 
152 bool
153 ShapeContainer::removePolygon(const std::string& id, bool /* useLock */) {
154 #ifdef DEBUG_DYNAMIC_SHAPES
155  std::cout << "ShapeContainer: Removing Polygon '" << id << "'" << std::endl;
156 #endif
158  return myPolygons.remove(id);
159 }
160 
161 
162 bool
163 ShapeContainer::removePOI(const std::string& id) {
164  return myPOIs.remove(id);
165 }
166 
167 
168 void
169 ShapeContainer::movePOI(const std::string& id, const Position& pos) {
170  PointOfInterest* p = myPOIs.get(id);
171  if (p != nullptr) {
172  static_cast<Position*>(p)->set(pos);
173  }
174 }
175 
176 
177 void
178 ShapeContainer::reshapePolygon(const std::string& id, const PositionVector& shape) {
179  SUMOPolygon* p = myPolygons.get(id);
180  if (p != nullptr) {
181  p->setShape(shape);
182  }
183 }
184 
185 
186 bool
187 ShapeContainer::add(SUMOPolygon* poly, bool /* ignorePruning */) {
188  if (!myPolygons.add(poly->getID(), poly)) {
189  delete poly;
190  return false;
191  }
192  return true;
193 }
194 
195 
196 bool
197 ShapeContainer::add(PointOfInterest* poi, bool /* ignorePruning */) {
198  if (!myPOIs.add(poi->getID(), poi)) {
199  delete poi;
200  return false;
201  }
202  return true;
203 }
204 
205 void
207  for (auto& item : myPolygonUpdateCommands) {
208  item.second->deschedule();
209  }
210  myPolygonUpdateCommands.clear();
211 }
212 
213 void
215  auto j = myPolygonUpdateCommands.find(id);
216  if (j != myPolygonUpdateCommands.end()) {
217  j->second->deschedule();
218  myPolygonUpdateCommands.erase(j);
219  }
220 }
221 
222 
223 SUMOTime
225  SUMOTime next = pd->update(t);
226  if (next == 0) {
227  // Dynamics have expired => remove polygon
228  myPolygonUpdateCommands[pd->getPolygonID()]->deschedule();
229  // Don't aquire lock (in GUI case GUIShapeContainer::polygonDynamicsUpdate() does this)
230  removePolygon(pd->getPolygonID(), false);
231  }
232  return next;
233 }
234 
235 void
236 ShapeContainer::registerHighlight(const std::string& objectID, const int type, const std::string& polygonID) {
237  std::string toRemove = "";
238  clearHighlight(objectID, type, toRemove);
239  if (toRemove != "") {
240  removePolygon(toRemove);
241  }
242  auto i = myHighlightPolygons.find(objectID);
243  if (i == myHighlightPolygons.end()) {
244  myHighlightPolygons.insert(std::make_pair(objectID, std::map<int, std::string>({std::make_pair(type, polygonID)})));
245  } else {
246  i->second.insert(std::make_pair(type, polygonID));
247  }
248  myHighlightedObjects.insert(std::make_pair(polygonID, objectID));
249 }
250 
251 void
252 ShapeContainer::clearHighlight(const std::string& objectID, const int type, std::string& toRemove) {
253  auto i = myHighlightPolygons.find(objectID);
254  if (i != myHighlightPolygons.end()) {
255  auto j = i->second.find(type);
256  if (j != i->second.end()) {
257  toRemove = j->second;
258  myHighlightedObjects.erase(toRemove);
259  i->second.erase(j);
260  if (i->second.empty()) {
261  myHighlightPolygons.erase(i);
262  }
263  }
264  }
265 }
266 
267 void
268 ShapeContainer::clearHighlights(const std::string& objectID, SUMOPolygon* p) {
269  auto i = myHighlightPolygons.find(objectID);
270  if (i != myHighlightPolygons.end()) {
271  auto j = i->second.begin();
272  while (j != i->second.end()) {
273  if (j->second == p->getID()) {
274  i->second.erase(j);
275  break;
276  } else {
277  ++j;
278  }
279  }
280  if (i->second.empty()) {
281  myHighlightPolygons.erase(i);
282  }
283  }
284 }
285 
286 void
288  myPolygonUpdateCommands.insert(std::make_pair(polyID, cmd));
289 }
290 
291 
292 void
293 ShapeContainer::removeTrackers(std::string objectID) {
294  auto i = myTrackingPolygons.find(objectID);
295  if (i != myTrackingPolygons.end()) {
296 #ifdef DEBUG_DYNAMIC_SHAPES
297  std::cout << " Removing tracking polygons for object '" << objectID << "'" << std::endl;
298 #endif
299  while (!i->second.empty()) {
300  removePolygon((*i->second.begin())->getID());
301  }
302  myTrackingPolygons.erase(i);
303  }
304 }
305 
306 
307 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
const std::string & getID() const
Returns the id.
Definition: Named.h:74
T get(const std::string &id) const
Retrieves an item.
bool remove(const std::string &id, const bool del=true)
Removes an item.
bool add(const std::string &id, T item)
Adds an item.
A wrapper for a Command function with parameter.
A point-of-interest.
const std::string & getPolygonID() const
const std::string & getTrackedObjectID() const
SUMOTime update(SUMOTime t)
Updates the polygon according to its timeSpan and follows the tracked object.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
virtual void setShape(const PositionVector &shape)
Sets the shape of the polygon.
Definition: SUMOPolygon.cpp:87
Representation of a vehicle, person, or container.
virtual bool removePolygonDynamics(const std::string &polyID)
Remove dynamics (animation / tracking) for the given polygon.
virtual void clearHighlight(const std::string &objectID, const int type, std::string &toRemove)
virtual void cleanupPolygonDynamics(const std::string &id)
Unschedules the removal and update commands of the given polygon.
virtual bool removePOI(const std::string &id)
Removes a PoI from the container.
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
virtual void reshapePolygon(const std::string &id, const PositionVector &shape)
Assigns a shape to the named polygon.
std::map< std::string, std::map< int, std::string > > myHighlightPolygons
maps objects to a map of highlight types to highlighting polygons
std::map< std::string, PolygonDynamics * > myPolygonDynamics
stored PolygonDynamics
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
virtual void removeTrackers(std::string objectID)
Remove all tracking polygons for the given object.
virtual void movePOI(const std::string &id, const Position &pos)
Assigns a new position to the named PoI.
std::map< const std::string, ParametrisedWrappingCommand< ShapeContainer, PolygonDynamics * > * > myPolygonUpdateCommands
Command pointers for scheduled polygon update. Maps PolyID->Command.
virtual ~ShapeContainer()
Destructor.
POIs myPOIs
stored POIs
std::map< std::string, std::string > myHighlightedObjects
inverse map to myHighlightPolygons saves the highlighted object for each polygon
virtual bool addPolygon(const std::string &id, const std::string &type, const RGBColor &color, double layer, double angle, const std::string &imgFile, bool relativePath, const PositionVector &shape, bool geo, bool fill, double lineWidth, bool ignorePruning=false, const std::string &name=Shape::DEFAULT_NAME)
Builds a polygon using the given values and adds it to the container.
std::map< const std::string, std::set< const SUMOPolygon * > > myTrackingPolygons
Information about tracked objects.
virtual PolygonDynamics * addPolygonDynamics(double simtime, std::string polyID, SUMOTrafficObject *trackedObject, const std::vector< double > &timeSpan, const std::vector< double > &alphaSpan, bool looped, bool rotate)
Adds dynamics (animation / tracking) to the given polygon.
ShapeContainer()
Constructor.
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.
virtual void addPolygonUpdateCommand(std::string polyID, ParametrisedWrappingCommand< ShapeContainer, PolygonDynamics * > *cmd)
Register update command (for descheduling at removal)
virtual bool add(SUMOPolygon *poly, bool ignorePruning=false)
add polygon
virtual void clearHighlights(const std::string &objectID, SUMOPolygon *p)
Clears all highlight information from the maps when the object leaves the net (Highlight polygons and...
virtual void registerHighlight(const std::string &objectID, const int type, const std::string &polygonID)
register highlight of the specified type if the given id
void clearState()
Remove all dynamics before quick-loading state.
Polygons myPolygons
stored Polygons