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

# Line 23 | Line 23 | typedef struct {
23          }       lat[MAXLATS+1];         /* latitudes */
24   } ANGLE_BASIS;
25  
26 < #define MAXABASES       5               /* limit on defined bases */
26 > #define MAXABASES       7               /* limit on defined bases */
27  
28   static ANGLE_BASIS      abase_list[MAXABASES] = {
29          {
# Line 200 | Line 200 | ab_getndxR(            /* get index corresponding to the reverse
200  
201  
202   static void
203 < load_angle_basis(       /* load BSDF angle basis */
203 > load_angle_basis(       /* load custom BSDF angle basis */
204          ezxml_t wab
205   )
206   {
# Line 208 | Line 208 | load_angle_basis(      /* load BSDF angle basis */
208          ezxml_t wbb;
209          int     i;
210          
211 <        if (abname == NULL || !*abname)
211 >        if (!abname || !*abname)
212                  return;
213          for (i = nabases; i--; )
214                  if (!strcmp(abname, abase_list[i].name))
215 <                        return;         /* XXX assume it's the same */
215 >                        return;         /* assume it's the same */
216          if (nabases >= MAXABASES)
217                  error(INTERNAL, "too many angle bases");
218          strcpy(abase_list[nabases].name, abname);
# Line 239 | Line 239 | load_angle_basis(      /* load 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 250 | Line 306 | load_bsdf_data(                /* load BSDF distribution for this wa
306          char  *sdata;
307          int  i;
308          
309 <        if ((cbasis == NULL || !*cbasis) | (rbasis == NULL || !*rbasis)) {
309 >        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
310                  error(WARNING, "missing column/row basis for BSDF");
311                  return;
312          }
257        /* XXX need to add routines for loading in foreign bases */
313          for (i = nabases; i--; )
314                  if (!strcmp(cbasis, abase_list[i].name)) {
315                          dp->ninc = abase_list[i].nangles;
# Line 265 | Line 320 | load_bsdf_data(                /* load BSDF distribution for this wa
320                          break;
321                  }
322          if (i < 0) {
323 <                sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
323 >                sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
324                  error(WARNING, errmsg);
325                  return;
326          }
# Line 279 | Line 334 | load_bsdf_data(                /* load BSDF distribution for this wa
334                          break;
335                  }
336          if (i < 0) {
337 <                sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
337 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
338                  error(WARNING, errmsg);
339                  return;
340          }
341                                  /* read BSDF data */
342          sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
343 <        if (sdata == NULL || !*sdata) {
343 >        if (!sdata || !*sdata) {
344                  error(WARNING, "missing BSDF ScatteringData");
345                  return;
346          }
# Line 309 | 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 321 | 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 401 | 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 413 | 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 457 | 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 487 | 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 593 | 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 629 | 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