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