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.4 by greg, Sat Jul 3 05:28:05 2010 UTC vs.
Revision 2.7 by greg, Tue Sep 7 23:10:50 2010 UTC

# Line 239 | Line 239 | load_angle_basis(      /* load custom BSDF angle basis */
239   }
240  
241  
242 + static double
243 + to_meters(              /* return factor to convert given unit to meters */
244 +        const char *unit
245 + )
246 + {
247 +        if (unit == NULL) return(1.);           /* safe assumption? */
248 +        if (!strcasecmp(unit, "Meter")) return(1.);
249 +        if (!strcasecmp(unit, "Foot")) return(.3048);
250 +        if (!strcasecmp(unit, "Inch")) return(.0254);
251 +        if (!strcasecmp(unit, "Centimeter")) return(.01);
252 +        sprintf(errmsg, "unknown dimensional unit '%s'", unit);
253 +        error(USER, errmsg);
254 + }
255 +
256 +
257   static void
258 + load_geometry(          /* load geometric dimensions and description (if any) */
259 +        struct BSDF_data *dp,
260 +        ezxml_t wdb
261 + )
262 + {
263 +        ezxml_t         geom;
264 +        double          cfact;
265 +        const char      *fmt, *mgfstr;
266 +
267 +        dp->dim[0] = dp->dim[1] = dp->dim[2] = 0;
268 +        dp->mgf = NULL;
269 +        if ((geom = ezxml_child(wdb, "Width")) != NULL)
270 +                dp->dim[0] = atof(ezxml_txt(geom)) *
271 +                                to_meters(ezxml_attr(geom, "unit"));
272 +        if ((geom = ezxml_child(wdb, "Height")) != NULL)
273 +                dp->dim[1] = atof(ezxml_txt(geom)) *
274 +                                to_meters(ezxml_attr(geom, "unit"));
275 +        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
276 +                dp->dim[2] = atof(ezxml_txt(geom)) *
277 +                                to_meters(ezxml_attr(geom, "unit"));
278 +        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
279 +                        (mgfstr = ezxml_txt(geom)) == NULL)
280 +                return;
281 +        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
282 +                        strcasecmp(fmt, "MGF")) {
283 +                sprintf(errmsg, "unrecognized geometry format '%s'", fmt);
284 +                error(WARNING, errmsg);
285 +                return;
286 +        }
287 +        cfact = to_meters(ezxml_attr(geom, "unit"));
288 +        dp->mgf = (char *)malloc(strlen(mgfstr)+32);
289 +        if (dp->mgf == NULL)
290 +                error(SYSTEM, "out of memory in load_geometry");
291 +        if (cfact < 0.99 || cfact > 1.01)
292 +                sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
293 +        else
294 +                strcpy(dp->mgf, mgfstr);
295 + }
296 +
297 +
298 + static void
299   load_bsdf_data(         /* load BSDF distribution for this wavelength */
300          struct BSDF_data *dp,
301          ezxml_t wdb
# Line 308 | Line 364 | load_bsdf_data(                /* load BSDF distribution for this wa
364                  sdata++;
365          if (*sdata) {
366                  sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
367 <                                strlen(sdata));
367 >                                (int)strlen(sdata));
368                  error(WARNING, errmsg);
369          }
370   }
# Line 320 | Line 376 | check_bsdf_data(       /* check that BSDF data is sane */
376   )
377   {
378          double          *omega_iarr, *omega_oarr;
379 <        double          dom, contrib, hemi_total;
379 >        double          dom, contrib, hemi_total, full_total;
380          int             nneg;
381          FVECT           v;
382          int             i, o;
# Line 400 | Line 456 | check_bsdf_data(       /* check that BSDF data is sane */
456                  sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
457                  error(WARNING, errmsg);
458          }
459 <                                        /* reverse roles and check again */
459 >        full_total = .0;                /* reverse roles and check again */
460          for (o = 0; o < dp->nout; o++) {
461                  hemi_total = .0;
462                  for (i = dp->ninc; i--; )
# Line 412 | Line 468 | check_bsdf_data(       /* check that BSDF data is sane */
468                                          o, 100.*hemi_total);
469                          error(WARNING, errmsg);
470                  }
471 +                full_total += hemi_total*omega_oarr[o];
472          }
473 +        full_total /= PI;
474 +        if (full_total > 1.02) {
475 +                sprintf(errmsg, "BSDF transfers %.1f%% of light",
476 +                                100.*full_total);
477 +                error(WARNING, errmsg);
478 +        }
479          free(omega_iarr); free(omega_oarr);
480          return(1);
481   }
482  
483 +
484   struct BSDF_data *
485   load_BSDF(              /* load BSDF data from file */
486          char *fname
# Line 456 | Line 520 | load_BSDF(             /* load BSDF data from file */
520          load_angle_basis(ezxml_child(ezxml_child(wtl,
521                                  "DataDefinition"), "AngleBasis"));
522          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
523 +        load_geometry(dp, ezxml_child(wtl, "Material"));
524          for (wld = ezxml_child(wtl, "WavelengthData");
525                                  wld != NULL; wld = wld->next) {
526                  if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
# Line 486 | Line 551 | free_BSDF(             /* free BSDF data structure */
551   {
552          if (b == NULL)
553                  return;
554 +        if (b->mgf != NULL)
555 +                free(b->mgf);
556          if (b->bsdf != NULL)
557                  free(b->bsdf);
558          free(b);
# Line 592 | Line 659 | int
659   getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */
660          MAT4 xm,
661          FVECT nrm,
662 <        UpDir ud
662 >        UpDir ud,
663 >        char *xfbuf
664   )
665   {
666          char    *xfargs[7];
667          XF      myxf;
668          FVECT   updir, xdest, ydest;
669 +        int     i;
670  
671          updir[0] = updir[1] = updir[2] = 0.;
672          switch (ud) {
# Line 628 | Line 697 | getBSDF_xfm(           /* compute BSDF orient. -> world orient.
697          fcross(ydest, nrm, xdest);
698          xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
699          copymat4(xm, myxf.xfm);
700 +        if (xfbuf == NULL)
701 +                return(1);
702 +                                /* return xf arguments as well */
703 +        for (i = 0; xfargs[i] != NULL; i++) {
704 +                *xfbuf++ = ' ';
705 +                strcpy(xfbuf, xfargs[i]);
706 +                while (*xfbuf) ++xfbuf;
707 +        }
708          return(1);
709   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines