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.dev/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
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, bool friendlyPos, double posLat, const std::string& icon,
47  double layer, double angle, const std::string& imgFile, bool relativePath, double width, double height,
48  bool /* ignorePruning */) {
49  GUIPointOfInterest* p = new GUIPointOfInterest(id, type, color, pos, geo, lane, posOverLane, friendlyPos, posLat, icon,
50  layer, angle, imgFile, relativePath, width, height);
51  FXMutexLock locker(myLock);
52  if (!myPOIs.add(id, p)) {
53  if (myAllowReplacement) {
54  GUIPointOfInterest* oldP = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
56  myPOIs.remove(id);
57  myPOIs.add(id, p);
58  WRITE_WARNINGF(TL("Replacing POI '%'"), id);
59  } else {
60  delete p;
61  return false;
62  }
63  }
65  return true;
66 }
67 
68 
69 bool
70 GUIShapeContainer::addPolygon(const std::string& id, const std::string& type,
71  const RGBColor& color, double layer,
72  double angle, const std::string& imgFile, bool relativePath,
73  const PositionVector& shape, bool geo, bool fill, double lineWidth, bool /* ignorePruning */,
74  const std::string& name) {
75  GUIPolygon* p = new GUIPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath, name);
76  FXMutexLock locker(myLock);
77  if (!myPolygons.add(id, p)) {
78  if (myAllowReplacement) {
79  GUIPolygon* oldP = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
81  myPolygons.remove(id);
82  myPolygons.add(id, p);
83  WRITE_WARNINGF(TL("Replacing polygon '%'"), id);
84  } else {
85  delete p;
86  return false;
87  }
88  }
89  bool state = myInactivePolygonTypes.empty() || (std::find(myInactivePolygonTypes.begin(), myInactivePolygonTypes.end(), type) == myInactivePolygonTypes.end());
90  p->activate(state);
92  return true;
93 }
94 
95 
98  std::string polyID,
99  SUMOTrafficObject* trackedObject,
100  const std::vector<double>& timeSpan,
101  const std::vector<double>& alphaSpan,
102  bool looped,
103  bool rotate) {
104  PolygonDynamics* pd = ShapeContainer::addPolygonDynamics(simtime, polyID, trackedObject, timeSpan, alphaSpan, looped, rotate);
105  if (pd != nullptr) {
106  pd->setRTree(&myVis);
107  }
108  return pd;
109 }
110 
111 
112 SUMOTime
114  FXMutexLock locker(myLock);
115  GUIPolygon* p = dynamic_cast<GUIPolygon*>(pd->getPolygon());
116  assert(p != nullptr);
119  if (next != 0) {
120  // Update polygon position in RTree
122  }
123  return next;
124 }
125 
126 
127 bool
128 GUIShapeContainer::removePolygon(const std::string& id, bool useLock) {
129  GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
130  if (p == nullptr) {
131  return false;
132  }
133  FXMutexLock* locker = nullptr;
134  if (useLock) {
135  locker = new FXMutexLock(myLock);
136  }
138  bool succ = ShapeContainer::removePolygon(id);
139  delete locker;
140  return succ;
141 }
142 
143 
144 bool
145 GUIShapeContainer::removePOI(const std::string& id) {
146  FXMutexLock locker(myLock);
147  GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
148  if (p == nullptr) {
149  return false;
150  }
152  return myPOIs.remove(id);
153 }
154 
155 
156 void
157 GUIShapeContainer::movePOI(const std::string& id, const Position& pos) {
158  FXMutexLock locker(myLock);
159  GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
160  if (p != nullptr) {
162  static_cast<Position*>(p)->set(pos);
164  }
165 }
166 
167 
168 void
169 GUIShapeContainer::reshapePolygon(const std::string& id, const PositionVector& shape) {
170  FXMutexLock locker(myLock);
171  GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
172  if (p != nullptr) {
174  p->setShape(shape);
176  }
177 }
178 
179 
180 
181 std::vector<GUIGlID>
183  FXMutexLock locker(myLock);
184  std::vector<GUIGlID> ret;
185  for (const auto& poi : getPOIs()) {
186  ret.push_back(static_cast<GUIPointOfInterest*>(poi.second)->getGlID());
187  }
188  return ret;
189 }
190 
191 
192 std::vector<GUIGlID>
194  FXMutexLock locker(myLock);
195  std::vector<GUIGlID> ret;
196  for (const auto& poly : getPolygons()) {
197  ret.push_back(static_cast<GUIPolygon*>(poly.second)->getGlID());
198  }
199  return ret;
200 }
201 
202 
203 void
205  myAllowReplacement = true;
206 }
207 
208 
209 void
210 GUIShapeContainer::setInactivePolygonTypes(std::set<std::string> inactivePolygonTypes) {
211  myInactivePolygonTypes = inactivePolygonTypes;
213 }
214 
215 
216 void
217 GUIShapeContainer::addInactivePolygonTypes(std::set<std::string> inactivePolygonTypes) {
218  myInactivePolygonTypes.insert(inactivePolygonTypes.begin(), inactivePolygonTypes.end());
220 }
221 
222 
223 void
224 GUIShapeContainer::removeInactivePolygonTypes(std::set<std::string> inactivePolygonTypes) {
225  for (std::string type : inactivePolygonTypes) {
226  myInactivePolygonTypes.erase(type);
227  }
229 }
230 
231 
232 void
234  for (auto polygonWithID : myPolygons) {
235  GUIPolygon* polygon = (GUIPolygon*)polygonWithID.second;
236  bool state = std::find(myInactivePolygonTypes.begin(), myInactivePolygonTypes.end(), polygon->getShapeType()) == myInactivePolygonTypes.end();
237  polygon->activate(state);
238  }
239 }
240 
241 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define TL(string)
Definition: MsgHandler.h:315
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.h:104
void activate(bool isActive)
Definition: GUIPolygon.h:177
virtual void setShape(const PositionVector &shape) override
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:270
void removeInactivePolygonTypes(std::set< std::string > inactivePolygonTypes)
Remove some polygon types that were deemed as inactive.
void setInactivePolygonTypes(std::set< std::string > inactivePolygonTypes)
Sets polygon types that define which one is active or not.
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.
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) override
Builds a polygon using the given values and adds it to the container.
void allowReplacement()
allow replacement
SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd) override
Update PolygonDynamics,.
std::vector< GUIGlID > getPOIIds() const
Returns the gl-ids of all pois.
std::set< std::string > myInactivePolygonTypes
The polygon types that define the inactive polygons.
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.
void computeActivePolygons(void)
Determine which polygons are active based on their type.
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) override
Builds a POI using the given values and adds it to the container.
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.
void addInactivePolygonTypes(std::set< std::string > inactivePolygonTypes)
Adds new polygon types to the set of inactive ones.
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:37
A list of positions.
A RT-tree for efficient storing of SUMO's GL-objects.
Definition: SUMORTree.h:66
void addAdditionalGLObject(GUIGlObject *o, const double exaggeration=1)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: SUMORTree.h:124
void removeAdditionalGLObject(GUIGlObject *o, const double exaggeration=1)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition: SUMORTree.h:160
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.
const std::string & getShapeType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:77