ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
(Generate patch)

Comparing ray/src/util/findglare.c (file contents):
Revision 1.6 by greg, Tue Mar 19 11:19:23 1991 UTC vs.
Revision 2.14 by greg, Tue Jul 29 21:36:08 2014 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Find glare sources in a scene or image.
6   *
# Line 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9  
10   #include "glare.h"
11  
12 < #define FEQ(a,b)        ((a)-(b)<=FTINY&&(a)-(b)<=FTINY)
12 > #define FEQ(a,b)        ((a)-(b)<=FTINY&&(b)-(a)<=FTINY)
13   #define VEQ(v1,v2)      (FEQ((v1)[0],(v2)[0])&&FEQ((v1)[1],(v2)[1]) \
14                                  &&FEQ((v1)[2],(v2)[2]))
15  
16 < char    *rtargv[32] = {"rtrace", "-h", "-ov", "-fff"};
17 < int     rtargc = 4;
16 > char    *rtargv[64] = {"rtrace", "-h-", "-ov", "-fff", "-ld-", "-i-", "-I-"};
17 > int     rtargc = 7;
18  
19   VIEW    ourview = STDVIEW;              /* our view */
20   VIEW    pictview = STDVIEW;             /* picture view */
# Line 29 | Line 26 | char   *octree = NULL;                 /* octree file name */
26   int     verbose = 0;                    /* verbose reporting */
27   char    *progname;                      /* global argv[0] */
28  
29 + double  threshold = 0.;                 /* glare threshold */
30 +
31   int     sampdens = SAMPDENS;            /* sample density */
32   ANGLE   glarang[180] = {AEND};          /* glare calculation angles */
33   int     nglarangs = 0;
34   double  maxtheta;                       /* maximum angle (in radians) */
35   int     hsize;                          /* horizontal size */
37 int     hlim;                           /* central limit of horizontal */
36  
37   struct illum    *indirect;              /* array of indirect illuminances */
38  
39 + long    npixinvw;                       /* number of pixels in view */
40 + long    npixmiss;                       /* number of pixels missed */
41  
42 < main(argc, argv)
43 < int     argc;
44 < char    *argv[];
42 > static int angcmp(const void *ap1, const void *ap2);
43 > static void init(void);
44 > static void cleanup(void);
45 > static void printsources(void);
46 > static void printillum(void);
47 >
48 >
49 >
50 > int
51 > main(
52 >        int     argc,
53 >        char    *argv[]
54 > )
55   {
56 +        int     combine = 1;
57          int     gotview = 0;
58          int     rval, i;
59          char    *err;
# Line 50 | Line 61 | char   *argv[];
61          progname = argv[0];
62                                          /* process options */
63          for (i = 1; i < argc && argv[i][0] == '-'; i++) {
64 +                                                /* expand arguments */
65 +                while ((rval = expandarg(&argc, &argv, i)) > 0)
66 +                        ;
67 +                if (rval < 0) {
68 +                        fprintf(stderr, "%s: cannot expand '%s'\n",
69 +                                        argv[0], argv[i]);
70 +                        exit(1);
71 +                }
72                  rval = getviewopt(&ourview, argc-i, argv+i);
73                  if (rval >= 0) {
74                          i += rval;
# Line 57 | Line 76 | char   *argv[];
76                          continue;
77                  }
78                  switch (argv[i][1]) {
79 +                case 't':
80 +                        threshold = atof(argv[++i]);
81 +                        break;
82                  case 'r':
83                          sampdens = atoi(argv[++i])/2;
84                          break;
# Line 67 | Line 89 | char   *argv[];
89                          }
90                          if (argv[i][2] != 'f')
91                                  goto userr;
92 <                        rval = viewfile(argv[++i], &ourview, 0, 0);
92 >                        rval = viewfile(argv[++i], &ourview, NULL);
93                          if (rval < 0) {
94                                  fprintf(stderr,
95                                  "%s: cannot open view file \"%s\"\n",
# Line 93 | Line 115 | char   *argv[];
115                  case 'p':
116                          picture = argv[++i];
117                          break;
118 +                case 'c':
119 +                        combine = !combine;
120 +                        break;
121                  case 'd':
122 +                        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 +                                break;
129 +                        /* FALL THROUGH */
130 +                case 's':
131 +                case 'P':
132 +                case 'n':
133                          rtargv[rtargc++] = argv[i];
134                          rtargv[rtargc++] = argv[++i];
135                          break;
136 +                case 'w':
137 +                case 'u':
138 +                case 'b':
139 +                        rtargv[rtargc++] = argv[i];
140 +                        break;
141                  case 'a':
142                          rtargv[rtargc++] = argv[i];
143                          if (argv[i][2] == 'v') {
# Line 106 | Line 146 | char   *argv[];
146                          }
147                          rtargv[rtargc++] = argv[++i];
148                          break;
149 +                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                  default:
158                          goto userr;
159                  }
# Line 119 | Line 167 | char   *argv[];
167          }
168                                                  /* get view */
169          if (picture != NULL) {
170 <                rval = viewfile(picture, &pictview, 0, 0);
170 >                rval = viewfile(picture, &pictview, NULL);
171                  if (rval < 0) {
172                          fprintf(stderr, "%s: cannot open picture file \"%s\"\n",
173                                          progname, picture);
# Line 146 | Line 194 | char   *argv[];
194                                          progname);
195                          exit(1);
196                  }
197 <                copystruct(&ourview, &pictview);
197 >                ourview = pictview;
198          } else if (picture != NULL && !VEQ(ourview.vp, pictview.vp)) {
199                  fprintf(stderr, "%s: picture must have same viewpoint\n",
200                                  progname);
# Line 168 | Line 216 | char   *argv[];
216                  exit(1);
217          }
218          init();                                 /* initialize program */
219 <        comp_thresh();                          /* compute glare threshold */
219 >        if (threshold <= FTINY)
220 >                comp_thresh();                  /* compute glare threshold */
221          analyze();                              /* analyze view */
222 +        if (combine)
223 +                absorb_specks();                /* eliminate tiny sources */
224          cleanup();                              /* tidy up */
225                                                  /* print header */
226 +        newheader("RADIANCE", stdout);
227          printargs(argc, argv, stdout);
228          fputs(VIEWSTR, stdout);
229          fprintview(&ourview, stdout);
230 <        printf("\n\n");
230 >        printf("\n");
231 >        fputformat("ascii", stdout);
232 >        printf("\n");
233          printsources();                         /* print glare sources */
234          printillum();                           /* print illuminances */
235          exit(0);
# Line 187 | Line 241 | userr:
241   }
242  
243  
244 < init()                          /* initialize global variables */
244 > static int
245 > angcmp(         /* compare two angles */
246 >        const void      *ap1,
247 >        const void      *ap2
248 > )
249   {
250 +        register int    a1, a2;
251 +
252 +        a1 = *(ANGLE *)ap1;
253 +        a2 = *(ANGLE *)ap2;
254 +        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 + static void
264 + init(void)                              /* initialize global variables */
265 + {
266          double  d;
267          register int    i;
268  
# Line 198 | Line 272 | init()                         /* initialize global variables */
272                                                  /* set direction vectors */
273          for (i = 0; glarang[i] != AEND; i++)
274                  ;
275 <        if (i > 0 && glarang[0] <= 0 || glarang[i-1] >= 180) {
276 <                fprintf(stderr, "%s: glare angles must be between 1 and 179\n",
275 >        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                                  progname);
279                  exit(1);
280          }
# Line 210 | Line 285 | init()                         /* initialize global variables */
285                  maxtheta = (PI/180.)*glarang[nglarangs-1];
286          else
287                  maxtheta = 0.0;
288 <        hlim = sampdens*maxtheta;
214 <        hsize = hlim + sampdens - 1;
288 >        hsize = hlim(0) + sampdens - 1;
289          if (hsize > (int)(PI*sampdens))
290                  hsize = PI*sampdens;
291          indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum));
292          if (indirect == NULL)
293                  memerr("indirect illuminances");
294 <        copystruct(&leftview, &ourview);
295 <        copystruct(&rightview, &ourview);
294 >        npixinvw = npixmiss = 0L;
295 >        leftview = ourview;
296 >        rightview = ourview;
297          spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta);
298          spinvector(rightview.vdir, ourview.vdir, ourview.vup, -maxtheta);
299          setview(&leftview);
300          setview(&rightview);
301          indirect[nglarangs].lcos =
302          indirect[nglarangs].rcos = cos(maxtheta);
303 <        indirect[nglarangs].lsin =
304 <        -(indirect[nglarangs].rsin = sin(maxtheta));
303 >        indirect[nglarangs].rsin =
304 >        -(indirect[nglarangs].lsin = sin(maxtheta));
305          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 <                indirect[nglarangs-i-1].lsin =
311 <                -(indirect[nglarangs+i+1].rsin = sin(d));
310 >                indirect[nglarangs+i+1].rsin =
311 >                -(indirect[nglarangs-i-1].lsin = sin(d));
312                  d = (glarang[nglarangs-1] + glarang[i])*(PI/180.);
313                  indirect[nglarangs-i-1].rcos =
314                  indirect[nglarangs+i+1].lcos = cos(d);
315 <                indirect[nglarangs+i+1].lsin =
316 <                -(indirect[nglarangs-i-1].rsin = sin(d));
317 <                indirect[nglarangs-i-1].theta = -(PI/180.)*glarang[i];
318 <                indirect[nglarangs+i+1].theta = (PI/180.)*glarang[i];
315 >                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          }
320                                                  /* open picture */
321          if (picture != NULL) {
# Line 262 | Line 337 | init()                         /* initialize global variables */
337   }
338  
339  
340 < cleanup()                               /* close files, wait for children */
340 > static void
341 > cleanup(void)                           /* close files, wait for children */
342   {
343          if (verbose)
344 <                fprintf(stderr, "%s: cleaning up...\n", progname);
344 >                fprintf(stderr, "%s: cleaning up...        \n", progname);
345          if (picture != NULL)
346                  close_pict();
347          if (octree != NULL)
348                  done_rtrace();
349 +        if (npixinvw < 100*npixmiss)
350 +                fprintf(stderr, "%s: warning -- missing %d%% of samples\n",
351 +                                progname, (int)(100L*npixmiss/npixinvw));
352   }
353  
354  
355 < compdir(vd, x, y)                       /* compute direction for x,y */
356 < FVECT   vd;
357 < int     x, y;
355 > extern int
356 > compdir(                        /* compute direction for x,y */
357 >        FVECT   vd,
358 >        int     x,
359 >        int     y
360 > )
361   {
362 +        int     hl;
363          FVECT   org;                    /* dummy variable */
364  
365 <        if (x <= -hlim)                 /* left region */
365 >        hl = hlim(y);
366 >        if (x <= -hl) {                 /* left region */
367 >                if (x <= -hl-sampdens)
368 >                        return(-1);
369                  return(viewray(org, vd, &leftview,
370 <                                (x+hlim)/(2.*sampdens)+.5,
371 <                                y/(2.*sampdens)+.5));
372 <        if (x >= hlim)                  /* right region */
370 >                                (double)(x+hl)/(2*sampdens)+.5,
371 >                                (double)y/(2*sampdens)+.5));
372 >        }
373 >        if (x >= hl) {                  /* right region */
374 >                if (x >= hl+sampdens)
375 >                        return(-1);
376                  return(viewray(org, vd, &rightview,
377 <                                (x-hlim)/(2.*sampdens)+.5,
378 <                                y/(2.*sampdens)+.5));
379 <                                                /* central region */
380 <        if (viewray(org, vd, &ourview, .5, y/(2.*sampdens)+.5) < 0)
377 >                                (double)(x-hl)/(2*sampdens)+.5,
378 >                                (double)y/(2*sampdens)+.5));
379 >        }
380 >                                        /* central region */
381 >        if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0)
382                  return(-1);
383 <        spinvector(vd, vd, ourview.vup, h_theta(x));
383 >        spinvector(vd, vd, ourview.vup, h_theta(x,y));
384          return(0);
385   }
386  
387  
388 < spinvector(vres, vorig, vnorm, theta)   /* rotate vector around normal */
389 < FVECT  vres, vorig, vnorm;
390 < double  theta;
388 > extern double
389 > pixsize(                /* return the solid angle of pixel at (x,y) */
390 >        int     x,
391 >        int     y
392 > )
393   {
394 <        extern double  sin(), cos();
395 <        double  sint, cost, dotp;
396 <        FVECT  vperp;
397 <        register int  i;
398 <        
399 <        sint = sin(theta);
400 <        cost = cos(theta);
401 <        dotp = DOT(vorig, vnorm);
402 <        fcross(vperp, vnorm, vorig);
403 <        for (i = 0; i < 3; i++)
404 <                vres[i] = vnorm[i]*dotp*(1.-cost) +
405 <                                vorig[i]*cost + vperp[i]*sint;
394 >        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 >        disc = 1. - (double)((long)xo*xo + (long)y*y)/((long)sampdens*sampdens);
405 >        if (disc <= FTINY*FTINY)
406 >                return(0.);
407 >        return(1./(sampdens*sampdens*sqrt(disc)));
408   }
409  
410  
411 < memerr(s)                       /* malloc failure */
412 < char    *s;
411 > extern void
412 > memerr(                 /* malloc failure */
413 >        char    *s
414 > )
415   {
416          fprintf(stderr, "%s: out of memory for %s\n", progname, s);
417          exit(1);
418   }
419  
420  
421 < printsources()                  /* print out glare sources */
421 > static void
422 > printsources(void)                      /* print out glare sources */
423   {
424          register struct source  *sp;
425  
# Line 335 | Line 432 | printsources()                 /* print out glare sources */
432   }
433  
434  
435 < printillum()                    /* print out indirect illuminances */
435 > static void
436 > printillum(void)                        /* print out indirect illuminances */
437   {
438          register int    i;
439  
440          printf("BEGIN indirect illuminance\n");
441          for (i = 0; i < nglardirs; i++)
442 <                printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta,
443 <                                PI * indirect[i].sum / (double)indirect[i].n);
442 >                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          printf("END indirect illuminance\n");
446   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines