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