--- ray/src/cv/bsdf2ttree.c 2020/10/28 18:54:21 2.55 +++ ray/src/cv/bsdf2ttree.c 2023/07/26 15:50:03 2.59 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.55 2020/10/28 18:54:21 greg Exp $"; +static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.59 2023/07/26 15:50:03 greg Exp $"; #endif /* * Load measured BSDF interpolant and write out as XML file with tensor tree. @@ -135,7 +135,7 @@ eval_isotropic(char *funame) int assignD = 0; char cmd[128]; int ix, ox, oy; - double iovec[6]; + RREAL iovec[6]; float bsdf, uv[2]; if (pctcull >= 0) { @@ -208,7 +208,7 @@ eval_isotropic(char *funame) rbf = advect_rbf(iovec, lobe_lim); /* presample first row */ for (oy = 0; oy < sqres; oy++) { - SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); + square2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); iovec[5] = output_orient * sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); if (funame == NULL) { @@ -230,7 +230,7 @@ eval_isotropic(char *funame) */ for (oy = 0; oy < sqres; oy++) { if (ox < sqres-1) { /* keeping one row ahead... */ - SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); + square2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); iovec[5] = output_orient * sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); } @@ -250,7 +250,7 @@ eval_isotropic(char *funame) for (ssi = nssamp; ssi--; ) { SDmultiSamp(ssa, 2, (ssi+frandom()) / (double)nssamp); - SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, + square2disk(iovec+3, (ox+ssa[0])*sqfact, (oy+ssa[1])*sqfact); iovec[5] = output_orient * sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); @@ -268,8 +268,7 @@ eval_isotropic(char *funame) uv[0] = usum / (sum+FTINY); uv[1] = vsum / (sum+FTINY); } - } else - if (rbf_colorimetry == RBCtristimulus) { + } else if (rbf_colorimetry == RBCtristimulus) { uv[0] = uv[1] = 1. / (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); uv[0] *= 4.*sdv.spec.cx; @@ -306,7 +305,7 @@ eval_isotropic(char *funame) ssvec[2] = 1. - ssvec[0]*ssvec[0]; } ssvec[2] = input_orient * sqrt(ssvec[2]); - SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, + square2disk(ssvec+3, (ox+ssa[2])*sqfact, (oy+ssa[3])*sqfact); ssvec[5] = output_orient * sqrt(1. - ssvec[3]*ssvec[3] - @@ -400,7 +399,7 @@ eval_anisotropic(char *funame) int assignD = 0; char cmd[128]; int ix, iy, ox, oy; - double iovec[6]; + RREAL iovec[6]; float bsdf, uv[2]; if (pctcull >= 0) { @@ -470,14 +469,14 @@ eval_anisotropic(char *funame) for (ix = 0; ix < sqres; ix++) for (iy = 0; iy < sqres; iy++) { RBFNODE *rbf = NULL; /* Klems reversal */ - SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); + square2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); if (funame == NULL) rbf = advect_rbf(iovec, lobe_lim); /* presample first row */ for (oy = 0; oy < sqres; oy++) { - SDsquare2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); + square2disk(iovec+3, .5*sqfact, (oy+.5)*sqfact); iovec[5] = output_orient * sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); if (funame == NULL) { @@ -499,7 +498,7 @@ eval_anisotropic(char *funame) */ for (oy = 0; oy < sqres; oy++) { if (ox < sqres-1) { /* keeping one row ahead... */ - SDsquare2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); + square2disk(iovec+3, (ox+1.5)*sqfact, (oy+.5)*sqfact); iovec[5] = output_orient * sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); } @@ -519,7 +518,7 @@ eval_anisotropic(char *funame) for (ssi = nssamp; ssi--; ) { SDmultiSamp(ssa, 2, (ssi+frandom()) / (double)nssamp); - SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, + square2disk(iovec+3, (ox+ssa[0])*sqfact, (oy+ssa[1])*sqfact); iovec[5] = output_orient * sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); @@ -537,8 +536,7 @@ eval_anisotropic(char *funame) uv[0] = usum / (sum+FTINY); uv[1] = vsum / (sum+FTINY); } - } else - if (rbf_colorimetry == RBCtristimulus) { + } else if (rbf_colorimetry == RBCtristimulus) { uv[0] = uv[1] = 1. / (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); uv[0] *= 4.*sdv.spec.cx; @@ -566,12 +564,12 @@ eval_anisotropic(char *funame) for (ssi = nssamp; ssi--; ) { SDmultiSamp(ssa, 4, (ssi+frandom()) / (double)nssamp); - SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, + square2disk(ssvec, 1.-(ix+ssa[0])*sqfact, 1.-(iy+ssa[1])*sqfact); ssvec[2] = input_orient * sqrt(1. - ssvec[0]*ssvec[0] - ssvec[1]*ssvec[1]); - SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, + square2disk(ssvec+3, (ox+ssa[2])*sqfact, (oy+ssa[3])*sqfact); ssvec[5] = output_orient * sqrt(1. - ssvec[3]*ssvec[3] - @@ -702,7 +700,7 @@ wrap_up(void) } #endif -#define HEAD_BUFLEN 8192 +#define HEAD_BUFLEN 10240 static char head_buf[HEAD_BUFLEN]; static int cur_headlen = 0; @@ -743,25 +741,31 @@ done_header(void) int main(int argc, char *argv[]) { - static char tfmt[2][4] = {"t4", "t3"}; - int dofwd = 0, dobwd = 1; - char buf[2048]; - int i, na; + static const char tfmt[2][4] = {"t4", "t3"}; + int dofwd = 0, dobwd = 1; + int nsirs = 0; + char buf[1024]; + int i; progname = argv[0]; esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; esupport &= ~(E_INCHAN|E_OUTCHAN); scompile("PI:3.14159265358979323846", NULL, 0); biggerlib(); - for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) - switch (argv[i][1]) { /* get options */ + strcpy(buf, "File produced by: "); + if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { + add_wbsdf("-C", 1); add_wbsdf(buf, 0); + } + for (i = 1; i < argc; i++) + if ((argv[i][0] == '-') | (argv[i][0] == '+')) { + switch (argv[i][1]) { /* get option */ case 'e': scompile(argv[++i], NULL, 0); if (single_plane_incident < 0) single_plane_incident = 0; break; case 'f': - if (!argv[i][2]) { + if ((argv[i][0] == '-') & !argv[i][2]) { if (strchr(argv[++i], '=') != NULL) { add_wbsdf("-f", 1); add_wbsdf(argv[i], 1); @@ -832,11 +836,39 @@ main(int argc, char *argv[]) default: goto userr; } - strcpy(buf, "File produced by: "); - if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { - add_wbsdf("-C", 1); add_wbsdf(buf, 0); - } - if (single_plane_incident >= 0) { /* function-based BSDF? */ + } else { /* input SIR or function */ + FILE *fpin; + if (!nsirs & (single_plane_incident >= 0)) + break; /* must be a function */ + if (nsirs >= 4) { + fprintf(stderr, "At most 4 SIR inputs supported\n"); + goto userr; + } + fpin = fopen(argv[i], "rb"); /* open SIR file */ + if (fpin == NULL) { + fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", + progname, argv[i]); + return(1); + } + sprintf(buf, "%s:\n", argv[i]); + record2header(buf); + sir_headshare = &record2header; + if (!load_bsdf_rep(fpin)) + return(1); + fclose(fpin); + done_header(); + sprintf(buf, "Interpolating component '%s'", argv[i]); + prog_start(buf); + if (!nsirs++) { + add_wbsdf("-a", 1); + add_wbsdf(tfmt[single_plane_incident], 1); + } + if (single_plane_incident) + eval_isotropic(NULL); + else + eval_anisotropic(NULL); + } + if (i < argc) { /* function-based BSDF? */ void (*evf)(char *s) = single_plane_incident ? eval_isotropic : eval_anisotropic; if (i != argc-1 || fundefined(argv[i]) < 3) { @@ -867,53 +899,24 @@ main(int argc, char *argv[]) prog_start("Evaluating inside->outside transmission"); (*evf)(argv[i]); } - return(wrap_up()); + } else if (!nsirs) { /* else load SIR from stdin? */ + record2header(":\n"); + sir_headshare = &record2header; + if (!load_bsdf_rep(stdin)) + return(1); + done_header(); + prog_start("Interpolating from standard input"); + add_wbsdf("-a", 1); + add_wbsdf(tfmt[single_plane_incident], 1); + if (single_plane_incident) /* resample dist. */ + eval_isotropic(NULL); + else + eval_anisotropic(NULL); } - if (i < argc) { /* open input files if given */ - int nbsdf = 0; - for ( ; i < argc; i++) { /* interpolate each component */ - char pbuf[256]; - FILE *fpin = fopen(argv[i], "rb"); - if (fpin == NULL) { - fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", - progname, argv[i]); - return(1); - } - sprintf(pbuf, "%s:\n", argv[i]); - record2header(pbuf); - sir_headshare = &record2header; - if (!load_bsdf_rep(fpin)) - return(1); - fclose(fpin); - done_header(); - sprintf(pbuf, "Interpolating component '%s'", argv[i]); - prog_start(pbuf); - if (!nbsdf++) { - add_wbsdf("-a", 1); - add_wbsdf(tfmt[single_plane_incident], 1); - } - if (single_plane_incident) - eval_isotropic(NULL); - else - eval_anisotropic(NULL); - } - return(wrap_up()); - } - SET_FILE_BINARY(stdin); /* load from stdin */ - if (!load_bsdf_rep(stdin)) - return(1); - prog_start("Interpolating from standard input"); - add_wbsdf("-a", 1); - add_wbsdf(tfmt[single_plane_incident], 1); - if (single_plane_incident) /* resample dist. */ - eval_isotropic(NULL); - else - eval_anisotropic(NULL); - - return(wrap_up()); + return(wrap_up()); /* call wrapBSDF */ userr: fprintf(stderr, - "Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", + "Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][-l maxlobes][bsdf.sir ..] > bsdf.xml\n", progname); fprintf(stderr, " or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][-n nss][-s thresh][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",