9 |
|
|
10 |
|
#include <ctype.h> |
11 |
|
#include "rtio.h" |
12 |
< |
#include "rtprocess.h" |
12 |
> |
#include "paths.h" |
13 |
|
#include "ezxml.h" |
14 |
|
#include "bsdf.h" |
15 |
|
#include "bsdf_m.h" |
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 */ |
25 |
|
const char legal_units[] = "meter|foot|inch|centimeter|millimeter"; |
26 |
|
/* system materials & geometry */ |
27 |
|
const char *mgf_geometry = NULL; |
28 |
< |
|
28 |
> |
/* comment list */ |
29 |
> |
#define MAXCOMM 30 |
30 |
> |
const char *commlist[MAXCOMM]; |
31 |
> |
int ncomm = 0; |
32 |
|
/* angle bases */ |
33 |
|
enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter, |
34 |
|
ABtensorTree3, ABtensorTree4, ABend }; |
35 |
|
|
36 |
|
int angle_basis = ABdefault; |
37 |
|
|
38 |
+ |
int correct_solid_angle = 0; |
39 |
+ |
|
40 |
+ |
const char *klems_basis_name[] = { |
41 |
+ |
"LBNL/Klems Full", |
42 |
+ |
"LBNL/Klems Half", |
43 |
+ |
"LBNL/Klems Quarter", |
44 |
+ |
}; |
45 |
|
/* field IDs and nicknames */ |
46 |
|
struct s_fieldID { |
47 |
|
char nickName[4]; |
66 |
|
const char *field_assignment[MAXASSIGN]; |
67 |
|
int nfield_assign = 0; |
68 |
|
#define FASEP ';' |
60 |
– |
|
69 |
|
/* data file(s) & spectra */ |
70 |
|
enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward }; |
71 |
|
|
72 |
< |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 }; |
72 |
> |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5, |
73 |
> |
DSuprime=-6, DSvprime=-7 }; |
74 |
|
|
75 |
|
#define MAXFILES 20 |
76 |
|
|
82 |
|
|
83 |
|
int ndataf = 0; /* number of data files */ |
84 |
|
|
85 |
+ |
int unlink_datafiles = 0; /* unlink data files when done */ |
86 |
+ |
|
87 |
|
const char *spectr_file[MAXFILES]; /* custom spectral curve input */ |
88 |
|
|
89 |
|
const char top_level_name[] = "WindowElement"; |
165 |
|
fprintf(stderr, "%s: cannot open\n", inpspec); |
166 |
|
return ""; |
167 |
|
} |
168 |
+ |
#ifndef _WIN32 /* XXX somehow broken on Windows */ |
169 |
|
len = lseek(fd, 0L, SEEK_END); |
170 |
|
if (len > 0) { |
171 |
|
lseek(fd, 0L, SEEK_SET); |
184 |
|
close(fd); |
185 |
|
return str; |
186 |
|
} |
187 |
+ |
#endif |
188 |
|
fp = fdopen(fd, "r"); /* not a regular file */ |
189 |
|
} |
190 |
|
/* reading from stream */ |
302 |
|
if (xml_input == win6_template) |
303 |
|
for (i = 0; XMLfieldID[i].nickName[0]; i++) |
304 |
|
if (XMLfieldID[i].win_need && |
305 |
< |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) { |
305 |
> |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) |
306 |
|
fprintf(stderr, |
307 |
< |
"%s: missing required '%s' assignment for WINDOW <%s>\n", |
307 |
> |
"%s: warning - missing '%s' assignment for WINDOW <%s>\n", |
308 |
|
caller, XMLfieldID[i].nickName, |
309 |
|
XMLfieldID[i].fullName); |
310 |
< |
return 0; |
298 |
< |
} |
299 |
< |
return 1; /* no errors */ |
310 |
> |
return 1; |
311 |
|
} |
312 |
|
|
313 |
|
/* Complete angle basis specification */ |
389 |
|
return -1; |
390 |
|
} |
391 |
|
|
392 |
< |
/* Filter Klems angle basis, applying appropriate solid angle correction */ |
392 |
> |
/* Filter Klems angle basis, factoring out incident projected solid angle */ |
393 |
|
static int |
394 |
|
filter_klems_matrix(FILE *fp) |
395 |
|
{ |
396 |
|
#define MAX_COLUMNS 145 |
397 |
+ |
const char *bn = klems_basis_name[angle_basis]; |
398 |
|
float col_corr[MAX_COLUMNS]; |
387 |
– |
const char *bn; |
399 |
|
int i, j, n = nabases; |
400 |
|
/* get angle basis */ |
390 |
– |
switch (angle_basis) { |
391 |
– |
case ABklemsFull: bn = "LBNL/Klems Full"; break; |
392 |
– |
case ABklemsHalf: bn = "LBNL/Klems Half"; break; |
393 |
– |
case ABklemsQuarter: bn = "LBNL/Klems Quarter"; break; |
394 |
– |
default: |
395 |
– |
return 0; |
396 |
– |
} |
401 |
|
while (n-- > 0) |
402 |
|
if (!strcasecmp(bn, abase_list[n].name)) |
403 |
|
break; |
404 |
|
if (n < 0) |
405 |
|
return 0; |
406 |
|
if (abase_list[n].nangles > MAX_COLUMNS) { |
407 |
< |
fprintf(stderr, "Internal error - too many Klems columns!\n"); |
407 |
> |
fputs("Internal error - too many Klems columns!\n", stderr); |
408 |
|
return 0; |
409 |
|
} |
410 |
|
/* get correction factors */ |
416 |
|
double d; |
417 |
|
if (fscanf(fp, "%lf", &d) != 1) |
418 |
|
return 0; |
419 |
< |
printf(" %f", d*col_corr[j]); |
419 |
> |
if (d < -1e-3) { |
420 |
> |
fputs("Negative BSDF data!\n", stderr); |
421 |
> |
return 0; |
422 |
> |
} |
423 |
> |
printf(" %.3e", d*col_corr[j]*(d > 0)); |
424 |
|
} |
425 |
|
fputc('\n', stdout); |
426 |
|
} |
427 |
< |
return 1; |
427 |
> |
while ((i = getc(fp)) != EOF) |
428 |
> |
if (!isspace(i)) { |
429 |
> |
fputs("Unexpected data past EOF\n", stderr); |
430 |
> |
return 0; |
431 |
> |
} |
432 |
> |
return 1; /* all is good */ |
433 |
|
#undef MAX_COLUMNS |
434 |
|
} |
435 |
|
|
437 |
|
static int |
438 |
|
writeBSDFblock(const char *caller, struct s_dfile *df) |
439 |
|
{ |
440 |
< |
int klems_data = 0; |
440 |
> |
int correct_klems = correct_solid_angle; |
441 |
|
char *cp; |
442 |
|
|
443 |
|
puts("\t<WavelengthData>"); |
445 |
|
switch (df->spectrum) { |
446 |
|
case DSvisible: |
447 |
|
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
448 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
448 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
449 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
450 |
|
break; |
451 |
|
case DSxbar31: |
452 |
|
puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>"); |
453 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
453 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
454 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>"); |
455 |
|
break; |
456 |
|
case DSzbar31: |
457 |
|
puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>"); |
458 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
458 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
459 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>"); |
460 |
|
break; |
461 |
+ |
case DSuprime: |
462 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>"); |
463 |
+ |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
464 |
+ |
break; |
465 |
+ |
case DSvprime: |
466 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>"); |
467 |
+ |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
468 |
+ |
break; |
469 |
|
case DSsolar: |
470 |
|
puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>"); |
471 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
471 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
472 |
|
puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>"); |
473 |
|
break; |
474 |
|
case DSnir: |
475 |
|
puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>"); |
476 |
< |
puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>"); |
476 |
> |
puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>"); |
477 |
|
puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>"); |
478 |
|
break; |
479 |
|
default: |
484 |
|
spectr_file[df->spectrum]); |
485 |
|
if (cp != NULL) |
486 |
|
*cp = '.'; |
487 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
487 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
488 |
|
printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n", |
489 |
|
spectr_file[df->spectrum]); |
490 |
|
break; |
509 |
|
return 0; |
510 |
|
} |
511 |
|
puts("</WavelengthDataDirection>"); |
491 |
– |
klems_data = 1; |
512 |
|
switch (angle_basis) { |
513 |
|
case ABklemsFull: |
494 |
– |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>"); |
495 |
– |
break; |
514 |
|
case ABklemsHalf: |
497 |
– |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>"); |
498 |
– |
break; |
515 |
|
case ABklemsQuarter: |
516 |
< |
puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>"); |
516 |
> |
fputs("\t\t\t<ColumnAngleBasis>", stdout); |
517 |
> |
fputs(klems_basis_name[angle_basis], stdout); |
518 |
> |
puts("</ColumnAngleBasis>"); |
519 |
> |
fputs("\t\t\t<RowAngleBasis>", stdout); |
520 |
> |
fputs(klems_basis_name[angle_basis], stdout); |
521 |
> |
puts("</RowAngleBasis>"); |
522 |
|
break; |
523 |
|
case ABtensorTree3: |
524 |
|
case ABtensorTree4: |
525 |
|
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
526 |
< |
klems_data = 0; |
526 |
> |
correct_klems = 0; |
527 |
|
break; |
528 |
|
default: |
529 |
|
fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis); |
532 |
|
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
533 |
|
puts("\t\t\t<ScatteringData>"); |
534 |
|
fflush(stdout); |
535 |
< |
if (klems_data) { |
535 |
> |
if (correct_klems) { /* correct Klems matrix data */ |
536 |
|
FILE *fp = stdin; |
537 |
|
if (df->fname[0] == '!') |
538 |
|
fp = popen(df->fname+1, "r"); |
590 |
|
free(xml); |
591 |
|
return 0; |
592 |
|
} |
593 |
+ |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
594 |
+ |
for (i = 0; i < ncomm; i++) |
595 |
+ |
printf("<!-- %s -->\n", commlist[i]); |
596 |
|
fflush(stdout); /* write previous XML info. */ |
597 |
|
if (write(fileno(stdout), xml, ei) != ei) { |
598 |
|
free(xml); |
606 |
|
fputs(xml+ei, stdout); /* write trailer */ |
607 |
|
free(xml); /* free string */ |
608 |
|
fputc('\n', stdout); |
609 |
< |
return (fflush(stdout) == 0); |
609 |
> |
if (fflush(stdout) != 0) |
610 |
> |
return 0; |
611 |
> |
/* unlink data files if req. */ |
612 |
> |
for (i = ndataf*(unlink_datafiles != 0); i--; ) |
613 |
> |
if (data_file[i].fname != stdin_name && |
614 |
> |
data_file[i].fname[0] != '!') |
615 |
> |
unlink(data_file[i].fname); |
616 |
> |
return 1; |
617 |
|
} |
618 |
|
|
619 |
|
/* Insert BSDF data into XML wrapper */ |
731 |
|
{ |
732 |
|
fputs("Usage: ", stderr); |
733 |
|
fputs(pname, stderr); |
734 |
< |
fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
735 |
< |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr); |
734 |
> |
fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
735 |
> |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr); |
736 |
|
fputs(" [input.xml]\n", stderr); |
737 |
|
exit(1); |
738 |
|
} |
777 |
|
} |
778 |
|
attr_unit = argv[i]; |
779 |
|
continue; |
780 |
+ |
case 'U': /* unlink data files when done */ |
781 |
+ |
unlink_datafiles = 1; |
782 |
+ |
continue; |
783 |
|
case 'a': /* angle basis */ |
784 |
|
if (++i >= argc) |
785 |
|
UsageExit(argv[0]); |
801 |
|
else |
802 |
|
UsageExit(argv[0]); |
803 |
|
continue; |
804 |
+ |
case 'c': /* correct solid angle */ |
805 |
+ |
correct_solid_angle = 1; |
806 |
+ |
continue; |
807 |
+ |
case 'C': /* comment */ |
808 |
+ |
if (ncomm >= MAXCOMM) { |
809 |
+ |
fprintf(stderr, "%s: too many comments\n", |
810 |
+ |
argv[0]); |
811 |
+ |
return 1; |
812 |
+ |
} |
813 |
+ |
if (strchr(argv[++i], '>') != NULL) { |
814 |
+ |
fprintf(stderr, "%s: illegal character in comment\n", |
815 |
+ |
argv[0]); |
816 |
+ |
return 1; |
817 |
+ |
} |
818 |
+ |
commlist[ncomm++] = argv[i]; |
819 |
+ |
continue; |
820 |
|
case 't': /* transmission */ |
821 |
|
if (i >= argc-1) |
822 |
|
UsageExit(argv[0]); |
858 |
|
argv[i] = (char *)stdin_name; |
859 |
|
} |
860 |
|
data_file[ndataf].fname = argv[i]; |
861 |
< |
data_file[ndataf++].spectrum = cur_spectrum; |
861 |
> |
data_file[ndataf].spectrum = cur_spectrum; |
862 |
> |
ndataf++; |
863 |
|
continue; |
864 |
|
case 's': /* spectrum name or input file */ |
865 |
|
if (++i >= argc) |
873 |
|
cur_spectrum = DSxbar31; |
874 |
|
else if (!strcasecmp(argv[i], "CIE-Z")) |
875 |
|
cur_spectrum = DSzbar31; |
876 |
+ |
else if (!strcasecmp(argv[i], "CIE-u")) |
877 |
+ |
cur_spectrum = DSuprime; |
878 |
+ |
else if (!strcasecmp(argv[i], "CIE-v")) |
879 |
+ |
cur_spectrum = DSvprime; |
880 |
|
else if (!strcasecmp(argv[i], "NIR")) |
881 |
|
cur_spectrum = DSnir; |
882 |
|
else { |
925 |
|
} else { |
926 |
|
xml_input = argv[i]; |
927 |
|
} |
873 |
– |
if ((xml_input == win6_template) & (angle_basis == ABdefault)) |
874 |
– |
angle_basis = ABklemsFull; |
928 |
|
/* wrap it! */ |
929 |
|
return !wrapBSDF(argv[0]); |
930 |
|
} |