ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/cmbsdf.c
(Generate patch)

Comparing ray/src/util/cmbsdf.c (file contents):
Revision 2.1 by greg, Mon Jan 20 21:29:04 2014 UTC vs.
Revision 2.10 by greg, Sat Mar 27 17:50:18 2021 UTC

# Line 15 | Line 15 | static const char RCSid[] = "$Id$";
15  
16   /* Convert a BSDF to our matrix representation */
17   static CMATRIX *
18 < cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf)
18 > cm_bsdf(const COLOR diffBSDF, const SDMat *bsdf)
19   {
20          CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc);
21          int     nbadohm = 0;
# Line 28 | Line 28 | cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, con
28                  nbadohm += (dom <= 0);
29  
30                  for (r = 0; r < cm->nrows; r++) {
31 <                        float   f = mBSDF_value(bsdf,c,r);
31 >                        float   f = mBSDF_value(bsdf,r,c);
32                          COLORV  *mp = cm_lval(cm,r,c);
33                                          /* check BSDF value */
34                          if ((f <= 0) | (dom <= 0)) {
35                                  nneg += (f < -FTINY);
36 <                                f = .0f;
37 <                        }
38 <                        copycolor(mp, specCol);
39 <                        scalecolor(mp, f);
40 <                        addcolor(mp, bsdfLamb);
36 >                                setcolor(mp, .0f, .0f, .0f);
37 >                        } else if (bsdf->chroma != NULL) {
38 >                                C_COLOR cxy;
39 >                                c_decodeChroma(&cxy, mBSDF_chroma(bsdf,r,c));
40 >                                ccy2rgb(&cxy, f, mp);
41 >                        } else
42 >                                setcolor(mp, f, f, f);
43 >                        addcolor(mp, diffBSDF);
44                          scalecolor(mp, dom);
45                  }
46          }
# Line 76 | Line 79 | recip_in_from_out(const SDMat *bsdf, int out_recip)
79  
80   /* Convert a BSDF to our matrix representation, applying reciprocity */
81   static CMATRIX *
82 < cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf)
82 > cm_bsdf_recip(const COLOR diffBSDF, const SDMat *bsdf)
83   {
84          CMATRIX *cm = cm_alloc(bsdf->ninc, bsdf->nout);
85          int     nbadohm = 0;
# Line 91 | Line 94 | cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCo
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);
97 >                        float           f = mBSDF_value(bsdf,ro,ri);
98                          COLORV          *mp = cm_lval(cm,r,c);
99                                          /* check BSDF value */
100                          if ((f <= 0) | (dom <= 0)) {
101                                  nneg += (f < -FTINY);
102 <                                f = .0f;
103 <                        }
104 <                        copycolor(mp, specCol);
105 <                        scalecolor(mp, f);
106 <                        addcolor(mp, bsdfLamb);
102 >                                setcolor(mp, .0f, .0f, .0f);
103 >                        } else if (bsdf->chroma != NULL) {
104 >                                C_COLOR cxy;
105 >                                c_decodeChroma(&cxy, mBSDF_chroma(bsdf,ro,ri));
106 >                                ccy2rgb(&cxy, f, mp);
107 >                        } else
108 >                                setcolor(mp, f, f, f);
109 >                        addcolor(mp, diffBSDF);
110                          scalecolor(mp, dom);
111                  }
112          }
# Line 113 | Line 119 | cm_bsdf_recip(const COLOR bsdfLamb, const COLOR specCo
119          return(cm);
120   }
121  
122 < /* Load and convert a matrix BSDF from the given XML file */
122 > /* 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 >        ANGLE_BASIS     *abase = abase_list;
128 >        int             r, c;
129 >
130 >        while (abase->nangles != cm->nrows)
131 >                if (++abase >= abase_list+nabases)
132 >                        error(INTERNAL, "Code error in cm_bsdf_Lamb()");
133 >
134 >        for (c = 0; c < cm->ncols; c++) {
135 >                const double    dom = io_getohm(c,abase);
136 >                for (r = 0; r < cm->nrows; r++) {
137 >                        COLORV  *mp = cm_lval(cm,r,c);
138 >                        copycolor(mp, diffBSDF);
139 >                        scalecolor(mp, dom);
140 >                }
141 >        }
142 >        return(cm);
143 > }
144 >
145 > /* Load and convert a matrix BTDF from the given XML file */
146   CMATRIX *
147 < cm_loadBSDF(char *fname, COLOR cLamb)
147 > cm_loadBTDF(const char *fname)
148   {
149          CMATRIX         *Tmat;
121        char            *fpath;
150          int             recip;
151          SDError         ec;
152          SDData          myBSDF;
153          SDSpectralDF    *tdf;
154 <        COLOR           bsdfLamb, specCol;
155 <                                        /* find path to BSDF file */
128 <        fpath = getpath(fname, getrlibpath(), R_OK);
129 <        if (fpath == NULL) {
130 <                sprintf(errmsg, "cannot find BSDF file '%s'", fname);
131 <                error(USER, errmsg);
132 <        }
154 >        COLOR           diffBTDF;
155 >
156          SDclearBSDF(&myBSDF, fname);    /* load XML and check type */
157 <        ec = SDloadFile(&myBSDF, fpath);
157 >        ec = SDloadFile(&myBSDF, fname);
158          if (ec)
159                  error(USER, transSDError(ec));
137        ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, bsdfLamb);
160          recip = (myBSDF.tb == NULL);
161 +        if (recip)
162 +                ccy2rgb(&myBSDF.tLambFront.spec, myBSDF.tLambFront.cieY/PI, diffBTDF);
163 +        else
164 +                ccy2rgb(&myBSDF.tLambBack.spec, myBSDF.tLambBack.cieY/PI, diffBTDF);
165          tdf = recip ? myBSDF.tf : myBSDF.tb;
166          if (tdf == NULL) {              /* no non-Lambertian transmission? */
141                if (cLamb != NULL)
142                        copycolor(cLamb, bsdfLamb);
167                  SDfreeBSDF(&myBSDF);
168 <                return(NULL);
168 >                return(cm_bsdf_Lamb(diffBTDF));
169          }
170          if (tdf->ncomp != 1 || tdf->comp[0].func != &SDhandleMtx) {
171 <                sprintf(errmsg, "unsupported BSDF '%s'", fpath);
171 >                sprintf(errmsg, "unsupported BSDF '%s'", fname);
172                  error(USER, errmsg);
173          }
174                                          /* convert BTDF to matrix */
175 <        ccy2rgb(&tdf->comp[0].cspec[0], 1., specCol);
176 <        Tmat = recip ? cm_bsdf_recip(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist)
153 <                        : cm_bsdf(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist);
154 <        if (cLamb != NULL)              /* Lambertian is included */
155 <                setcolor(cLamb, .0, .0, .0);
175 >        Tmat = recip ? cm_bsdf_recip(diffBTDF, (SDMat *)tdf->comp[0].dist)
176 >                        : cm_bsdf(diffBTDF, (SDMat *)tdf->comp[0].dist);
177                                          /* free BSDF and return */
178          SDfreeBSDF(&myBSDF);
179          return(Tmat);
180 + }
181 +
182 + /* Load and convert a matrix BRDF from the given XML file */
183 + CMATRIX *
184 + cm_loadBRDF(const char *fname, int backside)
185 + {
186 +        CMATRIX         *Rmat;
187 +        SDError         ec;
188 +        SDData          myBSDF;
189 +        SDSpectralDF    *rdf;
190 +        COLOR           diffBRDF;
191 +
192 +        SDclearBSDF(&myBSDF, fname);    /* load XML and check type */
193 +        ec = SDloadFile(&myBSDF, fname);
194 +        if (ec)
195 +                error(USER, transSDError(ec));
196 +        if (backside) {
197 +                ccy2rgb(&myBSDF.rLambBack.spec, myBSDF.rLambBack.cieY/PI, diffBRDF);
198 +                rdf = myBSDF.rb;
199 +        } else {
200 +                ccy2rgb(&myBSDF.rLambFront.spec, myBSDF.rLambFront.cieY/PI, diffBRDF);
201 +                rdf = myBSDF.rf;
202 +        }
203 +        if (rdf == NULL) {              /* no non-Lambertian reflection? */
204 +                SDfreeBSDF(&myBSDF);
205 +                return(cm_bsdf_Lamb(diffBRDF));
206 +        }
207 +        if (rdf->ncomp != 1 || rdf->comp[0].func != &SDhandleMtx) {
208 +                sprintf(errmsg, "unsupported BSDF '%s'", fname);
209 +                error(USER, errmsg);
210 +        }
211 +                                        /* convert BRDF to matrix */
212 +        Rmat = cm_bsdf(diffBRDF, (SDMat *)rdf->comp[0].dist);
213 +                                        /* free BSDF and return */
214 +        SDfreeBSDF(&myBSDF);
215 +        return(Rmat);
216   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines