ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 2.4
Committed: Mon Dec 7 09:13:58 1992 UTC (31 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +8 -0 lines
Log Message:
added provisions for -dv and -w rtrace switches

File Contents

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