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.2 by greg, Fri Jun 19 06:49:25 2009 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       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-7 && (b)-(a) <= 1e-7)
65  
66 + // returns the name of the given tag
67 + #ifdef ezxml_name
68 + #undef ezxml_name
69 + static char *
70 + ezxml_name(ezxml_t xml)
71 + {
72 +        if (xml == NULL)
73 +                return(NULL);
74 +        return(xml->name);
75 + }
76 + #endif
77 +
78 + // returns the given tag's character content or empty string if none
79 + #ifdef ezxml_txt
80 + #undef ezxml_txt
81 + static char *
82 + ezxml_txt(ezxml_t xml)
83 + {
84 +        if (xml == NULL)
85 +                return("");
86 +        return(xml->txt);
87 + }
88 + #endif
89 +
90 +
91   static int
92   ab_getvec(              /* get vector for this angle basis index */
93          FVECT v,
# Line 174 | Line 200 | ab_getndxR(            /* get index corresponding to the reverse
200  
201  
202   static void
203 + load_angle_basis(       /* load custom BSDF angle basis */
204 +        ezxml_t wab
205 + )
206 + {
207 +        char    *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName"));
208 +        ezxml_t wbb;
209 +        int     i;
210 +        
211 +        if (!abname || !*abname)
212 +                return;
213 +        for (i = nabases; i--; )
214 +                if (!strcmp(abname, abase_list[i].name))
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);
219 +        abase_list[nabases].nangles = 0;
220 +        for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock");
221 +                        wbb != NULL; i++, wbb = wbb->next) {
222 +                if (i >= MAXLATS)
223 +                        error(INTERNAL, "too many latitudes in custom basis");
224 +                abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt(
225 +                                ezxml_child(ezxml_child(wbb,
226 +                                        "ThetaBounds"), "UpperTheta")));
227 +                if (!i)
228 +                        abase_list[nabases].lat[i].tmin =
229 +                                        -abase_list[nabases].lat[i+1].tmin;
230 +                else if (!FEQ(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
231 +                                        "ThetaBounds"), "LowerTheta"))),
232 +                                abase_list[nabases].lat[i].tmin))
233 +                        error(WARNING, "theta values disagree in custom basis");
234 +                abase_list[nabases].nangles +=
235 +                        abase_list[nabases].lat[i].nphis =
236 +                                atoi(ezxml_txt(ezxml_child(wbb, "nPhis")));
237 +        }
238 +        abase_list[nabases++].lat[i].nphis = 0;
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 184 | Line 306 | load_bsdf_data(                /* load BSDF distribution for this wa
306          char  *sdata;
307          int  i;
308          
309 <        if ((cbasis == NULL) | (rbasis == NULL)) {
309 >        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
310                  error(WARNING, "missing column/row basis for BSDF");
311                  return;
312          }
191        /* 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 199 | 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 213 | 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) {
343 >        if (!sdata || !*sdata) {
344                  error(WARNING, "missing BSDF ScatteringData");
345                  return;
346          }
# Line 243 | 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 255 | 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 335 | 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 347 | 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 388 | Line 517 | load_BSDF(             /* load BSDF data from file */
517                  return(NULL);
518          }
519          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
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 419 | 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 475 | Line 609 | r_BSDF_outvec(         /* compute random output vector at giv
609   }
610  
611  
478 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
479
612   static int
613   addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */
614          char *xfarg[],
# Line 527 | 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 563 | 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