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.8 by greg, Sun Sep 26 15:43:26 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 +        if (!strcasecmp(unit, "Millimeter")) return(.001);
253 +        sprintf(errmsg, "unknown dimensional unit '%s'", unit);
254 +        error(USER, errmsg);
255 + }
256 +
257 +
258   static void
259 + load_geometry(          /* load geometric dimensions and description (if any) */
260 +        struct BSDF_data *dp,
261 +        ezxml_t wdb
262 + )
263 + {
264 +        ezxml_t         geom;
265 +        double          cfact;
266 +        const char      *fmt, *mgfstr;
267 +
268 +        dp->dim[0] = dp->dim[1] = dp->dim[2] = 0;
269 +        dp->mgf = NULL;
270 +        if ((geom = ezxml_child(wdb, "Width")) != NULL)
271 +                dp->dim[0] = atof(ezxml_txt(geom)) *
272 +                                to_meters(ezxml_attr(geom, "unit"));
273 +        if ((geom = ezxml_child(wdb, "Height")) != NULL)
274 +                dp->dim[1] = atof(ezxml_txt(geom)) *
275 +                                to_meters(ezxml_attr(geom, "unit"));
276 +        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
277 +                dp->dim[2] = atof(ezxml_txt(geom)) *
278 +                                to_meters(ezxml_attr(geom, "unit"));
279 +        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
280 +                        (mgfstr = ezxml_txt(geom)) == NULL)
281 +                return;
282 +        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
283 +                        strcasecmp(fmt, "MGF")) {
284 +                sprintf(errmsg, "unrecognized geometry format '%s'", fmt);
285 +                error(WARNING, errmsg);
286 +                return;
287 +        }
288 +        cfact = to_meters(ezxml_attr(geom, "unit"));
289 +        dp->mgf = (char *)malloc(strlen(mgfstr)+32);
290 +        if (dp->mgf == NULL)
291 +                error(SYSTEM, "out of memory in load_geometry");
292 +        if (cfact < 0.99 || cfact > 1.01)
293 +                sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
294 +        else
295 +                strcpy(dp->mgf, mgfstr);
296 + }
297 +
298 +
299 + static void
300   load_bsdf_data(         /* load BSDF distribution for this wavelength */
301          struct BSDF_data *dp,
302          ezxml_t wdb
# Line 308 | Line 365 | load_bsdf_data(                /* load BSDF distribution for this wa
365                  sdata++;
366          if (*sdata) {
367                  sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
368 <                                strlen(sdata));
368 >                                (int)strlen(sdata));
369                  error(WARNING, errmsg);
370          }
371   }
# Line 320 | Line 377 | check_bsdf_data(       /* check that BSDF data is sane */
377   )
378   {
379          double          *omega_iarr, *omega_oarr;
380 <        double          dom, contrib, hemi_total;
380 >        double          dom, contrib, hemi_total, full_total;
381          int             nneg;
382          FVECT           v;
383          int             i, o;
# Line 389 | Line 446 | check_bsdf_data(       /* check that BSDF data is sane */
446                                  BSDF_value(dp,i,o) = .0f;
447                          }
448                  }
449 <                if (hemi_total > 1.02) {
449 >                if (hemi_total > 1.01) {
450                          sprintf(errmsg,
451                          "incoming BSDF direction %d passes %.1f%% of light",
452                                          i, 100.*hemi_total);
# Line 400 | Line 457 | check_bsdf_data(       /* check that BSDF data is sane */
457                  sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
458                  error(WARNING, errmsg);
459          }
460 <                                        /* reverse roles and check again */
460 >        full_total = .0;                /* reverse roles and check again */
461          for (o = 0; o < dp->nout; o++) {
462                  hemi_total = .0;
463                  for (i = dp->ninc; i--; )
464                          hemi_total += BSDF_value(dp,i,o) * omega_iarr[i];
465  
466 <                if (hemi_total > 1.02) {
466 >                if (hemi_total > 1.01) {
467                          sprintf(errmsg,
468                          "outgoing BSDF direction %d collects %.1f%% of light",
469                                          o, 100.*hemi_total);
470                          error(WARNING, errmsg);
471                  }
472 +                full_total += hemi_total*omega_oarr[o];
473          }
474 +        full_total /= PI;
475 +        if (full_total > 1.00001) {
476 +                sprintf(errmsg, "BSDF transfers %.4f%% of light",
477 +                                100.*full_total);
478 +                error(WARNING, errmsg);
479 +        }
480          free(omega_iarr); free(omega_oarr);
481          return(1);
482   }
483  
484 +
485   struct BSDF_data *
486   load_BSDF(              /* load BSDF data from file */
487          char *fname
# Line 456 | Line 521 | load_BSDF(             /* load BSDF data from file */
521          load_angle_basis(ezxml_child(ezxml_child(wtl,
522                                  "DataDefinition"), "AngleBasis"));
523          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
524 +        load_geometry(dp, ezxml_child(wtl, "Material"));
525          for (wld = ezxml_child(wtl, "WavelengthData");
526                                  wld != NULL; wld = wld->next) {
527                  if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
# Line 486 | Line 552 | free_BSDF(             /* free BSDF data structure */
552   {
553          if (b == NULL)
554                  return;
555 +        if (b->mgf != NULL)
556 +                free(b->mgf);
557          if (b->bsdf != NULL)
558                  free(b->bsdf);
559          free(b);
# Line 592 | Line 660 | int
660   getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */
661          MAT4 xm,
662          FVECT nrm,
663 <        UpDir ud
663 >        UpDir ud,
664 >        char *xfbuf
665   )
666   {
667          char    *xfargs[7];
668          XF      myxf;
669          FVECT   updir, xdest, ydest;
670 +        int     i;
671  
672          updir[0] = updir[1] = updir[2] = 0.;
673          switch (ud) {
# Line 628 | Line 698 | getBSDF_xfm(           /* compute BSDF orient. -> world orient.
698          fcross(ydest, nrm, xdest);
699          xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
700          copymat4(xm, myxf.xfm);
701 +        if (xfbuf == NULL)
702 +                return(1);
703 +                                /* return xf arguments as well */
704 +        for (i = 0; xfargs[i] != NULL; i++) {
705 +                *xfbuf++ = ' ';
706 +                strcpy(xfbuf, xfargs[i]);
707 +                while (*xfbuf) ++xfbuf;
708 +        }
709          return(1);
710   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines