Eclipse SUMO - Simulation of Urban MObility
GLHelper.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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Some methods which help to draw certain geometrical objects in openGL
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cassert>
27 #include <utils/geom/GeomHelper.h>
28 #include <utils/common/StdDefs.h>
30 #include <utils/common/ToString.h>
31 #define FONTSTASH_IMPLEMENTATION // Expands implementation
32 #ifdef _MSC_VER
33 #pragma warning(disable: 4505) // do not warn about unused functions
34 #endif
35 #if __GNUC__ > 3
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wunused-function"
38 #endif
41 #define GLFONTSTASH_IMPLEMENTATION // Expands implementation
43 #include <utils/geom/Boundary.h>
44 #ifdef HAVE_GL2PS
45 #include <gl2ps.h>
46 #endif
47 #include "Roboto.h"
48 #include "GLHelper.h"
49 
50 #define CIRCLE_RESOLUTION (double)10 // inverse in degrees
51 
52 // ===========================================================================
53 // static member definitions
54 // ===========================================================================
55 std::vector<std::pair<double, double> > GLHelper::myCircleCoords;
56 std::vector<RGBColor> GLHelper::myDottedcontourColors;
57 FONScontext* GLHelper::myFont = nullptr;
58 double GLHelper::myFontSize = 50.0;
59 bool GLHelper::myGL2PSActive = false;
60 
61 void APIENTRY combCallback(GLdouble coords[3],
62  GLdouble* vertex_data[4],
63  GLfloat weight[4], GLdouble** dataOut) {
64  UNUSED_PARAMETER(weight);
65  UNUSED_PARAMETER(*vertex_data);
66  GLdouble* vertex;
67 
68  vertex = (GLdouble*)malloc(7 * sizeof(GLdouble));
69 
70  vertex[0] = coords[0];
71  vertex[1] = coords[1];
72  vertex[2] = coords[2];
73  *dataOut = vertex;
74 }
75 
76 // ===========================================================================
77 // method definitions
78 // ===========================================================================
79 
80 
81 void
83  if (v.size() == 0) {
84  return;
85  }
86  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
87  glBegin(GL_POLYGON);
88  for (PositionVector::const_iterator i = v.begin(); i != v.end(); i++) {
89  const Position& p = *i;
90  glVertex2d(p.x(), p.y());
91  }
92  if (close) {
93  const Position& p = *(v.begin());
94  glVertex2d(p.x(), p.y());
95  }
96  glEnd();
97 }
98 
99 
100 void
102  if (v.size() == 0) {
103  return;
104  }
105  GLUtesselator* tobj = gluNewTess();
106  gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid(APIENTRY*)()) &glVertex3dv);
107  gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid(APIENTRY*)()) &glBegin);
108  gluTessCallback(tobj, GLU_TESS_END, (GLvoid(APIENTRY*)()) &glEnd);
109  gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid(APIENTRY*)()) &combCallback);
110  gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
111  gluTessBeginPolygon(tobj, nullptr);
112  gluTessBeginContour(tobj);
113  double* points = new double[(v.size() + int(close)) * 3];
114 
115  for (int i = 0; i != (int)v.size(); ++i) {
116  points[3 * i] = v[i].x();
117  points[3 * i + 1] = v[i].y();
118  points[3 * i + 2] = 0;
119  gluTessVertex(tobj, points + 3 * i, points + 3 * i);
120  }
121  if (close) {
122  const int i = (int)v.size();
123  points[3 * i] = v[0].x();
124  points[3 * i + 1] = v[0].y();
125  points[3 * i + 2] = 0;
126  gluTessVertex(tobj, points + 3 * i, points + 3 * i);
127  }
128  gluTessEndContour(tobj);
129  gluTessEndPolygon(tobj);
130  gluDeleteTess(tobj);
131  delete[] points;
132 }
133 
134 
135 void
136 GLHelper::drawBoxLine(const Position& beg, double rot, double visLength,
137  double width, double offset) {
138  glPushMatrix();
139  glTranslated(beg.x(), beg.y(), 0);
140  glRotated(rot, 0, 0, 1);
141  glBegin(GL_QUADS);
142  glVertex2d(-width - offset, 0);
143  glVertex2d(-width - offset, -visLength);
144  glVertex2d(width - offset, -visLength);
145  glVertex2d(width - offset, 0);
146  glEnd();
147  glPopMatrix();
148 }
149 
150 
151 void
152 GLHelper::drawBoxLine(const Position& beg1, const Position& beg2,
153  double rot, double visLength,
154  double width) {
155  glPushMatrix();
156  glTranslated((beg2.x() + beg1.x())*.5, (beg2.y() + beg1.y())*.5, 0);
157  glRotated(rot, 0, 0, 1);
158  glBegin(GL_QUADS);
159  glVertex2d(-width, 0);
160  glVertex2d(-width, -visLength);
161  glVertex2d(width, -visLength);
162  glVertex2d(width, 0);
163  glEnd();
164  glPopMatrix();
165 }
166 
167 
168 bool
169 GLHelper::rightTurn(double angle1, double angle2) {
170  double delta = angle2 - angle1;
171  while (delta > 180) {
172  delta -= 360;
173  }
174  while (delta < -180) {
175  delta += 360;
176  }
177  return delta <= 0;
178 }
179 
180 
181 void
183  const std::vector<double>& rots,
184  const std::vector<double>& lengths,
185  double width, int cornerDetail, double offset) {
186  // draw the lane
187  int e = (int) geom.size() - 1;
188  for (int i = 0; i < e; i++) {
189  drawBoxLine(geom[i], rots[i], lengths[i], width, offset);
190  }
191  // draw the corner details
192  if (cornerDetail > 0) {
193  for (int i = 1; i < e; i++) {
194  glPushMatrix();
195  glTranslated(geom[i].x(), geom[i].y(), 0.1);
196  double angleBeg = -rots[i - 1];
197  double angleEnd = 180 - rots[i];
198  if (rightTurn(rots[i - 1], rots[i])) {
199  std::swap(angleBeg, angleEnd);
200  }
201  // only draw the missing piece
202  angleBeg -= 90;
203  angleEnd += 90;
204  // avoid drawing more than 360 degrees
205  if (angleEnd - angleBeg > 360) {
206  angleBeg += 360;
207  }
208  if (angleEnd - angleBeg < -360) {
209  angleEnd += 360;
210  }
211  // draw the right way around
212  if (angleEnd > angleBeg) {
213  angleEnd -= 360;
214  }
215  drawFilledCircle(width + offset, cornerDetail, angleBeg, angleEnd);
216  glPopMatrix();
217  }
218  }
219 }
220 
221 
222 void
224  const std::vector<double>& rots,
225  const std::vector<double>& lengths,
226  const std::vector<RGBColor>& cols,
227  double width, int cornerDetail, double offset) {
228  int e = (int) geom.size() - 1;
229  for (int i = 0; i < e; i++) {
230  setColor(cols[i]);
231  drawBoxLine(geom[i], rots[i], lengths[i], width, offset);
232  }
233  if (cornerDetail > 0) {
234  for (int i = 1; i < e; i++) {
235  glPushMatrix();
236  setColor(cols[i]);
237  glTranslated(geom[i].x(), geom[i].y(), 0);
238  drawFilledCircle(width, cornerDetail);
239  glEnd();
240  glPopMatrix();
241  }
242  }
243 }
244 
245 
246 void
248  const PositionVector& geom2,
249  const std::vector<double>& rots,
250  const std::vector<double>& lengths,
251  double width) {
252  int minS = (int) MIN4(rots.size(), lengths.size(), geom1.size(), geom2.size());
253  for (int i = 0; i < minS; i++) {
254  GLHelper::drawBoxLine(geom1[i], geom2[i], rots[i], lengths[i], width);
255  }
256 }
257 
258 
259 void
260 GLHelper::drawBoxLines(const PositionVector& geom, double width) {
261  int e = (int) geom.size() - 1;
262  for (int i = 0; i < e; i++) {
263  const Position& f = geom[i];
264  const Position& s = geom[i + 1];
265  drawBoxLine(f,
266  RAD2DEG(atan2((s.x() - f.x()), (f.y() - s.y()))),
267  f.distanceTo(s),
268  width);
269  }
270 }
271 
272 
273 void
274 GLHelper::drawLine(const Position& beg, double rot, double visLength) {
275  glPushMatrix();
276  glTranslated(beg.x(), beg.y(), 0);
277  glRotated(rot, 0, 0, 1);
278  glBegin(GL_LINES);
279  glVertex2d(0, 0);
280  glVertex2d(0, -visLength);
281  glEnd();
282  glPopMatrix();
283 }
284 
285 
286 void
287 GLHelper::drawLine(const Position& beg1, const Position& beg2,
288  double rot, double visLength) {
289  glPushMatrix();
290  glTranslated((beg2.x() + beg1.x())*.5, (beg2.y() + beg1.y())*.5, 0);
291  glRotated(rot, 0, 0, 1);
292  glBegin(GL_LINES);
293  glVertex2d(0, 0);
294  glVertex2d(0, -visLength);
295  glEnd();
296  glPopMatrix();
297 }
298 
299 
300 
301 void
303  glBegin(GL_LINES);
304  int e = (int) v.size() - 1;
305  for (int i = 0; i < e; ++i) {
306  glVertex2d(v[i].x(), v[i].y());
307  glVertex2d(v[i + 1].x(), v[i + 1].y());
308  }
309  glEnd();
310 }
311 
312 
313 void
314 GLHelper::drawLine(const PositionVector& v, const std::vector<RGBColor>& cols) {
315  glBegin(GL_LINES);
316  int e = (int) v.size() - 1;
317  for (int i = 0; i < e; ++i) {
318  setColor(cols[i]);
319  glVertex2d(v[i].x(), v[i].y());
320  glVertex2d(v[i + 1].x(), v[i + 1].y());
321  }
322  glEnd();
323 }
324 
325 
326 void
327 GLHelper::drawLine(const Position& beg, const Position& end) {
328  glBegin(GL_LINES);
329  glVertex2d(beg.x(), beg.y());
330  glVertex2d(end.x(), end.y());
331  glEnd();
332 }
333 
334 
335 int
336 GLHelper::angleLookup(double angleDeg) {
337  const int numCoords = (int)myCircleCoords.size() - 1;
338  int index = ((int)(floor(angleDeg * CIRCLE_RESOLUTION + 0.5))) % numCoords;
339  if (index < 0) {
340  index += numCoords;
341  }
342  assert(index >= 0);
343  return (int)index;
344 }
345 
346 
347 void
348 GLHelper::drawFilledCircle(double width, int steps) {
349  drawFilledCircle(width, steps, 0, 360);
350 }
351 
352 
353 std::vector<Position>
355  drawFilledCircle(width, steps, 0, 360);
356  std::vector<Position> result;
357  const double inc = 360 / (double)steps;
358  // obtain all vertices
359  for (int i = 0; i <= steps; ++i) {
360  const std::pair<double, double>& vertex = myCircleCoords[angleLookup(i * inc)];
361  result.push_back(Position(vertex.first * width, vertex.second * width));
362  }
363  return result;
364 }
365 
366 
367 void
368 GLHelper::drawFilledCircle(double width, int steps, double beg, double end) {
369  if (myCircleCoords.size() == 0) {
370  for (int i = 0; i <= (int)(360 * CIRCLE_RESOLUTION); ++i) {
371  const double x = (double) sin(DEG2RAD(i / CIRCLE_RESOLUTION));
372  const double y = (double) cos(DEG2RAD(i / CIRCLE_RESOLUTION));
373  myCircleCoords.push_back(std::pair<double, double>(x, y));
374  }
375  }
376  const double inc = (end - beg) / (double)steps;
377  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
378  std::pair<double, double> p1 = myCircleCoords[angleLookup(beg)];
379 
380  for (int i = 0; i <= steps; ++i) {
381  const std::pair<double, double>& p2 = myCircleCoords[angleLookup(beg + i * inc)];
382  glBegin(GL_TRIANGLES);
383  glVertex2d(p1.first * width, p1.second * width);
384  glVertex2d(p2.first * width, p2.second * width);
385  glVertex2d(0, 0);
386  glEnd();
387  p1 = p2;
388  }
389 }
390 
391 
392 void
393 GLHelper::drawOutlineCircle(double width, double iwidth, int steps) {
394  drawOutlineCircle(width, iwidth, steps, 0, 360);
395 }
396 
397 
398 void
399 GLHelper::drawOutlineCircle(double width, double iwidth, int steps,
400  double beg, double end) {
401  if (myCircleCoords.size() == 0) {
402  for (int i = 0; i < 360; i += 10) {
403  double x = (double) sin(DEG2RAD(i));
404  double y = (double) cos(DEG2RAD(i));
405  myCircleCoords.push_back(std::pair<double, double>(x, y));
406  }
407  }
408  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
409  std::pair<double, double> p1 =
410  beg == 0 ? myCircleCoords[0] : myCircleCoords[((int) beg / 10) % 36];
411  for (int i = (int)(beg / 10); i < steps && (36.0 / (double) steps * (double) i) * 10 < end; i++) {
412  const std::pair<double, double>& p2 =
413  myCircleCoords[(int)(36.0 / (double) steps * (double) i)];
414  glBegin(GL_TRIANGLES);
415  glVertex2d(p1.first * width, p1.second * width);
416  glVertex2d(p2.first * width, p2.second * width);
417  glVertex2d(p2.first * iwidth, p2.second * iwidth);
418 
419  glVertex2d(p2.first * iwidth, p2.second * iwidth);
420  glVertex2d(p1.first * iwidth, p1.second * iwidth);
421  glVertex2d(p1.first * width, p1.second * width);
422  glEnd();
423  p1 = p2;
424  }
425  const std::pair<double, double>& p2 =
426  end == 360 ? myCircleCoords[0] : myCircleCoords[((int) end / 10) % 36];
427  glBegin(GL_TRIANGLES);
428  glVertex2d(p1.first * width, p1.second * width);
429  glVertex2d(p2.first * width, p2.second * width);
430  glVertex2d(p2.first * iwidth, p2.second * iwidth);
431 
432  glVertex2d(p2.first * iwidth, p2.second * iwidth);
433  glVertex2d(p1.first * iwidth, p1.second * iwidth);
434  glVertex2d(p1.first * width, p1.second * width);
435  glEnd();
436 }
437 
438 
439 void
441  double tLength, double tWidth) {
442  const double length = p1.distanceTo(p2);
443  if (length < tLength) {
444  tWidth *= length / tLength;
445  tLength = length;
446  }
447  Position rl(PositionVector::positionAtOffset(p1, p2, length - tLength));
448  glPushMatrix();
449  glTranslated(rl.x(), rl.y(), 0);
450  glRotated(-GeomHelper::naviDegree(p1.angleTo2D(p2)), 0, 0, 1);
451  glBegin(GL_TRIANGLES);
452  glVertex2d(0, tLength);
453  glVertex2d(-tWidth, 0);
454  glVertex2d(+tWidth, 0);
455  glEnd();
456  glPopMatrix();
457 }
458 
459 
460 void
461 GLHelper::drawShapeDottedContourAroundShape(const GUIVisualizationSettings& s, const int type, const PositionVector& shape, const double width) {
462  // first check that given shape isn't empty
463  if (!s.drawForSelecting && (shape.size() > 0)) {
464  // build contour using shapes of first and last lane shapes
465  PositionVector contourFront = shape;
466  // only add an contourback if width is greather of 0
467  if (width > 0) {
468  PositionVector contourback = contourFront;
469  contourFront.move2side(width);
470  contourback.move2side(-width);
471  contourback = contourback.reverse();
472  for (auto i : contourback) {
473  contourFront.push_back(i);
474  }
475  contourFront.push_back(shape.front());
476  }
477  // resample shape
478  PositionVector resampledShape = contourFront.resample(s.widthSettings.dottedContourSegmentLenght);
479  // push matrix
480  glPushMatrix();
481  // draw contour over shape
482  glTranslated(0, 0, type + 2);
483  // set custom line width
484  glLineWidth(s.widthSettings.dottedContour);
485  // draw contour
486  drawLine(resampledShape, getDottedcontourColors((int)resampledShape.size()));
487  //restore line width
488  glLineWidth(1);
489  // pop matrix
490  glPopMatrix();
491  }
492 }
493 
494 
495 void
497  // first check that given shape isn't empty
498  if (!s.drawForSelecting && (shape.size() > 0)) {
499  // close shape
500  PositionVector closedShape = shape;
501  if (closedShape.front() != closedShape.back()) {
502  closedShape.push_back(closedShape.front());
503  }
504  // resample junction shape
505  PositionVector resampledShape = closedShape.resample(s.widthSettings.dottedContourSegmentLenght);
506  // push matrix
507  glPushMatrix();
508  // draw contour over shape
509  glTranslated(0, 0, type + 0.1);
510  // set custom line width
511  glLineWidth(s.widthSettings.dottedContour);
512  // draw contour
513  GLHelper::drawLine(resampledShape, GLHelper::getDottedcontourColors((int)resampledShape.size()));
514  //restore line width
515  glLineWidth(1);
516  // pop matrix
517  glPopMatrix();
518  }
519 }
520 
521 
522 void
523 GLHelper::drawShapeDottedContourBetweenLanes(const GUIVisualizationSettings& s, const int type, const PositionVector& frontLaneShape, const double offsetFrontLaneShape, const PositionVector& backLaneShape, const double offsetBackLaneShape) {
524  // first check that given shape isn't empty
525  if (!s.drawForSelecting && (frontLaneShape.size() > 0) && (backLaneShape.size() > 0)) {
526  // build contour using shapes of first and last lane shapes
527  PositionVector contourFront = frontLaneShape;
528  PositionVector contourback = backLaneShape;
529  contourFront.move2side(offsetFrontLaneShape);
530  contourback.move2side(offsetBackLaneShape);
531  contourback = contourback.reverse();
532  for (auto i : contourback) {
533  contourFront.push_back(i);
534  }
535  contourFront.push_back(frontLaneShape.front());
536  // resample shape
537  PositionVector resampledShape = contourFront.resample(s.widthSettings.dottedContourSegmentLenght);
538  // push matrix
539  glPushMatrix();
540  // draw contour over shape
541  glTranslated(0, 0, type + 2);
542  // set custom line width
543  glLineWidth(s.widthSettings.dottedContour);
544  // draw contour
545  GLHelper::drawLine(resampledShape, getDottedcontourColors((int)resampledShape.size()));
546  //restore line width
547  glLineWidth(1);
548  // pop matrix
549  glPopMatrix();
550  }
551 }
552 
553 
554 void
555 GLHelper::drawShapeDottedContourRectangle(const GUIVisualizationSettings& s, const int type, const Position& center, const double width, const double height, const double rotation, const double offsetX, const double offsetY) {
556  // first check that given width and height is valid
557  if (!s.drawForSelecting && (width > 0) && (height > 0)) {
558  // create shaperectangle around center
559  PositionVector shape;
560  shape.push_back(Position(width / 2, height / 2));
561  shape.push_back(Position(width / -2, height / 2));
562  shape.push_back(Position(width / -2, height / -2));
563  shape.push_back(Position(width / 2, height / -2));
564  shape.push_back(Position(width / 2, height / 2));
565  // resample shape
567  // push matrix
568  glPushMatrix();
569  // translate to center
570  glTranslated(center.x(), center.y(), type + 2);
571  // set custom line width
572  glLineWidth(3);
573  // rotate
574  glRotated(rotation, 0, 0, 1);
575  // translate offset
576  glTranslated(offsetX, offsetY, 0);
577  // draw contour
578  GLHelper::drawLine(shape, getDottedcontourColors((int)shape.size()));
579  //restore line width
580  glLineWidth(1);
581  // pop matrix
582  glPopMatrix();
583  }
584 }
585 
586 
587 void
588 GLHelper::drawShapeDottedContourPartialShapes(const GUIVisualizationSettings& s, const int type, const Position& begin, const Position& end, const double width) {
589  // check that both positions are valid and differents
590  if (!s.drawForSelecting && (begin != Position::INVALID) && (end != Position::INVALID) && (begin != end)) {
591  // calculate and resample shape
592  PositionVector shape{begin, end};
593  shape.move2side(width);
594  shape = shape.resample(s.widthSettings.dottedContourSegmentLenght);
595  // push matrix
596  glPushMatrix();
597  // draw contour over shape
598  glTranslated(0, 0, type + 0.1);
599  // set custom line width
600  glLineWidth(s.widthSettings.dottedContour);
601  // draw contour
602  GLHelper::drawLine(shape, GLHelper::getDottedcontourColors((int)shape.size()));
603  // move shape to other side
604  shape.move2side(width * -2);
605  // draw contour
606  GLHelper::drawLine(shape, GLHelper::getDottedcontourColors((int)shape.size()));
607  //restore line width
608  glLineWidth(1);
609  // pop matrix
610  glPopMatrix();
611  }
612 }
613 
614 
615 void
617  glColor4ub(c.red(), c.green(), c.blue(), c.alpha());
618 }
619 
620 
621 RGBColor
623  GLdouble current[4];
624  glGetDoublev(GL_CURRENT_COLOR, current);
625  return RGBColor(static_cast<unsigned char>(current[0] * 255. + 0.5),
626  static_cast<unsigned char>(current[1] * 255. + 0.5),
627  static_cast<unsigned char>(current[2] * 255. + 0.5),
628  static_cast<unsigned char>(current[3] * 255. + 0.5));
629 }
630 
631 
632 void
635  myFont = nullptr;
636 }
637 
638 
639 bool
641  if (myFont == nullptr) {
643  if (myFont != nullptr) {
645  fonsSetFont(myFont, fontNormal);
646  fonsSetSize(myFont, (float)myFontSize);
647  }
648  }
649  return myFont != nullptr;
650 }
651 
652 
653 const std::vector<RGBColor>&
655  // check if more colors has to be added
656  while ((int)myDottedcontourColors.size() < size) {
659  } else {
661  }
662  }
663  return myDottedcontourColors;
664 }
665 
666 
667 void
668 GLHelper::drawText(const std::string& text, const Position& pos,
669  const double layer, const double size,
670  const RGBColor& col, const double angle, const int align,
671  double width) {
672  if (width <= 0) {
673  width = size;
674  }
675  if (!initFont()) {
676  return;
677  }
678  glPushMatrix();
679  glAlphaFunc(GL_GREATER, 0.5);
680  glEnable(GL_ALPHA_TEST);
681 #ifdef HAVE_GL2PS
682  if (myGL2PSActive) {
683  glRasterPos3d(pos.x(), pos.y(), layer);
684  GLfloat color[] = {col.red() / 255.f, col.green() / 255.f, col.blue() / 255.f, col.alpha() / 255.f};
685  gl2psTextOptColor(text.c_str(), "Roboto", 10, align == 0 ? GL2PS_TEXT_C : align, (GLfloat) - angle, color);
686  glPopMatrix();
687  return;
688  }
689 #endif
690  glTranslated(pos.x(), pos.y(), layer);
691  glScaled(width / myFontSize, size / myFontSize, 1.);
692  glRotated(-angle, 0, 0, 1);
693  fonsSetAlign(myFont, align == 0 ? FONS_ALIGN_CENTER | FONS_ALIGN_MIDDLE : align);
694  fonsSetColor(myFont, glfonsRGBA(col.red(), col.green(), col.blue(), col.alpha()));
695  fonsDrawText(myFont, 0., 0., text.c_str(), nullptr);
696  glPopMatrix();
697 }
698 
699 
700 void
702  const GUIVisualizationTextSettings& settings,
703  const std::string& text, const Position& pos,
704  const double scale,
705  const double angle,
706  const double layer) {
707  drawTextBox(text, pos, layer,
708  settings.scaledSize(scale),
709  settings.color,
710  settings.bgColor,
712  angle, 0, 0.2);
713 }
714 
715 
716 void
717 GLHelper::drawTextBox(const std::string& text, const Position& pos,
718  const double layer, const double size,
719  const RGBColor& txtColor, const RGBColor& bgColor, const RGBColor& borderColor,
720  const double angle,
721  const double relBorder,
722  const double relMargin) {
723  if (!initFont()) {
724  return;
725  };
726  if (bgColor.alpha() != 0) {
727  const double boxAngle = 90;
728  const double stringWidth = size / myFontSize * fonsTextBounds(myFont, 0, 0, text.c_str(), nullptr, nullptr);
729  const double borderWidth = size * relBorder;
730  const double boxHeight = size * (0.32 + 0.6 * relMargin);
731  const double boxWidth = stringWidth + size * relMargin;
732  glPushMatrix();
733  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
734  glTranslated(pos.x(), pos.y(), layer);
735  glRotated(-angle, 0, 0, 1);
736  Position left(-boxWidth * 0.5, 0);
737  setColor(borderColor);
738  drawBoxLine(left, boxAngle, boxWidth, boxHeight);
739  left.add(borderWidth * 1.5, 0);
740  setColor(bgColor);
741  glTranslated(0, 0, 0.01);
742  drawBoxLine(left, boxAngle, boxWidth - 3 * borderWidth, boxHeight - 2 * borderWidth);
743  glPopMatrix();
744  }
745  drawText(text, pos, layer + 0.02, size, txtColor, angle);
746 }
747 
748 
749 void
750 GLHelper::drawTextAtEnd(const std::string& text, const PositionVector& shape, double x, double size, RGBColor color) {
751  glPushMatrix();
752  const Position& end = shape.back();
753  const Position& f = shape[-2];
754  const double rot = RAD2DEG(atan2((end.x() - f.x()), (f.y() - end.y())));
755  glTranslated(end.x(), end.y(), 0);
756  glRotated(rot, 0, 0, 1);
757  GLHelper::drawText(text, Position(x, 0.26), 0, .6 * size / 50, color, 180);
758  glPopMatrix();
759 }
760 
761 
762 void
764  const std::vector<double>& rots,
765  const std::vector<double>& lengths,
766  double length, double spacing,
767  double halfWidth, bool drawForSelecting) {
768  glPushMatrix();
769  // draw on top of of the white area between the rails
770  glTranslated(0, 0, 0.1);
771  int e = (int) geom.size() - 1;
772  for (int i = 0; i < e; ++i) {
773  glPushMatrix();
774  glTranslated(geom[i].x(), geom[i].y(), 0.0);
775  glRotated(rots[i], 0, 0, 1);
776  // draw crossing depending if isn't being drawn for selecting
777  if (!drawForSelecting) {
778  for (double t = 0; t < lengths[i]; t += spacing) {
779  glBegin(GL_QUADS);
780  glVertex2d(-halfWidth, -t);
781  glVertex2d(-halfWidth, -t - length);
782  glVertex2d(halfWidth, -t - length);
783  glVertex2d(halfWidth, -t);
784  glEnd();
785  }
786  } else {
787  // only draw a single rectangle if it's being drawn only for selecting
788  glBegin(GL_QUADS);
789  glVertex2d(-halfWidth, 0);
790  glVertex2d(-halfWidth, -lengths.back());
791  glVertex2d(halfWidth, -lengths.back());
792  glVertex2d(halfWidth, 0);
793  glEnd();
794  }
795  // pop three draw matrix
796  glPopMatrix();
797  }
798  glPopMatrix();
799 }
800 
801 
802 void
803 GLHelper::debugVertices(const PositionVector& shape, double size, double layer) {
804  RGBColor color = RGBColor::randomHue();
805  for (int i = 0; i < (int)shape.size(); ++i) {
806  GLHelper::drawText(toString(i), shape[i], layer, size, color, 0);
807  }
808 }
809 
810 
811 void
813  glPushMatrix();
815  // draw on top
816  glTranslated(0, 0, 1024);
817  drawLine(Position(b.xmin(), b.ymax()), Position(b.xmax(), b.ymax()));
818  drawLine(Position(b.xmax(), b.ymax()), Position(b.xmax(), b.ymin()));
819  drawLine(Position(b.xmax(), b.ymin()), Position(b.xmin(), b.ymin()));
820  drawLine(Position(b.xmin(), b.ymin()), Position(b.xmin(), b.ymax()));
821  glPopMatrix();
822 }
823 
824 
825 /****************************************************************************/
FONS_DEF void fonsSetAlign(FONScontext *s, int align)
static std::vector< std::pair< double, double > > myCircleCoords
Storage for precomputed sin/cos-values describing a circle.
Definition: GLHelper.h:369
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
static RGBColor randomHue(double s=1, double v=1)
Return color with random hue.
Definition: RGBColor.cpp:328
static void resetFont()
to be called when the font context is invalidated
Definition: GLHelper.cpp:633
FONS_DEF float fonsDrawText(FONScontext *s, float x, float y, const char *string, const char *end)
static void drawBoxLines(const PositionVector &geom, const std::vector< double > &rots, const std::vector< double > &lengths, double width, int cornerDetail=0, double offset=0)
Draws thick lines.
Definition: GLHelper.cpp:182
static void drawTextBox(const std::string &text, const Position &pos, const double layer, const double size, const RGBColor &txtColor=RGBColor::BLACK, const RGBColor &bgColor=RGBColor::WHITE, const RGBColor &borderColor=RGBColor::BLACK, const double angle=0, const double relBorder=0.05, const double relMargin=0.5)
draw Text box with given parameters
Definition: GLHelper.cpp:717
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:127
static void drawTextAtEnd(const std::string &text, const PositionVector &shape, double x, double size, RGBColor color)
draw text and the end of shape
Definition: GLHelper.cpp:750
static const RGBColor WHITE
Definition: RGBColor.h:197
static bool initFont()
init myFont
Definition: GLHelper.cpp:640
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:83
T MIN4(T a, T b, T c, T d)
Definition: StdDefs.h:101
Stores the information about how to visualize structures.
PositionVector resample(double maxLength) const
resample shape with the given number of points (equal spacing)
static void debugVertices(const PositionVector &shape, double size, double layer=256)
draw vertex numbers for the given shape (in a random color)
Definition: GLHelper.cpp:803
RGBColor bgColor
background text color
double y() const
Returns the y-position.
Definition: Position.h:62
static void drawTextSettings(const GUIVisualizationTextSettings &settings, const std::string &text, const Position &pos, const double scale, const double angle=0, const double layer=2048)
Definition: GLHelper.cpp:701
double x() const
Returns the x-position.
Definition: Position.h:57
static void drawText(const std::string &text, const Position &pos, const double layer, const double size, const RGBColor &col=RGBColor::BLACK, const double angle=0, int align=0, double width=-1)
Definition: GLHelper.cpp:668
void glfonsDelete(FONScontext *ctx)
FONS_DEF void fonsSetFont(FONScontext *s, int font)
double angleTo2D(const Position &other) const
returns the angle in the plane of the vector pointing from here to the other position ...
Definition: Position.h:254
unsigned int glfonsRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
static void drawFilledPoly(const PositionVector &v, bool close)
Draws a filled polygon described by the list of points.
Definition: GLHelper.cpp:82
PositionVector reverse() const
reverse position vector
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:76
#define RAD2DEG(x)
Definition: GeomHelper.h:39
static const RGBColor BLACK
Definition: RGBColor.h:198
static void drawFilledCircle(double width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:348
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static bool myGL2PSActive
whether we are currently rendering for gl2ps
Definition: GLHelper.h:376
static void drawShapeDottedContourBetweenLanes(const GUIVisualizationSettings &s, const int type, const PositionVector &frontLaneShape, const double offsetFrontLaneShape, const PositionVector &backLaneShape, const double offsetBackLaneShape)
draw a dotted contour around the given lane shapes
Definition: GLHelper.cpp:523
static void drawFilledPolyTesselated(const PositionVector &v, bool close)
Draws a filled polygon described by the list of points.
Definition: GLHelper.cpp:101
static void drawBoundary(const Boundary &b)
Draw a boundary (used for debugging)
Definition: GLHelper.cpp:812
static double naviDegree(const double angle)
Definition: GeomHelper.cpp:194
GUIVisualizationWidthSettings widthSettings
width settings
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:616
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
double scaledSize(double scale, double constFactor=0.1) const
get scale size
void move2side(double amount, double maxExtension=100)
move position vector to side using certain ammount
static std::vector< Position > drawFilledCircleReturnVertices(double width, int steps=8)
Draws a filled circle around (0,0) returning circle vertex.
Definition: GLHelper.cpp:354
static double myFontSize
Definition: GLHelper.h:373
static int angleLookup(double angleDeg)
normalize angle for lookup in myCircleCoords
Definition: GLHelper.cpp:336
static const RGBColor MAGENTA
Definition: RGBColor.h:195
static void drawShapeDottedContourAroundClosedShape(const GUIVisualizationSettings &s, const int type, const PositionVector &shape)
draw a dotted contour around the given closed shape with certain width
Definition: GLHelper.cpp:496
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
static void drawOutlineCircle(double width, double iwidth, int steps=8)
Draws an unfilled circle around (0,0)
Definition: GLHelper.cpp:393
static void drawLine(const Position &beg, double rot, double visLength)
Draws a thin line.
Definition: GLHelper.cpp:274
#define DEG2RAD(x)
Definition: GeomHelper.h:38
static void drawShapeDottedContourAroundShape(const GUIVisualizationSettings &s, const int type, const PositionVector &shape, const double width)
draw a dotted contour around the given Non closed shape with certain width
Definition: GLHelper.cpp:461
static struct FONScontext * myFont
Font context.
Definition: GLHelper.h:372
FONS_DEF void fonsSetColor(FONScontext *s, unsigned int color)
static void drawShapeDottedContourRectangle(const GUIVisualizationSettings &s, const int type, const Position &center, const double width, const double height, const double rotation=0, const double offsetX=0, const double offsetY=0)
draw a dotted contour around the given Position with certain width and height
Definition: GLHelper.cpp:555
void APIENTRY combCallback(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **dataOut)
Definition: GLHelper.cpp:61
FONS_DEF void fonsSetSize(FONScontext *s, float size)
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:69
unsigned char data_font_Roboto_Medium_ttf[]
Definition: Roboto.h:18
static void drawCrossTies(const PositionVector &geom, const std::vector< double > &rots, const std::vector< double > &lengths, double length, double spacing, double halfWidth, bool drawForSelecting)
draw crossties for railroads or pedestrian crossings
Definition: GLHelper.cpp:763
static bool rightTurn(double angle1, double angle2)
whether the road makes a right turn (or goes straight)
Definition: GLHelper.cpp:169
static std::vector< RGBColor > myDottedcontourColors
static vector with a list of alternated black/white colors (used for contourns)
Definition: GLHelper.h:379
#define CIRCLE_RESOLUTION
Definition: GLHelper.cpp:50
static const float dottedContour
width of dotted contours (note: must be float)
static void drawShapeDottedContourPartialShapes(const GUIVisualizationSettings &s, const int type, const Position &begin, const Position &end, const double width)
draw a dotted contour in a partial shapes
Definition: GLHelper.cpp:588
unsigned int data_font_Roboto_Medium_ttf_len
Definition: Roboto.h:14359
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:62
FONScontext * glfonsCreate(int width, int height, int flags)
static void drawTriangleAtEnd(const Position &p1, const Position &p2, double tLength, double tWidth)
Draws a triangle at the end of the given line.
Definition: GLHelper.cpp:440
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:234
FONS_DEF int fonsAddFontMem(FONScontext *s, const char *name, unsigned char *data, int ndata, int freeData)
struct FONScontext FONScontext
Definition: fontstash.h:95
static void drawBoxLine(const Position &beg, double rot, double visLength, double width, double offset=0)
Draws a thick line.
Definition: GLHelper.cpp:136
static const RGBColor INVISIBLE
Definition: RGBColor.h:200
bool drawForSelecting
whether drawing is performed for the purpose of selecting objects
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
FONS_DEF float fonsTextBounds(FONScontext *s, float x, float y, const char *string, const char *end, float *bounds)
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:285
static const double dottedContourSegmentLenght
lenght of dotted contour segments
static const std::vector< RGBColor > & getDottedcontourColors(const int size)
get dotted contour colors (black and white). Vector will be automatically increased if current size i...
Definition: GLHelper.cpp:654
static RGBColor getColor()
gets the gl-color
Definition: GLHelper.cpp:622