--- ray/src/util/dctimestep.c 2012/02/21 20:09:25 2.19 +++ ray/src/util/dctimestep.c 2012/11/02 00:26:55 2.23 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dctimestep.c,v 2.19 2012/02/21 20:09:25 greg Exp $"; +static const char RCSid[] = "$Id: dctimestep.c,v 2.23 2012/11/02 00:26:55 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. @@ -252,8 +252,29 @@ not_handled: return(NULL); /* gratis return */ } -/* Multiply two matrices (or a matrix and a vector) and allocate the result*/ +/* Scale a matrix by a single value */ static CMATRIX * +cm_scale(const CMATRIX *cm1, const COLOR sca) +{ + CMATRIX *cmr; + int dr, dc; + + cmr = cm_alloc(cm1->nrows, cm1->ncols); + if (cmr == NULL) + return(NULL); + for (dr = 0; dr < cmr->nrows; dr++) + for (dc = 0; dc < cmr->ncols; dc++) { + const COLORV *sp = cm_lval(cm1,dr,dc); + COLORV *dp = cm_lval(cmr,dr,dc); + dp[0] = sp[0] * sca[0]; + dp[1] = sp[1] * sca[1]; + dp[2] = sp[2] * sca[2]; + } + return(cmr); +} + +/* Multiply two matrices (or a matrix and a vector) and allocate the result */ +static CMATRIX * cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) { CMATRIX *cmr; @@ -301,7 +322,7 @@ cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, con int nbadohm = 0; int nneg = 0; int r, c; - /* reciprocity is "transparent" */ + /* loop over incident angles */ for (c = 0; c < cm->ncols; c++) { const double dom = mBSDF_incohm(bsdf,c); /* projected solid angle */ @@ -330,15 +351,80 @@ cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, con return(cm); } +/* Convert between input and output indices for reciprocity */ +static int +recip_out_from_in(const SDMat *bsdf, int in_recip) +{ + FVECT v; + + if (!mBSDF_incvec(v, bsdf, in_recip+.5)) + return(in_recip); /* XXX should be error! */ + v[2] = -v[2]; + return(mBSDF_outndx(bsdf, v)); +} + +/* Convert between output and input indices for reciprocity */ +static int +recip_in_from_out(const SDMat *bsdf, int out_recip) +{ + FVECT v; + + if (!mBSDF_outvec(v, bsdf, out_recip+.5)) + return(out_recip); /* XXX should be error! */ + v[2] = -v[2]; + return(mBSDF_incndx(bsdf, v)); +} + +/* Convert a BSDF to our matrix representation, applying reciprocity */ +static CMATRIX * +cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf) +{ + CMATRIX *cm = cm_alloc(bsdf->ninc, bsdf->nout); + int nbadohm = 0; + int nneg = 0; + int r, c; + /* loop over incident angles */ + for (c = 0; c < cm->ncols; c++) { + const int ro = recip_out_from_in(bsdf,c); + const double dom = mBSDF_outohm(bsdf,ro); + /* projected solid angle */ + nbadohm += (dom <= 0); + + for (r = 0; r < cm->nrows; r++) { + const int ri = recip_in_from_out(bsdf,r); + float f = mBSDF_value(bsdf,ri,ro); + COLORV *mp = cm_lval(cm,r,c); + /* check BSDF value */ + if ((f <= 0) | (dom <= 0)) { + nneg += (f < -FTINY); + f = .0f; + } + copycolor(mp, specCol); + scalecolor(mp, f); + addcolor(mp, bsdfLamb); + scalecolor(mp, dom); + } + } + if (nneg | nbadohm) { + sprintf(errmsg, + "BTDF has %d negatives and %d bad incoming solid angles", + nneg, nbadohm); + error(WARNING, errmsg); + } + return(cm); +} + /* Load and convert a matrix BSDF from the given XML file */ static CMATRIX * -cm_loadBSDF(char *fname) +cm_loadBSDF(char *fname, COLOR cLamb) { - CMATRIX *Tmat; - char *fpath; - SDError ec; - SDData myBSDF; - COLOR bsdfLamb, specCol; + CMATRIX *Tmat; + char *fpath; + int recip; + SDError ec; + SDData myBSDF; + SDSpectralDF *tdf; + COLOR bsdfLamb, specCol; /* find path to BSDF file */ fpath = getpath(fname, getrlibpath(), R_OK); if (fpath == NULL) { @@ -349,15 +435,25 @@ cm_loadBSDF(char *fname) ec = SDloadFile(&myBSDF, fpath); if (ec) error(USER, transSDError(ec)); - if (myBSDF.tf == NULL || myBSDF.tf->ncomp != 1 || - myBSDF.tf->comp[0].func != &SDhandleMtx) { + ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, bsdfLamb); + recip = (myBSDF.tb == NULL); + tdf = recip ? myBSDF.tf : myBSDF.tb; + if (tdf == NULL) { /* no non-Lambertian transmission? */ + if (cLamb != NULL) + copycolor(cLamb, bsdfLamb); + SDfreeBSDF(&myBSDF); + return(NULL); + } + if (tdf->ncomp != 1 || tdf->comp[0].func != &SDhandleMtx) { sprintf(errmsg, "unsupported BSDF '%s'", fpath); error(USER, errmsg); } /* convert BTDF to matrix */ - ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, bsdfLamb); - ccy2rgb(&myBSDF.tf->comp[0].cspec[0], 1., specCol); - Tmat = cm_bsdf(bsdfLamb, specCol, (SDMat *)myBSDF.tf->comp[0].dist); + ccy2rgb(&tdf->comp[0].cspec[0], 1., specCol); + Tmat = recip ? cm_bsdf_recip(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist) + : cm_bsdf(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist); + if (cLamb != NULL) /* Lambertian is included */ + setcolor(cLamb, .0, .0, .0); /* free BSDF and return */ SDfreeBSDF(&myBSDF); return(Tmat); @@ -466,7 +562,7 @@ hasNumberFormat(const char *s) int main(int argc, char *argv[]) { - CMATRIX *cvec; + CMATRIX *cvec; /* component vector result */ progname = argv[0]; @@ -479,17 +575,24 @@ main(int argc, char *argv[]) if (argc > 3) { /* VTDs expression */ CMATRIX *svec, *Dmat, *Tmat, *ivec; + COLOR tLamb; /* get sky vector */ svec = cm_load(argv[4], 0, 1, DTascii); /* load BSDF */ - Tmat = cm_loadBSDF(argv[2]); + Tmat = cm_loadBSDF(argv[2], tLamb); /* load Daylight matrix */ - Dmat = cm_load(argv[3], Tmat->ncols, svec->nrows, DTfromHeader); + Dmat = cm_load(argv[3], Tmat==NULL ? 0 : Tmat->ncols, + svec->nrows, DTfromHeader); /* multiply vector through */ ivec = cm_multiply(Dmat, svec); cm_free(Dmat); cm_free(svec); - cvec = cm_multiply(Tmat, ivec); /* cvec = component vector */ - cm_free(Tmat); cm_free(ivec); + if (Tmat == NULL) { /* diffuse only */ + cvec = cm_scale(ivec, tLamb); + } else { /* else apply BTDF matrix */ + cvec = cm_multiply(Tmat, ivec); + cm_free(Tmat); + } + cm_free(ivec); } else { /* else just use sky vector */ cvec = cm_load(argv[2], 0, 1, DTascii); }