| 1 | 
greg | 
2.1 | 
#ifndef lint | 
| 2 | 
greg | 
2.4 | 
static const char RCSid[] = "$Id: cmbsdf.c,v 2.3 2015/04/17 01:47:03 greg Exp $"; | 
| 3 | 
greg | 
2.1 | 
#endif | 
| 4 | 
  | 
  | 
/* | 
| 5 | 
  | 
  | 
 * Load and convert BSDF into color coefficient matrix representation. | 
| 6 | 
  | 
  | 
 * | 
| 7 | 
  | 
  | 
 *      G. Ward | 
| 8 | 
  | 
  | 
 */ | 
| 9 | 
  | 
  | 
 | 
| 10 | 
  | 
  | 
#include "standard.h" | 
| 11 | 
  | 
  | 
#include "cmatrix.h" | 
| 12 | 
  | 
  | 
#include "paths.h" | 
| 13 | 
  | 
  | 
#include "bsdf.h" | 
| 14 | 
  | 
  | 
#include "bsdf_m.h" | 
| 15 | 
  | 
  | 
 | 
| 16 | 
  | 
  | 
/* Convert a BSDF to our matrix representation */ | 
| 17 | 
  | 
  | 
static CMATRIX * | 
| 18 | 
greg | 
2.3 | 
cm_bsdf(const COLOR diffBSDF, const SDMat *bsdf) | 
| 19 | 
greg | 
2.1 | 
{ | 
| 20 | 
  | 
  | 
        CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc); | 
| 21 | 
  | 
  | 
        int     nbadohm = 0; | 
| 22 | 
  | 
  | 
        int     nneg = 0; | 
| 23 | 
  | 
  | 
        int     r, c; | 
| 24 | 
  | 
  | 
                                        /* loop over incident angles */ | 
| 25 | 
  | 
  | 
        for (c = 0; c < cm->ncols; c++) { | 
| 26 | 
  | 
  | 
                const double    dom = mBSDF_incohm(bsdf,c); | 
| 27 | 
  | 
  | 
                                        /* projected solid angle */ | 
| 28 | 
  | 
  | 
                nbadohm += (dom <= 0); | 
| 29 | 
  | 
  | 
 | 
| 30 | 
  | 
  | 
                for (r = 0; r < cm->nrows; r++) { | 
| 31 | 
  | 
  | 
                        float   f = mBSDF_value(bsdf,c,r); | 
| 32 | 
  | 
  | 
                        COLORV  *mp = cm_lval(cm,r,c); | 
| 33 | 
  | 
  | 
                                        /* check BSDF value */ | 
| 34 | 
  | 
  | 
                        if ((f <= 0) | (dom <= 0)) { | 
| 35 | 
  | 
  | 
                                nneg += (f < -FTINY); | 
| 36 | 
greg | 
2.3 | 
                                setcolor(mp, .0f, .0f, .0f); | 
| 37 | 
  | 
  | 
                        } else if (bsdf->chroma != NULL) { | 
| 38 | 
  | 
  | 
                                C_COLOR cxy; | 
| 39 | 
greg | 
2.4 | 
                                c_decodeChroma(&cxy, mBSDF_chroma(bsdf,c,r)); | 
| 40 | 
greg | 
2.3 | 
                                ccy2rgb(&cxy, f, mp); | 
| 41 | 
  | 
  | 
                        } else | 
| 42 | 
  | 
  | 
                                setcolor(mp, f, f, f); | 
| 43 | 
greg | 
2.2 | 
                        addcolor(mp, diffBSDF); | 
| 44 | 
greg | 
2.1 | 
                        scalecolor(mp, dom); | 
| 45 | 
  | 
  | 
                } | 
| 46 | 
  | 
  | 
        } | 
| 47 | 
  | 
  | 
        if (nneg | nbadohm) { | 
| 48 | 
  | 
  | 
                sprintf(errmsg, | 
| 49 | 
  | 
  | 
                    "BTDF has %d negatives and %d bad incoming solid angles", | 
| 50 | 
  | 
  | 
                                nneg, nbadohm); | 
| 51 | 
  | 
  | 
                error(WARNING, errmsg); | 
| 52 | 
  | 
  | 
        } | 
| 53 | 
  | 
  | 
        return(cm); | 
| 54 | 
  | 
  | 
} | 
| 55 | 
  | 
  | 
 | 
| 56 | 
  | 
  | 
/* Convert between input and output indices for reciprocity */ | 
| 57 | 
  | 
  | 
static int | 
| 58 | 
  | 
  | 
recip_out_from_in(const SDMat *bsdf, int in_recip) | 
| 59 | 
  | 
  | 
{ | 
| 60 | 
  | 
  | 
        FVECT   v; | 
| 61 | 
  | 
  | 
 | 
| 62 | 
  | 
  | 
        if (!mBSDF_incvec(v, bsdf, in_recip+.5)) | 
| 63 | 
  | 
  | 
                return(in_recip);               /* XXX should be error! */ | 
| 64 | 
  | 
  | 
        v[2] = -v[2]; | 
| 65 | 
  | 
  | 
        return(mBSDF_outndx(bsdf, v)); | 
| 66 | 
  | 
  | 
} | 
| 67 | 
  | 
  | 
 | 
| 68 | 
  | 
  | 
/* Convert between output and input indices for reciprocity */ | 
| 69 | 
  | 
  | 
static int | 
| 70 | 
  | 
  | 
recip_in_from_out(const SDMat *bsdf, int out_recip) | 
| 71 | 
  | 
  | 
{ | 
| 72 | 
  | 
  | 
        FVECT   v; | 
| 73 | 
  | 
  | 
 | 
| 74 | 
  | 
  | 
        if (!mBSDF_outvec(v, bsdf, out_recip+.5)) | 
| 75 | 
  | 
  | 
                return(out_recip);              /* XXX should be error! */ | 
| 76 | 
  | 
  | 
        v[2] = -v[2]; | 
| 77 | 
  | 
  | 
        return(mBSDF_incndx(bsdf, v)); | 
| 78 | 
  | 
  | 
} | 
| 79 | 
  | 
  | 
 | 
| 80 | 
  | 
  | 
/* Convert a BSDF to our matrix representation, applying reciprocity */ | 
| 81 | 
  | 
  | 
static CMATRIX * | 
| 82 | 
greg | 
2.3 | 
cm_bsdf_recip(const COLOR diffBSDF, const SDMat *bsdf) | 
| 83 | 
greg | 
2.1 | 
{ | 
| 84 | 
  | 
  | 
        CMATRIX *cm = cm_alloc(bsdf->ninc, bsdf->nout); | 
| 85 | 
  | 
  | 
        int     nbadohm = 0; | 
| 86 | 
  | 
  | 
        int     nneg = 0; | 
| 87 | 
  | 
  | 
        int     r, c; | 
| 88 | 
  | 
  | 
                                        /* loop over incident angles */ | 
| 89 | 
  | 
  | 
        for (c = 0; c < cm->ncols; c++) { | 
| 90 | 
  | 
  | 
                const int       ro = recip_out_from_in(bsdf,c); | 
| 91 | 
  | 
  | 
                const double    dom = mBSDF_outohm(bsdf,ro); | 
| 92 | 
  | 
  | 
                                        /* projected solid angle */ | 
| 93 | 
  | 
  | 
                nbadohm += (dom <= 0); | 
| 94 | 
  | 
  | 
 | 
| 95 | 
  | 
  | 
                for (r = 0; r < cm->nrows; r++) { | 
| 96 | 
  | 
  | 
                        const int       ri = recip_in_from_out(bsdf,r); | 
| 97 | 
  | 
  | 
                        float           f = mBSDF_value(bsdf,ri,ro); | 
| 98 | 
  | 
  | 
                        COLORV          *mp = cm_lval(cm,r,c); | 
| 99 | 
  | 
  | 
                                        /* check BSDF value */ | 
| 100 | 
  | 
  | 
                        if ((f <= 0) | (dom <= 0)) { | 
| 101 | 
  | 
  | 
                                nneg += (f < -FTINY); | 
| 102 | 
greg | 
2.3 | 
                                setcolor(mp, .0f, .0f, .0f); | 
| 103 | 
  | 
  | 
                        } else if (bsdf->chroma != NULL) { | 
| 104 | 
  | 
  | 
                                C_COLOR cxy; | 
| 105 | 
greg | 
2.4 | 
                                c_decodeChroma(&cxy, mBSDF_chroma(bsdf,ri,ro)); | 
| 106 | 
greg | 
2.3 | 
                                ccy2rgb(&cxy, f, mp); | 
| 107 | 
  | 
  | 
                        } else | 
| 108 | 
  | 
  | 
                                setcolor(mp, f, f, f); | 
| 109 | 
greg | 
2.2 | 
                        addcolor(mp, diffBSDF); | 
| 110 | 
greg | 
2.1 | 
                        scalecolor(mp, dom); | 
| 111 | 
  | 
  | 
                } | 
| 112 | 
  | 
  | 
        } | 
| 113 | 
  | 
  | 
        if (nneg | nbadohm) { | 
| 114 | 
  | 
  | 
                sprintf(errmsg, | 
| 115 | 
  | 
  | 
                    "BTDF has %d negatives and %d bad incoming solid angles", | 
| 116 | 
  | 
  | 
                                nneg, nbadohm); | 
| 117 | 
  | 
  | 
                error(WARNING, errmsg); | 
| 118 | 
  | 
  | 
        } | 
| 119 | 
  | 
  | 
        return(cm); | 
| 120 | 
  | 
  | 
} | 
| 121 | 
  | 
  | 
 | 
| 122 | 
greg | 
2.2 | 
/* Return a Lambertian (diffuse) matrix */ | 
| 123 | 
  | 
  | 
static CMATRIX * | 
| 124 | 
  | 
  | 
cm_bsdf_Lamb(const COLOR diffBSDF) | 
| 125 | 
  | 
  | 
{ | 
| 126 | 
  | 
  | 
        CMATRIX *cm = cm_alloc(145, 145);       /* this is a hack */ | 
| 127 | 
  | 
  | 
        int     r, c; | 
| 128 | 
  | 
  | 
 | 
| 129 | 
  | 
  | 
        for (c = 0; c < cm->ncols; c++) | 
| 130 | 
  | 
  | 
                for (r = 0; r < cm->nrows; r++) { | 
| 131 | 
  | 
  | 
                        COLORV  *mp = cm_lval(cm,r,c); | 
| 132 | 
  | 
  | 
                        copycolor(mp, diffBSDF); | 
| 133 | 
  | 
  | 
                } | 
| 134 | 
  | 
  | 
        return(cm); | 
| 135 | 
  | 
  | 
} | 
| 136 | 
  | 
  | 
 | 
| 137 | 
  | 
  | 
/* Load and convert a matrix BTDF from the given XML file */ | 
| 138 | 
greg | 
2.1 | 
CMATRIX * | 
| 139 | 
greg | 
2.2 | 
cm_loadBTDF(char *fname) | 
| 140 | 
greg | 
2.1 | 
{ | 
| 141 | 
  | 
  | 
        CMATRIX         *Tmat; | 
| 142 | 
  | 
  | 
        char            *fpath; | 
| 143 | 
  | 
  | 
        int             recip; | 
| 144 | 
  | 
  | 
        SDError         ec; | 
| 145 | 
  | 
  | 
        SDData          myBSDF; | 
| 146 | 
  | 
  | 
        SDSpectralDF    *tdf; | 
| 147 | 
greg | 
2.3 | 
        COLOR           diffBSDF; | 
| 148 | 
greg | 
2.1 | 
                                        /* find path to BSDF file */ | 
| 149 | 
  | 
  | 
        fpath = getpath(fname, getrlibpath(), R_OK); | 
| 150 | 
  | 
  | 
        if (fpath == NULL) { | 
| 151 | 
  | 
  | 
                sprintf(errmsg, "cannot find BSDF file '%s'", fname); | 
| 152 | 
  | 
  | 
                error(USER, errmsg); | 
| 153 | 
  | 
  | 
        } | 
| 154 | 
  | 
  | 
        SDclearBSDF(&myBSDF, fname);    /* load XML and check type */ | 
| 155 | 
  | 
  | 
        ec = SDloadFile(&myBSDF, fpath); | 
| 156 | 
  | 
  | 
        if (ec) | 
| 157 | 
  | 
  | 
                error(USER, transSDError(ec)); | 
| 158 | 
greg | 
2.2 | 
        ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, diffBSDF); | 
| 159 | 
greg | 
2.1 | 
        recip = (myBSDF.tb == NULL); | 
| 160 | 
  | 
  | 
        tdf = recip ? myBSDF.tf : myBSDF.tb; | 
| 161 | 
  | 
  | 
        if (tdf == NULL) {              /* no non-Lambertian transmission? */ | 
| 162 | 
  | 
  | 
                SDfreeBSDF(&myBSDF); | 
| 163 | 
greg | 
2.2 | 
                return(cm_bsdf_Lamb(diffBSDF)); | 
| 164 | 
greg | 
2.1 | 
        } | 
| 165 | 
  | 
  | 
        if (tdf->ncomp != 1 || tdf->comp[0].func != &SDhandleMtx) { | 
| 166 | 
  | 
  | 
                sprintf(errmsg, "unsupported BSDF '%s'", fpath); | 
| 167 | 
  | 
  | 
                error(USER, errmsg); | 
| 168 | 
  | 
  | 
        } | 
| 169 | 
  | 
  | 
                                        /* convert BTDF to matrix */ | 
| 170 | 
greg | 
2.3 | 
        Tmat = recip ? cm_bsdf_recip(diffBSDF, (SDMat *)tdf->comp[0].dist) | 
| 171 | 
  | 
  | 
                        : cm_bsdf(diffBSDF, (SDMat *)tdf->comp[0].dist); | 
| 172 | 
greg | 
2.1 | 
                                        /* free BSDF and return */ | 
| 173 | 
  | 
  | 
        SDfreeBSDF(&myBSDF); | 
| 174 | 
  | 
  | 
        return(Tmat); | 
| 175 | 
  | 
  | 
} |