ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum4.c
Revision: 2.17
Committed: Sat May 30 22:19:08 2009 UTC (16 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +70 -10 lines
Log Message:
Fixed bug in solid angle calculation and added BSDF checks

File Contents

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