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.5 by greg, Sun Aug 1 22:26:37 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 void
243   load_bsdf_data(         /* load BSDF distribution for this wavelength */
244          struct BSDF_data *dp,
245          ezxml_t wdb
# Line 184 | Line 250 | load_bsdf_data(                /* load BSDF distribution for this wa
250          char  *sdata;
251          int  i;
252          
253 <        if ((cbasis == NULL) | (rbasis == NULL)) {
253 >        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
254                  error(WARNING, "missing column/row basis for BSDF");
255                  return;
256          }
191        /* XXX need to add routines for loading in foreign bases */
257          for (i = nabases; i--; )
258                  if (!strcmp(cbasis, abase_list[i].name)) {
259                          dp->ninc = abase_list[i].nangles;
# Line 199 | Line 264 | load_bsdf_data(                /* load BSDF distribution for this wa
264                          break;
265                  }
266          if (i < 0) {
267 <                sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
267 >                sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
268                  error(WARNING, errmsg);
269                  return;
270          }
# Line 213 | Line 278 | load_bsdf_data(                /* load BSDF distribution for this wa
278                          break;
279                  }
280          if (i < 0) {
281 <                sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
281 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
282                  error(WARNING, errmsg);
283                  return;
284          }
285                                  /* read BSDF data */
286          sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
287 <        if (sdata == NULL) {
287 >        if (!sdata || !*sdata) {
288                  error(WARNING, "missing BSDF ScatteringData");
289                  return;
290          }
# Line 243 | Line 308 | load_bsdf_data(                /* load BSDF distribution for this wa
308                  sdata++;
309          if (*sdata) {
310                  sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
311 <                                strlen(sdata));
311 >                                (int)strlen(sdata));
312                  error(WARNING, errmsg);
313          }
314   }
# Line 254 | Line 319 | check_bsdf_data(       /* check that BSDF data is sane */
319          struct BSDF_data *dp
320   )
321   {
322 <        double *        omega_arr;
323 <        double          dom, hemi_total;
322 >        double          *omega_iarr, *omega_oarr;
323 >        double          dom, contrib, hemi_total;
324          int             nneg;
325 +        FVECT           v;
326          int             i, o;
327  
328          if (dp == NULL || dp->bsdf == NULL)
329                  return(0);
330 <        omega_arr = (double *)calloc(dp->nout, sizeof(double));
331 <        if (omega_arr == NULL)
330 >        omega_iarr = (double *)calloc(dp->ninc, sizeof(double));
331 >        omega_oarr = (double *)calloc(dp->nout, sizeof(double));
332 >        if ((omega_iarr == NULL) | (omega_oarr == NULL))
333                  error(SYSTEM, "out of memory in check_bsdf_data");
334 +                                        /* incoming projected solid angles */
335          hemi_total = .0;
336 +        for (i = dp->ninc; i--; ) {
337 +                dom = getBSDF_incohm(dp,i);
338 +                if (dom <= .0) {
339 +                        error(WARNING, "zero/negative incoming solid angle");
340 +                        continue;
341 +                }
342 +                if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) {
343 +                        error(WARNING, "illegal incoming BSDF direction");
344 +                        free(omega_iarr); free(omega_oarr);
345 +                        return(0);
346 +                }
347 +                hemi_total += omega_iarr[i] = dom * -v[2];
348 +        }
349 +        if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
350 +                sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%",
351 +                                100.*(hemi_total/PI - 1.));
352 +                error(WARNING, errmsg);
353 +        }
354 +        dom = PI / hemi_total;          /* fix normalization */
355 +        for (i = dp->ninc; i--; )
356 +                omega_iarr[i] *= dom;
357 +                                        /* outgoing projected solid angles */
358 +        hemi_total = .0;
359          for (o = dp->nout; o--; ) {
269                FVECT   v;
360                  dom = getBSDF_outohm(dp,o);
361                  if (dom <= .0) {
362 <                        error(WARNING, "zero/negative solid angle");
362 >                        error(WARNING, "zero/negative outgoing solid angle");
363                          continue;
364                  }
365                  if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) {
366                          error(WARNING, "illegal outgoing BSDF direction");
367 <                        free(omega_arr);
367 >                        free(omega_iarr); free(omega_oarr);
368                          return(0);
369                  }
370 <                hemi_total += omega_arr[o] = dom*v[2];
370 >                hemi_total += omega_oarr[o] = dom * v[2];
371          }
372          if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
373                  sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%",
374                                  100.*(hemi_total/PI - 1.));
375                  error(WARNING, errmsg);
376          }
377 <        dom = PI / hemi_total;          /* normalize solid angles */
377 >        dom = PI / hemi_total;          /* fix normalization */
378          for (o = dp->nout; o--; )
379 <                omega_arr[o] *= dom;
380 <        nneg = 0;
381 <        for (i = dp->ninc; i--; ) {
379 >                omega_oarr[o] *= dom;
380 >        nneg = 0;                       /* check outgoing totals */
381 >        for (i = 0; i < dp->ninc; i++) {
382                  hemi_total = .0;
383                  for (o = dp->nout; o--; ) {
384                          double  f = BSDF_value(dp,i,o);
385 <                        if (f > .0)
386 <                                hemi_total += f*omega_arr[o];
387 <                        else if (f < -FTINY)
388 <                                ++nneg;
385 >                        if (f >= .0)
386 >                                hemi_total += f*omega_oarr[o];
387 >                        else {
388 >                                nneg += (f < -FTINY);
389 >                                BSDF_value(dp,i,o) = .0f;
390 >                        }
391                  }
392                  if (hemi_total > 1.02) {
393 <                        sprintf(errmsg, "BSDF direction passes %.1f%% of light",
394 <                                        100.*hemi_total);
393 >                        sprintf(errmsg,
394 >                        "incoming BSDF direction %d passes %.1f%% of light",
395 >                                        i, 100.*hemi_total);
396                          error(WARNING, errmsg);
397                  }
398          }
399 <        free(omega_arr);
400 <        if (nneg > 0) {
308 <                sprintf(errmsg, "%d negative BSDF values", nneg);
399 >        if (nneg) {
400 >                sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
401                  error(WARNING, errmsg);
310                return(0);
402          }
403 +                                        /* reverse roles and check again */
404 +        for (o = 0; o < dp->nout; o++) {
405 +                hemi_total = .0;
406 +                for (i = dp->ninc; i--; )
407 +                        hemi_total += BSDF_value(dp,i,o) * omega_iarr[i];
408 +
409 +                if (hemi_total > 1.02) {
410 +                        sprintf(errmsg,
411 +                        "outgoing BSDF direction %d collects %.1f%% of light",
412 +                                        o, 100.*hemi_total);
413 +                        error(WARNING, errmsg);
414 +                }
415 +        }
416 +        free(omega_iarr); free(omega_oarr);
417          return(1);
418   }
419  
# Line 348 | Line 453 | load_BSDF(             /* load BSDF data from file */
453                  return(NULL);
454          }
455          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
456 +        load_angle_basis(ezxml_child(ezxml_child(wtl,
457 +                                "DataDefinition"), "AngleBasis"));
458          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
459          for (wld = ezxml_child(wtl, "WavelengthData");
460                                  wld != NULL; wld = wld->next) {
# Line 434 | Line 541 | r_BSDF_outvec(         /* compute random output vector at giv
541          return(normalize(v) != 0.0);
542   }
543  
437
438 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
544  
545   static int
546   addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines