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 /****************************************************************************/
17 // Storage for geometrical objects, sorted by the layers they are in
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <fstream>
27 #include <stdlib.h>
28 #include <iostream>
29 #include <utility>
30 #include <string>
31 #include <cmath>
35 #include <utils/common/ToString.h>
36 #include <utils/common/StdDefs.h>
38 #include "PolygonDynamics.h"
39 #include "ShapeContainer.h"
40 
41 
42 // Debug defines
43 //#define DEBUG_DYNAMIC_SHAPES
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49 
51  for (auto& p : myPolygonUpdateCommands) {
52  p.second->deschedule();
53  }
55 
56  for (auto& p : myPolygonDynamics) {
57  delete p.second;
58  }
59  myPolygonDynamics.clear();
60 
61 }
62 
63 bool
64 ShapeContainer::addPolygon(const std::string& id, const std::string& type,
65  const RGBColor& color, double layer,
66  double angle, const std::string& imgFile, bool relativePath,
67  const PositionVector& shape, bool geo, bool fill, double lineWidth, bool ignorePruning) {
68  return add(new SUMOPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath), 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, double posLat, double layer, double angle,
147  const std::string& imgFile, bool relativePath, double width, double height, bool ignorePruning) {
148  return add(new PointOfInterest(id, type, color, pos, geo, lane, posOverLane, posLat, 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 
206 void
208  auto j = myPolygonUpdateCommands.find(id);
209  if (j != myPolygonUpdateCommands.end()) {
210  j->second->deschedule();
211  myPolygonUpdateCommands.erase(j);
212  }
213 }
214 
215 
216 SUMOTime
218  SUMOTime next = pd->update(t);
219  if (next == 0) {
220  // Dynamics have expired => remove polygon
221  myPolygonUpdateCommands[pd->getPolygonID()]->deschedule();
222  // Don't aquire lock (in GUI case GUIShapeContainer::polygonDynamicsUpdate() does this)
223  removePolygon(pd->getPolygonID(), false);
224  }
225  return next;
226 }
227 
228 void
229 ShapeContainer::registerHighlight(const std::string& objectID, const int type, const std::string& polygonID) {
230  std::string toRemove = "";
231  clearHighlight(objectID, type, toRemove);
232  if (toRemove != "") {
233  removePolygon(toRemove);
234  }
235  auto i = myHighlightPolygons.find(objectID);
236  if (i == myHighlightPolygons.end()) {
237  myHighlightPolygons.insert(std::make_pair(objectID, std::map<int, std::string>({std::make_pair(type, polygonID)})));
238  } else {
239  i->second.insert(std::make_pair(type, polygonID));
240  }
241  myHighlightedObjects.insert(std::make_pair(polygonID, objectID));
242 }
243 
244 void
245 ShapeContainer::clearHighlight(const std::string& objectID, const int type, std::string& toRemove) {
246  auto i = myHighlightPolygons.find(objectID);
247  if (i != myHighlightPolygons.end()) {
248  auto j = i->second.find(type);
249  if (j != i->second.end()) {
250  toRemove = j->second;
251  myHighlightedObjects.erase(toRemove);
252  i->second.erase(j);
253  if (i->second.empty()) {
254  myHighlightPolygons.erase(i);
255  }
256  }
257  }
258 }
259 
260 void
261 ShapeContainer::clearHighlights(const std::string& objectID, SUMOPolygon* p) {
262  auto i = myHighlightPolygons.find(objectID);
263  if (i != myHighlightPolygons.end()) {
264  auto j = i->second.begin();
265  while (j != i->second.end()) {
266  if (j->second == p->getID()) {
267  i->second.erase(j);
268  break;
269  } else {
270  ++j;
271  }
272  }
273  if (i->second.empty()) {
274  myHighlightPolygons.erase(i);
275  }
276  }
277 }
278 
279 void
281  myPolygonUpdateCommands.insert(std::make_pair(polyID, cmd));
282 }
283 
284 
285 void
286 ShapeContainer::removeTrackers(std::string objectID) {
287  auto i = myTrackingPolygons.find(objectID);
288  if (i != myTrackingPolygons.end()) {
289 #ifdef DEBUG_DYNAMIC_SHAPES
290  std::cout << " Removing tracking polygons for object '" << objectID << "'" << std::endl;
291 #endif
292  while (!i->second.empty()) {
293  removePolygon((*i->second.begin())->getID());
294  }
295  myTrackingPolygons.erase(i);
296  }
297 }
298 
299 
300 /****************************************************************************/
ShapeContainer::cleanupPolygonDynamics
virtual void cleanupPolygonDynamics(const std::string &id)
Unschedules the removal and update commands of the given polygon.
Definition: ShapeContainer.cpp:207
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
ToString.h
PolygonDynamics.h
ShapeContainer::removeTrackers
virtual void removeTrackers(std::string objectID)
Remove all tracking polygons for the given object.
Definition: ShapeContainer.cpp:286
ShapeContainer::polygonDynamicsUpdate
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
Definition: ShapeContainer.cpp:217
ShapeContainer::myPolygonDynamics
std::map< std::string, PolygonDynamics * > myPolygonDynamics
stored PolygonDynamics
Definition: ShapeContainer.h:202
ShapeContainer::myTrackingPolygons
std::map< const std::string, std::set< const SUMOPolygon * > > myTrackingPolygons
Information about tracked objects.
Definition: ShapeContainer.h:212
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:169
SUMOPolygon::setShape
virtual void setShape(const PositionVector &shape)
Sets the shape of the polygon.
Definition: SUMOPolygon.h:120
ShapeContainer::add
virtual bool add(SUMOPolygon *poly, bool ignorePruning=false)
add polygon
Definition: ShapeContainer.cpp:187
ShapeContainer::removePolygonDynamics
virtual bool removePolygonDynamics(const std::string &polyID)
Remove dynamics (animation / tracking) for the given polygon.
Definition: ShapeContainer.cpp:113
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
PolygonDynamics::getTrackedObjectID
const std::string & getTrackedObjectID() const
Definition: PolygonDynamics.h:62
ShapeContainer::reshapePolygon
virtual void reshapePolygon(const std::string &id, const PositionVector &shape)
Assigns a shape to the named polygon.
Definition: ShapeContainer.cpp:178
PositionVector
A list of positions.
Definition: PositionVector.h:45
PolygonDynamics::getPolygonID
const std::string & getPolygonID() const
Definition: PolygonDynamics.h:54
ShapeContainer::~ShapeContainer
virtual ~ShapeContainer()
Destructor.
Definition: ShapeContainer.cpp:50
RGBColor
Definition: RGBColor.h:39
ShapeContainer::clearHighlight
virtual void clearHighlight(const std::string &objectID, const int type, std::string &toRemove)
Definition: ShapeContainer.cpp:245
NamedObjectCont::remove
bool remove(const std::string &id, const bool del=true)
Removes an item.
Definition: NamedObjectCont.h:78
ShapeContainer::myPolygonUpdateCommands
std::map< const std::string, ParametrisedWrappingCommand< ShapeContainer, PolygonDynamics * > * > myPolygonUpdateCommands
Command pointers for scheduled polygon update. Maps PolyID->Command.
Definition: ShapeContainer.h:219
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:73
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
ShapeContainer::myPOIs
POIs myPOIs
stored POIs
Definition: ShapeContainer.h:215
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:205
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:64
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:229
SUMOPolygon
Definition: SUMOPolygon.h:46
ParametrisedWrappingCommand
A wrapper for a Command function with parameter.
Definition: ParametrisedWrappingCommand.h:32
NamedObjectCont::get
T get(const std::string &id) const
Retrieves an item.
Definition: NamedObjectCont.h:98
ShapeContainer::removePolygon
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
Definition: ShapeContainer.cpp:153
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
PolygonDynamics
Definition: PolygonDynamics.h:29
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:94
ParametrisedWrappingCommand.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
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:261
ShapeContainer::ShapeContainer
ShapeContainer()
Constructor.
Definition: ShapeContainer.cpp:48
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
ShapeContainer::myPolygons
Polygons myPolygons
stored Polygons
Definition: ShapeContainer.h:199
ShapeContainer::myHighlightedObjects
std::map< std::string, std::string > myHighlightedObjects
inverse map to myHighlightPolygons saves the highlighted object for each polygon
Definition: ShapeContainer.h:207
ShapeContainer::addPolygonUpdateCommand
virtual void addPolygonUpdateCommand(std::string polyID, ParametrisedWrappingCommand< ShapeContainer, PolygonDynamics * > *cmd)
Register update command (for descheduling at removal)
Definition: ShapeContainer.cpp:280
NamedObjectCont::add
bool add(const std::string &id, T item)
Adds an item.
Definition: NamedObjectCont.h:65