Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/gui/div/GLHelper.h
169685 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 GLHelper.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Sept 2002
19
///
20
// Some methods which help to draw certain geometrical objects in openGL
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <vector>
26
#include <utility>
27
#include <utils/common/RGBColor.h>
28
#include <utils/geom/PositionVector.h>
29
#include <utils/gui/settings/GUIVisualizationSettings.h>
30
31
32
// ===========================================================================
33
// class declarations
34
// ===========================================================================
35
struct GUIVisualizationTextSettings;
36
37
38
// ===========================================================================
39
// class definitions
40
// ===========================================================================
41
/**
42
* @class GLHelper
43
* @brief Some methods which help to draw certain geometrical objects in openGL
44
*
45
* This class offers some static methods for drawing primitives in openGL.
46
*/
47
class GLHelper {
48
49
public:
50
/// @brief Storage for precomputed sin/cos-values describing a circle
51
static const std::vector<std::pair<double, double> >& getCircleCoords();
52
53
/// @brief normalize angle for lookup in myCircleCoords
54
static int angleLookup(double angleDeg);
55
56
/// @brief push matrix
57
static void pushMatrix();
58
59
/// @brief pop matrix
60
static void popMatrix();
61
62
/// @brief push Name
63
static void pushName(unsigned int name);
64
65
/// @brief pop Name
66
static void popName();
67
68
/// @brief get matrix counter
69
static int getMatrixCounter();
70
71
/// @brief reset matrix counter
72
static void resetMatrixCounter();
73
74
/// @brief get vertex counter
75
static int getVertexCounter();
76
77
/// @brief reset vertex counter
78
static void resetVertexCounter();
79
80
/// @brief check counter matrix (for debug purposes)
81
static void checkCounterMatrix();
82
83
/// @brief check counter name (for debug purposes)
84
static void checkCounterName();
85
86
/** @brief Draws a filled polygon described by the list of points
87
* @note this only works well for convex polygons
88
*
89
* @param[in] v The polygon to draw
90
* @param[in] close Whether the first point shall be appended
91
*/
92
static void drawFilledPoly(const PositionVector& v, bool close);
93
94
95
/** @brief Draws a filled polygon described by the list of points
96
* @note this works for convex and concave polygons but is slower than
97
* drawFilledPoly
98
*
99
* @param[in] v The polygon to draw
100
* @param[in] close Whether the first point shall be appended
101
*/
102
static void drawFilledPolyTesselated(const PositionVector& v, bool close);
103
104
/** @brief Draws a rectangle line
105
*
106
* The line is drawn as a GL_QUADS.
107
*
108
* @param[in] center rectangle center
109
* @param[in] width The width of the rectangle
110
* @param[in] height The height of the rectangle
111
*/
112
static void drawRectangle(const Position& center, const double width, const double height);
113
114
/** @brief Draws a thick line
115
*
116
* The line is drawn as a GL_QUADS.
117
*
118
* @param[in] beg The begin position of the line
119
* @param[in] rot The direction the line shall be drawn to (in radiants)
120
* @param[in] visLength The length of the line
121
* @param[in] width The width of the line
122
* @param[in] offset The orthogonal offset
123
*/
124
static void drawBoxLine(const Position& beg, double rot,
125
double visLength, double width, double offset = 0);
126
127
128
/** @brief Draws a thick line using the mean of both given points as begin position
129
*
130
* The line is drawn as a GL_QUADS.
131
*
132
* @param[in] beg1 One of the begin positions of the line to use the mean of
133
* @param[in] beg2 One of the begin positions of the line to use the mean of
134
* @param[in] rot The direction the line shall be drawn to (in radiants)
135
* @param[in] visLength The length of the line
136
* @param[in] width The width of the line
137
*/
138
static void drawBoxLine(const Position& beg1, const Position& beg2,
139
double rot, double visLength, double width);
140
141
142
/** @brief Draws thick lines
143
*
144
* Each line is drawn using drawBoxLine.
145
*
146
* @param[in] geom The list of begin positions of the lines
147
* @param[in] rots The directions the lines shall be drawn to (in radiants)
148
* @param[in] lengths The lengths of the lines
149
* @param[in] width The width of the lines
150
* @param[in] cornerDetail Detail level for filling the corners between angled segments
151
* @param[in] the orthogonal offset
152
* @see drawBoxLine
153
*/
154
static void drawBoxLines(const PositionVector& geom,
155
const std::vector<double>& rots, const std::vector<double>& lengths,
156
double width, int cornerDetail = 0, double offset = 0);
157
158
/** @brief Draws thick lines with varying color
159
*
160
* Each line is drawn using drawBoxLine.
161
*
162
* @param[in] geom The list of begin positions of the lines
163
* @param[in] rots The directions the lines shall be drawn to (in radiants)
164
* @param[in] lengths The lengths of the lines
165
* @param[in] cols The colors of the lines
166
* @param[in] width The width of the lines
167
* @param[in] cornerDetail Detail level for filling the corners between angled segments
168
* @param[in] the orthogonal offset
169
* @see drawBoxLine
170
*/
171
static void drawBoxLines(const PositionVector& geom,
172
const std::vector<double>& rots, const std::vector<double>& lengths,
173
const std::vector<RGBColor>& cols,
174
double width, int cornerDetail = 0, double offset = 0);
175
176
177
/** @brief Draws thick lines using the mean of the points given in the point lists as begin positions
178
*
179
* Each line is drawn using drawBoxLine.
180
*
181
* @param[in] geom1 One of the lists to obtain the lines' begin positions to use the mean of
182
* @param[in] geom2 One of the lists to obtain the lines' begin positions to use the mean of
183
* @param[in] rots The directions the lines shall be drawn to (in radiants)
184
* @param[in] lengths The lengths of the lines
185
* @param[in] width The width of the lines
186
* @see drawBoxLine
187
*/
188
static void drawBoxLines(const PositionVector& geom1,
189
const PositionVector& geom2,
190
const std::vector<double>& rots, const std::vector<double>& lengths,
191
double width);
192
193
194
/** @brief Draws thick lines
195
*
196
* Widths and length are computed internally by this method, each line is then
197
* drawn using drawBoxLine.
198
*
199
* @param[in] geom The list of begin positions of the lines
200
* @param[in] width The width of the lines
201
* @see drawBoxLine
202
*/
203
static void drawBoxLines(const PositionVector& geom, double width);
204
205
206
/** @brief Draws a thin line
207
*
208
* The line is drawn as a GL_LINES.
209
*
210
* @param[in] beg The begin position of the line
211
* @param[in] rot The direction the line shall be drawn to (in radiants)
212
* @param[in] visLength The length of the line
213
*/
214
static void drawLine(const Position& beg, double rot,
215
double visLength);
216
217
218
/** @brief Draws a thin line using the mean of both given points as begin position
219
*
220
* The line is drawn as a GL_LINES.
221
*
222
* @param[in] beg1 One of the begin positions of the line to use the mean of
223
* @param[in] beg2 One of the begin positions of the line to use the mean of
224
* @param[in] rot The direction the line shall be drawn to (in radiants)
225
* @param[in] visLength The length of the line
226
*/
227
static void drawLine(const Position& beg1, const Position& beg2,
228
double rot, double visLength);
229
230
231
/** @brief Draws a thin line along the given position vector
232
*
233
* The line is drawn as a GL_LINES.
234
*
235
* @param[in] v The positions vector to use
236
*/
237
static void drawLine(const PositionVector& v);
238
239
240
/** @brief Draws a thin line along the given position vector with variable color
241
*
242
* The line is drawn as a GL_LINES.
243
*
244
* @param[in] v The positions vector to use
245
* @param[in] cols vector with the color of every line
246
* @note cols must have the same size as v
247
*/
248
static void drawLine(const PositionVector& v, const std::vector<RGBColor>& cols);
249
250
251
/** @brief Draws a thin line between the two points
252
*
253
* The line is drawn as a GL_LINES.
254
*
255
* @param[in] beg Begin of the line
256
* @param[in] end End of the line
257
*/
258
static void drawLine(const Position& beg, const Position& end);
259
260
/** @brief Draws a filled circle around (0,0) depending of level of detail
261
*
262
* The circle is drawn by calling drawFilledCircle(width, steps, 0, 360).
263
*
264
* @param[in] radius The radius of the circle
265
*/
266
static void drawFilledCircleDetailled(const GUIVisualizationSettings::Detail d, const double radius);
267
268
/** @brief Draws a filled circle around (0,0) depending of level of detail
269
*
270
* The circle is drawn by calling drawFilledCircle(width, steps, 0, 360).
271
* @param[in] beg The begin angle in degrees
272
* @param[in] end The end angle in degrees
273
*
274
* @param[in] radius The radius of the circle
275
*/
276
static void drawFilledCircleDetailled(const GUIVisualizationSettings::Detail d, const double radius,
277
double beg, double end);
278
279
/** @brief Draws a filled circle around (0,0)
280
*
281
* The circle is drawn by calling drawFilledCircle(width, steps, 0, 360).
282
*
283
* @param[in] radius The radius of the circle
284
* @param[in] steps The number of steps to divide the circle into
285
*/
286
static void drawFilledCircle(const double widradiusth, const int steps = 8);
287
288
/** @brief Draws a filled circle around (0,0)
289
*
290
* The circle is drawn use GL_TRIANGLES.
291
*
292
* @param[in] radius The radius of the circle
293
* @param[in] steps The number of steps to divide the circle into
294
* @param[in] beg The begin angle in degrees
295
* @param[in] end The end angle in degrees
296
*/
297
static void drawFilledCircle(double radius, int steps,
298
double beg, double end);
299
300
301
/** @brief Draws an unfilled circle around (0,0)
302
*
303
* The circle is drawn by calling drawOutlineCircle(width, iwidth, steps, 0, 360).
304
*
305
* @param[in] radius The (outer) radius of the circle
306
* @param[in] iRadius The inner radius of the circle
307
* @param[in] steps The number of steps to divide the circle into
308
*/
309
static void drawOutlineCircle(double radius, double iRadius,
310
int steps = 8);
311
312
313
/** @brief Draws an unfilled circle around (0,0)
314
*
315
* The circle is drawn use GL_TRIANGLES.
316
*
317
* @param[in] radius The (outer) radius of the circle
318
* @param[in] iRadius The inner radius of the circle
319
* @param[in] steps The number of steps to divide the circle into
320
* @param[in] beg The begin angle in degrees
321
* @param[in] end The end angle in degrees
322
*/
323
static void drawOutlineCircle(double radius, double iRadius,
324
int steps, double beg, double end);
325
326
327
/** @brief Draws a triangle at the end of the given line
328
*
329
* @param[in] p1 The start of the line at which end the triangle shall be drawn
330
* @param[in] p2 The end of the line at which end the triangle shall be drawn
331
* @param[in] tLength The length of the triangle
332
* @param[in] tWidth The width of the triangle
333
* @param[in] extraOffset extra offset at end
334
*/
335
static void drawTriangleAtEnd(const Position& p1, const Position& p2, double tLength,
336
double tWidth, const double extraOffset = 0);
337
338
/// @brief Sets the gl-color to this value
339
static void setColor(const RGBColor& c);
340
341
/// @brief gets the gl-color
342
static RGBColor getColor();
343
344
/// @brief get required width of text
345
static double getTextWidth(const std::string& text, double size);
346
347
/* @brief draw Text with given parameters
348
* when width is not given (negative) the font is scaled proportionally in
349
* height and width according to size.
350
*
351
* align: see foreign/fontstash/fontstash.h for flags
352
*/
353
static void drawText(const std::string& text, const Position& pos,
354
const double layer, const double size,
355
const RGBColor& col = RGBColor::BLACK,
356
const double angle = 0,
357
const int align = 0,
358
double width = -1);
359
360
static void drawTextSettings(
361
const GUIVisualizationTextSettings& settings,
362
const std::string& text, const Position& pos,
363
const double scale,
364
const double angle = 0,
365
const double layer = 2048, // GLO_MAX
366
const int align = 0); // centered
367
368
/// @brief draw Text box with given parameters
369
static void drawTextBox(const std::string& text, const Position& pos,
370
const double layer, const double size,
371
const RGBColor& txtColor = RGBColor::BLACK,
372
const RGBColor& bgColor = RGBColor::WHITE,
373
const RGBColor& borderColor = RGBColor::BLACK,
374
const double angle = 0,
375
const double relBorder = 0.05,
376
const double relMargin = 0.5,
377
const int align = 0);
378
379
/// @brief draw text and the end of shape
380
static void drawTextAtEnd(const std::string& text, const PositionVector& shape, double x,
381
const GUIVisualizationTextSettings& settings, const double scale);
382
383
/// @brief draw crossties for railroads or pedestrian crossings
384
static void drawCrossTies(const PositionVector& geom,
385
const std::vector<double>& rots,
386
const std::vector<double>& lengths,
387
double length, double spacing, double halfWidth,
388
double offset, bool lessDetail);
389
390
/// @bried draw the space between markings (in road color)
391
static void drawInverseMarkings(const PositionVector& geom,
392
const std::vector<double>& rots,
393
const std::vector<double>& lengths,
394
double maxLength, double spacing,
395
double halfWidth, bool cl, bool cr, bool lefthand, double scale);
396
397
/// @brief draw vertex numbers for the given shape (in a random color)
398
static void debugVertices(const PositionVector& shape, const GUIVisualizationTextSettings& settings, double scale, double layer = 1024);
399
400
/// @brief Draw a boundary (used for debugging)
401
static void drawBoundary(const GUIVisualizationSettings& s, const Boundary& b);
402
403
/// @brief to be called when the font context is invalidated
404
static void resetFont();
405
406
/// @brief set GL2PS
407
static void setGL2PS(bool active = true);
408
409
/// @brief draw
410
static void drawSpaceOccupancies(const double exaggeration, const Position& pos, const double rotation,
411
const double width, const double length, const bool vehicle);
412
413
private:
414
/// @brief whether the road makes a right turn (or goes straight)
415
static bool rightTurn(double angle1, double angle2);
416
417
/// @brief init myFont
418
static bool initFont();
419
420
/// @brief get dotted contour colors (black and white). Vector will be automatically increased if current size is minor than size
421
static const std::vector<RGBColor>& getDottedcontourColors(const int size);
422
423
/// @brief matrix counter (for debug purposes)
424
static int myMatrixCounter;
425
426
/// @brief matrix counter (for debug purposes)
427
static int myVertexCounter;
428
429
/// @brief matrix counter (for debug purposes)
430
static int myMatrixCounterDebug;
431
432
/// @brief name counter
433
static int myNameCounter;
434
435
/// @brief Storage for precomputed sin/cos-values describing a circle
436
static std::vector<std::pair<double, double> > myCircleCoords;
437
438
/// @brief Font context
439
static struct FONScontext* myFont;
440
static double myFontSize;
441
442
/// @brief whether we are currently rendering for gl2ps
443
static bool myGL2PSActive;
444
445
/// @brief static vector with a list of alternated black/white colors (used for contours)
446
static std::vector<RGBColor> myDottedcontourColors;
447
};
448
449