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