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 or -t option: |
6 |
|
* |
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) |
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 |
|
|
23 |
– |
extern double erfc(); |
27 |
|
|
28 |
< |
double posindex(); |
28 |
> |
struct glare_src { |
29 |
> |
FVECT dir; /* source direction */ |
30 |
> |
double dom; /* solid angle */ |
31 |
> |
double lum; /* average luminance */ |
32 |
> |
struct glare_src *next; |
33 |
> |
} *all_srcs = NULL; |
34 |
|
|
35 |
< |
double direct(), guth_dgr(), guth_vcp(), cie_cgi(), |
36 |
< |
indirect(), total(); |
35 |
> |
struct glare_dir { |
36 |
> |
double ang; /* angle (in radians) */ |
37 |
> |
double indirect; /* indirect illuminance */ |
38 |
> |
struct glare_dir *next; |
39 |
> |
} *all_dirs = NULL; |
40 |
|
|
41 |
+ |
typedef double gdfun(struct glare_dir *gd); |
42 |
+ |
|
43 |
+ |
static gdfun dgi; |
44 |
+ |
static gdfun brs_gi; |
45 |
+ |
static gdfun ugr; |
46 |
+ |
static gdfun guth_dgr; |
47 |
+ |
static gdfun guth_vcp; |
48 |
+ |
static gdfun cie_cgi; |
49 |
+ |
static gdfun direct; |
50 |
+ |
static gdfun indirect; |
51 |
+ |
static gdfun total; |
52 |
+ |
|
53 |
+ |
static gethfunc headline; |
54 |
+ |
static void init(void); |
55 |
+ |
static void read_input(void); |
56 |
+ |
static void print_values(gdfun *func); |
57 |
+ |
static double posindex(FVECT sd, FVECT vd, FVECT vu); |
58 |
+ |
|
59 |
+ |
|
60 |
|
struct named_func { |
61 |
|
char *name; |
62 |
< |
double (*func)(); |
62 |
> |
gdfun *func; |
63 |
|
char *descrip; |
64 |
|
} all_funcs[] = { |
65 |
+ |
{"dgi", dgi, "Daylight Glare Index"}, |
66 |
+ |
{"brs_gi", brs_gi, "BRS Glare Index"}, |
67 |
+ |
{"ugr", ugr, "Unified Glare Rating"}, |
68 |
|
{"guth_vcp", guth_vcp, "Guth Visual Comfort Probability"}, |
69 |
|
{"cie_cgi", cie_cgi, "CIE Glare Index (Einhorn)"}, |
70 |
|
{"guth_dgr", guth_dgr, "Guth Disability Glare Rating"}, |
74 |
|
{NULL} |
75 |
|
}; |
76 |
|
|
44 |
– |
struct glare_src { |
45 |
– |
FVECT dir; /* source direction */ |
46 |
– |
double dom; /* solid angle */ |
47 |
– |
double lum; /* average luminance */ |
48 |
– |
struct glare_src *next; |
49 |
– |
} *all_srcs = NULL; |
50 |
– |
|
51 |
– |
struct glare_dir { |
52 |
– |
double ang; /* angle (in radians) */ |
53 |
– |
double indirect; /* indirect illuminance */ |
54 |
– |
struct glare_dir *next; |
55 |
– |
} *all_dirs = NULL; |
56 |
– |
|
77 |
|
#define newp(type) (type *)malloc(sizeof(type)) |
78 |
|
|
79 |
|
char *progname; |
84 |
|
int wrongformat = 0; |
85 |
|
|
86 |
|
|
87 |
< |
main(argc, argv) |
88 |
< |
int argc; |
89 |
< |
char *argv[]; |
87 |
> |
int |
88 |
> |
main( |
89 |
> |
int argc, |
90 |
> |
char *argv[] |
91 |
> |
) |
92 |
|
{ |
71 |
– |
extern char *rindex(); |
93 |
|
struct named_func *funp; |
94 |
|
char *progtail; |
95 |
|
int i; |
96 |
|
/* get program name */ |
97 |
|
progname = argv[0]; |
98 |
< |
progtail = rindex(progname, '/'); /* final component */ |
98 |
> |
progtail = strrchr(progname, '/'); /* final component */ |
99 |
|
if (progtail == NULL) |
100 |
|
progtail = progname; |
101 |
|
else |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
headline(s) /* get line from header */ |
145 |
< |
char *s; |
144 |
> |
static int |
145 |
> |
headline( /* get line from header */ |
146 |
> |
char *s, |
147 |
> |
void *p |
148 |
> |
) |
149 |
|
{ |
150 |
|
char fmt[32]; |
151 |
|
|
152 |
|
if (print_header) /* copy to output */ |
153 |
|
fputs(s, stdout); |
154 |
< |
if (!strncmp(s, VIEWSTR, VIEWSTRL)) |
155 |
< |
sscanview(&midview, s+VIEWSTRL); |
154 |
> |
if (isview(s)) |
155 |
> |
sscanview(&midview, s); |
156 |
|
else if (isformat(s)) { |
157 |
|
formatval(fmt, s); |
158 |
|
wrongformat = strcmp(fmt, "ascii"); |
159 |
|
} |
160 |
+ |
return(0); |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
< |
init() /* initialize calculation */ |
164 |
> |
static void |
165 |
> |
init(void) /* initialize calculation */ |
166 |
|
{ |
167 |
|
/* read header */ |
168 |
|
getheader(stdin, headline, NULL); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
< |
read_input() /* read glare sources from stdin */ |
182 |
> |
static void |
183 |
> |
read_input(void) /* read glare sources from stdin */ |
184 |
|
{ |
185 |
|
#define S_SEARCH 0 |
186 |
|
#define S_SOURCE 1 |
187 |
|
#define S_DIREC 2 |
188 |
|
int state = S_SEARCH; |
189 |
|
char buf[128]; |
190 |
< |
register struct glare_src *gs; |
191 |
< |
register struct glare_dir *gd; |
190 |
> |
struct glare_src *gs; |
191 |
> |
struct glare_dir *gd; |
192 |
|
|
193 |
|
while (fgets(buf, sizeof(buf), stdin) != NULL) |
194 |
|
switch (state) { |
205 |
|
} |
206 |
|
if ((gs = newp(struct glare_src)) == NULL) |
207 |
|
goto memerr; |
208 |
< |
if (sscanf(buf, "%lf %lf %lf %lf %lf", |
209 |
< |
&gs->dir[0], &gs->dir[1], &gs->dir[2], |
210 |
< |
&gs->dom, &gs->lum) != 5) |
208 |
> |
if (sscanf(buf, FVFORMAT, &gs->dir[0], &gs->dir[1], |
209 |
> |
&gs->dir[2]) != 3 || |
210 |
> |
sscanf(buf, "%lf %lf", |
211 |
> |
&gs->dom, &gs->lum) != 2) |
212 |
|
goto readerr; |
213 |
|
normalize(gs->dir); |
214 |
|
gs->next = all_srcs; |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
< |
print_values(funp) /* print out calculations */ |
246 |
< |
double (*funp)(); |
245 |
> |
static void |
246 |
> |
print_values( /* print out calculations */ |
247 |
> |
gdfun *func |
248 |
> |
) |
249 |
|
{ |
250 |
< |
register struct glare_dir *gd; |
250 |
> |
struct glare_dir *gd; |
251 |
|
|
252 |
|
for (gd = all_dirs; gd != NULL; gd = gd->next) |
253 |
< |
printf("%f\t%f\n", gd->ang*(180.0/PI), (*funp)(gd)); |
253 |
> |
printf("%f\t%f\n", gd->ang*(180.0/PI), (*func)(gd)); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
< |
double |
258 |
< |
direct(gd) /* compute direct vertical illuminance */ |
259 |
< |
struct glare_dir *gd; |
257 |
> |
static double |
258 |
> |
direct( /* compute direct vertical illuminance */ |
259 |
> |
struct glare_dir *gd |
260 |
> |
) |
261 |
|
{ |
262 |
|
FVECT mydir; |
263 |
|
double d, dval; |
264 |
< |
register struct glare_src *gs; |
264 |
> |
struct glare_src *gs; |
265 |
|
|
266 |
|
spinvector(mydir, midview.vdir, midview.vup, gd->ang); |
267 |
|
dval = 0.0; |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
< |
double |
278 |
< |
indirect(gd) /* return indirect vertical illuminance */ |
279 |
< |
struct glare_dir *gd; |
277 |
> |
static double |
278 |
> |
indirect( /* return indirect vertical illuminance */ |
279 |
> |
struct glare_dir *gd |
280 |
> |
) |
281 |
|
{ |
282 |
|
return(gd->indirect); |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
< |
double |
287 |
< |
total(gd) /* return total vertical illuminance */ |
288 |
< |
struct glare_dir *gd; |
286 |
> |
static double |
287 |
> |
total( /* return total vertical illuminance */ |
288 |
> |
struct glare_dir *gd |
289 |
> |
) |
290 |
|
{ |
291 |
|
return(direct(gd)+gd->indirect); |
292 |
|
} |
306 |
|
* We return a value less than zero for improper positions. |
307 |
|
*/ |
308 |
|
|
309 |
< |
double |
310 |
< |
posindex(sd, vd, vu) /* compute position index */ |
311 |
< |
FVECT sd, vd, vu; |
309 |
> |
static double |
310 |
> |
posindex( /* compute position index */ |
311 |
> |
FVECT sd, |
312 |
> |
FVECT vd, |
313 |
> |
FVECT vu |
314 |
> |
) |
315 |
|
{ |
316 |
|
double sigma, tau; |
317 |
|
double d; |
322 |
|
if (d >= 1.0) |
323 |
|
return(1.0); |
324 |
|
sigma = acos(d) * (180./PI); |
325 |
< |
d = DOT(sd,vu)/sqrt(1.0-d*d); |
325 |
> |
d = fabs(DOT(sd,vu)/sqrt(1.0-d*d)); |
326 |
|
if (d >= 1.0) |
327 |
|
tau = 0.0; |
328 |
|
else |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
< |
double |
337 |
< |
guth_dgr(gd) /* compute Guth discomfort glare rating */ |
338 |
< |
struct glare_dir *gd; |
336 |
> |
static double |
337 |
> |
dgi( /* compute Daylight Glare Index */ |
338 |
> |
struct glare_dir *gd |
339 |
> |
) |
340 |
|
{ |
341 |
+ |
struct glare_src *gs; |
342 |
+ |
FVECT mydir,testdir[7],vhor; |
343 |
+ |
double r,posn,omega,p[7],sum; |
344 |
+ |
int i,n; |
345 |
+ |
|
346 |
+ |
spinvector(mydir, midview.vdir, midview.vup, gd->ang); |
347 |
+ |
sum = 0.0; n = 0; |
348 |
+ |
for (gs = all_srcs; gs != NULL; gs = gs->next) { |
349 |
+ |
|
350 |
+ |
/* compute 1/p^2 weighted solid angle of the source */ |
351 |
+ |
r = sqrt(1 - pow(1.-gs->dom/2./PI,2.)); |
352 |
+ |
fcross(vhor,gs->dir,midview.vup); |
353 |
+ |
normalize(vhor); |
354 |
+ |
VCOPY(testdir[0],gs->dir); |
355 |
+ |
fvsum(testdir[1],gs->dir,vhor,r); |
356 |
+ |
fvsum(testdir[2],gs->dir,vhor,0.5*r); |
357 |
+ |
fvsum(testdir[5],testdir[2],midview.vup,-0.866*r); |
358 |
+ |
fvsum(testdir[2],testdir[2],midview.vup,0.866*r); |
359 |
+ |
fvsum(testdir[3],gs->dir,vhor,-r); |
360 |
+ |
fvsum(testdir[4],gs->dir,vhor,-0.5*r); |
361 |
+ |
fvsum(testdir[6],testdir[4],midview.vup,0.866*r); |
362 |
+ |
fvsum(testdir[4],testdir[4],midview.vup,-0.866*r); |
363 |
+ |
for (i = 0; i < 7; i++) { |
364 |
+ |
normalize(testdir[i]); |
365 |
+ |
posn = posindex(testdir[i],mydir,midview.vup); |
366 |
+ |
if (posn <= FTINY) |
367 |
+ |
p[i] = 0.0; |
368 |
+ |
else |
369 |
+ |
p[i] = 1./(posn*posn); |
370 |
+ |
} |
371 |
+ |
r = 1-gs->dom/2./PI; |
372 |
+ |
omega = gs->dom*p[0]; |
373 |
+ |
omega += (r*PI*(1+1/r/r)-2*PI)*(-p[0]+(p[1]+p[2])*0.5); |
374 |
+ |
omega += (2*PI-r*PI*(1+1/r/r))*(-p[0]-0.1667*(p[1]+p[3]) |
375 |
+ |
+0.3334*(p[2]+p[4]+p[5]+p[6])); |
376 |
+ |
|
377 |
+ |
sum += pow(gs->lum,1.6) * pow(omega,0.8) / |
378 |
+ |
(gd->indirect/PI + 0.07*sqrt(gs->dom)*gs->lum); |
379 |
+ |
n++; |
380 |
+ |
} |
381 |
+ |
if (n == 0) |
382 |
+ |
return(0.0); |
383 |
+ |
return( 10*log10(0.478*sum) ); |
384 |
+ |
} |
385 |
+ |
|
386 |
+ |
|
387 |
+ |
static double |
388 |
+ |
brs_gi( /* compute BRS Glare Index */ |
389 |
+ |
struct glare_dir *gd |
390 |
+ |
) |
391 |
+ |
{ |
392 |
+ |
struct glare_src *gs; |
393 |
+ |
FVECT mydir; |
394 |
+ |
double p; |
395 |
+ |
double sum; |
396 |
+ |
|
397 |
+ |
spinvector(mydir, midview.vdir, midview.vup, gd->ang); |
398 |
+ |
sum = 0.0; |
399 |
+ |
for (gs = all_srcs; gs != NULL; gs = gs->next) { |
400 |
+ |
p = posindex(gs->dir, mydir, midview.vup); |
401 |
+ |
if (p <= FTINY) |
402 |
+ |
continue; |
403 |
+ |
sum += pow(gs->lum/p,1.6) * pow(gs->dom,0.8); |
404 |
+ |
} |
405 |
+ |
if (sum <= FTINY) |
406 |
+ |
return(0.0); |
407 |
+ |
sum /= gd->indirect/PI; |
408 |
+ |
return(10*log10(0.478*sum)); |
409 |
+ |
} |
410 |
+ |
|
411 |
+ |
|
412 |
+ |
static double |
413 |
+ |
guth_dgr( /* compute Guth discomfort glare rating */ |
414 |
+ |
struct glare_dir *gd |
415 |
+ |
) |
416 |
+ |
{ |
417 |
|
#define q(w) (20.4*w+1.52*pow(w,.2)-.075) |
418 |
< |
register struct glare_src *gs; |
418 |
> |
struct glare_src *gs; |
419 |
|
FVECT mydir; |
420 |
|
double p; |
421 |
|
double sum; |
435 |
|
} |
436 |
|
if (n == 0) |
437 |
|
return(0.0); |
325 |
– |
|
438 |
|
return( pow(.5*sum/pow((brsum+(5.-wtot)*gd->indirect/PI)/5.,.44), |
439 |
|
pow((double)n, -.0914) ) ); |
440 |
|
#undef q |
441 |
|
} |
442 |
|
|
443 |
|
|
332 |
– |
extern double erf(), erfc(); |
333 |
– |
|
444 |
|
#ifndef M_SQRT2 |
445 |
|
#define M_SQRT2 1.41421356237309504880 |
446 |
|
#endif |
448 |
|
#define norm_integral(z) (1.-.5*erfc((z)/M_SQRT2)) |
449 |
|
|
450 |
|
|
451 |
< |
double |
452 |
< |
guth_vcp(gd) /* compute Guth visual comfort probability */ |
453 |
< |
struct glare_dir *gd; |
451 |
> |
static double |
452 |
> |
guth_vcp( /* compute Guth visual comfort probability */ |
453 |
> |
struct glare_dir *gd |
454 |
> |
) |
455 |
|
{ |
456 |
+ |
extern double erfc(); |
457 |
|
double dgr; |
458 |
|
|
459 |
|
dgr = guth_dgr(gd); |
463 |
|
} |
464 |
|
|
465 |
|
|
466 |
< |
double |
467 |
< |
cie_cgi(gd) /* compute CIE Glare Index */ |
468 |
< |
struct glare_dir *gd; |
466 |
> |
static double |
467 |
> |
cie_cgi( /* compute CIE Glare Index */ |
468 |
> |
struct glare_dir *gd |
469 |
> |
) |
470 |
|
{ |
471 |
< |
register struct glare_src *gs; |
471 |
> |
struct glare_src *gs; |
472 |
|
FVECT mydir; |
473 |
|
double dillum; |
474 |
|
double p; |
486 |
|
return(0.0); |
487 |
|
dillum = direct(gd); |
488 |
|
return(8.*log10(2.*sum*(1.+dillum/500.)/(dillum+gd->indirect))); |
489 |
+ |
} |
490 |
+ |
|
491 |
+ |
|
492 |
+ |
static double |
493 |
+ |
ugr( /* compute Unified Glare Rating */ |
494 |
+ |
struct glare_dir *gd |
495 |
+ |
) |
496 |
+ |
{ |
497 |
+ |
struct glare_src *gs; |
498 |
+ |
FVECT mydir; |
499 |
+ |
double p; |
500 |
+ |
double sum; |
501 |
+ |
|
502 |
+ |
spinvector(mydir, midview.vdir, midview.vup, gd->ang); |
503 |
+ |
sum = 0.0; |
504 |
+ |
for (gs = all_srcs; gs != NULL; gs = gs->next) { |
505 |
+ |
p = posindex(gs->dir, mydir, midview.vup); |
506 |
+ |
if (p <= FTINY) |
507 |
+ |
continue; |
508 |
+ |
sum += gs->lum*gs->lum * gs->dom / (p*p); |
509 |
+ |
} |
510 |
+ |
if (sum <= FTINY) |
511 |
+ |
return(0.0); |
512 |
+ |
return(8.*log10(0.25*sum*PI/gd->indirect)); |
513 |
|
} |