void dividespace(int maxoctnodes, object **toplist) { bndbox * gbox; vector gmin, gmax; if (countobj(*toplist) > maxoctnodes) { globalbound(toplist, &gmin, &gmax); octreespace(toplist, maxoctnodes); gbox = newbndbox(gmin, gmax); gbox->objlist = NULL; gbox->tex = NULL; gbox->nextobj=NULL; gbox->objlist=*toplist; *toplist=(object *) gbox; } }
int engrid_scene(object ** list) { grid * g; int numobj, numcbrt; vector gmin, gmax; gridindex index; if (*list == NULL) return 0; numobj = countobj(*list); if ( !silent_mode ) fprintf(stderr, "Scene contains %d bounded objects.\n", numobj); if (numobj > 16) { numcbrt = (int) cbrt(4*numobj); globalbound(list, &gmin, &gmax); g = (grid *) newgrid(numcbrt, numcbrt, numcbrt, gmin, gmax); engrid_objlist(g, list); numobj = countobj(*list); g->nextobj = *list; *list = (object *) g; /* now create subgrids.. */ for (index.z=0; index.z<g->zsize; index.z++) { for (index.y=0; index.y<g->ysize; index.y++) { for (index.x=0; index.x<g->xsize; index.x++) { engrid_cell(g, &index); } } } } return 1; }
static void octreespace(object ** rootlist, int maxoctnodes) { object * cur; vector gmin, gmax, gctr; vector cmin1, cmin2, cmin3, cmin4, cmin5, cmin6, cmin7, cmin8; vector cmax1, cmax2, cmax3, cmax4, cmax5, cmax6, cmax7, cmax8; bndbox * box1, * box2, * box3, * box4; bndbox * box5, * box6, * box7, * box8; int skipobj; if (*rootlist == NULL) /* don't subdivide non-existant data */ return; skipobj=0; globalbound(rootlist, &gmin, &gmax); /* find global min and max */ gctr.x = ((gmax.x - gmin.x) / 2.0) + gmin.x; gctr.y = ((gmax.y - gmin.y) / 2.0) + gmin.y; gctr.z = ((gmax.z - gmin.z) / 2.0) + gmin.z; cmin1=gmin; cmax1=gctr; box1 = newbndbox(cmin1, cmax1); cmin2=gmin; cmin2.x=gctr.x; cmax2=gmax; cmax2.y=gctr.y; cmax2.z=gctr.z; box2 = newbndbox(cmin2, cmax2); cmin3=gmin; cmin3.y=gctr.y; cmax3=gmax; cmax3.x=gctr.x; cmax3.z=gctr.z; box3 = newbndbox(cmin3, cmax3); cmin4=gmin; cmin4.x=gctr.x; cmin4.y=gctr.y; cmax4=gmax; cmax4.z=gctr.z; box4 = newbndbox(cmin4, cmax4); cmin5=gmin; cmin5.z=gctr.z; cmax5=gctr; cmax5.z=gmax.z; box5 = newbndbox(cmin5, cmax5); cmin6=gctr; cmin6.y=gmin.y; cmax6=gmax; cmax6.y=gctr.y; box6 = newbndbox(cmin6, cmax6); cmin7=gctr; cmin7.x=gmin.x; cmax7=gctr; cmax7.y=gmax.y; cmax7.z=gmax.z; box7 = newbndbox(cmin7, cmax7); cmin8=gctr; cmax8=gmax; box8 = newbndbox(cmin8, cmax8); cur = *rootlist; while (cur != NULL) { if (objinside((object *)cur->nextobj, &cmin1, &cmax1)) { movenextobj(cur, &box1->objlist); } else if (objinside((object *)cur->nextobj, &cmin2, &cmax2)) { movenextobj(cur, &box2->objlist); } else if (objinside((object *)cur->nextobj, &cmin3, &cmax3)) { movenextobj(cur, &box3->objlist); } else if (objinside((object *)cur->nextobj, &cmin4, &cmax4)) { movenextobj(cur, &box4->objlist); } else if (objinside((object *)cur->nextobj, &cmin5, &cmax5)) { movenextobj(cur, &box5->objlist); } else if (objinside((object *)cur->nextobj, &cmin6, &cmax6)) { movenextobj(cur, &box6->objlist); } else if (objinside((object *)cur->nextobj, &cmin7, &cmax7)) { movenextobj(cur, &box7->objlist); } else if (objinside((object *)cur->nextobj, &cmin8, &cmax8)) { movenextobj(cur, &box8->objlist); } else { skipobj++; cur=(object *)cur->nextobj; } } /* new scope, for redefinition of cur, and old */ { bndbox * cur, * old; old=box1; cur=box2; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } cur=box3; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } cur=box4; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } cur=box5; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } cur=box6; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } cur=box7; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } cur=box8; if (countobj(cur->objlist) > 0) { old->nextobj=cur; globalbound(&cur->objlist, &cur->min, &cur->max); old=cur; } old->nextobj=*rootlist; if (countobj(box1->objlist) > 0) { globalbound(&box1->objlist, &box1->min, &box1->max); *rootlist=(object *) box1; } else { *rootlist=(object *) box1->nextobj; } } /**** end of special cur and old scope */ if (countobj(box1->objlist) > maxoctnodes) { octreespace(&box1->objlist, maxoctnodes); } if (countobj(box2->objlist) > maxoctnodes) { octreespace(&box2->objlist, maxoctnodes); } if (countobj(box3->objlist) > maxoctnodes) { octreespace(&box3->objlist, maxoctnodes); } if (countobj(box4->objlist) > maxoctnodes) { octreespace(&box4->objlist, maxoctnodes); } if (countobj(box5->objlist) > maxoctnodes) { octreespace(&box5->objlist, maxoctnodes); } if (countobj(box6->objlist) > maxoctnodes) { octreespace(&box6->objlist, maxoctnodes); } if (countobj(box7->objlist) > maxoctnodes) { octreespace(&box7->objlist, maxoctnodes); } if (countobj(box8->objlist) > maxoctnodes) { octreespace(&box8->objlist, maxoctnodes); } }