47 #include "Optimizer.h"
48 #include "Primitive.h"
56 void VisibilityOptimizer::optimize(vector<PtrPrimitive>& primitives,
float& percentage_finished,
string& message)
58 void VisibilityOptimizer::optimize(vector<PtrPrimitive>& primitives,
VRenderParams& vparams)
62 cout <<
"Optimizing visibility." << endl ;
64 unsigned int N = primitives.size()/200 + 1 ;
66 #ifdef DEBUG_EPSRENDER__SHOW1
70 double minx = FLT_MAX ;
71 double miny = FLT_MAX ;
72 double maxx = -FLT_MAX ;
73 double maxy = -FLT_MAX ;
74 for(
unsigned int i=0;i<primitives.size();++i)
75 for(
int j=0;j<primitives[i]->nbVertices();++j)
77 if(maxx < primitives[i]->vertex(j).x()) maxx = primitives[i]->vertex(j).x() ;
78 if(maxy < primitives[i]->vertex(j).y()) maxy = primitives[i]->vertex(j).y() ;
79 if(minx > primitives[i]->vertex(j).x()) minx = primitives[i]->vertex(j).x() ;
80 if(miny > primitives[i]->vertex(j).y()) miny = primitives[i]->vertex(j).y() ;
83 glMatrixMode(GL_PROJECTION) ;
85 glOrtho(minx,maxx,miny,maxy,-1,1) ;
86 glMatrixMode(GL_MODELVIEW) ;
89 cout <<
"Window set to " << minx <<
" " << maxx <<
" " << miny <<
" " << maxy << endl ;
90 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) ;
99 cumulated_union.num_contours = 0 ;
100 cumulated_union.hole = NULL ;
101 cumulated_union.contour = NULL ;
102 int nboptimised = 0 ;
104 for(
int pindex = (
int)(primitives.size()) - 1; pindex >= 0;--pindex,++nboptimised)
105 if(primitives[pindex] != NULL)
108 percentage_finished = pindex / (float)primitives.size() ;
111 if(primitives[pindex]->nbVertices() > 1)
117 sprintf(buff,
"Left: % 6ld - Culled: % 6ld", (
long)pindex,(
long)nb_culled) ;
118 fprintf(stdout,buff);
120 for(
unsigned int j=0;j<strlen(buff);++j)
121 fprintf(stdout,
"\b") ;
133 new_poly.num_contours = 0 ;
134 new_poly.hole = NULL ;
135 new_poly.contour = NULL ;
136 new_poly_reduced.num_contours = 0 ;
137 new_poly_reduced.hole = NULL ;
138 new_poly_reduced.contour = NULL ;
148 if(p->nbVertices() == 2)
150 new_poly_verts->num_vertices = 4 ;
152 new_poly_reduced_verts->num_vertices = 4 ;
153 new_poly_reduced_verts->vertex =
new gpc_vertex[4] ;
155 double deps = 0.001 ;
156 double du = p->vertex(1).y()-p->vertex(0).y() ;
157 double dv = p->vertex(1).x()-p->vertex(0).x() ;
158 double n = sqrt(du*du+dv*dv) ;
161 new_poly_verts->vertex[0].x = p->vertex(0).x() + du ;
162 new_poly_verts->vertex[0].y = p->vertex(0).y() + dv ;
163 new_poly_verts->vertex[1].x = p->vertex(1).x() + du ;
164 new_poly_verts->vertex[1].y = p->vertex(1).y() + dv ;
165 new_poly_verts->vertex[2].x = p->vertex(1).x() - du ;
166 new_poly_verts->vertex[2].y = p->vertex(1).y() - dv ;
167 new_poly_verts->vertex[3].x = p->vertex(0).x() - du ;
168 new_poly_verts->vertex[3].y = p->vertex(0).y() - dv ;
170 new_poly_reduced_verts->vertex[0].x = p->vertex(0).x() + du ;
171 new_poly_reduced_verts->vertex[0].y = p->vertex(0).y() + dv ;
172 new_poly_reduced_verts->vertex[1].x = p->vertex(1).x() + du ;
173 new_poly_reduced_verts->vertex[1].y = p->vertex(1).y() + dv ;
174 new_poly_reduced_verts->vertex[2].x = p->vertex(1).x() - du ;
175 new_poly_reduced_verts->vertex[2].y = p->vertex(1).y() - dv ;
176 new_poly_reduced_verts->vertex[3].x = p->vertex(0).x() - du ;
177 new_poly_reduced_verts->vertex[3].y = p->vertex(0).y() - dv ;
181 new_poly_verts->num_vertices = p->nbVertices() ;
182 new_poly_verts->vertex =
new gpc_vertex[p->nbVertices()] ;
184 for(
unsigned int i=0;i<p->nbVertices();++i)
186 new_poly_verts->vertex[i].x = p->vertex(i).x() ;
187 new_poly_verts->vertex[i].y = p->vertex(i).y() ;
188 mx += p->vertex(i).x() ;
189 my += p->vertex(i).y() ;
191 mx /= p->nbVertices() ;
192 my /= p->nbVertices() ;
194 new_poly_reduced_verts->num_vertices = p->nbVertices() ;
195 new_poly_reduced_verts->vertex =
new gpc_vertex[p->nbVertices()] ;
197 for(
unsigned int j=0;j<p->nbVertices();++j)
199 new_poly_reduced_verts->vertex[j].x = mx + (p->vertex(j).x() - mx)*0.999 ;
200 new_poly_reduced_verts->vertex[j].y = my + (p->vertex(j).y() - my)*0.999 ;
203 gpc_add_contour(&new_poly,new_poly_verts,
false) ;
204 gpc_add_contour(&new_poly_reduced,new_poly_reduced_verts,
false) ;
209 gpc_polygon_clip(GPC_DIFF,&new_poly_reduced,&cumulated_union,&difference) ;
214 if(difference.num_contours == 0)
218 primitives[pindex] = NULL ;
225 if(p->nbVertices() > 2)
228 cumulated_union_tmp.num_contours = 0 ;
229 cumulated_union_tmp.hole = NULL ;
230 cumulated_union_tmp.contour = NULL ;
232 gpc_polygon_clip(GPC_UNION,&new_poly,&cumulated_union,&cumulated_union_tmp) ;
234 gpc_free_polygon(&cumulated_union) ;
235 cumulated_union = cumulated_union_tmp ;
238 gpc_free_polygon(&new_poly) ;
239 gpc_free_polygon(&new_poly_reduced) ;
240 gpc_free_polygon(&difference) ;
241 #ifdef DEBUG_EPSRENDER__SHOW1
242 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT) ;
244 glColor3f(1.0,0.0,0.0) ;
246 for(
int i=0;i<cumulated_union.num_contours;++i)
248 glBegin(GL_LINE_LOOP) ;
249 for(
int j=0;j<cumulated_union.contour[i].num_vertices;++j)
250 glVertex2f(cumulated_union.contour[i].vertex[j].x,cumulated_union.contour[i].vertex[j].y) ;
255 glXSwapBuffers(glXGetCurrentDisplay(),glXGetCurrentDrawable()) ;
265 vparams.progress(nboptimised/(
float)primitives.size(), QGLViewer::tr(
"Visibility optimization")) ;
269 cout << nb_culled <<
" primitives culled over " << primitives.size() <<
"." << endl ;
272 gpc_free_polygon(&cumulated_union) ;