ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
(Generate patch)

Comparing ray/src/hd/rhd_qtree.c (file contents):
Revision 3.11 by gregl, Fri Dec 5 15:40:54 1997 UTC vs.
Revision 3.16 by gregl, Wed Dec 31 09:06:54 1997 UTC

# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ SGI";
16   #endif
17                                  /* maximum allowed angle difference (deg.) */
18   #ifndef MAXANG
19 < #define MAXANG          20.
19 > #define MAXANG          20
20   #endif
21 + #if MAXANG>0
22 + #define MAXDIFF2        ( MAXANG*MAXANG * (PI*PI/180./180.))
23 + #endif
24  
22 #define MAXDIFF2        (PI*PI/180./180.* MAXANG*MAXANG )
23
25   #define abs(i)          ((i) < 0 ? -(i) : (i))
26  
27   RTREE   qtrunk;                 /* our quadtree trunk */
# Line 28 | Line 29 | double qtDepthEps = .05;       /* epsilon to compare depths
29   int     qtMinNodesiz = 2;       /* minimum node dimension (pixels) */
30   struct rleaves  qtL;            /* our pile of leaves */
31  
32 + static int4     falleaves;      /* our list of fallen leaves */
33 +
34 + #define composted(li)   (qtL.bl <= qtL.tl ? \
35 +                                        ((li) < qtL.bl || (li) >= qtL.tl) : \
36 +                                        ((li) < qtL.bl && (li) >= qtL.tl))
37 +
38   #define TBUNDLESIZ      409     /* number of twigs in a bundle */
39  
40   static RTREE    **twigbundle;   /* free twig blocks (NULL term.) */
41   static int      nexttwig;       /* next free twig */
42  
36 #define ungetleaf(li)   (qtL.tl=(li))   /* dangerous if used improperly */
43  
38
44   static RTREE *
45   newtwig()                       /* allocate a twig */
46   {
# Line 88 | Line 93 | int    really;
93   }
94  
95  
91 static int
92 newleaf()                       /* allocate a leaf from our pile */
93 {
94        int     li;
95        
96        li = qtL.tl++;
97        if (qtL.tl >= qtL.nl)   /* get next leaf in ring */
98                qtL.tl = 0;
99        if (qtL.tl == qtL.bl)   /* need to shake some free */
100                qtCompost(LFREEPCT);
101        return(li);
102 }
103
104
96   #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
97                          sizeof(TMbright)+6*sizeof(BYTE))
98  
# Line 135 | Line 126 | register int   n;
126          qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
127          qtL.nl = n;
128          qtL.tml = qtL.bl = qtL.tl = 0;
129 +        falleaves = -1;
130          return(n);
131   }
132  
# Line 165 | Line 157 | register RTREE *tp;
157                                  tp->flgs &= ~BRF(i);
158                  } else if (tp->flgs & LFF(i)) {
159                          li = tp->k[i].li;
160 <                        if (qtL.bl < qtL.tl ?
169 <                                (li < qtL.bl || li >= qtL.tl) :
170 <                                (li < qtL.bl && li >= qtL.tl))
160 >                        if (composted(li))
161                                  tp->flgs &= ~LFF(i);
162                  }
163   }
# Line 177 | Line 167 | int
167   qtCompost(pct)                  /* free up some leaves */
168   int     pct;
169   {
170 +        register int4   *fl;
171          int     nused, nclear, nmapped;
181
172                                  /* figure out how many leaves to clear */
173          nclear = qtL.nl * pct / 100;
174          nused = qtL.tl - qtL.bl;
# Line 189 | Line 179 | int    pct;
179          if (nclear >= nused) {  /* clear them all */
180                  qtFreeTree(0);
181                  qtL.tml = qtL.bl = qtL.tl = 0;
182 +                falleaves = -1;
183                  return(nused);
184          }
185                                  /* else clear leaves from bottom */
# Line 197 | Line 188 | int    pct;
188          qtL.bl += nclear;
189          if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl;
190          if (nmapped <= nclear) qtL.tml = qtL.bl;
191 <        shaketree(&qtrunk);
191 >        shaketree(&qtrunk);     /* dereference composted leaves */
192 >        for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl)
193 >                while (composted(*fl))
194 >                        if ((*fl = qtL.wd[*fl]) < 0)
195 >                                return(nclear);
196          return(nclear);
197   }
198  
199  
205 #define DCSCALE         11585.2         /* (1<<13)*sqrt(2) */
206 #define FXNEG           01
207 #define FYNEG           02
208 #define FZNEG           04
209 #define FXACT           010
210 #define FZACT           020
211 #define F1SFT           5
212 #define F2SFT           18
213 #define FMASK           0x1fff
214
215 static int4
216 encodedir(dv)           /* encode a normalized direction vector */
217 FVECT   dv;
218 {
219        register int4   dc = 0;
220        int     cd[3], cm;
221        register int    i;
222
223        for (i = 0; i < 3; i++)
224                if (dv[i] < 0.) {
225                        cd[i] = dv[i] * -DCSCALE;
226                        dc |= 1<<i;
227                } else
228                        cd[i] = dv[i] * DCSCALE;
229        if (cd[0] <= cd[1]) {
230                dc |= FXACT | cd[0] << F1SFT;
231                cm = cd[1];
232        } else {
233                dc |= cd[1] << F1SFT;
234                cm = cd[0];
235        }
236        if (cd[2] <= cm)
237                dc |= FZACT | cd[2] << F2SFT;
238        else
239                dc |= cm << F2SFT;
240        return(dc);
241 }
242
243
244 static
245 decodedir(dv, dc)       /* decode a normalized direction vector */
246 FVECT   dv;     /* returned */
247 register int4   dc;
248 {
249        double  d1, d2, der;
250
251        d1 = ((dc>>F1SFT & FMASK)+.5)/DCSCALE;
252        d2 = ((dc>>F2SFT & FMASK)+.5)/DCSCALE;
253        der = sqrt(1. - d1*d1 - d2*d2);
254        if (dc & FXACT) {
255                dv[0] = d1;
256                if (dc & FZACT) { dv[1] = der; dv[2] = d2; }
257                else { dv[1] = d2; dv[2] = der; }
258        } else {
259                dv[1] = d1;
260                if (dc & FZACT) { dv[0] = der; dv[2] = d2; }
261                else { dv[0] = d2; dv[2] = der; }
262        }
263        if (dc & FXNEG) dv[0] = -dv[0];
264        if (dc & FYNEG) dv[1] = -dv[1];
265        if (dc & FZNEG) dv[2] = -dv[2];
266 }
267
268
269 static double
270 dir2diff(dc1, dc2)              /* relative radians^2 between directions */
271 int4    dc1, dc2;
272 {
273        FVECT   v1, v2;
274
275        decodedir(v1, dc1);
276        decodedir(v2, dc2);
277
278        return(2. - 2.*DOT(v1,v2));
279 }
280
281
282 static double
283 fdir2diff(dc1, v2)              /* relative radians^2 between directions */
284 int4    dc1;
285 register FVECT  v2;
286 {
287        FVECT   v1;
288
289        decodedir(v1, dc1);
290
291        return(2. - 2.*DOT(v1,v2));
292 }
293
294
200   int
201   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
202   int     x, y;
# Line 330 | Line 235 | int    x, y;
235  
236  
237   static
238 < addleaf(li)                     /* add a leaf to our tree */
239 < int     li;
238 > putleaf(li, drop)               /* put a leaf in our tree */
239 > register int    li;
240 > int     drop;
241   {
242          register RTREE  *tp = &qtrunk;
243          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
244 <        int     lo = -1;
244 >        register int    lo = -1;
245          double  d2;
246          int     x, y, mx, my;
247          double  z;
248          FVECT   ip, wp, vd;
249          register int    q;
250 +                                        /* check for dead leaf */
251 +        if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2]))
252 +                return(0);
253                                          /* compute leaf location in view */
254          VCOPY(wp, qtL.wp[li]);
255          viewloc(ip, &odev.v, wp);
256          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
257                          || ip[1] < 0. || ip[1] >= 1.)
258 <                return(0);                      /* behind or outside view */
258 >                goto dropit;                    /* behind or outside view */
259   #ifdef DEBUG
260          if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
261 <                error(INTERNAL, "bad view assumption in addleaf");
261 >                error(INTERNAL, "bad view assumption in putleaf");
262   #endif
263          for (q = 0; q < 3; q++)
264                  vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
265          d2 = fdir2diff(qtL.wd[li], vd);
266 + #ifdef MAXDIFF2
267          if (d2 > MAXDIFF2)
268 <                return(0);                      /* leaf dir. too far off */
268 >                goto dropit;                    /* leaf dir. too far off */
269 > #endif
270          x = ip[0] * odev.hres;
271          y = ip[1] * odev.vres;
272          z = ip[2];
# Line 376 | Line 287 | int    li;
287                  if (!(tp->flgs & LFF(q))) {     /* found stem for leaf */
288                          tp->k[q].li = li;
289                          tp->flgs |= CHLFF(q);
290 <                        break;
290 >                        return(1);
291                  }      
292                  if (lo != tp->k[q].li) {        /* check old leaf */
293                          lo = tp->k[q].li;
# Line 386 | Line 297 | int    li;
297                                                  /* is node minimum size? */
298                  if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
299                          if (z > (1.+qtDepthEps)*ip[2])
300 <                                return(0);              /* old one closer */
300 >                                break;                  /* old one closer */
301                          if (z >= (1.-qtDepthEps)*ip[2] &&
302                                          fdir2diff(qtL.wd[lo], vd) < d2)
303 <                                return(0);              /* old one better */
304 <                        tp->k[q].li = li;               /* else new one is */
303 >                                break;                  /* old one better */
304 >                        tp->k[q].li = li;               /* attach new */
305                          tp->flgs |= CHF(q);
306 +                        li = lo;                        /* drop old... */
307                          break;
308                  }
309                  tp->flgs &= ~LFF(q);            /* else grow tree */
# Line 405 | Line 317 | int    li;
317                  tp->flgs = CH_ANY|LFF(q);       /* all new */
318                  tp->k[q].li = lo;
319          }
320 <        return(1);              /* done */
320 > dropit:
321 >        if (drop)
322 >                if (li+1 == (qtL.tl ? qtL.tl : qtL.nl))
323 >                        qtL.tl = li;            /* special case */
324 >                else {
325 >                        qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0;
326 >                        qtL.wd[li] = falleaves;
327 >                        falleaves = li;
328 >                }
329 >        return(li == lo);
330   }
331  
332  
# Line 414 | Line 335 | COLR   c;
335   FVECT   p, v;
336   {
337          register int    li;
338 <
339 <        li = newleaf();
338 >        int     mapit;
339 >                                /* grab a leaf */
340 >        if (!imm_mode && falleaves >= 0) {      /* check for fallen leaves */
341 >                li = falleaves;
342 >                falleaves = qtL.wd[li];
343 >                mapit = qtL.tml <= qtL.tl ?
344 >                                (li < qtL.tml || li >= qtL.tl) :
345 >                                (li < qtL.tml && li >= qtL.tl) ;
346 >        } else {                                /* else allocate new one */
347 >                li = qtL.tl++;
348 >                if (qtL.tl >= qtL.nl)           /* next leaf in ring */
349 >                        qtL.tl = 0;
350 >                if (qtL.tl == qtL.bl)           /* need to shake some free */
351 >                        qtCompost(LFREEPCT);
352 >                mapit = 0;                      /* we'll map it later */
353 >        }
354          VCOPY(qtL.wp[li], p);
355          qtL.wd[li] = encodedir(v);
356          tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
357 <        if (!addleaf(li))
358 <                ungetleaf(li);
357 >        if (putleaf(li, 1) && mapit)
358 >                tmMapPixels(qtL.rgb+li, qtL.brt+li, qtL.chr+li, 1);
359   }
360  
361  
# Line 433 | Line 368 | qtReplant()                    /* replant our tree using new view */
368          qtFreeTree(0);                  /* blow the old tree away */
369                                          /* regrow it in new place */
370          for (i = qtL.bl; i != qtL.tl; ) {
371 <                addleaf(i);
371 >                putleaf(i, 0);
372                  if (++i >= qtL.nl) i = 0;
373          }
374   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines