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.1 by greg, Wed Jun 17 20:41:47 2009 UTC vs.
Revision 2.8 by greg, Sun Sep 26 15:43:26 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 71 | Line 97 | ab_getvec(             /* get vector for this angle basis index *
97   {
98          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
99          int     li;
100 <        double  alt, azi, d;
100 >        double  pol, azi, d;
101          
102          if ((ndx < 0) | (ndx >= ab->nangles))
103                  return(0);
104          for (li = 0; ndx >= ab->lat[li].nphis; li++)
105                  ndx -= ab->lat[li].nphis;
106 <        alt = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
106 >        pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
107          azi = 2.*PI*ndx/ab->lat[li].nphis;
108 <        v[2] = d = cos(alt);
109 <        d = sqrt(1. - d*d);     /* sin(alt) */
108 >        v[2] = d = cos(pol);
109 >        d = sqrt(1. - d*d);     /* sin(pol) */
110          v[0] = cos(azi)*d;
111          v[1] = sin(azi)*d;
112          return(1);
# Line 95 | Line 121 | ab_getndx(             /* get index corresponding to the given ve
121   {
122          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
123          int     li, ndx;
124 <        double  alt, azi, d;
124 >        double  pol, azi, d;
125  
126          if ((v[2] < -1.0) | (v[2] > 1.0))
127                  return(-1);
128 <        alt = 180.0/PI*acos(v[2]);
128 >        pol = 180.0/PI*acos(v[2]);
129          azi = 180.0/PI*atan2(v[1], v[0]);
130          if (azi < 0.0) azi += 360.0;
131 <        for (li = 1; ab->lat[li].tmin <= alt; li++)
131 >        for (li = 1; ab->lat[li].tmin <= pol; li++)
132                  if (!ab->lat[li].nphis)
133                          return(-1);
134          --li;
# 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 +        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 184 | Line 307 | load_bsdf_data(                /* load BSDF distribution for this wa
307          char  *sdata;
308          int  i;
309          
310 <        if ((cbasis == NULL) | (rbasis == NULL)) {
310 >        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
311                  error(WARNING, "missing column/row basis for BSDF");
312                  return;
313          }
191        /* XXX need to add routines for loading in foreign bases */
314          for (i = nabases; i--; )
315                  if (!strcmp(cbasis, abase_list[i].name)) {
316                          dp->ninc = abase_list[i].nangles;
# Line 199 | Line 321 | load_bsdf_data(                /* load BSDF distribution for this wa
321                          break;
322                  }
323          if (i < 0) {
324 <                sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
324 >                sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
325                  error(WARNING, errmsg);
326                  return;
327          }
# Line 213 | Line 335 | load_bsdf_data(                /* load BSDF distribution for this wa
335                          break;
336                  }
337          if (i < 0) {
338 <                sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
338 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
339                  error(WARNING, errmsg);
340                  return;
341          }
342                                  /* read BSDF data */
343          sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
344 <        if (sdata == NULL) {
344 >        if (!sdata || !*sdata) {
345                  error(WARNING, "missing BSDF ScatteringData");
346                  return;
347          }
# Line 243 | 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 254 | Line 376 | check_bsdf_data(       /* check that BSDF data is sane */
376          struct BSDF_data *dp
377   )
378   {
379 <        double *        omega_arr;
380 <        double          dom, hemi_total;
379 >        double          *omega_iarr, *omega_oarr;
380 >        double          dom, contrib, hemi_total, full_total;
381          int             nneg;
382 +        FVECT           v;
383          int             i, o;
384  
385          if (dp == NULL || dp->bsdf == NULL)
386                  return(0);
387 <        omega_arr = (double *)calloc(dp->nout, sizeof(double));
388 <        if (omega_arr == NULL)
387 >        omega_iarr = (double *)calloc(dp->ninc, sizeof(double));
388 >        omega_oarr = (double *)calloc(dp->nout, sizeof(double));
389 >        if ((omega_iarr == NULL) | (omega_oarr == NULL))
390                  error(SYSTEM, "out of memory in check_bsdf_data");
391 +                                        /* incoming projected solid angles */
392          hemi_total = .0;
393 +        for (i = dp->ninc; i--; ) {
394 +                dom = getBSDF_incohm(dp,i);
395 +                if (dom <= .0) {
396 +                        error(WARNING, "zero/negative incoming solid angle");
397 +                        continue;
398 +                }
399 +                if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) {
400 +                        error(WARNING, "illegal incoming BSDF direction");
401 +                        free(omega_iarr); free(omega_oarr);
402 +                        return(0);
403 +                }
404 +                hemi_total += omega_iarr[i] = dom * -v[2];
405 +        }
406 +        if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
407 +                sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%",
408 +                                100.*(hemi_total/PI - 1.));
409 +                error(WARNING, errmsg);
410 +        }
411 +        dom = PI / hemi_total;          /* fix normalization */
412 +        for (i = dp->ninc; i--; )
413 +                omega_iarr[i] *= dom;
414 +                                        /* outgoing projected solid angles */
415 +        hemi_total = .0;
416          for (o = dp->nout; o--; ) {
269                FVECT   v;
417                  dom = getBSDF_outohm(dp,o);
418                  if (dom <= .0) {
419 <                        error(WARNING, "zero/negative solid angle");
419 >                        error(WARNING, "zero/negative outgoing solid angle");
420                          continue;
421                  }
422                  if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) {
423                          error(WARNING, "illegal outgoing BSDF direction");
424 <                        free(omega_arr);
424 >                        free(omega_iarr); free(omega_oarr);
425                          return(0);
426                  }
427 <                hemi_total += omega_arr[o] = dom*v[2];
427 >                hemi_total += omega_oarr[o] = dom * v[2];
428          }
429          if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
430                  sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%",
431                                  100.*(hemi_total/PI - 1.));
432                  error(WARNING, errmsg);
433          }
434 <        dom = PI / hemi_total;          /* normalize solid angles */
434 >        dom = PI / hemi_total;          /* fix normalization */
435          for (o = dp->nout; o--; )
436 <                omega_arr[o] *= dom;
437 <        nneg = 0;
438 <        for (i = dp->ninc; i--; ) {
436 >                omega_oarr[o] *= dom;
437 >        nneg = 0;                       /* check outgoing totals */
438 >        for (i = 0; i < dp->ninc; i++) {
439                  hemi_total = .0;
440                  for (o = dp->nout; o--; ) {
441                          double  f = BSDF_value(dp,i,o);
442 <                        if (f > .0)
443 <                                hemi_total += f*omega_arr[o];
444 <                        else if (f < -FTINY)
445 <                                ++nneg;
442 >                        if (f >= .0)
443 >                                hemi_total += f*omega_oarr[o];
444 >                        else {
445 >                                nneg += (f < -FTINY);
446 >                                BSDF_value(dp,i,o) = .0f;
447 >                        }
448                  }
449 <                if (hemi_total > 1.02) {
450 <                        sprintf(errmsg, "BSDF direction passes %.1f%% of light",
451 <                                        100.*hemi_total);
449 >                if (hemi_total > 1.01) {
450 >                        sprintf(errmsg,
451 >                        "incoming BSDF direction %d passes %.1f%% of light",
452 >                                        i, 100.*hemi_total);
453                          error(WARNING, errmsg);
454                  }
455          }
456 <        free(omega_arr);
457 <        if (nneg > 0) {
308 <                sprintf(errmsg, "%d negative BSDF values", nneg);
456 >        if (nneg) {
457 >                sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
458                  error(WARNING, errmsg);
310                return(0);
459          }
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.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 348 | Line 518 | load_BSDF(             /* load BSDF data from file */
518                  return(NULL);
519          }
520          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
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 379 | 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 435 | Line 610 | r_BSDF_outvec(         /* compute random output vector at giv
610   }
611  
612  
438 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
439
613   static int
614   addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */
615          char *xfarg[],
# Line 487 | 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 523 | 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