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.2 by greg, Fri Feb 18 00:41:44 2011 UTC vs.
Revision 3.16 by greg, Thu Jun 9 17:09:39 2011 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   *
12   */
13  
14 < #include <stdio.h>
14 > #define _USE_MATH_DEFINES
15 > #include "rtio.h"
16   #include <stdlib.h>
17   #include <math.h>
17 #include <strings.h>
18   #include <ctype.h>
19   #include "ezxml.h"
20   #include "bsdf.h"
21   #include "bsdf_m.h"
22  
23 #ifndef FTINY
24 #define FTINY           1e-6
25 #endif
26
23   /* Function return codes */
24   #define RC_GOOD         1
25   #define RC_FAIL         0
# Line 86 | 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 */
90 > /* Returns the name of the given tag */
91   #ifdef ezxml_name
92   #undef ezxml_name
93   static char *
# Line 103 | Line 99 | ezxml_name(ezxml_t xml)
99   }
100   #endif
101  
102 < /* returns the given tag's character content or empty string if none */
102 > /* Returns the given tag's character content or empty string if none */
103   #ifdef ezxml_txt
104   #undef ezxml_txt
105   static char *
# Line 162 | Line 158 | SDnewMatrix(int ni, int no)
158   /* Free a BSDF matrix */
159   #define SDfreeMatrix            free
160  
161 < /* get vector for this angle basis index */
161 > /* get vector for this angle basis index (front exiting) */
162   static int
163 < ab_getvec(FVECT v, int ndx, double randX, void *p)
163 > fo_getvec(FVECT v, double ndxr, void *p)
164   {
165 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
165 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
166 >        int             ndx = (int)ndxr;
167 >        double          randX = ndxr - ndx;
168          double          rx[2];
169          int             li;
170          double          pol, azi, d;
171          
172 <        if ((ndx < 0) | (ndx >= ab->nangles))
172 >        if ((ndxr < 0) | (ndx >= ab->nangles))
173                  return RC_FAIL;
174          for (li = 0; ndx >= ab->lat[li].nphis; li++)
175                  ndx -= ab->lat[li].nphis;
# Line 186 | Line 184 | ab_getvec(FVECT v, int ndx, double randX, void *p)
184          return RC_GOOD;
185   }
186  
187 < /* get index corresponding to the given vector */
187 > /* get index corresponding to the given vector (front exiting) */
188   static int
189 < ab_getndx(const FVECT v, void *p)
189 > fo_getndx(const FVECT v, void *p)
190   {
191 <        ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
191 >        ANGLE_BASIS     *ab = (ANGLE_BASIS *)p;
192          int     li, ndx;
193          double  pol, azi, d;
194  
195          if (v == NULL)
196                  return -1;
197 <        if ((v[2] < .0) | (v[2] > 1.0))
197 >        if ((v[2] < 0) | (v[2] > 1.))
198                  return -1;
199          pol = 180.0/M_PI*acos(v[2]);
200          azi = 180.0/M_PI*atan2(v[1], v[0]);
# Line 215 | Line 213 | ab_getndx(const FVECT v, void *p)
213   /* compute square of real value */
214   static double sq(double x) { return x*x; }
215  
216 < /* get projected solid angle for this angle basis index */
216 > /* get projected solid angle for this angle basis index (universal) */
217   static double
218 < ab_getohm(int ndx, void *p)
218 > io_getohm(int ndx, void *p)
219   {
220          static int      last_li = -1;
221          static double   last_ohm;
# Line 240 | Line 238 | ab_getohm(int ndx, void *p)
238                                  (double)ab->lat[li].nphis;
239   }
240  
241 < /* get reverse vector for this angle basis index */
241 > /* get vector for this angle basis index (back incident) */
242   static int
243 < ab_getvecR(FVECT v, int ndx, double randX, void *p)
243 > bi_getvec(FVECT v, double ndxr, void *p)
244   {
245 <        int     na = (*(ANGLE_BASIS *)p).nangles;
248 <
249 <        if (!ab_getvec(v, ndx, randX, p))
245 >        if (!fo_getvec(v, ndxr, p))
246                  return RC_FAIL;
247  
248          v[0] = -v[0];
# Line 256 | Line 252 | ab_getvecR(FVECT v, int ndx, double randX, void *p)
252          return RC_GOOD;
253   }
254  
255 < /* get index corresponding to the reverse vector */
255 > /* get index corresponding to the vector (back incident) */
256   static int
257 < ab_getndxR(const FVECT v, void *p)
257 > bi_getndx(const FVECT v, void *p)
258   {
259          FVECT  v2;
260          
# Line 266 | Line 262 | ab_getndxR(const FVECT v, void *p)
262          v2[1] = -v[1];
263          v2[2] = -v[2];
264  
265 <        return ab_getndx(v2, p);
265 >        return fo_getndx(v2, p);
266   }
267  
268 + /* get vector for this angle basis index (back exiting) */
269 + static int
270 + bo_getvec(FVECT v, double ndxr, void *p)
271 + {
272 +        if (!fo_getvec(v, ndxr, p))
273 +                return RC_FAIL;
274 +
275 +        v[2] = -v[2];
276 +
277 +        return RC_GOOD;
278 + }
279 +
280 + /* get index corresponding to the vector (back exiting) */
281 + static int
282 + bo_getndx(const FVECT v, void *p)
283 + {
284 +        FVECT  v2;
285 +        
286 +        v2[0] = v[0];
287 +        v2[1] = v[1];
288 +        v2[2] = -v[2];
289 +
290 +        return fo_getndx(v2, p);
291 + }
292 +
293 + /* get vector for this angle basis index (front incident) */
294 + static int
295 + fi_getvec(FVECT v, double ndxr, void *p)
296 + {
297 +        if (!fo_getvec(v, ndxr, p))
298 +                return RC_FAIL;
299 +
300 +        v[0] = -v[0];
301 +        v[1] = -v[1];
302 +
303 +        return RC_GOOD;
304 + }
305 +
306 + /* get index corresponding to the vector (front incident) */
307 + static int
308 + fi_getndx(const FVECT v, void *p)
309 + {
310 +        FVECT  v2;
311 +        
312 +        v2[0] = -v[0];
313 +        v2[1] = -v[1];
314 +        v2[2] = v[2];
315 +
316 +        return fo_getndx(v2, p);
317 + }
318 +
319   /* load custom BSDF angle basis */
320   static int
321   load_angle_basis(ezxml_t wab)
# Line 306 | Line 353 | load_angle_basis(ezxml_t wab)
353                                          "ThetaBounds"), "LowerTheta"))),
354                                  abase_list[nabases].lat[i].tmin)) {
355                          sprintf(SDerrorDetail, "Theta values disagree in '%s'",
356 <                                abname);
356 >                                        abname);
357                          return RC_DATERR;
358                  }
359                  abase_list[nabases].nangles +=
# Line 316 | Line 363 | load_angle_basis(ezxml_t wab)
363                                  (abase_list[nabases].lat[i].nphis == 1 &&
364                                  abase_list[nabases].lat[i].tmin > FTINY)) {
365                          sprintf(SDerrorDetail, "Illegal phi count in '%s'",
366 <                                abname);
366 >                                        abname);
367                          return RC_DATERR;
368                  }
369          }
# Line 351 | Line 398 | get_extrema(SDSpectralDF *df)
398          }
399          free(ohma);
400                                          /* need incoming solid angles, too? */
401 <        if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm ||
355 <                                dp->ib_priv != dp->ob_priv) {
401 >        if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) {
402                  double  ohm;
403                  for (i = dp->ninc; i--; )
404                          if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA)
# Line 361 | Line 407 | get_extrema(SDSpectralDF *df)
407          return (df->maxHemi <= 1.01);
408   }
409  
364 /* skip integer in string */
365 static char *
366 i_skip(char  *s)
367 {
368        while (isspace(*s))
369                s++;
370        if (*s == '-' || *s == '+')
371                s++;
372        if (!isdigit(*s))
373                return(NULL);
374        do
375                s++;
376        while (isdigit(*s));
377        return(s);
378 }
379
380 /* skip float in string */
381 static char *
382 f_skip(char  *s)
383 {
384        register char  *cp;
385
386        while (isspace(*s))
387                s++;
388        if (*s == '-' || *s == '+')
389                s++;
390        cp = s;
391        while (isdigit(*cp))
392                cp++;
393        if (*cp == '.') {
394                cp++; s++;
395                while (isdigit(*cp))
396                        cp++;
397        }
398        if (cp == s)
399                return(NULL);
400        if (*cp == 'e' || *cp == 'E')
401                return(i_skip(cp+1));
402        return(cp);
403 }
404
410   /* load BSDF distribution for this wavelength */
411   static int
412   load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
413   {
409        char            *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
410        char            *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
414          SDSpectralDF    *df;
415          SDMat           *dp;
416          char            *sdata;
417 +        int             tfront;
418          int             inbi, outbi;
419          int             i;
416
417        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
418                sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'",
419                                sd->name);
420                return RC_FORMERR;
421        }
420                                          /* allocate BSDF component */
421          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
422 <        if (!strcasecmp(sdata, "Transmission Front")) {
422 >        if (!sdata)
423 >                return RC_FAIL;
424 >        /*
425 >         * Remember that front and back are reversed from WINDOW 6 orientations
426 >         * Favor their "Front" (incoming light) since that's more often valid
427 >         */
428 >        tfront = !strcasecmp(sdata, "Transmission Back");
429 >        if (!strcasecmp(sdata, "Transmission Front") ||
430 >                        tfront & (sd->tf == NULL)) {
431                  if (sd->tf != NULL)
432                          SDfreeSpectralDF(sd->tf);
433                  if ((sd->tf = SDnewSpectralDF(1)) == NULL)
434                          return RC_MEMERR;
435                  df = sd->tf;
436          } else if (!strcasecmp(sdata, "Reflection Front")) {
437 <                if (sd->rf != NULL)
432 <                        SDfreeSpectralDF(sd->rf);
433 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
434 <                        return RC_MEMERR;
435 <                df = sd->rf;
436 <        } else if (!strcasecmp(sdata, "Reflection Back")) {
437 <                if (sd->rb != NULL)
437 >                if (sd->rb != NULL)     /* note back-front reversal */
438                          SDfreeSpectralDF(sd->rb);
439                  if ((sd->rb = SDnewSpectralDF(1)) == NULL)
440                          return RC_MEMERR;
441                  df = sd->rb;
442 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
443 +                if (sd->rf != NULL)     /* note front-back reversal */
444 +                        SDfreeSpectralDF(sd->rf);
445 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
446 +                        return RC_MEMERR;
447 +                df = sd->rf;
448          } else
449                  return RC_FAIL;
450 +        /* XXX should also check "ScatteringDataType" for consistency? */
451                                          /* get angle bases */
452          sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
453          if (!sdata || !*sdata) {
# Line 449 | Line 456 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
456                  return RC_FORMERR;
457          }
458          for (inbi = nabases; inbi--; )
459 <                if (!strcasecmp(cbasis, abase_list[inbi].name))
459 >                if (!strcasecmp(sdata, abase_list[inbi].name))
460                          break;
461          if (inbi < 0) {
462 <                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'",
456 <                                cbasis);
462 >                sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata);
463                  return RC_FORMERR;
464          }
465          sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
# Line 463 | Line 469 | load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc)
469                  return RC_FORMERR;
470          }
471          for (outbi = nabases; outbi--; )
472 <                if (!strcasecmp(rbasis, abase_list[outbi].name))
472 >                if (!strcasecmp(sdata, abase_list[outbi].name))
473                          break;
474          if (outbi < 0) {
475 <                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis);
475 >                sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata);
476                  return RC_FORMERR;
477          }
478                                          /* allocate BSDF matrix */
479          dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles);
480          if (dp == NULL)
481                  return RC_MEMERR;
482 <        dp->ib_priv = (void *)&abase_list[inbi];
483 <        dp->ob_priv = (void *)&abase_list[outbi];
482 >        dp->ib_priv = &abase_list[inbi];
483 >        dp->ob_priv = &abase_list[outbi];
484          if (df == sd->tf) {
485 <                dp->ib_vec = ab_getvecR;
486 <                dp->ib_ndx = ab_getndxR;
487 <                dp->ob_vec = ab_getvec;
488 <                dp->ob_ndx = ab_getndx;
485 >                if (tfront) {
486 >                        dp->ib_vec = &fi_getvec;
487 >                        dp->ib_ndx = &fi_getndx;
488 >                        dp->ob_vec = &bo_getvec;
489 >                        dp->ob_ndx = &bo_getndx;
490 >                } else {
491 >                        dp->ib_vec = &bi_getvec;
492 >                        dp->ib_ndx = &bi_getndx;
493 >                        dp->ob_vec = &fo_getvec;
494 >                        dp->ob_ndx = &fo_getndx;
495 >                }
496          } else if (df == sd->rf) {
497 <                dp->ib_vec = ab_getvec;
498 <                dp->ib_ndx = ab_getndx;
499 <                dp->ob_vec = ab_getvec;
500 <                dp->ob_ndx = ab_getndx;
497 >                dp->ib_vec = &fi_getvec;
498 >                dp->ib_ndx = &fi_getndx;
499 >                dp->ob_vec = &fo_getvec;
500 >                dp->ob_ndx = &fo_getndx;
501          } else /* df == sd->rb */ {
502 <                dp->ib_vec = ab_getvecR;
503 <                dp->ib_ndx = ab_getndxR;
504 <                dp->ob_vec = ab_getvecR;
505 <                dp->ob_ndx = ab_getndxR;
502 >                dp->ib_vec = &bi_getvec;
503 >                dp->ib_ndx = &bi_getndx;
504 >                dp->ob_vec = &bo_getvec;
505 >                dp->ob_ndx = &bo_getndx;
506          }
507 <        dp->ib_ohm = ab_getohm;
508 <        dp->ob_ohm = ab_getohm;
507 >        dp->ib_ohm = &io_getohm;
508 >        dp->ob_ohm = &io_getohm;
509          df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
510          df->comp[0].dist = dp;
511          df->comp[0].func = &SDhandleMtx;
512                                          /* read BSDF data */
513 <        sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
513 >        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
514          if (!sdata || !*sdata) {
515                  sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
516                                  sd->name);
517                  return RC_FORMERR;
518          }
519          for (i = 0; i < dp->ninc*dp->nout; i++) {
520 <                char  *sdnext = f_skip(sdata);
520 >                char  *sdnext = fskip(sdata);
521                  if (sdnext == NULL) {
522                          sprintf(SDerrorDetail,
523                                  "Bad/missing BSDF ScatteringData in '%s'",
524                                          sd->name);
525                          return RC_FORMERR;
526                  }
527 <                while (*sdnext && isspace(*sdnext))
527 >                while (isspace(*sdnext))
528                          sdnext++;
529                  if (*sdnext == ',') sdnext++;
530                  if (rowinc) {
531                          int     r = i/dp->nout;
532 <                        int     c = i - c*dp->nout;
532 >                        int     c = i - r*dp->nout;
533                          mBSDF_value(dp,r,c) = atof(sdata);
534                  } else
535                          dp->bsdf[i] = atof(sdata);
# Line 536 | Line 549 | subtract_min(SDMat *sm)
549          for (i = n; --i; )
550                  if (sm->bsdf[i] < minv)
551                          minv = sm->bsdf[i];
552 +        
553 +        if (minv <= FTINY)
554 +                return .0;
555 +
556          for (i = n; i--; )
557                  sm->bsdf[i] -= minv;
558  
# Line 561 | Line 578 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
578                  c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
579                  dv->cieY += ymin;
580          }
581 <        df->maxHemi -= dv->cieY;        /* correct minimum hemispherical */
582 <        dv->spec.clock++;               /* make sure everything is set */
581 >        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
582 >                                        /* make sure everything is set */
583          c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
584   }
585  
586   /* Load a BSDF matrix from an open XML file */
587   SDError
588 < SDloadMtx(SDData *sd, ezxml_t fl)
588 > SDloadMtx(SDData *sd, ezxml_t wtl)
589   {
590 <        ezxml_t                 wtl, wld, wdb;
591 <        int                     rowIn;
592 <        struct BSDF_data        *dp;
593 <        char                    *txt;
594 <        int                     rval;
595 <        
596 <        if (strcmp(ezxml_name(fl), "WindowElement")) {
590 >        ezxml_t         wld, wdb;
591 >        int             rowIn;
592 >        char            *txt;
593 >        int             rval;
594 >                                        /* basic checks and data ordering */
595 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
596 >                        "DataDefinition"), "IncidentDataStructure"));
597 >        if (txt == NULL || !*txt) {
598                  sprintf(SDerrorDetail,
599 <                        "BSDF \"%s\": top level node not 'WindowElement'",
599 >                        "BSDF \"%s\": missing IncidentDataStructure",
600                                  sd->name);
601                  return SDEformat;
602          }
585        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
586        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
587                        "DataDefinition"), "IncidentDataStructure"));
603          if (!strcasecmp(txt, "Rows"))
604                  rowIn = 1;
605          else if (!strcasecmp(txt, "Columns"))
# Line 595 | Line 610 | SDloadMtx(SDData *sd, ezxml_t fl)
610                                  sd->name);
611                  return SDEsupport;
612          }
613 <                                /* get angle basis */
613 >                                        /* get angle basis */
614          rval = load_angle_basis(ezxml_child(ezxml_child(wtl,
615                                  "DataDefinition"), "AngleBasis"));
616          if (rval < 0)
617 <                goto err_return;
618 <                                /* load BSDF components */
617 >                return convert_errcode(rval);
618 >                                        /* load BSDF components */
619          for (wld = ezxml_child(wtl, "WavelengthData");
620                                  wld != NULL; wld = wld->next) {
621                  if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
# Line 609 | Line 624 | SDloadMtx(SDData *sd, ezxml_t fl)
624                  for (wdb = ezxml_child(wld, "WavelengthDataBlock");
625                                          wdb != NULL; wdb = wdb->next)
626                          if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0)
627 <                                goto err_return;
627 >                                return convert_errcode(rval);
628          }
629 <                                /* separate diffuse components */
629 >                                        /* separate diffuse components */
630          extract_diffuse(&sd->rLambFront, sd->rf);
631          extract_diffuse(&sd->rLambBack, sd->rb);
632          extract_diffuse(&sd->tLamb, sd->tf);
633 <                                /* return success */
633 >                                        /* return success */
634          return SDEnone;
620 err_return:                     /* jump here on failure */
621        if (sd->rf != NULL) {
622                SDfreeSpectralDF(sd->rf);
623                sd->rf = NULL;
624        }
625        if (sd->rb != NULL) {
626                SDfreeSpectralDF(sd->rb);
627                sd->rb = NULL;
628        }
629        if (sd->tf != NULL) {
630                SDfreeSpectralDF(sd->tf);
631                sd->tf = NULL;
632        }
633        return convert_errcode(rval);
635   }
636  
637   /* Get Matrix BSDF value */
638   static int
639   SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
640 <                                const FVECT inVec, const void *dist)
640 >                                const FVECT inVec, SDComponent *sdc)
641   {
642 <        const SDMat     *dp = (const SDMat *)dist;
642 >        const SDMat     *dp;
643          int             i_ndx, o_ndx;
644 +                                        /* check arguments */
645 +        if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
646 +                        || (dp = (SDMat *)sdc->dist) == NULL)
647 +                return 0;
648                                          /* get angle indices */
649          i_ndx = mBSDF_incndx(dp, inVec);
650          o_ndx = mBSDF_outndx(dp, outVec);
# Line 654 | Line 659 | SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec,
659          return 1;                       /* XXX monochrome for now */
660   }
661  
662 < /* Query solid angle for vector */
662 > /* Query solid angle for vector(s) */
663   static SDError
664 < SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist)
664 > SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2,
665 >                                        int qflags, SDComponent *sdc)
666   {
667 <        const SDMat     *dp = (const SDMat *)dist;
668 <
669 <        if (!(qflags & SDqueryInc+SDqueryOut))
667 >        const SDMat     *dp;
668 >        double          inc_psa, out_psa;
669 >                                        /* check arguments */
670 >        if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
671 >                        (dp = (SDMat *)sdc->dist) == NULL)
672                  return SDEargument;
673 <        if (qflags & SDqueryInc) {
674 <                double  inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec));
675 <                if (inc_psa < .0)
676 <                        return SDEinternal;
677 <                switch (qflags & SDqueryMin+SDqueryMax) {
678 <                case SDqueryMax:
679 <                        if (inc_psa > psa[0])
680 <                                psa[0] = inc_psa;
681 <                        break;
682 <                case SDqueryMin+SDqueryMax:
683 <                        if (inc_psa > psa[1])
684 <                                psa[1] = inc_psa;
685 <                        /* fall through */
678 <                case SDqueryMin:
679 <                        if (inc_psa < psa[0])
680 <                                psa[0] = inc_psa;
681 <                        break;
682 <                case 0:
673 >        if (v2 == NULL)
674 >                v2 = v1;
675 >                                        /* get projected solid angles */
676 >        out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1));
677 >        inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2));
678 >        if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) {
679 >                inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2));
680 >                out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1));
681 >        }
682 >
683 >        switch (qflags) {               /* record based on flag settings */
684 >        case SDqueryMax:
685 >                if (inc_psa > psa[0])
686                          psa[0] = inc_psa;
687 <                        break;
688 <                }
687 >                if (out_psa > psa[0])
688 >                        psa[0] = out_psa;
689 >                break;
690 >        case SDqueryMin+SDqueryMax:
691 >                if (inc_psa > psa[1])
692 >                        psa[1] = inc_psa;
693 >                if (out_psa > psa[1])
694 >                        psa[1] = out_psa;
695 >                /* fall through */
696 >        case SDqueryVal:
697 >                if (qflags == SDqueryVal)
698 >                        psa[0] = M_PI;
699 >                /* fall through */
700 >        case SDqueryMin:
701 >                if ((inc_psa > 0) & (inc_psa < psa[0]))
702 >                        psa[0] = inc_psa;
703 >                if ((out_psa > 0) & (out_psa < psa[0]))
704 >                        psa[0] = out_psa;
705 >                break;
706          }
707 <        if (qflags & SDqueryOut) {
708 <                double  out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec));
689 <                if (out_psa < .0)
690 <                        return SDEinternal;
691 <                switch (qflags & SDqueryMin+SDqueryMax) {
692 <                case SDqueryMax:
693 <                        if (out_psa > psa[0])
694 <                                psa[0] = out_psa;
695 <                        break;
696 <                case SDqueryMin+SDqueryMax:
697 <                        if (out_psa > psa[1])
698 <                                psa[1] = out_psa;
699 <                        /* fall through */
700 <                case SDqueryMin:
701 <                        if (out_psa < psa[0])
702 <                                psa[0] = out_psa;
703 <                        break;
704 <                case 0:
705 <                        psa[(qflags&SDqueryInc)!=0] = out_psa;
706 <                        break;
707 <                }
708 <        }
709 <        return SDEnone;
707 >                                        /* make sure it's legal */
708 >        return (psa[0] <= 0) ? SDEinternal : SDEnone;
709   }
710  
711   /* Compute new cumulative distribution from BSDF */
# Line 744 | Line 743 | make_cdist(SDMatCDst *cd, const FVECT inVec, SDMat *dp
743   static const SDCDst *
744   SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
745   {
746 <        SDMat           *dp = (SDMat *)sdc->dist;
746 >        SDMat           *dp;
747          int             reverse;
748          SDMatCDst       myCD;
749          SDMatCDst       *cd, *cdlast;
750 <
751 <        if (dp == NULL)
750 >                                        /* check arguments */
751 >        if ((inVec == NULL) | (sdc == NULL) ||
752 >                        (dp = (SDMat *)sdc->dist) == NULL)
753                  return NULL;
754          memset(&myCD, 0, sizeof(myCD));
755          myCD.indx = mBSDF_incndx(dp, inVec);
# Line 768 | Line 768 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
768                  reverse = 1;
769          }
770          cdlast = NULL;                  /* check for it in cache list */
771 <        for (cd = (SDMatCDst *)sdc->cdList;
772 <                                cd != NULL; cd = (SDMatCDst *)cd->next) {
771 >        for (cd = (SDMatCDst *)sdc->cdList; cd != NULL;
772 >                                cdlast = cd, cd = (SDMatCDst *)cd->next)
773                  if (cd->indx == myCD.indx && (cd->calen == myCD.calen) &
774                                          (cd->ob_priv == myCD.ob_priv) &
775                                          (cd->ob_vec == myCD.ob_vec))
776                          break;
777                cdlast = cd;
778        }
777          if (cd == NULL) {               /* need to allocate new entry */
778                  cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) +
779 <                                        myCD.calen*sizeof(myCD.carr[0]));
779 >                                        sizeof(myCD.carr[0])*myCD.calen);
780                  if (cd == NULL)
781                          return NULL;
782                  *cd = myCD;             /* compute cumulative distribution */
# Line 798 | Line 796 | SDgetMtxCDist(const FVECT inVec, SDComponent *sdc)
796  
797   /* Sample cumulative distribution */
798   static SDError
799 < SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp)
799 > SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp)
800   {
801          const unsigned  maxval = ~0;
802          const SDMatCDst *mcd = (const SDMatCDst *)cdp;
803          const unsigned  target = randX*maxval;
804          int             i, iupper, ilower;
805 +                                        /* check arguments */
806 +        if ((ioVec == NULL) | (mcd == NULL))
807 +                return SDEargument;
808                                          /* binary search to find index */
809          ilower = 0; iupper = mcd->calen;
810          while ((i = (iupper + ilower) >> 1) != ilower)
# Line 815 | Line 816 | SDsampMtxCDist(FVECT outVec, double randX, const SDCDs
816          randX = (randX*maxval - mcd->carr[ilower]) /
817                          (double)(mcd->carr[iupper] - mcd->carr[ilower]);
818                                          /* convert index to vector */
819 <        if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv))
819 >        if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv))
820                  return SDEnone;
821 <        strcpy(SDerrorDetail, "BSDF sampling fault");
821 >        strcpy(SDerrorDetail, "Matrix BSDF sampling fault");
822          return SDEinternal;
823   }
824  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines