ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 1.2
Committed: Mon Mar 18 14:32:12 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +7 -3 lines
Log Message:
bug fixes and enhancements

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