1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Find glare sources in a scene or image. |
6 |
|
* |
9 |
|
|
10 |
|
#include "glare.h" |
11 |
|
|
12 |
< |
#define FEQ(a,b) ((a)-(b)<=FTINY&&(a)-(b)<=FTINY) |
12 |
> |
#define FEQ(a,b) ((a)-(b)<=FTINY&&(b)-(a)<=FTINY) |
13 |
|
#define VEQ(v1,v2) (FEQ((v1)[0],(v2)[0])&&FEQ((v1)[1],(v2)[1]) \ |
14 |
|
&&FEQ((v1)[2],(v2)[2])) |
15 |
|
|
16 |
< |
char *rtargv[32] = {"rtrace", "-h", "-ov", "-fff"}; |
17 |
< |
int rtargc = 4; |
16 |
> |
char *rtargv[64] = {"rtrace", "-h-", "-ov", "-fff", "-ld-", "-i-", "-I-"}; |
17 |
> |
int rtargc = 7; |
18 |
|
|
19 |
|
VIEW ourview = STDVIEW; /* our view */ |
20 |
|
VIEW pictview = STDVIEW; /* picture view */ |
26 |
|
int verbose = 0; /* verbose reporting */ |
27 |
|
char *progname; /* global argv[0] */ |
28 |
|
|
29 |
+ |
double threshold = 0.; /* glare threshold */ |
30 |
+ |
|
31 |
|
int sampdens = SAMPDENS; /* sample density */ |
32 |
|
ANGLE glarang[180] = {AEND}; /* glare calculation angles */ |
33 |
|
int nglarangs = 0; |
34 |
|
double maxtheta; /* maximum angle (in radians) */ |
35 |
|
int hsize; /* horizontal size */ |
37 |
– |
int hlim; /* central limit of horizontal */ |
36 |
|
|
37 |
|
struct illum *indirect; /* array of indirect illuminances */ |
38 |
|
|
39 |
+ |
long npixinvw; /* number of pixels in view */ |
40 |
+ |
long npixmiss; /* number of pixels missed */ |
41 |
|
|
42 |
+ |
|
43 |
|
main(argc, argv) |
44 |
|
int argc; |
45 |
|
char *argv[]; |
46 |
|
{ |
47 |
+ |
int combine = 1; |
48 |
|
int gotview = 0; |
49 |
|
int rval, i; |
50 |
|
char *err; |
52 |
|
progname = argv[0]; |
53 |
|
/* process options */ |
54 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
55 |
+ |
/* expand arguments */ |
56 |
+ |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
57 |
+ |
; |
58 |
+ |
if (rval < 0) { |
59 |
+ |
fprintf(stderr, "%s: cannot expand '%s'", |
60 |
+ |
argv[0], argv[i]); |
61 |
+ |
exit(1); |
62 |
+ |
} |
63 |
|
rval = getviewopt(&ourview, argc-i, argv+i); |
64 |
|
if (rval >= 0) { |
65 |
|
i += rval; |
67 |
|
continue; |
68 |
|
} |
69 |
|
switch (argv[i][1]) { |
70 |
+ |
case 't': |
71 |
+ |
threshold = atof(argv[++i]); |
72 |
+ |
break; |
73 |
|
case 'r': |
74 |
|
sampdens = atoi(argv[++i])/2; |
75 |
|
break; |
80 |
|
} |
81 |
|
if (argv[i][2] != 'f') |
82 |
|
goto userr; |
83 |
< |
rval = viewfile(argv[++i], &ourview, 0, 0); |
83 |
> |
rval = viewfile(argv[++i], &ourview, NULL); |
84 |
|
if (rval < 0) { |
85 |
|
fprintf(stderr, |
86 |
|
"%s: cannot open view file \"%s\"\n", |
106 |
|
case 'p': |
107 |
|
picture = argv[++i]; |
108 |
|
break; |
109 |
+ |
case 'c': |
110 |
+ |
combine = !combine; |
111 |
+ |
break; |
112 |
|
case 'd': |
113 |
+ |
rtargv[rtargc++] = argv[i]; |
114 |
+ |
if (argv[i][2] != 'v') |
115 |
+ |
rtargv[rtargc++] = argv[++i]; |
116 |
+ |
break; |
117 |
|
case 'l': |
118 |
+ |
if (argv[i][2] == 'd') |
119 |
+ |
break; |
120 |
+ |
/* FALL THROUGH */ |
121 |
+ |
case 's': |
122 |
+ |
case 'P': |
123 |
|
rtargv[rtargc++] = argv[i]; |
124 |
|
rtargv[rtargc++] = argv[++i]; |
125 |
|
break; |
126 |
+ |
case 'w': |
127 |
+ |
rtargv[rtargc++] = argv[i]; |
128 |
+ |
break; |
129 |
|
case 'a': |
130 |
|
rtargv[rtargc++] = argv[i]; |
131 |
|
if (argv[i][2] == 'v') { |
147 |
|
} |
148 |
|
/* get view */ |
149 |
|
if (picture != NULL) { |
150 |
< |
rval = viewfile(picture, &pictview, 0, 0); |
150 |
> |
rval = viewfile(picture, &pictview, NULL); |
151 |
|
if (rval < 0) { |
152 |
|
fprintf(stderr, "%s: cannot open picture file \"%s\"\n", |
153 |
|
progname, picture); |
196 |
|
exit(1); |
197 |
|
} |
198 |
|
init(); /* initialize program */ |
199 |
< |
comp_thresh(); /* compute glare threshold */ |
199 |
> |
if (threshold <= FTINY) |
200 |
> |
comp_thresh(); /* compute glare threshold */ |
201 |
|
analyze(); /* analyze view */ |
202 |
+ |
if (combine) |
203 |
+ |
absorb_specks(); /* eliminate tiny sources */ |
204 |
|
cleanup(); /* tidy up */ |
205 |
|
/* print header */ |
206 |
+ |
newheader("RADIANCE", stdout); |
207 |
|
printargs(argc, argv, stdout); |
208 |
|
fputs(VIEWSTR, stdout); |
209 |
|
fprintview(&ourview, stdout); |
210 |
< |
printf("\n\n"); |
210 |
> |
printf("\n"); |
211 |
> |
fputformat("ascii", stdout); |
212 |
> |
printf("\n"); |
213 |
|
printsources(); /* print glare sources */ |
214 |
|
printillum(); /* print illuminances */ |
215 |
|
exit(0); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
+ |
int |
225 |
+ |
angcmp(ap1, ap2) /* compare two angles */ |
226 |
+ |
ANGLE *ap1, *ap2; |
227 |
+ |
{ |
228 |
+ |
register int a1, a2; |
229 |
+ |
|
230 |
+ |
a1 = *ap1; |
231 |
+ |
a2 = *ap2; |
232 |
+ |
if (a1 == a2) { |
233 |
+ |
fprintf(stderr, "%s: duplicate glare angle (%d)\n", |
234 |
+ |
progname, a1); |
235 |
+ |
exit(1); |
236 |
+ |
} |
237 |
+ |
return(a1-a2); |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
|
241 |
|
init() /* initialize global variables */ |
242 |
|
{ |
243 |
|
double d; |
249 |
|
/* set direction vectors */ |
250 |
|
for (i = 0; glarang[i] != AEND; i++) |
251 |
|
; |
252 |
< |
if (i > 0 && glarang[0] <= 0 || glarang[i-1] >= 180) { |
253 |
< |
fprintf(stderr, "%s: glare angles must be between 1 and 179\n", |
252 |
> |
qsort(glarang, i, sizeof(ANGLE), angcmp); |
253 |
> |
if (i > 0 && (glarang[0] <= 0 || glarang[i-1] > 180)) { |
254 |
> |
fprintf(stderr, "%s: glare angles must be between 1 and 180\n", |
255 |
|
progname); |
256 |
|
exit(1); |
257 |
|
} |
262 |
|
maxtheta = (PI/180.)*glarang[nglarangs-1]; |
263 |
|
else |
264 |
|
maxtheta = 0.0; |
265 |
< |
hlim = sampdens*maxtheta; |
214 |
< |
hsize = hlim + sampdens - 1; |
265 |
> |
hsize = hlim(0) + sampdens - 1; |
266 |
|
if (hsize > (int)(PI*sampdens)) |
267 |
|
hsize = PI*sampdens; |
268 |
|
indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum)); |
269 |
|
if (indirect == NULL) |
270 |
|
memerr("indirect illuminances"); |
271 |
+ |
npixinvw = npixmiss = 0L; |
272 |
|
copystruct(&leftview, &ourview); |
273 |
|
copystruct(&rightview, &ourview); |
274 |
|
spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta); |
277 |
|
setview(&rightview); |
278 |
|
indirect[nglarangs].lcos = |
279 |
|
indirect[nglarangs].rcos = cos(maxtheta); |
280 |
< |
indirect[nglarangs].lsin = |
281 |
< |
-(indirect[nglarangs].rsin = sin(maxtheta)); |
280 |
> |
indirect[nglarangs].rsin = |
281 |
> |
-(indirect[nglarangs].lsin = sin(maxtheta)); |
282 |
|
indirect[nglarangs].theta = 0.0; |
283 |
|
for (i = 0; i < nglarangs; i++) { |
284 |
|
d = (glarang[nglarangs-1] - glarang[i])*(PI/180.); |
285 |
|
indirect[nglarangs-i-1].lcos = |
286 |
|
indirect[nglarangs+i+1].rcos = cos(d); |
287 |
< |
indirect[nglarangs-i-1].lsin = |
288 |
< |
-(indirect[nglarangs+i+1].rsin = sin(d)); |
287 |
> |
indirect[nglarangs+i+1].rsin = |
288 |
> |
-(indirect[nglarangs-i-1].lsin = sin(d)); |
289 |
|
d = (glarang[nglarangs-1] + glarang[i])*(PI/180.); |
290 |
|
indirect[nglarangs-i-1].rcos = |
291 |
|
indirect[nglarangs+i+1].lcos = cos(d); |
292 |
< |
indirect[nglarangs+i+1].lsin = |
293 |
< |
-(indirect[nglarangs-i-1].rsin = sin(d)); |
294 |
< |
indirect[nglarangs-i-1].theta = -(PI/180.)*glarang[i]; |
295 |
< |
indirect[nglarangs+i+1].theta = (PI/180.)*glarang[i]; |
292 |
> |
indirect[nglarangs-i-1].rsin = |
293 |
> |
-(indirect[nglarangs+i+1].lsin = sin(d)); |
294 |
> |
indirect[nglarangs-i-1].theta = (PI/180.)*glarang[i]; |
295 |
> |
indirect[nglarangs+i+1].theta = -(PI/180.)*glarang[i]; |
296 |
|
} |
297 |
|
/* open picture */ |
298 |
|
if (picture != NULL) { |
317 |
|
cleanup() /* close files, wait for children */ |
318 |
|
{ |
319 |
|
if (verbose) |
320 |
< |
fprintf(stderr, "%s: cleaning up...\n", progname); |
320 |
> |
fprintf(stderr, "%s: cleaning up... \n", progname); |
321 |
|
if (picture != NULL) |
322 |
|
close_pict(); |
323 |
|
if (octree != NULL) |
324 |
|
done_rtrace(); |
325 |
+ |
if (npixinvw < 100*npixmiss) |
326 |
+ |
fprintf(stderr, "%s: warning -- missing %d%% of samples\n", |
327 |
+ |
progname, (int)(100L*npixmiss/npixinvw)); |
328 |
|
} |
329 |
|
|
330 |
|
|
332 |
|
FVECT vd; |
333 |
|
int x, y; |
334 |
|
{ |
335 |
+ |
int hl; |
336 |
|
FVECT org; /* dummy variable */ |
337 |
|
|
338 |
< |
if (x <= -hlim) /* left region */ |
338 |
> |
hl = hlim(y); |
339 |
> |
if (x <= -hl) { /* left region */ |
340 |
> |
if (x <= -hl-sampdens) |
341 |
> |
return(-1); |
342 |
|
return(viewray(org, vd, &leftview, |
343 |
< |
(x+hlim)/(2.*sampdens)+.5, |
344 |
< |
y/(2.*sampdens)+.5)); |
345 |
< |
if (x >= hlim) /* right region */ |
343 |
> |
(double)(x+hl)/(2*sampdens)+.5, |
344 |
> |
(double)y/(2*sampdens)+.5)); |
345 |
> |
} |
346 |
> |
if (x >= hl) { /* right region */ |
347 |
> |
if (x >= hl+sampdens) |
348 |
> |
return(-1); |
349 |
|
return(viewray(org, vd, &rightview, |
350 |
< |
(x-hlim)/(2.*sampdens)+.5, |
351 |
< |
y/(2.*sampdens)+.5)); |
352 |
< |
/* central region */ |
353 |
< |
if (viewray(org, vd, &ourview, .5, y/(2.*sampdens)+.5) < 0) |
350 |
> |
(double)(x-hl)/(2*sampdens)+.5, |
351 |
> |
(double)y/(2*sampdens)+.5)); |
352 |
> |
} |
353 |
> |
/* central region */ |
354 |
> |
if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0) |
355 |
|
return(-1); |
356 |
< |
spinvector(vd, vd, ourview.vup, h_theta(x)); |
356 |
> |
spinvector(vd, vd, ourview.vup, h_theta(x,y)); |
357 |
|
return(0); |
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
+ |
double |
362 |
+ |
pixsize(x, y) /* return the solid angle of pixel at (x,y) */ |
363 |
+ |
int x, y; |
364 |
+ |
{ |
365 |
+ |
register int hl, xo; |
366 |
+ |
double disc; |
367 |
+ |
|
368 |
+ |
hl = hlim(y); |
369 |
+ |
if (x < -hl) |
370 |
+ |
xo = x+hl; |
371 |
+ |
else if (x > hl) |
372 |
+ |
xo = x-hl; |
373 |
+ |
else |
374 |
+ |
xo = 0; |
375 |
+ |
disc = 1. - (double)((long)xo*xo + (long)y*y)/((long)sampdens*sampdens); |
376 |
+ |
if (disc <= FTINY*FTINY) |
377 |
+ |
return(0.); |
378 |
+ |
return(1./(sampdens*sampdens*sqrt(disc))); |
379 |
+ |
} |
380 |
+ |
|
381 |
+ |
|
382 |
|
memerr(s) /* malloc failure */ |
383 |
|
char *s; |
384 |
|
{ |
406 |
|
|
407 |
|
printf("BEGIN indirect illuminance\n"); |
408 |
|
for (i = 0; i < nglardirs; i++) |
409 |
< |
printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta, |
410 |
< |
PI * indirect[i].sum / (double)indirect[i].n); |
409 |
> |
if (indirect[i].n > FTINY) |
410 |
> |
printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta, |
411 |
> |
PI * indirect[i].sum / indirect[i].n); |
412 |
|
printf("END indirect illuminance\n"); |
413 |
|
} |