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

Comparing ray/src/util/glarendx.c (file contents):
Revision 1.1 by greg, Tue Apr 16 16:40:22 1991 UTC vs.
Revision 2.8 by schorsch, Mon Jun 30 14:59:13 2003 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 < * Compute Glare Index given by program name:
5 > * Compute Glare Index given by program name or -t option:
6   *
7 < *      gnuth_dgr -     Gnuth discomfort glare rating
8 < *      gnuth_vcp -     Gnuth visual comfort probability
7 > *      dgi -           Daylight Glare Index
8 > *      brs_gi -        Building Research Station Glare Index (Petherbridge
9 > *                                                             & Hopkinson)
10 > *      ugr -           Unified Glare Rating System (Fischer)
11 > *      guth_dgr -      Guth discomfort glare rating
12 > *      guth_vcp -      Guth visual comfort probability
13 > *      cie_cgi -       CIE Glare Index (1983, due to Einhorn)
14 > *      vert_dir -      Direct vertical illuminance
15 > *      vert_ind -      Indirect vertical illuminance (from input)
16 > *      vert_ill -      Total vertical illuminance
17   *
18   *              12 April 1991   Greg Ward       EPFL
19 + *              19 April 1993   R. Compagnon    EPFL (added dgi, brs_gi, ugr)
20   */
21 +
22 + #include <string.h>
23  
24   #include "standard.h"
25   #include "view.h"
26 <
27 < extern double   erfc();
20 <
26 >
27 >
28   double  posindex();
29 < int     headline();
30 <
31 < double  direct(), gnuth_dgr(), gnuth_vcp();
32 <
29 >
30 > double  direct(), guth_dgr(), guth_vcp(), cie_cgi(),
31 >        indirect(), total(), dgi(), brs_gi(), ugr();
32 >
33   struct named_func {
34          char    *name;
35          double  (*func)();
36 +        char    *descrip;
37   } all_funcs[] = {
38 <        {"direct", direct},
39 <        {"gnuth_dgr", gnuth_dgr},
40 <        {"gnuth_vcp", gnuth_vcp},
38 >        {"dgi", dgi, "Daylight Glare Index"},
39 >        {"brs_gi", brs_gi, "BRS Glare Index"},
40 >        {"ugr", ugr, "Unified Glare Rating"},
41 >        {"guth_vcp", guth_vcp, "Guth Visual Comfort Probability"},
42 >        {"cie_cgi", cie_cgi, "CIE Glare Index (Einhorn)"},
43 >        {"guth_dgr", guth_dgr, "Guth Disability Glare Rating"},
44 >        {"vert_dir", direct, "Direct Vertical Illuminance"},
45 >        {"vert_ill", total, "Total Vertical Illuminance"},
46 >        {"vert_ind", indirect, "Indirect Vertical Illuminance"},
47          {NULL}
48   };
49 <
49 >
50   struct glare_src {
51          FVECT   dir;            /* source direction */
52          double  dom;            /* solid angle */
53          double  lum;            /* average luminance */
54          struct glare_src        *next;
55   } *all_srcs = NULL;
56 <
56 >
57   struct glare_dir {
58          double  ang;            /* angle (in radians) */
59          double  indirect;       /* indirect illuminance */
60          struct glare_dir        *next;
61   } *all_dirs = NULL;
62 <
62 >
63   #define newp(type)      (type *)malloc(sizeof(type))
64 <
64 >
65   char    *progname;
66   int     print_header = 1;
67 <
67 >
68   VIEW    midview = STDVIEW;
69 <
70 <
69 >
70 > int     wrongformat = 0;
71 >
72 >
73   main(argc, argv)
74   int     argc;
75   char    *argv[];
76   {
61        extern char     *rindex();
77          struct named_func       *funp;
78          char    *progtail;
79          int     i;
80                                          /* get program name */
81          progname = argv[0];
82 <        progtail = rindex(progname, '/');       /* final component */
82 >        progtail = strrchr(progname, '/');      /* final component */
83          if (progtail == NULL)
84                  progtail = progname;
85          else
# Line 88 | Line 103 | char   *argv[];
103                          perror(argv[i]);
104                          exit(1);
105                  }
106 <                                        /* read header */
92 <        getheader(stdin, headline);
93 <        if (print_header) {             /* add to header */
94 <                printargs(i, argv, stdout);
95 <                putchar('\n');
96 <        }
97 <                                        /* set view */
98 <        if (setview(&midview) != NULL) {
99 <                fprintf(stderr, "%s: bad view information in input\n");
100 <                exit(1);
101 <        }
102 <                                        /* get findglare data */
103 <        read_input();
104 <                                        /* find calculation */
106 >                                        /* find and run calculation */
107          for (funp = all_funcs; funp->name != NULL; funp++)
108                  if (!strcmp(funp->name, progtail)) {
109 +                        init();
110 +                        read_input();
111 +                        if (print_header) {
112 +                                printargs(i, argv, stdout);
113 +                                putchar('\n');
114 +                        }
115                          print_values(funp->func);
116                          exit(0);                /* we're done */
117                  }
118                                          /* invalid function */
111        fprintf(stderr, "%s: unknown function!\n", progtail);
112        exit(1);
119   userr:
120 <        fprintf(stderr, "Usage: %s [-t type][-h] [input]\n", progname);
120 >        fprintf(stderr, "Usage: %s -t type [-h] [input]\n", progname);
121 >        fprintf(stderr, "\twhere 'type' is one of the following:\n");
122 >        for (funp = all_funcs; funp->name != NULL; funp++)
123 >                fprintf(stderr, "\t%12s\t%s\n", funp->name, funp->descrip);
124          exit(1);
125   }
126 <
127 <
126 >
127 >
128 > int
129   headline(s)                     /* get line from header */
130   char    *s;
131   {
132 +        char    fmt[32];
133 +
134          if (print_header)               /* copy to output */
135                  fputs(s, stdout);
136 <        if (!strncmp(s, VIEWSTR, VIEWSTRL))
137 <                sscanview(&midview, s+VIEWSTRL);
136 >        if (isview(s))
137 >                sscanview(&midview, s);
138 >        else if (isformat(s)) {
139 >                formatval(fmt, s);
140 >                wrongformat = strcmp(fmt, "ascii");
141 >        }
142 >        return(0);
143   }
144 <
145 <
144 >
145 >
146 > init()                          /* initialize calculation */
147 > {
148 >                                        /* read header */
149 >        getheader(stdin, headline, NULL);
150 >        if (wrongformat) {
151 >                fprintf(stderr, "%s: bad input format\n", progname);
152 >                exit(1);
153 >        }
154 >                                        /* set view */
155 >        if (setview(&midview) != NULL) {
156 >                fprintf(stderr, "%s: bad view information in input\n",
157 >                                progname);
158 >                exit(1);
159 >        }
160 > }
161 >
162 >
163   read_input()                    /* read glare sources from stdin */
164   {
165   #define S_SEARCH        0
# Line 135 | Line 169 | read_input()                   /* read glare sources from stdin */
169          char    buf[128];
170          register struct glare_src       *gs;
171          register struct glare_dir       *gd;
172 <
172 >
173          while (fgets(buf, sizeof(buf), stdin) != NULL)
174                  switch (state) {
175                  case S_SEARCH:
# Line 155 | Line 189 | read_input()                   /* read glare sources from stdin */
189                                          &gs->dir[0], &gs->dir[1], &gs->dir[2],
190                                          &gs->dom, &gs->lum) != 5)
191                                  goto readerr;
192 +                        normalize(gs->dir);
193                          gs->next = all_srcs;
194                          all_srcs = gs;
195                          break;
# Line 184 | Line 219 | readerr:
219   #undef S_SOURCE
220   #undef S_DIREC
221   }
222 <
223 <
222 >
223 >
224   print_values(funp)              /* print out calculations */
225   double  (*funp)();
226   {
227          register struct glare_dir       *gd;
228 <
228 >
229          for (gd = all_dirs; gd != NULL; gd = gd->next)
230                  printf("%f\t%f\n", gd->ang*(180.0/PI), (*funp)(gd));
231   }
232 <
233 <
232 >
233 >
234   double
235 < direct(gd)                      /* compute direct illuminance */
235 > direct(gd)                      /* compute direct vertical illuminance */
236   struct glare_dir        *gd;
237   {
238          FVECT   mydir;
239          double  d, dval;
240          register struct glare_src       *gs;
241 <
241 >
242          spinvector(mydir, midview.vdir, midview.vup, gd->ang);
243          dval = 0.0;
244          for (gs = all_srcs; gs != NULL; gs = gs->next) {
# Line 213 | Line 248 | struct glare_dir       *gd;
248          }
249          return(dval);
250   }
251 <
252 <
251 >
252 >
253 > double
254 > indirect(gd)                    /* return indirect vertical illuminance */
255 > struct glare_dir        *gd;
256 > {
257 >        return(gd->indirect);
258 > }
259 >
260 >
261 > double
262 > total(gd)                       /* return total vertical illuminance */
263 > struct glare_dir        *gd;
264 > {
265 >        return(direct(gd)+gd->indirect);
266 > }
267 >
268 >
269   /*
270   * posindex -   compute glare position index from:
271   *
# Line 225 | Line 276 | struct glare_dir       *gd;
276   * All vectors are assumed to be normalized.
277   * This function is an implementation of the method proposed by
278   * Robert Levin in his 1975 JIES article.
279 + * This calculation presumes the view direction and up vectors perpendicular.
280   * We return a value less than zero for improper positions.
281   */
282 <
282 >
283   double
284   posindex(sd, vd, vu)                    /* compute position index */
285   FVECT   sd, vd, vu;
286   {
287          double  sigma, tau;
288          double  d;
289 <
289 >
290          d = DOT(sd,vd);
291          if (d <= 0.0)
292                  return(-1.0);
293 +        if (d >= 1.0)
294 +                return(1.0);
295          sigma = acos(d) * (180./PI);
296 <        tau = acos(DOT(sd,vu)/sqrt(1.0-d*d)) * (180./PI);
296 >        d = fabs(DOT(sd,vu)/sqrt(1.0-d*d));
297 >        if (d >= 1.0)
298 >                tau = 0.0;
299 >        else
300 >                tau = acos(d) * (180./PI);
301          return( exp( sigma*( (35.2 - tau*.31889 - 1.22*exp(-.22222*tau))*1e-3
302                          + sigma*(21. + tau*(.26667 + tau*-.002963))*1e-5 )
303                  ) );
304   }
305 +
306 +
307 + double
308 + dgi(gd)         /* compute Daylight Glare Index */
309 + struct glare_dir        *gd;
310 + {
311 +        register struct glare_src       *gs;
312 +        FVECT   mydir,testdir[7],vhor;
313 +        double  r,omega,p[7],sum;
314 +        int     i,n;
315 +
316 +        spinvector(mydir, midview.vdir, midview.vup, gd->ang);
317 +        sum = 0.0; n = 0;
318 +        for (gs = all_srcs; gs != NULL; gs = gs->next) {
319  
320 +                /* compute 1/p^2 weighted solid angle of the source */
321 +                r = sqrt(1 - pow(1.-gs->dom/2./PI,2.));
322 +                fcross(vhor,gs->dir,midview.vup);
323 +                normalize(vhor);
324 +                VCOPY(testdir[0],gs->dir);
325 +                fvsum(testdir[1],gs->dir,vhor,r);
326 +                fvsum(testdir[2],gs->dir,vhor,0.5*r);
327 +                fvsum(testdir[5],testdir[2],midview.vup,-0.866*r);
328 +                fvsum(testdir[2],testdir[2],midview.vup,0.866*r);
329 +                fvsum(testdir[3],gs->dir,vhor,-r);
330 +                fvsum(testdir[4],gs->dir,vhor,-0.5*r);
331 +                fvsum(testdir[6],testdir[4],midview.vup,0.866*r);
332 +                fvsum(testdir[4],testdir[4],midview.vup,-0.866*r);
333 +                for (i = 0; i < 7; i++) {
334 +                        normalize(testdir[i]);
335 +                        p[i] = pow(posindex(testdir[i],mydir,midview.vup),-2.0);
336 +                        if (p[i] <= FTINY) p[i] = 0.0;
337 +                }
338 +                r = 1-gs->dom/2./PI;
339 +                omega = gs->dom*p[0];
340 +                omega += (r*PI*(1+1/r/r)-2*PI)*(-p[0]+(p[1]+p[2])*0.5);
341 +                omega += (2*PI-r*PI*(1+1/r/r))*(-p[0]-0.1667*(p[1]+p[3])
342 +                          +0.3334*(p[2]+p[4]+p[5]+p[6]));
343  
344 +                sum += pow(gs->lum,1.6) * pow(omega,0.8) /
345 +                       (gd->indirect/PI + 0.07*sqrt(gs->dom)*gs->lum);
346 +                n++;
347 +        }
348 +        if (n == 0)
349 +                return(0.0);
350 +        return( 10*log10(0.478*sum) );
351 + }
352 +
353 +
354   double
355 < gnuth_dgr(gd)           /* compute Gnuth discomfort glare rating */
355 > brs_gi(gd)              /* compute BRS Glare Index */
356   struct glare_dir        *gd;
357   {
358 +        register struct glare_src       *gs;
359 +        FVECT   mydir;
360 +        double  p;
361 +        double  sum;
362 +
363 +        spinvector(mydir, midview.vdir, midview.vup, gd->ang);
364 +        sum = 0.0;
365 +        for (gs = all_srcs; gs != NULL; gs = gs->next) {
366 +                p = posindex(gs->dir, mydir, midview.vup);
367 +                if (p <= FTINY)
368 +                        continue;
369 +                sum += pow(gs->lum/p,1.6) * pow(gs->dom,0.8);
370 +        }
371 +        if (sum <= FTINY)
372 +                return(0.0);
373 +        sum /= gd->indirect/PI;
374 +        return(10*log10(0.478*sum));
375 + }
376 +
377 +
378 + double
379 + guth_dgr(gd)            /* compute Guth discomfort glare rating */
380 + struct glare_dir        *gd;
381 + {
382   #define q(w)    (20.4*w+1.52*pow(w,.2)-.075)
383          register struct glare_src       *gs;
384 +        FVECT   mydir;
385          double  p;
386          double  sum;
387 +        double  wtot, brsum;
388          int     n;
389 <
390 <        sum = 0.0; n = 0;
389 >
390 >        spinvector(mydir, midview.vdir, midview.vup, gd->ang);
391 >        sum = wtot = brsum = 0.0; n = 0;
392          for (gs = all_srcs; gs != NULL; gs = gs->next) {
393 <                p = posindex(gs->dir, midview.vdir, midview.vup);
393 >                p = posindex(gs->dir, mydir, midview.vup);
394                  if (p <= FTINY)
395                          continue;
396                  sum += gs->lum * q(gs->dom) / p;
397 +                brsum += gs->lum * gs->dom;
398 +                wtot += gs->dom;
399                  n++;
400          }
401          if (n == 0)
402                  return(0.0);
403 <        else
270 <                return( pow(
271 <                        .5*sum/pow(direct(gd)+gd->indirect,.44),
403 >        return( pow(.5*sum/pow((brsum+(5.-wtot)*gd->indirect/PI)/5.,.44),
404                          pow((double)n, -.0914) ) );
405   #undef q
406   }
407 <
408 <
277 < extern double   erf(), erfc();
278 <
407 >
408 >
409   #ifndef M_SQRT2
410   #define M_SQRT2 1.41421356237309504880
411   #endif
412 <
412 >
413   #define norm_integral(z)        (1.-.5*erfc((z)/M_SQRT2))
414 +
415 +
416 + double
417 + guth_vcp(gd)            /* compute Guth visual comfort probability */
418 + struct glare_dir        *gd;
419 + {
420 +        extern double   erfc();
421 +        double  dgr;
422 +
423 +        dgr = guth_dgr(gd);
424 +        if (dgr <= FTINY)
425 +                return(100.0);
426 +        return(100.*norm_integral(6.374-1.3227*log(dgr)));
427 + }
428 +
429 +
430 + double
431 + cie_cgi(gd)             /* compute CIE Glare Index */
432 + struct glare_dir        *gd;
433 + {
434 +        register struct glare_src       *gs;
435 +        FVECT   mydir;
436 +        double  dillum;
437 +        double  p;
438 +        double  sum;
439 +
440 +        spinvector(mydir, midview.vdir, midview.vup, gd->ang);
441 +        sum = 0.0;
442 +        for (gs = all_srcs; gs != NULL; gs = gs->next) {
443 +                p = posindex(gs->dir, mydir, midview.vup);
444 +                if (p <= FTINY)
445 +                        continue;
446 +                sum += gs->lum*gs->lum * gs->dom / (p*p);
447 +        }
448 +        if (sum <= FTINY)
449 +                return(0.0);
450 +        dillum = direct(gd);
451 +        return(8.*log10(2.*sum*(1.+dillum/500.)/(dillum+gd->indirect)));
452 + }
453  
454  
455   double
456 < gnuth_vcp(gd)           /* compute Gnuth visual comfort probability */
456 > ugr(gd)         /* compute Unified Glare Rating */
457   struct glare_dir        *gd;
458   {
459 <        return(100.*norm_integral(-6.374+1.3227*log(gnuth_dgr(gd))));
459 >        register struct glare_src       *gs;
460 >        FVECT   mydir;
461 >        double  p;
462 >        double  sum;
463 >
464 >        spinvector(mydir, midview.vdir, midview.vup, gd->ang);
465 >        sum = 0.0;
466 >        for (gs = all_srcs; gs != NULL; gs = gs->next) {
467 >                p = posindex(gs->dir, mydir, midview.vup);
468 >                if (p <= FTINY)
469 >                        continue;
470 >                sum += gs->lum*gs->lum * gs->dom / (p*p);
471 >        }
472 >        if (sum <= FTINY)
473 >                return(0.0);
474 >        return(8.*log10(0.25*sum*PI/gd->indirect));
475   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines