ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 2.5
Committed: Wed Feb 3 17:55:34 1993 UTC (31 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +1 -0 lines
Log Message:
added -P and -PP option for rtrace

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