ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
Revision: 3.14
Committed: Mon Dec 29 17:31:45 1997 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.13: +6 -18 lines
Log Message:
consolidated newleaf() with dev_value()

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Quadtree driver support routines.
9 */
10
11 #include "standard.h"
12 #include "rhd_qtree.h"
13 /* quantity of leaves to free at a time */
14 #ifndef LFREEPCT
15 #define LFREEPCT 25
16 #endif
17 /* maximum allowed angle difference (deg.) */
18 #ifndef MAXANG
19 #define MAXANG 20
20 #endif
21 #if MAXANG>0
22 #define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.))
23 #endif
24
25 #define abs(i) ((i) < 0 ? -(i) : (i))
26
27 RTREE qtrunk; /* our quadtree trunk */
28 double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */
29 int qtMinNodesiz = 2; /* minimum node dimension (pixels) */
30 struct rleaves qtL; /* our pile of leaves */
31
32 #define TBUNDLESIZ 409 /* number of twigs in a bundle */
33
34 static RTREE **twigbundle; /* free twig blocks (NULL term.) */
35 static int nexttwig; /* next free twig */
36
37
38 static RTREE *
39 newtwig() /* allocate a twig */
40 {
41 register int bi;
42
43 if (twigbundle == NULL) { /* initialize */
44 twigbundle = (RTREE **)malloc(sizeof(RTREE *));
45 if (twigbundle == NULL)
46 goto memerr;
47 twigbundle[0] = NULL;
48 }
49 bi = nexttwig / TBUNDLESIZ;
50 if (twigbundle[bi] == NULL) { /* new block */
51 twigbundle = (RTREE **)realloc((char *)twigbundle,
52 (bi+2)*sizeof(RTREE *));
53 if (twigbundle == NULL)
54 goto memerr;
55 twigbundle[bi] = (RTREE *)calloc(TBUNDLESIZ, sizeof(RTREE));
56 if (twigbundle[bi] == NULL)
57 goto memerr;
58 twigbundle[bi+1] = NULL;
59 }
60 /* nexttwig++ % TBUNDLESIZ */
61 return(twigbundle[bi] + (nexttwig++ - bi*TBUNDLESIZ));
62 memerr:
63 error(SYSTEM, "out of memory in newtwig");
64 }
65
66
67 qtFreeTree(really) /* free allocated twigs */
68 int really;
69 {
70 register int i;
71
72 qtrunk.flgs = CH_ANY; /* chop down tree */
73 if (twigbundle == NULL)
74 return;
75 i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ;
76 nexttwig = 0;
77 if (!really) { /* just clear allocated blocks */
78 while (i--)
79 bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE));
80 return;
81 }
82 /* else "really" means free up memory */
83 for (i = 0; twigbundle[i] != NULL; i++)
84 free((char *)twigbundle[i]);
85 free((char *)twigbundle);
86 twigbundle = NULL;
87 }
88
89
90 #define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\
91 sizeof(TMbright)+6*sizeof(BYTE))
92
93 int
94 qtAllocLeaves(n) /* allocate space for n leaves */
95 register int n;
96 {
97 unsigned nbytes;
98 register unsigned i;
99
100 qtFreeTree(0); /* make sure tree is empty */
101 if (n <= 0)
102 return(0);
103 if (qtL.nl >= n)
104 return(qtL.nl);
105 else if (qtL.nl > 0)
106 free(qtL.base);
107 /* round space up to nearest power of 2 */
108 nbytes = n*LEAFSIZ + 8;
109 for (i = 1024; nbytes > i; i <<= 1)
110 ;
111 n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */
112 qtL.base = (char *)malloc(n*LEAFSIZ);
113 if (qtL.base == NULL)
114 return(0);
115 /* assign larger alignment types earlier */
116 qtL.wp = (float (*)[3])qtL.base;
117 qtL.wd = (int4 *)(qtL.wp + n);
118 qtL.brt = (TMbright *)(qtL.wd + n);
119 qtL.chr = (BYTE (*)[3])(qtL.brt + n);
120 qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
121 qtL.nl = n;
122 qtL.tml = qtL.bl = qtL.tl = 0;
123 return(n);
124 }
125
126 #undef LEAFSIZ
127
128
129 qtFreeLeaves() /* free our allocated leaves and twigs */
130 {
131 qtFreeTree(1); /* free tree also */
132 if (qtL.nl <= 0)
133 return;
134 free(qtL.base);
135 qtL.base = NULL;
136 qtL.nl = 0;
137 }
138
139
140 static
141 shaketree(tp) /* shake dead leaves from tree */
142 register RTREE *tp;
143 {
144 register int i, li;
145
146 for (i = 0; i < 4; i++)
147 if (tp->flgs & BRF(i)) {
148 shaketree(tp->k[i].b);
149 if (is_stump(tp->k[i].b))
150 tp->flgs &= ~BRF(i);
151 } else if (tp->flgs & LFF(i)) {
152 li = tp->k[i].li;
153 if (qtL.bl < qtL.tl ?
154 (li < qtL.bl || li >= qtL.tl) :
155 (li < qtL.bl && li >= qtL.tl))
156 tp->flgs &= ~LFF(i);
157 }
158 }
159
160
161 int
162 qtCompost(pct) /* free up some leaves */
163 int pct;
164 {
165 int nused, nclear, nmapped;
166
167 /* figure out how many leaves to clear */
168 nclear = qtL.nl * pct / 100;
169 nused = qtL.tl - qtL.bl;
170 if (nused <= 0) nused += qtL.nl;
171 nclear -= qtL.nl - nused;
172 if (nclear <= 0)
173 return(0);
174 if (nclear >= nused) { /* clear them all */
175 qtFreeTree(0);
176 qtL.tml = qtL.bl = qtL.tl = 0;
177 return(nused);
178 }
179 /* else clear leaves from bottom */
180 nmapped = qtL.tml - qtL.bl;
181 if (nmapped < 0) nmapped += qtL.nl;
182 qtL.bl += nclear;
183 if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl;
184 if (nmapped <= nclear) qtL.tml = qtL.bl;
185 shaketree(&qtrunk);
186 return(nclear);
187 }
188
189
190 int
191 qtFindLeaf(x, y) /* find closest leaf to (x,y) */
192 int x, y;
193 {
194 register RTREE *tp = &qtrunk;
195 int li = -1;
196 int x0=0, y0=0, x1=odev.hres, y1=odev.vres;
197 int mx, my;
198 register int q;
199 /* check limits */
200 if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres)
201 return(-1);
202 /* find nearby leaf in our tree */
203 for ( ; ; ) {
204 for (q = 0; q < 4; q++) /* find any leaf this level */
205 if (tp->flgs & LFF(q)) {
206 li = tp->k[q].li;
207 break;
208 }
209 q = 0; /* which quadrant are we? */
210 mx = (x0 + x1) >> 1;
211 my = (y0 + y1) >> 1;
212 if (x < mx) x1 = mx;
213 else {x0 = mx; q |= 01;}
214 if (y < my) y1 = my;
215 else {y0 = my; q |= 02;}
216 if (tp->flgs & BRF(q)) { /* branch down if not a leaf */
217 tp = tp->k[q].b;
218 continue;
219 }
220 if (tp->flgs & LFF(q)) /* good shot! */
221 return(tp->k[q].li);
222 return(li); /* else return what we have */
223 }
224 }
225
226
227 static
228 addleaf(li) /* add a leaf to our tree */
229 int li;
230 {
231 register RTREE *tp = &qtrunk;
232 int x0=0, y0=0, x1=odev.hres, y1=odev.vres;
233 int lo = -1;
234 double d2;
235 int x, y, mx, my;
236 double z;
237 FVECT ip, wp, vd;
238 register int q;
239 /* compute leaf location in view */
240 VCOPY(wp, qtL.wp[li]);
241 viewloc(ip, &odev.v, wp);
242 if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
243 || ip[1] < 0. || ip[1] >= 1.)
244 return(-1); /* behind or outside view */
245 #ifdef DEBUG
246 if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
247 error(INTERNAL, "bad view assumption in addleaf");
248 #endif
249 for (q = 0; q < 3; q++)
250 vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
251 d2 = fdir2diff(qtL.wd[li], vd);
252 #ifdef MAXDIFF2
253 if (d2 > MAXDIFF2)
254 return(0); /* leaf dir. too far off */
255 #endif
256 x = ip[0] * odev.hres;
257 y = ip[1] * odev.vres;
258 z = ip[2];
259 /* find the place for it */
260 for ( ; ; ) {
261 q = 0; /* which quadrant? */
262 mx = (x0 + x1) >> 1;
263 my = (y0 + y1) >> 1;
264 if (x < mx) x1 = mx;
265 else {x0 = mx; q |= 01;}
266 if (y < my) y1 = my;
267 else {y0 = my; q |= 02;}
268 if (tp->flgs & BRF(q)) { /* move to next branch */
269 tp->flgs |= CHF(q); /* not sure; guess */
270 tp = tp->k[q].b;
271 continue;
272 }
273 if (!(tp->flgs & LFF(q))) { /* found stem for leaf */
274 tp->k[q].li = li;
275 tp->flgs |= CHLFF(q);
276 break;
277 }
278 if (lo != tp->k[q].li) { /* check old leaf */
279 lo = tp->k[q].li;
280 VCOPY(wp, qtL.wp[lo]);
281 viewloc(ip, &odev.v, wp);
282 }
283 /* is node minimum size? */
284 if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
285 if (z > (1.+qtDepthEps)*ip[2])
286 return(0); /* old one closer */
287 if (z >= (1.-qtDepthEps)*ip[2] &&
288 fdir2diff(qtL.wd[lo], vd) < d2)
289 return(0); /* old one better */
290 tp->k[q].li = li; /* else new one is */
291 tp->flgs |= CHF(q);
292 break;
293 }
294 tp->flgs &= ~LFF(q); /* else grow tree */
295 tp->flgs |= CHBRF(q);
296 tp = tp->k[q].b = newtwig();
297 q = 0; /* old leaf -> new branch */
298 mx = ip[0] * odev.hres;
299 my = ip[1] * odev.vres;
300 if (mx >= (x0 + x1) >> 1) q |= 01;
301 if (my >= (y0 + y1) >> 1) q |= 02;
302 tp->flgs = CH_ANY|LFF(q); /* all new */
303 tp->k[q].li = lo;
304 }
305 return(1); /* done */
306 }
307
308
309 dev_value(c, p, v) /* add a pixel value to our quadtree */
310 COLR c;
311 FVECT p, v;
312 {
313 register int li;
314
315 li = qtL.tl++;
316 if (qtL.tl >= qtL.nl) /* advance to next leaf in ring */
317 qtL.tl = 0;
318 if (qtL.tl == qtL.bl) /* need to shake some free */
319 qtCompost(LFREEPCT);
320 VCOPY(qtL.wp[li], p);
321 qtL.wd[li] = encodedir(v);
322 tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
323 if (!addleaf(li))
324 qtL.tl = li; /* unget this leaf */
325 }
326
327
328 qtReplant() /* replant our tree using new view */
329 {
330 register int i;
331 /* anything to replant? */
332 if (qtL.bl == qtL.tl)
333 return;
334 qtFreeTree(0); /* blow the old tree away */
335 /* regrow it in new place */
336 for (i = qtL.bl; i != qtL.tl; ) {
337 addleaf(i);
338 if (++i >= qtL.nl) i = 0;
339 }
340 }
341
342
343 qtMapLeaves(redo) /* map our leaves to RGB */
344 int redo;
345 {
346 int aorg, alen, borg, blen;
347 /* recompute mapping? */
348 if (redo)
349 qtL.tml = qtL.bl;
350 /* already done? */
351 if (qtL.tml == qtL.tl)
352 return(1);
353 /* compute segments */
354 aorg = qtL.tml;
355 if (qtL.tl >= aorg) {
356 alen = qtL.tl - aorg;
357 blen = 0;
358 } else {
359 alen = qtL.nl - aorg;
360 borg = 0;
361 blen = qtL.tl;
362 }
363 /* (re)compute tone mapping? */
364 if (qtL.tml == qtL.bl) {
365 tmClearHisto();
366 tmAddHisto(qtL.brt+aorg, alen, 1);
367 if (blen > 0)
368 tmAddHisto(qtL.brt+borg, blen, 1);
369 if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
370 return(0);
371 }
372 if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg,
373 qtL.chr+aorg, alen) != TM_E_OK)
374 return(0);
375 if (blen > 0)
376 tmMapPixels(qtL.rgb+borg, qtL.brt+borg,
377 qtL.chr+borg, blen);
378 qtL.tml = qtL.tl;
379 return(1);
380 }