SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GLHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Some methods which help to draw certain geometrical objects in openGL
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "GLHelper.h"
34 #include <utils/geom/GeomHelper.h>
35 #include <utils/common/StdDefs.h>
38 
39 #ifdef CHECK_MEMORY_LEAKS
40 #include <foreign/nvwa/debug_new.h>
41 #endif // CHECK_MEMORY_LEAKS
42 
43 
44 // ===========================================================================
45 // static member definitions
46 // ===========================================================================
47 std::vector<std::pair<SUMOReal, SUMOReal> > GLHelper::myCircleCoords;
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 void
55  if (v.size() == 0) {
56  return;
57  }
58  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
59  glBegin(GL_POLYGON);
60  for (PositionVector::const_iterator i = v.begin(); i != v.end(); i++) {
61  const Position& p = *i;
62  glVertex2d(p.x(), p.y());
63  }
64  if (close) {
65  const Position& p = *(v.begin());
66  glVertex2d(p.x(), p.y());
67  }
68  glEnd();
69 }
70 
71 
72 void
73 GLHelper::drawBoxLine(const Position& beg, SUMOReal rot, SUMOReal visLength,
74  SUMOReal width) {
75  glPushMatrix();
76  glTranslated(beg.x(), beg.y(), 0);
77  glRotated(rot, 0, 0, 1);
78  glBegin(GL_QUADS);
79  glVertex2d(-width, 0);
80  glVertex2d(-width, -visLength);
81  glVertex2d(width, -visLength);
82  glVertex2d(width, 0);
83  glEnd();
84  glPopMatrix();
85 }
86 
87 
88 void
89 GLHelper::drawBoxLine(const Position& beg1, const Position& beg2,
90  SUMOReal rot, SUMOReal visLength,
91  SUMOReal width) {
92  glPushMatrix();
93  glTranslated((beg2.x() + beg1.x())*.5, (beg2.y() + beg1.y())*.5, 0);
94  glRotated(rot, 0, 0, 1);
95  glBegin(GL_QUADS);
96  glVertex2d(-width, 0);
97  glVertex2d(-width, -visLength);
98  glVertex2d(width, -visLength);
99  glVertex2d(width, 0);
100  glEnd();
101  glPopMatrix();
102 }
103 
104 
105 void
107  const std::vector<SUMOReal>& rots,
108  const std::vector<SUMOReal>& lengths,
109  SUMOReal width) {
110  int e = (int) geom.size() - 1;
111  for (int i = 0; i < e; i++) {
112  drawBoxLine(geom[i], rots[i], lengths[i], width);
113  }
114 }
115 
116 
117 void
119  const PositionVector& geom2,
120  const std::vector<SUMOReal>& rots,
121  const std::vector<SUMOReal>& lengths,
122  SUMOReal width) {
123  int minS = (int) MIN4(rots.size(), lengths.size(), geom1.size(), geom2.size());
124  for (int i = 0; i < minS; i++) {
125  GLHelper::drawBoxLine(geom1[i], geom2[i], rots[i], lengths[i], width);
126  }
127 }
128 
129 
130 void
132  int e = (int) geom.size() - 1;
133  for (int i = 0; i < e; i++) {
134  const Position& f = geom[i];
135  const Position& s = geom[i + 1];
136  drawBoxLine(f,
137  RAD2DEG(atan2((s.x() - f.x()), (f.y() - s.y()))),
138  f.distanceTo(s),
139  width);
140  }
141 }
142 
143 
144 void
145 GLHelper::drawLine(const Position& beg, SUMOReal rot, SUMOReal visLength) {
146  glPushMatrix();
147  glTranslated(beg.x(), beg.y(), 0);
148  glRotated(rot, 0, 0, 1);
149  glBegin(GL_LINES);
150  glVertex2d(0, 0);
151  glVertex2d(0, -visLength);
152  glEnd();
153  glPopMatrix();
154 }
155 
156 
157 void
158 GLHelper::drawLine(const Position& beg1, const Position& beg2,
159  SUMOReal rot, SUMOReal visLength) {
160  glPushMatrix();
161  glTranslated((beg2.x() + beg1.x())*.5, (beg2.y() + beg1.y())*.5, 0);
162  glRotated(rot, 0, 0, 1);
163  glBegin(GL_LINES);
164  glVertex2d(0, 0);
165  glVertex2d(0, -visLength);
166  glEnd();
167  glPopMatrix();
168 }
169 
170 
171 
172 void
174  glBegin(GL_LINES);
175  int e = (int) v.size() - 1;
176  for (int i = 0; i < e; ++i) {
177  glVertex2d(v[i].x(), v[i].y());
178  glVertex2d(v[i + 1].x(), v[i + 1].y());
179  }
180  glEnd();
181 }
182 
183 
184 
185 void
186 GLHelper::drawLine(const Position& beg, const Position& end) {
187  glBegin(GL_LINES);
188  glVertex2d(beg.x(), beg.y());
189  glVertex2d(end.x(), end.y());
190  glEnd();
191 }
192 
193 
194 
195 void
197  drawFilledCircle(width, steps, 0, 360);
198 }
199 
200 
201 void
203  if (myCircleCoords.size() == 0) {
204  for (int i = 0; i < 360; i += 10) {
205  const SUMOReal x = (SUMOReal) sin(DEG2RAD(i));
206  const SUMOReal y = (SUMOReal) cos(DEG2RAD(i));
207  myCircleCoords.push_back(std::pair<SUMOReal, SUMOReal>(x, y));
208  }
209  }
210  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
211  std::pair<SUMOReal, SUMOReal> p1 =
212  beg == 0 ? myCircleCoords[0] : myCircleCoords[((int) beg / 10) % 36];
213  for (int i = (int)(beg / 10); i < steps && (36.0 / (SUMOReal) steps * (SUMOReal) i) * 10 < end; i++) {
214  const std::pair<SUMOReal, SUMOReal>& p2 =
215  myCircleCoords[(size_t)(36.0 / (SUMOReal) steps * (SUMOReal) i)];
216  glBegin(GL_TRIANGLES);
217  glVertex2d(p1.first * width, p1.second * width);
218  glVertex2d(p2.first * width, p2.second * width);
219  glVertex2d(0, 0);
220  glEnd();
221  p1 = p2;
222  }
223  const std::pair<SUMOReal, SUMOReal>& p2 =
224  end == 360 ? myCircleCoords[0] : myCircleCoords[((int) end / 10) % 36];
225  glBegin(GL_TRIANGLES);
226  glVertex2d(p1.first * width, p1.second * width);
227  glVertex2d(p2.first * width, p2.second * width);
228  glVertex2d(0, 0);
229  glEnd();
230 }
231 
232 
233 void
234 GLHelper::drawOutlineCircle(SUMOReal width, SUMOReal iwidth, int steps) {
235  drawOutlineCircle(width, iwidth, steps, 0, 360);
236 }
237 
238 
239 void
241  SUMOReal beg, SUMOReal end) {
242  if (myCircleCoords.size() == 0) {
243  for (int i = 0; i < 360; i += 10) {
244  SUMOReal x = (SUMOReal) sin(DEG2RAD(i));
245  SUMOReal y = (SUMOReal) cos(DEG2RAD(i));
246  myCircleCoords.push_back(std::pair<SUMOReal, SUMOReal>(x, y));
247  }
248  }
249  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
250  std::pair<SUMOReal, SUMOReal> p1 =
251  beg == 0 ? myCircleCoords[0] : myCircleCoords[((int) beg / 10) % 36];
252  for (int i = (int)(beg / 10); i < steps && (36.0 / (SUMOReal) steps * (SUMOReal) i) * 10 < end; i++) {
253  const std::pair<SUMOReal, SUMOReal>& p2 =
254  myCircleCoords[(size_t)(36.0 / (SUMOReal) steps * (SUMOReal) i)];
255  glBegin(GL_TRIANGLES);
256  glVertex2d(p1.first * width, p1.second * width);
257  glVertex2d(p2.first * width, p2.second * width);
258  glVertex2d(p2.first * iwidth, p2.second * iwidth);
259 
260  glVertex2d(p2.first * iwidth, p2.second * iwidth);
261  glVertex2d(p1.first * iwidth, p1.second * iwidth);
262  glVertex2d(p1.first * width, p1.second * width);
263  glEnd();
264  p1 = p2;
265  }
266  const std::pair<SUMOReal, SUMOReal>& p2 =
267  end == 360 ? myCircleCoords[0] : myCircleCoords[((int) end / 10) % 36];
268  glBegin(GL_TRIANGLES);
269  glVertex2d(p1.first * width, p1.second * width);
270  glVertex2d(p2.first * width, p2.second * width);
271  glVertex2d(p2.first * iwidth, p2.second * iwidth);
272 
273  glVertex2d(p2.first * iwidth, p2.second * iwidth);
274  glVertex2d(p1.first * iwidth, p1.second * iwidth);
275  glVertex2d(p1.first * width, p1.second * width);
276  glEnd();
277 }
278 
279 
280 void
282  SUMOReal tWidth) {
283  if (l.length() < tLength) {
284  tWidth = tWidth * l.length() / tLength;
285  tLength = l.length();
286  }
287  Line rl(l.getPositionAtDistance(l.length() - tLength), l.p2());
288  glPushMatrix();
289  glTranslated(rl.p1().x(), rl.p1().y(), 0);
290  glRotated(-l.atan2DegreeAngle(), 0, 0, 1);
291  glBegin(GL_TRIANGLES);
292  glVertex2d(0, -tLength);
293  glVertex2d(-tWidth, 0);
294  glVertex2d(+tWidth, 0);
295  glEnd();
296  glPopMatrix();
297 }
298 
299 
300 void
302  glColor4ub(c.red(), c.green(), c.blue(), c.alpha());
303 }
304 
305 
306 RGBColor
308  GLdouble current[4];
309  glGetDoublev(GL_CURRENT_COLOR, current);
310  return RGBColor(static_cast<unsigned char>(current[0] * 255. + 0.5),
311  static_cast<unsigned char>(current[1] * 255. + 0.5),
312  static_cast<unsigned char>(current[2] * 255. + 0.5),
313  static_cast<unsigned char>(current[3] * 255. + 0.5));
314 }
315 
316 
317 void
318 GLHelper::drawText(const std::string& text, const Position& pos,
319  const SUMOReal layer, const SUMOReal size,
320  const RGBColor& col, const SUMOReal angle) {
321  glPushMatrix();
322  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
323  setColor(col);
324  glTranslated(pos.x(), pos.y(), layer);
325  pfSetPosition(0, 0);
326  pfSetScale(size);
327  SUMOReal w = pfdkGetStringWidth(text.c_str());
328  glRotated(180, 1, 0, 0);
329  glRotated(angle, 0, 0, 1);
330  glTranslated(-w / 2., 0.4, 0);
331  pfDrawString(text.c_str());
332  glPopMatrix();
333 }
334 
335 void
336 GLHelper::drawTextBox(const std::string& text, const Position& pos,
337  const SUMOReal layer, const SUMOReal size,
338  const RGBColor& txtColor, const RGBColor& bgColor, const RGBColor& borderColor,
339  const SUMOReal angle) {
340  SUMOReal boxAngle = angle + 90;
341  if (boxAngle > 360) {
342  boxAngle -= 360;
343  }
344  pfSetScale(size);
345  const SUMOReal stringWidth = pfdkGetStringWidth(text.c_str());
346  const SUMOReal borderWidth = size / 20;
347  const SUMOReal boxHeight = size * 0.8;
348  const SUMOReal boxWidth = stringWidth + size / 2;
349  glPushMatrix();
350  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
351  glTranslated(0, 0, layer);
352  setColor(borderColor);
353  Position left = pos;
354  left.sub(boxWidth / 2, -boxHeight / 2.7);
355  drawBoxLine(left, boxAngle, boxWidth, boxHeight);
356  left.add(borderWidth * 1.5, 0);
357  setColor(bgColor);
358  glTranslated(0, 0, 0.01);
359  drawBoxLine(left, boxAngle, boxWidth - 3 * borderWidth, boxHeight - 2 * borderWidth);
360  // actually we should be able to use drawText here. however, there's
361  // something about the constant 0.4 offset which causes trouble
362  //drawText(text, pos, layer+0.02, size, txtColor, angle);
363  setColor(txtColor);
364  glTranslated(pos.x(), pos.y(), 0.01);
365  pfSetPosition(0, 0);
366  pfSetScale(size);
367  glRotated(180, 1, 0, 0);
368  glRotated(angle, 0, 0, 1);
369  glTranslated(-stringWidth / 2., 0, 0);
370  pfDrawString(text.c_str());
371  glPopMatrix();
372 }
373 
374 /****************************************************************************/
375 
void sub(SUMOReal dx, SUMOReal dy)
Substracts the given position from this one.
Definition: Position.h:139
SUMOReal atan2DegreeAngle() const
Definition: Line.cpp:143
int pfDrawString(const char *c)
Definition: polyfonts.c:1070
const Position & p2() const
Definition: Line.cpp:86
void add(const Position &pos)
Adds the given position to this one.
Definition: Position.h:119
T MIN4(T a, T b, T c, T d)
Definition: StdDefs.h:84
static void drawOutlineCircle(SUMOReal width, SUMOReal iwidth, int steps=8)
Draws an unfilled circle around (0,0)
Definition: GLHelper.cpp:234
void pfSetPosition(SUMOReal x, SUMOReal y)
Definition: polyfonts.c:476
static void drawFilledPoly(const PositionVector &v, bool close)
Draws a filled polygon described by the list of points.
Definition: GLHelper.cpp:54
#define RAD2DEG(x)
Definition: GeomHelper.h:46
SUMOReal distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:208
static void drawText(const std::string &text, const Position &pos, const SUMOReal layer, const SUMOReal size, const RGBColor &col=RGBColor::BLACK, const SUMOReal angle=0)
draw Text with given parameters
Definition: GLHelper.cpp:318
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
static void drawFilledCircle(SUMOReal width, int steps=8)
Draws a filled circle around (0,0)
Definition: GLHelper.cpp:196
Position getPositionAtDistance(SUMOReal offset) const
Definition: Line.cpp:92
static std::vector< std::pair< SUMOReal, SUMOReal > > myCircleCoords
Storage for precomputed sin/cos-values describing a circle.
Definition: GLHelper.h:255
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:301
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
Definition: Line.h:51
#define DEG2RAD(x)
Definition: GeomHelper.h:45
static void drawBoxLines(const PositionVector &geom, const std::vector< SUMOReal > &rots, const std::vector< SUMOReal > &lengths, SUMOReal width)
Draws thick lines.
Definition: GLHelper.cpp:106
static void drawTextBox(const std::string &text, const Position &pos, const SUMOReal layer, const SUMOReal size, const RGBColor &txtColor=RGBColor::BLACK, const RGBColor &bgColor=RGBColor::WHITE, const RGBColor &borderColor=RGBColor::BLACK, const SUMOReal angle=0)
draw Text box with given parameters
Definition: GLHelper.cpp:336
static void drawBoxLine(const Position &beg, SUMOReal rot, SUMOReal visLength, SUMOReal width)
Draws a thick line.
Definition: GLHelper.cpp:73
SUMOReal length() const
Definition: Line.cpp:183
void pfSetScale(SUMOReal s)
Definition: polyfonts.c:461
static void drawTriangleAtEnd(const Line &l, SUMOReal tLength, SUMOReal tWidth)
Draws a triangle at the end of the given line.
Definition: GLHelper.cpp:281
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
SUMOReal pfdkGetStringWidth(const char *c)
Definition: polyfonts.c:1109
static void drawLine(const Position &beg, SUMOReal rot, SUMOReal visLength)
Draws a thin line.
Definition: GLHelper.cpp:145
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:215
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
static RGBColor getColor()
gets the gl-color
Definition: GLHelper.cpp:307