--- ray/src/util/wrapBSDF.c 2015/02/14 00:39:21 2.4 +++ ray/src/util/wrapBSDF.c 2019/04/03 23:50:25 2.23 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: wrapBSDF.c,v 2.4 2015/02/14 00:39:21 greg Exp $"; +static const char RCSid[] = "$Id: wrapBSDF.c,v 2.23 2019/04/03 23:50:25 greg Exp $"; #endif /* * Wrap BSDF data in valid WINDOW XML file @@ -7,9 +7,10 @@ static const char RCSid[] = "$Id: wrapBSDF.c,v 2.4 201 * G. Ward February 2015 */ +#include "platform.h" #include #include "rtio.h" -#include "rtprocess.h" +#include "paths.h" #include "ezxml.h" #include "bsdf.h" #include "bsdf_m.h" @@ -18,7 +19,6 @@ const char def_template[] = "minimalBSDFt.xml"; const char win6_template[] = "WINDOW6BSDFt.xml"; const char stdin_name[] = ""; - /* input files (can be stdin_name) */ const char *xml_input = NULL; /* unit for materials & geometry */ @@ -26,13 +26,23 @@ const char *attr_unit = "meter"; const char legal_units[] = "meter|foot|inch|centimeter|millimeter"; /* system materials & geometry */ const char *mgf_geometry = NULL; - - /* angle basis */ + /* comment list */ +#define MAXCOMM 30 +const char *commlist[MAXCOMM]; +int ncomm = 0; + /* angle bases */ enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter, ABtensorTree3, ABtensorTree4, ABend }; int angle_basis = ABdefault; +int correct_solid_angle = 0; + +const char *klems_basis_name[] = { + "LBNL/Klems Full", + "LBNL/Klems Half", + "LBNL/Klems Quarter", +}; /* field IDs and nicknames */ struct s_fieldID { char nickName[4]; @@ -42,6 +52,7 @@ struct s_fieldID { } XMLfieldID[] = { {"m", 0, 1, "Manufacturer"}, {"n", 0, 1, "Name"}, + {"d", 0, 0, "DeviceType"}, {"c", 0, 0, "ThermalConductivity"}, {"ef", 0, 0, "EmissivityFront"}, {"eb", 0, 0, "EmissivityBack"}, @@ -53,15 +64,15 @@ struct s_fieldID { {"\0", 0, 0, NULL} /* terminator */ }; /* field assignments */ -#define MAXASSIGN 12 +#define MAXASSIGN 16 const char *field_assignment[MAXASSIGN]; int nfield_assign = 0; #define FASEP ';' - /* data file(s) & spectra */ enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward }; -enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 }; +enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5, + DSuprime=-6, DSvprime=-7 }; #define MAXFILES 20 @@ -73,6 +84,8 @@ struct s_dfile { int ndataf = 0; /* number of data files */ +int unlink_datafiles = 0; /* unlink data files when done */ + const char *spectr_file[MAXFILES]; /* custom spectral curve input */ const char top_level_name[] = "WindowElement"; @@ -109,6 +122,23 @@ static char basis_definition[][256] = { "\t\n", }; +/* Check that the last-added data file is unique */ +static int +check_new_data_file() +{ + int i = ndataf; + + while (i-- > 0) + if ((data_file[i].spectrum == data_file[ndataf].spectrum) & + (data_file[i].type == data_file[ndataf].type)) { + fprintf(stderr, + "%s: warning - ignoring duplicate component\n", + data_file[ndataf].fname); + return 0; + } + return 1; +} + /* Copy data from file descriptor to stdout and close */ static int copy_and_close(int fd) @@ -154,6 +184,8 @@ input2str(const char *inpspec) fprintf(stderr, "%s: cannot open\n", inpspec); return ""; } +#if !defined(_WIN32) && !defined(_WIN64) + /* XXX somehow broken on Windows */ len = lseek(fd, 0L, SEEK_END); if (len > 0) { lseek(fd, 0L, SEEK_SET); @@ -172,6 +204,7 @@ input2str(const char *inpspec) close(fd); return str; } +#endif fp = fdopen(fd, "r"); /* not a regular file */ } /* reading from stream */ @@ -221,6 +254,7 @@ mat_assignments(const char *caller, const char *fn, ez for (i = 0; i < nfield_assign; i++) { const char *fnext = field_assignment[i]; for ( ; ; ) { + int added = 0; ezxml_t fld; char sbuf[512]; int j; @@ -253,7 +287,9 @@ mat_assignments(const char *caller, const char *fn, ez fprintf(stderr, "%s: warning - adding tag <%s>\n", fn, sbuf); + ezxml_add_txt(wtl, "\t"); fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt)); + ++added; } if (XMLfieldID[j].has_unit) ezxml_set_attr(fld, "unit", attr_unit); @@ -277,6 +313,8 @@ mat_assignments(const char *caller, const char *fn, ez } sbuf[j] = '\0'; ezxml_set_txt_d(fld, sbuf); + if (added) + ezxml_add_txt(wtl, "\n\t"); fnext += (*fnext == FASEP); } } @@ -284,14 +322,12 @@ mat_assignments(const char *caller, const char *fn, ez if (xml_input == win6_template) for (i = 0; XMLfieldID[i].nickName[0]; i++) if (XMLfieldID[i].win_need && - !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) { + !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) fprintf(stderr, - "%s: missing required '%s' assignment for WINDOW <%s>\n", + "%s: warning - missing '%s' assignment for WINDOW <%s>\n", caller, XMLfieldID[i].nickName, XMLfieldID[i].fullName); - return 0; - } - return 1; /* no errors */ + return 1; } /* Complete angle basis specification */ @@ -373,10 +409,46 @@ determine_angle_basis(const char *fn, ezxml_t wtl) return -1; } +/* Filter Klems angle basis, factoring out incident projected solid angle */ +static int +filter_klems_matrix(FILE *fp) +{ + const char *bn = klems_basis_name[angle_basis]; + int i, j, n = nabases; + /* get angle basis */ + while (n-- > 0) + if (!strcasecmp(bn, abase_list[n].name)) + break; + if (n < 0) + return 0; + /* read/correct/write matrix */ + for (i = 0; i < abase_list[n].nangles; i++) { + const double corr = 1./io_getohm(i, &abase_list[n]); + for (j = 0; j < abase_list[n].nangles; j++) { + double d; + if (fscanf(fp, "%lf", &d) != 1) + return 0; + if (d < -1e-3) { + fputs("Negative BSDF data!\n", stderr); + return 0; + } + printf(" %.3e", d*corr*(d > 0)); + } + fputc('\n', stdout); + } + while ((i = getc(fp)) != EOF) + if (!isspace(i)) { + fputs("Unexpected data past EOF\n", stderr); + return 0; + } + return 1; /* all is good */ +} + /* Write out BSDF data block with surrounding tags */ static int writeBSDFblock(const char *caller, struct s_dfile *df) { + int correct_klems = correct_solid_angle; char *cp; puts("\t"); @@ -384,27 +456,35 @@ writeBSDFblock(const char *caller, struct s_dfile *df) switch (df->spectrum) { case DSvisible: puts("\t\tVisible"); - puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); puts("\t\tASTM E308 1931 Y.dsp"); break; case DSxbar31: puts("\t\tCIE-X"); - puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); puts("\t\tASTM E308 1931 X.dsp"); break; case DSzbar31: puts("\t\tCIE-Z"); - puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); puts("\t\tASTM E308 1931 Z.dsp"); break; + case DSuprime: + puts("\t\tCIE-u"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); + break; + case DSvprime: + puts("\t\tCIE-v"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); + break; case DSsolar: puts("\t\tSolar"); - puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); puts("\t\tNone"); break; case DSnir: puts("\t\tNIR"); - puts("\t\tSourceSpectrum>PLACE_HOLDER"); + puts("\t\tPLACE_HOLDER"); puts("\t\tPLACE_HOLDER"); break; default: @@ -415,7 +495,7 @@ writeBSDFblock(const char *caller, struct s_dfile *df) spectr_file[df->spectrum]); if (cp != NULL) *cp = '.'; - puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); printf("\t\t%s\n", spectr_file[df->spectrum]); break; @@ -442,17 +522,19 @@ writeBSDFblock(const char *caller, struct s_dfile *df) puts(""); switch (angle_basis) { case ABklemsFull: - puts("\t\t\tLBNL/Klems Full"); - break; case ABklemsHalf: - puts("\t\t\tLBNL/Klems Half"); - break; case ABklemsQuarter: - puts("\t\t\tLBNL/Klems Quarter"); + fputs("\t\t\t", stdout); + fputs(klems_basis_name[angle_basis], stdout); + puts(""); + fputs("\t\t\t", stdout); + fputs(klems_basis_name[angle_basis], stdout); + puts(""); break; case ABtensorTree3: case ABtensorTree4: puts("\t\t\tLBNL/Shirley-Chiu"); + correct_klems = 0; break; default: fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis); @@ -461,7 +543,30 @@ writeBSDFblock(const char *caller, struct s_dfile *df) puts("\t\t\tBTDF"); puts("\t\t\t"); fflush(stdout); - if (df->fname == stdin_name) { + if (correct_klems) { /* correct Klems matrix data */ + FILE *fp = stdin; + if (df->fname[0] == '!') + fp = popen(df->fname+1, "r"); + else if (df->fname != stdin_name) + fp = fopen(df->fname, "r"); + if (fp == NULL) { + fprintf(stderr, "%s: cannot open '%s'\n", + caller, df->fname); + return 0; + } + if (!filter_klems_matrix(fp)) { + fprintf(stderr, "%s: Klems data error from '%s'\n", + caller, df->fname); + return 0; + } + if (df->fname[0] != '!') { + fclose(fp); + } else if (pclose(fp)) { + fprintf(stderr, "%s: error running '%s'\n", + caller, df->fname); + return 0; + } + } else if (df->fname == stdin_name) { copy_and_close(fileno(stdin)); } else if (df->fname[0] != '!') { if (!copy_and_close(open(df->fname, O_RDONLY))) { @@ -496,6 +601,9 @@ writeBSDF(const char *caller, ezxml_t fl) free(xml); return 0; } + puts(""); + for (i = 0; i < ncomm; i++) + printf("\n", commlist[i]); fflush(stdout); /* write previous XML info. */ if (write(fileno(stdout), xml, ei) != ei) { free(xml); @@ -509,7 +617,18 @@ writeBSDF(const char *caller, ezxml_t fl) fputs(xml+ei, stdout); /* write trailer */ free(xml); /* free string */ fputc('\n', stdout); - return (fflush(stdout) == 0); + if (fflush(stdout) != 0) + return 0; + /* unlink data files if req. */ + for (i = ndataf*(unlink_datafiles != 0); i--; ) + if (data_file[i].fname != stdin_name && + data_file[i].fname[0] != '!') + unlink(data_file[i].fname); + if (unlink_datafiles > 1 && mgf_geometry != NULL && + mgf_geometry != stdin_name && + mgf_geometry[0] != '!') + unlink(mgf_geometry); + return 1; } /* Insert BSDF data into XML wrapper */ @@ -593,7 +712,8 @@ wrapBSDF(const char *caller) ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition"); if (dd != NULL) { offset = dd->off; - fprintf(stderr, + if (dd->child != NULL) + fprintf(stderr, "%s: warning - replacing existing in '%s'\n", caller, xml_path); ezxml_remove(dd); @@ -626,8 +746,8 @@ UsageExit(const char *pname) { fputs("Usage: ", stderr); fputs(pname, stderr); - fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); - fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr); + fputs(" [-W][-c][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); + fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp][-C comm]", stderr); fputs(" [input.xml]\n", stderr); exit(1); } @@ -672,6 +792,9 @@ main(int argc, char *argv[]) } attr_unit = argv[i]; continue; + case 'U': /* unlink data files when done */ + unlink_datafiles = 1 + (argv[i][2] == 'U'); + continue; case 'a': /* angle basis */ if (++i >= argc) UsageExit(argv[0]); @@ -693,6 +816,22 @@ main(int argc, char *argv[]) else UsageExit(argv[0]); continue; + case 'c': /* correct solid angle */ + correct_solid_angle = 1; + continue; + case 'C': /* comment */ + if (ncomm >= MAXCOMM) { + fprintf(stderr, "%s: too many comments\n", + argv[0]); + return 1; + } + if (strchr(argv[++i], '>') != NULL) { + fprintf(stderr, "%s: illegal character in comment\n", + argv[0]); + return 1; + } + commlist[ncomm++] = argv[i]; + continue; case 't': /* transmission */ if (i >= argc-1) UsageExit(argv[0]); @@ -713,7 +852,7 @@ main(int argc, char *argv[]) } data_file[ndataf].fname = argv[i]; data_file[ndataf].spectrum = cur_spectrum; - ndataf++; + ndataf += check_new_data_file(); continue; case 'r': /* reflection */ if (i >= argc-1) @@ -734,7 +873,8 @@ main(int argc, char *argv[]) argv[i] = (char *)stdin_name; } data_file[ndataf].fname = argv[i]; - data_file[ndataf++].spectrum = cur_spectrum; + data_file[ndataf].spectrum = cur_spectrum; + ndataf++; continue; case 's': /* spectrum name or input file */ if (++i >= argc) @@ -748,6 +888,10 @@ main(int argc, char *argv[]) cur_spectrum = DSxbar31; else if (!strcasecmp(argv[i], "CIE-Z")) cur_spectrum = DSzbar31; + else if (!strcasecmp(argv[i], "CIE-u")) + cur_spectrum = DSuprime; + else if (!strcasecmp(argv[i], "CIE-v")) + cur_spectrum = DSvprime; else if (!strcasecmp(argv[i], "NIR")) cur_spectrum = DSnir; else { @@ -796,8 +940,6 @@ doneOptions: /* get XML input */ } else { xml_input = argv[i]; } - if ((xml_input == win6_template) & (angle_basis == ABdefault)) - angle_basis = ABklemsFull; /* wrap it! */ return !wrapBSDF(argv[0]); }