Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/gui/globjects/GUIShapeContainer.cpp
169684 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file GUIShapeContainer.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date 08.10.2009
19
///
20
// Storage for geometrical objects extended by mutexes
21
/****************************************************************************/
22
#include <config.h>
23
24
#include "GUIShapeContainer.h"
25
#include <utils/common/MsgHandler.h>
26
#include <utils/shapes/PolygonDynamics.h>
27
#include <foreign/rtree/SUMORTree.h>
28
#include <utils/gui/globjects/GUIPolygon.h>
29
#include <utils/gui/globjects/GUIPointOfInterest.h>
30
31
32
// ===========================================================================
33
// method definitions
34
// ===========================================================================
35
GUIShapeContainer::GUIShapeContainer(SUMORTree& vis) :
36
myVis(vis),
37
myAllowReplacement(false) {
38
}
39
40
41
GUIShapeContainer::~GUIShapeContainer() {}
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, 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, width, height);
51
FXMutexLock locker(myLock);
52
if (!myPOIs.add(id, p)) {
53
if (myAllowReplacement) {
54
GUIPointOfInterest* oldP = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
55
myVis.removeAdditionalGLObject(oldP);
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
}
64
myVis.addAdditionalGLObject(p);
65
return true;
66
}
67
68
69
bool
70
GUIShapeContainer::addPolygon(const std::string& id, const std::string& type, const RGBColor& color, double layer,
71
double angle, const std::string& imgFile, const PositionVector& shape, bool geo, bool fill,
72
double lineWidth, bool /* ignorePruning */, const std::string& name) {
73
GUIPolygon* p = new GUIPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, name);
74
FXMutexLock locker(myLock);
75
if (!myPolygons.add(id, p)) {
76
if (myAllowReplacement) {
77
GUIPolygon* oldP = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
78
myVis.removeAdditionalGLObject(oldP);
79
myPolygons.remove(id);
80
myPolygons.add(id, p);
81
WRITE_WARNINGF(TL("Replacing polygon '%'"), id);
82
} else {
83
delete p;
84
return false;
85
}
86
}
87
bool state = myInactivePolygonTypes.empty() || (std::find(myInactivePolygonTypes.begin(), myInactivePolygonTypes.end(), type) == myInactivePolygonTypes.end());
88
p->activate(state);
89
myVis.addAdditionalGLObject(p);
90
return true;
91
}
92
93
94
PolygonDynamics*
95
GUIShapeContainer::addPolygonDynamics(double simtime,
96
std::string polyID,
97
SUMOTrafficObject* trackedObject,
98
const std::vector<double>& timeSpan,
99
const std::vector<double>& alphaSpan,
100
bool looped,
101
bool rotate) {
102
PolygonDynamics* pd = ShapeContainer::addPolygonDynamics(simtime, polyID, trackedObject, timeSpan, alphaSpan, looped, rotate);
103
if (pd != nullptr) {
104
pd->setRTree(&myVis);
105
}
106
return pd;
107
}
108
109
110
SUMOTime
111
GUIShapeContainer::polygonDynamicsUpdate(SUMOTime t, PolygonDynamics* pd) {
112
FXMutexLock locker(myLock);
113
GUIPolygon* p = dynamic_cast<GUIPolygon*>(pd->getPolygon());
114
assert(p != nullptr);
115
myVis.removeAdditionalGLObject(p);
116
SUMOTime next = ShapeContainer::polygonDynamicsUpdate(t, pd);
117
if (next != 0) {
118
// Update polygon position in RTree
119
myVis.addAdditionalGLObject(p);
120
}
121
return next;
122
}
123
124
125
bool
126
GUIShapeContainer::removePolygon(const std::string& id, bool useLock) {
127
GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
128
if (p == nullptr) {
129
return false;
130
}
131
FXMutexLock* locker = nullptr;
132
if (useLock) {
133
locker = new FXMutexLock(myLock);
134
}
135
myVis.removeAdditionalGLObject(p);
136
bool succ = ShapeContainer::removePolygon(id);
137
delete locker;
138
return succ;
139
}
140
141
142
bool
143
GUIShapeContainer::removePOI(const std::string& id) {
144
FXMutexLock locker(myLock);
145
GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
146
if (p == nullptr) {
147
return false;
148
}
149
myVis.removeAdditionalGLObject(p);
150
return myPOIs.remove(id);
151
}
152
153
154
void
155
GUIShapeContainer::movePOI(const std::string& id, const Position& pos) {
156
FXMutexLock locker(myLock);
157
GUIPointOfInterest* p = dynamic_cast<GUIPointOfInterest*>(myPOIs.get(id));
158
if (p != nullptr) {
159
myVis.removeAdditionalGLObject(p);
160
static_cast<Position*>(p)->set(pos);
161
myVis.addAdditionalGLObject(p);
162
}
163
}
164
165
166
void
167
GUIShapeContainer::reshapePolygon(const std::string& id, const PositionVector& shape) {
168
FXMutexLock locker(myLock);
169
GUIPolygon* p = dynamic_cast<GUIPolygon*>(myPolygons.get(id));
170
if (p != nullptr) {
171
myVis.removeAdditionalGLObject(p);
172
p->setShape(shape);
173
myVis.addAdditionalGLObject(p);
174
}
175
}
176
177
178
179
std::vector<GUIGlID>
180
GUIShapeContainer::getPOIIds() const {
181
FXMutexLock locker(myLock);
182
std::vector<GUIGlID> ret;
183
for (const auto& poi : getPOIs()) {
184
ret.push_back(static_cast<GUIPointOfInterest*>(poi.second)->getGlID());
185
}
186
return ret;
187
}
188
189
190
std::vector<GUIGlID>
191
GUIShapeContainer::getPolygonIDs() const {
192
FXMutexLock locker(myLock);
193
std::vector<GUIGlID> ret;
194
for (const auto& poly : getPolygons()) {
195
ret.push_back(static_cast<GUIPolygon*>(poly.second)->getGlID());
196
}
197
return ret;
198
}
199
200
201
void
202
GUIShapeContainer::allowReplacement() {
203
myAllowReplacement = true;
204
}
205
206
207
void
208
GUIShapeContainer::setInactivePolygonTypes(std::set<std::string> inactivePolygonTypes) {
209
myInactivePolygonTypes = inactivePolygonTypes;
210
computeActivePolygons();
211
}
212
213
214
void
215
GUIShapeContainer::addInactivePolygonTypes(std::set<std::string> inactivePolygonTypes) {
216
myInactivePolygonTypes.insert(inactivePolygonTypes.begin(), inactivePolygonTypes.end());
217
computeActivePolygons();
218
}
219
220
221
void
222
GUIShapeContainer::removeInactivePolygonTypes(std::set<std::string> inactivePolygonTypes) {
223
for (std::string type : inactivePolygonTypes) {
224
myInactivePolygonTypes.erase(type);
225
}
226
computeActivePolygons();
227
}
228
229
230
void
231
GUIShapeContainer::computeActivePolygons(void) {
232
for (auto polygonWithID : myPolygons) {
233
GUIPolygon* polygon = (GUIPolygon*)polygonWithID.second;
234
bool state = std::find(myInactivePolygonTypes.begin(), myInactivePolygonTypes.end(), polygon->getShapeType()) == myInactivePolygonTypes.end();
235
polygon->activate(state);
236
}
237
}
238
239
/****************************************************************************/
240
241