--- ray/src/common/testBSDF.c 2017/02/02 00:30:16 1.7 +++ ray/src/common/testBSDF.c 2021/12/07 23:49:50 1.17 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: testBSDF.c,v 1.7 2017/02/02 00:30:16 greg Exp $"; +static const char RCSid[] = "$Id: testBSDF.c,v 1.17 2021/12/07 23:49:50 greg Exp $"; #endif /* * Simple test program to demonstrate BSDF operation. @@ -8,7 +8,6 @@ static const char RCSid[] = "$Id: testBSDF.c,v 1.7 201 */ #define _USE_MATH_DEFINES -#include #include #include #include @@ -24,10 +23,10 @@ Usage(const char *prog) printf(" i\t\t\t\t Report general information (metadata)\n"); printf(" c\t\t\t\t Report diffuse and specular components\n"); printf(" q theta_i phi_i theta_o phi_o\t Query BSDF for given path (CIE-XYZ)\n"); - printf(" s N theta phi\t\t\t Generate N ray directions at given incidence\n"); - printf(" h theta phi\t\t\t Report hemispherical total at given incidence\n"); - printf(" r theta phi\t\t\t Report hemispherical reflection at given incidence\n"); - printf(" t theta phi\t\t\t Report hemispherical transmission at given incidence\n"); + printf(" s[r|t][s|d] N theta phi\t Generate N ray directions & colors at given incidence\n"); + printf(" h[s|d] theta phi\t\t Report hemispherical scattering at given incidence\n"); + printf(" r[s|d] theta phi\t\t Report hemispherical reflection at given incidence\n"); + printf(" t[s|d] theta phi\t\t Report hemispherical transmission at given incidence\n"); printf(" a theta phi [t2 p2]\t\t Report resolution (in proj. steradians) for given direction(s)\n"); printf(" ^D\t\t\t\t Quit program\n"); } @@ -47,6 +46,10 @@ vec_from_deg(FVECT v, double theta, double phi) static void printXYZ(const char *intro, const SDValue *vp) { + if (vp->cieY <= 1e-9) { + printf("%s0 0 0\n", intro); + return; + } printf("%s%.3e %.3e %.3e\n", intro, vp->spec.cx/vp->spec.cy*vp->cieY, vp->cieY, @@ -71,13 +74,13 @@ main(int argc, char *argv[]) SDretainSet = SDretainBSDFs; /* keep BSDFs in memory */ /* loop on command */ - while (fgets(inp, sizeof(inp), stdin) != NULL) { + while (fgets(inp, sizeof(inp), stdin)) { int sflags = SDsampAll; char *cp = inp; char *cp2; FVECT vin, vout; double proja[2]; - int n; + int n, i; SDValue val; while (isspace(*cp)) cp++; @@ -98,92 +101,117 @@ main(int argc, char *argv[]) bsdf = SDcacheFile(path); continue; case 'I': /* report general info. */ - if (bsdf == NULL) + if (!bsdf) goto noBSDFerr; printf("Material: '%s'\n", bsdf->matn); printf("Manufacturer: '%s'\n", bsdf->makr); printf("Width, Height, Thickness (m): %.4e, %.4e, %.4e\n", bsdf->dim[0], bsdf->dim[1], bsdf->dim[2]); - printf("Has geometry: %s\n", bsdf->mgf!=NULL ? "yes" : "no"); - continue; + if (bsdf->mgf) + printf("Has geometry: %lu bytes\n", + (unsigned long)strlen(bsdf->mgf)); + else + printf("Has geometry: no\n"); + break; case 'C': /* report constant values */ - if (bsdf == NULL) + if (!bsdf) goto noBSDFerr; - if (bsdf->rf != NULL) + if (bsdf->rf) printf("Peak front hemispherical reflectance: %.3e\n", bsdf->rLambFront.cieY + bsdf->rf->maxHemi); - if (bsdf->rb != NULL) + if (bsdf->rb) printf("Peak back hemispherical reflectance: %.3e\n", bsdf->rLambBack.cieY + bsdf->rb->maxHemi); - if (bsdf->tf != NULL) + if (bsdf->tf) printf("Peak front hemispherical transmittance: %.3e\n", - bsdf->tLamb.cieY + bsdf->tf->maxHemi); - if (bsdf->tb != NULL) + bsdf->tLambFront.cieY + bsdf->tf->maxHemi); + if (bsdf->tb) printf("Peak back hemispherical transmittance: %.3e\n", - bsdf->tLamb.cieY + bsdf->tb->maxHemi); + bsdf->tLambBack.cieY + bsdf->tb->maxHemi); printXYZ("Diffuse Front Reflectance: ", &bsdf->rLambFront); printXYZ("Diffuse Back Reflectance: ", &bsdf->rLambBack); - printXYZ("Diffuse Transmittance: ", &bsdf->tLamb); - continue; + printXYZ("Diffuse Front Transmittance: ", &bsdf->tLambFront); + printXYZ("Diffuse Back Transmittance: ", &bsdf->tLambBack); + break; case 'Q': /* query BSDF value */ - if (bsdf == NULL) + if (!bsdf) goto noBSDFerr; if (!*sskip2(cp,4)) break; vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2))); vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4))); - if (!SDreportError(SDevalBSDF(&val, vout, vin, bsdf), stderr)) + if (!SDreportError(SDevalBSDF(&val, vin, vout, bsdf), stderr)) printXYZ("", &val); - continue; + break; case 'S': /* sample BSDF */ - if (bsdf == NULL) + if (!bsdf) goto noBSDFerr; if (!*sskip2(cp,3)) break; - n = atoi(sskip2(cp,1)); + if (toupper(cp[1]) == 'R') { + sflags &= ~SDsampT; + ++cp; + } else if (toupper(cp[1]) == 'T') { + sflags &= ~SDsampR; + ++cp; + } + if (toupper(cp[1]) == 'S') + sflags &= ~SDsampDf; + else if (toupper(cp[1]) == 'D') + sflags &= ~SDsampSp; + i = n = atoi(sskip2(cp,1)); vec_from_deg(vin, atof(sskip2(cp,2)), atof(sskip2(cp,3))); - while (n-- > 0) { - if (SDreportError(SDsampBSDF(&val, vin, - rand()*(1./(RAND_MAX+.5)), + while (i-- > 0) { + VCOPY(vout, vin); + if (SDreportError(SDsampBSDF(&val, vout, + (i+rand()*(1./(RAND_MAX+.5)))/(double)n, sflags, bsdf), stderr)) break; - printf("%.8f %.8f %.8f\n", vin[0], vin[1], vin[2]); + printf("%.8f %.8f %.8f ", vout[0], vout[1], vout[2]); + printXYZ("", &val); } - continue; - case 'H': /* hemispherical totals */ + break; + case 'H': /* hemispherical values */ case 'R': case 'T': - if (bsdf == NULL) + if (!bsdf) goto noBSDFerr; if (!*sskip2(cp,2)) break; - if (tolower(*cp) == 'r') + if (toupper(cp[0]) == 'R') sflags &= ~SDsampT; - else if (tolower(*cp) == 't') + else if (toupper(cp[0]) == 'T') sflags &= ~SDsampR; + if (toupper(cp[1]) == 'S') + sflags &= ~SDsampDf; + else if (toupper(cp[1]) == 'D') + sflags &= ~SDsampSp; vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2))); printf("%.4e\n", SDdirectHemi(vin, sflags, bsdf)); - continue; + break; case 'A': /* resolution in proj. steradians */ - if (bsdf == NULL) + if (!bsdf) goto noBSDFerr; if (!*sskip2(cp,2)) break; vec_from_deg(vin, atof(sskip2(cp,1)), atof(sskip2(cp,2))); if (*sskip2(cp,4)) { vec_from_deg(vout, atof(sskip2(cp,3)), atof(sskip2(cp,4))); - if (SDreportError(SDsizeBSDF(proja, vin, vout, + if (SDreportError(SDsizeBSDF(proja, vout, vin, SDqueryMin+SDqueryMax, bsdf), stderr)) continue; } else if (SDreportError(SDsizeBSDF(proja, vin, NULL, SDqueryMin+SDqueryMax, bsdf), stderr)) continue; printf("%.4e %.4e\n", proja[0], proja[1]); - continue; + break; + default: + Usage(argv[0]); + break; } - Usage(argv[0]); + fflush(stdout); /* in case we're on remote */ continue; noBSDFerr: fprintf(stderr, "%s: First, use 'L' command to load BSDF\n", argv[0]);