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) |
13 |
< |
#define VEQ(v1,v2) (FEQ((v1)[0],(v2)[0])&&FEQ((v1)[1],(v2)[1]) \ |
17 |
< |
&&FEQ((v1)[2],(v2)[2])) |
12 |
> |
char *rtargv[64] = {"rtrace", "-h-", "-ov", "-fff", "-ld-", "-i-", "-I-"}; |
13 |
> |
int rtargc = 7; |
14 |
|
|
19 |
– |
char *rtargv[32] = {"rtrace", "-h", "-ov", "-fff"}; |
20 |
– |
int rtargc = 4; |
21 |
– |
|
15 |
|
VIEW ourview = STDVIEW; /* our view */ |
16 |
|
VIEW pictview = STDVIEW; /* picture view */ |
17 |
|
VIEW leftview, rightview; /* leftmost and rightmost views */ |
22 |
|
int verbose = 0; /* verbose reporting */ |
23 |
|
char *progname; /* global argv[0] */ |
24 |
|
|
25 |
+ |
double threshold = 0.; /* glare threshold */ |
26 |
+ |
|
27 |
|
int sampdens = SAMPDENS; /* sample density */ |
28 |
|
ANGLE glarang[180] = {AEND}; /* glare calculation angles */ |
29 |
|
int nglarangs = 0; |
30 |
|
double maxtheta; /* maximum angle (in radians) */ |
31 |
|
int hsize; /* horizontal size */ |
37 |
– |
int hlim; /* central limit of horizontal */ |
32 |
|
|
33 |
|
struct illum *indirect; /* array of indirect illuminances */ |
34 |
|
|
35 |
+ |
long npixinvw; /* number of pixels in view */ |
36 |
+ |
long npixmiss; /* number of pixels missed */ |
37 |
|
|
38 |
< |
main(argc, argv) |
39 |
< |
int argc; |
40 |
< |
char *argv[]; |
38 |
> |
static int angcmp(const void *ap1, const void *ap2); |
39 |
> |
static void init(void); |
40 |
> |
static void cleanup(void); |
41 |
> |
static void printsources(void); |
42 |
> |
static void printillum(void); |
43 |
> |
|
44 |
> |
|
45 |
> |
|
46 |
> |
int |
47 |
> |
main( |
48 |
> |
int argc, |
49 |
> |
char *argv[] |
50 |
> |
) |
51 |
|
{ |
52 |
+ |
int combine = 1; |
53 |
|
int gotview = 0; |
54 |
|
int rval, i; |
55 |
|
char *err; |
57 |
|
progname = argv[0]; |
58 |
|
/* process options */ |
59 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
60 |
+ |
/* expand arguments */ |
61 |
+ |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
62 |
+ |
; |
63 |
+ |
if (rval < 0) { |
64 |
+ |
fprintf(stderr, "%s: cannot expand '%s'\n", |
65 |
+ |
argv[0], argv[i]); |
66 |
+ |
exit(1); |
67 |
+ |
} |
68 |
|
rval = getviewopt(&ourview, argc-i, argv+i); |
69 |
|
if (rval >= 0) { |
70 |
|
i += rval; |
72 |
|
continue; |
73 |
|
} |
74 |
|
switch (argv[i][1]) { |
75 |
+ |
case 't': |
76 |
+ |
threshold = atof(argv[++i]); |
77 |
+ |
break; |
78 |
|
case 'r': |
79 |
|
sampdens = atoi(argv[++i])/2; |
80 |
|
break; |
85 |
|
} |
86 |
|
if (argv[i][2] != 'f') |
87 |
|
goto userr; |
88 |
< |
rval = viewfile(argv[++i], &ourview, 0, 0); |
88 |
> |
rval = viewfile(argv[++i], &ourview, NULL); |
89 |
|
if (rval < 0) { |
90 |
|
fprintf(stderr, |
91 |
|
"%s: cannot open view file \"%s\"\n", |
111 |
|
case 'p': |
112 |
|
picture = argv[++i]; |
113 |
|
break; |
114 |
+ |
case 'c': |
115 |
+ |
combine = !combine; |
116 |
+ |
break; |
117 |
|
case 'd': |
118 |
+ |
rtargv[rtargc++] = argv[i]; |
119 |
+ |
if (argv[i][2] != 'v') |
120 |
+ |
rtargv[rtargc++] = argv[++i]; |
121 |
+ |
break; |
122 |
|
case 'l': |
123 |
+ |
if (argv[i][2] == 'd') |
124 |
+ |
break; |
125 |
+ |
/* FALL THROUGH */ |
126 |
+ |
case 's': |
127 |
+ |
case 'P': |
128 |
+ |
case 'n': |
129 |
|
rtargv[rtargc++] = argv[i]; |
130 |
|
rtargv[rtargc++] = argv[++i]; |
131 |
|
break; |
132 |
+ |
case 'w': |
133 |
+ |
case 'u': |
134 |
+ |
case 'b': |
135 |
+ |
rtargv[rtargc++] = argv[i]; |
136 |
+ |
break; |
137 |
|
case 'a': |
138 |
|
rtargv[rtargc++] = argv[i]; |
139 |
|
if (argv[i][2] == 'v') { |
142 |
|
} |
143 |
|
rtargv[rtargc++] = argv[++i]; |
144 |
|
break; |
145 |
+ |
case 'm': |
146 |
+ |
rtargv[rtargc++] = argv[i]; |
147 |
+ |
if ((argv[i][2] == 'e') | (argv[i][2] == 'a')) { |
148 |
+ |
rtargv[rtargc++] = argv[++i]; |
149 |
+ |
rtargv[rtargc++] = argv[++i]; |
150 |
+ |
} |
151 |
+ |
rtargv[rtargc++] = argv[++i]; |
152 |
+ |
break; |
153 |
|
default: |
154 |
|
goto userr; |
155 |
|
} |
163 |
|
} |
164 |
|
/* get view */ |
165 |
|
if (picture != NULL) { |
166 |
< |
rval = viewfile(picture, &pictview, 0, 0); |
166 |
> |
rval = viewfile(picture, &pictview, NULL); |
167 |
|
if (rval < 0) { |
168 |
|
fprintf(stderr, "%s: cannot open picture file \"%s\"\n", |
169 |
|
progname, picture); |
190 |
|
progname); |
191 |
|
exit(1); |
192 |
|
} |
193 |
< |
copystruct(&ourview, &pictview); |
194 |
< |
} else if (picture != NULL && !VEQ(ourview.vp, pictview.vp)) { |
193 |
> |
ourview = pictview; |
194 |
> |
} else if (picture != NULL && !VABSEQ(ourview.vp, pictview.vp)) { |
195 |
|
fprintf(stderr, "%s: picture must have same viewpoint\n", |
196 |
|
progname); |
197 |
|
exit(1); |
212 |
|
exit(1); |
213 |
|
} |
214 |
|
init(); /* initialize program */ |
215 |
< |
comp_thresh(); /* compute glare threshold */ |
215 |
> |
if (threshold <= FTINY) |
216 |
> |
comp_thresh(); /* compute glare threshold */ |
217 |
|
analyze(); /* analyze view */ |
218 |
+ |
if (combine) |
219 |
+ |
absorb_specks(); /* eliminate tiny sources */ |
220 |
|
cleanup(); /* tidy up */ |
221 |
|
/* print header */ |
222 |
+ |
newheader("RADIANCE", stdout); |
223 |
|
printargs(argc, argv, stdout); |
224 |
|
fputs(VIEWSTR, stdout); |
225 |
|
fprintview(&ourview, stdout); |
226 |
< |
printf("\n\n"); |
226 |
> |
printf("\n"); |
227 |
> |
fputformat("ascii", stdout); |
228 |
> |
printf("\n"); |
229 |
|
printsources(); /* print glare sources */ |
230 |
|
printillum(); /* print illuminances */ |
231 |
|
exit(0); |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
< |
init() /* initialize global variables */ |
240 |
> |
static int |
241 |
> |
angcmp( /* compare two angles */ |
242 |
> |
const void *ap1, |
243 |
> |
const void *ap2 |
244 |
> |
) |
245 |
|
{ |
246 |
+ |
int a1, a2; |
247 |
+ |
|
248 |
+ |
a1 = *(ANGLE *)ap1; |
249 |
+ |
a2 = *(ANGLE *)ap2; |
250 |
+ |
if (a1 == a2) { |
251 |
+ |
fprintf(stderr, "%s: duplicate glare angle (%d)\n", |
252 |
+ |
progname, a1); |
253 |
+ |
exit(1); |
254 |
+ |
} |
255 |
+ |
return(a1-a2); |
256 |
+ |
} |
257 |
+ |
|
258 |
+ |
|
259 |
+ |
static void |
260 |
+ |
init(void) /* initialize global variables */ |
261 |
+ |
{ |
262 |
|
double d; |
263 |
< |
register int i; |
263 |
> |
int i; |
264 |
|
|
265 |
|
if (verbose) |
266 |
|
fprintf(stderr, "%s: initializing data structures...\n", |
268 |
|
/* set direction vectors */ |
269 |
|
for (i = 0; glarang[i] != AEND; i++) |
270 |
|
; |
271 |
< |
if (i > 0 && glarang[0] <= 0 || glarang[i-1] >= 180) { |
272 |
< |
fprintf(stderr, "%s: glare angles must be between 1 and 179\n", |
271 |
> |
qsort(glarang, i, sizeof(ANGLE), angcmp); |
272 |
> |
if (i > 0 && (glarang[0] <= 0 || glarang[i-1] > 180)) { |
273 |
> |
fprintf(stderr, "%s: glare angles must be between 1 and 180\n", |
274 |
|
progname); |
275 |
|
exit(1); |
276 |
|
} |
281 |
|
maxtheta = (PI/180.)*glarang[nglarangs-1]; |
282 |
|
else |
283 |
|
maxtheta = 0.0; |
284 |
< |
hlim = sampdens*maxtheta; |
214 |
< |
hsize = hlim + sampdens - 1; |
284 |
> |
hsize = hlim(0) + sampdens - 1; |
285 |
|
if (hsize > (int)(PI*sampdens)) |
286 |
|
hsize = PI*sampdens; |
287 |
|
indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum)); |
288 |
|
if (indirect == NULL) |
289 |
|
memerr("indirect illuminances"); |
290 |
< |
copystruct(&leftview, &ourview); |
291 |
< |
copystruct(&rightview, &ourview); |
290 |
> |
npixinvw = npixmiss = 0L; |
291 |
> |
leftview = ourview; |
292 |
> |
rightview = ourview; |
293 |
|
spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta); |
294 |
|
spinvector(rightview.vdir, ourview.vdir, ourview.vup, -maxtheta); |
295 |
|
setview(&leftview); |
296 |
|
setview(&rightview); |
297 |
|
indirect[nglarangs].lcos = |
298 |
|
indirect[nglarangs].rcos = cos(maxtheta); |
299 |
< |
indirect[nglarangs].lsin = |
300 |
< |
-(indirect[nglarangs].rsin = sin(maxtheta)); |
299 |
> |
indirect[nglarangs].rsin = |
300 |
> |
-(indirect[nglarangs].lsin = sin(maxtheta)); |
301 |
|
indirect[nglarangs].theta = 0.0; |
302 |
|
for (i = 0; i < nglarangs; i++) { |
303 |
|
d = (glarang[nglarangs-1] - glarang[i])*(PI/180.); |
304 |
|
indirect[nglarangs-i-1].lcos = |
305 |
|
indirect[nglarangs+i+1].rcos = cos(d); |
306 |
< |
indirect[nglarangs-i-1].lsin = |
307 |
< |
-(indirect[nglarangs+i+1].rsin = sin(d)); |
306 |
> |
indirect[nglarangs+i+1].rsin = |
307 |
> |
-(indirect[nglarangs-i-1].lsin = sin(d)); |
308 |
|
d = (glarang[nglarangs-1] + glarang[i])*(PI/180.); |
309 |
|
indirect[nglarangs-i-1].rcos = |
310 |
|
indirect[nglarangs+i+1].lcos = cos(d); |
311 |
< |
indirect[nglarangs+i+1].lsin = |
312 |
< |
-(indirect[nglarangs-i-1].rsin = sin(d)); |
313 |
< |
indirect[nglarangs-i-1].theta = -(PI/180.)*glarang[i]; |
314 |
< |
indirect[nglarangs+i+1].theta = (PI/180.)*glarang[i]; |
311 |
> |
indirect[nglarangs-i-1].rsin = |
312 |
> |
-(indirect[nglarangs+i+1].lsin = sin(d)); |
313 |
> |
indirect[nglarangs-i-1].theta = (PI/180.)*glarang[i]; |
314 |
> |
indirect[nglarangs+i+1].theta = -(PI/180.)*glarang[i]; |
315 |
|
} |
316 |
|
/* open picture */ |
317 |
|
if (picture != NULL) { |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
< |
cleanup() /* close files, wait for children */ |
336 |
> |
static void |
337 |
> |
cleanup(void) /* close files, wait for children */ |
338 |
|
{ |
339 |
|
if (verbose) |
340 |
< |
fprintf(stderr, "%s: cleaning up...\n", progname); |
340 |
> |
fprintf(stderr, "%s: cleaning up... \n", progname); |
341 |
|
if (picture != NULL) |
342 |
|
close_pict(); |
343 |
|
if (octree != NULL) |
344 |
|
done_rtrace(); |
345 |
+ |
if (npixinvw < 100*npixmiss) |
346 |
+ |
fprintf(stderr, "%s: warning -- missing %d%% of samples\n", |
347 |
+ |
progname, (int)(100L*npixmiss/npixinvw)); |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
< |
compdir(vd, x, y) /* compute direction for x,y */ |
352 |
< |
FVECT vd; |
353 |
< |
int x, y; |
351 |
> |
int |
352 |
> |
compdir( /* compute direction for x,y */ |
353 |
> |
FVECT vd, |
354 |
> |
int x, |
355 |
> |
int y |
356 |
> |
) |
357 |
|
{ |
358 |
+ |
int hl; |
359 |
|
FVECT org; /* dummy variable */ |
360 |
|
|
361 |
< |
if (x <= -hlim) /* left region */ |
361 |
> |
hl = hlim(y); |
362 |
> |
if (x <= -hl) { /* left region */ |
363 |
> |
if (x <= -hl-sampdens) |
364 |
> |
return(-1); |
365 |
|
return(viewray(org, vd, &leftview, |
366 |
< |
(x+hlim)/(2.*sampdens)+.5, |
367 |
< |
y/(2.*sampdens)+.5)); |
368 |
< |
if (x >= hlim) /* right region */ |
366 |
> |
(double)(x+hl)/(2*sampdens)+.5, |
367 |
> |
(double)y/(2*sampdens)+.5)); |
368 |
> |
} |
369 |
> |
if (x >= hl) { /* right region */ |
370 |
> |
if (x >= hl+sampdens) |
371 |
> |
return(-1); |
372 |
|
return(viewray(org, vd, &rightview, |
373 |
< |
(x-hlim)/(2.*sampdens)+.5, |
374 |
< |
y/(2.*sampdens)+.5)); |
375 |
< |
/* central region */ |
376 |
< |
if (viewray(org, vd, &ourview, .5, y/(2.*sampdens)+.5) < 0) |
373 |
> |
(double)(x-hl)/(2*sampdens)+.5, |
374 |
> |
(double)y/(2*sampdens)+.5)); |
375 |
> |
} |
376 |
> |
/* central region */ |
377 |
> |
if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0) |
378 |
|
return(-1); |
379 |
< |
spinvector(vd, vd, ourview.vup, h_theta(x)); |
379 |
> |
spinvector(vd, vd, ourview.vup, h_theta(x,y)); |
380 |
|
return(0); |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
< |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
385 |
< |
FVECT vres, vorig, vnorm; |
386 |
< |
double theta; |
384 |
> |
double |
385 |
> |
pixsize( /* return the solid angle of pixel at (x,y) */ |
386 |
> |
int x, |
387 |
> |
int y |
388 |
> |
) |
389 |
|
{ |
390 |
< |
extern double sin(), cos(); |
391 |
< |
double sint, cost, dotp; |
392 |
< |
FVECT vperp; |
393 |
< |
register int i; |
394 |
< |
|
395 |
< |
sint = sin(theta); |
396 |
< |
cost = cos(theta); |
397 |
< |
dotp = DOT(vorig, vnorm); |
398 |
< |
fcross(vperp, vnorm, vorig); |
399 |
< |
for (i = 0; i < 3; i++) |
400 |
< |
vres[i] = vnorm[i]*dotp*(1.-cost) + |
401 |
< |
vorig[i]*cost + vperp[i]*sint; |
390 |
> |
int hl, xo; |
391 |
> |
double disc; |
392 |
> |
|
393 |
> |
hl = hlim(y); |
394 |
> |
if (x < -hl) |
395 |
> |
xo = x+hl; |
396 |
> |
else if (x > hl) |
397 |
> |
xo = x-hl; |
398 |
> |
else |
399 |
> |
xo = 0; |
400 |
> |
disc = 1. - (double)((long)xo*xo + (long)y*y)/((long)sampdens*sampdens); |
401 |
> |
if (disc <= FTINY*FTINY) |
402 |
> |
return(0.); |
403 |
> |
return(1./(sampdens*sampdens*sqrt(disc))); |
404 |
|
} |
405 |
|
|
406 |
|
|
407 |
< |
memerr(s) /* malloc failure */ |
408 |
< |
char *s; |
407 |
> |
void |
408 |
> |
memerr( /* malloc failure */ |
409 |
> |
char *s |
410 |
> |
) |
411 |
|
{ |
412 |
|
fprintf(stderr, "%s: out of memory for %s\n", progname, s); |
413 |
|
exit(1); |
414 |
|
} |
415 |
|
|
416 |
|
|
417 |
< |
printsources() /* print out glare sources */ |
417 |
> |
static void |
418 |
> |
printsources(void) /* print out glare sources */ |
419 |
|
{ |
420 |
< |
register struct source *sp; |
420 |
> |
struct source *sp; |
421 |
|
|
422 |
|
printf("BEGIN glare source\n"); |
423 |
|
for (sp = donelist; sp != NULL; sp = sp->next) |
424 |
< |
printf("\t%f %f %f\t%f\t%f\n", |
424 |
> |
printf("\t%f %f %f\t%e\t%e\n", |
425 |
|
sp->dir[0], sp->dir[1], sp->dir[2], |
426 |
|
sp->dom, sp->brt); |
427 |
|
printf("END glare source\n"); |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
< |
printillum() /* print out indirect illuminances */ |
431 |
> |
static void |
432 |
> |
printillum(void) /* print out indirect illuminances */ |
433 |
|
{ |
434 |
< |
register int i; |
434 |
> |
int i; |
435 |
|
|
436 |
|
printf("BEGIN indirect illuminance\n"); |
437 |
|
for (i = 0; i < nglardirs; i++) |
438 |
< |
printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta, |
439 |
< |
PI * indirect[i].sum / (double)indirect[i].n); |
438 |
> |
if (indirect[i].n > FTINY) |
439 |
> |
printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta, |
440 |
> |
PI * indirect[i].sum / indirect[i].n); |
441 |
|
printf("END indirect illuminance\n"); |
442 |
|
} |