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.1 by greg, Fri Feb 18 00:40:25 2011 UTC vs.
Revision 3.19 by greg, Thu Jul 7 15:48:00 2011 UTC

# Line 1 | Line 1
1 + #ifndef lint
2 + static const char RCSid[] = "$Id$";
3 + #endif
4   /*
5   *  bsdf_m.c
6   *  
# Line 8 | Line 11
11   *
12   */
13  
14 < #include <stdio.h>
14 > #define _USE_MATH_DEFINES
15 > #include "rtio.h"
16   #include <stdlib.h>
17   #include <math.h>
14 #include <strings.h>
18   #include <ctype.h>
19   #include "ezxml.h"
20   #include "bsdf.h"
21   #include "bsdf_m.h"
22  
20 #ifndef FTINY
21 #define FTINY           1e-6
22 #endif
23
23   /* Function return codes */
24   #define RC_GOOD         1
25   #define RC_FAIL         0
# Line 83 | 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 */
92 < #ifdef ezxml_name
93 < #undef ezxml_name
94 < static char *
95 < ezxml_name(ezxml_t xml)
96 < {
97 <        if (xml == NULL)
98 <                return NULL;
99 <        return xml->name;
100 < }
101 < #endif
102 <
103 < /* 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 159 | 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 183 | 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 212 | 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 237 | Line 226 | ab_getohm(int ndx, void *p)
226                                  (double)ab->lat[li].nphis;
227   }
228  
229 < /* get reverse vector for this angle basis index */
229 > /* get vector for this angle basis index (back incident) */
230   static int
231 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
231 > bi_getvec(FVECT v, double ndxr, void *p)
232   {
233 <        int     na = (*(ANGLE_BASIS *)p).nangles;
245 <
246 <        if (!ab_getvec(v, ndx, randX, p))
233 >        if (!fo_getvec(v, ndxr, p))
234                  return RC_FAIL;
235  
236          v[0] = -v[0];
# Line 253 | Line 240 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
240          return RC_GOOD;
241   }
242  
243 < /* get index corresponding to the reverse vector */
243 > /* get index corresponding to the vector (back incident) */
244   static int
245 < ab_getndxR(const FVECT v, void *p)
245 > bi_getndx(const FVECT v, void *p)
246   {
247          FVECT  v2;
248          
# Line 263 | Line 250 | ab_getndxR(const FVECT v, void *p)
250          v2[1] = -v[1];
251          v2[2] = -v[2];
252  
253 <        return ab_getndx(v2, p);
253 >        return fo_getndx(v2, p);
254   }
255  
256 + /* get vector for this angle basis index (back exiting) */
257 + static int
258 + bo_getvec(FVECT v, double ndxr, void *p)
259 + {
260 +        if (!fo_getvec(v, ndxr, p))
261 +                return RC_FAIL;
262 +
263 +        v[2] = -v[2];
264 +
265 +        return RC_GOOD;
266 + }
267 +
268 + /* get index corresponding to the vector (back exiting) */
269 + static int
270 + bo_getndx(const FVECT v, void *p)
271 + {
272 +        FVECT  v2;
273 +        
274 +        v2[0] = v[0];
275 +        v2[1] = v[1];
276 +        v2[2] = -v[2];
277 +
278 +        return fo_getndx(v2, p);
279 + }
280 +
281 + /* get vector for this angle basis index (front incident) */
282 + static int
283 + fi_getvec(FVECT v, double ndxr, void *p)
284 + {
285 +        if (!fo_getvec(v, ndxr, p))
286 +                return RC_FAIL;
287 +
288 +        v[0] = -v[0];
289 +        v[1] = -v[1];
290 +
291 +        return RC_GOOD;
292 + }
293 +
294 + /* get index corresponding to the vector (front incident) */
295 + static int
296 + fi_getndx(const FVECT v, void *p)
297 + {
298 +        FVECT  v2;
299 +        
300 +        v2[0] = -v[0];
301 +        v2[1] = -v[1];
302 +        v2[2] = v[2];
303 +
304 +        return fo_getndx(v2, p);
305 + }
306 +
307   /* load custom BSDF angle basis */
308   static int
309   load_angle_basis(ezxml_t wab)
# Line 303 | Line 341 | load_angle_basis(ezxml_t wab)
341                                          "ThetaBounds"), "LowerTheta"))),
342                                  abase_list[nabases].lat[i].tmin)) {
343                          sprintf(SDerrorDetail, "Theta values disagree in '%s'",
344 <                                abname);
344 >                                        abname);
345                          return RC_DATERR;
346                  }
347                  abase_list[nabases].nangles +=
# Line 313 | Line 351 | load_angle_basis(ezxml_t wab)
351                                  (abase_list[nabases].lat[i].nphis == 1 &&
352                                  abase_list[nabases].lat[i].tmin > FTINY)) {
353                          sprintf(SDerrorDetail, "Illegal phi count in '%s'",
354 <                                abname);
354 >                                        abname);
355                          return RC_DATERR;
356                  }
357          }
# Line 348 | Line 386 | get_extrema(SDSpectralDF *df)
386          }
387          free(ohma);
388                                          /* need incoming solid angles, too? */
389 <        if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm ||
352 <                                dp->ib_priv != dp->ob_priv) {
389 >        if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) {
390                  double  ohm;
391                  for (i = dp->ninc; i--; )
392                          if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA)
# Line 358 | Line 395 | get_extrema(SDSpectralDF *df)
395          return (df->maxHemi <= 1.01);
396   }
397  
361 /* skip integer in string */
362 static char *
363 i_skip(char  *s)
364 {
365        while (isspace(*s))
366                s++;
367        if (*s == '-' || *s == '+')
368                s++;
369        if (!isdigit(*s))
370                return(NULL);
371        do
372                s++;
373        while (isdigit(*s));
374        return(s);
375 }
376
377 /* skip float in string */
378 static char *
379 f_skip(char  *s)
380 {
381        register char  *cp;
382
383        while (isspace(*s))
384                s++;
385        if (*s == '-' || *s == '+')
386                s++;
387        cp = s;
388        while (isdigit(*cp))
389                cp++;
390        if (*cp == '.') {
391                cp++; s++;
392                while (isdigit(*cp))
393                        cp++;
394        }
395        if (cp == s)
396                return(NULL);
397        if (*cp == 'e' || *cp == 'E')
398                return(i_skip(cp+1));
399        return(cp);
400 }
401
398   /* load BSDF distribution for this wavelength */
399   static int
400   load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
401   {
406        char            *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
407        char            *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
402          SDSpectralDF    *df;
403          SDMat           *dp;
404          char            *sdata;
405 +        int             tfront;
406          int             inbi, outbi;
407          int             i;
413
414        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
415                sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'",
416                                sd->name);
417                return RC_FORMERR;
418        }
408                                          /* allocate BSDF component */
409          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
410 <        if (!strcasecmp(sdata, "Transmission Front")) {
410 >        if (!sdata)
411 >                return RC_FAIL;
412 >        /*
413 >         * Remember that front and back are reversed from WINDOW 6 orientations
414 >         * Favor their "Front" (incoming light) since that's more often valid
415 >         */
416 >        tfront = !strcasecmp(sdata, "Transmission Back");
417 >        if (!strcasecmp(sdata, "Transmission Front") ||
418 >                        tfront & (sd->tf == NULL)) {
419                  if (sd->tf != NULL)
420                          SDfreeSpectralDF(sd->tf);
421                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
422                          return RC_MEMERR;
423                  df = sd->tf;
424          } else if (!strcasecmp(sdata, "Reflection Front")) {
425 <                if (sd->rf != NULL)
429 <                        SDfreeSpectralDF(sd->rf);
430 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
431 <                        return RC_MEMERR;
432 <                df = sd->rf;
433 <        } else if (!strcasecmp(sdata, "Reflection Back")) {
434 <                if (sd->rb != NULL)
425 >                if (sd->rb != NULL)     /* note back-front reversal */
426                          SDfreeSpectralDF(sd->rb);
427                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
428                          return RC_MEMERR;
429                  df = sd->rb;
430 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
431 +                if (sd->rf != NULL)     /* note front-back reversal */
432 +                        SDfreeSpectralDF(sd->rf);
433 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
434 +                        return RC_MEMERR;
435 +                df = sd->rf;
436          } else
437                  return RC_FAIL;
438 +        /* XXX should also check "ScatteringDataType" for consistency? */
439                                          /* get angle bases */
440          sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
441          if (!sdata || !*sdata) {
# Line 446 | Line 444 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
444                  return RC_FORMERR;
445          }
446          for (inbi = nabases; inbi--; )
447 <                if (!strcasecmp(cbasis, abase_list[inbi].name))
447 >                if (!strcasecmp(sdata, abase_list[inbi].name))
448                          break;
449          if (inbi < 0) {
450 <                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'",
453 <                                cbasis);
450 >                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata);
451                  return RC_FORMERR;
452          }
453          sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
# Line 460 | Line 457 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
457                  return RC_FORMERR;
458          }
459          for (outbi = nabases; outbi--; )
460 <                if (!strcasecmp(rbasis, abase_list[outbi].name))
460 >                if (!strcasecmp(sdata, abase_list[outbi].name))
461                          break;
462          if (outbi < 0) {
463 <                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis);
463 >                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata);
464                  return RC_FORMERR;
465          }
466                                          /* allocate BSDF matrix */
467          dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles);
468          if (dp == NULL)
469                  return RC_MEMERR;
470 <        dp->ib_priv = (void *)&abase_list[inbi];
471 <        dp->ob_priv = (void *)&abase_list[outbi];
470 >        dp->ib_priv = &abase_list[inbi];
471 >        dp->ob_priv = &abase_list[outbi];
472          if (df == sd->tf) {
473 <                dp->ib_vec = ab_getvecR;
474 <                dp->ib_ndx = ab_getndxR;
475 <                dp->ob_vec = ab_getvec;
476 <                dp->ob_ndx = ab_getndx;
473 >                if (tfront) {
474 >                        dp->ib_vec = &fi_getvec;
475 >                        dp->ib_ndx = &fi_getndx;
476 >                        dp->ob_vec = &bo_getvec;
477 >                        dp->ob_ndx = &bo_getndx;
478 >                } else {
479 >                        dp->ib_vec = &bi_getvec;
480 >                        dp->ib_ndx = &bi_getndx;
481 >                        dp->ob_vec = &fo_getvec;
482 >                        dp->ob_ndx = &fo_getndx;
483 >                }
484          } else if (df == sd->rf) {
485 <                dp->ib_vec = ab_getvec;
486 <                dp->ib_ndx = ab_getndx;
487 <                dp->ob_vec = ab_getvec;
488 <                dp->ob_ndx = ab_getndx;
485 >                dp->ib_vec = &fi_getvec;
486 >                dp->ib_ndx = &fi_getndx;
487 >                dp->ob_vec = &fo_getvec;
488 >                dp->ob_ndx = &fo_getndx;
489          } else /* df == sd->rb */ {
490 <                dp->ib_vec = ab_getvecR;
491 <                dp->ib_ndx = ab_getndxR;
492 <                dp->ob_vec = ab_getvecR;
493 <                dp->ob_ndx = ab_getndxR;
490 >                dp->ib_vec = &bi_getvec;
491 >                dp->ib_ndx = &bi_getndx;
492 >                dp->ob_vec = &bo_getvec;
493 >                dp->ob_ndx = &bo_getndx;
494          }
495 <        dp->ib_ohm = ab_getohm;
496 <        dp->ob_ohm = ab_getohm;
495 >        dp->ib_ohm = &io_getohm;
496 >        dp->ob_ohm = &io_getohm;
497          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
498          df->comp[0].dist = dp;
499          df->comp[0].func = &SDhandleMtx;
500                                          /* read BSDF data */
501 <        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
501 >        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
502          if (!sdata || !*sdata) {
503                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
504                                  sd->name);
505                  return RC_FORMERR;
506          }
507          for (i = 0; i < dp->ninc*dp->nout; i++) {
508 <                char  *sdnext = f_skip(sdata);
508 >                char  *sdnext = fskip(sdata);
509                  if (sdnext == NULL) {
510                          sprintf(SDerrorDetail,
511                                  "Bad/missing BSDF ScatteringData in '%s'",
512                                          sd->name);
513                          return RC_FORMERR;
514                  }
515 <                while (*sdnext && isspace(*sdnext))
515 >                while (isspace(*sdnext))
516                          sdnext++;
517                  if (*sdnext == ',') sdnext++;
518                  if (rowinc) {
519                          int     r = i/dp->nout;
520 <                        int     c = i - c*dp->nout;
520 >                        int     c = i - r*dp->nout;
521                          mBSDF_value(dp,r,c) = atof(sdata);
522                  } else
523                          dp->bsdf[i] = atof(sdata);
# Line 533 | Line 537 | subtract_min(SDMat *sm)
537          for (i = n; --i; )
538                  if (sm->bsdf[i] < minv)
539                          minv = sm->bsdf[i];
540 +        
541 +        if (minv <= FTINY)
542 +                return .0;
543 +
544          for (i = n; i--; )
545                  sm->bsdf[i] -= minv;
546  
# Line 558 | Line 566 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
566                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
567                  dv->cieY += ymin;
568          }
569 <        df->maxHemi -= dv->cieY;        /* correct minimum hemispherical */
570 <        dv->spec.clock++;               /* make sure everything is set */
569 >        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
570 >                                        /* make sure everything is set */
571          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
572   }
573  
574   /* Load a BSDF matrix from an open XML file */
575   SDError
576 < SDloadMtx(SDData *sd, ezxml_t fl)
576 > SDloadMtx(SDData *sd, ezxml_t wtl)
577   {
578 <        ezxml_t                 wtl, wld, wdb;
579 <        int                     rowIn;
580 <        struct BSDF_data        *dp;
581 <        char                    *txt;
582 <        int                     rval;
583 <        
584 <        if (strcmp(ezxml_name(fl), "WindowElement")) {
578 >        ezxml_t         wld, wdb;
579 >        int             rowIn;
580 >        char            *txt;
581 >        int             rval;
582 >                                        /* basic checks and data ordering */
583 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
584 >                        "DataDefinition"), "IncidentDataStructure"));
585 >        if (txt == NULL || !*txt) {
586                  sprintf(SDerrorDetail,
587 <                        "BSDF \"%s\": top level node not 'WindowElement'",
587 >                        "BSDF \"%s\": missing IncidentDataStructure",
588                                  sd->name);
589                  return SDEformat;
590          }
582        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
583        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
584                        "DataDefinition"), "IncidentDataStructure"));
591          if (!strcasecmp(txt, "Rows"))
592                  rowIn = 1;
593          else if (!strcasecmp(txt, "Columns"))
# Line 592 | Line 598 | SDloadMtx(SDData *sd, ezxml_t fl)
598                                  sd->name);
599                  return SDEsupport;
600          }
601 <                                /* get angle basis */
602 <        rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
603 <                                "DataDefinition"), "AngleBasis"));
604 <        if (rval < 0)
605 <                goto err_return;
606 <                                /* load BSDF components */
601 >                                        /* get angle bases */
602 >        for (wld = ezxml_child(ezxml_child(wtl, "DataDefinition"), "AngleBasis");
603 >                                wld != NULL; wld = wld->next) {
604 >                rval = load_angle_basis(wld);
605 >                if (rval < 0)
606 >                        return convert_errcode(rval);
607 >        }
608 >                                        /* load BSDF components */
609          for (wld = ezxml_child(wtl, "WavelengthData");
610                                  wld != NULL; wld = wld->next) {
611                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 606 | Line 614 | SDloadMtx(SDData *sd, ezxml_t fl)
614                  for (wdb = ezxml_child(wld, "WavelengthDataBlock");
615                                          wdb != NULL; wdb = wdb->next)
616                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
617 <                                goto err_return;
617 >                                return convert_errcode(rval);
618          }
619 <                                /* separate diffuse components */
619 >                                        /* separate diffuse components */
620          extract_diffuse(&sd->rLambFront, sd->rf);
621          extract_diffuse(&sd->rLambBack, sd->rb);
622          extract_diffuse(&sd->tLamb, sd->tf);
623 <                                /* return success */
623 >                                        /* return success */
624          return SDEnone;
617 err_return:                     /* jump here on failure */
618        if (sd->rf != NULL) {
619                SDfreeSpectralDF(sd->rf);
620                sd->rf = NULL;
621        }
622        if (sd->rb != NULL) {
623                SDfreeSpectralDF(sd->rb);
624                sd->rb = NULL;
625        }
626        if (sd->tf != NULL) {
627                SDfreeSpectralDF(sd->tf);
628                sd->tf = NULL;
629        }
630        return convert_errcode(rval);
625   }
626  
627   /* Get Matrix BSDF value */
628   static int
629   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
630 <                                const FVECT inVec, const void *dist)
630 >                                const FVECT inVec, SDComponent *sdc)
631   {
632 <        const SDMat     *dp = (const SDMat *)dist;
632 >        const SDMat     *dp;
633          int             i_ndx, o_ndx;
634 +                                        /* check arguments */
635 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
636 +                        || (dp = (SDMat *)sdc->dist) == NULL)
637 +                return 0;
638                                          /* get angle indices */
639          i_ndx = mBSDF_incndx(dp, inVec);
640          o_ndx = mBSDF_outndx(dp, outVec);
# Line 651 | Line 649 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
649          return 1;                       /* XXX monochrome for now */
650   }
651  
652 < /* Query solid angle for vector */
652 > /* Query solid angle for vector(s) */
653   static SDError
654 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
654 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
655 >                                        int qflags, SDComponent *sdc)
656   {
657 <        const SDMat     *dp = (const SDMat *)dist;
658 <
659 <        if (!(qflags & SDqueryInc+SDqueryOut))
657 >        const SDMat     *dp;
658 >        double          inc_psa, out_psa;
659 >                                        /* check arguments */
660 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
661 >                        (dp = (SDMat *)sdc->dist) == NULL)
662                  return SDEargument;
663 <        if (qflags & SDqueryInc) {
664 <                double  inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
665 <                if (inc_psa < .0)
666 <                        return SDEinternal;
667 <                switch (qflags & SDqueryMin+SDqueryMax) {
668 <                case SDqueryMax:
669 <                        if (inc_psa > psa[0])
670 <                                psa[0] = inc_psa;
671 <                        break;
672 <                case SDqueryMin+SDqueryMax:
673 <                        if (inc_psa > psa[1])
674 <                                psa[1] = inc_psa;
675 <                        /* fall through */
675 <                case SDqueryMin:
676 <                        if (inc_psa < psa[0])
677 <                                psa[0] = inc_psa;
678 <                        break;
679 <                case 0:
663 >        if (v2 == NULL)
664 >                v2 = v1;
665 >                                        /* get projected solid angles */
666 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
667 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
668 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
669 >                inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
670 >                out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
671 >        }
672 >
673 >        switch (qflags) {               /* record based on flag settings */
674 >        case SDqueryMax:
675 >                if (inc_psa > psa[0])
676                          psa[0] = inc_psa;
677 <                        break;
678 <                }
677 >                if (out_psa > psa[0])
678 >                        psa[0] = out_psa;
679 >                break;
680 >        case SDqueryMin+SDqueryMax:
681 >                if (inc_psa > psa[1])
682 >                        psa[1] = inc_psa;
683 >                if (out_psa > psa[1])
684 >                        psa[1] = out_psa;
685 >                /* fall through */
686 >        case SDqueryVal:
687 >                if (qflags == SDqueryVal)
688 >                        psa[0] = M_PI;
689 >                /* fall through */
690 >        case SDqueryMin:
691 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
692 >                        psa[0] = inc_psa;
693 >                if ((out_psa > 0) & (out_psa < psa[0]))
694 >                        psa[0] = out_psa;
695 >                break;
696          }
697 <        if (qflags & SDqueryOut) {
698 <                double  out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
686 <                if (out_psa < .0)
687 <                        return SDEinternal;
688 <                switch (qflags & SDqueryMin+SDqueryMax) {
689 <                case SDqueryMax:
690 <                        if (out_psa > psa[0])
691 <                                psa[0] = out_psa;
692 <                        break;
693 <                case SDqueryMin+SDqueryMax:
694 <                        if (out_psa > psa[1])
695 <                                psa[1] = out_psa;
696 <                        /* fall through */
697 <                case SDqueryMin:
698 <                        if (out_psa < psa[0])
699 <                                psa[0] = out_psa;
700 <                        break;
701 <                case 0:
702 <                        psa[(qflags&SDqueryInc)!=0] = out_psa;
703 <                        break;
704 <                }
705 <        }
706 <        return SDEnone;
697 >                                        /* make sure it's legal */
698 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
699   }
700  
701   /* Compute new cumulative distribution from BSDF */
# Line 741 | Line 733 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
733   static const SDCDst *
734   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
735   {
736 <        SDMat           *dp = (SDMat *)sdc->dist;
736 >        SDMat           *dp;
737          int             reverse;
738          SDMatCDst       myCD;
739          SDMatCDst       *cd, *cdlast;
740 <
741 <        if (dp == NULL)
740 >                                        /* check arguments */
741 >        if ((inVec == NULL) | (sdc == NULL) ||
742 >                        (dp = (SDMat *)sdc->dist) == NULL)
743                  return NULL;
744          memset(&myCD, 0, sizeof(myCD));
745          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 765 | Line 758 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
758                  reverse = 1;
759          }
760          cdlast = NULL;                  /* check for it in cache list */
761 <        for (cd = (SDMatCDst *)sdc->cdList;
762 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
761 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
762 >                                cdlast = cd, cd = (SDMatCDst *)cd->next)
763                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
764                                          (cd->ob_priv == myCD.ob_priv) &
765                                          (cd->ob_vec == myCD.ob_vec))
766                          break;
774                cdlast = cd;
775        }
767          if (cd == NULL) {               /* need to allocate new entry */
768                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
769 <                                        myCD.calen*sizeof(myCD.carr[0]));
769 >                                        sizeof(myCD.carr[0])*myCD.calen);
770                  if (cd == NULL)
771                          return NULL;
772                  *cd = myCD;             /* compute cumulative distribution */
# Line 795 | Line 786 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
786  
787   /* Sample cumulative distribution */
788   static SDError
789 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
789 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
790   {
791          const unsigned  maxval = ~0;
792          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
793          const unsigned  target = randX*maxval;
794          int             i, iupper, ilower;
795 +                                        /* check arguments */
796 +        if ((ioVec == NULL) | (mcd == NULL))
797 +                return SDEargument;
798                                          /* binary search to find index */
799          ilower = 0; iupper = mcd->calen;
800          while ((i = (iupper + ilower) >> 1) != ilower)
801 <                if ((long)target >= (long)mcd->carr[i])
801 >                if (target >= mcd->carr[i])
802                          ilower = i;
803                  else
804                          iupper = i;
# Line 812 | Line 806 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
806          randX = (randX*maxval - mcd->carr[ilower]) /
807                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
808                                          /* convert index to vector */
809 <        if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv))
809 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
810                  return SDEnone;
811 <        strcpy(SDerrorDetail, "BSDF sampling fault");
811 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
812          return SDEinternal;
813   }
814  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines