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