--- ray/src/cv/bsdfquery.c 2013/11/22 00:00:27 2.2 +++ ray/src/cv/bsdfquery.c 2017/04/13 00:14:36 2.9 @@ -1,10 +1,11 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfquery.c,v 2.2 2013/11/22 00:00:27 greg Exp $"; +static const char RCSid[] = "$Id: bsdfquery.c,v 2.9 2017/04/13 00:14:36 greg Exp $"; #endif /* * Query values from the given BSDF (scattering interpolant or XML repres.) * Input query is incident and exiting vectors directed away from surface. * We normalize. Output is a BSDF value for the vector pair. + * A zero length in or out vector is ignored, causing output to be flushed. * It is wise to sort the input directions to keep identical ones together * when using a scattering interpolant representation. */ @@ -14,6 +15,7 @@ static const char RCSid[] = "$Id: bsdfquery.c,v 2.2 20 #include #include #include "rtmath.h" +#include "rtio.h" #include "bsdfrep.h" char *progname; @@ -24,31 +26,29 @@ readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt) { double dvec[6]; float fvec[6]; - +tryagain: switch (fmt) { case 'a': - if (fscanf(fp, "%lf %lf %lf %lf %lf %lf", - dvec, dvec+1, dvec+2, dvec+3, dvec+4, dvec+5) != 6) + if (fscanf(fp, FVFORMAT, &idir[0], &idir[1], &idir[2]) != 3 || + fscanf(fp, FVFORMAT, &odir[0], &odir[1], &odir[2]) != 3) return(0); - VCOPY(idir, dvec); - VCOPY(odir, dvec+3); break; case 'd': - if (fread(dvec, sizeof(double), 6, fp) != 6) + if (getbinary(dvec, sizeof(double), 6, fp) != 6) return(0); VCOPY(idir, dvec); VCOPY(odir, dvec+3); break; case 'f': - if (fread(fvec, sizeof(float), 6, fp) != 6) + if (getbinary(fvec, sizeof(float), 6, fp) != 6) return(0); VCOPY(idir, fvec); VCOPY(odir, fvec+3); break; } if ((normalize(idir) == 0) | (normalize(odir) == 0)) { - fprintf(stderr, "%s: zero input vector!\n", progname); - return(0); + fflush(stdout); /* desired side-effect? */ + goto tryagain; } return(1); } @@ -57,6 +57,7 @@ readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt) int main(int argc, char *argv[]) { + int repXYZ = 0; int inpXML = -1; int inpfmt = 'a'; int outfmt = 'a'; @@ -67,11 +68,21 @@ main(int argc, char *argv[]) int n; /* check arguments */ progname = argv[0]; - if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'f' && - argv[1][2] && strchr("afd", argv[1][2]) != NULL) { - inpfmt = outfmt = argv[1][2]; - if (argv[1][3] && strchr("afd", argv[1][3]) != NULL) - outfmt = argv[1][3]; + while (argc > 2 && argv[1][0] == '-') { + switch (argv[1][1]) { + case 'c': /* color output */ + repXYZ = 1; + break; + case 'f': /* i/o format */ + if (!argv[1][2] || strchr("afd", argv[1][2]) == NULL) + goto userr; + inpfmt = outfmt = argv[1][2]; + if (argv[1][3] && strchr("afd", argv[1][3]) != NULL) + outfmt = argv[1][3]; + break; + default: + goto userr; + } ++argv; --argc; } if (argc > 1 && (n = strlen(argv[1])-4) > 0) { @@ -80,10 +91,8 @@ main(int argc, char *argv[]) else if (!strcasecmp(argv[1]+n, ".sir")) inpXML = 0; } - if ((argc != 2) | (inpXML < 0)) { - fprintf(stderr, "Usage: %s [-fio] bsdf.{sir|xml}\n", progname); - return(1); - } + if ((argc != 2) | (inpXML < 0)) + goto userr; /* load BSDF representation */ if (inpXML) { SDclearBSDF(&myBSDF, argv[1]); @@ -102,14 +111,11 @@ main(int argc, char *argv[]) } /* query BSDF values */ while (readIOdir(idir, odir, stdin, inpfmt)) { - double bsdf; - float fval; + SDValue sval; if (inpXML) { - SDValue sval; if (SDreportError(SDevalBSDF(&sval, odir, idir, &myBSDF), stderr)) return(1); - bsdf = sval.cieY; } else { int32 inpDir = encodedir(idir); if (inpDir != prevInpDir) { @@ -127,20 +133,49 @@ main(int argc, char *argv[]) progname); return(1); } - bsdf = eval_rbfrep(rbf, odir)/(output_orient*odir[2]); + if (SDreportError(eval_rbfcol(&sval, rbf, odir), stderr)) + return(1); } + switch (outfmt) { /* write to stdout */ case 'a': - printf("%.6e\n", bsdf); + if (repXYZ) { + double cieX = sval.spec.cx/sval.spec.cy*sval.cieY; + double cieZ = (1. - sval.spec.cx - sval.spec.cy) / + sval.spec.cy * sval.cieY; + printf("%.6e %.6e %.6e\n", cieX, sval.cieY, cieZ); + } else + printf("%.6e\n", sval.cieY); break; case 'd': - fwrite(&bsdf, sizeof(double), 1, stdout); + if (repXYZ) { + double cieXYZ[3]; + cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY; + cieXYZ[1] = sval.cieY; + cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) / + sval.spec.cy * sval.cieY; + putbinary(cieXYZ, sizeof(double), 3, stdout); + } else + putbinary(&sval.cieY, sizeof(double), 1, stdout); break; case 'f': - fval = bsdf; - fwrite(&fval, sizeof(float), 1, stdout); + if (repXYZ) { + float cieXYZ[3]; + cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY; + cieXYZ[1] = sval.cieY; + cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) / + sval.spec.cy * sval.cieY; + putbinary(cieXYZ, sizeof(float), 3, stdout); + } else { + float cieY = sval.cieY; + putbinary(&cieY, sizeof(float), 1, stdout); + } break; } } + /* if (rbf != NULL) free(rbf); */ return(0); +userr: + fprintf(stderr, "Usage: %s [-c][-fio] bsdf.{sir|xml}\n", progname); + return(1); }