--- ray/src/hd/rhd_qtree.c 1997/11/24 15:16:10 3.6 +++ ray/src/hd/rhd_qtree.c 2004/01/01 11:21:55 3.25 @@ -1,35 +1,54 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rhd_qtree.c,v 3.25 2004/01/01 11:21:55 schorsch Exp $"; #endif - /* * Quadtree driver support routines. */ +#include + #include "standard.h" #include "rhd_qtree.h" /* quantity of leaves to free at a time */ #ifndef LFREEPCT #define LFREEPCT 25 #endif + /* maximum allowed angle difference (deg.) */ +#ifndef MAXANG +#define MAXANG 20 +#endif +#if MAXANG>0 +#define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.)) +#endif +#define abs(i) ((i) < 0 ? -(i) : (i)) + RTREE qtrunk; /* our quadtree trunk */ -double qtDepthEps = .02; /* epsilon to compare depths (z fraction) */ +double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */ int qtMinNodesiz = 2; /* minimum node dimension (pixels) */ struct rleaves qtL; /* our pile of leaves */ +int rayqleft = 0; /* rays left to queue before flush */ + +static int32 falleaves; /* our list of fallen leaves */ + +#define composted(li) (qtL.bl <= qtL.tl ? \ + ((li) < qtL.bl || (li) >= qtL.tl) : \ + ((li) < qtL.bl && (li) >= qtL.tl)) + #define TBUNDLESIZ 409 /* number of twigs in a bundle */ static RTREE **twigbundle; /* free twig blocks (NULL term.) */ static int nexttwig; /* next free twig */ -#define is_stump(t) (!((t)->flgs & (BR_ANY|LF_ANY))) +static RTREE *newtwig(void); +static void qtFreeTree(int really); +static void shaketree(RTREE *tp); +static int putleaf(int li, int drop); static RTREE * -newtwig() /* allocate a twig */ +newtwig(void) /* allocate a twig */ { register int bi; @@ -41,7 +60,7 @@ newtwig() /* allocate a twig */ } bi = nexttwig / TBUNDLESIZ; if (twigbundle[bi] == NULL) { /* new block */ - twigbundle = (RTREE **)realloc((char *)twigbundle, + twigbundle = (RTREE **)realloc((void *)twigbundle, (bi+2)*sizeof(RTREE *)); if (twigbundle == NULL) goto memerr; @@ -54,50 +73,42 @@ newtwig() /* allocate a twig */ return(twigbundle[bi] + (nexttwig++ - bi*TBUNDLESIZ)); memerr: error(SYSTEM, "out of memory in newtwig"); + return NULL; /* pro forma return */ } -qtFreeTree(really) /* free allocated twigs */ -int really; +static void +qtFreeTree( /* free allocated twigs */ + int really +) { register int i; - qtrunk.flgs = CH_ANY; - nexttwig = 0; + qtrunk.flgs = CH_ANY; /* chop down tree */ if (twigbundle == NULL) return; + i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ; + nexttwig = 0; if (!really) { /* just clear allocated blocks */ - for (i = 0; twigbundle[i] != NULL; i++) - bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE)); + while (i--) + memset((char *)twigbundle[i], '\0', TBUNDLESIZ*sizeof(RTREE)); return; } /* else "really" means free up memory */ for (i = 0; twigbundle[i] != NULL; i++) - free((char *)twigbundle[i]); - free((char *)twigbundle); + free((void *)twigbundle[i]); + free((void *)twigbundle); twigbundle = NULL; } -static int -newleaf() /* allocate a leaf from our pile */ -{ - int li; - - li = qtL.tl++; - if (qtL.tl >= qtL.nl) /* get next leaf in ring */ - qtL.tl = 0; - if (qtL.tl == qtL.bl) /* need to shake some free */ - qtCompost(LFREEPCT); - return(li); -} +#define LEAFSIZ (3*sizeof(float)+sizeof(int32)+\ + sizeof(TMbright)+6*sizeof(BYTE)) - -#define LEAFSIZ (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE)) - -int -qtAllocLeaves(n) /* allocate space for n leaves */ -register int n; +extern int +qtAllocLeaves( /* allocate space for n leaves */ + register int n +) { unsigned nbytes; register unsigned i; @@ -119,18 +130,21 @@ register int n; return(0); /* assign larger alignment types earlier */ qtL.wp = (float (*)[3])qtL.base; - qtL.brt = (TMbright *)(qtL.wp + n); + qtL.wd = (int32 *)(qtL.wp + n); + qtL.brt = (TMbright *)(qtL.wd + n); qtL.chr = (BYTE (*)[3])(qtL.brt + n); qtL.rgb = (BYTE (*)[3])(qtL.chr + n); qtL.nl = n; qtL.tml = qtL.bl = qtL.tl = 0; + falleaves = -1; return(n); } #undef LEAFSIZ -qtFreeLeaves() /* free our allocated leaves and twigs */ +extern void +qtFreeLeaves(void) /* free our allocated leaves and twigs */ { qtFreeTree(1); /* free tree also */ if (qtL.nl <= 0) @@ -141,9 +155,10 @@ qtFreeLeaves() /* free our allocated leaves and twig } -static -shaketree(tp) /* shake dead leaves from tree */ -register RTREE *tp; +static void +shaketree( /* shake dead leaves from tree */ + register RTREE *tp +) { register int i, li; @@ -154,20 +169,19 @@ register RTREE *tp; tp->flgs &= ~BRF(i); } else if (tp->flgs & LFF(i)) { li = tp->k[i].li; - if (qtL.bl < qtL.tl ? - (li < qtL.bl || li >= qtL.tl) : - (li < qtL.bl && li >= qtL.tl)) + if (composted(li)) tp->flgs &= ~LFF(i); } } -int -qtCompost(pct) /* free up some leaves */ -int pct; +extern int +qtCompost( /* free up some leaves */ + int pct +) { + register int32 *fl; int nused, nclear, nmapped; - /* figure out how many leaves to clear */ nclear = qtL.nl * pct / 100; nused = qtL.tl - qtL.bl; @@ -178,6 +192,7 @@ int pct; if (nclear >= nused) { /* clear them all */ qtFreeTree(0); qtL.tml = qtL.bl = qtL.tl = 0; + falleaves = -1; return(nused); } /* else clear leaves from bottom */ @@ -186,14 +201,20 @@ int pct; qtL.bl += nclear; if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl; if (nmapped <= nclear) qtL.tml = qtL.bl; - shaketree(&qtrunk); + shaketree(&qtrunk); /* dereference composted leaves */ + for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl) + while (composted(*fl)) + if ((*fl = qtL.wd[*fl]) < 0) + return(nclear); return(nclear); } -int -qtFindLeaf(x, y) /* find closest leaf to (x,y) */ -int x, y; +extern int +qtFindLeaf( /* find closest leaf to (x,y) */ + int x, + int y +) { register RTREE *tp = &qtrunk; int li = -1; @@ -228,23 +249,40 @@ int x, y; } -static -addleaf(li) /* add a leaf to our tree */ -int li; +static int +putleaf( /* put a leaf in our tree */ + register int li, + int drop +) { register RTREE *tp = &qtrunk; int x0=0, y0=0, x1=odev.hres, y1=odev.vres; - int lo = -1; + register int lo = -1; + double d2; int x, y, mx, my; double z; - FVECT ip, wp; + FVECT ip, wp, vd; register int q; - /* compute leaf location */ + /* check for dead leaf */ + if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2])) + return(0); + /* compute leaf location in view */ VCOPY(wp, qtL.wp[li]); viewloc(ip, &odev.v, wp); if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. || ip[1] < 0. || ip[1] >= 1.) - return; + goto dropit; /* behind or outside view */ +#ifdef DEBUG + if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) + error(INTERNAL, "bad view assumption in putleaf"); +#endif + for (q = 0; q < 3; q++) + vd[q] = (wp[q] - odev.v.vp[q])/ip[2]; + d2 = fdir2diff(qtL.wd[li], vd); +#ifdef MAXDIFF2 + if (d2 > MAXDIFF2) + goto dropit; /* leaf dir. too far off */ +#endif x = ip[0] * odev.hres; y = ip[1] * odev.vres; z = ip[2]; @@ -265,20 +303,23 @@ int li; if (!(tp->flgs & LFF(q))) { /* found stem for leaf */ tp->k[q].li = li; tp->flgs |= CHLFF(q); - break; + return(1); } - /* check existing leaf */ - if (lo != tp->k[q].li) { + if (lo != tp->k[q].li) { /* check old leaf */ lo = tp->k[q].li; VCOPY(wp, qtL.wp[lo]); viewloc(ip, &odev.v, wp); } /* is node minimum size? */ - if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) { - if (z > (1.-qtDepthEps)*ip[2]) /* who is closer? */ - return; /* old one is */ - tp->k[q].li = li; /* new one is */ + if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { + if (z > (1.+qtDepthEps)*ip[2]) + break; /* old one closer */ + if (z >= (1.-qtDepthEps)*ip[2] && + fdir2diff(qtL.wd[lo], vd) < d2) + break; /* old one better */ + tp->k[q].li = li; /* attach new */ tp->flgs |= CHF(q); + li = lo; /* drop old... */ break; } tp->flgs &= ~LFF(q); /* else grow tree */ @@ -289,26 +330,65 @@ int li; my = ip[1] * odev.vres; if (mx >= (x0 + x1) >> 1) q |= 01; if (my >= (y0 + y1) >> 1) q |= 02; + tp->flgs = CH_ANY|LFF(q); /* all new */ tp->k[q].li = lo; - tp->flgs |= LFF(q)|CH_ANY; /* all new */ } +dropit: + if (drop) { + if (li+1 == (qtL.tl ? qtL.tl : qtL.nl)) + qtL.tl = li; /* special case */ + else { + qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0; + qtL.wd[li] = falleaves; + falleaves = li; + } + } + return(li == lo); } -dev_value(c, p) /* add a pixel value to our output queue */ -COLR c; -FVECT p; +extern void +dev_value( /* add a pixel value to our quadtree */ + COLR c, + FVECT d, + FVECT p +) { register int li; - - li = newleaf(); - VCOPY(qtL.wp[li], p); - tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); - addleaf(li); + int mapit; + /* grab a leaf */ + if (!imm_mode && falleaves >= 0) { /* check for fallen leaves */ + li = falleaves; + falleaves = qtL.wd[li]; + mapit = qtL.tml <= qtL.tl ? + (li < qtL.tml || li >= qtL.tl) : + (li < qtL.tml && li >= qtL.tl) ; + } else { /* else allocate new one */ + li = qtL.tl++; + if (qtL.tl >= qtL.nl) /* next leaf in ring */ + qtL.tl = 0; + if (qtL.tl == qtL.bl) /* need to shake some free */ + qtCompost(LFREEPCT); + mapit = 0; /* we'll map it later */ + } + if (p == NULL) + VSUM(qtL.wp[li], odev.v.vp, d, FHUGE); + else + VCOPY(qtL.wp[li], p); + qtL.wd[li] = encodedir(d); + tmCvColrs(&qtL.brt[li], qtL.chr[li], (COLR *)c, 1); + if (putleaf(li, 1)) { + if (mapit) + tmMapPixels((BYTE *)(qtL.rgb+li), qtL.brt+li, + (BYTE *)(qtL.chr+li), 1); + if (--rayqleft == 0) + dev_flush(); /* flush output */ + } } -qtReplant() /* replant our tree using new view */ +extern void +qtReplant(void) /* replant our tree using new view */ { register int i; /* anything to replant? */ @@ -317,14 +397,16 @@ qtReplant() /* replant our tree using new view */ qtFreeTree(0); /* blow the old tree away */ /* regrow it in new place */ for (i = qtL.bl; i != qtL.tl; ) { - addleaf(i); + putleaf(i, 0); if (++i >= qtL.nl) i = 0; } } -qtMapLeaves(redo) /* map our leaves to RGB */ -int redo; +extern int +qtMapLeaves( /* map our leaves to RGB */ + int redo +) { int aorg, alen, borg, blen; /* recompute mapping? */ @@ -352,150 +434,12 @@ int redo; if (tmComputeMapping(0., 0., 0.) != TM_E_OK) return(0); } - if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg, - qtL.chr+aorg, alen) != TM_E_OK) + if (tmMapPixels((BYTE *)(qtL.rgb+aorg), qtL.brt+aorg, + (BYTE *)(qtL.chr+aorg), alen) != TM_E_OK) return(0); if (blen > 0) - tmMapPixels(qtL.rgb+borg, qtL.brt+borg, - qtL.chr+borg, blen); + tmMapPixels((BYTE *)(qtL.rgb+borg), qtL.brt+borg, + (BYTE *)(qtL.chr+borg), blen); qtL.tml = qtL.tl; return(1); -} - - -static -redraw(ca, tp, x0, y0, x1, y1, l) /* redraw portion of a tree */ -BYTE ca[3]; /* returned average color */ -register RTREE *tp; -int x0, y0, x1, y1; -int l[2][2]; -{ - int csm[3], nc; - register BYTE *cp; - BYTE rgb[3]; - int quads = CH_ANY; - int mx, my; - register int i; - /* compute midpoint */ - mx = (x0 + x1) >> 1; - my = (y0 + y1) >> 1; - /* see what to do */ - if (l[0][0] >= mx) - quads &= ~(CHF(2)|CHF(0)); - else if (l[0][1] <= mx) - quads &= ~(CHF(3)|CHF(1)); - if (l[1][0] >= my) - quads &= ~(CHF(1)|CHF(0)); - else if (l[1][1] <= my) - quads &= ~(CHF(3)|CHF(2)); - tp->flgs &= ~quads; /* mark them done */ - csm[0] = csm[1] = csm[2] = nc = 0; - /* do leaves first */ - for (i = 0; i < 4; i++) - if (quads & CHF(i) && tp->flgs & LFF(i)) { - dev_paintr(cp=qtL.rgb[tp->k[i].li], - i&01 ? mx : x0, i&02 ? my : y0, - i&01 ? x1 : mx, i&02 ? y1 : my); - csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2]; - nc++; - quads &= ~CHF(i); - } - /* now do branches */ - for (i = 0; i < 4; i++) - if (quads & CHF(i) && tp->flgs & BRF(i)) { - redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, - i&01 ? x1 : mx, i&02 ? y1 : my, l); - csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; - nc++; - quads &= ~CHF(i); - } - if (nc > 1) { - ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; - } else { - ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; - } - if (!quads) return; - /* fill in gaps with average */ - for (i = 0; i < 4; i++) - if (quads & CHF(i)) - dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, - i&01 ? x1 : mx, i&02 ? y1 : my); -} - - -static -update(ca, tp, x0, y0, x1, y1) /* update tree display as needed */ -BYTE ca[3]; /* returned average color */ -register RTREE *tp; -int x0, y0, x1, y1; -{ - int csm[3], nc; - register BYTE *cp; - BYTE rgb[3]; - int gaps = 0; - int mx, my; - register int i; - /* compute midpoint */ - mx = (x0 + x1) >> 1; - my = (y0 + y1) >> 1; - csm[0] = csm[1] = csm[2] = nc = 0; - /* do leaves first */ - for (i = 0; i < 4; i++) { - if (!(tp->flgs & CHF(i))) - continue; - if (tp->flgs & LFF(i)) { - dev_paintr(cp=qtL.rgb[tp->k[i].li], - i&01 ? mx : x0, i&02 ? my : y0, - i&01 ? x1 : mx, i&02 ? y1 : my); - csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2]; - nc++; - } else if (!(tp->flgs & BRF(i))) - gaps |= 1<flgs & CHBRF(i)) == CHBRF(i)) { - update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, - i&01 ? x1 : mx, i&02 ? y1 : my); - csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; - nc++; - } - if (nc > 1) { - ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; - } else { - ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; - } - /* fill in gaps with average */ - for (i = 0; gaps && i < 4; gaps >>= 1, i++) - if (gaps & 01) - dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, - i&01 ? x1 : mx, i&02 ? y1 : my); - tp->flgs &= ~CH_ANY; /* all done */ -} - - -qtRedraw(x0, y0, x1, y1) /* redraw part or all of our screen */ -int x0, y0, x1, y1; -{ - int lim[2][2]; - BYTE ca[3]; - - if (is_stump(&qtrunk)) - return; - if (!qtMapLeaves((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 & - (lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1)) - return; - redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim); -} - - -qtUpdate() /* update our tree display */ -{ - BYTE ca[3]; - - if (is_stump(&qtrunk)) - return; - if (!qtMapLeaves(0)) - return; - update(ca, &qtrunk, 0, 0, odev.hres, odev.vres); }