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

Comparing ray/src/common/bsdf.c (file contents):
Revision 2.1 by greg, Wed Jun 17 20:41:47 2009 UTC vs.
Revision 2.14 by greg, Fri Feb 11 17:31:25 2011 UTC

# Line 23 | Line 23 | typedef struct {
23          }       lat[MAXLATS+1];         /* latitudes */
24   } ANGLE_BASIS;
25  
26 < #define MAXABASES       3               /* limit on defined bases */
26 > #define MAXABASES       7               /* limit on defined bases */
27  
28   static ANGLE_BASIS      abase_list[MAXABASES] = {
29          {
# Line 61 | Line 61 | static ANGLE_BASIS     abase_list[MAXABASES] = {
61  
62   static int      nabases = 3;    /* current number of defined bases */
63  
64 + #define  FEQ(a,b)       ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6)
65  
66   static int
67 + fequal(double a, double b)
68 + {
69 +        if (b != .0)
70 +                a = a/b - 1.;
71 +        return((a <= 1e-6) & (a >= -1e-6));
72 + }
73 +
74 + /* Returns the name of the given tag */
75 + #ifdef ezxml_name
76 + #undef ezxml_name
77 + static char *
78 + ezxml_name(ezxml_t xml)
79 + {
80 +        if (xml == NULL)
81 +                return(NULL);
82 +        return(xml->name);
83 + }
84 + #endif
85 +
86 + /* Returns the given tag's character content or empty string if none */
87 + #ifdef ezxml_txt
88 + #undef ezxml_txt
89 + static char *
90 + ezxml_txt(ezxml_t xml)
91 + {
92 +        if (xml == NULL)
93 +                return("");
94 +        return(xml->txt);
95 + }
96 + #endif
97 +
98 +
99 + static int
100   ab_getvec(              /* get vector for this angle basis index */
101          FVECT v,
102          int ndx,
# Line 71 | Line 105 | ab_getvec(             /* get vector for this angle basis index *
105   {
106          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
107          int     li;
108 <        double  alt, azi, d;
108 >        double  pol, azi, d;
109          
110          if ((ndx < 0) | (ndx >= ab->nangles))
111                  return(0);
112          for (li = 0; ndx >= ab->lat[li].nphis; li++)
113                  ndx -= ab->lat[li].nphis;
114 <        alt = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
114 >        pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
115          azi = 2.*PI*ndx/ab->lat[li].nphis;
116 <        v[2] = d = cos(alt);
117 <        d = sqrt(1. - d*d);     /* sin(alt) */
116 >        v[2] = d = cos(pol);
117 >        d = sqrt(1. - d*d);     /* sin(pol) */
118          v[0] = cos(azi)*d;
119          v[1] = sin(azi)*d;
120          return(1);
# Line 95 | Line 129 | ab_getndx(             /* get index corresponding to the given ve
129   {
130          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
131          int     li, ndx;
132 <        double  alt, azi, d;
132 >        double  pol, azi, d;
133  
134          if ((v[2] < -1.0) | (v[2] > 1.0))
135                  return(-1);
136 <        alt = 180.0/PI*acos(v[2]);
136 >        pol = 180.0/PI*acos(v[2]);
137          azi = 180.0/PI*atan2(v[1], v[0]);
138          if (azi < 0.0) azi += 360.0;
139 <        for (li = 1; ab->lat[li].tmin <= alt; li++)
139 >        for (li = 1; ab->lat[li].tmin <= pol; li++)
140                  if (!ab->lat[li].nphis)
141                          return(-1);
142          --li;
# Line 174 | Line 208 | ab_getndxR(            /* get index corresponding to the reverse
208  
209  
210   static void
211 + load_angle_basis(       /* load custom BSDF angle basis */
212 +        ezxml_t wab
213 + )
214 + {
215 +        char    *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName"));
216 +        ezxml_t wbb;
217 +        int     i;
218 +        
219 +        if (!abname || !*abname)
220 +                return;
221 +        for (i = nabases; i--; )
222 +                if (!strcasecmp(abname, abase_list[i].name))
223 +                        return;         /* assume it's the same */
224 +        if (nabases >= MAXABASES)
225 +                error(INTERNAL, "too many angle bases");
226 +        strcpy(abase_list[nabases].name, abname);
227 +        abase_list[nabases].nangles = 0;
228 +        for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock");
229 +                        wbb != NULL; i++, wbb = wbb->next) {
230 +                if (i >= MAXLATS)
231 +                        error(INTERNAL, "too many latitudes in custom basis");
232 +                abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt(
233 +                                ezxml_child(ezxml_child(wbb,
234 +                                        "ThetaBounds"), "UpperTheta")));
235 +                if (!i)
236 +                        abase_list[nabases].lat[i].tmin =
237 +                                        -abase_list[nabases].lat[i+1].tmin;
238 +                else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
239 +                                        "ThetaBounds"), "LowerTheta"))),
240 +                                abase_list[nabases].lat[i].tmin))
241 +                        error(WARNING, "theta values disagree in custom basis");
242 +                abase_list[nabases].nangles +=
243 +                        abase_list[nabases].lat[i].nphis =
244 +                                atoi(ezxml_txt(ezxml_child(wbb, "nPhis")));
245 +        }
246 +        abase_list[nabases++].lat[i].nphis = 0;
247 + }
248 +
249 +
250 + static double
251 + to_meters(              /* return factor to convert given unit to meters */
252 +        const char *unit
253 + )
254 + {
255 +        if (unit == NULL) return(1.);           /* safe assumption? */
256 +        if (!strcasecmp(unit, "Meter")) return(1.);
257 +        if (!strcasecmp(unit, "Foot")) return(.3048);
258 +        if (!strcasecmp(unit, "Inch")) return(.0254);
259 +        if (!strcasecmp(unit, "Centimeter")) return(.01);
260 +        if (!strcasecmp(unit, "Millimeter")) return(.001);
261 +        sprintf(errmsg, "unknown dimensional unit '%s'", unit);
262 +        error(USER, errmsg);
263 + }
264 +
265 +
266 + static void
267 + load_geometry(          /* load geometric dimensions and description (if any) */
268 +        struct BSDF_data *dp,
269 +        ezxml_t wdb
270 + )
271 + {
272 +        ezxml_t         geom;
273 +        double          cfact;
274 +        const char      *fmt, *mgfstr;
275 +
276 +        dp->dim[0] = dp->dim[1] = dp->dim[2] = 0;
277 +        dp->mgf = NULL;
278 +        if ((geom = ezxml_child(wdb, "Width")) != NULL)
279 +                dp->dim[0] = atof(ezxml_txt(geom)) *
280 +                                to_meters(ezxml_attr(geom, "unit"));
281 +        if ((geom = ezxml_child(wdb, "Height")) != NULL)
282 +                dp->dim[1] = atof(ezxml_txt(geom)) *
283 +                                to_meters(ezxml_attr(geom, "unit"));
284 +        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
285 +                dp->dim[2] = atof(ezxml_txt(geom)) *
286 +                                to_meters(ezxml_attr(geom, "unit"));
287 +        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
288 +                        (mgfstr = ezxml_txt(geom)) == NULL)
289 +                return;
290 +        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
291 +                        strcasecmp(fmt, "MGF")) {
292 +                sprintf(errmsg, "unrecognized geometry format '%s'", fmt);
293 +                error(WARNING, errmsg);
294 +                return;
295 +        }
296 +        cfact = to_meters(ezxml_attr(geom, "unit"));
297 +        dp->mgf = (char *)malloc(strlen(mgfstr)+32);
298 +        if (dp->mgf == NULL)
299 +                error(SYSTEM, "out of memory in load_geometry");
300 +        if (cfact < 0.99 || cfact > 1.01)
301 +                sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
302 +        else
303 +                strcpy(dp->mgf, mgfstr);
304 + }
305 +
306 +
307 + static void
308   load_bsdf_data(         /* load BSDF distribution for this wavelength */
309          struct BSDF_data *dp,
310          ezxml_t wdb
# Line 184 | Line 315 | load_bsdf_data(                /* load BSDF distribution for this wa
315          char  *sdata;
316          int  i;
317          
318 <        if ((cbasis == NULL) | (rbasis == NULL)) {
318 >        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
319                  error(WARNING, "missing column/row basis for BSDF");
320                  return;
321          }
191        /* XXX need to add routines for loading in foreign bases */
322          for (i = nabases; i--; )
323 <                if (!strcmp(cbasis, abase_list[i].name)) {
323 >                if (!strcasecmp(cbasis, abase_list[i].name)) {
324                          dp->ninc = abase_list[i].nangles;
325                          dp->ib_priv = (void *)&abase_list[i];
326                          dp->ib_vec = ab_getvecR;
# Line 199 | Line 329 | load_bsdf_data(                /* load BSDF distribution for this wa
329                          break;
330                  }
331          if (i < 0) {
332 <                sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
332 >                sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
333                  error(WARNING, errmsg);
334                  return;
335          }
336          for (i = nabases; i--; )
337 <                if (!strcmp(rbasis, abase_list[i].name)) {
337 >                if (!strcasecmp(rbasis, abase_list[i].name)) {
338                          dp->nout = abase_list[i].nangles;
339                          dp->ob_priv = (void *)&abase_list[i];
340                          dp->ob_vec = ab_getvec;
# Line 213 | Line 343 | load_bsdf_data(                /* load BSDF distribution for this wa
343                          break;
344                  }
345          if (i < 0) {
346 <                sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
346 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
347                  error(WARNING, errmsg);
348                  return;
349          }
350                                  /* read BSDF data */
351          sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
352 <        if (sdata == NULL) {
352 >        if (!sdata || !*sdata) {
353                  error(WARNING, "missing BSDF ScatteringData");
354                  return;
355          }
# Line 243 | Line 373 | load_bsdf_data(                /* load BSDF distribution for this wa
373                  sdata++;
374          if (*sdata) {
375                  sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
376 <                                strlen(sdata));
376 >                                (int)strlen(sdata));
377                  error(WARNING, errmsg);
378          }
379   }
# Line 254 | Line 384 | check_bsdf_data(       /* check that BSDF data is sane */
384          struct BSDF_data *dp
385   )
386   {
387 <        double *        omega_arr;
388 <        double          dom, hemi_total;
387 >        double          *omega_iarr, *omega_oarr;
388 >        double          dom, contrib, hemi_total, full_total;
389          int             nneg;
390 +        FVECT           v;
391          int             i, o;
392  
393          if (dp == NULL || dp->bsdf == NULL)
394                  return(0);
395 <        omega_arr = (double *)calloc(dp->nout, sizeof(double));
396 <        if (omega_arr == NULL)
395 >        omega_iarr = (double *)calloc(dp->ninc, sizeof(double));
396 >        omega_oarr = (double *)calloc(dp->nout, sizeof(double));
397 >        if ((omega_iarr == NULL) | (omega_oarr == NULL))
398                  error(SYSTEM, "out of memory in check_bsdf_data");
399 +                                        /* incoming projected solid angles */
400          hemi_total = .0;
401 +        for (i = dp->ninc; i--; ) {
402 +                dom = getBSDF_incohm(dp,i);
403 +                if (dom <= .0) {
404 +                        error(WARNING, "zero/negative incoming solid angle");
405 +                        continue;
406 +                }
407 +                if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) {
408 +                        error(WARNING, "illegal incoming BSDF direction");
409 +                        free(omega_iarr); free(omega_oarr);
410 +                        return(0);
411 +                }
412 +                hemi_total += omega_iarr[i] = dom * -v[2];
413 +        }
414 +        if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
415 +                sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%",
416 +                                100.*(hemi_total/PI - 1.));
417 +                error(WARNING, errmsg);
418 +        }
419 +        dom = PI / hemi_total;          /* fix normalization */
420 +        for (i = dp->ninc; i--; )
421 +                omega_iarr[i] *= dom;
422 +                                        /* outgoing projected solid angles */
423 +        hemi_total = .0;
424          for (o = dp->nout; o--; ) {
269                FVECT   v;
425                  dom = getBSDF_outohm(dp,o);
426                  if (dom <= .0) {
427 <                        error(WARNING, "zero/negative solid angle");
427 >                        error(WARNING, "zero/negative outgoing solid angle");
428                          continue;
429                  }
430                  if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) {
431                          error(WARNING, "illegal outgoing BSDF direction");
432 <                        free(omega_arr);
432 >                        free(omega_iarr); free(omega_oarr);
433                          return(0);
434                  }
435 <                hemi_total += omega_arr[o] = dom*v[2];
435 >                hemi_total += omega_oarr[o] = dom * v[2];
436          }
437          if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
438                  sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%",
439                                  100.*(hemi_total/PI - 1.));
440                  error(WARNING, errmsg);
441          }
442 <        dom = PI / hemi_total;          /* normalize solid angles */
442 >        dom = PI / hemi_total;          /* fix normalization */
443          for (o = dp->nout; o--; )
444 <                omega_arr[o] *= dom;
445 <        nneg = 0;
446 <        for (i = dp->ninc; i--; ) {
444 >                omega_oarr[o] *= dom;
445 >        nneg = 0;                       /* check outgoing totals */
446 >        for (i = 0; i < dp->ninc; i++) {
447                  hemi_total = .0;
448                  for (o = dp->nout; o--; ) {
449                          double  f = BSDF_value(dp,i,o);
450 <                        if (f > .0)
451 <                                hemi_total += f*omega_arr[o];
452 <                        else if (f < -FTINY)
453 <                                ++nneg;
450 >                        if (f >= .0)
451 >                                hemi_total += f*omega_oarr[o];
452 >                        else {
453 >                                nneg += (f < -FTINY);
454 >                                BSDF_value(dp,i,o) = .0f;
455 >                        }
456                  }
457 <                if (hemi_total > 1.02) {
458 <                        sprintf(errmsg, "BSDF direction passes %.1f%% of light",
459 <                                        100.*hemi_total);
457 >                if (hemi_total > 1.01) {
458 >                        sprintf(errmsg,
459 >                        "incoming BSDF direction %d passes %.1f%% of light",
460 >                                        i, 100.*hemi_total);
461                          error(WARNING, errmsg);
462                  }
463          }
464 <        free(omega_arr);
465 <        if (nneg > 0) {
308 <                sprintf(errmsg, "%d negative BSDF values", nneg);
464 >        if (nneg) {
465 >                sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
466                  error(WARNING, errmsg);
310                return(0);
467          }
468 +        full_total = .0;                /* reverse roles and check again */
469 +        for (o = 0; o < dp->nout; o++) {
470 +                hemi_total = .0;
471 +                for (i = dp->ninc; i--; )
472 +                        hemi_total += BSDF_value(dp,i,o) * omega_iarr[i];
473 +
474 +                if (hemi_total > 1.01) {
475 +                        sprintf(errmsg,
476 +                        "outgoing BSDF direction %d collects %.1f%% of light",
477 +                                        o, 100.*hemi_total);
478 +                        error(WARNING, errmsg);
479 +                }
480 +                full_total += hemi_total*omega_oarr[o];
481 +        }
482 +        full_total /= PI;
483 +        if (full_total > 1.00001) {
484 +                sprintf(errmsg, "BSDF transfers %.4f%% of light",
485 +                                100.*full_total);
486 +                error(WARNING, errmsg);
487 +        }
488 +        free(omega_iarr); free(omega_oarr);
489          return(1);
490   }
491  
492 +
493   struct BSDF_data *
494   load_BSDF(              /* load BSDF data from file */
495          char *fname
# Line 348 | Line 526 | load_BSDF(             /* load BSDF data from file */
526                  return(NULL);
527          }
528          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
529 +        if (strcasecmp(ezxml_txt(ezxml_child(ezxml_child(wtl,
530 +                        "DataDefinition"), "IncidentDataStructure")),
531 +                        "Columns")) {
532 +                sprintf(errmsg,
533 +                        "BSDF \"%s\": unsupported IncidentDataStructure",
534 +                                path);
535 +                error(WARNING, errmsg);
536 +                ezxml_free(fl);
537 +                return(NULL);
538 +        }              
539 +        load_angle_basis(ezxml_child(ezxml_child(wtl,
540 +                                "DataDefinition"), "AngleBasis"));
541          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
542 +        load_geometry(dp, ezxml_child(wtl, "Material"));
543          for (wld = ezxml_child(wtl, "WavelengthData");
544                                  wld != NULL; wld = wld->next) {
545 <                if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
545 >                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
546 >                                "Visible"))
547                          continue;
548 <                wdb = ezxml_child(wld, "WavelengthDataBlock");
549 <                if (wdb == NULL) continue;
550 <                if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")),
548 >                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
549 >                                wdb != NULL; wdb = wdb->next)
550 >                        if (!strcasecmp(ezxml_txt(ezxml_child(wdb,
551 >                                        "WavelengthDataDirection")),
552                                          "Transmission Front"))
553 <                        continue;
554 <                load_bsdf_data(dp, wdb);        /* load front BTDF */
555 <                break;                          /* ignore the rest */
553 >                                break;
554 >                if (wdb != NULL) {              /* load front BTDF */
555 >                        load_bsdf_data(dp, wdb);
556 >                        break;                  /* ignore the rest */
557 >                }
558          }
559          ezxml_free(fl);                         /* done with XML file */
560          if (!check_bsdf_data(dp)) {
# Line 379 | Line 574 | free_BSDF(             /* free BSDF data structure */
574   {
575          if (b == NULL)
576                  return;
577 +        if (b->mgf != NULL)
578 +                free(b->mgf);
579          if (b->bsdf != NULL)
580                  free(b->bsdf);
581          free(b);
# Line 435 | Line 632 | r_BSDF_outvec(         /* compute random output vector at giv
632   }
633  
634  
438 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
439
635   static int
636   addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */
637          char *xfarg[],
# Line 487 | Line 682 | int
682   getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */
683          MAT4 xm,
684          FVECT nrm,
685 <        UpDir ud
685 >        UpDir ud,
686 >        char *xfbuf
687   )
688   {
689          char    *xfargs[7];
690          XF      myxf;
691          FVECT   updir, xdest, ydest;
692 +        int     i;
693  
694          updir[0] = updir[1] = updir[2] = 0.;
695          switch (ud) {
# Line 523 | Line 720 | getBSDF_xfm(           /* compute BSDF orient. -> world orient.
720          fcross(ydest, nrm, xdest);
721          xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
722          copymat4(xm, myxf.xfm);
723 +        if (xfbuf == NULL)
724 +                return(1);
725 +                                /* return xf arguments as well */
726 +        for (i = 0; xfargs[i] != NULL; i++) {
727 +                *xfbuf++ = ' ';
728 +                strcpy(xfbuf, xfargs[i]);
729 +                while (*xfbuf) ++xfbuf;
730 +        }
731          return(1);
732   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines