VisibilityOptimizer.cpp 13.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*
 This file is part of the VRender library.
 Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr)
 Version 1.0.0, released on June 27, 2005.

 http://artis.imag.fr/Members/Cyril.Soler/VRender

 VRender is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 VRender is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with VRender; if not, write to the Free Software Foundation, Inc.,
 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/****************************************************************************

 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.

 This file is part of the QGLViewer library version 2.6.1.

 http://www.libqglviewer.com - contact@libqglviewer.com

 This file may be used under the terms of the GNU General Public License 
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the LICENSE file included in the packaging of this file.
 In addition, as a special exception, Gilles Debunne gives you certain 
 additional rights, described in the file GPL_EXCEPTION in this package.

 libQGLViewer uses dual licensing. Commercial/proprietary software must
 purchase a libQGLViewer Commercial License.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include <vector>
#include "VRender.h"
#include "Optimizer.h"
#include "Primitive.h"
#include "gpc.h"
#include "math.h"

using namespace vrender ;
using namespace std ;

#ifdef A_FAIRE
void VisibilityOptimizer::optimize(vector<PtrPrimitive>& primitives,float& percentage_finished,string& message)
#else
void VisibilityOptimizer::optimize(vector<PtrPrimitive>& primitives,VRenderParams& vparams)
#endif
{
#ifdef DEBUG_VO
        cout << "Optimizing visibility." << endl ;
#endif
64
        unsigned long N = (unsigned long)(primitives.size()/200 + 1) ;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

#ifdef DEBUG_EPSRENDER__SHOW1
//	cout << "Showing viewer." << endl ;
//	myViewer viewer ;
//	viewer.show();
        double minx =  FLT_MAX ;
        double miny =  FLT_MAX ;
        double maxx = -FLT_MAX ;
        double maxy = -FLT_MAX ;
        for(unsigned int i=0;i<primitives.size();++i)
                for(int j=0;j<primitives[i]->nbVertices();++j)
                {
                        if(maxx < primitives[i]->vertex(j).x()) maxx = primitives[i]->vertex(j).x() ;
                        if(maxy < primitives[i]->vertex(j).y()) maxy = primitives[i]->vertex(j).y() ;
                        if(minx > primitives[i]->vertex(j).x()) minx = primitives[i]->vertex(j).x() ;
                        if(miny > primitives[i]->vertex(j).y()) miny = primitives[i]->vertex(j).y() ;
                }

        glMatrixMode(GL_PROJECTION) ;
        glLoadIdentity() ;
        glOrtho(minx,maxx,miny,maxy,-1,1) ;
        glMatrixMode(GL_MODELVIEW) ;
        glLoadIdentity() ;

        cout << "Window set to " << minx << " " << maxx << " " << miny << " " << maxy << endl ;
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) ;
        glLineWidth(3.0) ;
#endif

        int nb_culled = 0 ;

        // Ca serait pas mal mieux avec une interface c++...

        gpc_polygon cumulated_union ;
        cumulated_union.num_contours = 0 ;
        cumulated_union.hole = NULL ;
        cumulated_union.contour = NULL ;
        size_t nboptimised = 0 ;

        for(size_t pindex = primitives.size() - 1; long(pindex) >= 0;--pindex,++nboptimised)
                if(primitives[pindex] != NULL)
                {
#ifdef A_FAIRE
                        percentage_finished = pindex / (float)primitives.size() ;
#endif

                        if(primitives[pindex]->nbVertices() > 1)
                        {
#ifdef DEBUG_VO
                                if(pindex%50==0)
                                {
                                        char buff[500] ;
                                        sprintf(buff,"Left: % 6ld - Culled: % 6ld", pindex,(long)nb_culled) ;
                                        fprintf(stdout,buff);

                                        for(unsigned int j=0;j<strlen(buff);++j)
                                                fprintf(stdout,"\b") ;

                                        fflush(stdout) ;
                                }
#endif

                                try
                                {
                                        PtrPrimitive p(primitives[pindex]) ;
                                        gpc_polygon difference ;
                                        gpc_polygon new_poly ;
                                        gpc_polygon new_poly_reduced ;
                                        new_poly.num_contours = 0 ;
                                        new_poly.hole = NULL ;
                                        new_poly.contour = NULL ;
                                        new_poly_reduced.num_contours = 0 ;
                                        new_poly_reduced.hole = NULL ;
                                        new_poly_reduced.contour = NULL ;

                                        // 1 - creates a gpc_polygon corresponding to the current primitive

                                        gpc_vertex_list *new_poly_verts = new gpc_vertex_list ;
                                        gpc_vertex_list *new_poly_reduced_verts = new gpc_vertex_list ;

                                        double mx = 0.0 ;
                                        double my = 0.0 ;

                                        if(p->nbVertices() == 2)
                                        {
                                                new_poly_verts->num_vertices = 4 ;
                                                new_poly_verts->vertex = new gpc_vertex[4] ;
                                                new_poly_reduced_verts->num_vertices = 4 ;
                                                new_poly_reduced_verts->vertex = new gpc_vertex[4] ;

                                                double deps = 0.001 ;
                                                double du = p->vertex(1).y()-p->vertex(0).y() ;
                                                double dv = p->vertex(1).x()-p->vertex(0).x() ;
                                                double n = sqrt(du*du+dv*dv) ;
                                                du *= deps/n ;
                                                dv *= deps/n ;
                                                new_poly_verts->vertex[0].x = p->vertex(0).x() + du ;
                                                new_poly_verts->vertex[0].y = p->vertex(0).y() + dv ;
                                                new_poly_verts->vertex[1].x = p->vertex(1).x() + du ;
                                                new_poly_verts->vertex[1].y = p->vertex(1).y() + dv ;
                                                new_poly_verts->vertex[2].x = p->vertex(1).x() - du ;
                                                new_poly_verts->vertex[2].y = p->vertex(1).y() - dv ;
                                                new_poly_verts->vertex[3].x = p->vertex(0).x() - du ;
                                                new_poly_verts->vertex[3].y = p->vertex(0).y() - dv ;

                                                new_poly_reduced_verts->vertex[0].x = p->vertex(0).x() + du ;
                                                new_poly_reduced_verts->vertex[0].y = p->vertex(0).y() + dv ;
                                                new_poly_reduced_verts->vertex[1].x = p->vertex(1).x() + du ;
                                                new_poly_reduced_verts->vertex[1].y = p->vertex(1).y() + dv ;
                                                new_poly_reduced_verts->vertex[2].x = p->vertex(1).x() - du ;
                                                new_poly_reduced_verts->vertex[2].y = p->vertex(1).y() - dv ;
                                                new_poly_reduced_verts->vertex[3].x = p->vertex(0).x() - du ;
                                                new_poly_reduced_verts->vertex[3].y = p->vertex(0).y() - dv ;
                                        }
                                        else
                                        {
181
                                                new_poly_verts->num_vertices = long(p->nbVertices()) ;
182 183 184 185 186 187 188 189 190 191 192 193
                                                new_poly_verts->vertex = new gpc_vertex[p->nbVertices()] ;

                                                for(size_t i=0;i<p->nbVertices();++i)
                                                {
                                                        new_poly_verts->vertex[i].x = p->vertex(i).x() ;
                                                        new_poly_verts->vertex[i].y = p->vertex(i).y() ;
                                                        mx += p->vertex(i).x() ;
                                                        my += p->vertex(i).y() ;
                                                }
                                                mx /= p->nbVertices() ;
                                                my /= p->nbVertices() ;

194
                                                new_poly_reduced_verts->num_vertices = long(p->nbVertices()) ;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
                                                new_poly_reduced_verts->vertex = new gpc_vertex[p->nbVertices()] ;

                                                for(size_t j=0;j<p->nbVertices();++j)
                                                {
                                                        new_poly_reduced_verts->vertex[j].x = mx + (p->vertex(j).x() - mx)*0.999 ;
                                                        new_poly_reduced_verts->vertex[j].y = my + (p->vertex(j).y() - my)*0.999 ;
                                                }
                                        }
                                        gpc_add_contour(&new_poly,new_poly_verts,false) ;
                                        gpc_add_contour(&new_poly_reduced,new_poly_reduced_verts,false) ;

                                        // 2 - computes the difference between this polygon, and the union of the
                                        // 	preceeding ones.

                                        gpc_polygon_clip(GPC_DIFF,&new_poly_reduced,&cumulated_union,&difference) ;

                                        // 3 - checks the difference. If void, the primitive is not visible: skip it
                                        // 	and go to next primitive.

                                        if(difference.num_contours == 0)
                                        {
                                                ++nb_culled ;
                                                delete p ;
                                                primitives[pindex] = NULL ;
                                                continue ;
                                        }

                                        // 4 - The primitive is visible. Let's add it to the cumulated union of
                                        // 	primitives.

                                        if(p->nbVertices() > 2)
                                        {
                                                gpc_polygon cumulated_union_tmp ;
                                                cumulated_union_tmp.num_contours = 0 ;
                                                cumulated_union_tmp.hole = NULL ;
                                                cumulated_union_tmp.contour = NULL ;

                                                gpc_polygon_clip(GPC_UNION,&new_poly,&cumulated_union,&cumulated_union_tmp) ;

                                                gpc_free_polygon(&cumulated_union) ;
                                                cumulated_union = cumulated_union_tmp ;
                                        }

                                        gpc_free_polygon(&new_poly) ;
                                        gpc_free_polygon(&new_poly_reduced) ;
                                        gpc_free_polygon(&difference) ;
#ifdef DEBUG_EPSRENDER__SHOW1
                                        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) ;

                                        glColor3f(1.0,0.0,0.0) ;

                                        for(unsigned long i=0;i<cumulated_union.num_contours;++i)
                                        {
                                                glBegin(GL_LINE_LOOP) ;
                                                for(unsigned long j=0;j<cumulated_union.contour[i].num_vertices;++j)
                                                        glVertex2f(cumulated_union.contour[i].vertex[j].x,cumulated_union.contour[i].vertex[j].y) ;
                                                glEnd() ;
                                        }

                                        glFlush() ;
                                        glXSwapBuffers(glXGetCurrentDisplay(),glXGetCurrentDrawable()) ;
#endif
                                }
                                catch(exception& )
                                {
                                        ; // std::cout << "Could not treat primitive " << pindex << ": internal gpc error." << endl ;
                                }
                        }

                        if(nboptimised%N==0)
                                vparams.progress(nboptimised/(float)primitives.size(), QGLViewer::tr("Visibility optimization")) ;
                }

#ifdef DEBUG_VO
        cout << nb_culled << " primitives culled over " << primitives.size() << "." << endl ;
#endif

        gpc_free_polygon(&cumulated_union) ;
}