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 
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:48
PolygonDynamics.h
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
ShapeContainer::polygonDynamicsUpdate
virtual SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd)
Regular update event for updating polygon dynamics.
Definition: ShapeContainer.cpp:218
GUIShapeContainer::myLock
FXMutex myLock
The mutex for adding/removing operations.
Definition: GUIShapeContainer.h:153
GUIPolygon.h
GUIShapeContainer::myVis
SUMORTree & myVis
The RTree structure to add and remove visualization elements.
Definition: GUIShapeContainer.h:156
GUIShapeContainer::movePOI
virtual void movePOI(const std::string &id, const Position &pos) override
Assigns a new position to the named PoI.
Definition: GUIShapeContainer.cpp:154
MsgHandler.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
GUIShapeContainer::polygonDynamicsUpdate
SUMOTime polygonDynamicsUpdate(SUMOTime t, PolygonDynamics *pd) override
Update PolygonDynamics,.
Definition: GUIShapeContainer.cpp:110
GUIShapeContainer::removePOI
virtual bool removePOI(const std::string &id) override
Removes a PoI from the container.
Definition: GUIShapeContainer.cpp:142
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:70
PositionVector
A list of positions.
Definition: PositionVector.h:46
GUIShapeContainer::~GUIShapeContainer
virtual ~GUIShapeContainer()
Destructor.
Definition: GUIShapeContainer.cpp:43
SUMORTree
A RT-tree for efficient storing of SUMO's GL-objects.
Definition: SUMORTree.h:69
RGBColor
Definition: RGBColor.h:40
GUIShapeContainer::removePolygon
virtual bool removePolygon(const std::string &id, bool useLock=true) override
Removes a polygon from the container.
Definition: GUIShapeContainer.cpp:125
GUIPointOfInterest
Definition: GUIPointOfInterest.h:46
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:47
GUIPointOfInterest.h
NamedObjectCont::remove
bool remove(const std::string &id, const bool del=true)
Removes an item.
Definition: NamedObjectCont.h:79
GUIPolygon::setShape
virtual void setShape(const PositionVector &shape)
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:162
SUMORTree.h
GUIShapeContainer.h
SUMORTree::removeAdditionalGLObject
void removeAdditionalGLObject(GUIGlObject *o)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition: SUMORTree.h:157
PolygonDynamics::setRTree
void setRTree(SUMORTree *rtree)
Set the RTree.
Definition: PolygonDynamics.h:73
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:74
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
ShapeContainer::myPOIs
POIs myPOIs
stored POIs
Definition: ShapeContainer.h:216
ShapeContainer::getPOIs
const POIs & getPOIs() const
Returns all pois.
Definition: ShapeContainer.h:155
GUIShapeContainer::getPolygonIDs
std::vector< GUIGlID > getPolygonIDs() const
Returns the gl-ids of all polygons.
Definition: GUIShapeContainer.cpp:190
GUIShapeContainer::reshapePolygon
virtual void reshapePolygon(const std::string &id, const PositionVector &shape) override
Assigns a shape to the named polygon.
Definition: GUIShapeContainer.cpp:166
NamedObjectCont::get
T get(const std::string &id) const
Retrieves an item.
Definition: NamedObjectCont.h:99
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:94
GUIShapeContainer::getPOIIds
std::vector< GUIGlID > getPOIIds() const
Returns the gl-ids of all pois.
Definition: GUIShapeContainer.cpp:179
ShapeContainer::removePolygon
virtual bool removePolygon(const std::string &id, bool useLock=true)
Removes a polygon from the container.
Definition: ShapeContainer.cpp:154
SUMORTree::addAdditionalGLObject
void addAdditionalGLObject(GUIGlObject *o)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: SUMORTree.h:127
PolygonDynamics
Definition: PolygonDynamics.h:30
config.h
PolygonDynamics::getPolygon
SUMOPolygon * getPolygon() const
Definition: PolygonDynamics.h:59
ShapeContainer::getPolygons
const Polygons & getPolygons() const
Returns all polygons.
Definition: ShapeContainer.h:150
ShapeContainer::myPolygons
Polygons myPolygons
stored Polygons
Definition: ShapeContainer.h:200
GUIPolygon
Definition: GUIPolygon.h:42
GUIShapeContainer::GUIShapeContainer
GUIShapeContainer(SUMORTree &vis)
Constructor.
Definition: GUIShapeContainer.cpp:37
GUIShapeContainer::myAllowReplacement
bool myAllowReplacement
whether existing ids shall be replaced
Definition: GUIShapeContainer.h:160
NamedObjectCont::add
bool add(const std::string &id, T item)
Adds an item.
Definition: NamedObjectCont.h:66