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&&(a)-(b)<=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 |
rtargv[rtargc++] = argv[i]; |
110 |
rtargv[rtargc++] = argv[++i]; |
111 |
break; |
112 |
case 'a': |
113 |
rtargv[rtargc++] = argv[i]; |
114 |
if (argv[i][2] == 'v') { |
115 |
rtargv[rtargc++] = argv[++i]; |
116 |
rtargv[rtargc++] = argv[++i]; |
117 |
} |
118 |
rtargv[rtargc++] = argv[++i]; |
119 |
break; |
120 |
default: |
121 |
goto userr; |
122 |
} |
123 |
} |
124 |
/* get octree */ |
125 |
if (i < argc-1) |
126 |
goto userr; |
127 |
if (i == argc-1) { |
128 |
rtargv[rtargc++] = octree = argv[i]; |
129 |
rtargv[rtargc] = NULL; |
130 |
} |
131 |
/* get view */ |
132 |
if (picture != NULL) { |
133 |
rval = viewfile(picture, &pictview, 0, 0); |
134 |
if (rval < 0) { |
135 |
fprintf(stderr, "%s: cannot open picture file \"%s\"\n", |
136 |
progname, picture); |
137 |
exit(1); |
138 |
} else if (rval == 0) { |
139 |
fprintf(stderr, |
140 |
"%s: cannot get view from picture \"%s\"\n", |
141 |
progname, picture); |
142 |
exit(1); |
143 |
} |
144 |
if (pictview.type == VT_PAR) { |
145 |
fprintf(stderr, "%s: %s: cannot use parallel view\n", |
146 |
progname, picture); |
147 |
exit(1); |
148 |
} |
149 |
if ((err = setview(&pictview)) != NULL) { |
150 |
fprintf(stderr, "%s: %s\n", picture, err); |
151 |
exit(1); |
152 |
} |
153 |
} |
154 |
if (!gotview) { |
155 |
if (picture == NULL) { |
156 |
fprintf(stderr, "%s: must have view or picture\n", |
157 |
progname); |
158 |
exit(1); |
159 |
} |
160 |
copystruct(&ourview, &pictview); |
161 |
} else if (picture != NULL && !VEQ(ourview.vp, pictview.vp)) { |
162 |
fprintf(stderr, "%s: picture must have same viewpoint\n", |
163 |
progname); |
164 |
exit(1); |
165 |
} |
166 |
ourview.type = VT_HEM; |
167 |
ourview.horiz = ourview.vert = 180.0; |
168 |
ourview.hoff = ourview.voff = 0.0; |
169 |
fvsum(ourview.vdir, ourview.vdir, ourview.vup, |
170 |
-DOT(ourview.vdir,ourview.vup)); |
171 |
if ((err = setview(&ourview)) != NULL) { |
172 |
fprintf(stderr, "%s: %s\n", progname, err); |
173 |
exit(1); |
174 |
} |
175 |
if (octree == NULL && picture == NULL) { |
176 |
fprintf(stderr, |
177 |
"%s: must specify at least one of picture or octree\n", |
178 |
progname); |
179 |
exit(1); |
180 |
} |
181 |
init(); /* initialize program */ |
182 |
if (threshold <= FTINY) |
183 |
comp_thresh(); /* compute glare threshold */ |
184 |
analyze(); /* analyze view */ |
185 |
if (combine) |
186 |
absorb_specks(); /* eliminate tiny sources */ |
187 |
cleanup(); /* tidy up */ |
188 |
/* print header */ |
189 |
printargs(argc, argv, stdout); |
190 |
fputs(VIEWSTR, stdout); |
191 |
fprintview(&ourview, stdout); |
192 |
printf("\n"); |
193 |
fputformat("ascii", stdout); |
194 |
printf("\n"); |
195 |
printsources(); /* print glare sources */ |
196 |
printillum(); /* print illuminances */ |
197 |
exit(0); |
198 |
userr: |
199 |
fprintf(stderr, |
200 |
"Usage: %s [view options][-ga angles][-p picture][[rtrace options] octree]\n", |
201 |
progname); |
202 |
exit(1); |
203 |
} |
204 |
|
205 |
|
206 |
init() /* initialize global variables */ |
207 |
{ |
208 |
double d; |
209 |
register int i; |
210 |
|
211 |
if (verbose) |
212 |
fprintf(stderr, "%s: initializing data structures...\n", |
213 |
progname); |
214 |
/* set direction vectors */ |
215 |
for (i = 0; glarang[i] != AEND; i++) |
216 |
; |
217 |
if (i > 0 && (glarang[0] <= 0 || glarang[i-1] >= 180)) { |
218 |
fprintf(stderr, "%s: glare angles must be between 1 and 179\n", |
219 |
progname); |
220 |
exit(1); |
221 |
} |
222 |
nglarangs = i; |
223 |
/* nglardirs = 2*nglarangs + 1; */ |
224 |
/* vsize = sampdens - 1; */ |
225 |
if (nglarangs > 0) |
226 |
maxtheta = (PI/180.)*glarang[nglarangs-1]; |
227 |
else |
228 |
maxtheta = 0.0; |
229 |
hsize = hlim(0) + sampdens - 1; |
230 |
if (hsize > (int)(PI*sampdens)) |
231 |
hsize = PI*sampdens; |
232 |
indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum)); |
233 |
if (indirect == NULL) |
234 |
memerr("indirect illuminances"); |
235 |
npixinvw = npixmiss = 0L; |
236 |
copystruct(&leftview, &ourview); |
237 |
copystruct(&rightview, &ourview); |
238 |
spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta); |
239 |
spinvector(rightview.vdir, ourview.vdir, ourview.vup, -maxtheta); |
240 |
setview(&leftview); |
241 |
setview(&rightview); |
242 |
indirect[nglarangs].lcos = |
243 |
indirect[nglarangs].rcos = cos(maxtheta); |
244 |
indirect[nglarangs].rsin = |
245 |
-(indirect[nglarangs].lsin = sin(maxtheta)); |
246 |
indirect[nglarangs].theta = 0.0; |
247 |
for (i = 0; i < nglarangs; i++) { |
248 |
d = (glarang[nglarangs-1] - glarang[i])*(PI/180.); |
249 |
indirect[nglarangs-i-1].lcos = |
250 |
indirect[nglarangs+i+1].rcos = cos(d); |
251 |
indirect[nglarangs+i+1].rsin = |
252 |
-(indirect[nglarangs-i-1].lsin = sin(d)); |
253 |
d = (glarang[nglarangs-1] + glarang[i])*(PI/180.); |
254 |
indirect[nglarangs-i-1].rcos = |
255 |
indirect[nglarangs+i+1].lcos = cos(d); |
256 |
indirect[nglarangs-i-1].rsin = |
257 |
-(indirect[nglarangs+i+1].lsin = sin(d)); |
258 |
indirect[nglarangs-i-1].theta = (PI/180.)*glarang[i]; |
259 |
indirect[nglarangs+i+1].theta = -(PI/180.)*glarang[i]; |
260 |
} |
261 |
/* open picture */ |
262 |
if (picture != NULL) { |
263 |
if (verbose) |
264 |
fprintf(stderr, "%s: opening picture file \"%s\"...\n", |
265 |
progname, picture); |
266 |
open_pict(picture); |
267 |
} |
268 |
/* start rtrace */ |
269 |
if (octree != NULL) { |
270 |
if (verbose) { |
271 |
fprintf(stderr, |
272 |
"%s: starting luminance calculation...\n\t", |
273 |
progname); |
274 |
printargs(rtargc, rtargv, stderr); |
275 |
} |
276 |
fork_rtrace(rtargv); |
277 |
} |
278 |
} |
279 |
|
280 |
|
281 |
cleanup() /* close files, wait for children */ |
282 |
{ |
283 |
if (verbose) |
284 |
fprintf(stderr, "%s: cleaning up... \n", progname); |
285 |
if (picture != NULL) |
286 |
close_pict(); |
287 |
if (octree != NULL) |
288 |
done_rtrace(); |
289 |
if (npixinvw < 100*npixmiss) |
290 |
fprintf(stderr, "%s: warning -- missing %ld%% of samples\n", |
291 |
progname, 100L*npixmiss/npixinvw); |
292 |
} |
293 |
|
294 |
|
295 |
compdir(vd, x, y) /* compute direction for x,y */ |
296 |
FVECT vd; |
297 |
int x, y; |
298 |
{ |
299 |
int hl; |
300 |
FVECT org; /* dummy variable */ |
301 |
|
302 |
hl = hlim(y); |
303 |
if (x <= -hl) { /* left region */ |
304 |
if (x <= -hl-sampdens) |
305 |
return(-1); |
306 |
return(viewray(org, vd, &leftview, |
307 |
(double)(x+hl)/(2*sampdens)+.5, |
308 |
(double)y/(2*sampdens)+.5)); |
309 |
} |
310 |
if (x >= hl) { /* right region */ |
311 |
if (x >= hl+sampdens) |
312 |
return(-1); |
313 |
return(viewray(org, vd, &rightview, |
314 |
(double)(x-hl)/(2*sampdens)+.5, |
315 |
(double)y/(2*sampdens)+.5)); |
316 |
} |
317 |
/* central region */ |
318 |
if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0) |
319 |
return(-1); |
320 |
spinvector(vd, vd, ourview.vup, h_theta(x,y)); |
321 |
return(0); |
322 |
} |
323 |
|
324 |
|
325 |
double |
326 |
pixsize(x, y) /* return the solid angle of pixel at (x,y) */ |
327 |
int x, y; |
328 |
{ |
329 |
register int hl, xo; |
330 |
double disc; |
331 |
|
332 |
hl = hlim(y); |
333 |
if (x < -hl) |
334 |
xo = x+hl; |
335 |
else if (x > hl) |
336 |
xo = x-hl; |
337 |
else |
338 |
xo = 0; |
339 |
disc = 1. - (double)(xo*xo + y*y)/(sampdens*sampdens); |
340 |
if (disc <= FTINY) |
341 |
return(0.); |
342 |
return(1./(sampdens*sampdens*sqrt(disc))); |
343 |
} |
344 |
|
345 |
|
346 |
memerr(s) /* malloc failure */ |
347 |
char *s; |
348 |
{ |
349 |
fprintf(stderr, "%s: out of memory for %s\n", progname, s); |
350 |
exit(1); |
351 |
} |
352 |
|
353 |
|
354 |
printsources() /* print out glare sources */ |
355 |
{ |
356 |
register struct source *sp; |
357 |
|
358 |
printf("BEGIN glare source\n"); |
359 |
for (sp = donelist; sp != NULL; sp = sp->next) |
360 |
printf("\t%f %f %f\t%f\t%f\n", |
361 |
sp->dir[0], sp->dir[1], sp->dir[2], |
362 |
sp->dom, sp->brt); |
363 |
printf("END glare source\n"); |
364 |
} |
365 |
|
366 |
|
367 |
printillum() /* print out indirect illuminances */ |
368 |
{ |
369 |
register int i; |
370 |
|
371 |
printf("BEGIN indirect illuminance\n"); |
372 |
for (i = 0; i < nglardirs; i++) |
373 |
if (indirect[i].n > FTINY) |
374 |
printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta, |
375 |
PI * indirect[i].sum / indirect[i].n); |
376 |
printf("END indirect illuminance\n"); |
377 |
} |