| 18 |
|
const char win6_template[] = "WINDOW6BSDFt.xml"; |
| 19 |
|
|
| 20 |
|
const char stdin_name[] = "<stdin>"; |
| 21 |
– |
|
| 21 |
|
/* input files (can be stdin_name) */ |
| 22 |
|
const char *xml_input = NULL; |
| 23 |
|
/* unit for materials & geometry */ |
| 26 |
|
/* system materials & geometry */ |
| 27 |
|
const char *mgf_geometry = NULL; |
| 28 |
|
|
| 29 |
< |
/* angle basis */ |
| 29 |
> |
/* angle bases */ |
| 30 |
|
enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter, |
| 31 |
|
ABtensorTree3, ABtensorTree4, ABend }; |
| 32 |
|
|
| 33 |
|
int angle_basis = ABdefault; |
| 34 |
|
|
| 35 |
+ |
int correct_solid_angle = 0; |
| 36 |
+ |
|
| 37 |
+ |
const char *klems_basis_name[] = { |
| 38 |
+ |
"LBNL/Klems Full", |
| 39 |
+ |
"LBNL/Klems Half", |
| 40 |
+ |
"LBNL/Klems Quarter", |
| 41 |
+ |
}; |
| 42 |
|
/* field IDs and nicknames */ |
| 43 |
|
struct s_fieldID { |
| 44 |
|
char nickName[4]; |
| 63 |
|
const char *field_assignment[MAXASSIGN]; |
| 64 |
|
int nfield_assign = 0; |
| 65 |
|
#define FASEP ';' |
| 60 |
– |
|
| 66 |
|
/* data file(s) & spectra */ |
| 67 |
|
enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward }; |
| 68 |
|
|
| 69 |
< |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 }; |
| 69 |
> |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5, |
| 70 |
> |
DSuprime=-6, DSvprime=-7 }; |
| 71 |
|
|
| 72 |
|
#define MAXFILES 20 |
| 73 |
|
|
| 160 |
|
fprintf(stderr, "%s: cannot open\n", inpspec); |
| 161 |
|
return ""; |
| 162 |
|
} |
| 163 |
+ |
#ifndef _WIN32 /* XXX somehow broken on Windows */ |
| 164 |
|
len = lseek(fd, 0L, SEEK_END); |
| 165 |
|
if (len > 0) { |
| 166 |
|
lseek(fd, 0L, SEEK_SET); |
| 179 |
|
close(fd); |
| 180 |
|
return str; |
| 181 |
|
} |
| 182 |
+ |
#endif |
| 183 |
|
fp = fdopen(fd, "r"); /* not a regular file */ |
| 184 |
|
} |
| 185 |
|
/* reading from stream */ |
| 229 |
|
for (i = 0; i < nfield_assign; i++) { |
| 230 |
|
const char *fnext = field_assignment[i]; |
| 231 |
|
for ( ; ; ) { |
| 232 |
+ |
int added = 0; |
| 233 |
|
ezxml_t fld; |
| 234 |
|
char sbuf[512]; |
| 235 |
|
int j; |
| 262 |
|
fprintf(stderr, |
| 263 |
|
"%s: warning - adding tag <%s>\n", |
| 264 |
|
fn, sbuf); |
| 265 |
+ |
ezxml_add_txt(wtl, "\t"); |
| 266 |
|
fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt)); |
| 267 |
+ |
++added; |
| 268 |
|
} |
| 269 |
|
if (XMLfieldID[j].has_unit) |
| 270 |
|
ezxml_set_attr(fld, "unit", attr_unit); |
| 288 |
|
} |
| 289 |
|
sbuf[j] = '\0'; |
| 290 |
|
ezxml_set_txt_d(fld, sbuf); |
| 291 |
+ |
if (added) |
| 292 |
+ |
ezxml_add_txt(wtl, "\n\t"); |
| 293 |
|
fnext += (*fnext == FASEP); |
| 294 |
|
} |
| 295 |
|
} |
| 297 |
|
if (xml_input == win6_template) |
| 298 |
|
for (i = 0; XMLfieldID[i].nickName[0]; i++) |
| 299 |
|
if (XMLfieldID[i].win_need && |
| 300 |
< |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) { |
| 300 |
> |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) |
| 301 |
|
fprintf(stderr, |
| 302 |
< |
"%s: missing required '%s' assignment for WINDOW <%s>\n", |
| 302 |
> |
"%s: warning - missing '%s' assignment for WINDOW <%s>\n", |
| 303 |
|
caller, XMLfieldID[i].nickName, |
| 304 |
|
XMLfieldID[i].fullName); |
| 305 |
< |
return 0; |
| 293 |
< |
} |
| 294 |
< |
return 1; /* no errors */ |
| 305 |
> |
return 1; |
| 306 |
|
} |
| 307 |
|
|
| 308 |
|
/* Complete angle basis specification */ |
| 384 |
|
return -1; |
| 385 |
|
} |
| 386 |
|
|
| 387 |
+ |
/* Filter Klems angle basis, factoring out incident projected solid angle */ |
| 388 |
+ |
static int |
| 389 |
+ |
filter_klems_matrix(FILE *fp) |
| 390 |
+ |
{ |
| 391 |
+ |
#define MAX_COLUMNS 145 |
| 392 |
+ |
const char *bn = klems_basis_name[angle_basis]; |
| 393 |
+ |
float col_corr[MAX_COLUMNS]; |
| 394 |
+ |
int i, j, n = nabases; |
| 395 |
+ |
/* get angle basis */ |
| 396 |
+ |
while (n-- > 0) |
| 397 |
+ |
if (!strcasecmp(bn, abase_list[n].name)) |
| 398 |
+ |
break; |
| 399 |
+ |
if (n < 0) |
| 400 |
+ |
return 0; |
| 401 |
+ |
if (abase_list[n].nangles > MAX_COLUMNS) { |
| 402 |
+ |
fputs("Internal error - too many Klems columns!\n", stderr); |
| 403 |
+ |
return 0; |
| 404 |
+ |
} |
| 405 |
+ |
/* get correction factors */ |
| 406 |
+ |
for (j = abase_list[n].nangles; j--; ) |
| 407 |
+ |
col_corr[j] = 1.f / io_getohm(j, &abase_list[n]); |
| 408 |
+ |
/* read/correct/write matrix */ |
| 409 |
+ |
for (i = 0; i < abase_list[n].nangles; i++) { |
| 410 |
+ |
for (j = 0; j < abase_list[n].nangles; j++) { |
| 411 |
+ |
double d; |
| 412 |
+ |
if (fscanf(fp, "%lf", &d) != 1) |
| 413 |
+ |
return 0; |
| 414 |
+ |
if (d < -1e-3) { |
| 415 |
+ |
fputs("Negative BSDF data!\n", stderr); |
| 416 |
+ |
return 0; |
| 417 |
+ |
} |
| 418 |
+ |
printf(" %.3e", d*col_corr[j]*(d > 0)); |
| 419 |
+ |
} |
| 420 |
+ |
fputc('\n', stdout); |
| 421 |
+ |
} |
| 422 |
+ |
while ((i = getc(fp)) != EOF) |
| 423 |
+ |
if (!isspace(i)) { |
| 424 |
+ |
fputs("Unexpected data past EOF\n", stderr); |
| 425 |
+ |
return 0; |
| 426 |
+ |
} |
| 427 |
+ |
return 1; /* all is good */ |
| 428 |
+ |
#undef MAX_COLUMNS |
| 429 |
+ |
} |
| 430 |
+ |
|
| 431 |
|
/* Write out BSDF data block with surrounding tags */ |
| 432 |
|
static int |
| 433 |
|
writeBSDFblock(const char *caller, struct s_dfile *df) |
| 434 |
|
{ |
| 435 |
+ |
int correct_klems = correct_solid_angle; |
| 436 |
|
char *cp; |
| 437 |
|
|
| 438 |
|
puts("\t<WavelengthData>"); |
| 440 |
|
switch (df->spectrum) { |
| 441 |
|
case DSvisible: |
| 442 |
|
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
| 443 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 443 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 444 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
| 445 |
|
break; |
| 446 |
|
case DSxbar31: |
| 453 |
|
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 454 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>"); |
| 455 |
|
break; |
| 456 |
+ |
case DSuprime: |
| 457 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>"); |
| 458 |
+ |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 459 |
+ |
puts("\t\t<DetectorSpectrum>ASTM E308 1931 u.dsp</DetectorSpectrum>"); |
| 460 |
+ |
break; |
| 461 |
+ |
case DSvprime: |
| 462 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>"); |
| 463 |
+ |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 464 |
+ |
puts("\t\t<DetectorSpectrum>ASTM E308 1931 v.dsp</DetectorSpectrum>"); |
| 465 |
+ |
break; |
| 466 |
|
case DSsolar: |
| 467 |
|
puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>"); |
| 468 |
|
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 508 |
|
puts("</WavelengthDataDirection>"); |
| 509 |
|
switch (angle_basis) { |
| 510 |
|
case ABklemsFull: |
| 445 |
– |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>"); |
| 446 |
– |
break; |
| 511 |
|
case ABklemsHalf: |
| 448 |
– |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>"); |
| 449 |
– |
break; |
| 512 |
|
case ABklemsQuarter: |
| 513 |
< |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>"); |
| 513 |
> |
fputs("\t\t\t<ColumnAngleBasis>", stdout); |
| 514 |
> |
fputs(klems_basis_name[angle_basis], stdout); |
| 515 |
> |
puts("</ColumnAngleBasis>"); |
| 516 |
> |
fputs("\t\t\t<RowAngleBasis>", stdout); |
| 517 |
> |
fputs(klems_basis_name[angle_basis], stdout); |
| 518 |
> |
puts("</RowAngleBasis>"); |
| 519 |
|
break; |
| 520 |
|
case ABtensorTree3: |
| 521 |
|
case ABtensorTree4: |
| 522 |
|
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
| 523 |
+ |
correct_klems = 0; |
| 524 |
|
break; |
| 525 |
|
default: |
| 526 |
|
fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis); |
| 529 |
|
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
| 530 |
|
puts("\t\t\t<ScatteringData>"); |
| 531 |
|
fflush(stdout); |
| 532 |
< |
if (df->fname == stdin_name) { |
| 532 |
> |
if (correct_klems) { /* correct Klems matrix data */ |
| 533 |
> |
FILE *fp = stdin; |
| 534 |
> |
if (df->fname[0] == '!') |
| 535 |
> |
fp = popen(df->fname+1, "r"); |
| 536 |
> |
else if (df->fname != stdin_name) |
| 537 |
> |
fp = fopen(df->fname, "r"); |
| 538 |
> |
if (fp == NULL) { |
| 539 |
> |
fprintf(stderr, "%s: cannot open '%s'\n", |
| 540 |
> |
caller, df->fname); |
| 541 |
> |
return 0; |
| 542 |
> |
} |
| 543 |
> |
if (!filter_klems_matrix(fp)) { |
| 544 |
> |
fprintf(stderr, "%s: Klems data error from '%s'\n", |
| 545 |
> |
caller, df->fname); |
| 546 |
> |
return 0; |
| 547 |
> |
} |
| 548 |
> |
if (df->fname[0] != '!') { |
| 549 |
> |
fclose(fp); |
| 550 |
> |
} else if (pclose(fp)) { |
| 551 |
> |
fprintf(stderr, "%s: error running '%s'\n", |
| 552 |
> |
caller, df->fname); |
| 553 |
> |
return 0; |
| 554 |
> |
} |
| 555 |
> |
} else if (df->fname == stdin_name) { |
| 556 |
|
copy_and_close(fileno(stdin)); |
| 557 |
|
} else if (df->fname[0] != '!') { |
| 558 |
|
if (!copy_and_close(open(df->fname, O_RDONLY))) { |
| 684 |
|
ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition"); |
| 685 |
|
if (dd != NULL) { |
| 686 |
|
offset = dd->off; |
| 687 |
< |
fprintf(stderr, |
| 687 |
> |
if (dd->child != NULL) |
| 688 |
> |
fprintf(stderr, |
| 689 |
|
"%s: warning - replacing existing <DataDefinition> in '%s'\n", |
| 690 |
|
caller, xml_path); |
| 691 |
|
ezxml_remove(dd); |
| 718 |
|
{ |
| 719 |
|
fputs("Usage: ", stderr); |
| 720 |
|
fputs(pname, stderr); |
| 721 |
< |
fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
| 721 |
> |
fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
| 722 |
|
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr); |
| 723 |
|
fputs(" [input.xml]\n", stderr); |
| 724 |
|
exit(1); |
| 785 |
|
else |
| 786 |
|
UsageExit(argv[0]); |
| 787 |
|
continue; |
| 788 |
+ |
case 'c': /* correct solid angle */ |
| 789 |
+ |
correct_solid_angle = 1; |
| 790 |
+ |
continue; |
| 791 |
|
case 't': /* transmission */ |
| 792 |
|
if (i >= argc-1) |
| 793 |
|
UsageExit(argv[0]); |
| 829 |
|
argv[i] = (char *)stdin_name; |
| 830 |
|
} |
| 831 |
|
data_file[ndataf].fname = argv[i]; |
| 832 |
< |
data_file[ndataf++].spectrum = cur_spectrum; |
| 832 |
> |
data_file[ndataf].spectrum = cur_spectrum; |
| 833 |
> |
ndataf++; |
| 834 |
|
continue; |
| 835 |
|
case 's': /* spectrum name or input file */ |
| 836 |
|
if (++i >= argc) |
| 844 |
|
cur_spectrum = DSxbar31; |
| 845 |
|
else if (!strcasecmp(argv[i], "CIE-Z")) |
| 846 |
|
cur_spectrum = DSzbar31; |
| 847 |
+ |
else if (!strcasecmp(argv[i], "CIE-u")) |
| 848 |
+ |
cur_spectrum = DSuprime; |
| 849 |
+ |
else if (!strcasecmp(argv[i], "CIE-v")) |
| 850 |
+ |
cur_spectrum = DSvprime; |
| 851 |
|
else if (!strcasecmp(argv[i], "NIR")) |
| 852 |
|
cur_spectrum = DSnir; |
| 853 |
|
else { |
| 896 |
|
} else { |
| 897 |
|
xml_input = argv[i]; |
| 898 |
|
} |
| 799 |
– |
if ((xml_input == win6_template) & (angle_basis == ABdefault)) |
| 800 |
– |
angle_basis = ABklemsFull; |
| 899 |
|
/* wrap it! */ |
| 900 |
|
return !wrapBSDF(argv[0]); |
| 901 |
|
} |