16 |
|
struct source *curlist = NULL; /* current source list */ |
17 |
|
struct source *donelist = NULL; /* finished sources */ |
18 |
|
|
19 |
– |
double threshold; /* glare threshold */ |
19 |
|
|
21 |
– |
|
20 |
|
struct srcspan * |
21 |
|
newspan(l, r, v, sb) /* allocate a new source span */ |
22 |
|
int l, r, v; |
48 |
|
if (spanbr == NULL) |
49 |
|
memerr("view span brightness buffer"); |
50 |
|
for (v = vsize; v >= -vsize; v--) { |
51 |
+ |
close_sources(v); |
52 |
+ |
#ifndef DEBUG |
53 |
+ |
if (verbose) |
54 |
+ |
fprintf(stderr, "%s: analyzing... %3ld%%\r", |
55 |
+ |
progname, 100L*(vsize-v)/(2*vsize)); |
56 |
+ |
#endif |
57 |
|
getviewspan(v, spanbr); |
58 |
|
left = hsize + 1; |
59 |
< |
for (h = -hsize; h <= hsize; h++) |
59 |
> |
for (h = -hsize; h <= hsize; h++) { |
60 |
|
if (spanbr[h+hsize] < 0.0) { /* off view */ |
61 |
|
if (left < h) { |
62 |
< |
addsrcspan(newspan(left,h-1,v,spanbr)); |
62 |
> |
addsrcspan(newspan(left,h,v,spanbr)); |
63 |
|
left = hsize + 1; |
64 |
|
} |
65 |
|
continue; |
66 |
|
} |
67 |
|
if (spanbr[h+hsize] > threshold) { /* in source */ |
68 |
< |
if (left >= h) |
68 |
> |
if (left > h) |
69 |
|
left = h; |
70 |
|
} else { /* out of source */ |
71 |
|
if (left < h) { |
72 |
< |
addsrcspan(newspan(left,h-1,v,spanbr)); |
72 |
> |
addsrcspan(newspan(left,h,v,spanbr)); |
73 |
|
left = hsize + 1; |
74 |
|
} |
75 |
|
addindirect(h, spanbr[h+hsize]); |
76 |
|
} |
77 |
+ |
} |
78 |
|
if (left < h) |
79 |
< |
addsrcspan(newspan(left,h-1,v,spanbr)); |
75 |
< |
close_sources(v); |
79 |
> |
addsrcspan(newspan(left,h,v,spanbr)); |
80 |
|
} |
77 |
– |
close_allsrcs(); |
81 |
|
free((char *)spanbr); |
82 |
+ |
close_allsrcs(); |
83 |
+ |
absorb_specks(); |
84 |
|
} |
85 |
|
|
86 |
|
|
92 |
|
register int i; |
93 |
|
|
94 |
|
if (h <= -hlim) { /* left region */ |
95 |
< |
d = (double)(h+hlim)/SAMPDENS; |
95 |
> |
d = (double)(h+hlim)/sampdens; |
96 |
|
if (d <= -1.0+FTINY) |
97 |
|
return; |
98 |
|
tanb = d/sqrt(1.0-d*d); |
106 |
|
return; |
107 |
|
} |
108 |
|
if (h >= hlim) { /* right region */ |
109 |
< |
d = (double)(h-hlim)/SAMPDENS; |
109 |
> |
d = (double)(h-hlim)/sampdens; |
110 |
|
if (d >= 1.0-FTINY) |
111 |
|
return; |
112 |
|
tanb = d/sqrt(1.0-d*d); |
157 |
|
fprintf(stderr, "%s: threshold zero!\n", progname); |
158 |
|
exit(1); |
159 |
|
} |
160 |
< |
if (verbose) |
160 |
> |
if (verbose) { |
161 |
> |
#ifdef DEBUG |
162 |
> |
pict_stats(); |
163 |
> |
#endif |
164 |
|
fprintf(stderr, |
165 |
|
"%s: threshold set to %f cd/m2 from %d samples\n", |
166 |
|
progname, threshold, nsamps); |
167 |
+ |
} |
168 |
|
} |
169 |
|
|
170 |
|
|
173 |
|
{ |
174 |
|
struct source *last, *cs, *this; |
175 |
|
register struct srcspan *ss; |
167 |
– |
register int res; |
176 |
|
|
177 |
|
cs = NULL; |
178 |
|
for (this = curlist; this != NULL; this = this->next) { |
183 |
|
if (cs == NULL) |
184 |
|
cs = this; |
185 |
|
else { |
178 |
– |
mergesource(cs, this); |
186 |
|
last->next = this->next; |
187 |
< |
free((char *)this); |
187 |
> |
mergesource(cs, this); |
188 |
> |
this = last; |
189 |
|
} |
190 |
|
break; |
191 |
|
} |
196 |
|
cs = (struct source *)malloc(sizeof(struct source)); |
197 |
|
if (cs == NULL) |
198 |
|
memerr("source records"); |
199 |
+ |
cs->dom = 0.0; |
200 |
|
cs->first = NULL; |
201 |
|
cs->next = curlist; |
202 |
|
curlist = cs; |
227 |
|
if (prev->next == NULL) |
228 |
|
prev->next = alp; |
229 |
|
sp->first = head.next; |
230 |
< |
ap->first = NULL; |
230 |
> |
if (ap->dom > 0.0 && sp->dom > 0.0) { /* sources are done */ |
231 |
> |
sp->dir[0] *= sp->dom; |
232 |
> |
sp->dir[1] *= sp->dom; |
233 |
> |
sp->dir[2] *= sp->dom; |
234 |
> |
fvsum(sp->dir, sp->dir, ap->dir, ap->dom); |
235 |
> |
normalize(sp->dir); |
236 |
> |
sp->brt = (sp->brt*sp->dom + ap->brt*ap->dom) |
237 |
> |
/ (sp->dom + ap->dom); |
238 |
> |
} |
239 |
> |
free((char *)ap); |
240 |
|
} |
241 |
|
|
242 |
|
|
261 |
|
|
262 |
|
close_allsrcs() /* done with everything */ |
263 |
|
{ |
264 |
< |
register struct source *this; |
264 |
> |
register struct source *this, *next; |
265 |
|
|
266 |
< |
for (this = curlist; this != NULL; this = this->next) |
266 |
> |
this = curlist; |
267 |
> |
while (this != NULL) { |
268 |
> |
next = this->next; |
269 |
|
donesource(this); |
270 |
+ |
this = next; |
271 |
+ |
} |
272 |
|
curlist = NULL; |
273 |
|
} |
274 |
|
|
282 |
|
double d; |
283 |
|
|
284 |
|
sp->dom = 0.0; |
285 |
< |
sp->dir[0] = sp->dir[1] = sp->dir[1] = 0.0; |
285 |
> |
sp->dir[0] = sp->dir[1] = sp->dir[2] = 0.0; |
286 |
|
sp->brt = 0.0; |
287 |
+ |
n = 0; |
288 |
|
for (ss = sp->first; ss != NULL; ss = ss->next) { |
289 |
|
sp->brt += ss->brsum; |
290 |
|
n += ss->r - ss->l; |
291 |
< |
compdir(dright, ss->r, ss->v); |
292 |
< |
for (h = ss->r-1; h >= ss->l; h--) { |
293 |
< |
compdir(dthis, h, ss->v); |
294 |
< |
d = dist2(dthis, dright); |
295 |
< |
fvsum(sp->dir, sp->dir, dthis, d); |
296 |
< |
sp->dom += d; |
297 |
< |
VCOPY(dright, dthis); |
298 |
< |
} |
291 |
> |
if (compdir(dright, ss->r, ss->v) < 0) |
292 |
> |
compdir(dright, ss->r-2, ss->v); |
293 |
> |
for (h = ss->r-1; h >= ss->l; h--) |
294 |
> |
if (compdir(dthis, h, ss->v) == 0) { |
295 |
> |
d = dist2(dthis, dright); |
296 |
> |
fvsum(sp->dir, sp->dir, dthis, d); |
297 |
> |
sp->dom += d; |
298 |
> |
VCOPY(dright, dthis); |
299 |
> |
} |
300 |
|
free((char *)ss); |
301 |
|
} |
302 |
|
sp->first = NULL; |
303 |
|
sp->brt /= (double)n; |
304 |
< |
sp->dir[0] /= sp->dom; |
281 |
< |
sp->dir[1] /= sp->dom; |
282 |
< |
sp->dir[2] /= sp->dom; |
304 |
> |
normalize(sp->dir); |
305 |
|
sp->next = donelist; |
306 |
|
donelist = sp; |
307 |
|
if (verbose) |
308 |
|
fprintf(stderr, |
309 |
< |
"%s: found source at (%f,%f,%f), solid angle %f, brightness %f\n", |
310 |
< |
progname, sp->dir[0], sp->dir[1], sp->dir[2], sp->brt); |
309 |
> |
"%s: found source at (%.3f,%.3f,%.3f), dw %.5f, br %.1f (%d samps)\n", |
310 |
> |
progname, sp->dir[0], sp->dir[1], sp->dir[2], |
311 |
> |
sp->dom, sp->brt, n); |
312 |
> |
} |
313 |
> |
|
314 |
> |
|
315 |
> |
struct source * |
316 |
> |
findbuddy(s, l) /* find close enough source to s in l*/ |
317 |
> |
register struct source *s, *l; |
318 |
> |
{ |
319 |
> |
struct source *bestbuddy = NULL; |
320 |
> |
double d, r, mindist = MAXBUDDY; |
321 |
> |
|
322 |
> |
r = sqrt(s->dom/PI); |
323 |
> |
for ( ; l != NULL; l = l->next) { |
324 |
> |
d = sqrt(dist2(l->dir, s->dir)) - sqrt(l->dom/PI) - r; |
325 |
> |
if (d < mindist) { |
326 |
> |
bestbuddy = l; |
327 |
> |
mindist = d; |
328 |
> |
} |
329 |
> |
} |
330 |
> |
return(bestbuddy); |
331 |
> |
} |
332 |
> |
|
333 |
> |
|
334 |
> |
absorb_specks() /* eliminate too-small sources */ |
335 |
> |
{ |
336 |
> |
struct source head, *buddy; |
337 |
> |
register struct source *last, *this; |
338 |
> |
|
339 |
> |
if (verbose) |
340 |
> |
fprintf(stderr, "%s: absorbing small sources...\n", progname); |
341 |
> |
head.next = donelist; |
342 |
> |
last = &head; |
343 |
> |
for (this = donelist; this != NULL; this = this->next) |
344 |
> |
if (TOOSMALL(this)) { |
345 |
> |
last->next = this->next; |
346 |
> |
buddy = findbuddy(this, donelist); |
347 |
> |
if (buddy != NULL) |
348 |
> |
mergesource(buddy, this); |
349 |
> |
else |
350 |
> |
absorb(this); |
351 |
> |
this = last; |
352 |
> |
} else |
353 |
> |
last = this; |
354 |
> |
donelist = head.next; |
355 |
> |
} |
356 |
> |
|
357 |
> |
|
358 |
> |
absorb(s) /* absorb a source into indirect */ |
359 |
> |
register struct source *s; |
360 |
> |
{ |
361 |
> |
FVECT dir; |
362 |
> |
register int i, n; |
363 |
> |
|
364 |
> |
for (i = 0; i < nglardirs; i++) { |
365 |
> |
spinvector(dir, ourview.vdir, ourview.vup, indirect[i].theta); |
366 |
> |
n = DOT(dir,s->dir)*s->dom*(sampdens*sampdens) + 0.5; |
367 |
> |
if (n == 0) |
368 |
> |
continue; |
369 |
> |
indirect[i].sum += n * s->brt; |
370 |
> |
indirect[i].n += n; |
371 |
> |
} |
372 |
> |
for ( ; s->first != NULL; s->first = s->first->next) |
373 |
> |
free((char *)s->first); |
374 |
> |
free((char *)s); |
375 |
|
} |