Eclipse SUMO - Simulation of Urban MObility
GUIDottedGeometry.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 /****************************************************************************/
18 // File for dotted geometry classes and functions
19 /****************************************************************************/
20 #include <utils/geom/GeomHelper.h>
21 #include <utils/gui/div/GLHelper.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 
40  mySettings(settings),
41  myColorFlag(true) {}
42 
43 
44 const RGBColor
46  if (type == DottedContourType::INSPECT) {
47  if (myColorFlag) {
48  myColorFlag = false;
49  return mySettings.dottedContourSettings.firstInspectedColor;
50  } else {
51  myColorFlag = true;
52  return mySettings.dottedContourSettings.secondInspectedColor;
53  }
54  } else if (type == DottedContourType::FRONT) {
55  if (myColorFlag) {
56  myColorFlag = false;
57  return mySettings.dottedContourSettings.firstFrontColor;
58  } else {
59  myColorFlag = true;
60  return mySettings.dottedContourSettings.secondFrontColor;
61  }
62  } else if (type == DottedContourType::GREEN) {
63  if (myColorFlag) {
64  myColorFlag = false;
65  return RGBColor::GREEN;
66  } else {
67  myColorFlag = true;
69  }
70  } else if (type == DottedContourType::MAGENTA) {
71  if (myColorFlag) {
72  myColorFlag = false;
73  return RGBColor::MAGENTA;
74  } else {
75  myColorFlag = true;
77  }
78  } else {
79  return RGBColor::BLACK;
80  }
81 }
82 
83 
84 void
86  if (myColorFlag) {
87  myColorFlag = false;
88  } else {
89  myColorFlag = true;
90  }
91 }
92 
93 
94 void
96  myColorFlag = true;
97 }
98 
99 // ---------------------------------------------------------------------------
100 // GUIDottedGeometry::Segment - methods
101 // ---------------------------------------------------------------------------
102 
104  offset(-1) {
105 }
106 
107 
109  shape(newShape),
110  offset(-1) {
111 }
112 
113 // ---------------------------------------------------------------------------
114 // GUIDottedGeometry - methods
115 // ---------------------------------------------------------------------------
116 
118  myWidth(0) {
119 }
120 
121 
123  myWidth(s.dottedContourSettings.segmentWidth) {
124  // check if shape has to be closed
125  if (closeShape && (shape.size() > 2)) {
126  shape.closePolygon();
127  }
128  if (shape.size() > 1) {
129  // get shape
130  for (int i = 1; i < (int)shape.size(); i++) {
131  myDottedGeometrySegments.push_back(Segment({shape[i - 1], shape[i]}));
132  }
133  // calculate segment length
134  double segmentLength = s.dottedContourSettings.segmentLength;
135  if (shape.length2D() > MAXIMUM_DOTTEDGEOMETRYLENGTH) {
136  segmentLength = shape.length2D() / (MAXIMUM_DOTTEDGEOMETRYLENGTH * 0.5);
137  }
138  // resample
139  for (auto& segment : myDottedGeometrySegments) {
140  segment.shape = segment.shape.resample(segmentLength, true);
141  }
142  // calculate shape rotations and lenghts
144  }
145 }
146 
147 
149  const GUIDottedGeometry& topDottedGeometry, const bool drawFirstExtrem,
150  const GUIDottedGeometry& botDottedGeometry, const bool drawLastExtrem) :
151  myWidth(s.dottedContourSettings.segmentWidth) {
152  // check size of both geometries
153  if ((topDottedGeometry.myDottedGeometrySegments.size() > 0) &&
154  (botDottedGeometry.myDottedGeometrySegments.size() > 0)) {
155  // add extremes
156  if (drawFirstExtrem &&
157  (topDottedGeometry.myDottedGeometrySegments.front().shape.size() > 0) &&
158  (botDottedGeometry.myDottedGeometrySegments.front().shape.size() > 0)) {
159  // add first extreme
161  topDottedGeometry.myDottedGeometrySegments.front().shape.front(),
162  botDottedGeometry.myDottedGeometrySegments.front().shape.front()}));
163  }
164  if (drawLastExtrem &&
165  (topDottedGeometry.myDottedGeometrySegments.back().shape.size() > 0) &&
166  (botDottedGeometry.myDottedGeometrySegments.back().shape.size() > 0)) {
167  // add last extreme
169  topDottedGeometry.myDottedGeometrySegments.back().shape.back(),
170  botDottedGeometry.myDottedGeometrySegments.back().shape.back()}));
171  // invert offset of second dotted geometry
172  myDottedGeometrySegments.back().offset *= -1;
173  }
174  }
175  // resample
176  for (auto& segment : myDottedGeometrySegments) {
177  segment.shape = segment.shape.resample(s.dottedContourSettings.segmentLength, true);
178  }
179  // calculate shape rotations and lenghts
181 }
182 
183 
184 void
186  // update settings and width
188  // reset segments
189  myDottedGeometrySegments.clear();
190  // get shape
191  for (int i = 1; i < (int)laneShape.size(); i++) {
192  myDottedGeometrySegments.push_back(Segment({laneShape[i - 1], laneShape[i]}));
193  }
194  // resample
195  for (auto& segment : myDottedGeometrySegments) {
196  segment.shape = segment.shape.resample(s.dottedContourSettings.segmentLength, true);
197  }
198  // calculate shape rotations and lenghts
200 }
201 
202 
203 void
205  // update settings and width
207  // reset segments
208  myDottedGeometrySegments.clear();
209  // check if shape has to be closed
210  if (closeShape && (shape.size() > 2)) {
211  shape.closePolygon();
212  }
213  if (shape.size() > 1) {
214  // get shape
215  for (int i = 1; i < (int)shape.size(); i++) {
216  myDottedGeometrySegments.push_back(Segment({shape[i - 1], shape[i]}));
217  }
218  // resample
219  for (auto& segment : myDottedGeometrySegments) {
220  segment.shape = segment.shape.resample(s.dottedContourSettings.segmentLength, true);
221  }
222  // calculate shape rotations and lenghts
224  }
225 }
226 
227 
228 void
230  GUIDottedGeometry::DottedContourType type, const double customWidth) const {
231  // get width
232  const double width = (customWidth > 0) ? customWidth : myWidth;
233  // iterate over all segments
234  for (auto& segment : myDottedGeometrySegments) {
235  // iterate over shape
236  for (int i = 0; i < ((int)segment.shape.size() - 1); i++) {
237  // set color
238  GLHelper::setColor(dottedGeometryColor.getColor(type));
239  // draw box line
240  GLHelper::drawBoxLine(segment.shape[i], segment.rotations.at(i), segment.lengths.at(i), width, 0);
241  }
242  }
243 }
244 
245 
246 void
248  // move 2 side
249  for (auto& segment : myDottedGeometrySegments) {
250  segment.shape.move2side(value);
251  }
252 }
253 
254 
255 double
257  return myWidth;
258 }
259 
260 
261 void
262 GUIDottedGeometry::setWidth(const double width) {
263  myWidth = width;
264 }
265 
266 
267 void
269  // iterate over all segments
270  for (auto& segment : myDottedGeometrySegments) {
271  segment.offset *= -1;
272  }
273 }
274 
275 
276 void
278  const PositionVector& shape, const double exaggeration, const double lineWidth) {
279  if (s.drawDottedContour(exaggeration)) {
280  // declare DottedGeometryColor
281  DottedGeometryColor dottedGeometryColor(s);
282  // scale shape using exaggeration and default dotted geometry width
283  PositionVector scaledShape = shape;
284  // scale exaggeration
285  scaledShape.scaleRelative(exaggeration);
286  // calculate dotted geometry
287  GUIDottedGeometry dottedGeometry(s, scaledShape, true);
288  // Push draw matrix
290  // draw inspect or front dotted contour
291  if (type == DottedContourType::FRONT) {
292  // translate to front
293  glTranslated(0, 0, GLO_DOTTEDCONTOUR_INSPECTED);
294  } else {
295  // translate to front
296  glTranslated(0, 0, GLO_DOTTEDCONTOUR_INSPECTED);
297  }
298  // draw dotted geometry
299  dottedGeometry.drawDottedGeometry(dottedGeometryColor, type, lineWidth);
300  // pop matrix
302  }
303 }
304 
305 
306 void
308  const PositionVector& shape, const double width, const double exaggeration, const bool drawFirstExtrem,
309  const bool drawLastExtrem, const double lineWidth) {
310  if (s.drawDottedContour(exaggeration)) {
311  // declare DottedGeometryColor
312  DottedGeometryColor dottedGeometryColor(s);
313  // calculate center dotted geometry
314  GUIDottedGeometry dottedGeometry(s, shape, false);
315  // make a copy of dotted geometry
316  GUIDottedGeometry topDottedGeometry = dottedGeometry;
317  GUIDottedGeometry botDottedGeometry = dottedGeometry;
318  // move geometries top and bot
319  topDottedGeometry.moveShapeToSide(width * exaggeration);
320  botDottedGeometry.moveShapeToSide(width * exaggeration * -1);
321  // invert offset of top dotted geometry
322  topDottedGeometry.invertOffset();
323  // calculate extremes
324  GUIDottedGeometry extremes(s, topDottedGeometry, drawFirstExtrem, botDottedGeometry, drawLastExtrem);
325  // Push draw matrix
327  // translate to front
328  if (type == DottedContourType::FRONT) {
329  // translate to front
330  glTranslated(0, 0, GLO_DOTTEDCONTOUR_FRONT);
331  } else {
332  // translate to front
333  glTranslated(0, 0, GLO_DOTTEDCONTOUR_INSPECTED);
334  }
335  // draw top dotted geometry
336  topDottedGeometry.drawDottedGeometry(dottedGeometryColor, type, lineWidth);
337  // reset color
338  dottedGeometryColor.reset();
339  // draw top dotted geometry
340  botDottedGeometry.drawDottedGeometry(dottedGeometryColor, type, lineWidth);
341  // change color
342  dottedGeometryColor.changeColor();
343  // draw extrem dotted geometry
344  extremes.drawDottedGeometry(dottedGeometryColor, type, lineWidth);
345  // pop matrix
347  }
348 }
349 
350 
351 void
353  const Position& pos, const double radius, const double exaggeration) {
354  // continue depending of exaggeratedRadio
355  if ((radius * exaggeration) < 2) {
356  drawDottedContourClosedShape(type, s, GUIGeometry::getVertexCircleAroundPosition(pos, radius, 8), exaggeration);
357  } else {
358  drawDottedContourClosedShape(type, s, GUIGeometry::getVertexCircleAroundPosition(pos, radius, 16), exaggeration);
359  }
360 }
361 
362 
363 void
365  const double width, const double height, const double offsetX, const double offsetY, const double rot, const double exaggeration) {
366  if (s.drawDottedContour(exaggeration)) {
367  // create shape
368  PositionVector shape;
369  // make rectangle
370  shape.push_back(Position(0 + width, 0 + height));
371  shape.push_back(Position(0 + width, 0 - height));
372  shape.push_back(Position(0 - width, 0 - height));
373  shape.push_back(Position(0 - width, 0 + height));
374  // move shape
375  shape.add(offsetX, offsetY, 0);
376  // rotate shape
377  shape.rotate2D(DEG2RAD((rot * -1) + 90));
378  // move to position
379  shape.add(pos);
380  // draw using drawDottedContourClosedShape
381  drawDottedContourClosedShape(type, s, shape, exaggeration);
382  }
383 }
384 
385 
386 void
388  // iterate over all segments
389  for (auto& segment : myDottedGeometrySegments) {
390  // Get number of parts of the shape
391  int numberOfSegments = (int)segment.shape.size() - 1;
392  // If number of segments is more than 0
393  if (numberOfSegments >= 0) {
394  // Reserve memory (To improve efficiency)
395  segment.rotations.reserve(numberOfSegments);
396  segment.lengths.reserve(numberOfSegments);
397  // Calculate lengths and rotations for every shape
398  for (int i = 0; i < numberOfSegments; i++) {
399  segment.rotations.push_back(GUIGeometry::calculateRotation(segment.shape[i], segment.shape[i + 1]));
400  segment.lengths.push_back(GUIGeometry::calculateLength(segment.shape[i], segment.shape[i + 1]));
401  }
402  }
403  }
404 }
405 
406 /****************************************************************************/
#define MAXIMUM_DOTTEDGEOMETRYLENGTH
@ GLO_DOTTEDCONTOUR_INSPECTED
dotted contour inspected element (used in NETEDIT)
@ GLO_DOTTEDCONTOUR_FRONT
dotted contour front element (used in NETEDIT)
#define DEG2RAD(x)
Definition: GeomHelper.h:35
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:507
static void popMatrix()
pop matrix
Definition: GLHelper.cpp:123
static void drawBoxLine(const Position &beg, double rot, double visLength, double width, double offset=0)
Draws a thick line.
Definition: GLHelper.cpp:231
static void pushMatrix()
push matrix
Definition: GLHelper.cpp:114
class for pack all variables related with GUIDottedGeometry color
const RGBColor getColor(DottedContourType type)
get inspected color (and change flag)
void reset()
rest Dotted Geometry Color
DottedGeometryColor(const GUIVisualizationSettings &settings)
constructor
std::vector< GUIDottedGeometry::Segment > myDottedGeometrySegments
dotted element shape (note: It's centered in 0,0 due scaling)
double myWidth
geometry width
void drawDottedGeometry(DottedGeometryColor &dottedGeometryColor, GUIDottedGeometry::DottedContourType type, const double customWidth=-1) const
draw inspected dottedShape
static void drawDottedSquaredShape(const DottedContourType type, const GUIVisualizationSettings &s, const Position &pos, const double width, const double height, const double offsetX, const double offsetY, const double rot, const double exaggeration)
draw dotted squared contour (used by additionals and demand elements)
static void drawDottedContourCircle(const DottedContourType type, const GUIVisualizationSettings &s, const Position &pos, const double radius, const double exaggeration)
draw dotted contour for the given Position and radius (used by Juctions and POIs)
void moveShapeToSide(const double value)
move shape to side
void calculateShapeRotationsAndLengths()
calculate shape rotations and lengths
double getWidth() const
get width
static void drawDottedContourShape(const DottedContourType type, const GUIVisualizationSettings &s, const PositionVector &shape, const double width, const double exaggeration, const bool drawFirstExtrem, const bool drawLastExtrem, const double lineWidth=-1)
draw dotted contour for the given shape (used by additionals)
void updateDottedGeometry(const GUIVisualizationSettings &s, const PositionVector &laneShape)
update GUIDottedGeometry (using lane shape)
void invertOffset()
invert offset of all segments
void setWidth(const double width)
change default width
static void drawDottedContourClosedShape(const DottedContourType type, const GUIVisualizationSettings &s, const PositionVector &shape, const double exaggeration, const double lineWidth=-1)
draw dotted contour for the given closed shape (used by Juctions, shapes and TAZs)
GUIDottedGeometry()
constructor
static PositionVector getVertexCircleAroundPosition(const Position &pos, const double width, const int steps=8)
get a circle around the given position
static double calculateRotation(const Position &first, const Position &second)
return angle between two points (used in geometric calculations)
static double calculateLength(const Position &first, const Position &second)
return length between two points (used in geometric calculations)
Stores the information about how to visualize structures.
bool drawDottedContour(const double exaggeration) const
check if dotted contour can be drawn
GUIVisualizationDottedContourSettings dottedContourSettings
dotted contour settings
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
double length2D() const
Returns the length.
void rotate2D(double angle)
void add(double xoff, double yoff, double zoff)
void closePolygon()
ensures that the last position equals the first
void scaleRelative(double factor)
enlarges/shrinks the polygon by a factor based at the centroid
static const RGBColor GREEN
Definition: RGBColor.h:186
static const RGBColor BLACK
Definition: RGBColor.h:193
RGBColor changedBrightness(int change, int toChange=3) const
Returns a new color with altered brightness.
Definition: RGBColor.cpp:197
static const RGBColor MAGENTA
Definition: RGBColor.h:190
dotted geometry segment
Segment()
default constructor
static const double segmentWidth
width of dotted contour segments
static const double segmentLength
length of dotted contour segments