--- ray/src/cv/bsdf2ttree.c 2020/05/06 02:28:21 2.46 +++ ray/src/cv/bsdf2ttree.c 2020/10/26 21:28:18 2.54 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.46 2020/05/06 02:28:21 greg Exp $"; +static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.54 2020/10/26 21:28:18 greg Exp $"; #endif /* * Load measured BSDF interpolant and write out as XML file with tensor tree. @@ -10,6 +10,7 @@ static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.46 #define _USE_MATH_DEFINES #include #include +#include #include "random.h" #include "platform.h" #include "paths.h" @@ -25,11 +26,9 @@ static double pctcull = 90.; /* sampling order */ static int samp_order = 6; /* super-sampling threshold */ -const double ssamp_thresh = 0.25; +static double ssamp_thresh = 0.35; /* number of super-samples */ -#ifndef NSSAMP -#define NSSAMP 256 -#endif +static int nssamp = 256; /* limit on number of RBF lobes */ static int lobe_lim = 15000; /* progress bar length */ @@ -129,6 +128,9 @@ eval_isotropic(char *funame) { const int sqres = 1< 0) - if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { + if (ox < sqres-1) + eval_rbfcol(&sdv_next[oy], rbf, iovec+3); + if (abs_diff(bsdf, sdv_next[oy].cieY) > ssamp_thresh || + (ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) || + (oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) || + (oy < sqres-1 && + abs_diff(bsdf, sdv_next[oy+1].cieY) > ssamp_thresh)) { int ssi; double ssa[2], sum = 0, usum = 0, vsum = 0; /* super-sample voxel */ - for (ssi = NSSAMP; ssi--; ) { - SDmultiSamp(ssa, 2, (ssi+frandom()) * - (1./NSSAMP)); + for (ssi = nssamp; ssi--; ) { + SDmultiSamp(ssa, 2, (ssi+frandom()) / + (double)nssamp); SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, (oy+ssa[1])*sqfact); iovec[5] = output_orient * @@ -455,13 +532,12 @@ eval_anisotropic(char *funame) vsum += 9.*sdv.spec.cy * sdv.cieY; } } - bsdf = sum * (1./NSSAMP); + bsdf = sum / (double)nssamp; if (rbf_colorimetry == RBCtristimulus) { uv[0] = usum / (sum+FTINY); uv[1] = vsum / (sum+FTINY); } } else -#endif if (rbf_colorimetry == RBCtristimulus) { uv[0] = uv[1] = 1. / (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); @@ -469,21 +545,27 @@ eval_anisotropic(char *funame) uv[1] *= 9.*sdv.spec.cy; } } else { - if (assignD) { - varset("Dx", '=', -iovec[3]); - varset("Dy", '=', -iovec[4]); - varset("Dz", '=', -iovec[5]); - ++eclock; + bsdf = val_next[oy]; + if (ox < sqres-1) { + if (assignD) { + varset("Dx", '=', -iovec[3]); + varset("Dy", '=', -iovec[4]); + varset("Dz", '=', -iovec[5]); + ++eclock; + } + val_next[oy] = funvalue(funame, 6, iovec); } - bsdf = funvalue(funame, 6, iovec); -#if (NSSAMP > 0) - if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { + if (abs_diff(bsdf, val_next[oy]) > ssamp_thresh || + (ox && abs_diff(bsdf, val_last[oy]) > ssamp_thresh) || + (oy && abs_diff(bsdf, val_last[oy-1]) > ssamp_thresh) || + (oy < sqres-1 && + abs_diff(bsdf, val_next[oy+1]) > ssamp_thresh)) { int ssi; double ssa[4], ssvec[6], sum = 0; /* super-sample voxel */ - for (ssi = NSSAMP; ssi--; ) { - SDmultiSamp(ssa, 4, (ssi+frandom()) * - (1./NSSAMP)); + for (ssi = nssamp; ssi--; ) { + SDmultiSamp(ssa, 4, (ssi+frandom()) / + (double)nssamp); SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, 1.-(iy+ssa[1])*sqfact); ssvec[2] = input_orient * @@ -502,9 +584,8 @@ eval_anisotropic(char *funame) } sum += funvalue(funame, 6, ssvec); } - bsdf = sum * (1./NSSAMP); + bsdf = sum / (double)nssamp; } -#endif } if (pctcull >= 0) putbinary(&bsdf, sizeof(bsdf), 1, ofp); @@ -520,7 +601,8 @@ eval_anisotropic(char *funame) fprintf(uvfp[1], "\t%.3e\n", uv[1]); } } - last_bsdf = bsdf; + if (val_last != NULL) + val_last[oy] = bsdf; } } if (rbf != NULL) @@ -528,6 +610,11 @@ eval_anisotropic(char *funame) prog_show((ix*sqres+iy+1.)/(sqres*sqres)); } prog_done(); + if (val_last != NULL) { + free(val_last); + if (val_next != NULL) free(val_next); + if (sdv_next != NULL) free(sdv_next); + } if (pctcull >= 0) { /* finish output */ if (pclose(ofp)) { fprintf(stderr, "%s: error running rttree_reduce on Y\n", @@ -615,6 +702,43 @@ wrap_up(void) } #endif +#define HEAD_BUFLEN 8192 +static char head_buf[HEAD_BUFLEN]; +static int cur_headlen = 0; + +/* Record header line as comment associated with this SIR input */ +static int +record2header(char *s) +{ + int len = strlen(s); + + if (cur_headlen+len >= HEAD_BUFLEN-6) + return(0); + /* includes EOL */ + strcpy(head_buf+cur_headlen, s); + cur_headlen += len; + +#if defined(_WIN32) || defined(_WIN64) + if (head_buf[cur_headlen-1] == '\n') + head_buf[cur_headlen-1] = '\t'; +#endif + return(1); +} + +/* Finish off header for this file */ +static void +done_header(void) +{ + while (cur_headlen > 0 && isspace(head_buf[cur_headlen-1])) + --cur_headlen; + head_buf[cur_headlen] = '\0'; + if (!cur_headlen) + return; + add_wbsdf("-C", 1); + add_wbsdf(head_buf, 0); + head_buf[cur_headlen=0] = '\0'; +} + /* Read in BSDF and interpolate as tensor tree representation */ int main(int argc, char *argv[]) @@ -663,6 +787,16 @@ main(int argc, char *argv[]) case 'b': dobwd = (argv[i][0] == '+'); break; + case 'n': + nssamp = atoi(argv[++i]); + if (nssamp <= 0) + goto userr; + break; + case 's': + ssamp_thresh = atof(argv[++i]); + if (ssamp_thresh <= FTINY) + goto userr; + break; case 't': switch (argv[i][2]) { case '3': @@ -745,9 +879,13 @@ main(int argc, char *argv[]) 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++) { @@ -775,10 +913,10 @@ main(int argc, char *argv[]) return(wrap_up()); userr: fprintf(stderr, - "Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-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][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", + " 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", progname); return(1); }