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

Comparing ray/src/common/bsdf_m.c (file contents):
Revision 3.3 by greg, Fri Feb 18 02:41:55 2011 UTC vs.
Revision 3.28 by greg, Fri Mar 21 17:49:53 2014 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   *
12   */
13  
14 + #define _USE_MATH_DEFINES
15   #include "rtio.h"
16   #include <stdlib.h>
17   #include <math.h>
# Line 19 | Line 20 | static const char RCSid[] = "$Id$";
20   #include "bsdf.h"
21   #include "bsdf_m.h"
22  
22 #ifndef FTINY
23 #define FTINY           1e-6
24 #endif
25
23   /* Function return codes */
24   #define RC_GOOD         1
25   #define RC_FAIL         0
# Line 32 | Line 29 | static const char RCSid[] = "$Id$";
29   #define RC_INTERR       (-4)
30   #define RC_MEMERR       (-5)
31  
32 < #define MAXLATS         46              /* maximum number of latitudes */
36 <
37 < /* BSDF angle specification */
38 < typedef struct {
39 <        char    name[64];               /* basis name */
40 <        int     nangles;                /* total number of directions */
41 <        struct {
42 <                float   tmin;                   /* starting theta */
43 <                int     nphis;                  /* number of phis (0 term) */
44 <        } lat[MAXLATS+1];               /* latitudes */
45 < } ANGLE_BASIS;
46 <
47 < #define MAXABASES       7               /* limit on defined bases */
48 <
49 < static ANGLE_BASIS      abase_list[MAXABASES] = {
32 > ANGLE_BASIS     abase_list[MAXABASES] = {
33          {
34                  "LBNL/Klems Full", 145,
35 <                { {-5., 1},
35 >                { {0., 1},
36                  {5., 8},
37                  {15., 16},
38                  {25., 20},
# Line 61 | Line 44 | static ANGLE_BASIS     abase_list[MAXABASES] = {
44                  {90., 0} }
45          }, {
46                  "LBNL/Klems Half", 73,
47 <                { {-6.5, 1},
47 >                { {0., 1},
48                  {6.5, 8},
49                  {19.5, 12},
50                  {32.5, 16},
# Line 71 | Line 54 | static ANGLE_BASIS     abase_list[MAXABASES] = {
54                  {90., 0} }
55          }, {
56                  "LBNL/Klems Quarter", 41,
57 <                { {-9., 1},
57 >                { {0., 1},
58                  {9., 8},
59                  {27., 12},
60                  {46., 12},
# Line 80 | Line 63 | static ANGLE_BASIS     abase_list[MAXABASES] = {
63          }
64   };
65  
66 < static int      nabases = 3;    /* current number of defined bases */
66 > int             nabases = 3;            /* current number of defined bases */
67  
68   static int
69   fequal(double a, double b)
70   {
71 <        if (b != .0)
71 >        if (b != 0)
72                  a = a/b - 1.;
73          return (a <= 1e-6) & (a >= -1e-6);
74   }
75  
76 < /* returns the name of the given tag */
94 < #ifdef ezxml_name
95 < #undef ezxml_name
96 < static char *
97 < ezxml_name(ezxml_t xml)
98 < {
99 <        if (xml == NULL)
100 <                return NULL;
101 <        return xml->name;
102 < }
103 < #endif
104 <
105 < /* returns the given tag's character content or empty string if none */
76 > /* Returns the given tag's character content or empty string if none */
77   #ifdef ezxml_txt
78   #undef ezxml_txt
79   static char *
# Line 161 | Line 132 | SDnewMatrix(int ni, int no)
132   /* Free a BSDF matrix */
133   #define SDfreeMatrix            free
134  
135 < /* get vector for this angle basis index */
136 < static int
137 < ab_getvec(FVECT v, int ndx, double randX, void *p)
135 > /* Get vector for this angle basis index (front exiting) */
136 > int
137 > fo_getvec(FVECT v, double ndxr, void *p)
138   {
139 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
139 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
140 >        int             ndx = (int)ndxr;
141 >        double          randX = ndxr - ndx;
142          double          rx[2];
143          int             li;
144          double          pol, azi, d;
145          
146 <        if ((ndx < 0) | (ndx >= ab->nangles))
146 >        if ((ndxr < 0) | (ndx >= ab->nangles))
147                  return RC_FAIL;
148          for (li = 0; ndx >= ab->lat[li].nphis; li++)
149                  ndx -= ab->lat[li].nphis;
# Line 185 | Line 158 | ab_getvec(FVECT v, int ndx, double randX, void *p)
158          return RC_GOOD;
159   }
160  
161 < /* get index corresponding to the given vector */
162 < static int
163 < ab_getndx(const FVECT v, void *p)
161 > /* Get index corresponding to the given vector (front exiting) */
162 > int
163 > fo_getndx(const FVECT v, void *p)
164   {
165 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
165 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
166          int     li, ndx;
167 <        double  pol, azi, d;
167 >        double  pol, azi;
168  
169          if (v == NULL)
170                  return -1;
171 <        if ((v[2] < .0) | (v[2] > 1.0))
171 >        if ((v[2] < 0) | (v[2] > 1.))
172                  return -1;
173 <        pol = 180.0/M_PI*acos(v[2]);
173 >        pol = 180.0/M_PI*Acos(v[2]);
174          azi = 180.0/M_PI*atan2(v[1], v[0]);
175          if (azi < 0.0) azi += 360.0;
176          for (li = 1; ab->lat[li].tmin <= pol; li++)
# Line 214 | Line 187 | ab_getndx(const FVECT v, void *p)
187   /* compute square of real value */
188   static double sq(double x) { return x*x; }
189  
190 < /* get projected solid angle for this angle basis index */
191 < static double
192 < ab_getohm(int ndx, void *p)
190 > /* Get projected solid angle for this angle basis index (universal) */
191 > double
192 > io_getohm(int ndx, void *p)
193   {
194          static int      last_li = -1;
195          static double   last_ohm;
# Line 231 | Line 204 | ab_getohm(int ndx, void *p)
204          if (li == last_li)                      /* cached latitude? */
205                  return last_ohm;
206          last_li = li;
234        theta1 = M_PI/180. * ab->lat[li+1].tmin;
235        if (ab->lat[li].nphis == 1)             /* special case */
236                return last_ohm = M_PI*(1. - sq(cos(theta1)));
207          theta = M_PI/180. * ab->lat[li].tmin;
208 +        theta1 = M_PI/180. * ab->lat[li+1].tmin;
209          return last_ohm = M_PI*(sq(cos(theta)) - sq(cos(theta1))) /
210                                  (double)ab->lat[li].nphis;
211   }
212  
213 < /* get reverse vector for this angle basis index */
214 < static int
215 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
213 > /* Get vector for this angle basis index (back incident) */
214 > int
215 > bi_getvec(FVECT v, double ndxr, void *p)
216   {
217 <        int     na = (*(ANGLE_BASIS *)p).nangles;
247 <
248 <        if (!ab_getvec(v, ndx, randX, p))
217 >        if (!fo_getvec(v, ndxr, p))
218                  return RC_FAIL;
219  
220          v[0] = -v[0];
# Line 255 | Line 224 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
224          return RC_GOOD;
225   }
226  
227 < /* get index corresponding to the reverse vector */
228 < static int
229 < ab_getndxR(const FVECT v, void *p)
227 > /* Get index corresponding to the vector (back incident) */
228 > int
229 > bi_getndx(const FVECT v, void *p)
230   {
231          FVECT  v2;
232          
# Line 265 | Line 234 | ab_getndxR(const FVECT v, void *p)
234          v2[1] = -v[1];
235          v2[2] = -v[2];
236  
237 <        return ab_getndx(v2, p);
237 >        return fo_getndx(v2, p);
238   }
239  
240 + /* Get vector for this angle basis index (back exiting) */
241 + int
242 + bo_getvec(FVECT v, double ndxr, void *p)
243 + {
244 +        if (!fo_getvec(v, ndxr, p))
245 +                return RC_FAIL;
246 +
247 +        v[2] = -v[2];
248 +
249 +        return RC_GOOD;
250 + }
251 +
252 + /* Get index corresponding to the vector (back exiting) */
253 + int
254 + bo_getndx(const FVECT v, void *p)
255 + {
256 +        FVECT  v2;
257 +        
258 +        v2[0] = v[0];
259 +        v2[1] = v[1];
260 +        v2[2] = -v[2];
261 +
262 +        return fo_getndx(v2, p);
263 + }
264 +
265 + /* Get vector for this angle basis index (front incident) */
266 + int
267 + fi_getvec(FVECT v, double ndxr, void *p)
268 + {
269 +        if (!fo_getvec(v, ndxr, p))
270 +                return RC_FAIL;
271 +
272 +        v[0] = -v[0];
273 +        v[1] = -v[1];
274 +
275 +        return RC_GOOD;
276 + }
277 +
278 + /* Get index corresponding to the vector (front incident) */
279 + int
280 + fi_getndx(const FVECT v, void *p)
281 + {
282 +        FVECT  v2;
283 +        
284 +        v2[0] = -v[0];
285 +        v2[1] = -v[1];
286 +        v2[2] = v[2];
287 +
288 +        return fo_getndx(v2, p);
289 + }
290 +
291   /* load custom BSDF angle basis */
292   static int
293   load_angle_basis(ezxml_t wab)
# Line 299 | Line 319 | load_angle_basis(ezxml_t wab)
319                                  ezxml_child(ezxml_child(wbb,
320                                          "ThetaBounds"), "UpperTheta")));
321                  if (!i)
322 <                        abase_list[nabases].lat[i].tmin =
303 <                                        -abase_list[nabases].lat[i+1].tmin;
322 >                        abase_list[nabases].lat[0].tmin = 0;
323                  else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
324                                          "ThetaBounds"), "LowerTheta"))),
325                                  abase_list[nabases].lat[i].tmin)) {
326                          sprintf(SDerrorDetail, "Theta values disagree in '%s'",
327 <                                abname);
327 >                                        abname);
328                          return RC_DATERR;
329                  }
330                  abase_list[nabases].nangles +=
# Line 315 | Line 334 | load_angle_basis(ezxml_t wab)
334                                  (abase_list[nabases].lat[i].nphis == 1 &&
335                                  abase_list[nabases].lat[i].tmin > FTINY)) {
336                          sprintf(SDerrorDetail, "Illegal phi count in '%s'",
337 <                                abname);
337 >                                        abname);
338                          return RC_DATERR;
339                  }
340          }
# Line 350 | Line 369 | get_extrema(SDSpectralDF *df)
369          }
370          free(ohma);
371                                          /* need incoming solid angles, too? */
372 <        if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm ||
354 <                                dp->ib_priv != dp->ob_priv) {
372 >        if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) {
373                  double  ohm;
374                  for (i = dp->ninc; i--; )
375                          if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA)
# Line 364 | Line 382 | get_extrema(SDSpectralDF *df)
382   static int
383   load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
384   {
367        char            *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
368        char            *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
385          SDSpectralDF    *df;
386          SDMat           *dp;
387          char            *sdata;
388          int             inbi, outbi;
389          int             i;
374
375        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
376                sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'",
377                                sd->name);
378                return RC_FORMERR;
379        }
390                                          /* allocate BSDF component */
391          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
392 +        if (!sdata)
393 +                return RC_FAIL;
394 +        /*
395 +         * Remember that front and back are reversed from WINDOW 6 orientations
396 +         */
397          if (!strcasecmp(sdata, "Transmission Front")) {
398 +                if (sd->tb != NULL)
399 +                        SDfreeSpectralDF(sd->tb);
400 +                if ((sd->tb = SDnewSpectralDF(1)) == NULL)
401 +                        return RC_MEMERR;
402 +                df = sd->tb;
403 +        } else if (!strcasecmp(sdata, "Transmission Back")) {
404                  if (sd->tf != NULL)
405                          SDfreeSpectralDF(sd->tf);
406                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
407                          return RC_MEMERR;
408                  df = sd->tf;
409          } else if (!strcasecmp(sdata, "Reflection Front")) {
389                if (sd->rf != NULL)
390                        SDfreeSpectralDF(sd->rf);
391                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
392                        return RC_MEMERR;
393                df = sd->rf;
394        } else if (!strcasecmp(sdata, "Reflection Back")) {
410                  if (sd->rb != NULL)
411                          SDfreeSpectralDF(sd->rb);
412                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
413                          return RC_MEMERR;
414                  df = sd->rb;
415 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
416 +                if (sd->rf != NULL)
417 +                        SDfreeSpectralDF(sd->rf);
418 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
419 +                        return RC_MEMERR;
420 +                df = sd->rf;
421          } else
422                  return RC_FAIL;
423 +        /* XXX should also check "ScatteringDataType" for consistency? */
424                                          /* get angle bases */
425          sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
426          if (!sdata || !*sdata) {
# Line 407 | Line 429 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
429                  return RC_FORMERR;
430          }
431          for (inbi = nabases; inbi--; )
432 <                if (!strcasecmp(cbasis, abase_list[inbi].name))
432 >                if (!strcasecmp(sdata, abase_list[inbi].name))
433                          break;
434          if (inbi < 0) {
435 <                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'",
414 <                                cbasis);
435 >                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata);
436                  return RC_FORMERR;
437          }
438          sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
# Line 421 | Line 442 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
442                  return RC_FORMERR;
443          }
444          for (outbi = nabases; outbi--; )
445 <                if (!strcasecmp(rbasis, abase_list[outbi].name))
445 >                if (!strcasecmp(sdata, abase_list[outbi].name))
446                          break;
447          if (outbi < 0) {
448 <                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis);
448 >                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata);
449                  return RC_FORMERR;
450          }
451                                          /* allocate BSDF matrix */
452          dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles);
453          if (dp == NULL)
454                  return RC_MEMERR;
455 <        dp->ib_priv = (void *)&abase_list[inbi];
456 <        dp->ob_priv = (void *)&abase_list[outbi];
455 >        dp->ib_priv = &abase_list[inbi];
456 >        dp->ob_priv = &abase_list[outbi];
457          if (df == sd->tf) {
458 <                dp->ib_vec = ab_getvecR;
459 <                dp->ib_ndx = ab_getndxR;
460 <                dp->ob_vec = ab_getvec;
461 <                dp->ob_ndx = ab_getndx;
458 >                dp->ib_vec = &fi_getvec;
459 >                dp->ib_ndx = &fi_getndx;
460 >                dp->ob_vec = &bo_getvec;
461 >                dp->ob_ndx = &bo_getndx;
462 >        } else if (df == sd->tb) {
463 >                dp->ib_vec = &bi_getvec;
464 >                dp->ib_ndx = &bi_getndx;
465 >                dp->ob_vec = &fo_getvec;
466 >                dp->ob_ndx = &fo_getndx;
467          } else if (df == sd->rf) {
468 <                dp->ib_vec = ab_getvec;
469 <                dp->ib_ndx = ab_getndx;
470 <                dp->ob_vec = ab_getvec;
471 <                dp->ob_ndx = ab_getndx;
468 >                dp->ib_vec = &fi_getvec;
469 >                dp->ib_ndx = &fi_getndx;
470 >                dp->ob_vec = &fo_getvec;
471 >                dp->ob_ndx = &fo_getndx;
472          } else /* df == sd->rb */ {
473 <                dp->ib_vec = ab_getvecR;
474 <                dp->ib_ndx = ab_getndxR;
475 <                dp->ob_vec = ab_getvecR;
476 <                dp->ob_ndx = ab_getndxR;
473 >                dp->ib_vec = &bi_getvec;
474 >                dp->ib_ndx = &bi_getndx;
475 >                dp->ob_vec = &bo_getvec;
476 >                dp->ob_ndx = &bo_getndx;
477          }
478 <        dp->ib_ohm = ab_getohm;
479 <        dp->ob_ohm = ab_getohm;
478 >        dp->ib_ohm = &io_getohm;
479 >        dp->ob_ohm = &io_getohm;
480          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
481          df->comp[0].dist = dp;
482          df->comp[0].func = &SDhandleMtx;
483                                          /* read BSDF data */
484 <        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
484 >        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
485          if (!sdata || !*sdata) {
486                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
487                                  sd->name);
488                  return RC_FORMERR;
489          }
490          for (i = 0; i < dp->ninc*dp->nout; i++) {
491 <                char  *sdnext = fskip(sdata);
491 >                char    *sdnext = fskip(sdata);
492 >                double  val;
493                  if (sdnext == NULL) {
494                          sprintf(SDerrorDetail,
495                                  "Bad/missing BSDF ScatteringData in '%s'",
496                                          sd->name);
497                          return RC_FORMERR;
498                  }
499 <                while (*sdnext && isspace(*sdnext))
499 >                while (isspace(*sdnext))
500                          sdnext++;
501                  if (*sdnext == ',') sdnext++;
502 +                if ((val = atof(sdata)) < 0)
503 +                        val = 0;        /* don't allow negative values */
504                  if (rowinc) {
505                          int     r = i/dp->nout;
506 <                        int     c = i - c*dp->nout;
507 <                        mBSDF_value(dp,r,c) = atof(sdata);
506 >                        int     c = i - r*dp->nout;
507 >                        mBSDF_value(dp,r,c) = val;
508                  } else
509 <                        dp->bsdf[i] = atof(sdata);
509 >                        dp->bsdf[i] = val;
510                  sdata = sdnext;
511          }
512          return get_extrema(df);
# Line 494 | Line 523 | subtract_min(SDMat *sm)
523          for (i = n; --i; )
524                  if (sm->bsdf[i] < minv)
525                          minv = sm->bsdf[i];
526 +        
527 +        if (minv <= FTINY)
528 +                return .0;
529 +
530          for (i = n; i--; )
531                  sm->bsdf[i] -= minv;
532  
# Line 519 | Line 552 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
552                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
553                  dv->cieY += ymin;
554          }
555 <        df->maxHemi -= dv->cieY;        /* correct minimum hemispherical */
556 <        dv->spec.clock++;               /* make sure everything is set */
555 >        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
556 >                                        /* make sure everything is set */
557          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
558   }
559  
560   /* Load a BSDF matrix from an open XML file */
561   SDError
562 < SDloadMtx(SDData *sd, ezxml_t fl)
562 > SDloadMtx(SDData *sd, ezxml_t wtl)
563   {
564 <        ezxml_t                 wtl, wld, wdb;
565 <        int                     rowIn;
566 <        struct BSDF_data        *dp;
567 <        char                    *txt;
568 <        int                     rval;
569 <        
570 <        if (strcmp(ezxml_name(fl), "WindowElement")) {
564 >        ezxml_t         wld, wdb;
565 >        int             rowIn;
566 >        char            *txt;
567 >        int             rval;
568 >                                        /* basic checks and data ordering */
569 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
570 >                        "DataDefinition"), "IncidentDataStructure"));
571 >        if (txt == NULL || !*txt) {
572                  sprintf(SDerrorDetail,
573 <                        "BSDF \"%s\": top level node not 'WindowElement'",
573 >                        "BSDF \"%s\": missing IncidentDataStructure",
574                                  sd->name);
575                  return SDEformat;
576          }
543        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
544        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
545                        "DataDefinition"), "IncidentDataStructure"));
577          if (!strcasecmp(txt, "Rows"))
578                  rowIn = 1;
579          else if (!strcasecmp(txt, "Columns"))
# Line 553 | Line 584 | SDloadMtx(SDData *sd, ezxml_t fl)
584                                  sd->name);
585                  return SDEsupport;
586          }
587 <                                /* get angle basis */
588 <        rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
589 <                                "DataDefinition"), "AngleBasis"));
590 <        if (rval < 0)
591 <                goto err_return;
592 <                                /* load BSDF components */
587 >                                        /* get angle bases */
588 >        for (wld = ezxml_child(ezxml_child(wtl, "DataDefinition"), "AngleBasis");
589 >                                wld != NULL; wld = wld->next) {
590 >                rval = load_angle_basis(wld);
591 >                if (rval < 0)
592 >                        return convert_errcode(rval);
593 >        }
594 >                                        /* load BSDF components */
595          for (wld = ezxml_child(wtl, "WavelengthData");
596                                  wld != NULL; wld = wld->next) {
597                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 567 | Line 600 | SDloadMtx(SDData *sd, ezxml_t fl)
600                  for (wdb = ezxml_child(wld, "WavelengthDataBlock");
601                                          wdb != NULL; wdb = wdb->next)
602                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
603 <                                goto err_return;
603 >                                return convert_errcode(rval);
604          }
605 <                                /* separate diffuse components */
605 >                                        /* separate diffuse components */
606          extract_diffuse(&sd->rLambFront, sd->rf);
607          extract_diffuse(&sd->rLambBack, sd->rb);
608 <        extract_diffuse(&sd->tLamb, sd->tf);
609 <                                /* return success */
608 >        if (sd->tf != NULL)
609 >                extract_diffuse(&sd->tLamb, sd->tf);
610 >        if (sd->tb != NULL)
611 >                extract_diffuse(&sd->tLamb, sd->tb);
612 >                                        /* return success */
613          return SDEnone;
578 err_return:                     /* jump here on failure */
579        if (sd->rf != NULL) {
580                SDfreeSpectralDF(sd->rf);
581                sd->rf = NULL;
582        }
583        if (sd->rb != NULL) {
584                SDfreeSpectralDF(sd->rb);
585                sd->rb = NULL;
586        }
587        if (sd->tf != NULL) {
588                SDfreeSpectralDF(sd->tf);
589                sd->tf = NULL;
590        }
591        return convert_errcode(rval);
614   }
615  
616   /* Get Matrix BSDF value */
617   static int
618   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
619 <                                const FVECT inVec, const void *dist)
619 >                                const FVECT inVec, SDComponent *sdc)
620   {
621 <        const SDMat     *dp = (const SDMat *)dist;
621 >        const SDMat     *dp;
622          int             i_ndx, o_ndx;
623 +                                        /* check arguments */
624 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
625 +                        || (dp = (SDMat *)sdc->dist) == NULL)
626 +                return 0;
627                                          /* get angle indices */
628          i_ndx = mBSDF_incndx(dp, inVec);
629          o_ndx = mBSDF_outndx(dp, outVec);
# Line 612 | Line 638 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
638          return 1;                       /* XXX monochrome for now */
639   }
640  
641 < /* Query solid angle for vector */
641 > /* Query solid angle for vector(s) */
642   static SDError
643 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
643 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
644 >                                        int qflags, SDComponent *sdc)
645   {
646 <        const SDMat     *dp = (const SDMat *)dist;
647 <
648 <        if (!(qflags & SDqueryInc+SDqueryOut))
646 >        const SDMat     *dp;
647 >        double          inc_psa, out_psa;
648 >                                        /* check arguments */
649 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
650 >                        (dp = (SDMat *)sdc->dist) == NULL)
651                  return SDEargument;
652 <        if (qflags & SDqueryInc) {
653 <                double  inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
654 <                if (inc_psa < .0)
655 <                        return SDEinternal;
656 <                switch (qflags & SDqueryMin+SDqueryMax) {
657 <                case SDqueryMax:
658 <                        if (inc_psa > psa[0])
659 <                                psa[0] = inc_psa;
660 <                        break;
661 <                case SDqueryMin+SDqueryMax:
662 <                        if (inc_psa > psa[1])
663 <                                psa[1] = inc_psa;
664 <                        /* fall through */
636 <                case SDqueryMin:
637 <                        if (inc_psa < psa[0])
638 <                                psa[0] = inc_psa;
639 <                        break;
640 <                case 0:
652 >        if (v2 == NULL)
653 >                v2 = v1;
654 >                                        /* get projected solid angles */
655 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
656 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
657 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
658 >                inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
659 >                out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
660 >        }
661 >
662 >        switch (qflags) {               /* record based on flag settings */
663 >        case SDqueryMax:
664 >                if (inc_psa > psa[0])
665                          psa[0] = inc_psa;
666 <                        break;
667 <                }
666 >                if (out_psa > psa[0])
667 >                        psa[0] = out_psa;
668 >                break;
669 >        case SDqueryMin+SDqueryMax:
670 >                if (inc_psa > psa[1])
671 >                        psa[1] = inc_psa;
672 >                if (out_psa > psa[1])
673 >                        psa[1] = out_psa;
674 >                /* fall through */
675 >        case SDqueryVal:
676 >                if (qflags == SDqueryVal)
677 >                        psa[0] = M_PI;
678 >                /* fall through */
679 >        case SDqueryMin:
680 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
681 >                        psa[0] = inc_psa;
682 >                if ((out_psa > 0) & (out_psa < psa[0]))
683 >                        psa[0] = out_psa;
684 >                break;
685          }
686 <        if (qflags & SDqueryOut) {
687 <                double  out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
647 <                if (out_psa < .0)
648 <                        return SDEinternal;
649 <                switch (qflags & SDqueryMin+SDqueryMax) {
650 <                case SDqueryMax:
651 <                        if (out_psa > psa[0])
652 <                                psa[0] = out_psa;
653 <                        break;
654 <                case SDqueryMin+SDqueryMax:
655 <                        if (out_psa > psa[1])
656 <                                psa[1] = out_psa;
657 <                        /* fall through */
658 <                case SDqueryMin:
659 <                        if (out_psa < psa[0])
660 <                                psa[0] = out_psa;
661 <                        break;
662 <                case 0:
663 <                        psa[(qflags&SDqueryInc)!=0] = out_psa;
664 <                        break;
665 <                }
666 <        }
667 <        return SDEnone;
686 >                                        /* make sure it's legal */
687 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
688   }
689  
690   /* Compute new cumulative distribution from BSDF */
# Line 702 | Line 722 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
722   static const SDCDst *
723   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
724   {
725 <        SDMat           *dp = (SDMat *)sdc->dist;
725 >        SDMat           *dp;
726          int             reverse;
727          SDMatCDst       myCD;
728          SDMatCDst       *cd, *cdlast;
729 <
730 <        if (dp == NULL)
729 >                                        /* check arguments */
730 >        if ((inVec == NULL) | (sdc == NULL) ||
731 >                        (dp = (SDMat *)sdc->dist) == NULL)
732                  return NULL;
733          memset(&myCD, 0, sizeof(myCD));
734          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 726 | Line 747 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
747                  reverse = 1;
748          }
749          cdlast = NULL;                  /* check for it in cache list */
750 <        for (cd = (SDMatCDst *)sdc->cdList;
751 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
750 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
751 >                                        cdlast = cd, cd = cd->next)
752                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
753                                          (cd->ob_priv == myCD.ob_priv) &
754                                          (cd->ob_vec == myCD.ob_vec))
755                          break;
735                cdlast = cd;
736        }
756          if (cd == NULL) {               /* need to allocate new entry */
757                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
758 <                                        myCD.calen*sizeof(myCD.carr[0]));
758 >                                        sizeof(myCD.carr[0])*myCD.calen);
759                  if (cd == NULL)
760                          return NULL;
761                  *cd = myCD;             /* compute cumulative distribution */
# Line 748 | Line 767 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
767          }
768          if (cdlast != NULL) {           /* move entry to head of cache list */
769                  cdlast->next = cd->next;
770 <                cd->next = sdc->cdList;
770 >                cd->next = (SDMatCDst *)sdc->cdList;
771                  sdc->cdList = (SDCDst *)cd;
772          }
773          return (SDCDst *)cd;            /* ready to go */
# Line 756 | Line 775 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
775  
776   /* Sample cumulative distribution */
777   static SDError
778 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
778 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
779   {
780          const unsigned  maxval = ~0;
781          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
782          const unsigned  target = randX*maxval;
783          int             i, iupper, ilower;
784 +                                        /* check arguments */
785 +        if ((ioVec == NULL) | (mcd == NULL))
786 +                return SDEargument;
787                                          /* binary search to find index */
788          ilower = 0; iupper = mcd->calen;
789          while ((i = (iupper + ilower) >> 1) != ilower)
790 <                if ((long)target >= (long)mcd->carr[i])
790 >                if (target >= mcd->carr[i])
791                          ilower = i;
792                  else
793                          iupper = i;
# Line 773 | Line 795 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
795          randX = (randX*maxval - mcd->carr[ilower]) /
796                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
797                                          /* convert index to vector */
798 <        if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv))
798 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
799                  return SDEnone;
800 <        strcpy(SDerrorDetail, "BSDF sampling fault");
800 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
801          return SDEinternal;
802   }
803  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines