GUIPolygon2D.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 2001-2010 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 "GUIPolygon2D.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 #else
00045 #define APIENTRY
00046 #endif
00047 
00048 #include <GL/gl.h>
00049 #include <GL/glu.h>
00050 
00051 
00052 // ===========================================================================
00053 // method definitions
00054 // ===========================================================================
00055 GUIPolygon2D::GUIPolygon2D(GUIGlObjectStorage &idStorage,
00056                            int layer,
00057                            const std::string name, const std::string type,
00058                            const RGBColor &color,
00059                            const Position2DVector &Pos,
00060                            bool fill) throw()
00061         : Polygon2D(name, type, color, Pos, fill),
00062         GUIGlObject_AbstractAdd(idStorage, "poly:"+name, GLO_SHAPE), myLayer(layer) {}
00063 
00064 
00065 GUIPolygon2D::~GUIPolygon2D() throw() {}
00066 
00067 
00068 
00069 GUIGLObjectPopupMenu *
00070 GUIPolygon2D::getPopUpMenu(GUIMainWindow &app,
00071                            GUISUMOAbstractView &parent) throw() {
00072     GUIGLObjectPopupMenu *ret = new GUIGLObjectPopupMenu(app, parent, *this);
00073     buildPopupHeader(ret, app, false);
00074     FXString t(myType.c_str());
00075     new FXMenuCommand(ret, "(" + t + ")", 0, 0, 0);
00076     new FXMenuSeparator(ret);
00077     buildCenterPopupEntry(ret);
00078     buildNameCopyPopupEntry(ret);
00079     buildSelectionPopupEntry(ret);
00080     buildPositionCopyEntry(ret, false);
00081     return ret;
00082 }
00083 
00084 
00085 GUIParameterTableWindow *
00086 GUIPolygon2D::getParameterWindow(GUIMainWindow &,
00087                                  GUISUMOAbstractView &) throw() {
00088     return 0;
00089 }
00090 
00091 
00092 const std::string &
00093 GUIPolygon2D::getMicrosimID() const throw() {
00094     return myName;
00095 }
00096 
00097 
00098 Boundary
00099 GUIPolygon2D::getCenteringBoundary() const throw() {
00100     Boundary b;
00101     b.add(myShape.getBoxBoundary());
00102     b.grow(10);
00103     return b;
00104 }
00105 
00106 
00107 void APIENTRY beginCallback(GLenum which) {
00108     glBegin(which);
00109 }
00110 
00111 void APIENTRY errorCallback(GLenum errorCode) {
00112     const GLubyte *estring;
00113 
00114     estring = gluErrorString(errorCode);
00115     fprintf(stderr, "Tessellation Error: %s\n", estring);
00116     exit(0);
00117 }
00118 
00119 void APIENTRY endCallback(void) {
00120     glEnd();
00121 }
00122 
00123 void APIENTRY vertexCallback(GLvoid *vertex) {
00124     const GLdouble *pointer;
00125 
00126     pointer = (GLdouble *) vertex;
00127     glVertex3dv((GLdouble *) vertex);
00128 }
00129 
00130 void APIENTRY combineCallback(GLdouble coords[3],
00131                               GLdouble *vertex_data[4],
00132                               GLfloat weight[4], GLdouble **dataOut) {
00133     GLdouble *vertex;
00134 
00135     vertex = (GLdouble *) malloc(7 * sizeof(GLdouble));
00136 
00137     vertex[0] = coords[0];
00138     vertex[1] = coords[1];
00139     vertex[2] = coords[2];
00140     *dataOut = vertex;
00141 }
00142 
00143 double glvert[6];
00144 void
00145 GUIPolygon2D::drawGL(const GUIVisualizationSettings &s) const throw() {
00146     if (fill()) {
00147         if (myShape.size()<3) {
00148             return;
00149         }
00150     } else {
00151         if (myShape.size()<2) {
00152             return;
00153         }
00154     }
00155     glPushMatrix();
00156     if (getLayer()==0) {
00157         glTranslated(0, 0, -.03);
00158     } else if (getLayer()>0) {
00159         glTranslated(0, 0, -.05-.01*(SUMOReal) getLayer());
00160     } else {
00161         glTranslated(0, 0, -.01*(SUMOReal) getLayer()+.01);
00162     }
00163     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00164     // (optional) set id
00165     if (s.needsGlID) {
00166         glPushName(getGlID());
00167     }
00168     RGBColor color = getColor();
00169     glColor3d(color.red(), color.green(), color.blue());
00170     if (fill()) {
00171         double *points = new double[myShape.size()*3];
00172         GLUtesselator *tobj = gluNewTess();
00173         gluTessCallback(tobj, GLU_TESS_VERTEX, (GLvoid(APIENTRY*)()) &glVertex3dv);
00174         gluTessCallback(tobj, GLU_TESS_BEGIN, (GLvoid(APIENTRY*)()) &beginCallback);
00175         gluTessCallback(tobj, GLU_TESS_END, (GLvoid(APIENTRY*)()) &endCallback);
00176         //gluTessCallback(tobj, GLU_TESS_ERROR, (GLvoid (APIENTRY*) ()) &errorCallback);
00177         gluTessCallback(tobj, GLU_TESS_COMBINE, (GLvoid(APIENTRY*)()) &combineCallback);
00178         gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
00179         gluTessBeginPolygon(tobj, NULL);
00180         gluTessBeginContour(tobj);
00181         for (size_t i=0; i!=myShape.size(); ++i) {
00182             points[3*i]  = myShape[(int) i].x();
00183             points[3*i+1]  = myShape[(int) i].y();
00184             points[3*i+2]  = 0;
00185             glvert[0] = myShape[(int) i].x();
00186             glvert[1] = myShape[(int) i].y();
00187             glvert[2] = 0;
00188             glvert[3] = 1;
00189             glvert[4] = 1;
00190             glvert[5] = 1;
00191             gluTessVertex(tobj, points+3*i, points+3*i) ;
00192         }
00193         gluTessEndContour(tobj);
00194 
00195         gluTessEndPolygon(tobj);
00196         gluDeleteTess(tobj);
00197         delete[] points;
00198     } else {
00199         GLHelper::drawLine(myShape);
00200         GLHelper::drawBoxLines(myShape, 1.);
00201     }
00202     // (optional) clear id
00203     if (s.needsGlID) {
00204         glPopName();
00205     }
00206     glPopMatrix();
00207 }
00208 
00209 
00210 
00211 int
00212 GUIPolygon2D::getLayer() const {
00213     return myLayer;
00214 }
00215 
00216 
00217 
00218 /****************************************************************************/
00219 

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6