/** Set the number of polygons. Allocate new polygons or delete polygons (if the number is reduced). Setting the number to 0 deletes all polys. \param num New number of polys */ void PolyhedronGeom::setNumPolys(int num) { if (num<0) num=0; int prevsize = polys.size(); int i; // Delete polygons if the number of polys was decreased if (num<prevsize) { for(i=num; i<prevsize; i++) { deletePoly(i); } } // Resize poly list polys.resize(num); // Allocate new polys (which have 1 loop by default)... for(i=prevsize; i<num; i++) { polys[i] = new Poly(); setNumLoops(i, 1); } // Update the size constraint for uniform and facevarying variables UserSizeConstraint* usc = dynamic_cast<UserSizeConstraint*>(uniformSizeConstraint.get()); if (usc!=0) usc->setSize(num); usc = dynamic_cast<UserSizeConstraint*>(faceVaryingSizeConstraint.get()); if (usc!=0) usc->setSize(faceVaryingCount()); }
int main(int argc, char const *argv[]){ int i,a,b; int tmp; Poly li, lp, mulp, sump; li = newPoly(); lp = newPoly(); if ( NULL == li || NULL == lp) return 1; li->exp = 0; lp->exp = 0; srand( time(NULL) ); for(i = 0 ; i< LSIZE; i++){ a = rand()/(RAND_MAX/LSIZE); b = rand()/(RAND_MAX/LSIZE); addItem( a, b, li); addItem( b, a, lp); } //removeDup( li); //removeDup( lp); printPoly( li ); printPoly( lp ); mulp = polyMultiply2( li, lp); printf("\nmultiply : " ); printPoly( mulp ); deletePoly( mulp); sump = polyAdd( li, lp); printf("\nadd : " ); printPoly( sump ); deletePoly( sump ); printf("\n li ^2: "); printPoly( polyExp(li,2) ); //swapNext( first(li), li ); deletePoly( li ); deletePoly( lp ); return 0; }
/* * deallocate memory used by poly. must not use poly after that. * if delete_poly==1 then free memory of poly */ void deletePoly(Poly *poly, int delete_poly) { int i; if(poly == NULL) { return; } if(poly->verts != NULL) { free(poly->verts); } if(poly->triangles != NULL) { for(i=0; i<poly->num_triangles; i++) { deletePoly(poly->triangles+i, 0); } free(poly->triangles); } if(poly->normals != NULL) { free(poly->normals); } if(poly->dists != NULL) { free(poly->dists); } if(delete_poly) { free(poly); } }
Filter* getFilter(int (*transform)(void*, Point*, Point*), void *params, int dim_x, int dim_y, Point pt0) { Filter *tg; Point *t_grid; //transformed grid Poly *cell_poly, *poly; Vertex *v; char *is_transformed; int dim; float min_x, max_x, min_y, max_y; float area; int i, j, k; int ii, jj; int index; int last, allocated; if(params==NULL) return NULL; tg=(Filter*)calloc(1, sizeof(Filter)); tg->dim_x=dim_x; tg->dim_y=dim_y; tg->x0=pt0.x; tg->y0=pt0.y; tg->dx=tg->dy=1; dim=tg->dim_x*tg->dim_y; tg->num_pts=(int*)calloc(dim, sizeof(int)); tg->pts=(Point**)calloc(dim, sizeof(Point*)); tg->coefs=(float**)calloc(dim, sizeof(float*)); // compute the transformed grid (used to get the polygons corresponding to the cells. t_grid=(Point*)calloc((tg->dim_x+1)*(tg->dim_y+1), sizeof(Point)); is_transformed=(char*)calloc((tg->dim_x+1)*(tg->dim_y+1), sizeof(char)); for(k=0, j=0; j<=tg->dim_y; j++) { for(i=0; i<=tg->dim_x; i++, k++) { t_grid[k].x=tg->x0+i*tg->dx-tg->dx/2; t_grid[k].y=tg->y0+j*tg->dy-tg->dy/2; is_transformed[k]=(char)transform(params, t_grid+k, t_grid+k); if(is_transformed[k]==-1) return NULL; //should never happen! } } //for each cell, get the corresponding polygon, transform it, get all the overlapping cells, //(number of overlaping cells=numPoints[k]) and compute percentage of area inside each of these // cells cell_poly=generatePoly(4); cell_poly->verts[0].x=0; cell_poly->verts[0].y=1; cell_poly->verts[1].x=1; cell_poly->verts[1].y=1; cell_poly->verts[2].x=1; cell_poly->verts[2].y=0; cell_poly->verts[3].x=0; cell_poly->verts[3].y=0; setSides(cell_poly); // verts won't be used from now on, just normals and dists. only update dists for(index=0, k=0, j=0; j<tg->dim_y; j++, index++) { for(i=0; i<tg->dim_x; i++, k++, index++) { if(!(is_transformed[index] && is_transformed[index+1] && is_transformed[index+tg->dim_x+1] && is_transformed[index+tg->dim_x+2])) continue; if(!is_transformed[index] || !is_transformed[index+1] || !is_transformed[index+tg->dim_x+1] || !is_transformed[index+tg->dim_x+2]) { // cell contains border of transformation area. // TODO: handle this case continue; } poly=generatePoly(4); v=poly->verts; v[0].x=t_grid[index+tg->dim_x+1].x; v[0].y=t_grid[index+tg->dim_x+1].y; v[1].x=t_grid[index+tg->dim_x+2].x; v[1].y=t_grid[index+tg->dim_x+2].y; v[2].x=t_grid[index+1].x; v[2].y=t_grid[index+1].y; v[3].x=t_grid[index].x; v[3].y=t_grid[index].y; if(convexify4(poly)==-1) { deletePoly(poly, 1); continue; } setSides(poly); computeArea(poly); min_x=max_x=v[0].x; min_y=max_y=v[0].y; for(ii=1; ii<4; ii++) { if(v[ii].x<min_x) min_x=v[ii].x; else if(v[ii].x>max_x) max_x=v[ii].x; if(v[ii].y<min_y) min_y=v[ii].y; else if(v[ii].y>max_y) max_y=v[ii].y; } min_x=floorf(min_x); max_x=ceilf(max_x); min_y=floorf(min_y); max_y=ceilf(max_y); allocated=0; if((max_x-min_x) > 100 ||(max_y-min_y) > 100) { tg->num_pts[k]=1; tg->coefs[k]=(float*)calloc(1, sizeof(float)); tg->pts[k]=(Point*)calloc(1, sizeof(Point)); tg->coefs[k][0]=1; tg->pts[k][0].x=floor((max_x-min_x)/2); //TODO: fix that! tg->pts[k][0].y=floor((max_y-min_y)/2); deletePoly(poly, 1); continue; } for(jj=(int)min_y; jj<(int)max_y; jj++) { for(ii=(int)min_x; ii<(int)max_x; ii++) { cell_poly->dists[0]=jj+1; cell_poly->dists[1]=ii+1; cell_poly->dists[2]=-jj; cell_poly->dists[3]=-ii; area=intersectionArea(poly, cell_poly); if(area<SMALL) continue; last=tg->num_pts[k]++; if(tg->num_pts[k]>allocated) { allocated+=32; tg->coefs[k]=(float*)realloc(tg->coefs[k], allocated*sizeof(float)); tg->pts[k]=(Point*)realloc(tg->pts[k], allocated*sizeof(Point)); } tg->coefs[k][last]=area/poly->area; tg->pts[k][last].x=ii; tg->pts[k][last].y=jj; } } tg->coefs[k]=(float*)realloc(tg->coefs[k], tg->num_pts[k]*sizeof(float)); tg->pts[k]=(Point*)realloc(tg->pts[k], tg->num_pts[k]*sizeof(Point)); deletePoly(poly, 1); } //for(i) } //for(j) free(t_grid); free(is_transformed); return tg; }