ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 1.16
Committed: Wed Apr 10 15:59:07 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.15: +6 -0 lines
Log Message:
fixed small bug in absorb_specks() and added -c option

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