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