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