7 |
|
* G. Ward February 2015 |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "platform.h" |
11 |
|
#include <ctype.h> |
12 |
|
#include "rtio.h" |
13 |
< |
#include "rtprocess.h" |
13 |
> |
#include "paths.h" |
14 |
|
#include "ezxml.h" |
15 |
|
#include "bsdf.h" |
16 |
|
#include "bsdf_m.h" |
26 |
|
const char legal_units[] = "meter|foot|inch|centimeter|millimeter"; |
27 |
|
/* system materials & geometry */ |
28 |
|
const char *mgf_geometry = NULL; |
29 |
< |
|
29 |
> |
/* comment list */ |
30 |
> |
#define MAXCOMM 30 |
31 |
> |
const char *commlist[MAXCOMM]; |
32 |
> |
int ncomm = 0; |
33 |
|
/* angle bases */ |
34 |
|
enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter, |
35 |
|
ABtensorTree3, ABtensorTree4, ABend }; |
70 |
|
/* data file(s) & spectra */ |
71 |
|
enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward }; |
72 |
|
|
73 |
< |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 }; |
73 |
> |
enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5, |
74 |
> |
DSuprime=-6, DSvprime=-7 }; |
75 |
|
|
76 |
|
#define MAXFILES 20 |
77 |
|
|
83 |
|
|
84 |
|
int ndataf = 0; /* number of data files */ |
85 |
|
|
86 |
+ |
int unlink_datafiles = 0; /* unlink data files when done */ |
87 |
+ |
|
88 |
|
const char *spectr_file[MAXFILES]; /* custom spectral curve input */ |
89 |
|
|
90 |
|
const char top_level_name[] = "WindowElement"; |
166 |
|
fprintf(stderr, "%s: cannot open\n", inpspec); |
167 |
|
return ""; |
168 |
|
} |
169 |
+ |
#if !defined(_WIN32) && !defined(_WIN64) |
170 |
+ |
/* XXX somehow broken on Windows */ |
171 |
|
len = lseek(fd, 0L, SEEK_END); |
172 |
|
if (len > 0) { |
173 |
|
lseek(fd, 0L, SEEK_SET); |
186 |
|
close(fd); |
187 |
|
return str; |
188 |
|
} |
189 |
+ |
#endif |
190 |
|
fp = fdopen(fd, "r"); /* not a regular file */ |
191 |
|
} |
192 |
|
/* reading from stream */ |
304 |
|
if (xml_input == win6_template) |
305 |
|
for (i = 0; XMLfieldID[i].nickName[0]; i++) |
306 |
|
if (XMLfieldID[i].win_need && |
307 |
< |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) { |
307 |
> |
!ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) |
308 |
|
fprintf(stderr, |
309 |
< |
"%s: missing required '%s' assignment for WINDOW <%s>\n", |
309 |
> |
"%s: warning - missing '%s' assignment for WINDOW <%s>\n", |
310 |
|
caller, XMLfieldID[i].nickName, |
311 |
|
XMLfieldID[i].fullName); |
312 |
< |
return 0; |
303 |
< |
} |
304 |
< |
return 1; /* no errors */ |
312 |
> |
return 1; |
313 |
|
} |
314 |
|
|
315 |
|
/* Complete angle basis specification */ |
395 |
|
static int |
396 |
|
filter_klems_matrix(FILE *fp) |
397 |
|
{ |
390 |
– |
#define MAX_COLUMNS 145 |
398 |
|
const char *bn = klems_basis_name[angle_basis]; |
392 |
– |
float col_corr[MAX_COLUMNS]; |
399 |
|
int i, j, n = nabases; |
400 |
|
/* get angle basis */ |
401 |
|
while (n-- > 0) |
403 |
|
break; |
404 |
|
if (n < 0) |
405 |
|
return 0; |
400 |
– |
if (abase_list[n].nangles > MAX_COLUMNS) { |
401 |
– |
fputs("Internal error - too many Klems columns!\n", stderr); |
402 |
– |
return 0; |
403 |
– |
} |
404 |
– |
/* get correction factors */ |
405 |
– |
for (j = abase_list[n].nangles; j--; ) |
406 |
– |
col_corr[j] = 1.f / io_getohm(j, &abase_list[n]); |
406 |
|
/* read/correct/write matrix */ |
407 |
|
for (i = 0; i < abase_list[n].nangles; i++) { |
408 |
+ |
const double corr = 1./io_getohm(i, &abase_list[n]); |
409 |
|
for (j = 0; j < abase_list[n].nangles; j++) { |
410 |
|
double d; |
411 |
|
if (fscanf(fp, "%lf", &d) != 1) |
414 |
|
fputs("Negative BSDF data!\n", stderr); |
415 |
|
return 0; |
416 |
|
} |
417 |
< |
printf(" %.3e", d*col_corr[j]*(d > 0)); |
417 |
> |
printf(" %.3e", d*corr*(d > 0)); |
418 |
|
} |
419 |
|
fputc('\n', stdout); |
420 |
|
} |
424 |
|
return 0; |
425 |
|
} |
426 |
|
return 1; /* all is good */ |
427 |
– |
#undef MAX_COLUMNS |
427 |
|
} |
428 |
|
|
429 |
|
/* Write out BSDF data block with surrounding tags */ |
443 |
|
break; |
444 |
|
case DSxbar31: |
445 |
|
puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>"); |
446 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
446 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
447 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>"); |
448 |
|
break; |
449 |
|
case DSzbar31: |
450 |
|
puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>"); |
451 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
451 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
452 |
|
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>"); |
453 |
|
break; |
454 |
+ |
case DSuprime: |
455 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-u</Wavelength>"); |
456 |
+ |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
457 |
+ |
break; |
458 |
+ |
case DSvprime: |
459 |
+ |
puts("\t\t<Wavelength unit=\"Integral\">CIE-v</Wavelength>"); |
460 |
+ |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
461 |
+ |
break; |
462 |
|
case DSsolar: |
463 |
|
puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>"); |
464 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
464 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
465 |
|
puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>"); |
466 |
|
break; |
467 |
|
case DSnir: |
468 |
|
puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>"); |
469 |
< |
puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>"); |
469 |
> |
puts("\t\t<SourceSpectrum>PLACE_HOLDER</SourceSpectrum>"); |
470 |
|
puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>"); |
471 |
|
break; |
472 |
|
default: |
477 |
|
spectr_file[df->spectrum]); |
478 |
|
if (cp != NULL) |
479 |
|
*cp = '.'; |
480 |
< |
puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
480 |
> |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
481 |
|
printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n", |
482 |
|
spectr_file[df->spectrum]); |
483 |
|
break; |
583 |
|
free(xml); |
584 |
|
return 0; |
585 |
|
} |
586 |
+ |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
587 |
+ |
for (i = 0; i < ncomm; i++) |
588 |
+ |
printf("<!-- %s -->\n", commlist[i]); |
589 |
|
fflush(stdout); /* write previous XML info. */ |
590 |
|
if (write(fileno(stdout), xml, ei) != ei) { |
591 |
|
free(xml); |
599 |
|
fputs(xml+ei, stdout); /* write trailer */ |
600 |
|
free(xml); /* free string */ |
601 |
|
fputc('\n', stdout); |
602 |
< |
return (fflush(stdout) == 0); |
602 |
> |
if (fflush(stdout) != 0) |
603 |
> |
return 0; |
604 |
> |
/* unlink data files if req. */ |
605 |
> |
for (i = ndataf*(unlink_datafiles != 0); i--; ) |
606 |
> |
if (data_file[i].fname != stdin_name && |
607 |
> |
data_file[i].fname[0] != '!') |
608 |
> |
unlink(data_file[i].fname); |
609 |
> |
if (unlink_datafiles > 1 && mgf_geometry != NULL && |
610 |
> |
mgf_geometry != stdin_name && |
611 |
> |
mgf_geometry[0] != '!') |
612 |
> |
unlink(mgf_geometry); |
613 |
> |
return 1; |
614 |
|
} |
615 |
|
|
616 |
|
/* Insert BSDF data into XML wrapper */ |
728 |
|
{ |
729 |
|
fputs("Usage: ", stderr); |
730 |
|
fputs(pname, stderr); |
731 |
< |
fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
732 |
< |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr); |
731 |
> |
fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
732 |
> |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr); |
733 |
|
fputs(" [input.xml]\n", stderr); |
734 |
|
exit(1); |
735 |
|
} |
774 |
|
} |
775 |
|
attr_unit = argv[i]; |
776 |
|
continue; |
777 |
+ |
case 'U': /* unlink data files when done */ |
778 |
+ |
unlink_datafiles = 1 + (argv[i][2] == 'U'); |
779 |
+ |
continue; |
780 |
|
case 'a': /* angle basis */ |
781 |
|
if (++i >= argc) |
782 |
|
UsageExit(argv[0]); |
799 |
|
UsageExit(argv[0]); |
800 |
|
continue; |
801 |
|
case 'c': /* correct solid angle */ |
802 |
< |
correct_solid_angle ^= 1; |
802 |
> |
correct_solid_angle = 1; |
803 |
|
continue; |
804 |
+ |
case 'C': /* comment */ |
805 |
+ |
if (ncomm >= MAXCOMM) { |
806 |
+ |
fprintf(stderr, "%s: too many comments\n", |
807 |
+ |
argv[0]); |
808 |
+ |
return 1; |
809 |
+ |
} |
810 |
+ |
if (strchr(argv[++i], '>') != NULL) { |
811 |
+ |
fprintf(stderr, "%s: illegal character in comment\n", |
812 |
+ |
argv[0]); |
813 |
+ |
return 1; |
814 |
+ |
} |
815 |
+ |
commlist[ncomm++] = argv[i]; |
816 |
+ |
continue; |
817 |
|
case 't': /* transmission */ |
818 |
|
if (i >= argc-1) |
819 |
|
UsageExit(argv[0]); |
855 |
|
argv[i] = (char *)stdin_name; |
856 |
|
} |
857 |
|
data_file[ndataf].fname = argv[i]; |
858 |
< |
data_file[ndataf++].spectrum = cur_spectrum; |
858 |
> |
data_file[ndataf].spectrum = cur_spectrum; |
859 |
> |
ndataf++; |
860 |
|
continue; |
861 |
|
case 's': /* spectrum name or input file */ |
862 |
|
if (++i >= argc) |
870 |
|
cur_spectrum = DSxbar31; |
871 |
|
else if (!strcasecmp(argv[i], "CIE-Z")) |
872 |
|
cur_spectrum = DSzbar31; |
873 |
+ |
else if (!strcasecmp(argv[i], "CIE-u")) |
874 |
+ |
cur_spectrum = DSuprime; |
875 |
+ |
else if (!strcasecmp(argv[i], "CIE-v")) |
876 |
+ |
cur_spectrum = DSvprime; |
877 |
|
else if (!strcasecmp(argv[i], "NIR")) |
878 |
|
cur_spectrum = DSnir; |
879 |
|
else { |
922 |
|
} else { |
923 |
|
xml_input = argv[i]; |
924 |
|
} |
883 |
– |
if ((xml_input == win6_template) & (angle_basis == ABdefault)) |
884 |
– |
angle_basis = ABklemsFull; |
925 |
|
/* wrap it! */ |
926 |
|
return !wrapBSDF(argv[0]); |
927 |
|
} |