SUMO - Simulation of Urban MObility

GUIPolygon.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // The GUI-version of a polygon
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 // ===========================================================================
00021 // included modules
00022 // ===========================================================================
00023 #ifdef _MSC_VER
00024 #include <windows_config.h>
00025 #else
00026 #include <config.h>
00027 #endif
00028 
00029 #include <string>
00030 #include "GUIPolygon.h"
00031 #include <utils/gui/globjects/GUIGlObject.h>
00032 #include <utils/gui/div/GUIParameterTableWindow.h>
00033 #include <utils/gui/globjects/GUIGLObjectPopupMenu.h>
00034 #include <utils/gui/settings/GUIVisualizationSettings.h>
00035 #include <utils/gui/div/GLHelper.h>
00036 #include <foreign/polyfonts/polyfonts.h>
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041 
00042 #ifdef WIN32
00043 #include <windows.h>
00044 #endif
00045 
00046 #include <GL/gl.h>
00047 #include <GL/glu.h>
00048 
00049 
00050 // ===========================================================================
00051 // method definitions
00052 // ===========================================================================
00053 GUIPolygon::GUIPolygon(int layer,
00054                        const std::string name, const std::string type,
00055                        const RGBColor& color,
00056                        const PositionVector& Pos,
00057                        bool fill) throw()
00058     : Polygon(name, type, color, Pos, fill),
00059       GUIGlObject_AbstractAdd("poly", GLO_SHAPE, name), myLayer(layer) {}
00060 
00061 
00062 GUIPolygon::~GUIPolygon() throw() {}
00063 
00064 
00065 
00066 GUIGLObjectPopupMenu*
00067 GUIPolygon::getPopUpMenu(GUIMainWindow& app,
00068                          GUISUMOAbstractView& parent) throw() {
00069     GUIGLObjectPopupMenu* ret = new GUIGLObjectPopupMenu(app, parent, *this);
00070     buildPopupHeader(ret, app, false);
00071     FXString t(myType.c_str());
00072     new FXMenuCommand(ret, "(" + t + ")", 0, 0, 0);
00073     new FXMenuSeparator(ret);
00074     buildCenterPopupEntry(ret);
00075     buildNameCopyPopupEntry(ret);
00076     buildSelectionPopupEntry(ret);
00077     buildPositionCopyEntry(ret, false);
00078     return ret;
00079 }
00080 
00081 
00082 GUIParameterTableWindow*
00083 GUIPolygon::getParameterWindow(GUIMainWindow&,
00084                                GUISUMOAbstractView&) throw() {
00085     return 0;
00086 }
00087 
00088 
00089 Boundary
00090 GUIPolygon::getCenteringBoundary() const throw() {
00091     Boundary b;
00092     b.add(myShape.getBoxBoundary());
00093     b.grow(10);
00094     return b;
00095 }
00096 
00097 
00098 void APIENTRY beginCallback(GLenum which) {
00099     glBegin(which);
00100 }
00101 
00102 void APIENTRY errorCallback(GLenum errorCode) {
00103     const GLubyte* estring;
00104 
00105     estring = gluErrorString(errorCode);
00106     fprintf(stderr, "Tessellation Error: %s\n", estring);
00107     exit(0);
00108 }
00109 
00110 void APIENTRY endCallback(void) {
00111     glEnd();
00112 }
00113 
00114 void APIENTRY vertexCallback(GLvoid* vertex) {
00115     const GLdouble* pointer;
00116 
00117     pointer = (GLdouble*) vertex;
00118     glVertex3dv((GLdouble*) vertex);
00119 }
00120 
00121 void APIENTRY combineCallback(GLdouble coords[3],
00122                               GLdouble* vertex_data[4],
00123                               GLfloat weight[4], GLdouble** dataOut) {
00124     UNUSED_PARAMETER(weight);
00125     UNUSED_PARAMETER(*vertex_data);
00126     GLdouble* vertex;
00127 
00128     vertex = (GLdouble*) malloc(7 * sizeof(GLdouble));
00129 
00130     vertex[0] = coords[0];
00131     vertex[1] = coords[1];
00132     vertex[2] = coords[2];
00133     *dataOut = vertex;
00134 }
00135 
00136 double glvert[6];
00137 void
00138 GUIPolygon::drawGL(const GUIVisualizationSettings& s) const throw() {
00139     UNUSED_PARAMETER(s);
00140     if (fill()) {
00141         if (myShape.size()<3) {
00142             return;
00143         }
00144     } else {
00145         if (myShape.size()<2) {
00146             return;
00147         }
00148     }
00149     glPushName(getGlID());
00150     glPushMatrix();
00151     glTranslated(0, 0, getLayer());
00152     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00153     GLHelper::setColor(getColor());
00154     if (fill()) {
00155         double* points = new double[myShape.size()*3];
00156         GLUtesselator* tobj = gluNewTess();
00157         gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid(APIENTRY*)()) &glVertex3dv);
00158         gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid(APIENTRY*)()) &beginCallback);
00159         gluTessCallback(tobj, GLU_TESS_END, (GLvoid(APIENTRY*)()) &endCallback);
00160         //gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (APIENTRY*) ()) &errorCallback);
00161         gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid(APIENTRY*)()) &combineCallback);
00162         gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
00163         gluTessBeginPolygon(tobj, NULL);
00164         gluTessBeginContour(tobj);
00165         for (size_t i=0; i!=myShape.size(); ++i) {
00166             points[3*i]  = myShape[(int) i].x();
00167             points[3*i+1]  = myShape[(int) i].y();
00168             points[3*i+2]  = 0;
00169             glvert[0] = myShape[(int) i].x();
00170             glvert[1] = myShape[(int) i].y();
00171             glvert[2] = 0;
00172             glvert[3] = 1;
00173             glvert[4] = 1;
00174             glvert[5] = 1;
00175             gluTessVertex(tobj, points+3*i, points+3*i) ;
00176         }
00177         gluTessEndContour(tobj);
00178 
00179         gluTessEndPolygon(tobj);
00180         gluDeleteTess(tobj);
00181         delete[] points;
00182     } else {
00183         GLHelper::drawLine(myShape);
00184         GLHelper::drawBoxLines(myShape, 1.);
00185     }
00186     glPopName();
00187     glPopMatrix();
00188 }
00189 
00190 
00191 
00192 int
00193 GUIPolygon::getLayer() const {
00194     return myLayer;
00195 }
00196 
00197 
00198 
00199 /****************************************************************************/
00200 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines