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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // The GUI-version of a polygon
21 /****************************************************************************/
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include "GUIPolygon.h"
40 #include <utils/gui/div/GLHelper.h>
41 
42 //#define GUIPolygon_DEBUG_DRAW_VERTICES
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 GUIPolygon::GUIPolygon(const std::string& id, const std::string& type,
48  const RGBColor& color, const PositionVector& shape, bool geo,
49  bool fill, double layer, double angle, const std::string& imgFile):
50  SUMOPolygon(id, type, color, shape, geo, fill, layer, angle, imgFile),
52  myDisplayList(0),
53  myLineWidth(1) // m
54 
55 {}
56 
57 
59 
60 
61 
64  GUISUMOAbstractView& parent) {
65  GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
66  buildPopupHeader(ret, app, false);
67  FXString t(myType.c_str());
68  new FXMenuCommand(ret, "(" + t + ")", 0, 0, 0);
69  new FXMenuSeparator(ret);
73  buildShowParamsPopupEntry(ret, false);
74  buildPositionCopyEntry(ret, false);
75  return ret;
76 }
77 
78 
83  new GUIParameterTableWindow(app, *this, 3 + (int)getMap().size());
84  // add items
85  ret->mkItem("type", false, myType);
86  ret->mkItem("layer", false, toString(getLayer()));
87  ret->closeBuilding(this);
88  return ret;
89 }
90 
91 
94  Boundary b;
96  b.grow(2);
97  return b;
98 }
99 
100 
101 void APIENTRY beginCallback(GLenum which) {
102  glBegin(which);
103 }
104 
105 void APIENTRY errorCallback(GLenum errorCode) {
106  const GLubyte* estring;
107 
108  estring = gluErrorString(errorCode);
109  fprintf(stderr, "Tessellation Error: %s\n", estring);
110  exit(0);
111 }
112 
113 void APIENTRY endCallback(void) {
114  glEnd();
115 }
116 
117 void APIENTRY vertexCallback(GLvoid* vertex) {
118  glVertex3dv((GLdouble*) vertex);
119 }
120 
121 void APIENTRY combineCallback(GLdouble coords[3],
122  GLdouble* vertex_data[4],
123  GLfloat weight[4], GLdouble** dataOut) {
124  UNUSED_PARAMETER(weight);
125  UNUSED_PARAMETER(*vertex_data);
126  GLdouble* vertex;
127 
128  vertex = (GLdouble*) malloc(7 * sizeof(GLdouble));
129 
130  vertex[0] = coords[0];
131  vertex[1] = coords[1];
132  vertex[2] = coords[2];
133  *dataOut = vertex;
134 }
135 
136 
137 GLfloat INV_POLY_TEX_DIM = 1.0 / 256.0;
138 GLfloat xPlane[] = {INV_POLY_TEX_DIM, 0.0, 0.0, 0.0};
139 GLfloat yPlane[] = {0.0, INV_POLY_TEX_DIM, 0.0, 0.0};
140 
141 void
143  if (s.polySize.getExaggeration(s) == 0) {
144  return;
145  }
146  Boundary boundary = myShape.getBoxBoundary();
147  if (s.scale * MAX2(boundary.getWidth(), boundary.getHeight()) < s.polySize.minSize) {
148  return;
149  }
150  if (getFill()) {
151  if (myShape.size() < 3) {
152  return;
153  }
154  } else {
155  if (myShape.size() < 2) {
156  return;
157  }
158  }
160  //if (myDisplayList == 0 || (!getFill() && myLineWidth != s.polySize.getExaggeration(s))) {
161  // storeTesselation(s.polySize.getExaggeration(s));
162  //}
163  glPushName(getGlID());
164  glPushMatrix();
165  glTranslated(0, 0, getLayer());
166  glRotated(-getNaviDegree(), 0, 0, 1);
167  // set color depending of selection
169  GLHelper::setColor(RGBColor(0, 0, 204));
170  } else {
172  }
173 
174  int textureID = -1;
175  if (getFill()) {
176  const std::string& file = getImgFile();
177  if (file != "") {
178  textureID = GUITexturesHelper::getTextureID(file, true);
179  }
180  }
181  // init generation of texture coordinates
182  if (textureID >= 0) {
183  glEnable(GL_TEXTURE_2D);
184  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
185  glDisable(GL_CULL_FACE);
186  glDisable(GL_DEPTH_TEST); // without DEPTH_TEST vehicles may be drawn below roads
187  glDisable(GL_LIGHTING);
188  glDisable(GL_COLOR_MATERIAL);
189  glDisable(GL_ALPHA_TEST);
190  glEnable(GL_BLEND);
191  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
192  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
193  glBindTexture(GL_TEXTURE_2D, textureID);
194  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
195  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
196  // http://www.gamedev.net/topic/133564-glutesselation-and-texture-mapping/
197  glEnable(GL_TEXTURE_GEN_S);
198  glEnable(GL_TEXTURE_GEN_T);
199  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
200  glTexGenfv(GL_S, GL_OBJECT_PLANE, xPlane);
201  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
202  glTexGenfv(GL_T, GL_OBJECT_PLANE, yPlane);
203  }
204  // recall tesselation
205  //glCallList(myDisplayList);
207  // de-init generation of texture coordinates
208  if (textureID >= 0) {
209  glEnable(GL_DEPTH_TEST);
210  glBindTexture(GL_TEXTURE_2D, 0);
211  glDisable(GL_TEXTURE_2D);
212  glDisable(GL_TEXTURE_GEN_S);
213  glDisable(GL_TEXTURE_GEN_T);
214  }
215 #ifdef GUIPolygon_DEBUG_DRAW_VERTICES
217 #endif
218  glPopMatrix();
219  const Position namePos = myShape.getPolygonCenter();
220  drawName(namePos, s.scale, s.polyName);
221  if (s.polyType.show) {
222  GLHelper::drawText(myType, namePos + Position(0, -0.6 * s.polyType.size / s.scale),
224  }
225  glPopName();
226 }
227 
228 
229 void
232  SUMOPolygon::setShape(shape);
233  //storeTesselation(myLineWidth);
234 }
235 
236 
237 void
238 GUIPolygon::performTesselation(double lineWidth) const {
239  if (getFill()) {
240  // draw the tesselated shape
241  double* points = new double[myShape.size() * 3];
242  GLUtesselator* tobj = gluNewTess();
243  gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid(APIENTRY*)()) &glVertex3dv);
244  gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid(APIENTRY*)()) &beginCallback);
245  gluTessCallback(tobj, GLU_TESS_END, (GLvoid(APIENTRY*)()) &endCallback);
246  //gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (APIENTRY*) ()) &errorCallback);
247  gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid(APIENTRY*)()) &combineCallback);
248  gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
249  gluTessBeginPolygon(tobj, NULL);
250  gluTessBeginContour(tobj);
251  for (int i = 0; i != (int)myShape.size(); ++i) {
252  points[3 * i] = myShape[(int) i].x();
253  points[3 * i + 1] = myShape[(int) i].y();
254  points[3 * i + 2] = 0;
255  gluTessVertex(tobj, points + 3 * i, points + 3 * i);
256  }
257  gluTessEndContour(tobj);
258 
259  gluTessEndPolygon(tobj);
260  gluDeleteTess(tobj);
261  delete[] points;
262 
263  } else {
265  GLHelper::drawBoxLines(myShape, lineWidth);
266  }
267  //std::cout << "OpenGL says: '" << gluErrorString(glGetError()) << "'\n";
268 }
269 
270 
271 void
272 GUIPolygon::storeTesselation(double lineWidth) const {
273  if (myDisplayList > 0) {
274  glDeleteLists(myDisplayList, 1);
275  }
276  myDisplayList = glGenLists(1);
277  if (myDisplayList == 0) {
278  throw ProcessError("GUIPolygon::storeTesselation() could not create display list");
279  }
280  glNewList(myDisplayList, GL_COMPILE);
281  performTesselation(lineWidth);
282  glEndList();
283 }
284 
285 
286 /****************************************************************************/
287 
PositionVector myShape
The positions of the polygon.
Definition: SUMOPolygon.h:124
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
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:183
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:138
void performTesselation(double lineWidth) const
Definition: GUIPolygon.cpp:238
void storeTesselation(double lineWidth) const
store the drawing commands in a display list
Definition: GUIPolygon.cpp:272
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:556
void buildCenterPopupEntry(GUIGLObjectPopupMenu *ret, bool addSeparator=true)
Builds an entry which allows to center to the object.
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:487
T MAX2(T a, T b)
Definition: StdDefs.h:73
double getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:161
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
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:39
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
GLuint myDisplayList
id of the display list for the cached tesselation
Definition: GUIPolygon.h:124
const std::string & getImgFile() const
Returns the imgFile of the Shape.
Definition: Shape.h:100
std::string myType
The type of the Shape.
Definition: Shape.h:157
GUIVisualizationSizeSettings polySize
double getLayer() const
Returns the layer of the Shape.
Definition: Shape.h:86
virtual void setShape(const PositionVector &shape)
Sets the shape of the polygon.
Definition: SUMOPolygon.h:112
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
static void setColor(const RGBColor &c)
Sets the gl-color to this value.
Definition: GLHelper.cpp:449
double getNaviDegree() const
Returns the angle of the Shape in navigational degrees.
Definition: Shape.h:93
double minSize
The minimum size to draw this object.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
A list of positions.
Position getPolygonCenter() const
Returns the arithmetic of all corner points.
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:80
void drawName(const Position &pos, const double scale, const GUIVisualizationTextSettings &settings, const double angle=0) const
draw name of item
Boundary & grow(double by)
extends the boundary by the given amount
Definition: Boundary.cpp:301
static void drawLine(const Position &beg, double rot, double visLength)
Draws a thin line.
Definition: GLHelper.cpp:276
virtual void setShape(const PositionVector &shape)
set a new shape and update the tesselation
Definition: GUIPolygon.cpp:230
bool getFill() const
Returns whether the polygon is filled.
Definition: SUMOPolygon.h:92
GLfloat INV_POLY_TEX_DIM
Definition: GUIPolygon.cpp:137
void APIENTRY beginCallback(GLenum which)
Definition: GUIPolygon.cpp:101
virtual void drawGL(const GUIVisualizationSettings &s) const
Draws the object.
Definition: GUIPolygon.cpp:142
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:78
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:167
void APIENTRY endCallback(void)
Definition: GUIPolygon.cpp:113
void APIENTRY combineCallback(GLdouble coords[3], GLdouble *vertex_data[4], GLfloat weight[4], GLdouble **dataOut)
Definition: GUIPolygon.cpp:121
~GUIPolygon()
Destructor.
Definition: GUIPolygon.cpp:58
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:70
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
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:117
MFXMutex myLock
The mutex used to avoid concurrent updates of the shape.
Definition: GUIPolygon.h:121
GUIGlID getGlID() const
Returns the numerical id of the object.
double getExaggeration(const GUIVisualizationSettings &s, double factor=20) const
return the drawing size including exaggeration and constantSize values
double myLineWidth
the previous line width for deciding whether the display list must be refreshed
Definition: GUIPolygon.h:127
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
empty max
void add(double x, double y, double z=0)
Makes the boundary include the given coordinate.
Definition: Boundary.cpp:85
Boundary getCenteringBoundary() const
Returns the boundary to which the view shall be centered in order to show the object.
Definition: GUIPolygon.cpp:93
void mkItem(const char *name, bool dynamic, ValueSource< unsigned > *src)
Adds a row which obtains its value from an unsigned-ValueSource.
GUISelectedStorage gSelected
A global holder of selected objects.
A window containing a gl-object&#39;s parameter.
GUIPolygon(const std::string &id, const std::string &type, const RGBColor &color, const PositionVector &shape, bool geo, bool fill, double layer=0, double angle=0, const std::string &imgFile="")
Constructor.
Definition: GUIPolygon.cpp:47
GLfloat yPlane[]
Definition: GUIPolygon.cpp:139
GUIGLObjectPopupMenu * getPopUpMenu(GUIMainWindow &app, GUISUMOAbstractView &parent)
Returns an own popup-menu.
Definition: GUIPolygon.cpp:63
void buildPopupHeader(GUIGLObjectPopupMenu *ret, GUIMainWindow &app, bool addSeparator=true)
Builds the header.
void APIENTRY errorCallback(GLenum errorCode)
Definition: GUIPolygon.cpp:105
GUIVisualizationTextSettings polyName