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-2022 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
44bool
45GUIShapeContainer::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, 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, friendlyPos, posLat, layer, angle, imgFile, relativePath, width, height);
49 FXMutexLock locker(myLock);
50 if (!myPOIs.add(id, p)) {
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
67bool
68GUIShapeContainer::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 const std::string& name) {
73 GUIPolygon* p = new GUIPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath, name);
74 FXMutexLock locker(myLock);
75 if (!myPolygons.add(id, p)) {
77 GUIPolygon* oldP = dynamic_cast<GUIPolygon*>(myPolygons.get(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
110 FXMutexLock locker(myLock);
111 GUIPolygon* p = dynamic_cast<GUIPolygon*>(pd->getPolygon());
112 assert(p != nullptr);
115 if (next != 0) {
116 // Update polygon position in RTree
118 }
119 return next;
120}
121
122
123bool
124GUIShapeContainer::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
140bool
141GUIShapeContainer::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
152void
153GUIShapeContainer::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
164void
165GUIShapeContainer::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
177std::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
188std::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
199void
201 myAllowReplacement = true;
202}
203
204/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:265
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.h:102
virtual void setShape(const PositionVector &shape) override
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:257
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,.
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, 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.
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.
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.
SUMOPolygon * getPolygon() const
void setRTree(SUMORTree *rtree)
Set the RTree.
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.
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.
const Polygons & getPolygons() const
Returns all polygons.
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.
const POIs & getPOIs() const
Returns all pois.
Polygons myPolygons
stored Polygons