Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/gui/div/GUIDottedGeometry.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 GUIDottedGeometry.cpp
15
/// @author Pablo Alvarez Lopez
16
/// @date Oct 2021
17
///
18
// File for dotted geometry classes and functions
19
/****************************************************************************/
20
#include <utils/geom/GeomHelper.h>
21
#include <utils/gui/div/GLHelper.h>
22
#include <utils/gui/globjects/GLIncludes.h>
23
#include <utils/gui/globjects/GUIGlObjectTypes.h>
24
25
#include "GUIDottedGeometry.h"
26
#include "GUIGeometry.h"
27
28
29
#define MAXIMUM_DOTTEDGEOMETRYLENGTH 500.0
30
31
// ===========================================================================
32
// method definitions
33
// ===========================================================================
34
35
// ---------------------------------------------------------------------------
36
// GUIDottedGeometry::DottedGeometryColor - methods
37
// ---------------------------------------------------------------------------
38
39
GUIDottedGeometry::DottedGeometryColor::DottedGeometryColor() :
40
myColorFlag(true) {}
41
42
43
const RGBColor
44
GUIDottedGeometry::DottedGeometryColor::getColor(const GUIVisualizationSettings& settings, DottedContourType type) {
45
switch (type) {
46
case DottedContourType::INSPECT:
47
if (myColorFlag) {
48
myColorFlag = false;
49
return settings.dottedContourSettings.firstInspectedColor;
50
} else {
51
myColorFlag = true;
52
return settings.dottedContourSettings.secondInspectedColor;
53
}
54
case DottedContourType::FRONT:
55
if (myColorFlag) {
56
myColorFlag = false;
57
return settings.dottedContourSettings.firstFrontColor;
58
} else {
59
myColorFlag = true;
60
return settings.dottedContourSettings.secondFrontColor;
61
}
62
case DottedContourType::FROM:
63
if (myColorFlag) {
64
myColorFlag = false;
65
return RGBColor::GREEN;
66
} else {
67
myColorFlag = true;
68
return RGBColor::GREEN.changedBrightness(-30);
69
}
70
case DottedContourType::TO:
71
if (myColorFlag) {
72
myColorFlag = false;
73
return RGBColor::MAGENTA;
74
} else {
75
myColorFlag = true;
76
return RGBColor::MAGENTA.changedBrightness(-30);
77
}
78
case DottedContourType::REMOVE:
79
if (myColorFlag) {
80
myColorFlag = false;
81
return RGBColor(229, 233, 255);
82
} else {
83
myColorFlag = true;
84
return RGBColor(255, 109, 196);
85
}
86
case DottedContourType::SELECT:
87
if (myColorFlag) {
88
myColorFlag = false;
89
return RGBColor::BLUE;
90
} else {
91
myColorFlag = true;
92
return RGBColor::BLUE.changedBrightness(-30);
93
}
94
case DottedContourType::MOVE:
95
if (myColorFlag) {
96
myColorFlag = false;
97
return RGBColor(220, 0, 0);
98
} else {
99
myColorFlag = true;
100
return RGBColor(220, 0, 0).changedBrightness(-30);
101
}
102
case DottedContourType::OVER:
103
if (myColorFlag) {
104
myColorFlag = false;
105
return RGBColor::ORANGE;
106
} else {
107
myColorFlag = true;
108
return RGBColor::ORANGE.changedBrightness(-30);
109
}
110
case DottedContourType::RELATED:
111
if (myColorFlag) {
112
myColorFlag = false;
113
return RGBColor::CYAN;
114
} else {
115
myColorFlag = true;
116
return RGBColor::CYAN.changedBrightness(-30);
117
}
118
case DottedContourType::WALKINGAREA:
119
return settings.junctionColorer.getScheme().getColor(6);
120
default:
121
return RGBColor::BLACK;
122
}
123
}
124
125
126
void
127
GUIDottedGeometry::DottedGeometryColor::changeColor() {
128
if (myColorFlag) {
129
myColorFlag = false;
130
} else {
131
myColorFlag = true;
132
}
133
}
134
135
136
void
137
GUIDottedGeometry::DottedGeometryColor::reset() {
138
myColorFlag = true;
139
}
140
141
// ---------------------------------------------------------------------------
142
// GUIDottedGeometry::Segment - methods
143
// ---------------------------------------------------------------------------
144
145
GUIDottedGeometry::Segment::Segment() {}
146
147
148
GUIDottedGeometry::Segment::Segment(PositionVector newShape) :
149
shape(newShape) {
150
}
151
152
// ---------------------------------------------------------------------------
153
// GUIDottedGeometry - methods
154
// ---------------------------------------------------------------------------
155
156
GUIDottedGeometry::GUIDottedGeometry() {}
157
158
159
GUIDottedGeometry::GUIDottedGeometry(const GUIVisualizationSettings& s, const GUIVisualizationSettings::Detail d,
160
PositionVector shape, const bool closeShape) {
161
// set shape as unresampled shape
162
myUnresampledShape = shape;
163
// check if shape has to be closed
164
if (closeShape && (myUnresampledShape.size() > 2)) {
165
myUnresampledShape.closePolygon();
166
}
167
if (myUnresampledShape.size() > 1) {
168
// get shape
169
for (int i = 1; i < (int)myUnresampledShape.size(); i++) {
170
myDottedGeometrySegments.push_back(Segment({myUnresampledShape[i - 1], myUnresampledShape[i]}));
171
}
172
// check if resample
173
if (d <= GUIVisualizationSettings::Detail::DottedContoursResampled) {
174
// calculate segment length
175
double segmentLength = s.dottedContourSettings.segmentLength;
176
if (myUnresampledShape.length2D() > MAXIMUM_DOTTEDGEOMETRYLENGTH) {
177
segmentLength = myUnresampledShape.length2D() / (MAXIMUM_DOTTEDGEOMETRYLENGTH * 0.5);
178
}
179
// resample all dotted geometries
180
for (auto& segment : myDottedGeometrySegments) {
181
segment.shape = segment.shape.resample(segmentLength, true);
182
}
183
}
184
// calculate shape rotations and lengths
185
calculateShapeRotationsAndLengths();
186
}
187
}
188
189
190
void
191
GUIDottedGeometry::updateDottedGeometry(const GUIVisualizationSettings& s, const GUIVisualizationSettings::Detail d,
192
const PositionVector& laneShape) {
193
// set shape as unresampled shape
194
myUnresampledShape = laneShape;
195
// reset segments
196
myDottedGeometrySegments.clear();
197
// get shape
198
for (int i = 1; i < (int)myUnresampledShape.size(); i++) {
199
myDottedGeometrySegments.push_back(Segment({myUnresampledShape[i - 1], myUnresampledShape[i]}));
200
}
201
// check if resample
202
if (d <= GUIVisualizationSettings::Detail::DottedContoursResampled) {
203
for (auto& segment : myDottedGeometrySegments) {
204
segment.shape = segment.shape.resample(s.dottedContourSettings.segmentLength, true);
205
}
206
}
207
// calculate shape rotations and lengths
208
calculateShapeRotationsAndLengths();
209
}
210
211
212
void
213
GUIDottedGeometry::updateDottedGeometry(const GUIVisualizationSettings& s, const GUIVisualizationSettings::Detail d,
214
PositionVector shape, const bool closeShape) {
215
// set shape as unresampled shape
216
myUnresampledShape = shape;
217
// reset segments
218
myDottedGeometrySegments.clear();
219
// check if shape has to be closed
220
if (closeShape && (myUnresampledShape.size() > 2)) {
221
myUnresampledShape.closePolygon();
222
}
223
if (myUnresampledShape.size() > 1) {
224
// get shape
225
for (int i = 1; i < (int)myUnresampledShape.size(); i++) {
226
myDottedGeometrySegments.push_back(Segment({myUnresampledShape[i - 1], myUnresampledShape[i]}));
227
}
228
// check if resample
229
if (d <= GUIVisualizationSettings::Detail::DottedContoursResampled) {
230
for (auto& segment : myDottedGeometrySegments) {
231
segment.shape = segment.shape.resample(s.dottedContourSettings.segmentLength, true);
232
}
233
}
234
// calculate shape rotations and lengths
235
calculateShapeRotationsAndLengths();
236
}
237
}
238
239
240
void
241
GUIDottedGeometry::drawDottedGeometry(const GUIVisualizationSettings& s, GUIDottedGeometry::DottedContourType type,
242
DottedGeometryColor& dottedGeometryColor, const double lineWidth, const bool addOffset) const {
243
// iterate over all segments
244
for (auto& segment : myDottedGeometrySegments) {
245
// iterate over shape
246
for (int i = 0; i < ((int)segment.shape.size() - 1); i++) {
247
// set color
248
GLHelper::setColor(dottedGeometryColor.getColor(s, type));
249
// draw box line depending of addOffset
250
if (addOffset) {
251
GLHelper::drawBoxLine(segment.shape[i], segment.rotations.at(i), segment.lengths.at(i), lineWidth, -lineWidth);
252
} else {
253
GLHelper::drawBoxLine(segment.shape[i], segment.rotations.at(i), segment.lengths.at(i), lineWidth);
254
}
255
}
256
}
257
}
258
259
260
void
261
GUIDottedGeometry::drawInnenGeometry(const double lineWidth) const {
262
// iterate over all segments
263
for (auto& segment : myDottedGeometrySegments) {
264
// iterate over shape
265
for (int i = 0; i < ((int)segment.shape.size() - 1); i++) {
266
GLHelper::drawBoxLine(segment.shape[i], segment.rotations.at(i), segment.lengths.at(i), lineWidth, lineWidth);
267
}
268
}
269
}
270
271
272
void
273
GUIDottedGeometry::moveShapeToSide(const double value) {
274
// move to side all dotted geometry segments
275
for (auto& segment : myDottedGeometrySegments) {
276
segment.shape.move2side(value);
277
}
278
// also unresampled shape
279
myUnresampledShape.move2side(value);
280
}
281
282
283
Position
284
GUIDottedGeometry::getFrontPosition() const {
285
if (myUnresampledShape.size() > 0) {
286
return myUnresampledShape.front();
287
} else {
288
return Position::INVALID;
289
}
290
}
291
292
293
Position
294
GUIDottedGeometry::getBackPosition() const {
295
if (myUnresampledShape.size() > 0) {
296
return myUnresampledShape.back();
297
} else {
298
return Position::INVALID;
299
}
300
}
301
302
303
const PositionVector&
304
GUIDottedGeometry::getUnresampledShape() const {
305
return myUnresampledShape;
306
}
307
308
309
void
310
GUIDottedGeometry::clearDottedGeometry() {
311
myDottedGeometrySegments.clear();
312
myUnresampledShape.clear();
313
}
314
315
316
void
317
GUIDottedGeometry::calculateShapeRotationsAndLengths() {
318
// iterate over all segments
319
for (auto& segment : myDottedGeometrySegments) {
320
// Get number of parts of the shape
321
int numberOfSegments = (int)segment.shape.size() - 1;
322
// If number of segments is more than 0
323
if (numberOfSegments >= 0) {
324
// Reserve memory (To improve efficiency)
325
segment.rotations.reserve(numberOfSegments);
326
segment.lengths.reserve(numberOfSegments);
327
// Calculate lengths and rotations for every shape
328
for (int i = 0; i < numberOfSegments; i++) {
329
segment.rotations.push_back(GUIGeometry::calculateRotation(segment.shape[i], segment.shape[i + 1]));
330
segment.lengths.push_back(GUIGeometry::calculateLength(segment.shape[i], segment.shape[i + 1]));
331
}
332
}
333
}
334
}
335
336
/****************************************************************************/
337
338