Eclipse SUMO - Simulation of Urban MObility
GUIShapeContainer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
20 // Storage for geometrical objects extended by mutexes
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include "GUIShapeContainer.h"
30 
31 
32 // ===========================================================================
33 // method definitions
34 // ===========================================================================
36  myVis(vis),
37  myAllowReplacement(false) {
38 }
39 
40 
42 
43 
44 bool
45 GUIShapeContainer::addPOI(const std::string& id, const std::string& type, const RGBColor& color, const Position& pos, bool geo,
46  const std::string& lane, double posOverLane, double posLat, double layer, double angle,
47  const std::string& imgFile, bool relativePath, double width, double height, bool /* ignorePruning */) {
48  GUIPointOfInterest* p = new GUIPointOfInterest(id, type, color, pos, geo, lane, posOverLane, posLat, layer, angle, imgFile, relativePath, width, height);
49  FXMutexLock locker(myLock);
50  if (!myPOIs.add(id, p)) {
51  if (myAllowReplacement) {
52  GUIPointOfInterest* oldP = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
54  myPOIs.remove(id);
55  myPOIs.add(id, p);
56  WRITE_WARNING("Replacing POI '" + id + "'");
57  } else {
58  delete p;
59  return false;
60  }
61  }
63  return true;
64 }
65 
66 
67 bool
68 GUIShapeContainer::addPolygon(const std::string& id, const std::string& type,
69  const RGBColor& color, double layer,
70  double angle, const std::string& imgFile, bool relativePath,
71  const PositionVector& shape, bool geo, bool fill, double lineWidth, bool /* ignorePruning */) {
72  GUIPolygon* p = new GUIPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath);
73  FXMutexLock locker(myLock);
74  if (!myPolygons.add(id, p)) {
75  if (myAllowReplacement) {
76  GUIPolygon* oldP = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
78  myPolygons.remove(id);
79  myPolygons.add(id, p);
80  WRITE_WARNING("Replacing polygon '" + id + "'");
81  } else {
82  delete p;
83  return false;
84  }
85  }
87  return true;
88 }
89 
90 
93  std::string polyID,
94  SUMOTrafficObject* trackedObject,
95  const std::vector<double>& timeSpan,
96  const std::vector<double>& alphaSpan,
97  bool looped,
98  bool rotate) {
99  PolygonDynamics* pd = ShapeContainer::addPolygonDynamics(simtime, polyID, trackedObject, timeSpan, alphaSpan, looped, rotate);
100  if (pd != nullptr) {
101  pd->setRTree(&myVis);
102  }
103  return pd;
104 }
105 
106 
107 SUMOTime
109  FXMutexLock locker(myLock);
110  GUIPolygon* p = dynamic_cast<GUIPolygon*>(pd->getPolygon());
111  assert(p != nullptr);
114  if (next != 0) {
115  // Update polygon position in RTree
117  }
118  return next;
119 }
120 
121 
122 bool
123 GUIShapeContainer::removePolygon(const std::string& id, bool useLock) {
124  GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
125  if (p == nullptr) {
126  return false;
127  }
128  FXMutexLock* locker = nullptr;
129  if (useLock) {
130  locker = new FXMutexLock(myLock);
131  }
133  bool succ = ShapeContainer::removePolygon(id);
134  delete locker;
135  return succ;
136 }
137 
138 
139 bool
140 GUIShapeContainer::removePOI(const std::string& id) {
141  FXMutexLock locker(myLock);
142  GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
143  if (p == nullptr) {
144  return false;
145  }
147  return myPOIs.remove(id);
148 }
149 
150 
151 void
152 GUIShapeContainer::movePOI(const std::string& id, const Position& pos) {
153  FXMutexLock locker(myLock);
154  GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
155  if (p != nullptr) {
157  static_cast<Position*>(p)->set(pos);
159  }
160 }
161 
162 
163 void
164 GUIShapeContainer::reshapePolygon(const std::string& id, const PositionVector& shape) {
165  FXMutexLock locker(myLock);
166  GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
167  if (p != nullptr) {
169  p->setShape(shape);
171  }
172 }
173 
174 
175 
176 std::vector<GUIGlID>
178  FXMutexLock locker(myLock);
179  std::vector<GUIGlID> ret;
180  for (const auto& poi : getPOIs()) {
181  ret.push_back(static_cast<GUIPointOfInterest*>(poi.second)->getGlID());
182  }
183  return ret;
184 }
185 
186 
187 std::vector<GUIGlID>
189  FXMutexLock locker(myLock);
190  std::vector<GUIGlID> ret;
191  for (const auto& poly : getPolygons()) {
192  ret.push_back(static_cast<GUIPolygon*>(poly.second)->getGlID());
193  }
194  return ret;
195 }
196 
197 
198 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
long long int SUMOTime
Definition: SUMOTime.h:31
GUIGlID getGlID() const
Returns the numerical id of the object.
virtual void setShape(const PositionVector &shape)
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:171
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) override
Builds a polygon using the given values and adds it to the container.
virtual void movePOI(const std::string &id, const Position &pos) override
Assigns a new position to the named PoI.
virtual bool removePOI(const std::string &id) override
Removes a PoI from the container.
SUMORTree & myVis
The RTree structure to add and remove visualization elements.
virtual ~GUIShapeContainer()
Destructor.
SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd) override
Update PolygonDynamics,.
std::vector< GUIGlID > getPOIIds() const
Returns the gl-ids of all pois.
std::vector< GUIGlID > getPolygonIDs() const
Returns the gl-ids of all polygons.
virtual bool removePolygon(const std::string &id, bool useLock=true) override
Removes a polygon from the container.
FXMutex myLock
The mutex for adding/removing operations.
GUIShapeContainer(SUMORTree &vis)
Constructor.
bool myAllowReplacement
whether existing ids shall be replaced
virtual void reshapePolygon(const std::string &id, const PositionVector &shape) override
Assigns a shape to the named polygon.
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) override
Builds a POI using the given values and adds it to the container.
PolygonDynamics * addPolygonDynamics(double simtime, std::string polyID, SUMOTrafficObject *trackedObject, const std::vector< double > &timeSpan, const std::vector< double > &alphaSpan, bool looped, bool rotate) override
Adds dynamics to the given Polygon,.
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.
void setRTree(SUMORTree *rtree)
Set the RTree.
SUMOPolygon * getPolygon() const
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
A list of positions.
A RT-tree for efficient storing of SUMO's GL-objects.
Definition: SUMORTree.h:66
void removeAdditionalGLObject(GUIGlObject *o)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition: SUMORTree.h:154
void addAdditionalGLObject(GUIGlObject *o)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: SUMORTree.h:124
Representation of a vehicle, person, or container.
const Polygons & getPolygons() const
Returns all polygons.
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
POIs myPOIs
stored POIs
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.
Polygons myPolygons
stored Polygons
const POIs & getPOIs() const
Returns all pois.