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.6 by greg, Fri Sep 3 23:53: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 352 | Line 473 | check_bsdf_data(       /* check that BSDF data is sane */
473          return(1);
474   }
475  
476 +
477   struct BSDF_data *
478   load_BSDF(              /* load BSDF data from file */
479          char *fname
# Line 388 | Line 510 | load_BSDF(             /* load BSDF data from file */
510                  return(NULL);
511          }
512          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
513 +        load_angle_basis(ezxml_child(ezxml_child(wtl,
514 +                                "DataDefinition"), "AngleBasis"));
515          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
516 +        load_geometry(dp, ezxml_child(wtl, "Material"));
517          for (wld = ezxml_child(wtl, "WavelengthData");
518                                  wld != NULL; wld = wld->next) {
519                  if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
# Line 419 | Line 544 | free_BSDF(             /* free BSDF data structure */
544   {
545          if (b == NULL)
546                  return;
547 +        if (b->mgf != NULL)
548 +                free(b->mgf);
549          if (b->bsdf != NULL)
550                  free(b->bsdf);
551          free(b);
# Line 475 | Line 602 | r_BSDF_outvec(         /* compute random output vector at giv
602   }
603  
604  
478 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
479
605   static int
606   addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */
607          char *xfarg[],
# Line 527 | Line 652 | int
652   getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */
653          MAT4 xm,
654          FVECT nrm,
655 <        UpDir ud
655 >        UpDir ud,
656 >        char *xfbuf
657   )
658   {
659          char    *xfargs[7];
660          XF      myxf;
661          FVECT   updir, xdest, ydest;
662 +        int     i;
663  
664          updir[0] = updir[1] = updir[2] = 0.;
665          switch (ud) {
# Line 563 | Line 690 | getBSDF_xfm(           /* compute BSDF orient. -> world orient.
690          fcross(ydest, nrm, xdest);
691          xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
692          copymat4(xm, myxf.xfm);
693 +        if (xfbuf == NULL)
694 +                return(1);
695 +                                /* return xf arguments as well */
696 +        for (i = 0; xfargs[i] != NULL; i++) {
697 +                *xfbuf++ = ' ';
698 +                strcpy(xfbuf, xfargs[i]);
699 +                while (*xfbuf) ++xfbuf;
700 +        }
701          return(1);
702   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines