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.23 by greg, Sun Sep 2 15:33:15 2012 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 49 | Line 46 | typedef struct {
46   static ANGLE_BASIS      abase_list[MAXABASES] = {
47          {
48                  "LBNL/Klems Full", 145,
49 <                { {-5., 1},
49 >                { {0., 1},
50                  {5., 8},
51                  {15., 16},
52                  {25., 20},
# Line 61 | Line 58 | static ANGLE_BASIS     abase_list[MAXABASES] = {
58                  {90., 0} }
59          }, {
60                  "LBNL/Klems Half", 73,
61 <                { {-6.5, 1},
61 >                { {0., 1},
62                  {6.5, 8},
63                  {19.5, 12},
64                  {32.5, 16},
# Line 71 | Line 68 | static ANGLE_BASIS     abase_list[MAXABASES] = {
68                  {90., 0} }
69          }, {
70                  "LBNL/Klems Quarter", 41,
71 <                { {-9., 1},
71 >                { {0., 1},
72                  {9., 8},
73                  {27., 12},
74                  {46., 12},
# Line 85 | Line 82 | static int     nabases = 3;    /* current number of defined b
82   static int
83   fequal(double a, double b)
84   {
85 <        if (b != .0)
85 >        if (b != 0)
86                  a = a/b - 1.;
87          return (a <= 1e-6) & (a >= -1e-6);
88   }
89  
90 < /* 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 */
90 > /* Returns the given tag's character content or empty string if none */
91   #ifdef ezxml_txt
92   #undef ezxml_txt
93   static char *
# Line 161 | Line 146 | SDnewMatrix(int ni, int no)
146   /* Free a BSDF matrix */
147   #define SDfreeMatrix            free
148  
149 < /* get vector for this angle basis index */
149 > /* get vector for this angle basis index (front exiting) */
150   static int
151 < ab_getvec(FVECT v, int ndx, double randX, void *p)
151 > fo_getvec(FVECT v, double ndxr, void *p)
152   {
153 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
153 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
154 >        int             ndx = (int)ndxr;
155 >        double          randX = ndxr - ndx;
156          double          rx[2];
157          int             li;
158          double          pol, azi, d;
159          
160 <        if ((ndx < 0) | (ndx >= ab->nangles))
160 >        if ((ndxr < 0) | (ndx >= ab->nangles))
161                  return RC_FAIL;
162          for (li = 0; ndx >= ab->lat[li].nphis; li++)
163                  ndx -= ab->lat[li].nphis;
# Line 185 | Line 172 | ab_getvec(FVECT v, int ndx, double randX, void *p)
172          return RC_GOOD;
173   }
174  
175 < /* get index corresponding to the given vector */
175 > /* get index corresponding to the given vector (front exiting) */
176   static int
177 < ab_getndx(const FVECT v, void *p)
177 > fo_getndx(const FVECT v, void *p)
178   {
179 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
179 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
180          int     li, ndx;
181 <        double  pol, azi, d;
181 >        double  pol, azi;
182  
183          if (v == NULL)
184                  return -1;
185 <        if ((v[2] < .0) | (v[2] > 1.0))
185 >        if ((v[2] < 0) | (v[2] > 1.))
186                  return -1;
187          pol = 180.0/M_PI*acos(v[2]);
188          azi = 180.0/M_PI*atan2(v[1], v[0]);
# Line 214 | Line 201 | ab_getndx(const FVECT v, void *p)
201   /* compute square of real value */
202   static double sq(double x) { return x*x; }
203  
204 < /* get projected solid angle for this angle basis index */
204 > /* get projected solid angle for this angle basis index (universal) */
205   static double
206 < ab_getohm(int ndx, void *p)
206 > io_getohm(int ndx, void *p)
207   {
208          static int      last_li = -1;
209          static double   last_ohm;
# Line 231 | Line 218 | ab_getohm(int ndx, void *p)
218          if (li == last_li)                      /* cached latitude? */
219                  return last_ohm;
220          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)));
221          theta = M_PI/180. * ab->lat[li].tmin;
222 +        theta1 = M_PI/180. * ab->lat[li+1].tmin;
223          return last_ohm = M_PI*(sq(cos(theta)) - sq(cos(theta1))) /
224                                  (double)ab->lat[li].nphis;
225   }
226  
227 < /* get reverse vector for this angle basis index */
227 > /* get vector for this angle basis index (back incident) */
228   static int
229 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
229 > bi_getvec(FVECT v, double ndxr, void *p)
230   {
231 <        int     na = (*(ANGLE_BASIS *)p).nangles;
247 <
248 <        if (!ab_getvec(v, ndx, randX, p))
231 >        if (!fo_getvec(v, ndxr, p))
232                  return RC_FAIL;
233  
234          v[0] = -v[0];
# Line 255 | Line 238 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
238          return RC_GOOD;
239   }
240  
241 < /* get index corresponding to the reverse vector */
241 > /* get index corresponding to the vector (back incident) */
242   static int
243 < ab_getndxR(const FVECT v, void *p)
243 > bi_getndx(const FVECT v, void *p)
244   {
245          FVECT  v2;
246          
# Line 265 | Line 248 | ab_getndxR(const FVECT v, void *p)
248          v2[1] = -v[1];
249          v2[2] = -v[2];
250  
251 <        return ab_getndx(v2, p);
251 >        return fo_getndx(v2, p);
252   }
253  
254 + /* get vector for this angle basis index (back exiting) */
255 + static int
256 + bo_getvec(FVECT v, double ndxr, void *p)
257 + {
258 +        if (!fo_getvec(v, ndxr, p))
259 +                return RC_FAIL;
260 +
261 +        v[2] = -v[2];
262 +
263 +        return RC_GOOD;
264 + }
265 +
266 + /* get index corresponding to the vector (back exiting) */
267 + static int
268 + bo_getndx(const FVECT v, void *p)
269 + {
270 +        FVECT  v2;
271 +        
272 +        v2[0] = v[0];
273 +        v2[1] = v[1];
274 +        v2[2] = -v[2];
275 +
276 +        return fo_getndx(v2, p);
277 + }
278 +
279 + /* get vector for this angle basis index (front incident) */
280 + static int
281 + fi_getvec(FVECT v, double ndxr, void *p)
282 + {
283 +        if (!fo_getvec(v, ndxr, p))
284 +                return RC_FAIL;
285 +
286 +        v[0] = -v[0];
287 +        v[1] = -v[1];
288 +
289 +        return RC_GOOD;
290 + }
291 +
292 + /* get index corresponding to the vector (front incident) */
293 + static int
294 + fi_getndx(const FVECT v, void *p)
295 + {
296 +        FVECT  v2;
297 +        
298 +        v2[0] = -v[0];
299 +        v2[1] = -v[1];
300 +        v2[2] = v[2];
301 +
302 +        return fo_getndx(v2, p);
303 + }
304 +
305   /* load custom BSDF angle basis */
306   static int
307   load_angle_basis(ezxml_t wab)
# Line 299 | Line 333 | load_angle_basis(ezxml_t wab)
333                                  ezxml_child(ezxml_child(wbb,
334                                          "ThetaBounds"), "UpperTheta")));
335                  if (!i)
336 <                        abase_list[nabases].lat[i].tmin =
303 <                                        -abase_list[nabases].lat[i+1].tmin;
336 >                        abase_list[nabases].lat[0].tmin = 0;
337                  else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
338                                          "ThetaBounds"), "LowerTheta"))),
339                                  abase_list[nabases].lat[i].tmin)) {
340                          sprintf(SDerrorDetail, "Theta values disagree in '%s'",
341 <                                abname);
341 >                                        abname);
342                          return RC_DATERR;
343                  }
344                  abase_list[nabases].nangles +=
# Line 315 | Line 348 | load_angle_basis(ezxml_t wab)
348                                  (abase_list[nabases].lat[i].nphis == 1 &&
349                                  abase_list[nabases].lat[i].tmin > FTINY)) {
350                          sprintf(SDerrorDetail, "Illegal phi count in '%s'",
351 <                                abname);
351 >                                        abname);
352                          return RC_DATERR;
353                  }
354          }
# Line 350 | Line 383 | get_extrema(SDSpectralDF *df)
383          }
384          free(ohma);
385                                          /* need incoming solid angles, too? */
386 <        if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm ||
354 <                                dp->ib_priv != dp->ob_priv) {
386 >        if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) {
387                  double  ohm;
388                  for (i = dp->ninc; i--; )
389                          if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA)
# Line 364 | Line 396 | get_extrema(SDSpectralDF *df)
396   static int
397   load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
398   {
367        char            *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
368        char            *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
399          SDSpectralDF    *df;
400          SDMat           *dp;
401          char            *sdata;
402          int             inbi, outbi;
403          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        }
404                                          /* allocate BSDF component */
405          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
406 +        if (!sdata)
407 +                return RC_FAIL;
408 +        /*
409 +         * Remember that front and back are reversed from WINDOW 6 orientations
410 +         */
411          if (!strcasecmp(sdata, "Transmission Front")) {
412                  if (sd->tf != NULL)
413                          SDfreeSpectralDF(sd->tf);
414                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
415                          return RC_MEMERR;
416                  df = sd->tf;
417 <        } else if (!strcasecmp(sdata, "Reflection Front")) {
418 <                if (sd->rf != NULL)
419 <                        SDfreeSpectralDF(sd->rf);
420 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
417 >        } else if (!strcasecmp(sdata, "Transmission Back")) {
418 >                if (sd->tb != NULL)
419 >                        SDfreeSpectralDF(sd->tb);
420 >                if ((sd->tb = SDnewSpectralDF(1)) == NULL)
421                          return RC_MEMERR;
422 <                df = sd->rf;
423 <        } else if (!strcasecmp(sdata, "Reflection Back")) {
424 <                if (sd->rb != NULL)
422 >                df = sd->tb;
423 >        } else if (!strcasecmp(sdata, "Reflection Front")) {
424 >                if (sd->rb != NULL)     /* note back-front reversal */
425                          SDfreeSpectralDF(sd->rb);
426                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
427                          return RC_MEMERR;
428                  df = sd->rb;
429 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
430 +                if (sd->rf != NULL)     /* note front-back reversal */
431 +                        SDfreeSpectralDF(sd->rf);
432 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
433 +                        return RC_MEMERR;
434 +                df = sd->rf;
435          } else
436                  return RC_FAIL;
437 +        /* XXX should also check "ScatteringDataType" for consistency? */
438                                          /* get angle bases */
439          sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
440          if (!sdata || !*sdata) {
# Line 407 | Line 443 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
443                  return RC_FORMERR;
444          }
445          for (inbi = nabases; inbi--; )
446 <                if (!strcasecmp(cbasis, abase_list[inbi].name))
446 >                if (!strcasecmp(sdata, abase_list[inbi].name))
447                          break;
448          if (inbi < 0) {
449 <                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'",
414 <                                cbasis);
449 >                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata);
450                  return RC_FORMERR;
451          }
452          sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
# Line 421 | Line 456 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
456                  return RC_FORMERR;
457          }
458          for (outbi = nabases; outbi--; )
459 <                if (!strcasecmp(rbasis, abase_list[outbi].name))
459 >                if (!strcasecmp(sdata, abase_list[outbi].name))
460                          break;
461          if (outbi < 0) {
462 <                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis);
462 >                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata);
463                  return RC_FORMERR;
464          }
465                                          /* allocate BSDF matrix */
466          dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles);
467          if (dp == NULL)
468                  return RC_MEMERR;
469 <        dp->ib_priv = (void *)&abase_list[inbi];
470 <        dp->ob_priv = (void *)&abase_list[outbi];
469 >        dp->ib_priv = &abase_list[inbi];
470 >        dp->ob_priv = &abase_list[outbi];
471          if (df == sd->tf) {
472 <                dp->ib_vec = ab_getvecR;
473 <                dp->ib_ndx = ab_getndxR;
474 <                dp->ob_vec = ab_getvec;
475 <                dp->ob_ndx = ab_getndx;
472 >                dp->ib_vec = &fi_getvec;
473 >                dp->ib_ndx = &fi_getndx;
474 >                dp->ob_vec = &bo_getvec;
475 >                dp->ob_ndx = &bo_getndx;
476 >        } else if (df == sd->tb) {
477 >                dp->ib_vec = &bi_getvec;
478 >                dp->ib_ndx = &bi_getndx;
479 >                dp->ob_vec = &fo_getvec;
480 >                dp->ob_ndx = &fo_getndx;
481          } else if (df == sd->rf) {
482 <                dp->ib_vec = ab_getvec;
483 <                dp->ib_ndx = ab_getndx;
484 <                dp->ob_vec = ab_getvec;
485 <                dp->ob_ndx = ab_getndx;
482 >                dp->ib_vec = &fi_getvec;
483 >                dp->ib_ndx = &fi_getndx;
484 >                dp->ob_vec = &fo_getvec;
485 >                dp->ob_ndx = &fo_getndx;
486          } else /* df == sd->rb */ {
487 <                dp->ib_vec = ab_getvecR;
488 <                dp->ib_ndx = ab_getndxR;
489 <                dp->ob_vec = ab_getvecR;
490 <                dp->ob_ndx = ab_getndxR;
487 >                dp->ib_vec = &bi_getvec;
488 >                dp->ib_ndx = &bi_getndx;
489 >                dp->ob_vec = &bo_getvec;
490 >                dp->ob_ndx = &bo_getndx;
491          }
492 <        dp->ib_ohm = ab_getohm;
493 <        dp->ob_ohm = ab_getohm;
492 >        dp->ib_ohm = &io_getohm;
493 >        dp->ob_ohm = &io_getohm;
494          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
495          df->comp[0].dist = dp;
496          df->comp[0].func = &SDhandleMtx;
497                                          /* read BSDF data */
498 <        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
498 >        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
499          if (!sdata || !*sdata) {
500                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
501                                  sd->name);
# Line 469 | Line 509 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
509                                          sd->name);
510                          return RC_FORMERR;
511                  }
512 <                while (*sdnext && isspace(*sdnext))
512 >                while (isspace(*sdnext))
513                          sdnext++;
514                  if (*sdnext == ',') sdnext++;
515                  if (rowinc) {
516                          int     r = i/dp->nout;
517 <                        int     c = i - c*dp->nout;
517 >                        int     c = i - r*dp->nout;
518                          mBSDF_value(dp,r,c) = atof(sdata);
519                  } else
520                          dp->bsdf[i] = atof(sdata);
# Line 494 | Line 534 | subtract_min(SDMat *sm)
534          for (i = n; --i; )
535                  if (sm->bsdf[i] < minv)
536                          minv = sm->bsdf[i];
537 +        
538 +        if (minv <= FTINY)
539 +                return .0;
540 +
541          for (i = n; i--; )
542                  sm->bsdf[i] -= minv;
543  
# Line 519 | Line 563 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
563                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
564                  dv->cieY += ymin;
565          }
566 <        df->maxHemi -= dv->cieY;        /* correct minimum hemispherical */
567 <        dv->spec.clock++;               /* make sure everything is set */
566 >        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
567 >                                        /* make sure everything is set */
568          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
569   }
570  
571   /* Load a BSDF matrix from an open XML file */
572   SDError
573 < SDloadMtx(SDData *sd, ezxml_t fl)
573 > SDloadMtx(SDData *sd, ezxml_t wtl)
574   {
575 <        ezxml_t                 wtl, wld, wdb;
576 <        int                     rowIn;
577 <        struct BSDF_data        *dp;
578 <        char                    *txt;
579 <        int                     rval;
580 <        
581 <        if (strcmp(ezxml_name(fl), "WindowElement")) {
575 >        ezxml_t         wld, wdb;
576 >        int             rowIn;
577 >        char            *txt;
578 >        int             rval;
579 >                                        /* basic checks and data ordering */
580 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
581 >                        "DataDefinition"), "IncidentDataStructure"));
582 >        if (txt == NULL || !*txt) {
583                  sprintf(SDerrorDetail,
584 <                        "BSDF \"%s\": top level node not 'WindowElement'",
584 >                        "BSDF \"%s\": missing IncidentDataStructure",
585                                  sd->name);
586                  return SDEformat;
587          }
543        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
544        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
545                        "DataDefinition"), "IncidentDataStructure"));
588          if (!strcasecmp(txt, "Rows"))
589                  rowIn = 1;
590          else if (!strcasecmp(txt, "Columns"))
# Line 553 | Line 595 | SDloadMtx(SDData *sd, ezxml_t fl)
595                                  sd->name);
596                  return SDEsupport;
597          }
598 <                                /* get angle basis */
599 <        rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
600 <                                "DataDefinition"), "AngleBasis"));
601 <        if (rval < 0)
602 <                goto err_return;
603 <                                /* load BSDF components */
598 >                                        /* get angle bases */
599 >        for (wld = ezxml_child(ezxml_child(wtl, "DataDefinition"), "AngleBasis");
600 >                                wld != NULL; wld = wld->next) {
601 >                rval = load_angle_basis(wld);
602 >                if (rval < 0)
603 >                        return convert_errcode(rval);
604 >        }
605 >                                        /* load BSDF components */
606          for (wld = ezxml_child(wtl, "WavelengthData");
607                                  wld != NULL; wld = wld->next) {
608                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 567 | Line 611 | SDloadMtx(SDData *sd, ezxml_t fl)
611                  for (wdb = ezxml_child(wld, "WavelengthDataBlock");
612                                          wdb != NULL; wdb = wdb->next)
613                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
614 <                                goto err_return;
614 >                                return convert_errcode(rval);
615          }
616 <                                /* separate diffuse components */
616 >                                        /* separate diffuse components */
617          extract_diffuse(&sd->rLambFront, sd->rf);
618          extract_diffuse(&sd->rLambBack, sd->rb);
619 <        extract_diffuse(&sd->tLamb, sd->tf);
620 <                                /* return success */
619 >        extract_diffuse(&sd->tLamb, (sd->tf != NULL) ? sd->tf : sd->tb);
620 >                                        /* return success */
621          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);
622   }
623  
624   /* Get Matrix BSDF value */
625   static int
626   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
627 <                                const FVECT inVec, const void *dist)
627 >                                const FVECT inVec, SDComponent *sdc)
628   {
629 <        const SDMat     *dp = (const SDMat *)dist;
629 >        const SDMat     *dp;
630          int             i_ndx, o_ndx;
631 +                                        /* check arguments */
632 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
633 +                        || (dp = (SDMat *)sdc->dist) == NULL)
634 +                return 0;
635                                          /* get angle indices */
636          i_ndx = mBSDF_incndx(dp, inVec);
637          o_ndx = mBSDF_outndx(dp, outVec);
# Line 612 | Line 646 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
646          return 1;                       /* XXX monochrome for now */
647   }
648  
649 < /* Query solid angle for vector */
649 > /* Query solid angle for vector(s) */
650   static SDError
651 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
651 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
652 >                                        int qflags, SDComponent *sdc)
653   {
654 <        const SDMat     *dp = (const SDMat *)dist;
655 <
656 <        if (!(qflags & SDqueryInc+SDqueryOut))
654 >        const SDMat     *dp;
655 >        double          inc_psa, out_psa;
656 >                                        /* check arguments */
657 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
658 >                        (dp = (SDMat *)sdc->dist) == NULL)
659                  return SDEargument;
660 <        if (qflags & SDqueryInc) {
661 <                double  inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
662 <                if (inc_psa < .0)
663 <                        return SDEinternal;
664 <                switch (qflags & SDqueryMin+SDqueryMax) {
665 <                case SDqueryMax:
666 <                        if (inc_psa > psa[0])
667 <                                psa[0] = inc_psa;
668 <                        break;
669 <                case SDqueryMin+SDqueryMax:
670 <                        if (inc_psa > psa[1])
671 <                                psa[1] = inc_psa;
672 <                        /* fall through */
636 <                case SDqueryMin:
637 <                        if (inc_psa < psa[0])
638 <                                psa[0] = inc_psa;
639 <                        break;
640 <                case 0:
660 >        if (v2 == NULL)
661 >                v2 = v1;
662 >                                        /* get projected solid angles */
663 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
664 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
665 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
666 >                inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
667 >                out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
668 >        }
669 >
670 >        switch (qflags) {               /* record based on flag settings */
671 >        case SDqueryMax:
672 >                if (inc_psa > psa[0])
673                          psa[0] = inc_psa;
674 <                        break;
675 <                }
674 >                if (out_psa > psa[0])
675 >                        psa[0] = out_psa;
676 >                break;
677 >        case SDqueryMin+SDqueryMax:
678 >                if (inc_psa > psa[1])
679 >                        psa[1] = inc_psa;
680 >                if (out_psa > psa[1])
681 >                        psa[1] = out_psa;
682 >                /* fall through */
683 >        case SDqueryVal:
684 >                if (qflags == SDqueryVal)
685 >                        psa[0] = M_PI;
686 >                /* fall through */
687 >        case SDqueryMin:
688 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
689 >                        psa[0] = inc_psa;
690 >                if ((out_psa > 0) & (out_psa < psa[0]))
691 >                        psa[0] = out_psa;
692 >                break;
693          }
694 <        if (qflags & SDqueryOut) {
695 <                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;
694 >                                        /* make sure it's legal */
695 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
696   }
697  
698   /* Compute new cumulative distribution from BSDF */
# Line 702 | Line 730 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
730   static const SDCDst *
731   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
732   {
733 <        SDMat           *dp = (SDMat *)sdc->dist;
733 >        SDMat           *dp;
734          int             reverse;
735          SDMatCDst       myCD;
736          SDMatCDst       *cd, *cdlast;
737 <
738 <        if (dp == NULL)
737 >                                        /* check arguments */
738 >        if ((inVec == NULL) | (sdc == NULL) ||
739 >                        (dp = (SDMat *)sdc->dist) == NULL)
740                  return NULL;
741          memset(&myCD, 0, sizeof(myCD));
742          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 726 | Line 755 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
755                  reverse = 1;
756          }
757          cdlast = NULL;                  /* check for it in cache list */
758 <        for (cd = (SDMatCDst *)sdc->cdList;
759 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
758 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
759 >                                        cdlast = cd, cd = cd->next)
760                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
761                                          (cd->ob_priv == myCD.ob_priv) &
762                                          (cd->ob_vec == myCD.ob_vec))
763                          break;
735                cdlast = cd;
736        }
764          if (cd == NULL) {               /* need to allocate new entry */
765                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
766 <                                        myCD.calen*sizeof(myCD.carr[0]));
766 >                                        sizeof(myCD.carr[0])*myCD.calen);
767                  if (cd == NULL)
768                          return NULL;
769                  *cd = myCD;             /* compute cumulative distribution */
# Line 748 | Line 775 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
775          }
776          if (cdlast != NULL) {           /* move entry to head of cache list */
777                  cdlast->next = cd->next;
778 <                cd->next = sdc->cdList;
778 >                cd->next = (SDMatCDst *)sdc->cdList;
779                  sdc->cdList = (SDCDst *)cd;
780          }
781          return (SDCDst *)cd;            /* ready to go */
# Line 756 | Line 783 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
783  
784   /* Sample cumulative distribution */
785   static SDError
786 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
786 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
787   {
788          const unsigned  maxval = ~0;
789          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
790          const unsigned  target = randX*maxval;
791          int             i, iupper, ilower;
792 +                                        /* check arguments */
793 +        if ((ioVec == NULL) | (mcd == NULL))
794 +                return SDEargument;
795                                          /* binary search to find index */
796          ilower = 0; iupper = mcd->calen;
797          while ((i = (iupper + ilower) >> 1) != ilower)
798 <                if ((long)target >= (long)mcd->carr[i])
798 >                if (target >= mcd->carr[i])
799                          ilower = i;
800                  else
801                          iupper = i;
# Line 773 | Line 803 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
803          randX = (randX*maxval - mcd->carr[ilower]) /
804                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
805                                          /* convert index to vector */
806 <        if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv))
806 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
807                  return SDEnone;
808 <        strcpy(SDerrorDetail, "BSDF sampling fault");
808 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
809          return SDEinternal;
810   }
811  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines