Eclipse SUMO - Simulation of Urban MObility
GUIPolygon.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-2020 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 /****************************************************************************/
21 // The GUI-version of a polygon
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <string>
26 #include <utils/geom/GeomHelper.h>
33 #include <utils/gui/div/GLHelper.h>
35 
36 #include "GUIPolygon.h"
37 
38 //#define GUIPolygon_DEBUG_DRAW_VERTICES
39 
40 // ===========================================================================
41 // callbacks definitions
42 // ===========================================================================
43 
44 void APIENTRY beginCallback(GLenum which) {
45  glBegin(which);
46 }
47 
48 
49 void APIENTRY errorCallback(GLenum errorCode) {
50  const GLubyte* estring;
51 
52  estring = gluErrorString(errorCode);
53  fprintf(stderr, "Tessellation Error: %s\n", estring);
54  exit(0);
55 }
56 
57 
58 void APIENTRY endCallback(void) {
59  glEnd();
60 }
61 
62 
63 void APIENTRY vertexCallback(GLvoid* vertex) {
64  glVertex3dv((GLdouble*) vertex);
65 }
66 
67 
68 void APIENTRY combineCallback(GLdouble coords[3],
69  GLdouble* vertex_data[4],
70  GLfloat weight[4], GLdouble** dataOut) {
71  UNUSED_PARAMETER(weight);
72  UNUSED_PARAMETER(*vertex_data);
73  GLdouble* vertex;
74 
75  vertex = (GLdouble*) malloc(7 * sizeof(GLdouble));
76 
77  vertex[0] = coords[0];
78  vertex[1] = coords[1];
79  vertex[2] = coords[2];
80  *dataOut = vertex;
81 }
82 
83 GLfloat INV_POLY_TEX_DIM = 1.0 / 256.0;
84 GLfloat xPlane[] = {INV_POLY_TEX_DIM, 0.0, 0.0, 0.0};
85 GLfloat yPlane[] = {0.0, INV_POLY_TEX_DIM, 0.0, 0.0};
86 
87 // ===========================================================================
88 // method definitions
89 // ===========================================================================
90 
91 GUIPolygon::GUIPolygon(const std::string& id, const std::string& type,
92  const RGBColor& color, const PositionVector& shape, bool geo,
93  bool fill, double lineWidth, double layer, double angle, const std::string& imgFile,
94  bool relativePath):
95  SUMOPolygon(id, type, color, shape, geo, fill, lineWidth, layer, angle, imgFile, relativePath),
97  myDisplayList(0),
98  myRotatedShape(nullptr) {
99  if (angle != 0.) {
100  setShape(shape);
101  }
102 }
103 
104 
106  delete myRotatedShape;
107 }
108 
109 
112  GUISUMOAbstractView& parent) {
113  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
114  buildPopupHeader(ret, app, false);
115  GUIDesigns::buildFXMenuCommand(ret, "(" + getShapeType() + ")", nullptr, nullptr, 0);
116  new FXMenuSeparator(ret);
120  buildShowParamsPopupEntry(ret, false);
121  buildPositionCopyEntry(ret, false);
122  return ret;
123 }
124 
125 
129  GUIParameterTableWindow* ret = new GUIParameterTableWindow(app, *this);
130  // add items
131  ret->mkItem("type", false, getShapeType());
132  ret->mkItem("layer", false, toString(getShapeLayer()));
133  ret->closeBuilding(this);
134  return ret;
135 }
136 
137 
138 Boundary
140  const PositionVector& shape = myRotatedShape != nullptr ? *myRotatedShape : myShape;
141  Boundary b;
142  b.add(shape.getBoxBoundary());
143  b.grow(2);
144  return b;
145 }
146 
147 
148 void
150  // first check if polygon can be drawn
151  if (checkDraw(s, this, this)) {
152  FXMutexLock locker(myLock);
153  //if (myDisplayList == 0 || (!getFill() && myLineWidth != s.polySize.getExaggeration(s))) {
154  // storeTesselation(s.polySize.getExaggeration(s));
155  //}
156  // push name (needed for getGUIGlObjectsUnderCursor(...)
157  glPushName(getGlID());
158  // draw inner polygon
159  if (myRotatedShape) {
160  drawInnerPolygon(s, this, this, *myRotatedShape, getShapeLayer(), false);
161  } else {
162  drawInnerPolygon(s, this, this, myShape, getShapeLayer(), false);
163  }
164  // pop name
165  glPopName();
166  }
167 }
168 
169 
170 void
172  FXMutexLock locker(myLock);
173  SUMOPolygon::setShape(shape);
174  if (getShapeNaviDegree() != 0.) {
175  if (myRotatedShape == nullptr) {
177  }
178  const Position& centroid = myShape.getCentroid();
180  myRotatedShape->sub(centroid);
182  myRotatedShape->add(centroid);
183  } else {
184  delete myRotatedShape;
185  myRotatedShape = nullptr;
186  }
187  //storeTesselation(myLineWidth);
188 }
189 
190 
191 void
192 GUIPolygon::performTesselation(const bool fill, const PositionVector& shape, const double lineWidth) {
193  if (fill) {
194  // draw the tesselated shape
195  double* points = new double[shape.size() * 3];
196  GLUtesselator* tobj = gluNewTess();
197  gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid(APIENTRY*)()) &glVertex3dv);
198  gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid(APIENTRY*)()) &beginCallback);
199  gluTessCallback(tobj, GLU_TESS_END, (GLvoid(APIENTRY*)()) &endCallback);
200  //gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (APIENTRY*) ()) &errorCallback);
201  gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid(APIENTRY*)()) &combineCallback);
202  gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
203  gluTessBeginPolygon(tobj, nullptr);
204  gluTessBeginContour(tobj);
205  for (int i = 0; i != (int)shape.size(); ++i) {
206  points[3 * i] = shape[(int) i].x();
207  points[3 * i + 1] = shape[(int) i].y();
208  points[3 * i + 2] = 0;
209  gluTessVertex(tobj, points + 3 * i, points + 3 * i);
210  }
211  gluTessEndContour(tobj);
212 
213  gluTessEndPolygon(tobj);
214  gluDeleteTess(tobj);
215  delete[] points;
216 
217  } else {
218  GLHelper::drawLine(shape);
219  GLHelper::drawBoxLines(shape, lineWidth);
220  }
221 }
222 
223 
224 void
225 GUIPolygon::storeTesselation(const bool fill, const PositionVector& shape, double lineWidth) const {
226  if (myDisplayList > 0) {
227  glDeleteLists(myDisplayList, 1);
228  }
229  myDisplayList = glGenLists(1);
230  if (myDisplayList == 0) {
231  throw ProcessError("GUIPolygon::storeTesselation() could not create display list");
232  }
233  glNewList(myDisplayList, GL_COMPILE);
234  performTesselation(fill, shape, lineWidth);
235  glEndList();
236 }
237 
238 
239 void
240 GUIPolygon::setColor(const GUIVisualizationSettings& s, const SUMOPolygon* polygon, const GUIGlObject* o, bool disableSelectionColor) {
241  const GUIColorer& c = s.polyColorer;
242  const int active = c.getActive();
243  if (s.netedit && active != 1 && gSelected.isSelected(GLO_POLYGON, o->getGlID()) && disableSelectionColor) {
244  // override with special selection colors (unless the color scheme is based on selection)
245  GLHelper::setColor(RGBColor(0, 0, 204));
246  } else if (active == 0) {
247  GLHelper::setColor(polygon->getShapeColor());
248  } else if (active == 1) {
250  } else {
252  }
253 }
254 
255 
256 bool
258  if (s.polySize.getExaggeration(s, o) == 0) {
259  return false;
260  }
261  Boundary boundary = polygon->getShape().getBoxBoundary();
262  if (s.scale * MAX2(boundary.getWidth(), boundary.getHeight()) < s.polySize.minSize) {
263  return false;
264  }
265  if (polygon->getFill()) {
266  if (polygon->getShape().size() < 3) {
267  return false;
268  }
269  } else {
270  if (polygon->getShape().size() < 2) {
271  return false;
272  }
273  }
274  return true;
275 }
276 
277 
278 void
280  const PositionVector shape, double layer, bool disableSelectionColor) {
281  glPushMatrix();
282  glTranslated(0, 0, layer);
283  setColor(s, polygon, o, disableSelectionColor);
284  int textureID = -1;
285  if (polygon->getFill()) {
286  const std::string& file = polygon->getShapeImgFile();
287  if (file != "") {
288  textureID = GUITexturesHelper::getTextureID(file, true);
289  }
290  }
291  // init generation of texture coordinates
292  if (textureID >= 0) {
293  glEnable(GL_TEXTURE_2D);
294  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
295  glDisable(GL_CULL_FACE);
296  glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
297  glDisable(GL_LIGHTING);
298  glDisable(GL_COLOR_MATERIAL);
299  glDisable(GL_ALPHA_TEST);
300  glEnable(GL_BLEND);
301  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
302  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
303  glBindTexture(GL_TEXTURE_2D, textureID);
304  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
305  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
306  // http://www.gamedev.net/topic/133564-glutesselation-and-texture-mapping/
307  glEnable(GL_TEXTURE_GEN_S);
308  glEnable(GL_TEXTURE_GEN_T);
309  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
310  glTexGenfv(GL_S, GL_OBJECT_PLANE, xPlane);
311  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
312  glTexGenfv(GL_T, GL_OBJECT_PLANE, yPlane);
313  }
314  // recall tesselation
315  //glCallList(myDisplayList);
316  performTesselation(polygon->getFill(), shape, polygon->getLineWidth() * s.polySize.getExaggeration(s, o));
317  // de-init generation of texture coordinates
318  if (textureID >= 0) {
319  glEnable(GL_DEPTH_TEST);
320  glBindTexture(GL_TEXTURE_2D, 0);
321  glDisable(GL_TEXTURE_2D);
322  glDisable(GL_TEXTURE_GEN_S);
323  glDisable(GL_TEXTURE_GEN_T);
324  }
325 #ifdef GUIPolygon_DEBUG_DRAW_VERTICES
326  GLHelper::debugVertices(shape, 80 / s.scale);
327 #endif
328  glPopMatrix();
329  const Position& namePos = shape.getPolygonCenter();
330  o->drawName(namePos, s.scale, s.polyName, s.angle);
331  if (s.polyType.show) {
332  const Position p = namePos + Position(0, -0.6 * s.polyType.size / s.scale);
334  }
335 }
336 
337 
338 /****************************************************************************/
@ GLO_POLYGON
a polygon
GUISelectedStorage gSelected
A global holder of selected objects.
GLfloat INV_POLY_TEX_DIM
Definition: GUIPolygon.cpp:83
void APIENTRY errorCallback(GLenum errorCode)
Definition: GUIPolygon.cpp:49
GLfloat xPlane[]
Definition: GUIPolygon.cpp:84
void APIENTRY vertexCallback(GLvoid *vertex)
Definition: GUIPolygon.cpp:63
void APIENTRY combineCallback(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **dataOut)
Definition: GUIPolygon.cpp:68
GLfloat yPlane[]
Definition: GUIPolygon.cpp:85
void APIENTRY beginCallback(GLenum which)
Definition: GUIPolygon.cpp:44
void APIENTRY endCallback(void)
Definition: GUIPolygon.cpp:58
#define DEG2RAD(x)
Definition: GeomHelper.h:35
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
T MAX2(T a, T b)
Definition: StdDefs.h:79
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:77
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:299
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:159
double getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:153
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:638
static void drawLine(const Position &beg, double rot, double visLength)
Draws a thin line.
Definition: GLHelper.cpp:273
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:446
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:181
static void drawTextSettings(const GUIVisualizationTextSettings &settings, const std::string &text, const Position &pos, const double scale, const double angle=0, const double layer=2048, const int align=0)
Definition: GLHelper.cpp:529
static FXMenuCommand * buildFXMenuCommand(FXComposite *p, const std::string &text, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
Definition: GUIDesigns.cpp:40
The popup menu of a globject.
void buildPositionCopyEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to copy the cursor position if geo projection is used,...
void buildShowParamsPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to open the parameter window.
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
void buildNameCopyPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds entries which allow to copy the name / typed name into the clipboard.
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
void buildSelectionPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to (de)select the object.
GUIGlID getGlID() const
Returns the numerical id of the object.
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
A window containing a gl-object's parameter.
void mkItem(const char *name, bool dynamic, ValueSource< T > *src)
Adds a row which obtains its value from a ValueSource.
void closeBuilding(const Parameterised *p=0)
Closes the building of the table.
virtual void setShape(const PositionVector &shape)
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:171
static bool checkDraw(const GUIVisualizationSettings &s, const SUMOPolygon *polygon, const GUIGlObject *o)
check if Polygon can be drawn
Definition: GUIPolygon.cpp:257
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
Definition: GUIPolygon.cpp:111
void storeTesselation(const bool fill, const PositionVector &shape, double lineWidth) const
store the drawing commands in a display list
Definition: GUIPolygon.cpp:225
static void setColor(const GUIVisualizationSettings &s, const SUMOPolygon *polygon, const GUIGlObject *o, bool disableSelectionColor)
set color
Definition: GUIPolygon.cpp:240
PositionVector * myRotatedShape
shape rotated on the centroid, if rotation is needed, nullptr otherwise
Definition: GUIPolygon.h:134
GLuint myDisplayList
id of the display list for the cached tesselation
Definition: GUIPolygon.h:131
static void drawInnerPolygon(const GUIVisualizationSettings &s, const SUMOPolygon *polygon, const GUIGlObject *o, const PositionVector shape, double layer, bool disableSelectionColor)
draw inner Polygon (before pushName() )
Definition: GUIPolygon.cpp:279
GUIParameterTableWindow * getParameterWindow(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own parameter window.
Definition: GUIPolygon.cpp:127
~GUIPolygon()
Destructor.
Definition: GUIPolygon.cpp:105
static void performTesselation(const bool fill, const PositionVector &shape, const double lineWidth)
Definition: GUIPolygon.cpp:192
FXMutex myLock
The mutex used to avoid concurrent updates of the shape.
Definition: GUIPolygon.h:128
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
Definition: GUIPolygon.cpp:139
GUIPolygon(const std::string &id, const std::string &type, const RGBColor &color, const PositionVector &shape, bool geo, bool fill, double lineWidth, double layer=0, double angle=0, const std::string &imgFile="", bool relativePath=false)
Constructor.
Definition: GUIPolygon.cpp:91
virtual void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Definition: GUIPolygon.cpp:149
const T getColor(const double value) const
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
static int getTextureID(const std::string &filename, const bool mirrorX=false)
return texture id for the given filename (initialize on first use)
Stores the information about how to visualize structures.
GUIColorer polyColorer
The polygon colorer.
double scale
information about a lane's width (temporary, used for a single view)
bool netedit
Whether the settings are for Netedit.
GUIVisualizationTextSettings polyName
GUIVisualizationSizeSettings polySize
GUIVisualizationTextSettings polyType
double angle
The current view rotation angle.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
A list of positions.
void rotate2D(double angle)
Position getPolygonCenter() const
Returns the arithmetic of all corner points.
void add(double xoff, double yoff, double zoff)
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
void sub(double xoff, double yoff, double zoff)
Position getCentroid() const
Returns the centroid (closes the polygon if unclosed)
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:82
PositionVector myShape
The positions of the polygon.
Definition: SUMOPolygon.h:133
virtual void setShape(const PositionVector &shape)
Sets the shape of the polygon.
Definition: SUMOPolygon.h:121
double getLineWidth() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:97
bool getFill() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:90
const RGBColor & getShapeColor() const
Returns the color of the Shape.
Definition: Shape.h:80
const std::string & getShapeImgFile() const
Returns the imgFile of the Shape.
Definition: Shape.h:101
double getShapeLayer() const
Returns the layer of the Shape.
Definition: Shape.h:87
double getShapeNaviDegree() const
Returns the angle of the Shape in navigational degrees.
Definition: Shape.h:94
const std::string & getShapeType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:73
double getExaggeration(const GUIVisualizationSettings &s, const GUIGlObject *o, double factor=20) const
return the drawing size including exaggeration and constantSize values
double minSize
The minimum size to draw this object.