ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bsdf.c
Revision: 2.9
Committed: Mon Jan 3 19:27:00 2011 UTC (13 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +11 -3 lines
Log Message:
Slight improvement to conditional

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.9 static const char RCSid[] = "$Id: bsdf.c,v 2.8 2010/09/26 15:43:26 greg Exp $";
3 greg 2.1 #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 greg 2.4 #define MAXABASES 7 /* limit on defined bases */
27 greg 2.1
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 greg 2.9 #define FEQ(a,b) ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6)
65    
66     static int
67     fequal(double a, double b)
68     {
69     if (b != .0)
70     a = a/b - 1.;
71     return((a <= 1e-6) & (a >= -1e-6));
72     }
73 greg 2.3
74     // returns the name of the given tag
75     #ifdef ezxml_name
76     #undef ezxml_name
77     static char *
78     ezxml_name(ezxml_t xml)
79     {
80     if (xml == NULL)
81     return(NULL);
82     return(xml->name);
83     }
84     #endif
85    
86     // returns the given tag's character content or empty string if none
87     #ifdef ezxml_txt
88     #undef ezxml_txt
89     static char *
90     ezxml_txt(ezxml_t xml)
91     {
92     if (xml == NULL)
93     return("");
94     return(xml->txt);
95     }
96     #endif
97    
98 greg 2.1
99     static int
100     ab_getvec( /* get vector for this angle basis index */
101     FVECT v,
102     int ndx,
103     void *p
104     )
105     {
106     ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
107     int li;
108 greg 2.2 double pol, azi, d;
109 greg 2.1
110     if ((ndx < 0) | (ndx >= ab->nangles))
111     return(0);
112     for (li = 0; ndx >= ab->lat[li].nphis; li++)
113     ndx -= ab->lat[li].nphis;
114 greg 2.2 pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin);
115 greg 2.1 azi = 2.*PI*ndx/ab->lat[li].nphis;
116 greg 2.2 v[2] = d = cos(pol);
117     d = sqrt(1. - d*d); /* sin(pol) */
118 greg 2.1 v[0] = cos(azi)*d;
119     v[1] = sin(azi)*d;
120     return(1);
121     }
122    
123    
124     static int
125     ab_getndx( /* get index corresponding to the given vector */
126     FVECT v,
127     void *p
128     )
129     {
130     ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
131     int li, ndx;
132 greg 2.2 double pol, azi, d;
133 greg 2.1
134     if ((v[2] < -1.0) | (v[2] > 1.0))
135     return(-1);
136 greg 2.2 pol = 180.0/PI*acos(v[2]);
137 greg 2.1 azi = 180.0/PI*atan2(v[1], v[0]);
138     if (azi < 0.0) azi += 360.0;
139 greg 2.2 for (li = 1; ab->lat[li].tmin <= pol; li++)
140 greg 2.1 if (!ab->lat[li].nphis)
141     return(-1);
142     --li;
143     ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5);
144     if (ndx >= ab->lat[li].nphis) ndx = 0;
145     while (li--)
146     ndx += ab->lat[li].nphis;
147     return(ndx);
148     }
149    
150    
151     static double
152     ab_getohm( /* get solid angle for this angle basis index */
153     int ndx,
154     void *p
155     )
156     {
157     ANGLE_BASIS *ab = (ANGLE_BASIS *)p;
158     int li;
159     double theta, theta1;
160    
161     if ((ndx < 0) | (ndx >= ab->nangles))
162     return(0);
163     for (li = 0; ndx >= ab->lat[li].nphis; li++)
164     ndx -= ab->lat[li].nphis;
165     theta1 = PI/180. * ab->lat[li+1].tmin;
166     if (ab->lat[li].nphis == 1) { /* special case */
167     if (ab->lat[li].tmin > FTINY)
168     error(USER, "unsupported BSDF coordinate system");
169     return(2.*PI*(1. - cos(theta1)));
170     }
171     theta = PI/180. * ab->lat[li].tmin;
172     return(2.*PI*(cos(theta) - cos(theta1))/(double)ab->lat[li].nphis);
173     }
174    
175    
176     static int
177     ab_getvecR( /* get reverse vector for this angle basis index */
178     FVECT v,
179     int ndx,
180     void *p
181     )
182     {
183     if (!ab_getvec(v, ndx, p))
184     return(0);
185    
186     v[0] = -v[0];
187     v[1] = -v[1];
188     v[2] = -v[2];
189    
190     return(1);
191     }
192    
193    
194     static int
195     ab_getndxR( /* get index corresponding to the reverse vector */
196     FVECT v,
197     void *p
198     )
199     {
200     FVECT v2;
201    
202     v2[0] = -v[0];
203     v2[1] = -v[1];
204     v2[2] = -v[2];
205    
206     return ab_getndx(v2, p);
207     }
208    
209    
210     static void
211 greg 2.4 load_angle_basis( /* load custom BSDF angle basis */
212 greg 2.3 ezxml_t wab
213     )
214     {
215     char *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName"));
216     ezxml_t wbb;
217     int i;
218    
219 greg 2.4 if (!abname || !*abname)
220 greg 2.3 return;
221     for (i = nabases; i--; )
222     if (!strcmp(abname, abase_list[i].name))
223 greg 2.4 return; /* assume it's the same */
224 greg 2.3 if (nabases >= MAXABASES)
225     error(INTERNAL, "too many angle bases");
226     strcpy(abase_list[nabases].name, abname);
227     abase_list[nabases].nangles = 0;
228     for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock");
229     wbb != NULL; i++, wbb = wbb->next) {
230     if (i >= MAXLATS)
231     error(INTERNAL, "too many latitudes in custom basis");
232     abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt(
233     ezxml_child(ezxml_child(wbb,
234     "ThetaBounds"), "UpperTheta")));
235     if (!i)
236     abase_list[nabases].lat[i].tmin =
237     -abase_list[nabases].lat[i+1].tmin;
238 greg 2.9 else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
239 greg 2.3 "ThetaBounds"), "LowerTheta"))),
240     abase_list[nabases].lat[i].tmin))
241     error(WARNING, "theta values disagree in custom basis");
242     abase_list[nabases].nangles +=
243     abase_list[nabases].lat[i].nphis =
244     atoi(ezxml_txt(ezxml_child(wbb, "nPhis")));
245     }
246     abase_list[nabases++].lat[i].nphis = 0;
247     }
248    
249    
250 greg 2.6 static double
251     to_meters( /* return factor to convert given unit to meters */
252     const char *unit
253     )
254     {
255     if (unit == NULL) return(1.); /* safe assumption? */
256     if (!strcasecmp(unit, "Meter")) return(1.);
257     if (!strcasecmp(unit, "Foot")) return(.3048);
258     if (!strcasecmp(unit, "Inch")) return(.0254);
259     if (!strcasecmp(unit, "Centimeter")) return(.01);
260 greg 2.8 if (!strcasecmp(unit, "Millimeter")) return(.001);
261 greg 2.6 sprintf(errmsg, "unknown dimensional unit '%s'", unit);
262     error(USER, errmsg);
263     }
264    
265    
266     static void
267     load_geometry( /* load geometric dimensions and description (if any) */
268     struct BSDF_data *dp,
269     ezxml_t wdb
270     )
271     {
272     ezxml_t geom;
273     double cfact;
274     const char *fmt, *mgfstr;
275    
276     dp->dim[0] = dp->dim[1] = dp->dim[2] = 0;
277     dp->mgf = NULL;
278     if ((geom = ezxml_child(wdb, "Width")) != NULL)
279     dp->dim[0] = atof(ezxml_txt(geom)) *
280     to_meters(ezxml_attr(geom, "unit"));
281     if ((geom = ezxml_child(wdb, "Height")) != NULL)
282     dp->dim[1] = atof(ezxml_txt(geom)) *
283     to_meters(ezxml_attr(geom, "unit"));
284     if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
285     dp->dim[2] = atof(ezxml_txt(geom)) *
286     to_meters(ezxml_attr(geom, "unit"));
287     if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
288     (mgfstr = ezxml_txt(geom)) == NULL)
289     return;
290     if ((fmt = ezxml_attr(geom, "format")) != NULL &&
291     strcasecmp(fmt, "MGF")) {
292     sprintf(errmsg, "unrecognized geometry format '%s'", fmt);
293     error(WARNING, errmsg);
294     return;
295     }
296     cfact = to_meters(ezxml_attr(geom, "unit"));
297     dp->mgf = (char *)malloc(strlen(mgfstr)+32);
298     if (dp->mgf == NULL)
299     error(SYSTEM, "out of memory in load_geometry");
300     if (cfact < 0.99 || cfact > 1.01)
301     sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
302     else
303     strcpy(dp->mgf, mgfstr);
304     }
305    
306    
307 greg 2.3 static void
308 greg 2.1 load_bsdf_data( /* load BSDF distribution for this wavelength */
309     struct BSDF_data *dp,
310     ezxml_t wdb
311     )
312     {
313     char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis"));
314     char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis"));
315     char *sdata;
316     int i;
317    
318 greg 2.4 if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
319 greg 2.1 error(WARNING, "missing column/row basis for BSDF");
320     return;
321     }
322     for (i = nabases; i--; )
323     if (!strcmp(cbasis, abase_list[i].name)) {
324     dp->ninc = abase_list[i].nangles;
325     dp->ib_priv = (void *)&abase_list[i];
326     dp->ib_vec = ab_getvecR;
327     dp->ib_ndx = ab_getndxR;
328     dp->ib_ohm = ab_getohm;
329     break;
330     }
331     if (i < 0) {
332 greg 2.4 sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
333 greg 2.1 error(WARNING, errmsg);
334     return;
335     }
336     for (i = nabases; i--; )
337     if (!strcmp(rbasis, abase_list[i].name)) {
338     dp->nout = abase_list[i].nangles;
339     dp->ob_priv = (void *)&abase_list[i];
340     dp->ob_vec = ab_getvec;
341     dp->ob_ndx = ab_getndx;
342     dp->ob_ohm = ab_getohm;
343     break;
344     }
345     if (i < 0) {
346 greg 2.4 sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
347 greg 2.1 error(WARNING, errmsg);
348     return;
349     }
350     /* read BSDF data */
351     sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
352 greg 2.4 if (!sdata || !*sdata) {
353 greg 2.1 error(WARNING, "missing BSDF ScatteringData");
354     return;
355     }
356     dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout);
357     if (dp->bsdf == NULL)
358     error(SYSTEM, "out of memory in load_bsdf_data");
359     for (i = 0; i < dp->ninc*dp->nout; i++) {
360     char *sdnext = fskip(sdata);
361     if (sdnext == NULL) {
362     error(WARNING, "bad/missing BSDF ScatteringData");
363     free(dp->bsdf); dp->bsdf = NULL;
364     return;
365     }
366     while (*sdnext && isspace(*sdnext))
367     sdnext++;
368     if (*sdnext == ',') sdnext++;
369     dp->bsdf[i] = atof(sdata);
370     sdata = sdnext;
371     }
372     while (isspace(*sdata))
373     sdata++;
374     if (*sdata) {
375     sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
376 greg 2.5 (int)strlen(sdata));
377 greg 2.1 error(WARNING, errmsg);
378     }
379     }
380    
381    
382     static int
383     check_bsdf_data( /* check that BSDF data is sane */
384     struct BSDF_data *dp
385     )
386     {
387 greg 2.2 double *omega_iarr, *omega_oarr;
388 greg 2.7 double dom, contrib, hemi_total, full_total;
389 greg 2.1 int nneg;
390 greg 2.2 FVECT v;
391 greg 2.1 int i, o;
392    
393     if (dp == NULL || dp->bsdf == NULL)
394     return(0);
395 greg 2.2 omega_iarr = (double *)calloc(dp->ninc, sizeof(double));
396     omega_oarr = (double *)calloc(dp->nout, sizeof(double));
397     if ((omega_iarr == NULL) | (omega_oarr == NULL))
398 greg 2.1 error(SYSTEM, "out of memory in check_bsdf_data");
399 greg 2.2 /* incoming projected solid angles */
400     hemi_total = .0;
401     for (i = dp->ninc; i--; ) {
402     dom = getBSDF_incohm(dp,i);
403     if (dom <= .0) {
404     error(WARNING, "zero/negative incoming solid angle");
405     continue;
406     }
407     if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) {
408     error(WARNING, "illegal incoming BSDF direction");
409     free(omega_iarr); free(omega_oarr);
410     return(0);
411     }
412     hemi_total += omega_iarr[i] = dom * -v[2];
413     }
414     if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
415     sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%",
416     100.*(hemi_total/PI - 1.));
417     error(WARNING, errmsg);
418     }
419     dom = PI / hemi_total; /* fix normalization */
420     for (i = dp->ninc; i--; )
421     omega_iarr[i] *= dom;
422     /* outgoing projected solid angles */
423 greg 2.1 hemi_total = .0;
424     for (o = dp->nout; o--; ) {
425     dom = getBSDF_outohm(dp,o);
426     if (dom <= .0) {
427 greg 2.2 error(WARNING, "zero/negative outgoing solid angle");
428 greg 2.1 continue;
429     }
430     if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) {
431     error(WARNING, "illegal outgoing BSDF direction");
432 greg 2.2 free(omega_iarr); free(omega_oarr);
433 greg 2.1 return(0);
434     }
435 greg 2.2 hemi_total += omega_oarr[o] = dom * v[2];
436 greg 2.1 }
437     if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) {
438     sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%",
439     100.*(hemi_total/PI - 1.));
440     error(WARNING, errmsg);
441     }
442 greg 2.2 dom = PI / hemi_total; /* fix normalization */
443 greg 2.1 for (o = dp->nout; o--; )
444 greg 2.2 omega_oarr[o] *= dom;
445     nneg = 0; /* check outgoing totals */
446     for (i = 0; i < dp->ninc; i++) {
447 greg 2.1 hemi_total = .0;
448     for (o = dp->nout; o--; ) {
449     double f = BSDF_value(dp,i,o);
450 greg 2.2 if (f >= .0)
451     hemi_total += f*omega_oarr[o];
452     else {
453     nneg += (f < -FTINY);
454     BSDF_value(dp,i,o) = .0f;
455     }
456 greg 2.1 }
457 greg 2.8 if (hemi_total > 1.01) {
458 greg 2.2 sprintf(errmsg,
459     "incoming BSDF direction %d passes %.1f%% of light",
460     i, 100.*hemi_total);
461 greg 2.1 error(WARNING, errmsg);
462     }
463     }
464 greg 2.2 if (nneg) {
465     sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
466 greg 2.1 error(WARNING, errmsg);
467     }
468 greg 2.7 full_total = .0; /* reverse roles and check again */
469 greg 2.2 for (o = 0; o < dp->nout; o++) {
470     hemi_total = .0;
471     for (i = dp->ninc; i--; )
472     hemi_total += BSDF_value(dp,i,o) * omega_iarr[i];
473    
474 greg 2.8 if (hemi_total > 1.01) {
475 greg 2.2 sprintf(errmsg,
476     "outgoing BSDF direction %d collects %.1f%% of light",
477     o, 100.*hemi_total);
478     error(WARNING, errmsg);
479     }
480 greg 2.7 full_total += hemi_total*omega_oarr[o];
481     }
482     full_total /= PI;
483 greg 2.8 if (full_total > 1.00001) {
484     sprintf(errmsg, "BSDF transfers %.4f%% of light",
485 greg 2.7 100.*full_total);
486     error(WARNING, errmsg);
487 greg 2.2 }
488     free(omega_iarr); free(omega_oarr);
489 greg 2.1 return(1);
490     }
491    
492 greg 2.6
493 greg 2.1 struct BSDF_data *
494     load_BSDF( /* load BSDF data from file */
495     char *fname
496     )
497     {
498     char *path;
499     ezxml_t fl, wtl, wld, wdb;
500     struct BSDF_data *dp;
501    
502     path = getpath(fname, getrlibpath(), R_OK);
503     if (path == NULL) {
504     sprintf(errmsg, "cannot find BSDF file \"%s\"", fname);
505     error(WARNING, errmsg);
506     return(NULL);
507     }
508     fl = ezxml_parse_file(path);
509     if (fl == NULL) {
510     sprintf(errmsg, "cannot open BSDF \"%s\"", path);
511     error(WARNING, errmsg);
512     return(NULL);
513     }
514     if (ezxml_error(fl)[0]) {
515     sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl));
516     error(WARNING, errmsg);
517     ezxml_free(fl);
518     return(NULL);
519     }
520     if (strcmp(ezxml_name(fl), "WindowElement")) {
521     sprintf(errmsg,
522     "BSDF \"%s\": top level node not 'WindowElement'",
523     path);
524     error(WARNING, errmsg);
525     ezxml_free(fl);
526     return(NULL);
527     }
528     wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
529 greg 2.3 load_angle_basis(ezxml_child(ezxml_child(wtl,
530     "DataDefinition"), "AngleBasis"));
531 greg 2.1 dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
532 greg 2.6 load_geometry(dp, ezxml_child(wtl, "Material"));
533 greg 2.1 for (wld = ezxml_child(wtl, "WavelengthData");
534     wld != NULL; wld = wld->next) {
535     if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
536     continue;
537     wdb = ezxml_child(wld, "WavelengthDataBlock");
538     if (wdb == NULL) continue;
539     if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")),
540     "Transmission Front"))
541     continue;
542     load_bsdf_data(dp, wdb); /* load front BTDF */
543     break; /* ignore the rest */
544     }
545     ezxml_free(fl); /* done with XML file */
546     if (!check_bsdf_data(dp)) {
547     sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path);
548     error(WARNING, errmsg);
549     free_BSDF(dp);
550     dp = NULL;
551     }
552     return(dp);
553     }
554    
555    
556     void
557     free_BSDF( /* free BSDF data structure */
558     struct BSDF_data *b
559     )
560     {
561     if (b == NULL)
562     return;
563 greg 2.6 if (b->mgf != NULL)
564     free(b->mgf);
565 greg 2.1 if (b->bsdf != NULL)
566     free(b->bsdf);
567     free(b);
568     }
569    
570    
571     int
572     r_BSDF_incvec( /* compute random input vector at given location */
573     FVECT v,
574     struct BSDF_data *b,
575     int i,
576     double rv,
577     MAT4 xm
578     )
579     {
580     FVECT pert;
581     double rad;
582     int j;
583    
584     if (!getBSDF_incvec(v, b, i))
585     return(0);
586     rad = sqrt(getBSDF_incohm(b, i) / PI);
587     multisamp(pert, 3, rv);
588     for (j = 0; j < 3; j++)
589     v[j] += rad*(2.*pert[j] - 1.);
590     if (xm != NULL)
591     multv3(v, v, xm);
592     return(normalize(v) != 0.0);
593     }
594    
595    
596     int
597     r_BSDF_outvec( /* compute random output vector at given location */
598     FVECT v,
599     struct BSDF_data *b,
600     int o,
601     double rv,
602     MAT4 xm
603     )
604     {
605     FVECT pert;
606     double rad;
607     int j;
608    
609     if (!getBSDF_outvec(v, b, o))
610     return(0);
611     rad = sqrt(getBSDF_outohm(b, o) / PI);
612     multisamp(pert, 3, rv);
613     for (j = 0; j < 3; j++)
614     v[j] += rad*(2.*pert[j] - 1.);
615     if (xm != NULL)
616     multv3(v, v, xm);
617     return(normalize(v) != 0.0);
618     }
619    
620    
621     static int
622     addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */
623     char *xfarg[],
624     FVECT xp,
625     FVECT yp,
626     FVECT zp
627     )
628     {
629     static char bufs[3][16];
630     int bn = 0;
631     char **xfp = xfarg;
632     double theta;
633    
634     if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
635     /* Special case for X' along Z-axis */
636     theta = -atan2(yp[0], yp[1]);
637     *xfp++ = "-ry";
638     *xfp++ = xp[2] < 0.0 ? "90" : "-90";
639     *xfp++ = "-rz";
640     sprintf(bufs[bn], "%f", theta*(180./PI));
641     *xfp++ = bufs[bn++];
642     return(xfp - xfarg);
643     }
644     theta = atan2(yp[2], zp[2]);
645     if (!FEQ(theta,0.0)) {
646     *xfp++ = "-rx";
647     sprintf(bufs[bn], "%f", theta*(180./PI));
648     *xfp++ = bufs[bn++];
649     }
650     theta = asin(-xp[2]);
651     if (!FEQ(theta,0.0)) {
652     *xfp++ = "-ry";
653     sprintf(bufs[bn], " %f", theta*(180./PI));
654     *xfp++ = bufs[bn++];
655     }
656     theta = atan2(xp[1], xp[0]);
657     if (!FEQ(theta,0.0)) {
658     *xfp++ = "-rz";
659     sprintf(bufs[bn], "%f", theta*(180./PI));
660     *xfp++ = bufs[bn++];
661     }
662     *xfp = NULL;
663     return(xfp - xfarg);
664     }
665    
666    
667     int
668     getBSDF_xfm( /* compute BSDF orient. -> world orient. transform */
669     MAT4 xm,
670     FVECT nrm,
671 greg 2.6 UpDir ud,
672     char *xfbuf
673 greg 2.1 )
674     {
675     char *xfargs[7];
676     XF myxf;
677     FVECT updir, xdest, ydest;
678 greg 2.6 int i;
679 greg 2.1
680     updir[0] = updir[1] = updir[2] = 0.;
681     switch (ud) {
682     case UDzneg:
683     updir[2] = -1.;
684     break;
685     case UDyneg:
686     updir[1] = -1.;
687     break;
688     case UDxneg:
689     updir[0] = -1.;
690     break;
691     case UDxpos:
692     updir[0] = 1.;
693     break;
694     case UDypos:
695     updir[1] = 1.;
696     break;
697     case UDzpos:
698     updir[2] = 1.;
699     break;
700     case UDunknown:
701     return(0);
702     }
703     fcross(xdest, updir, nrm);
704     if (normalize(xdest) == 0.0)
705     return(0);
706     fcross(ydest, nrm, xdest);
707     xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
708     copymat4(xm, myxf.xfm);
709 greg 2.6 if (xfbuf == NULL)
710     return(1);
711     /* return xf arguments as well */
712     for (i = 0; xfargs[i] != NULL; i++) {
713     *xfbuf++ = ' ';
714     strcpy(xfbuf, xfargs[i]);
715     while (*xfbuf) ++xfbuf;
716     }
717 greg 2.1 return(1);
718     }