ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 2.10
Committed: Mon Jul 21 22:30:19 2003 UTC (20 years, 8 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.9: +4 -4 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.10 static const char RCSid[] = "$Id: findglare.c,v 2.9 2003/02/22 02:07:30 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Find glare sources in a scene or image.
6     *
7     * Greg Ward March 1991
8     */
9    
10     #include "glare.h"
11    
12 greg 1.25 #define FEQ(a,b) ((a)-(b)<=FTINY&&(b)-(a)<=FTINY)
13 greg 1.1 #define VEQ(v1,v2) (FEQ((v1)[0],(v2)[0])&&FEQ((v1)[1],(v2)[1]) \
14     &&FEQ((v1)[2],(v2)[2]))
15    
16 gregl 2.8 char *rtargv[64] = {"rtrace", "-h-", "-ov", "-fff", "-ld-", "-i-", "-I-"};
17     int rtargc = 7;
18 greg 1.1
19 greg 1.6 VIEW ourview = STDVIEW; /* our view */
20 greg 1.1 VIEW pictview = STDVIEW; /* picture view */
21     VIEW leftview, rightview; /* leftmost and rightmost views */
22    
23     char *picture = NULL; /* picture file name */
24     char *octree = NULL; /* octree file name */
25    
26     int verbose = 0; /* verbose reporting */
27     char *progname; /* global argv[0] */
28    
29 greg 1.8 double threshold = 0.; /* glare threshold */
30    
31 greg 1.4 int sampdens = SAMPDENS; /* sample density */
32 greg 1.1 ANGLE glarang[180] = {AEND}; /* glare calculation angles */
33     int nglarangs = 0;
34 greg 1.2 double maxtheta; /* maximum angle (in radians) */
35 greg 1.1 int hsize; /* horizontal size */
36    
37     struct illum *indirect; /* array of indirect illuminances */
38    
39 greg 1.13 long npixinvw; /* number of pixels in view */
40     long npixmiss; /* number of pixels missed */
41 greg 1.1
42 greg 1.13
43 greg 1.1 main(argc, argv)
44     int argc;
45     char *argv[];
46     {
47 greg 1.16 int combine = 1;
48 greg 1.1 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 greg 2.7 /* expand arguments */
56 greg 2.9 while ((rval = expandarg(&argc, &argv, i)) > 0)
57     ;
58     if (rval < 0) {
59     fprintf(stderr, "%s: cannot expand '%s'",
60     argv[0], argv[i]);
61     exit(1);
62     }
63 greg 1.1 rval = getviewopt(&ourview, argc-i, argv+i);
64     if (rval >= 0) {
65     i += rval;
66     gotview++;
67     continue;
68     }
69     switch (argv[i][1]) {
70 greg 1.8 case 't':
71     threshold = atof(argv[++i]);
72     break;
73 greg 1.4 case 'r':
74     sampdens = atoi(argv[++i])/2;
75     break;
76 greg 1.1 case 'v':
77     if (argv[i][2] == '\0') {
78     verbose++;
79     break;
80     }
81     if (argv[i][2] != 'f')
82     goto userr;
83 greg 2.3 rval = viewfile(argv[++i], &ourview, NULL);
84 greg 1.1 if (rval < 0) {
85     fprintf(stderr,
86     "%s: cannot open view file \"%s\"\n",
87     progname, argv[i]);
88     exit(1);
89     } else if (rval == 0) {
90     fprintf(stderr,
91     "%s: bad view file \"%s\"\n",
92     progname, argv[i]);
93     exit(1);
94     } else
95     gotview++;
96     break;
97     case 'g':
98     if (argv[i][2] != 'a')
99     goto userr;
100     if (setscan(glarang, argv[++i]) < 0) {
101     fprintf(stderr, "%s: bad angle spec \"%s\"\n",
102     progname, argv[i]);
103     exit(1);
104     }
105     break;
106     case 'p':
107     picture = argv[++i];
108     break;
109 greg 1.16 case 'c':
110     combine = !combine;
111     break;
112 greg 1.1 case 'd':
113 gregl 2.8 rtargv[rtargc++] = argv[i];
114     if (argv[i][2] != 'v')
115     rtargv[rtargc++] = argv[++i];
116     break;
117     case 'l':
118     if (argv[i][2] == 'd')
119 greg 2.4 break;
120     /* FALL THROUGH */
121 greg 2.2 case 's':
122 greg 2.5 case 'P':
123 greg 1.1 rtargv[rtargc++] = argv[i];
124     rtargv[rtargc++] = argv[++i];
125 greg 2.4 break;
126     case 'w':
127     rtargv[rtargc++] = argv[i];
128 greg 1.1 break;
129     case 'a':
130     rtargv[rtargc++] = argv[i];
131     if (argv[i][2] == 'v') {
132     rtargv[rtargc++] = argv[++i];
133     rtargv[rtargc++] = argv[++i];
134     }
135     rtargv[rtargc++] = argv[++i];
136     break;
137     default:
138     goto userr;
139     }
140     }
141     /* get octree */
142     if (i < argc-1)
143     goto userr;
144     if (i == argc-1) {
145     rtargv[rtargc++] = octree = argv[i];
146     rtargv[rtargc] = NULL;
147     }
148     /* get view */
149     if (picture != NULL) {
150 greg 2.3 rval = viewfile(picture, &pictview, NULL);
151 greg 1.1 if (rval < 0) {
152     fprintf(stderr, "%s: cannot open picture file \"%s\"\n",
153     progname, picture);
154     exit(1);
155     } else if (rval == 0) {
156     fprintf(stderr,
157     "%s: cannot get view from picture \"%s\"\n",
158     progname, picture);
159     exit(1);
160     }
161     if (pictview.type == VT_PAR) {
162     fprintf(stderr, "%s: %s: cannot use parallel view\n",
163     progname, picture);
164     exit(1);
165     }
166     if ((err = setview(&pictview)) != NULL) {
167     fprintf(stderr, "%s: %s\n", picture, err);
168     exit(1);
169     }
170     }
171     if (!gotview) {
172     if (picture == NULL) {
173     fprintf(stderr, "%s: must have view or picture\n",
174     progname);
175     exit(1);
176     }
177 schorsch 2.10 ourview = pictview;
178 greg 1.1 } else if (picture != NULL && !VEQ(ourview.vp, pictview.vp)) {
179     fprintf(stderr, "%s: picture must have same viewpoint\n",
180     progname);
181     exit(1);
182     }
183     ourview.type = VT_HEM;
184     ourview.horiz = ourview.vert = 180.0;
185     ourview.hoff = ourview.voff = 0.0;
186     fvsum(ourview.vdir, ourview.vdir, ourview.vup,
187     -DOT(ourview.vdir,ourview.vup));
188     if ((err = setview(&ourview)) != NULL) {
189     fprintf(stderr, "%s: %s\n", progname, err);
190     exit(1);
191     }
192     if (octree == NULL && picture == NULL) {
193     fprintf(stderr,
194     "%s: must specify at least one of picture or octree\n",
195     progname);
196     exit(1);
197     }
198     init(); /* initialize program */
199 greg 1.8 if (threshold <= FTINY)
200     comp_thresh(); /* compute glare threshold */
201 greg 1.1 analyze(); /* analyze view */
202 greg 1.16 if (combine)
203     absorb_specks(); /* eliminate tiny sources */
204 greg 1.1 cleanup(); /* tidy up */
205     /* print header */
206 greg 2.7 newheader("RADIANCE", stdout);
207 greg 1.1 printargs(argc, argv, stdout);
208     fputs(VIEWSTR, stdout);
209     fprintview(&ourview, stdout);
210 greg 1.18 printf("\n");
211 greg 1.21 fputformat("ascii", stdout);
212 greg 1.18 printf("\n");
213 greg 1.1 printsources(); /* print glare sources */
214     printillum(); /* print illuminances */
215     exit(0);
216     userr:
217     fprintf(stderr,
218     "Usage: %s [view options][-ga angles][-p picture][[rtrace options] octree]\n",
219     progname);
220     exit(1);
221     }
222    
223    
224 greg 1.23 int
225     angcmp(ap1, ap2) /* compare two angles */
226     ANGLE *ap1, *ap2;
227     {
228     register int a1, a2;
229    
230     a1 = *ap1;
231     a2 = *ap2;
232     if (a1 == a2) {
233     fprintf(stderr, "%s: duplicate glare angle (%d)\n",
234     progname, a1);
235     exit(1);
236     }
237     return(a1-a2);
238     }
239    
240    
241 greg 1.1 init() /* initialize global variables */
242     {
243     double d;
244     register int i;
245    
246     if (verbose)
247     fprintf(stderr, "%s: initializing data structures...\n",
248     progname);
249     /* set direction vectors */
250     for (i = 0; glarang[i] != AEND; i++)
251     ;
252 greg 1.23 qsort(glarang, i, sizeof(ANGLE), angcmp);
253     if (i > 0 && (glarang[0] <= 0 || glarang[i-1] > 180)) {
254     fprintf(stderr, "%s: glare angles must be between 1 and 180\n",
255 greg 1.1 progname);
256     exit(1);
257     }
258     nglarangs = i;
259     /* nglardirs = 2*nglarangs + 1; */
260 greg 1.5 /* vsize = sampdens - 1; */
261 greg 1.2 if (nglarangs > 0)
262     maxtheta = (PI/180.)*glarang[nglarangs-1];
263     else
264     maxtheta = 0.0;
265 greg 1.19 hsize = hlim(0) + sampdens - 1;
266 greg 1.4 if (hsize > (int)(PI*sampdens))
267     hsize = PI*sampdens;
268 greg 1.1 indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum));
269     if (indirect == NULL)
270     memerr("indirect illuminances");
271 greg 1.13 npixinvw = npixmiss = 0L;
272 schorsch 2.10 leftview = ourview;
273     rightview = ourview;
274 greg 1.3 spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta);
275     spinvector(rightview.vdir, ourview.vdir, ourview.vup, -maxtheta);
276 greg 1.1 setview(&leftview);
277     setview(&rightview);
278     indirect[nglarangs].lcos =
279     indirect[nglarangs].rcos = cos(maxtheta);
280 greg 1.9 indirect[nglarangs].rsin =
281     -(indirect[nglarangs].lsin = sin(maxtheta));
282 greg 1.1 indirect[nglarangs].theta = 0.0;
283     for (i = 0; i < nglarangs; i++) {
284     d = (glarang[nglarangs-1] - glarang[i])*(PI/180.);
285     indirect[nglarangs-i-1].lcos =
286     indirect[nglarangs+i+1].rcos = cos(d);
287 greg 1.9 indirect[nglarangs+i+1].rsin =
288     -(indirect[nglarangs-i-1].lsin = sin(d));
289 greg 1.1 d = (glarang[nglarangs-1] + glarang[i])*(PI/180.);
290     indirect[nglarangs-i-1].rcos =
291     indirect[nglarangs+i+1].lcos = cos(d);
292 greg 1.9 indirect[nglarangs-i-1].rsin =
293     -(indirect[nglarangs+i+1].lsin = sin(d));
294     indirect[nglarangs-i-1].theta = (PI/180.)*glarang[i];
295     indirect[nglarangs+i+1].theta = -(PI/180.)*glarang[i];
296 greg 1.1 }
297     /* open picture */
298     if (picture != NULL) {
299     if (verbose)
300     fprintf(stderr, "%s: opening picture file \"%s\"...\n",
301     progname, picture);
302     open_pict(picture);
303     }
304     /* start rtrace */
305     if (octree != NULL) {
306     if (verbose) {
307     fprintf(stderr,
308     "%s: starting luminance calculation...\n\t",
309     progname);
310     printargs(rtargc, rtargv, stderr);
311     }
312     fork_rtrace(rtargv);
313     }
314     }
315    
316    
317     cleanup() /* close files, wait for children */
318     {
319     if (verbose)
320 greg 1.17 fprintf(stderr, "%s: cleaning up... \n", progname);
321 greg 1.1 if (picture != NULL)
322     close_pict();
323     if (octree != NULL)
324     done_rtrace();
325 greg 1.13 if (npixinvw < 100*npixmiss)
326 greg 2.3 fprintf(stderr, "%s: warning -- missing %d%% of samples\n",
327     progname, (int)(100L*npixmiss/npixinvw));
328 greg 1.1 }
329    
330    
331 greg 1.19 compdir(vd, x, y) /* compute direction for x,y */
332 greg 1.1 FVECT vd;
333     int x, y;
334     {
335 greg 1.19 int hl;
336 greg 1.1 FVECT org; /* dummy variable */
337    
338 greg 1.19 hl = hlim(y);
339 greg 1.22 if (x <= -hl) { /* left region */
340     if (x <= -hl-sampdens)
341     return(-1);
342 greg 1.1 return(viewray(org, vd, &leftview,
343 greg 1.19 (double)(x+hl)/(2*sampdens)+.5,
344 greg 1.13 (double)y/(2*sampdens)+.5));
345 greg 1.22 }
346     if (x >= hl) { /* right region */
347     if (x >= hl+sampdens)
348     return(-1);
349 greg 1.1 return(viewray(org, vd, &rightview,
350 greg 1.19 (double)(x-hl)/(2*sampdens)+.5,
351 greg 1.13 (double)y/(2*sampdens)+.5));
352 greg 1.22 }
353 greg 1.14 /* central region */
354 greg 1.13 if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0)
355 greg 1.1 return(-1);
356 greg 1.19 spinvector(vd, vd, ourview.vup, h_theta(x,y));
357 greg 1.1 return(0);
358 greg 1.17 }
359    
360    
361 greg 1.19 double
362     pixsize(x, y) /* return the solid angle of pixel at (x,y) */
363     int x, y;
364 greg 1.17 {
365 greg 1.19 register int hl, xo;
366     double disc;
367    
368     hl = hlim(y);
369     if (x < -hl)
370     xo = x+hl;
371     else if (x > hl)
372     xo = x-hl;
373     else
374     xo = 0;
375 greg 2.6 disc = 1. - (double)((long)xo*xo + (long)y*y)/((long)sampdens*sampdens);
376     if (disc <= FTINY*FTINY)
377 greg 1.19 return(0.);
378     return(1./(sampdens*sampdens*sqrt(disc)));
379 greg 1.1 }
380    
381    
382     memerr(s) /* malloc failure */
383     char *s;
384     {
385 greg 1.5 fprintf(stderr, "%s: out of memory for %s\n", progname, s);
386 greg 1.1 exit(1);
387     }
388    
389    
390     printsources() /* print out glare sources */
391     {
392     register struct source *sp;
393    
394     printf("BEGIN glare source\n");
395     for (sp = donelist; sp != NULL; sp = sp->next)
396     printf("\t%f %f %f\t%f\t%f\n",
397     sp->dir[0], sp->dir[1], sp->dir[2],
398     sp->dom, sp->brt);
399     printf("END glare source\n");
400     }
401    
402    
403     printillum() /* print out indirect illuminances */
404     {
405     register int i;
406    
407     printf("BEGIN indirect illuminance\n");
408     for (i = 0; i < nglardirs; i++)
409 greg 1.12 if (indirect[i].n > FTINY)
410     printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta,
411     PI * indirect[i].sum / indirect[i].n);
412 greg 1.2 printf("END indirect illuminance\n");
413 greg 1.1 }