1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Quadtree driver support routines. |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include <string.h> |
9 |
+ |
|
10 |
|
#include "standard.h" |
11 |
|
#include "rhd_qtree.h" |
12 |
+ |
/* quantity of leaves to free at a time */ |
13 |
+ |
#ifndef LFREEPCT |
14 |
+ |
#define LFREEPCT 25 |
15 |
+ |
#endif |
16 |
+ |
/* maximum allowed angle difference (deg.) */ |
17 |
+ |
#ifndef MAXANG |
18 |
+ |
#define MAXANG 20 |
19 |
+ |
#endif |
20 |
+ |
#if MAXANG>0 |
21 |
+ |
#define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.)) |
22 |
+ |
#endif |
23 |
|
|
24 |
+ |
#define abs(i) ((i) < 0 ? -(i) : (i)) |
25 |
+ |
|
26 |
|
RTREE qtrunk; /* our quadtree trunk */ |
27 |
< |
double qtDepthEps = .02; /* epsilon to compare depths (z fraction) */ |
27 |
> |
double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */ |
28 |
|
int qtMinNodesiz = 2; /* minimum node dimension (pixels) */ |
29 |
+ |
struct rleaves qtL; /* our pile of leaves */ |
30 |
|
|
31 |
< |
static RLEAF *leafpile; /* our collection of leaf values */ |
19 |
< |
static int nleaves; /* count of leaves in our pile */ |
20 |
< |
static int bleaf, tleaf; /* bottom and top (next) leaf index (ring) */ |
31 |
> |
int rayqleft = 0; /* rays left to queue before flush */ |
32 |
|
|
33 |
+ |
static int32 falleaves; /* our list of fallen leaves */ |
34 |
+ |
|
35 |
+ |
#define composted(li) (qtL.bl <= qtL.tl ? \ |
36 |
+ |
((li) < qtL.bl || (li) >= qtL.tl) : \ |
37 |
+ |
((li) < qtL.bl && (li) >= qtL.tl)) |
38 |
+ |
|
39 |
|
#define TBUNDLESIZ 409 /* number of twigs in a bundle */ |
40 |
|
|
41 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
42 |
|
static int nexttwig; /* next free twig */ |
43 |
|
|
44 |
< |
static RTREE emptytree; /* empty tree for test below */ |
44 |
> |
static RTREE *newtwig(void); |
45 |
> |
static void qtFreeTree(int really); |
46 |
> |
static void shaketree(RTREE *tp); |
47 |
> |
static int putleaf(int li, int drop); |
48 |
|
|
29 |
– |
#define is_stump(t) (!bcmp((char *)(t), (char *)&emptytree, sizeof(RTREE))) |
49 |
|
|
31 |
– |
|
50 |
|
static RTREE * |
51 |
< |
newtwig() /* allocate a twig */ |
51 |
> |
newtwig(void) /* allocate a twig */ |
52 |
|
{ |
53 |
< |
register int bi; |
53 |
> |
int bi; |
54 |
|
|
55 |
|
if (twigbundle == NULL) { /* initialize */ |
56 |
|
twigbundle = (RTREE **)malloc(sizeof(RTREE *)); |
60 |
|
} |
61 |
|
bi = nexttwig / TBUNDLESIZ; |
62 |
|
if (twigbundle[bi] == NULL) { /* new block */ |
63 |
< |
twigbundle = (RTREE **)realloc((char *)twigbundle, |
63 |
> |
twigbundle = (RTREE **)realloc((void *)twigbundle, |
64 |
|
(bi+2)*sizeof(RTREE *)); |
65 |
|
if (twigbundle == NULL) |
66 |
|
goto memerr; |
73 |
|
return(twigbundle[bi] + (nexttwig++ - bi*TBUNDLESIZ)); |
74 |
|
memerr: |
75 |
|
error(SYSTEM, "out of memory in newtwig"); |
76 |
+ |
return NULL; /* pro forma return */ |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
< |
qtFreeTree(really) /* free allocated twigs */ |
81 |
< |
int really; |
80 |
> |
static void |
81 |
> |
qtFreeTree( /* free allocated twigs */ |
82 |
> |
int really |
83 |
> |
) |
84 |
|
{ |
85 |
< |
register int i; |
85 |
> |
int i; |
86 |
|
|
87 |
< |
if (tmTop != NULL) |
67 |
< |
tmClearHisto(); |
68 |
< |
bzero((char *)&qtrunk, sizeof(RTREE)); |
69 |
< |
nexttwig = 0; |
87 |
> |
qtrunk.flgs = CH_ANY; /* chop down tree */ |
88 |
|
if (twigbundle == NULL) |
89 |
|
return; |
90 |
+ |
i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ; |
91 |
+ |
nexttwig = 0; |
92 |
|
if (!really) { /* just clear allocated blocks */ |
93 |
< |
for (i = 0; twigbundle[i] != NULL; i++) |
94 |
< |
bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE)); |
93 |
> |
while (i--) |
94 |
> |
memset((char *)twigbundle[i], '\0', TBUNDLESIZ*sizeof(RTREE)); |
95 |
|
return; |
96 |
|
} |
97 |
|
/* else "really" means free up memory */ |
98 |
|
for (i = 0; twigbundle[i] != NULL; i++) |
99 |
< |
free((char *)twigbundle[i]); |
100 |
< |
free((char *)twigbundle); |
99 |
> |
free((void *)twigbundle[i]); |
100 |
> |
free((void *)twigbundle); |
101 |
|
twigbundle = NULL; |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
< |
static RLEAF * |
106 |
< |
newleaf() /* allocate a leaf from our pile */ |
87 |
< |
{ |
88 |
< |
if (tleaf++ >= nleaves) /* get next leaf in ring */ |
89 |
< |
tleaf = 0; |
90 |
< |
if (tleaf == bleaf) /* need to shake some free */ |
91 |
< |
qtCompost(LFREEPCT); |
92 |
< |
return(leafpile + tleaf); |
93 |
< |
} |
105 |
> |
#define LEAFSIZ (3*sizeof(float)+sizeof(int32)+\ |
106 |
> |
sizeof(TMbright)+6*sizeof(uby8)) |
107 |
|
|
95 |
– |
|
108 |
|
int |
109 |
< |
qtAllocLeaves(n) /* allocate space for n leaves */ |
110 |
< |
int n; |
109 |
> |
qtAllocLeaves( /* allocate space for n leaves */ |
110 |
> |
int n |
111 |
> |
) |
112 |
|
{ |
113 |
|
unsigned nbytes; |
114 |
< |
register unsigned i; |
114 |
> |
unsigned i; |
115 |
|
|
116 |
|
qtFreeTree(0); /* make sure tree is empty */ |
117 |
|
if (n <= 0) |
118 |
|
return(0); |
119 |
< |
if (nleaves >= n) |
120 |
< |
return(nleaves); |
121 |
< |
else if (nleaves > 0) |
122 |
< |
free((char *)leafpile); |
119 |
> |
if (qtL.nl >= n) |
120 |
> |
return(qtL.nl); |
121 |
> |
else if (qtL.nl > 0) |
122 |
> |
free(qtL.base); |
123 |
|
/* round space up to nearest power of 2 */ |
124 |
< |
nbytes = n*sizeof(RLEAF) + 8; |
124 |
> |
nbytes = n*LEAFSIZ + 8; |
125 |
|
for (i = 1024; nbytes > i; i <<= 1) |
126 |
|
; |
127 |
< |
n = (i - 8) / sizeof(RLEAF); |
128 |
< |
leafpile = (RLEAF *)malloc(n*sizeof(RLEAF)); |
129 |
< |
if (leafpile == NULL) |
130 |
< |
return(-1); |
131 |
< |
nleaves = n; |
132 |
< |
bleaf = tleaf = 0; |
133 |
< |
return(nleaves); |
127 |
> |
n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */ |
128 |
> |
qtL.base = (char *)malloc(n*LEAFSIZ); |
129 |
> |
if (qtL.base == NULL) |
130 |
> |
return(0); |
131 |
> |
/* assign larger alignment types earlier */ |
132 |
> |
qtL.wp = (float (*)[3])qtL.base; |
133 |
> |
qtL.wd = (int32 *)(qtL.wp + n); |
134 |
> |
qtL.brt = (TMbright *)(qtL.wd + n); |
135 |
> |
qtL.chr = (uby8 (*)[3])(qtL.brt + n); |
136 |
> |
qtL.rgb = (uby8 (*)[3])(qtL.chr + n); |
137 |
> |
qtL.nl = n; |
138 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
139 |
> |
falleaves = -1; |
140 |
> |
return(n); |
141 |
|
} |
142 |
|
|
143 |
+ |
#undef LEAFSIZ |
144 |
|
|
145 |
< |
qtFreeLeaves() /* free our allocated leaves and twigs */ |
145 |
> |
|
146 |
> |
void |
147 |
> |
qtFreeLeaves(void) /* free our allocated leaves and twigs */ |
148 |
|
{ |
149 |
|
qtFreeTree(1); /* free tree also */ |
150 |
< |
if (nleaves <= 0) |
150 |
> |
if (qtL.nl <= 0) |
151 |
|
return; |
152 |
< |
free((char *)leafpile); |
153 |
< |
leafpile = NULL; |
154 |
< |
nleaves = 0; |
152 |
> |
free(qtL.base); |
153 |
> |
qtL.base = NULL; |
154 |
> |
qtL.nl = 0; |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
< |
static |
159 |
< |
shaketree(tp) /* shake dead leaves from tree */ |
160 |
< |
register RTREE *tp; |
158 |
> |
static void |
159 |
> |
shaketree( /* shake dead leaves from tree */ |
160 |
> |
RTREE *tp |
161 |
> |
) |
162 |
|
{ |
163 |
< |
register int i, li; |
163 |
> |
int i, li; |
164 |
|
|
165 |
|
for (i = 0; i < 4; i++) |
166 |
< |
if (tp->flgs & BRF(i)) |
166 |
> |
if (tp->flgs & BRF(i)) { |
167 |
|
shaketree(tp->k[i].b); |
168 |
< |
else if (tp->k[i].l != NULL) { |
169 |
< |
li = tp->k[i].l - leafpile; |
170 |
< |
if (bleaf < tleaf ? (li < bleaf || li >= tleaf) : |
171 |
< |
(li < bleaf && li >= tleaf)) { |
172 |
< |
tmAddHisto(&tp->k[i].l->brt, 1, -1); |
173 |
< |
tp->k[i].l = NULL; |
150 |
< |
} |
168 |
> |
if (is_stump(tp->k[i].b)) |
169 |
> |
tp->flgs &= ~BRF(i); |
170 |
> |
} else if (tp->flgs & LFF(i)) { |
171 |
> |
li = tp->k[i].li; |
172 |
> |
if (composted(li)) |
173 |
> |
tp->flgs &= ~LFF(i); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
int |
179 |
< |
qtCompost(pct) /* free up some leaves */ |
180 |
< |
int pct; |
179 |
> |
qtCompost( /* free up some leaves */ |
180 |
> |
int pct |
181 |
> |
) |
182 |
|
{ |
183 |
< |
int nused, nclear; |
183 |
> |
int32 *fl; |
184 |
> |
int nused, nclear, nmapped; |
185 |
|
/* figure out how many leaves to clear */ |
186 |
< |
nclear = nleaves * pct / 100; |
186 |
> |
nclear = qtL.nl * pct / 100; |
187 |
> |
nused = qtL.tl - qtL.bl; |
188 |
> |
if (nused <= 0) nused += qtL.nl; |
189 |
> |
nclear -= qtL.nl - nused; |
190 |
|
if (nclear <= 0) |
191 |
|
return(0); |
164 |
– |
nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf; |
192 |
|
if (nclear >= nused) { /* clear them all */ |
193 |
|
qtFreeTree(0); |
194 |
< |
bleaf = tleaf = 0; |
194 |
> |
qtL.tml = qtL.bl = qtL.tl = 0; |
195 |
> |
falleaves = -1; |
196 |
|
return(nused); |
197 |
|
} |
198 |
|
/* else clear leaves from bottom */ |
199 |
< |
bleaf = (bleaf + nclear) % nleaves; |
200 |
< |
shaketree(&qtrunk); |
199 |
> |
nmapped = qtL.tml - qtL.bl; |
200 |
> |
if (nmapped < 0) nmapped += qtL.nl; |
201 |
> |
qtL.bl += nclear; |
202 |
> |
if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl; |
203 |
> |
if (nmapped <= nclear) qtL.tml = qtL.bl; |
204 |
> |
shaketree(&qtrunk); /* dereference composted leaves */ |
205 |
> |
for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl) |
206 |
> |
while (composted(*fl)) |
207 |
> |
if ((*fl = qtL.wd[*fl]) < 0) |
208 |
> |
return(nclear); |
209 |
|
return(nclear); |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
< |
RLEAF * |
214 |
< |
qtFindLeaf(x, y) /* find closest leaf to (x,y) */ |
215 |
< |
int x, y; |
213 |
> |
int |
214 |
> |
qtFindLeaf( /* find closest leaf to (x,y) */ |
215 |
> |
int x, |
216 |
> |
int y |
217 |
> |
) |
218 |
|
{ |
219 |
< |
register RTREE *tp = &qtrunk; |
220 |
< |
RLEAF *lp = NULL; |
219 |
> |
RTREE *tp = &qtrunk; |
220 |
> |
int li = -1; |
221 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
222 |
|
int mx, my; |
223 |
< |
register int q; |
223 |
> |
int q; |
224 |
|
/* check limits */ |
225 |
|
if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres) |
226 |
< |
return(NULL); |
226 |
> |
return(-1); |
227 |
|
/* find nearby leaf in our tree */ |
228 |
|
for ( ; ; ) { |
229 |
|
for (q = 0; q < 4; q++) /* find any leaf this level */ |
230 |
< |
if (!(tp->flgs & BRF(q)) && tp->k[q].l != NULL) { |
231 |
< |
lp = tp->k[q].l; |
230 |
> |
if (tp->flgs & LFF(q)) { |
231 |
> |
li = tp->k[q].li; |
232 |
|
break; |
233 |
|
} |
234 |
|
q = 0; /* which quadrant are we? */ |
242 |
|
tp = tp->k[q].b; |
243 |
|
continue; |
244 |
|
} |
245 |
< |
if (tp->k[q].l != NULL) /* good shot! */ |
246 |
< |
return(tp->k[q].l); |
247 |
< |
return(lp); /* else return what we have */ |
245 |
> |
if (tp->flgs & LFF(q)) /* good shot! */ |
246 |
> |
return(tp->k[q].li); |
247 |
> |
return(li); /* else return what we have */ |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
< |
static |
253 |
< |
addleaf(lp) /* add a leaf to our tree */ |
254 |
< |
RLEAF *lp; |
252 |
> |
static int |
253 |
> |
putleaf( /* put a leaf in our tree */ |
254 |
> |
int li, |
255 |
> |
int drop |
256 |
> |
) |
257 |
|
{ |
258 |
< |
register RTREE *tp = &qtrunk; |
258 |
> |
RTREE *tp = &qtrunk; |
259 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
260 |
< |
RLEAF *lo = NULL; |
260 |
> |
int lo = -1; |
261 |
> |
double d2; |
262 |
|
int x, y, mx, my; |
263 |
|
double z; |
264 |
< |
FVECT ip, wp; |
265 |
< |
register int q; |
266 |
< |
/* compute leaf location */ |
267 |
< |
VCOPY(wp, lp->wp); |
268 |
< |
viewloc(ip, &odev.v, wp); |
269 |
< |
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
270 |
< |
|| ip[1] < 0. || ip[1] >= 1.) |
271 |
< |
return; |
264 |
> |
FVECT ip, wp, vd; |
265 |
> |
int q; |
266 |
> |
/* check for dead leaf */ |
267 |
> |
if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2])) |
268 |
> |
return(0); |
269 |
> |
/* compute leaf location in view */ |
270 |
> |
VCOPY(wp, qtL.wp[li]); |
271 |
> |
if (viewloc(ip, &odev.v, wp) != VL_GOOD) |
272 |
> |
goto dropit; /* behind or outside view */ |
273 |
> |
#ifdef DEBUG |
274 |
> |
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
275 |
> |
error(INTERNAL, "bad view assumption in putleaf"); |
276 |
> |
#endif |
277 |
> |
for (q = 0; q < 3; q++) |
278 |
> |
vd[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
279 |
> |
d2 = fdir2diff(qtL.wd[li], vd); |
280 |
> |
#ifdef MAXDIFF2 |
281 |
> |
if (d2 > MAXDIFF2) |
282 |
> |
goto dropit; /* leaf dir. too far off */ |
283 |
> |
#endif |
284 |
|
x = ip[0] * odev.hres; |
285 |
|
y = ip[1] * odev.vres; |
286 |
|
z = ip[2]; |
298 |
|
tp = tp->k[q].b; |
299 |
|
continue; |
300 |
|
} |
301 |
< |
if (tp->k[q].l == NULL) { /* found stem for leaf */ |
302 |
< |
tp->k[q].l = lp; |
303 |
< |
tp->flgs |= CHF(q); |
304 |
< |
break; |
301 |
> |
if (!(tp->flgs & LFF(q))) { /* found stem for leaf */ |
302 |
> |
tp->k[q].li = li; |
303 |
> |
tp->flgs |= CHLFF(q); |
304 |
> |
return(1); |
305 |
|
} |
306 |
< |
/* check existing leaf */ |
307 |
< |
if (lo != tp->k[q].l) { |
308 |
< |
lo = tp->k[q].l; |
256 |
< |
VCOPY(wp, lo->wp); |
306 |
> |
if (lo != tp->k[q].li) { /* check old leaf */ |
307 |
> |
lo = tp->k[q].li; |
308 |
> |
VCOPY(wp, qtL.wp[lo]); |
309 |
|
viewloc(ip, &odev.v, wp); |
310 |
|
} |
311 |
|
/* is node minimum size? */ |
312 |
< |
if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) { |
313 |
< |
if (z > (1.-qtDepthEps)*ip[2]) /* who is closer? */ |
314 |
< |
return; /* old one is */ |
315 |
< |
tp->k[q].l = lp; /* new one is */ |
312 |
> |
if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { |
313 |
> |
if (z > (1.+qtDepthEps)*ip[2]) |
314 |
> |
break; /* old one closer */ |
315 |
> |
if (z >= (1.-qtDepthEps)*ip[2] && |
316 |
> |
fdir2diff(qtL.wd[lo], vd) < d2) |
317 |
> |
break; /* old one better */ |
318 |
> |
tp->k[q].li = li; /* attach new */ |
319 |
|
tp->flgs |= CHF(q); |
320 |
< |
tmAddHisto(&lo->brt, 1, -1); /* drop old one */ |
320 |
> |
li = lo; /* drop old... */ |
321 |
|
break; |
322 |
|
} |
323 |
< |
tp->flgs |= CHBRF(q); /* else grow tree */ |
323 |
> |
tp->flgs &= ~LFF(q); /* else grow tree */ |
324 |
> |
tp->flgs |= CHBRF(q); |
325 |
|
tp = tp->k[q].b = newtwig(); |
270 |
– |
tp->flgs |= CH_ANY; /* all new */ |
326 |
|
q = 0; /* old leaf -> new branch */ |
327 |
|
mx = ip[0] * odev.hres; |
328 |
|
my = ip[1] * odev.vres; |
329 |
|
if (mx >= (x0 + x1) >> 1) q |= 01; |
330 |
|
if (my >= (y0 + y1) >> 1) q |= 02; |
331 |
< |
tp->k[q].l = lo; |
331 |
> |
tp->flgs = CH_ANY|LFF(q); /* all new */ |
332 |
> |
tp->k[q].li = lo; |
333 |
|
} |
334 |
< |
tmAddHisto(&lp->brt, 1, 1); /* add leaf to histogram */ |
334 |
> |
dropit: |
335 |
> |
if (drop) { |
336 |
> |
if (li+1 == (qtL.tl ? qtL.tl : qtL.nl)) |
337 |
> |
qtL.tl = li; /* special case */ |
338 |
> |
else { |
339 |
> |
qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0; |
340 |
> |
qtL.wd[li] = falleaves; |
341 |
> |
falleaves = li; |
342 |
> |
} |
343 |
> |
} |
344 |
> |
return(li == lo); |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
< |
dev_value(c, p) /* add a pixel value to our output queue */ |
349 |
< |
COLR c; |
350 |
< |
FVECT p; |
348 |
> |
void |
349 |
> |
dev_value( /* add a pixel value to our quadtree */ |
350 |
> |
COLR c, |
351 |
> |
FVECT d, |
352 |
> |
FVECT p |
353 |
> |
) |
354 |
|
{ |
355 |
< |
register RLEAF *lp; |
356 |
< |
|
357 |
< |
lp = newleaf(); |
358 |
< |
VCOPY(lp->wp, p); |
359 |
< |
tmCvColrs(&lp->brt, lp->chr, c, 1); |
360 |
< |
addleaf(lp); |
355 |
> |
int li; |
356 |
> |
int mapit; |
357 |
> |
/* grab a leaf */ |
358 |
> |
if (!imm_mode && falleaves >= 0) { /* check for fallen leaves */ |
359 |
> |
li = falleaves; |
360 |
> |
falleaves = qtL.wd[li]; |
361 |
> |
mapit = qtL.tml <= qtL.tl ? |
362 |
> |
(li < qtL.tml || li >= qtL.tl) : |
363 |
> |
(li < qtL.tml && li >= qtL.tl) ; |
364 |
> |
} else { /* else allocate new one */ |
365 |
> |
li = qtL.tl++; |
366 |
> |
if (qtL.tl >= qtL.nl) /* next leaf in ring */ |
367 |
> |
qtL.tl = 0; |
368 |
> |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
369 |
> |
qtCompost(LFREEPCT); |
370 |
> |
mapit = 0; /* we'll map it later */ |
371 |
> |
} |
372 |
> |
if (p == NULL) |
373 |
> |
VSUM(qtL.wp[li], odev.v.vp, d, FHUGE); |
374 |
> |
else |
375 |
> |
VCOPY(qtL.wp[li], p); |
376 |
> |
qtL.wd[li] = encodedir(d); |
377 |
> |
tmCvColrs(tmGlobal, &qtL.brt[li], qtL.chr[li], (COLR *)c, 1); |
378 |
> |
if (putleaf(li, 1)) { |
379 |
> |
if (mapit) |
380 |
> |
tmMapPixels(tmGlobal, (uby8 *)(qtL.rgb+li), qtL.brt+li, |
381 |
> |
(uby8 *)(qtL.chr+li), 1); |
382 |
> |
if (--rayqleft == 0) |
383 |
> |
dev_flush(); /* flush output */ |
384 |
> |
} |
385 |
|
} |
386 |
|
|
387 |
|
|
388 |
< |
qtReplant() /* replant our tree using new view */ |
388 |
> |
void |
389 |
> |
qtReplant(void) /* replant our tree using new view */ |
390 |
|
{ |
391 |
< |
register int i; |
392 |
< |
|
393 |
< |
if (bleaf == tleaf) /* anything to replant? */ |
391 |
> |
int i; |
392 |
> |
/* anything to replant? */ |
393 |
> |
if (qtL.bl == qtL.tl) |
394 |
|
return; |
395 |
< |
qtFreeTree(0); /* blow the tree away */ |
396 |
< |
/* now rebuild it */ |
397 |
< |
for (i = bleaf; i != tleaf; ) { |
398 |
< |
addleaf(leafpile+i); |
399 |
< |
if (++i >= nleaves) i = 0; |
395 |
> |
qtFreeTree(0); /* blow the old tree away */ |
396 |
> |
/* regrow it in new place */ |
397 |
> |
for (i = qtL.bl; i != qtL.tl; ) { |
398 |
> |
putleaf(i, 0); |
399 |
> |
if (++i >= qtL.nl) i = 0; |
400 |
|
} |
307 |
– |
tmComputeMapping(0., 0., 0.); /* update the display */ |
308 |
– |
qtUpdate(); |
401 |
|
} |
402 |
|
|
403 |
|
|
404 |
< |
static |
405 |
< |
redraw(ca, tp, x0, y0, x1, y1, l) /* redraw portion of a tree */ |
406 |
< |
BYTE ca[3]; /* returned average color */ |
407 |
< |
register RTREE *tp; |
316 |
< |
int x0, y0, x1, y1; |
317 |
< |
int l[2][2]; |
404 |
> |
int |
405 |
> |
qtMapLeaves( /* map our leaves to RGB */ |
406 |
> |
int redo |
407 |
> |
) |
408 |
|
{ |
409 |
< |
int csm[3], nc; |
410 |
< |
BYTE rgb[3]; |
411 |
< |
int quads = CH_ANY; |
412 |
< |
int mx, my; |
413 |
< |
register int i; |
414 |
< |
/* compute midpoint */ |
415 |
< |
mx = (x0 + x1) >> 1; |
416 |
< |
my = (y0 + y1) >> 1; |
417 |
< |
/* see what to do */ |
418 |
< |
if (l[0][0] >= mx) |
419 |
< |
quads &= ~(CHF(2)|CHF(0)); |
420 |
< |
else if (l[0][1] <= mx) |
331 |
< |
quads &= ~(CHF(3)|CHF(1)); |
332 |
< |
if (l[1][0] >= my) |
333 |
< |
quads &= ~(CHF(1)|CHF(0)); |
334 |
< |
else if (l[1][1] <= my) |
335 |
< |
quads &= ~(CHF(3)|CHF(2)); |
336 |
< |
tp->flgs &= ~quads; /* mark them done */ |
337 |
< |
csm[0] = csm[1] = csm[2] = nc = 0; |
338 |
< |
/* do leaves first */ |
339 |
< |
for (i = 0; i < 4; i++) |
340 |
< |
if (quads & CHF(i) && !(tp->flgs & BRF(i)) && |
341 |
< |
tp->k[i].l != NULL) { |
342 |
< |
tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1); |
343 |
< |
dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0, |
344 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
345 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
346 |
< |
nc++; |
347 |
< |
quads &= ~CHF(i); |
348 |
< |
} |
349 |
< |
/* now do branches */ |
350 |
< |
for (i = 0; i < 4; i++) |
351 |
< |
if (quads & CHF(i) && tp->flgs & BRF(i)) { |
352 |
< |
redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, |
353 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my, l); |
354 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
355 |
< |
nc++; |
356 |
< |
quads &= ~CHF(i); |
357 |
< |
} |
358 |
< |
if (nc > 1) { |
359 |
< |
ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; |
409 |
> |
int aorg, alen, borg, blen; |
410 |
> |
/* recompute mapping? */ |
411 |
> |
if (redo) |
412 |
> |
qtL.tml = qtL.bl; |
413 |
> |
/* already done? */ |
414 |
> |
if (qtL.tml == qtL.tl) |
415 |
> |
return(1); |
416 |
> |
/* compute segments */ |
417 |
> |
aorg = qtL.tml; |
418 |
> |
if (qtL.tl >= aorg) { |
419 |
> |
alen = qtL.tl - aorg; |
420 |
> |
blen = 0; |
421 |
|
} else { |
422 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
422 |
> |
alen = qtL.nl - aorg; |
423 |
> |
borg = 0; |
424 |
> |
blen = qtL.tl; |
425 |
|
} |
426 |
< |
if (!quads) return; |
427 |
< |
/* fill in gaps with average */ |
428 |
< |
for (i = 0; i < 4; i++) |
429 |
< |
if (quads & CHF(i)) |
430 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
431 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
432 |
< |
} |
433 |
< |
|
371 |
< |
|
372 |
< |
static |
373 |
< |
update(ca, tp, x0, y0, x1, y1) /* update tree display as needed */ |
374 |
< |
BYTE ca[3]; /* returned average color */ |
375 |
< |
register RTREE *tp; |
376 |
< |
int x0, y0, x1, y1; |
377 |
< |
{ |
378 |
< |
int csm[3], nc; |
379 |
< |
BYTE rgb[3]; |
380 |
< |
int gaps = 0; |
381 |
< |
int mx, my; |
382 |
< |
register int i; |
383 |
< |
/* compute midpoint */ |
384 |
< |
mx = (x0 + x1) >> 1; |
385 |
< |
my = (y0 + y1) >> 1; |
386 |
< |
csm[0] = csm[1] = csm[2] = nc = 0; |
387 |
< |
/* do leaves first */ |
388 |
< |
for (i = 0; i < 4; i++) |
389 |
< |
if ((tp->flgs & CHBRF(i)) == CHF(i)) { |
390 |
< |
if (tp->k[i].l == NULL) { |
391 |
< |
gaps |= 1<<i; /* empty stem */ |
392 |
< |
continue; |
393 |
< |
} |
394 |
< |
tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1); |
395 |
< |
dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0, |
396 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
397 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
398 |
< |
nc++; |
399 |
< |
} |
400 |
< |
/* now do branches */ |
401 |
< |
for (i = 0; i < 4; i++) |
402 |
< |
if ((tp->flgs & CHBRF(i)) == CHBRF(i)) { |
403 |
< |
update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0, |
404 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
405 |
< |
csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2]; |
406 |
< |
nc++; |
407 |
< |
} |
408 |
< |
if (nc > 1) { |
409 |
< |
ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc; |
410 |
< |
} else { |
411 |
< |
ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2]; |
426 |
> |
/* (re)compute tone mapping? */ |
427 |
> |
if (qtL.tml == qtL.bl) { |
428 |
> |
tmClearHisto(tmGlobal); |
429 |
> |
tmAddHisto(tmGlobal, qtL.brt+aorg, alen, 1); |
430 |
> |
if (blen > 0) |
431 |
> |
tmAddHisto(tmGlobal, qtL.brt+borg, blen, 1); |
432 |
> |
if (tmComputeMapping(tmGlobal, 0., 0., 0.) != TM_E_OK) |
433 |
> |
return(0); |
434 |
|
} |
435 |
< |
/* fill in gaps with average */ |
436 |
< |
for (i = 0; gaps && i < 4; gaps >>= 1, i++) |
437 |
< |
if (gaps & 01) |
438 |
< |
dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0, |
439 |
< |
i&01 ? x1 : mx, i&02 ? y1 : my); |
440 |
< |
tp->flgs &= ~CH_ANY; /* all done */ |
441 |
< |
} |
442 |
< |
|
421 |
< |
|
422 |
< |
qtRedraw(x0, y0, x1, y1) /* redraw part of our screen */ |
423 |
< |
int x0, y0, x1, y1; |
424 |
< |
{ |
425 |
< |
int lim[2][2]; |
426 |
< |
BYTE ca[3]; |
427 |
< |
|
428 |
< |
if (is_stump(&qtrunk)) |
429 |
< |
return; |
430 |
< |
if ((lim[0][0]=x0) == 0 & (lim[1][0]=y0) == 0 & |
431 |
< |
(lim[0][1]=x1) == odev.hres & (lim[1][1]=y1) == odev.vres || |
432 |
< |
tmTop->lumap == NULL) |
433 |
< |
tmComputeMapping(0., 0., 0.); |
434 |
< |
redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim); |
435 |
< |
} |
436 |
< |
|
437 |
< |
|
438 |
< |
qtUpdate() /* update our tree display */ |
439 |
< |
{ |
440 |
< |
BYTE ca[3]; |
441 |
< |
|
442 |
< |
if (is_stump(&qtrunk)) |
443 |
< |
return; |
444 |
< |
if (tmTop->lumap == NULL) |
445 |
< |
tmComputeMapping(0., 0., 0.); |
446 |
< |
update(ca, &qtrunk, 0, 0, odev.hres, odev.vres); |
435 |
> |
if (tmMapPixels(tmGlobal, (uby8 *)(qtL.rgb+aorg), qtL.brt+aorg, |
436 |
> |
(uby8 *)(qtL.chr+aorg), alen) != TM_E_OK) |
437 |
> |
return(0); |
438 |
> |
if (blen > 0) |
439 |
> |
tmMapPixels(tmGlobal, (uby8 *)(qtL.rgb+borg), qtL.brt+borg, |
440 |
> |
(uby8 *)(qtL.chr+borg), blen); |
441 |
> |
qtL.tml = qtL.tl; |
442 |
> |
return(1); |
443 |
|
} |