--- ray/src/util/dctimestep.c 2009/06/20 04:37:43 2.6 +++ ray/src/util/dctimestep.c 2011/04/08 18:13:48 2.15 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dctimestep.c,v 2.6 2009/06/20 04:37:43 greg Exp $"; +static const char RCSid[] = "$Id: dctimestep.c,v 2.15 2011/04/08 18:13:48 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. @@ -10,9 +10,11 @@ static const char RCSid[] = "$Id: dctimestep.c,v 2.6 2 #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] */ @@ -38,7 +40,7 @@ cm_alloc(int nrows, int ncols) CMATRIX *cm; if ((nrows <= 0) | (ncols <= 0)) - return(NULL); + error(USER, "attempt to create empty matrix"); cm = (CMATRIX *)malloc(sizeof(CMATRIX) + 3*sizeof(COLORV)*(nrows*ncols - 1)); if (cm == NULL) @@ -108,6 +110,8 @@ cm_load(const char *fname, int nrows, int ncols, int d CMATRIX *cm; FILE *fp = stdin; + if (ncols <= 0) + error(USER, "Non-positive number of columns"); if (fname == NULL) fname = ""; else if ((fp = fopen(fname, "r")) == NULL) { @@ -289,36 +293,72 @@ 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); - COLORV *mp = cm->cmem; + CMATRIX *cm; int nbadohm = 0; int nneg = 0; + FVECT v; + float dom; + int doforward; int r, c; - - for (r = 0; r < cm->nrows; r++) - for (c = 0; c < cm->ncols; c++, mp += 3) { - float f = BSDF_value(bsdf,c,r); - float dom = getBSDF_incohm(bsdf,c); - FVECT v; + + mBSDF_incvec(v, bsdf, .5); /* check BTDF orientation */ + + if (v[2] < 0) { /* incident from outside */ + cm = cm_alloc(bsdf->nout, bsdf->ninc); + for (c = 0; c < cm->ncols; c++) { + dom = mBSDF_incohm(bsdf,c); - if (f <= .0) { - nneg += (f < -FTINY); - continue; + nbadohm += (dom <= 0); + + if (!mBSDF_incvec(v,bsdf,c+.5) || v[2] > 0) + error(USER, "illegal incoming BTDF direction"); + dom *= -v[2]; + + for (r = 0; r < cm->nrows; r++) { + float f = mBSDF_value(bsdf,c,r); + COLORV *mp = cm_lval(cm,r,c); + + if ((f <= 0) | (dom <= 0)) { + nneg += (f < -FTINY); + f = .0f; + } + copycolor(mp, specCol); + f *= dom; + scalecolor(mp, f); + addcolor(mp, bsdfLamb); } - if (dom <= .0) { - nbadohm++; - continue; + } + } else { /* rely on reciprocity */ + cm = cm_alloc(bsdf->ninc, bsdf->nout); + for (c = 0; c < cm->ncols; c++) { + dom = mBSDF_outohm(bsdf,c); + + nbadohm += (dom <= 0); + + if (!mBSDF_outvec(v,bsdf,c+.5) || v[2] > 0) + error(USER, "illegal outgoing BTDF direction"); + dom *= -v[2]; + + for (r = 0; r < cm->nrows; r++) { + float f = mBSDF_value(bsdf,r,c); + COLORV *mp = cm_lval(cm,r,c); + + if ((f <= 0) | (dom <= 0)) { + nneg += (f < -FTINY); + f = .0f; + } + copycolor(mp, specCol); + f *= dom; + scalecolor(mp, f); + addcolor(mp, bsdfLamb); } - if (!getBSDF_incvec(v,bsdf,c) || v[2] > FTINY) - error(USER, "illegal incoming BTDF direction"); - - mp[0] = mp[1] = mp[2] = f * dom * -v[2]; } - if (nneg || nbadohm) { + } + if (nneg | nbadohm) { sprintf(errmsg, "BTDF has %d negatives and %d bad incoming solid angles", nneg, nbadohm); @@ -327,9 +367,43 @@ 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); + } + SDclipName(myBSDF.name, 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) +sum_images(const char *fspec, const CMATRIX *cv, FILE *fout) { int myDT = DTfromHeader; CMATRIX *pmat; @@ -345,6 +419,9 @@ sum_images(const char *fspec, const CMATRIX *cv) FILE *fp; int dt, xr, yr; COLORV *psp; + /* check for zero */ + if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0)) + continue; /* open next picture */ sprintf(fname, fspec, i); if ((fp = fopen(fname, "r")) == NULL) { @@ -367,10 +444,10 @@ sum_images(const char *fspec, const CMATRIX *cv) pmat = cm_alloc(myYR, myXR); memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR); /* finish header */ - fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, stdout); - fputc('\n', stdout); - fprtresolu(myXR, myYR, stdout); - fflush(stdout); + fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout); + fputc('\n', fout); + fprtresolu(myXR, myYR, fout); + fflush(fout); } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) { sprintf(errmsg, "picture '%s' format/size mismatch", fname); @@ -395,15 +472,15 @@ sum_images(const char *fspec, const CMATRIX *cv) free(scanline); /* write scanlines */ for (y = 0; y < myYR; y++) - if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, stdout) < 0) + if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0) return(0); cm_free(pmat); /* all done */ - return(fflush(stdout) == 0); + return(fflush(fout) == 0); } -/* check to see if a string contains a %d specification */ -int -hasDecimalSpec(const char *s) +/* check to see if a string contains a %d or %o specification */ +static int +hasNumberFormat(const char *s) { while (*s && *s != '%') s++; @@ -413,43 +490,47 @@ hasDecimalSpec(const char *s) ++s; while (isdigit(*s)); - return(*s == 'd'); + return((*s == 'd') | (*s == 'i') | (*s == 'o') | + (*s == 'x') | (*s == 'X')); } int main(int argc, char *argv[]) { - CMATRIX *tvec, *Dmat, *Tmat, *ivec, *cvec; - struct BSDF_data *btdf; + CMATRIX *cvec; progname = argv[0]; - if ((argc < 4) | (argc > 5)) { - fprintf(stderr, "Usage: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n", - progname); + if ((argc < 2) | (argc > 5)) { + fprintf(stderr, "Usage: %s DCspec [tregvec]\n", progname); + fprintf(stderr, " or: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n", + progname); return(1); } - /* load Tregenza vector */ - tvec = cm_load(argv[4], 0, 1, DTascii); /* argv[4]==NULL iff argc==4 */ - /* load BTDF */ - btdf = load_BSDF(argv[2]); - if (btdf == NULL) - return(1); + + if (argc > 3) { /* VTDs expression */ + CMATRIX *svec, *Dmat, *Tmat, *ivec; + /* get sky vector */ + svec = cm_load(argv[4], 0, 1, DTascii); + /* load BSDF */ + Tmat = cm_loadBSDF(argv[2]); /* load Daylight matrix */ - Dmat = cm_load(argv[3], btdf->ninc, tvec->nrows, DTfromHeader); + Dmat = cm_load(argv[3], Tmat->ncols, svec->nrows, DTfromHeader); /* multiply vector through */ - ivec = cm_multiply(Dmat, tvec); - cm_free(Dmat); cm_free(tvec); - 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); - if (hasDecimalSpec(argv[1])) { /* generating image */ + 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); + } else { /* else just use sky vector */ + cvec = cm_load(argv[2], 0, 1, DTascii); + } + + if (hasNumberFormat(argv[1])) { /* generating image */ SET_FILE_BINARY(stdout); newheader("RADIANCE", stdout); printargs(argc, argv, stdout); fputnow(stdout); - if (!sum_images(argv[1], cvec)) + if (!sum_images(argv[1], cvec, stdout)) return(1); } else { /* generating vector */ CMATRIX *Vmat = cm_load(argv[1], 0, cvec->nrows, DTfromHeader);