1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Find glare sources in a scene or image. |
9 |
* |
10 |
* Greg Ward March 1991 |
11 |
*/ |
12 |
|
13 |
#include "glare.h" |
14 |
|
15 |
#define FEQ(a,b) ((a)-(b)<=FTINY&&(b)-(a)<=FTINY) |
16 |
#define VEQ(v1,v2) (FEQ((v1)[0],(v2)[0])&&FEQ((v1)[1],(v2)[1]) \ |
17 |
&&FEQ((v1)[2],(v2)[2])) |
18 |
|
19 |
char *rtargv[32] = {"rtrace", "-h-", "-ov", "-fff"}; |
20 |
int rtargc = 4; |
21 |
|
22 |
VIEW ourview = STDVIEW; /* our view */ |
23 |
VIEW pictview = STDVIEW; /* picture view */ |
24 |
VIEW leftview, rightview; /* leftmost and rightmost views */ |
25 |
|
26 |
char *picture = NULL; /* picture file name */ |
27 |
char *octree = NULL; /* octree file name */ |
28 |
|
29 |
int verbose = 0; /* verbose reporting */ |
30 |
char *progname; /* global argv[0] */ |
31 |
|
32 |
double threshold = 0.; /* glare threshold */ |
33 |
|
34 |
int sampdens = SAMPDENS; /* sample density */ |
35 |
ANGLE glarang[180] = {AEND}; /* glare calculation angles */ |
36 |
int nglarangs = 0; |
37 |
double maxtheta; /* maximum angle (in radians) */ |
38 |
int hsize; /* horizontal size */ |
39 |
|
40 |
struct illum *indirect; /* array of indirect illuminances */ |
41 |
|
42 |
long npixinvw; /* number of pixels in view */ |
43 |
long npixmiss; /* number of pixels missed */ |
44 |
|
45 |
|
46 |
main(argc, argv) |
47 |
int argc; |
48 |
char *argv[]; |
49 |
{ |
50 |
int combine = 1; |
51 |
int gotview = 0; |
52 |
int rval, i; |
53 |
char *err; |
54 |
|
55 |
progname = argv[0]; |
56 |
/* process options */ |
57 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
58 |
rval = getviewopt(&ourview, argc-i, argv+i); |
59 |
if (rval >= 0) { |
60 |
i += rval; |
61 |
gotview++; |
62 |
continue; |
63 |
} |
64 |
switch (argv[i][1]) { |
65 |
case 't': |
66 |
threshold = atof(argv[++i]); |
67 |
break; |
68 |
case 'r': |
69 |
sampdens = atoi(argv[++i])/2; |
70 |
break; |
71 |
case 'v': |
72 |
if (argv[i][2] == '\0') { |
73 |
verbose++; |
74 |
break; |
75 |
} |
76 |
if (argv[i][2] != 'f') |
77 |
goto userr; |
78 |
rval = viewfile(argv[++i], &ourview, 0, 0); |
79 |
if (rval < 0) { |
80 |
fprintf(stderr, |
81 |
"%s: cannot open view file \"%s\"\n", |
82 |
progname, argv[i]); |
83 |
exit(1); |
84 |
} else if (rval == 0) { |
85 |
fprintf(stderr, |
86 |
"%s: bad view file \"%s\"\n", |
87 |
progname, argv[i]); |
88 |
exit(1); |
89 |
} else |
90 |
gotview++; |
91 |
break; |
92 |
case 'g': |
93 |
if (argv[i][2] != 'a') |
94 |
goto userr; |
95 |
if (setscan(glarang, argv[++i]) < 0) { |
96 |
fprintf(stderr, "%s: bad angle spec \"%s\"\n", |
97 |
progname, argv[i]); |
98 |
exit(1); |
99 |
} |
100 |
break; |
101 |
case 'p': |
102 |
picture = argv[++i]; |
103 |
break; |
104 |
case 'c': |
105 |
combine = !combine; |
106 |
break; |
107 |
case 'd': |
108 |
case 'l': |
109 |
case 's': |
110 |
rtargv[rtargc++] = argv[i]; |
111 |
rtargv[rtargc++] = argv[++i]; |
112 |
break; |
113 |
case 'a': |
114 |
rtargv[rtargc++] = argv[i]; |
115 |
if (argv[i][2] == 'v') { |
116 |
rtargv[rtargc++] = argv[++i]; |
117 |
rtargv[rtargc++] = argv[++i]; |
118 |
} |
119 |
rtargv[rtargc++] = argv[++i]; |
120 |
break; |
121 |
default: |
122 |
goto userr; |
123 |
} |
124 |
} |
125 |
/* get octree */ |
126 |
if (i < argc-1) |
127 |
goto userr; |
128 |
if (i == argc-1) { |
129 |
rtargv[rtargc++] = octree = argv[i]; |
130 |
rtargv[rtargc] = NULL; |
131 |
} |
132 |
/* get view */ |
133 |
if (picture != NULL) { |
134 |
rval = viewfile(picture, &pictview, 0, 0); |
135 |
if (rval < 0) { |
136 |
fprintf(stderr, "%s: cannot open picture file \"%s\"\n", |
137 |
progname, picture); |
138 |
exit(1); |
139 |
} else if (rval == 0) { |
140 |
fprintf(stderr, |
141 |
"%s: cannot get view from picture \"%s\"\n", |
142 |
progname, picture); |
143 |
exit(1); |
144 |
} |
145 |
if (pictview.type == VT_PAR) { |
146 |
fprintf(stderr, "%s: %s: cannot use parallel view\n", |
147 |
progname, picture); |
148 |
exit(1); |
149 |
} |
150 |
if ((err = setview(&pictview)) != NULL) { |
151 |
fprintf(stderr, "%s: %s\n", picture, err); |
152 |
exit(1); |
153 |
} |
154 |
} |
155 |
if (!gotview) { |
156 |
if (picture == NULL) { |
157 |
fprintf(stderr, "%s: must have view or picture\n", |
158 |
progname); |
159 |
exit(1); |
160 |
} |
161 |
copystruct(&ourview, &pictview); |
162 |
} else if (picture != NULL && !VEQ(ourview.vp, pictview.vp)) { |
163 |
fprintf(stderr, "%s: picture must have same viewpoint\n", |
164 |
progname); |
165 |
exit(1); |
166 |
} |
167 |
ourview.type = VT_HEM; |
168 |
ourview.horiz = ourview.vert = 180.0; |
169 |
ourview.hoff = ourview.voff = 0.0; |
170 |
fvsum(ourview.vdir, ourview.vdir, ourview.vup, |
171 |
-DOT(ourview.vdir,ourview.vup)); |
172 |
if ((err = setview(&ourview)) != NULL) { |
173 |
fprintf(stderr, "%s: %s\n", progname, err); |
174 |
exit(1); |
175 |
} |
176 |
if (octree == NULL && picture == NULL) { |
177 |
fprintf(stderr, |
178 |
"%s: must specify at least one of picture or octree\n", |
179 |
progname); |
180 |
exit(1); |
181 |
} |
182 |
init(); /* initialize program */ |
183 |
if (threshold <= FTINY) |
184 |
comp_thresh(); /* compute glare threshold */ |
185 |
analyze(); /* analyze view */ |
186 |
if (combine) |
187 |
absorb_specks(); /* eliminate tiny sources */ |
188 |
cleanup(); /* tidy up */ |
189 |
/* print header */ |
190 |
printargs(argc, argv, stdout); |
191 |
fputs(VIEWSTR, stdout); |
192 |
fprintview(&ourview, stdout); |
193 |
printf("\n"); |
194 |
fputformat("ascii", stdout); |
195 |
printf("\n"); |
196 |
printsources(); /* print glare sources */ |
197 |
printillum(); /* print illuminances */ |
198 |
exit(0); |
199 |
userr: |
200 |
fprintf(stderr, |
201 |
"Usage: %s [view options][-ga angles][-p picture][[rtrace options] octree]\n", |
202 |
progname); |
203 |
exit(1); |
204 |
} |
205 |
|
206 |
|
207 |
int |
208 |
angcmp(ap1, ap2) /* compare two angles */ |
209 |
ANGLE *ap1, *ap2; |
210 |
{ |
211 |
register int a1, a2; |
212 |
|
213 |
a1 = *ap1; |
214 |
a2 = *ap2; |
215 |
if (a1 == a2) { |
216 |
fprintf(stderr, "%s: duplicate glare angle (%d)\n", |
217 |
progname, a1); |
218 |
exit(1); |
219 |
} |
220 |
return(a1-a2); |
221 |
} |
222 |
|
223 |
|
224 |
init() /* initialize global variables */ |
225 |
{ |
226 |
double d; |
227 |
register int i; |
228 |
|
229 |
if (verbose) |
230 |
fprintf(stderr, "%s: initializing data structures...\n", |
231 |
progname); |
232 |
/* set direction vectors */ |
233 |
for (i = 0; glarang[i] != AEND; i++) |
234 |
; |
235 |
qsort(glarang, i, sizeof(ANGLE), angcmp); |
236 |
if (i > 0 && (glarang[0] <= 0 || glarang[i-1] > 180)) { |
237 |
fprintf(stderr, "%s: glare angles must be between 1 and 180\n", |
238 |
progname); |
239 |
exit(1); |
240 |
} |
241 |
nglarangs = i; |
242 |
/* nglardirs = 2*nglarangs + 1; */ |
243 |
/* vsize = sampdens - 1; */ |
244 |
if (nglarangs > 0) |
245 |
maxtheta = (PI/180.)*glarang[nglarangs-1]; |
246 |
else |
247 |
maxtheta = 0.0; |
248 |
hsize = hlim(0) + sampdens - 1; |
249 |
if (hsize > (int)(PI*sampdens)) |
250 |
hsize = PI*sampdens; |
251 |
indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum)); |
252 |
if (indirect == NULL) |
253 |
memerr("indirect illuminances"); |
254 |
npixinvw = npixmiss = 0L; |
255 |
copystruct(&leftview, &ourview); |
256 |
copystruct(&rightview, &ourview); |
257 |
spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta); |
258 |
spinvector(rightview.vdir, ourview.vdir, ourview.vup, -maxtheta); |
259 |
setview(&leftview); |
260 |
setview(&rightview); |
261 |
indirect[nglarangs].lcos = |
262 |
indirect[nglarangs].rcos = cos(maxtheta); |
263 |
indirect[nglarangs].rsin = |
264 |
-(indirect[nglarangs].lsin = sin(maxtheta)); |
265 |
indirect[nglarangs].theta = 0.0; |
266 |
for (i = 0; i < nglarangs; i++) { |
267 |
d = (glarang[nglarangs-1] - glarang[i])*(PI/180.); |
268 |
indirect[nglarangs-i-1].lcos = |
269 |
indirect[nglarangs+i+1].rcos = cos(d); |
270 |
indirect[nglarangs+i+1].rsin = |
271 |
-(indirect[nglarangs-i-1].lsin = sin(d)); |
272 |
d = (glarang[nglarangs-1] + glarang[i])*(PI/180.); |
273 |
indirect[nglarangs-i-1].rcos = |
274 |
indirect[nglarangs+i+1].lcos = cos(d); |
275 |
indirect[nglarangs-i-1].rsin = |
276 |
-(indirect[nglarangs+i+1].lsin = sin(d)); |
277 |
indirect[nglarangs-i-1].theta = (PI/180.)*glarang[i]; |
278 |
indirect[nglarangs+i+1].theta = -(PI/180.)*glarang[i]; |
279 |
} |
280 |
/* open picture */ |
281 |
if (picture != NULL) { |
282 |
if (verbose) |
283 |
fprintf(stderr, "%s: opening picture file \"%s\"...\n", |
284 |
progname, picture); |
285 |
open_pict(picture); |
286 |
} |
287 |
/* start rtrace */ |
288 |
if (octree != NULL) { |
289 |
if (verbose) { |
290 |
fprintf(stderr, |
291 |
"%s: starting luminance calculation...\n\t", |
292 |
progname); |
293 |
printargs(rtargc, rtargv, stderr); |
294 |
} |
295 |
fork_rtrace(rtargv); |
296 |
} |
297 |
} |
298 |
|
299 |
|
300 |
cleanup() /* close files, wait for children */ |
301 |
{ |
302 |
if (verbose) |
303 |
fprintf(stderr, "%s: cleaning up... \n", progname); |
304 |
if (picture != NULL) |
305 |
close_pict(); |
306 |
if (octree != NULL) |
307 |
done_rtrace(); |
308 |
if (npixinvw < 100*npixmiss) |
309 |
fprintf(stderr, "%s: warning -- missing %ld%% of samples\n", |
310 |
progname, 100L*npixmiss/npixinvw); |
311 |
} |
312 |
|
313 |
|
314 |
compdir(vd, x, y) /* compute direction for x,y */ |
315 |
FVECT vd; |
316 |
int x, y; |
317 |
{ |
318 |
int hl; |
319 |
FVECT org; /* dummy variable */ |
320 |
|
321 |
hl = hlim(y); |
322 |
if (x <= -hl) { /* left region */ |
323 |
if (x <= -hl-sampdens) |
324 |
return(-1); |
325 |
return(viewray(org, vd, &leftview, |
326 |
(double)(x+hl)/(2*sampdens)+.5, |
327 |
(double)y/(2*sampdens)+.5)); |
328 |
} |
329 |
if (x >= hl) { /* right region */ |
330 |
if (x >= hl+sampdens) |
331 |
return(-1); |
332 |
return(viewray(org, vd, &rightview, |
333 |
(double)(x-hl)/(2*sampdens)+.5, |
334 |
(double)y/(2*sampdens)+.5)); |
335 |
} |
336 |
/* central region */ |
337 |
if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0) |
338 |
return(-1); |
339 |
spinvector(vd, vd, ourview.vup, h_theta(x,y)); |
340 |
return(0); |
341 |
} |
342 |
|
343 |
|
344 |
double |
345 |
pixsize(x, y) /* return the solid angle of pixel at (x,y) */ |
346 |
int x, y; |
347 |
{ |
348 |
register int hl, xo; |
349 |
double disc; |
350 |
|
351 |
hl = hlim(y); |
352 |
if (x < -hl) |
353 |
xo = x+hl; |
354 |
else if (x > hl) |
355 |
xo = x-hl; |
356 |
else |
357 |
xo = 0; |
358 |
disc = 1. - (double)(xo*xo + y*y)/(sampdens*sampdens); |
359 |
if (disc <= FTINY) |
360 |
return(0.); |
361 |
return(1./(sampdens*sampdens*sqrt(disc))); |
362 |
} |
363 |
|
364 |
|
365 |
memerr(s) /* malloc failure */ |
366 |
char *s; |
367 |
{ |
368 |
fprintf(stderr, "%s: out of memory for %s\n", progname, s); |
369 |
exit(1); |
370 |
} |
371 |
|
372 |
|
373 |
printsources() /* print out glare sources */ |
374 |
{ |
375 |
register struct source *sp; |
376 |
|
377 |
printf("BEGIN glare source\n"); |
378 |
for (sp = donelist; sp != NULL; sp = sp->next) |
379 |
printf("\t%f %f %f\t%f\t%f\n", |
380 |
sp->dir[0], sp->dir[1], sp->dir[2], |
381 |
sp->dom, sp->brt); |
382 |
printf("END glare source\n"); |
383 |
} |
384 |
|
385 |
|
386 |
printillum() /* print out indirect illuminances */ |
387 |
{ |
388 |
register int i; |
389 |
|
390 |
printf("BEGIN indirect illuminance\n"); |
391 |
for (i = 0; i < nglardirs; i++) |
392 |
if (indirect[i].n > FTINY) |
393 |
printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta, |
394 |
PI * indirect[i].sum / indirect[i].n); |
395 |
printf("END indirect illuminance\n"); |
396 |
} |