ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bsdf.c
Revision: 2.1
Committed: Wed Jun 17 20:41:47 2009 UTC (14 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Created dctimestep utility for daylight coefficient calculations

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2     static const char RCSid[] = "$Id: bsdf.c,v 2.1 2009/06/12 17:37:37 greg Exp $";
3     #endif
4     /*
5     * Routines for handling BSDF data
6     */
7    
8     #include "standard.h"
9     #include "bsdf.h"
10     #include "paths.h"
11     #include "ezxml.h"
12     #include <ctype.h>
13    
14     #define MAXLATS 46 /* maximum number of latitudes */
15    
16     /* BSDF angle specification */
17     typedef struct {
18     char name[64]; /* basis name */
19     int nangles; /* total number of directions */
20     struct {
21     float tmin; /* starting theta */
22     short nphis; /* number of phis (0 term) */
23     } lat[MAXLATS+1]; /* latitudes */
24     } ANGLE_BASIS;
25    
26     #define MAXABASES 3 /* limit on defined bases */
27    
28     static ANGLE_BASIS abase_list[MAXABASES] = {
29     {
30     "LBNL/Klems Full", 145,
31     { {-5., 1},
32     {5., 8},
33     {15., 16},
34     {25., 20},
35     {35., 24},
36     {45., 24},
37     {55., 24},
38     {65., 16},
39     {75., 12},
40     {90., 0} }
41     }, {
42     "LBNL/Klems Half", 73,
43     { {-6.5, 1},
44     {6.5, 8},
45     {19.5, 12},
46     {32.5, 16},
47     {46.5, 20},
48     {61.5, 12},
49     {76.5, 4},
50     {90., 0} }
51     }, {
52     "LBNL/Klems Quarter", 41,
53     { {-9., 1},
54     {9., 8},
55     {27., 12},
56     {46., 12},
57     {66., 8},
58     {90., 0} }
59     }
60     };
61    
62     static int nabases = 3; /* current number of defined bases */
63    
64    
65     static int
66     ab_getvec( /* get vector for this angle basis index */
67     FVECT v,
68     int ndx,
69     void *p
70     )
71     {
72     ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
73     int li;
74     double alt, azi, d;
75    
76     if ((ndx < 0) | (ndx >= ab->nangles))
77     return(0);
78     for (li = 0; ndx >= ab->lat[li].nphis; li++)
79     ndx -= ab->lat[li].nphis;
80     alt = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
81     azi = 2.*PI*ndx/ab->lat[li].nphis;
82     v[2] = d = cos(alt);
83     d = sqrt(1. - d*d); /* sin(alt) */
84     v[0] = cos(azi)*d;
85     v[1] = sin(azi)*d;
86     return(1);
87     }
88    
89    
90     static int
91     ab_getndx( /* get index corresponding to the given vector */
92     FVECT v,
93     void *p
94     )
95     {
96     ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
97     int li, ndx;
98     double alt, azi, d;
99    
100     if ((v[2] < -1.0) | (v[2] > 1.0))
101     return(-1);
102     alt = 180.0/PI*acos(v[2]);
103     azi = 180.0/PI*atan2(v[1], v[0]);
104     if (azi < 0.0) azi += 360.0;
105     for (li = 1; ab->lat[li].tmin <= alt; li++)
106     if (!ab->lat[li].nphis)
107     return(-1);
108     --li;
109     ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5);
110     if (ndx >= ab->lat[li].nphis) ndx = 0;
111     while (li--)
112     ndx += ab->lat[li].nphis;
113     return(ndx);
114     }
115    
116    
117     static double
118     ab_getohm( /* get solid angle for this angle basis index */
119     int ndx,
120     void *p
121     )
122     {
123     ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
124     int li;
125     double theta, theta1;
126    
127     if ((ndx < 0) | (ndx >= ab->nangles))
128     return(0);
129     for (li = 0; ndx >= ab->lat[li].nphis; li++)
130     ndx -= ab->lat[li].nphis;
131     theta1 = PI/180. * ab->lat[li+1].tmin;
132     if (ab->lat[li].nphis == 1) { /* special case */
133     if (ab->lat[li].tmin > FTINY)
134     error(USER, "unsupported BSDF coordinate system");
135     return(2.*PI*(1. - cos(theta1)));
136     }
137     theta = PI/180. * ab->lat[li].tmin;
138     return(2.*PI*(cos(theta) - cos(theta1))/(double)ab->lat[li].nphis);
139     }
140    
141    
142     static int
143     ab_getvecR( /* get reverse vector for this angle basis index */
144     FVECT v,
145     int ndx,
146     void *p
147     )
148     {
149     if (!ab_getvec(v, ndx, p))
150     return(0);
151    
152     v[0] = -v[0];
153     v[1] = -v[1];
154     v[2] = -v[2];
155    
156     return(1);
157     }
158    
159    
160     static int
161     ab_getndxR( /* get index corresponding to the reverse vector */
162     FVECT v,
163     void *p
164     )
165     {
166     FVECT v2;
167    
168     v2[0] = -v[0];
169     v2[1] = -v[1];
170     v2[2] = -v[2];
171    
172     return ab_getndx(v2, p);
173     }
174    
175    
176     static void
177     load_bsdf_data( /* load BSDF distribution for this wavelength */
178     struct BSDF_data *dp,
179     ezxml_t wdb
180     )
181     {
182     char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
183     char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
184     char *sdata;
185     int i;
186    
187     if ((cbasis == NULL) | (rbasis == NULL)) {
188     error(WARNING, "missing column/row basis for BSDF");
189     return;
190     }
191     /* XXX need to add routines for loading in foreign bases */
192     for (i = nabases; i--; )
193     if (!strcmp(cbasis, abase_list[i].name)) {
194     dp->ninc = abase_list[i].nangles;
195     dp->ib_priv = (void *)&abase_list[i];
196     dp->ib_vec = ab_getvecR;
197     dp->ib_ndx = ab_getndxR;
198     dp->ib_ohm = ab_getohm;
199     break;
200     }
201     if (i < 0) {
202     sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
203     error(WARNING, errmsg);
204     return;
205     }
206     for (i = nabases; i--; )
207     if (!strcmp(rbasis, abase_list[i].name)) {
208     dp->nout = abase_list[i].nangles;
209     dp->ob_priv = (void *)&abase_list[i];
210     dp->ob_vec = ab_getvec;
211     dp->ob_ndx = ab_getndx;
212     dp->ob_ohm = ab_getohm;
213     break;
214     }
215     if (i < 0) {
216     sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
217     error(WARNING, errmsg);
218     return;
219     }
220     /* read BSDF data */
221     sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
222     if (sdata == NULL) {
223     error(WARNING, "missing BSDF ScatteringData");
224     return;
225     }
226     dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout);
227     if (dp->bsdf == NULL)
228     error(SYSTEM, "out of memory in load_bsdf_data");
229     for (i = 0; i < dp->ninc*dp->nout; i++) {
230     char *sdnext = fskip(sdata);
231     if (sdnext == NULL) {
232     error(WARNING, "bad/missing BSDF ScatteringData");
233     free(dp->bsdf); dp->bsdf = NULL;
234     return;
235     }
236     while (*sdnext && isspace(*sdnext))
237     sdnext++;
238     if (*sdnext == ',') sdnext++;
239     dp->bsdf[i] = atof(sdata);
240     sdata = sdnext;
241     }
242     while (isspace(*sdata))
243     sdata++;
244     if (*sdata) {
245     sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
246     strlen(sdata));
247     error(WARNING, errmsg);
248     }
249     }
250    
251    
252     static int
253     check_bsdf_data( /* check that BSDF data is sane */
254     struct BSDF_data *dp
255     )
256     {
257     double * omega_arr;
258     double dom, hemi_total;
259     int nneg;
260     int i, o;
261    
262     if (dp == NULL || dp->bsdf == NULL)
263     return(0);
264     omega_arr = (double *)calloc(dp->nout, sizeof(double));
265     if (omega_arr == NULL)
266     error(SYSTEM, "out of memory in check_bsdf_data");
267     hemi_total = .0;
268     for (o = dp->nout; o--; ) {
269     FVECT v;
270     dom = getBSDF_outohm(dp,o);
271     if (dom <= .0) {
272     error(WARNING, "zero/negative solid angle");
273     continue;
274     }
275     if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) {
276     error(WARNING, "illegal outgoing BSDF direction");
277     free(omega_arr);
278     return(0);
279     }
280     hemi_total += omega_arr[o] = dom*v[2];
281     }
282     if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
283     sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%",
284     100.*(hemi_total/PI - 1.));
285     error(WARNING, errmsg);
286     }
287     dom = PI / hemi_total; /* normalize solid angles */
288     for (o = dp->nout; o--; )
289     omega_arr[o] *= dom;
290     nneg = 0;
291     for (i = dp->ninc; i--; ) {
292     hemi_total = .0;
293     for (o = dp->nout; o--; ) {
294     double f = BSDF_value(dp,i,o);
295     if (f > .0)
296     hemi_total += f*omega_arr[o];
297     else if (f < -FTINY)
298     ++nneg;
299     }
300     if (hemi_total > 1.02) {
301     sprintf(errmsg, "BSDF direction passes %.1f%% of light",
302     100.*hemi_total);
303     error(WARNING, errmsg);
304     }
305     }
306     free(omega_arr);
307     if (nneg > 0) {
308     sprintf(errmsg, "%d negative BSDF values", nneg);
309     error(WARNING, errmsg);
310     return(0);
311     }
312     return(1);
313     }
314    
315     struct BSDF_data *
316     load_BSDF( /* load BSDF data from file */
317     char *fname
318     )
319     {
320     char *path;
321     ezxml_t fl, wtl, wld, wdb;
322     struct BSDF_data *dp;
323    
324     path = getpath(fname, getrlibpath(), R_OK);
325     if (path == NULL) {
326     sprintf(errmsg, "cannot find BSDF file \"%s\"", fname);
327     error(WARNING, errmsg);
328     return(NULL);
329     }
330     fl = ezxml_parse_file(path);
331     if (fl == NULL) {
332     sprintf(errmsg, "cannot open BSDF \"%s\"", path);
333     error(WARNING, errmsg);
334     return(NULL);
335     }
336     if (ezxml_error(fl)[0]) {
337     sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl));
338     error(WARNING, errmsg);
339     ezxml_free(fl);
340     return(NULL);
341     }
342     if (strcmp(ezxml_name(fl), "WindowElement")) {
343     sprintf(errmsg,
344     "BSDF \"%s\": top level node not 'WindowElement'",
345     path);
346     error(WARNING, errmsg);
347     ezxml_free(fl);
348     return(NULL);
349     }
350     wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
351     dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
352     for (wld = ezxml_child(wtl, "WavelengthData");
353     wld != NULL; wld = wld->next) {
354     if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
355     continue;
356     wdb = ezxml_child(wld, "WavelengthDataBlock");
357     if (wdb == NULL) continue;
358     if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")),
359     "Transmission Front"))
360     continue;
361     load_bsdf_data(dp, wdb); /* load front BTDF */
362     break; /* ignore the rest */
363     }
364     ezxml_free(fl); /* done with XML file */
365     if (!check_bsdf_data(dp)) {
366     sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path);
367     error(WARNING, errmsg);
368     free_BSDF(dp);
369     dp = NULL;
370     }
371     return(dp);
372     }
373    
374    
375     void
376     free_BSDF( /* free BSDF data structure */
377     struct BSDF_data *b
378     )
379     {
380     if (b == NULL)
381     return;
382     if (b->bsdf != NULL)
383     free(b->bsdf);
384     free(b);
385     }
386    
387    
388     int
389     r_BSDF_incvec( /* compute random input vector at given location */
390     FVECT v,
391     struct BSDF_data *b,
392     int i,
393     double rv,
394     MAT4 xm
395     )
396     {
397     FVECT pert;
398     double rad;
399     int j;
400    
401     if (!getBSDF_incvec(v, b, i))
402     return(0);
403     rad = sqrt(getBSDF_incohm(b, i) / PI);
404     multisamp(pert, 3, rv);
405     for (j = 0; j < 3; j++)
406     v[j] += rad*(2.*pert[j] - 1.);
407     if (xm != NULL)
408     multv3(v, v, xm);
409     return(normalize(v) != 0.0);
410     }
411    
412    
413     int
414     r_BSDF_outvec( /* compute random output vector at given location */
415     FVECT v,
416     struct BSDF_data *b,
417     int o,
418     double rv,
419     MAT4 xm
420     )
421     {
422     FVECT pert;
423     double rad;
424     int j;
425    
426     if (!getBSDF_outvec(v, b, o))
427     return(0);
428     rad = sqrt(getBSDF_outohm(b, o) / PI);
429     multisamp(pert, 3, rv);
430     for (j = 0; j < 3; j++)
431     v[j] += rad*(2.*pert[j] - 1.);
432     if (xm != NULL)
433     multv3(v, v, xm);
434     return(normalize(v) != 0.0);
435     }
436    
437    
438     #define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
439    
440     static int
441     addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */
442     char *xfarg[],
443     FVECT xp,
444     FVECT yp,
445     FVECT zp
446     )
447     {
448     static char bufs[3][16];
449     int bn = 0;
450     char **xfp = xfarg;
451     double theta;
452    
453     if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
454     /* Special case for X' along Z-axis */
455     theta = -atan2(yp[0], yp[1]);
456     *xfp++ = "-ry";
457     *xfp++ = xp[2] < 0.0 ? "90" : "-90";
458     *xfp++ = "-rz";
459     sprintf(bufs[bn], "%f", theta*(180./PI));
460     *xfp++ = bufs[bn++];
461     return(xfp - xfarg);
462     }
463     theta = atan2(yp[2], zp[2]);
464     if (!FEQ(theta,0.0)) {
465     *xfp++ = "-rx";
466     sprintf(bufs[bn], "%f", theta*(180./PI));
467     *xfp++ = bufs[bn++];
468     }
469     theta = asin(-xp[2]);
470     if (!FEQ(theta,0.0)) {
471     *xfp++ = "-ry";
472     sprintf(bufs[bn], " %f", theta*(180./PI));
473     *xfp++ = bufs[bn++];
474     }
475     theta = atan2(xp[1], xp[0]);
476     if (!FEQ(theta,0.0)) {
477     *xfp++ = "-rz";
478     sprintf(bufs[bn], "%f", theta*(180./PI));
479     *xfp++ = bufs[bn++];
480     }
481     *xfp = NULL;
482     return(xfp - xfarg);
483     }
484    
485    
486     int
487     getBSDF_xfm( /* compute BSDF orient. -> world orient. transform */
488     MAT4 xm,
489     FVECT nrm,
490     UpDir ud
491     )
492     {
493     char *xfargs[7];
494     XF myxf;
495     FVECT updir, xdest, ydest;
496    
497     updir[0] = updir[1] = updir[2] = 0.;
498     switch (ud) {
499     case UDzneg:
500     updir[2] = -1.;
501     break;
502     case UDyneg:
503     updir[1] = -1.;
504     break;
505     case UDxneg:
506     updir[0] = -1.;
507     break;
508     case UDxpos:
509     updir[0] = 1.;
510     break;
511     case UDypos:
512     updir[1] = 1.;
513     break;
514     case UDzpos:
515     updir[2] = 1.;
516     break;
517     case UDunknown:
518     return(0);
519     }
520     fcross(xdest, updir, nrm);
521     if (normalize(xdest) == 0.0)
522     return(0);
523     fcross(ydest, nrm, xdest);
524     xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
525     copymat4(xm, myxf.xfm);
526     return(1);
527     }