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-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 extended by mutexes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include "GUIShapeContainer.h"
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
38  myVis(vis),
39  myAllowReplacement(false) {
40 }
41 
42 
44 
45 
46 bool
47 GUIShapeContainer::addPOI(const std::string& id, const std::string& type, const RGBColor& color, const Position& pos, bool geo,
48  const std::string& lane, double posOverLane, double posLat, double layer, double angle,
49  const std::string& imgFile, bool relativePath, double width, double height, bool /* ignorePruning */) {
50  GUIPointOfInterest* p = new GUIPointOfInterest(id, type, color, pos, geo, lane, posOverLane, posLat, 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_WARNING("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  GUIPolygon* p = new GUIPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath);
75  FXMutexLock locker(myLock);
76  if (!myPolygons.add(id, p)) {
77  if (myAllowReplacement) {
78  GUIPolygon* oldP = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
80  myPolygons.remove(id);
81  myPolygons.add(id, p);
82  WRITE_WARNING("Replacing polygon '" + id + "'");
83  } else {
84  delete p;
85  return false;
86  }
87  }
89  return true;
90 }
91 
92 
95  std::string polyID,
96  SUMOTrafficObject* trackedObject,
97  const std::vector<double>& timeSpan,
98  const std::vector<double>& alphaSpan,
99  bool looped,
100  bool rotate) {
101  PolygonDynamics* pd = ShapeContainer::addPolygonDynamics(simtime, polyID, trackedObject, timeSpan, alphaSpan, looped, rotate);
102  if (pd != nullptr) {
103  pd->setRTree(&myVis);
104  }
105  return pd;
106 }
107 
108 
109 SUMOTime
111  FXMutexLock locker(myLock);
113  if (next != 0) {
114  // Update polygon position in RTree
115  GUIPolygon* p = dynamic_cast<GUIPolygon*>(pd->getPolygon());
116  assert(p != nullptr);
119  }
120  return next;
121 }
122 
123 
124 bool
125 GUIShapeContainer::removePolygon(const std::string& id, bool useLock) {
126  GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
127  if (p == nullptr) {
128  return false;
129  }
130  FXMutexLock* locker = nullptr;
131  if (useLock) {
132  locker = new FXMutexLock(myLock);
133  }
135  bool succ = ShapeContainer::removePolygon(id);
136  delete locker;
137  return succ;
138 }
139 
140 
141 bool
142 GUIShapeContainer::removePOI(const std::string& id) {
143  FXMutexLock locker(myLock);
144  GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
145  if (p == nullptr) {
146  return false;
147  }
149  return myPOIs.remove(id);
150 }
151 
152 
153 void
154 GUIShapeContainer::movePOI(const std::string& id, const Position& pos) {
155  FXMutexLock locker(myLock);
156  GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
157  if (p != nullptr) {
159  static_cast<Position*>(p)->set(pos);
161  }
162 }
163 
164 
165 void
166 GUIShapeContainer::reshapePolygon(const std::string& id, const PositionVector& shape) {
167  FXMutexLock locker(myLock);
168  GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
169  if (p != nullptr) {
171  p->setShape(shape);
173  }
174 }
175 
176 
177 
178 std::vector<GUIGlID>
180  FXMutexLock locker(myLock);
181  std::vector<GUIGlID> ret;
182  for (const auto& poi : getPOIs()) {
183  ret.push_back(static_cast<GUIPointOfInterest*>(poi.second)->getGlID());
184  }
185  return ret;
186 }
187 
188 
189 std::vector<GUIGlID>
191  FXMutexLock locker(myLock);
192  std::vector<GUIGlID> ret;
193  for (const auto& poly : getPolygons()) {
194  ret.push_back(static_cast<GUIPolygon*>(poly.second)->getGlID());
195  }
196  return ret;
197 }
198 
199 /****************************************************************************/
200 
FXMutex myLock
The mutex for adding/removing operations.
long long int SUMOTime
Definition: SUMOTime.h:35
virtual bool removePolygon(const std::string &id, bool useLock=true) override
Removes a polygon from the container.
const Polygons & getPolygons() const
Returns all polygons.
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.
std::vector< GUIGlID > getPOIIds() const
Returns the gl-ids of all pois.
T get(const std::string &id) const
Retrieves an item.
POIs myPOIs
stored POIs
Polygons myPolygons
stored Polygons
SUMORTree & myVis
The RTree structure to add and remove visualization elements.
void removeAdditionalGLObject(GUIGlObject *o)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition: SUMORTree.h:157
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.
A RT-tree for efficient storing of SUMO&#39;s GL-objects.
Definition: SUMORTree.h:69
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.
void setRTree(SUMORTree *rtree)
Set the RTree.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
bool add(const std::string &id, T item)
Adds an item.
virtual void reshapePolygon(const std::string &id, const PositionVector &shape) override
Assigns a shape to the named polygon.
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
virtual ~GUIShapeContainer()
Destructor.
virtual void setShape(const PositionVector &shape)
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:162
GUIShapeContainer(SUMORTree &vis)
Constructor.
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,.
SUMOPolygon * getPolygon() const
Representation of a vehicle or person.
void addAdditionalGLObject(GUIGlObject *o)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: SUMORTree.h:127
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.
SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd) override
Update PolygonDynamics,.
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
bool remove(const std::string &id, const bool del=true)
Removes an item.
std::vector< GUIGlID > getPolygonIDs() const
Returns the gl-ids of all polygons.
bool myAllowReplacement
whether existing ids shall be replaced
const POIs & getPOIs() const
Returns all pois.