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