--- ray/src/cv/bsdf2ttree.c 2012/12/12 04:49:59 2.10 +++ ray/src/cv/bsdf2ttree.c 2014/03/12 21:15:31 2.26 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.10 2012/12/12 04:49:59 greg Exp $"; +static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.26 2014/03/12 21:15:31 greg Exp $"; #endif /* * Load measured BSDF interpolant and write out as XML file with tensor tree. @@ -11,7 +11,10 @@ static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.10 #include #include #include +#include "random.h" #include "platform.h" +#include "rtprocess.h" +#include "calcomp.h" #include "bsdfrep.h" /* global argv[0] */ char *progname; @@ -19,24 +22,139 @@ char *progname; double pctcull = 90.; /* sampling order */ int samp_order = 6; + /* super-sampling threshold */ +const double ssamp_thresh = 0.35; + /* number of super-samples */ +const int nssamp = 100; + /* limit on number of RBF lobes */ +static int lobe_lim = 15000; + /* progress bar length */ +static int do_prog = 79; + +/* Start new progress bar */ +#define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else + +/* Draw progress bar of the appropriate length */ +static void +prog_show(double frac) +{ + char pbar[256]; + int nchars; + + if (do_prog <= 0) return; + if (do_prog > sizeof(pbar)-2) + do_prog = sizeof(pbar)-2; + if (frac < 0) frac = 0; + else if (frac > 1) frac = 1; + nchars = do_prog*frac + .5; + pbar[0] = '\r'; + memset(pbar+1, '*', nchars); + memset(pbar+1+nchars, '-', do_prog-nchars); + pbar[do_prog+1] = '\0'; + fputs(pbar, stderr); +} + +/* Finish progress bar */ +#define prog_done() if (do_prog) fputc('\n',stderr); else + +/* Output XML prologue to stdout */ +static void +xml_prologue(int ac, char *av[]) +{ + puts(""); + puts(""); + fputs(""); + puts("System"); + puts("BSDF"); + puts(""); + puts(""); + puts("\t"); + printf("\t\t%s\n", bsdf_name[0] ? bsdf_name : "Unknown"); + printf("\t\t%s\n", + bsdf_manuf[0] ? bsdf_manuf : "Unknown"); + puts("\t\tOther"); + puts("\t"); + puts("\t"); + printf("\t\tTensorTree%c\n", + single_plane_incident ? '3' : '4'); + puts("\t"); +} + +/* Output XML data prologue to stdout */ +static void +data_prologue() +{ + static const char *bsdf_type[4] = { + "Reflection Front", + "Transmission Front", + "Transmission Back", + "Reflection Back" + }; + + puts("\t"); + puts("\t\tSystem"); + puts("\t\tVisible"); + puts("\t\tCIE Illuminant D65 1nm.ssp"); + puts("\t\tASTM E308 1931 Y.dsp"); + puts("\t\t"); + printf("\t\t\t%s\n", + bsdf_type[(input_orient>0)<<1 | (output_orient>0)]); + puts("\t\t\tLBNL/Shirley-Chiu"); + puts("\t\t\tBTDF"); + puts("\t\t\t"); +} + +/* Output XML data epilogue to stdout */ +static void +data_epilogue(void) +{ + puts("\t\t\t"); + puts("\t\t"); + puts("\t"); +} + +/* Output XML epilogue to stdout */ +static void +xml_epilogue(void) +{ + puts(""); + puts(""); + puts(""); +} + +/* Compute absolute relative difference */ +static double +abs_diff(double v1, double v0) +{ + if ((v0 < 0) | (v1 < 0)) + return(.0); + v1 = (v1-v0)*2./(v0+v1+.0001); + if (v1 < 0) + return(-v1); + return(v1); +} + /* Interpolate and output isotropic BSDF data */ static void -interp_isotropic() +eval_isotropic(char *funame) { const int sqres = 1<= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull); - else fputs("raw data\n", stderr); -#endif - if (pctcull >= 0) { /* begin output */ - sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d", + + data_prologue(); /* begin output */ + if (pctcull >= 0) { + sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d", pctcull, samp_order); fflush(stdout); ofp = popen(cmd, "w"); @@ -46,28 +164,75 @@ interp_isotropic() exit(1); } SET_FILE_BINARY(ofp); +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(ofp); +#endif } else fputs("{\n", stdout); + /* need to assign Dx, Dy, Dz? */ + if (funame != NULL) + assignD = (fundefined(funame) < 6); /* run through directions */ for (ix = 0; ix < sqres/2; ix++) { - RBFNODE *rbf; - SDsquare2disk(ivec, (ix+.5)/sqres, .5); - ivec[2] = input_orient * - sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]); - rbf = advect_rbf(ivec); - for (ox = 0; ox < sqres; ox++) + RBFNODE *rbf = NULL; + iovec[0] = 2.*(ix+.5)/sqres - 1.; + iovec[1] = .0; + iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); + if (funame == NULL) + rbf = advect_rbf(iovec, lobe_lim); + for (ox = 0; ox < sqres; ox++) { + float last_bsdf = -1; for (oy = 0; oy < sqres; oy++) { - SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres); - ovec[2] = output_orient * - sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]); - bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2]; + SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); + iovec[5] = output_orient * + sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); + if (funame == NULL) + bsdf = eval_rbfrep(rbf, iovec+3) * + output_orient/iovec[5]; + else { + double ssa[3], ssvec[6], sum; + int ssi; + if (assignD) { + varset("Dx", '=', -iovec[3]); + varset("Dy", '=', -iovec[4]); + varset("Dz", '=', -iovec[5]); + ++eclock; + } + bsdf = funvalue(funame, 6, iovec); + if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { + sum = 0; /* super-sample voxel */ + for (ssi = nssamp; ssi--; ) { + SDmultiSamp(ssa, 3, (ssi+frandom())/nssamp); + ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; + ssvec[1] = .0; + ssvec[2] = input_orient * + sqrt(1. - ssvec[0]*ssvec[0]); + SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, + (oy+ssa[2])/sqres); + ssvec[5] = output_orient * + sqrt(1. - ssvec[3]*ssvec[3] - + ssvec[4]*ssvec[4]); + if (assignD) { + varset("Dx", '=', -iovec[3]); + varset("Dy", '=', -iovec[4]); + varset("Dz", '=', -iovec[5]); + ++eclock; + } + sum += funvalue(funame, 6, ssvec); + } + bsdf = sum/nssamp; + } + } if (pctcull >= 0) fwrite(&bsdf, sizeof(bsdf), 1, ofp); else printf("\t%.3e\n", bsdf); + last_bsdf = bsdf; } + } if (rbf != NULL) free(rbf); + prog_show((ix+1.)*(2./sqres)); } if (pctcull >= 0) { /* finish output */ if (pclose(ofp)) { @@ -80,25 +245,26 @@ interp_isotropic() fputs("\t0\n", stdout); fputs("}\n", stdout); } + data_epilogue(); + prog_done(); } /* Interpolate and output anisotropic BSDF data */ static void -interp_anisotropic() +eval_anisotropic(char *funame) { const int sqres = 1<= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull); - else fputs("raw data\n", stderr); -#endif - if (pctcull >= 0) { /* begin output */ - sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d", + + data_prologue(); /* begin output */ + if (pctcull >= 0) { + sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d", + (input_orient>0 ^ output_orient>0) ? "" : " -a", pctcull, samp_order); fflush(stdout); ofp = popen(cmd, "w"); @@ -107,30 +273,78 @@ interp_anisotropic() progname); exit(1); } + SET_FILE_BINARY(ofp); +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(ofp); +#endif } else fputs("{\n", stdout); + /* need to assign Dx, Dy, Dz? */ + if (funame != NULL) + assignD = (fundefined(funame) < 6); /* run through directions */ for (ix = 0; ix < sqres; ix++) for (iy = 0; iy < sqres; iy++) { - RBFNODE *rbf; /* Klems reversal */ - SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres); - ivec[0] = -ivec[0]; ivec[1] = -ivec[1]; - ivec[2] = input_orient * - sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]); - rbf = advect_rbf(ivec); - for (ox = 0; ox < sqres; ox++) + RBFNODE *rbf = NULL; /* Klems reversal */ + SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres); + iovec[2] = input_orient * + sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); + if (funame == NULL) + rbf = advect_rbf(iovec, lobe_lim); + for (ox = 0; ox < sqres; ox++) { + float last_bsdf = -1; for (oy = 0; oy < sqres; oy++) { - SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres); - ovec[2] = output_orient * - sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]); - bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2]; + SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); + iovec[5] = output_orient * + sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); + if (funame == NULL) + bsdf = eval_rbfrep(rbf, iovec+3) * + output_orient/iovec[5]; + else { + double ssa[4], ssvec[6], sum; + int ssi; + if (assignD) { + varset("Dx", '=', -iovec[3]); + varset("Dy", '=', -iovec[4]); + varset("Dz", '=', -iovec[5]); + ++eclock; + } + bsdf = funvalue(funame, 6, iovec); + if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { + sum = 0; /* super-sample voxel */ + for (ssi = nssamp; ssi--; ) { + SDmultiSamp(ssa, 4, (ssi+frandom())/nssamp); + SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, + 1.-(iy+ssa[1])/sqres); + ssvec[2] = output_orient * + sqrt(1. - ssvec[0]*ssvec[0] - + ssvec[1]*ssvec[1]); + SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, + (oy+ssa[3])/sqres); + ssvec[5] = output_orient * + sqrt(1. - ssvec[3]*ssvec[3] - + ssvec[4]*ssvec[4]); + if (assignD) { + varset("Dx", '=', -iovec[3]); + varset("Dy", '=', -iovec[4]); + varset("Dz", '=', -iovec[5]); + ++eclock; + } + sum += funvalue(funame, 6, ssvec); + } + bsdf = sum/nssamp; + } + } if (pctcull >= 0) fwrite(&bsdf, sizeof(bsdf), 1, ofp); else printf("\t%.3e\n", bsdf); + last_bsdf = bsdf; } + } if (rbf != NULL) free(rbf); + prog_show((ix*sqres+iy+1.)/(sqres*sqres)); } if (pctcull >= 0) { /* finish output */ if (pclose(ofp)) { @@ -140,108 +354,138 @@ interp_anisotropic() } } else fputs("}\n", stdout); + data_epilogue(); + prog_done(); } -/* Output XML prologue to stdout */ -static void -xml_prologue(int ac, char *av[]) -{ - static const char *bsdf_type[4] = { - "Reflection Front", - "Transmission Front", - "Transmission Back", - "Reflection Back" - }; - - puts(""); - puts(""); - fputs(""); - puts("System"); - puts("BSDF"); - puts(""); - puts(""); - puts("\t"); - puts("\t\tName"); - puts("\t\tManufacturer"); - puts("\t\tOther"); - puts("\t"); - puts("\t"); - printf("\t\tTensorTree%c\n", - single_plane_incident ? '3' : '4'); - puts("\t"); - puts("\t"); - puts("\t\tSystem"); - puts("\t\tVisible"); - puts("\t\tCIE Illuminant D65 1nm.ssp"); - puts("\t\tASTM E308 1931 Y.dsp"); - puts("\t\t"); - printf("\t\t\t%s\n", - bsdf_type[(input_orient>0)<<1 | (output_orient>0)]); - puts("\t\t\tLBNL/Shirley-Chiu"); - puts("\t\t\tBTDF"); - puts("\t\t\t"); -} - -/* Output XML epilogue to stdout */ -static void -xml_epilogue(void) -{ - puts("\t\t\t"); - puts("\t\t"); - puts("\t"); - puts(""); - puts(""); - puts(""); -} - /* Read in BSDF and interpolate as tensor tree representation */ int main(int argc, char *argv[]) { - FILE *fpin = stdin; - int i; + int dofwd = 0, dobwd = 1; + int i, na; progname = argv[0]; - for (i = 1; i < argc-1 && argv[i][0] == '-'; i++) - switch (argv[i][1]) { /* get option */ + esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; + esupport &= ~(E_INCHAN|E_OUTCHAN); + scompile("PI:3.14159265358979323846", NULL, 0); + biggerlib(); + for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) + switch (argv[i][1]) { /* get options */ + case 'e': + scompile(argv[++i], NULL, 0); + break; + case 'f': + if (!argv[i][2]) + fcompile(argv[++i]); + else + dofwd = (argv[i][0] == '+'); + break; + case 'b': + dobwd = (argv[i][0] == '+'); + break; case 't': - pctcull = atof(argv[++i]); + switch (argv[i][2]) { + case '3': + single_plane_incident = 1; + break; + case '4': + single_plane_incident = 0; + break; + case '\0': + pctcull = atof(argv[++i]); + break; + default: + goto userr; + } break; case 'g': samp_order = atoi(argv[++i]); break; + case 'l': + lobe_lim = atoi(argv[++i]); + break; + case 'p': + do_prog = atoi(argv[i]+2); + break; default: goto userr; } - - if (i == argc-1) { /* open input if given */ - fpin = fopen(argv[i], "r"); - if (fpin == NULL) { - fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", - progname, argv[1]); - return(1); + if (single_plane_incident >= 0) { /* function-based BSDF? */ + void (*evf)(char *s) = single_plane_incident ? + &eval_isotropic : &eval_anisotropic; + if (i != argc-1 || fundefined(argv[i]) < 3) { + fprintf(stderr, + "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", + progname); + fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); + goto userr; } - } else if (i < argc-1) - goto userr; - SET_FILE_BINARY(fpin); /* load BSDF interpolant */ - if (!load_bsdf_rep(fpin)) + ++eclock; + xml_prologue(argc, argv); /* start XML output */ + if (dofwd) { + input_orient = -1; + output_orient = -1; + prog_start("Evaluating outside reflectance"); + (*evf)(argv[i]); + output_orient = 1; + prog_start("Evaluating outside->inside transmission"); + (*evf)(argv[i]); + } + if (dobwd) { + input_orient = 1; + output_orient = 1; + prog_start("Evaluating inside reflectance"); + (*evf)(argv[i]); + output_orient = -1; + prog_start("Evaluating inside->outside transmission"); + (*evf)(argv[i]); + } + xml_epilogue(); /* finish XML output & exit */ + return(0); + } + 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); + } + if (!load_bsdf_rep(fpin)) + return(1); + fclose(fpin); + if (!nbsdf++) /* start XML on first dist. */ + xml_prologue(argc, argv); + sprintf(pbuf, "Interpolating component '%s'", argv[i]); + prog_start(pbuf); + if (single_plane_incident) + eval_isotropic(NULL); + else + eval_anisotropic(NULL); + } + xml_epilogue(); /* finish XML output & exit */ + return(0); + } + SET_FILE_BINARY(stdin); /* load from stdin */ + if (!load_bsdf_rep(stdin)) return(1); - fclose(fpin); xml_prologue(argc, argv); /* start XML output */ + prog_start("Interpolating from standard input"); if (single_plane_incident) /* resample dist. */ - interp_isotropic(); + eval_isotropic(NULL); else - interp_anisotropic(); - xml_epilogue(); /* finish XML output */ + eval_anisotropic(NULL); + xml_epilogue(); /* finish XML output & exit */ return(0); userr: fprintf(stderr, - "Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n", + "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", + progname); + fprintf(stderr, + " or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", progname); return(1); }