--- ray/src/util/dctimestep.c 2011/04/03 03:06:19 2.14 +++ ray/src/util/dctimestep.c 2012/02/21 20:09:25 2.19 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dctimestep.c,v 2.14 2011/04/03 03:06:19 greg Exp $"; +static const char RCSid[] = "$Id: dctimestep.c,v 2.19 2012/02/21 20:09:25 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. @@ -10,9 +10,11 @@ static const char RCSid[] = "$Id: dctimestep.c,v 2.14 #include #include "standard.h" #include "platform.h" +#include "paths.h" #include "color.h" #include "resolu.h" #include "bsdf.h" +#include "bsdf_m.h" char *progname; /* global argv[0] */ @@ -291,39 +293,35 @@ cm_print(const CMATRIX *cm, FILE *fp) } } -/* convert a BSDF to our matrix representation */ +/* Convert a BSDF to our matrix representation */ static CMATRIX * -cm_bsdf(const struct BSDF_data *bsdf) +cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf) { CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc); int nbadohm = 0; int nneg = 0; int r, c; - + /* reciprocity is "transparent" */ for (c = 0; c < cm->ncols; c++) { - float dom = getBSDF_incohm(bsdf,c); - FVECT v; - - if (dom <= .0) { - nbadohm++; - continue; - } - if (!getBSDF_incvec(v,bsdf,c) || v[2] > FTINY) - error(USER, "illegal incoming BTDF direction"); - dom *= -v[2]; + const double dom = mBSDF_incohm(bsdf,c); + /* projected solid angle */ + nbadohm += (dom <= 0); for (r = 0; r < cm->nrows; r++) { - float f = BSDF_value(bsdf,c,r); + float f = mBSDF_value(bsdf,c,r); COLORV *mp = cm_lval(cm,r,c); - - if (f <= .0) { + /* check BSDF value */ + if ((f <= 0) | (dom <= 0)) { nneg += (f < -FTINY); f = .0f; } - mp[0] = mp[1] = mp[2] = f * dom; + copycolor(mp, specCol); + scalecolor(mp, f); + addcolor(mp, bsdfLamb); + scalecolor(mp, dom); } } - if (nneg || nbadohm) { + if (nneg | nbadohm) { sprintf(errmsg, "BTDF has %d negatives and %d bad incoming solid angles", nneg, nbadohm); @@ -332,6 +330,39 @@ cm_bsdf(const struct BSDF_data *bsdf) return(cm); } +/* Load and convert a matrix BSDF from the given XML file */ +static CMATRIX * +cm_loadBSDF(char *fname) +{ + CMATRIX *Tmat; + char *fpath; + SDError ec; + SDData myBSDF; + COLOR bsdfLamb, specCol; + /* find path to BSDF file */ + fpath = getpath(fname, getrlibpath(), R_OK); + if (fpath == NULL) { + sprintf(errmsg, "cannot find BSDF file '%s'", fname); + error(USER, errmsg); + } + SDclearBSDF(&myBSDF, fname); /* load XML and check type */ + ec = SDloadFile(&myBSDF, fpath); + if (ec) + error(USER, transSDError(ec)); + if (myBSDF.tf == NULL || myBSDF.tf->ncomp != 1 || + myBSDF.tf->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); + /* free BSDF and return */ + SDfreeBSDF(&myBSDF); + return(Tmat); +} + /* Sum together a set of images and write result to stdout */ static int sum_images(const char *fspec, const CMATRIX *cv, FILE *fout) @@ -351,7 +382,8 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE int dt, xr, yr; COLORV *psp; /* check for zero */ - if ((scv[RED] == .0) & (scv[GRN] == .0) & (scv[BLU] == .0)) + if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) && + (myDT != DTfromHeader) | (i < cv->nrows-1)) continue; /* open next picture */ sprintf(fname, fspec, i); @@ -413,15 +445,22 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE static int hasNumberFormat(const char *s) { - while (*s && *s != '%') - s++; - if (!*s) - return(0); - do - ++s; - while (isdigit(*s)); - - return(*s == 'd' | *s == 'i' | *s == 'o' | *s == 'x' | *s == 'X'); + while (*s) { + while (*s != '%') + if (!*s++) + return(0); + if (*++s == '%') { /* ignore "%%" */ + ++s; + continue; + } + while (isdigit(*s)) /* field length */ + ++s; + /* field we'll use? */ + if ((*s == 'd') | (*s == 'i') | (*s == 'o') | + (*s == 'x') | (*s == 'X')) + return(1); + } + return(0); /* didn't find one */ } int @@ -440,20 +479,15 @@ main(int argc, char *argv[]) if (argc > 3) { /* VTDs expression */ CMATRIX *svec, *Dmat, *Tmat, *ivec; - struct BSDF_data *btdf; /* get sky vector */ svec = cm_load(argv[4], 0, 1, DTascii); /* load BSDF */ - btdf = load_BSDF(argv[2]); - if (btdf == NULL) - return(1); + Tmat = cm_loadBSDF(argv[2]); /* load Daylight matrix */ - Dmat = cm_load(argv[3], btdf->ninc, svec->nrows, DTfromHeader); + Dmat = cm_load(argv[3], Tmat->ncols, svec->nrows, DTfromHeader); /* multiply vector through */ ivec = cm_multiply(Dmat, svec); cm_free(Dmat); cm_free(svec); - Tmat = cm_bsdf(btdf); /* convert BTDF to matrix */ - free_BSDF(btdf); cvec = cm_multiply(Tmat, ivec); /* cvec = component vector */ cm_free(Tmat); cm_free(ivec); } else { /* else just use sky vector */