Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 #include <vector>
00046 #include "VRender.h"
00047 #include "Optimizer.h"
00048 #include "Primitive.h"
00049 #include "gpc.h"
00050 #include "math.h"
00051 
00052 using namespace vrender ;
00053 using namespace std ;
00054 
00055 #ifdef A_FAIRE
00056 void VisibilityOptimizer::optimize(vector<PtrPrimitive>& primitives,float& percentage_finished,string& message)
00057 #else
00058 void VisibilityOptimizer::optimize(vector<PtrPrimitive>& primitives,VRenderParams& vparams)
00059 #endif
00060 {
00061 #ifdef DEBUG_VO
00062         cout << "Optimizing visibility." << endl ;
00063 #endif
00064         unsigned int N = primitives.size()/200 + 1 ;
00065 
00066 #ifdef DEBUG_EPSRENDER__SHOW1
00067 
00068 
00069 
00070         double minx =  FLT_MAX ;
00071         double miny =  FLT_MAX ;
00072         double maxx = -FLT_MAX ;
00073         double maxy = -FLT_MAX ;
00074         for(unsigned int i=0;i<primitives.size();++i)
00075                 for(int j=0;j<primitives[i]->nbVertices();++j)
00076                 {
00077                         if(maxx < primitives[i]->vertex(j).x()) maxx = primitives[i]->vertex(j).x() ;
00078                         if(maxy < primitives[i]->vertex(j).y()) maxy = primitives[i]->vertex(j).y() ;
00079                         if(minx > primitives[i]->vertex(j).x()) minx = primitives[i]->vertex(j).x() ;
00080                         if(miny > primitives[i]->vertex(j).y()) miny = primitives[i]->vertex(j).y() ;
00081                 }
00082 
00083         glMatrixMode(GL_PROJECTION) ;
00084         glLoadIdentity() ;
00085         glOrtho(minx,maxx,miny,maxy,-1,1) ;
00086         glMatrixMode(GL_MODELVIEW) ;
00087         glLoadIdentity() ;
00088 
00089         cout << "Window set to " << minx << " " << maxx << " " << miny << " " << maxy << endl ;
00090         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) ;
00091         glLineWidth(3.0) ;
00092 #endif
00093 
00094         int nb_culled = 0 ;
00095 
00096         
00097 
00098         gpc_polygon cumulated_union ;
00099         cumulated_union.num_contours = 0 ;
00100         cumulated_union.hole = NULL ;
00101         cumulated_union.contour = NULL ;
00102         int nboptimised = 0 ;
00103 
00104         for(int pindex = (int)(primitives.size()) - 1; pindex >= 0;--pindex,++nboptimised)
00105                 if(primitives[pindex] != NULL)
00106                 {
00107 #ifdef A_FAIRE
00108                         percentage_finished = pindex / (float)primitives.size() ;
00109 #endif
00110 
00111                         if(primitives[pindex]->nbVertices() > 1)
00112                         {
00113 #ifdef DEBUG_VO
00114                                 if(pindex%50==0)
00115                                 {
00116                                         char buff[500] ;
00117                                         sprintf(buff,"Left: % 6ld - Culled: % 6ld", (long)pindex,(long)nb_culled) ;
00118                                         fprintf(stdout,buff);
00119 
00120                                         for(unsigned int j=0;j<strlen(buff);++j)
00121                                                 fprintf(stdout,"\b") ;
00122 
00123                                         fflush(stdout) ;
00124                                 }
00125 #endif
00126 
00127                                 try
00128                                 {
00129                                         PtrPrimitive p(primitives[pindex]) ;
00130                                         gpc_polygon difference ;
00131                                         gpc_polygon new_poly ;
00132                                         gpc_polygon new_poly_reduced ;
00133                                         new_poly.num_contours = 0 ;
00134                                         new_poly.hole = NULL ;
00135                                         new_poly.contour = NULL ;
00136                                         new_poly_reduced.num_contours = 0 ;
00137                                         new_poly_reduced.hole = NULL ;
00138                                         new_poly_reduced.contour = NULL ;
00139 
00140                                         
00141 
00142                                         gpc_vertex_list *new_poly_verts = new gpc_vertex_list ;
00143                                         gpc_vertex_list *new_poly_reduced_verts = new gpc_vertex_list ;
00144 
00145                                         double mx = 0.0 ;
00146                                         double my = 0.0 ;
00147 
00148                                         if(p->nbVertices() == 2)
00149                                         {
00150                                                 new_poly_verts->num_vertices = 4 ;
00151                                                 new_poly_verts->vertex = new gpc_vertex[4] ;
00152                                                 new_poly_reduced_verts->num_vertices = 4 ;
00153                                                 new_poly_reduced_verts->vertex = new gpc_vertex[4] ;
00154 
00155                                                 double deps = 0.001 ;
00156                                                 double du = p->vertex(1).y()-p->vertex(0).y() ;
00157                                                 double dv = p->vertex(1).x()-p->vertex(0).x() ;
00158                                                 double n = sqrt(du*du+dv*dv) ;
00159                                                 du *= deps/n ;
00160                                                 dv *= deps/n ;
00161                                                 new_poly_verts->vertex[0].x = p->vertex(0).x() + du ;
00162                                                 new_poly_verts->vertex[0].y = p->vertex(0).y() + dv ;
00163                                                 new_poly_verts->vertex[1].x = p->vertex(1).x() + du ;
00164                                                 new_poly_verts->vertex[1].y = p->vertex(1).y() + dv ;
00165                                                 new_poly_verts->vertex[2].x = p->vertex(1).x() - du ;
00166                                                 new_poly_verts->vertex[2].y = p->vertex(1).y() - dv ;
00167                                                 new_poly_verts->vertex[3].x = p->vertex(0).x() - du ;
00168                                                 new_poly_verts->vertex[3].y = p->vertex(0).y() - dv ;
00169 
00170                                                 new_poly_reduced_verts->vertex[0].x = p->vertex(0).x() + du ;
00171                                                 new_poly_reduced_verts->vertex[0].y = p->vertex(0).y() + dv ;
00172                                                 new_poly_reduced_verts->vertex[1].x = p->vertex(1).x() + du ;
00173                                                 new_poly_reduced_verts->vertex[1].y = p->vertex(1).y() + dv ;
00174                                                 new_poly_reduced_verts->vertex[2].x = p->vertex(1).x() - du ;
00175                                                 new_poly_reduced_verts->vertex[2].y = p->vertex(1).y() - dv ;
00176                                                 new_poly_reduced_verts->vertex[3].x = p->vertex(0).x() - du ;
00177                                                 new_poly_reduced_verts->vertex[3].y = p->vertex(0).y() - dv ;
00178                                         }
00179                                         else
00180                                         {
00181                                                 new_poly_verts->num_vertices = p->nbVertices() ;
00182                                                 new_poly_verts->vertex = new gpc_vertex[p->nbVertices()] ;
00183 
00184                                                 for(unsigned int i=0;i<p->nbVertices();++i)
00185                                                 {
00186                                                         new_poly_verts->vertex[i].x = p->vertex(i).x() ;
00187                                                         new_poly_verts->vertex[i].y = p->vertex(i).y() ;
00188                                                         mx += p->vertex(i).x() ;
00189                                                         my += p->vertex(i).y() ;
00190                                                 }
00191                                                 mx /= p->nbVertices() ;
00192                                                 my /= p->nbVertices() ;
00193 
00194                                                 new_poly_reduced_verts->num_vertices = p->nbVertices() ;
00195                                                 new_poly_reduced_verts->vertex = new gpc_vertex[p->nbVertices()] ;
00196 
00197                                                 for(unsigned int j=0;j<p->nbVertices();++j)
00198                                                 {
00199                                                         new_poly_reduced_verts->vertex[j].x = mx + (p->vertex(j).x() - mx)*0.999 ;
00200                                                         new_poly_reduced_verts->vertex[j].y = my + (p->vertex(j).y() - my)*0.999 ;
00201                                                 }
00202                                         }
00203                                         gpc_add_contour(&new_poly,new_poly_verts,false) ;
00204                                         gpc_add_contour(&new_poly_reduced,new_poly_reduced_verts,false) ;
00205 
00206                                         
00207                                         
00208 
00209                                         gpc_polygon_clip(GPC_DIFF,&new_poly_reduced,&cumulated_union,&difference) ;
00210 
00211                                         
00212                                         
00213 
00214                                         if(difference.num_contours == 0)
00215                                         {
00216                                                 ++nb_culled ;
00217                                                 delete p ;
00218                                                 primitives[pindex] = NULL ;
00219                                                 continue ;
00220                                         }
00221 
00222                                         
00223                                         
00224 
00225                                         if(p->nbVertices() > 2)
00226                                         {
00227                                                 gpc_polygon cumulated_union_tmp ;
00228                                                 cumulated_union_tmp.num_contours = 0 ;
00229                                                 cumulated_union_tmp.hole = NULL ;
00230                                                 cumulated_union_tmp.contour = NULL ;
00231 
00232                                                 gpc_polygon_clip(GPC_UNION,&new_poly,&cumulated_union,&cumulated_union_tmp) ;
00233 
00234                                                 gpc_free_polygon(&cumulated_union) ;
00235                                                 cumulated_union = cumulated_union_tmp ;
00236                                         }
00237 
00238                                         gpc_free_polygon(&new_poly) ;
00239                                         gpc_free_polygon(&new_poly_reduced) ;
00240                                         gpc_free_polygon(&difference) ;
00241 #ifdef DEBUG_EPSRENDER__SHOW1
00242                                         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) ;
00243 
00244                                         glColor3f(1.0,0.0,0.0) ;
00245 
00246                                         for(int i=0;i<cumulated_union.num_contours;++i)
00247                                         {
00248                                                 glBegin(GL_LINE_LOOP) ;
00249                                                 for(int j=0;j<cumulated_union.contour[i].num_vertices;++j)
00250                                                         glVertex2f(cumulated_union.contour[i].vertex[j].x,cumulated_union.contour[i].vertex[j].y) ;
00251                                                 glEnd() ;
00252                                         }
00253 
00254                                         glFlush() ;
00255                                         glXSwapBuffers(glXGetCurrentDisplay(),glXGetCurrentDrawable()) ;
00256 #endif
00257                                 }
00258                                 catch(exception& )
00259                                 {
00260                                         ; 
00261                                 }
00262                         }
00263 
00264                         if(nboptimised%N==0)
00265                                 vparams.progress(nboptimised/(float)primitives.size(), QGLViewer::tr("Visibility optimization")) ;
00266                 }
00267 
00268 #ifdef DEBUG_VO
00269         cout << nb_culled << " primitives culled over " << primitives.size() << "." << endl ;
00270 #endif
00271 
00272         gpc_free_polygon(&cumulated_union) ;
00273 }
00274 
00275