ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 2.14
Committed: Tue Jul 29 21:36:08 2014 UTC (9 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R2P1, rad5R3
Changes since 2.13: +12 -1 lines
Log Message:
Added missing support for rtrace -n, -bv, and -m* options

File Contents

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