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.2 by greg, Fri Jun 19 06:49:25 2009 UTC vs.
Revision 2.33 by greg, Tue Jun 28 21:11:04 2011 UTC

# Line 2 | Line 2
2   static const char RCSid[] = "$Id$";
3   #endif
4   /*
5 + *  bsdf.c
6 + *  
7 + *  Definitions for bidirectional scattering distribution functions.
8 + *
9 + *  Created by Greg Ward on 1/10/11.
10 + *
11 + */
12 +
13 + #define _USE_MATH_DEFINES
14 + #include <stdio.h>
15 + #include <stdlib.h>
16 + #include <string.h>
17 + #include <math.h>
18 + #include <ctype.h>
19 + #include "ezxml.h"
20 + #include "hilbert.h"
21 + #include "bsdf.h"
22 + #include "bsdf_m.h"
23 + #include "bsdf_t.h"
24 +
25 + /* English ASCII strings corresponding to ennumerated errors */
26 + const char              *SDerrorEnglish[] = {
27 +                                "No error",
28 +                                "Memory error",
29 +                                "File input/output error",
30 +                                "File format error",
31 +                                "Illegal argument",
32 +                                "Invalid data",
33 +                                "Unsupported feature",
34 +                                "Internal program error",
35 +                                "Unknown error"
36 +                        };
37 +
38 + /* Additional information on last error (ASCII English) */
39 + char                    SDerrorDetail[256];
40 +
41 + /* Cache of loaded BSDFs */
42 + struct SDCache_s        *SDcacheList = NULL;
43 +
44 + /* Retain BSDFs in cache list */
45 + int                     SDretainSet = SDretainNone;
46 +
47 + /* Report any error to the indicated stream (in English) */
48 + SDError
49 + SDreportEnglish(SDError ec, FILE *fp)
50 + {
51 +        if (!ec)
52 +                return SDEnone;
53 +        if ((ec < SDEnone) | (ec > SDEunknown)) {
54 +                SDerrorDetail[0] = '\0';
55 +                ec = SDEunknown;
56 +        }
57 +        if (fp == NULL)
58 +                return ec;
59 +        fputs(SDerrorEnglish[ec], fp);
60 +        if (SDerrorDetail[0]) {
61 +                fputs(": ", fp);
62 +                fputs(SDerrorDetail, fp);
63 +        }
64 +        fputc('\n', fp);
65 +        if (fp != stderr)
66 +                fflush(fp);
67 +        return ec;
68 + }
69 +
70 + static double
71 + to_meters(              /* return factor to convert given unit to meters */
72 +        const char *unit
73 + )
74 + {
75 +        if (unit == NULL) return(1.);           /* safe assumption? */
76 +        if (!strcasecmp(unit, "Meter")) return(1.);
77 +        if (!strcasecmp(unit, "Foot")) return(.3048);
78 +        if (!strcasecmp(unit, "Inch")) return(.0254);
79 +        if (!strcasecmp(unit, "Centimeter")) return(.01);
80 +        if (!strcasecmp(unit, "Millimeter")) return(.001);
81 +        sprintf(SDerrorDetail, "Unknown dimensional unit '%s'", unit);
82 +        return(-1.);
83 + }
84 +
85 + /* Load geometric dimensions and description (if any) */
86 + static SDError
87 + SDloadGeometry(SDData *sd, ezxml_t wdb)
88 + {
89 +        ezxml_t         geom;
90 +        double          cfact;
91 +        const char      *fmt, *mgfstr;
92 +
93 +        if (wdb == NULL)                /* no geometry section? */
94 +                return SDEnone;
95 +        sd->dim[0] = sd->dim[1] = sd->dim[2] = .0;
96 +        if ((geom = ezxml_child(wdb, "Width")) != NULL)
97 +                sd->dim[0] = atof(ezxml_txt(geom)) *
98 +                                to_meters(ezxml_attr(geom, "unit"));
99 +        if ((geom = ezxml_child(wdb, "Height")) != NULL)
100 +                sd->dim[1] = atof(ezxml_txt(geom)) *
101 +                                to_meters(ezxml_attr(geom, "unit"));
102 +        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
103 +                sd->dim[2] = atof(ezxml_txt(geom)) *
104 +                                to_meters(ezxml_attr(geom, "unit"));
105 +        if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) {
106 +                sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
107 +                return SDEdata;
108 +        }
109 +        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
110 +                        (mgfstr = ezxml_txt(geom)) == NULL)
111 +                return SDEnone;
112 +        while (isspace(*mgfstr))
113 +                ++mgfstr;
114 +        if (!*mgfstr)
115 +                return SDEnone;
116 +        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
117 +                        strcasecmp(fmt, "MGF")) {
118 +                sprintf(SDerrorDetail,
119 +                        "Unrecognized geometry format '%s' in \"%s\"",
120 +                                        fmt, sd->name);
121 +                return SDEsupport;
122 +        }
123 +        cfact = to_meters(ezxml_attr(geom, "unit"));
124 +        sd->mgf = (char *)malloc(strlen(mgfstr)+32);
125 +        if (sd->mgf == NULL) {
126 +                strcpy(SDerrorDetail, "Out of memory in SDloadGeometry");
127 +                return SDEmemory;
128 +        }
129 +        if (cfact < 0.99 || cfact > 1.01)
130 +                sprintf(sd->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
131 +        else
132 +                strcpy(sd->mgf, mgfstr);
133 +        return SDEnone;
134 + }
135 +
136 + /* Load a BSDF struct from the given file (free first and keep name) */
137 + SDError
138 + SDloadFile(SDData *sd, const char *fname)
139 + {
140 +        SDError         lastErr;
141 +        ezxml_t         fl, wtl;
142 +
143 +        if ((sd == NULL) | (fname == NULL || !*fname))
144 +                return SDEargument;
145 +                                /* free old data, keeping name */
146 +        SDfreeBSDF(sd);
147 +                                /* parse XML file */
148 +        fl = ezxml_parse_file(fname);
149 +        if (fl == NULL) {
150 +                sprintf(SDerrorDetail, "Cannot open BSDF \"%s\"", fname);
151 +                return SDEfile;
152 +        }
153 +        if (ezxml_error(fl)[0]) {
154 +                sprintf(SDerrorDetail, "BSDF \"%s\" %s", fname, ezxml_error(fl));
155 +                ezxml_free(fl);
156 +                return SDEformat;
157 +        }
158 +        if (strcmp(ezxml_name(fl), "WindowElement")) {
159 +                sprintf(SDerrorDetail,
160 +                        "BSDF \"%s\": top level node not 'WindowElement'",
161 +                                sd->name);
162 +                ezxml_free(fl);
163 +                return SDEformat;
164 +        }
165 +        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
166 +        if (wtl == NULL) {
167 +                sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'",
168 +                                sd->name);
169 +                ezxml_free(fl);
170 +                return SDEformat;
171 +        }
172 +                                /* load geometry if present */
173 +        lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
174 +        if (lastErr)
175 +                return lastErr;
176 +                                /* try loading variable resolution data */
177 +        lastErr = SDloadTre(sd, wtl);
178 +                                /* check our result */
179 +        if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */
180 +                lastErr = SDloadMtx(sd, wtl);
181 +                
182 +                                /* done with XML file */
183 +        ezxml_free(fl);
184 +        
185 +        if (lastErr) {          /* was there a load error? */
186 +                SDfreeBSDF(sd);
187 +                return lastErr;
188 +        }
189 +                                /* remove any insignificant components */
190 +        if (sd->rf != NULL && sd->rf->maxHemi <= .001) {
191 +                SDfreeSpectralDF(sd->rf); sd->rf = NULL;
192 +        }
193 +        if (sd->rb != NULL && sd->rb->maxHemi <= .001) {
194 +                SDfreeSpectralDF(sd->rb); sd->rb = NULL;
195 +        }
196 +        if (sd->tf != NULL && sd->tf->maxHemi <= .001) {
197 +                SDfreeSpectralDF(sd->tf); sd->tf = NULL;
198 +        }
199 +                                /* return success */
200 +        return SDEnone;
201 + }
202 +
203 + /* Allocate new spectral distribution function */
204 + SDSpectralDF *
205 + SDnewSpectralDF(int nc)
206 + {
207 +        SDSpectralDF    *df;
208 +        
209 +        if (nc <= 0) {
210 +                strcpy(SDerrorDetail, "Zero component spectral DF request");
211 +                return NULL;
212 +        }
213 +        df = (SDSpectralDF *)malloc(sizeof(SDSpectralDF) +
214 +                                        (nc-1)*sizeof(SDComponent));
215 +        if (df == NULL) {
216 +                sprintf(SDerrorDetail,
217 +                                "Cannot allocate %d component spectral DF", nc);
218 +                return NULL;
219 +        }
220 +        df->minProjSA = .0;
221 +        df->maxHemi = .0;
222 +        df->ncomp = nc;
223 +        memset(df->comp, 0, nc*sizeof(SDComponent));
224 +        return df;
225 + }
226 +
227 + /* Free cached cumulative distributions for BSDF component */
228 + void
229 + SDfreeCumulativeCache(SDSpectralDF *df)
230 + {
231 +        int     n;
232 +        SDCDst  *cdp;
233 +
234 +        if (df == NULL)
235 +                return;
236 +        for (n = df->ncomp; n-- > 0; )
237 +                while ((cdp = df->comp[n].cdList) != NULL) {
238 +                        df->comp[n].cdList = cdp->next;
239 +                        free(cdp);
240 +                }
241 + }
242 +
243 + /* Free a spectral distribution function */
244 + void
245 + SDfreeSpectralDF(SDSpectralDF *df)
246 + {
247 +        int     n;
248 +
249 +        if (df == NULL)
250 +                return;
251 +        SDfreeCumulativeCache(df);
252 +        for (n = df->ncomp; n-- > 0; )
253 +                (*df->comp[n].func->freeSC)(df->comp[n].dist);
254 +        free(df);
255 + }
256 +
257 + /* Shorten file path to useable BSDF name, removing suffix */
258 + void
259 + SDclipName(char *res, const char *fname)
260 + {
261 +        const char      *cp, *dot = NULL;
262 +        
263 +        for (cp = fname; *cp; cp++)
264 +                if (*cp == '.')
265 +                        dot = cp;
266 +        if ((dot == NULL) | (dot < fname+2))
267 +                dot = cp;
268 +        if (dot - fname >= SDnameLn)
269 +                fname = dot - SDnameLn + 1;
270 +        while (fname < dot)
271 +                *res++ = *fname++;
272 +        *res = '\0';
273 + }
274 +
275 + /* Initialize an unused BSDF struct (simply clears to zeroes) */
276 + void
277 + SDclearBSDF(SDData *sd, const char *fname)
278 + {
279 +        if (sd == NULL)
280 +                return;
281 +        memset(sd, 0, sizeof(SDData));
282 +        if (fname == NULL)
283 +                return;
284 +        SDclipName(sd->name, fname);
285 + }
286 +
287 + /* Free data associated with BSDF struct */
288 + void
289 + SDfreeBSDF(SDData *sd)
290 + {
291 +        if (sd == NULL)
292 +                return;
293 +        if (sd->mgf != NULL) {
294 +                free(sd->mgf);
295 +                sd->mgf = NULL;
296 +        }
297 +        if (sd->rf != NULL) {
298 +                SDfreeSpectralDF(sd->rf);
299 +                sd->rf = NULL;
300 +        }
301 +        if (sd->rb != NULL) {
302 +                SDfreeSpectralDF(sd->rb);
303 +                sd->rb = NULL;
304 +        }
305 +        if (sd->tf != NULL) {
306 +                SDfreeSpectralDF(sd->tf);
307 +                sd->tf = NULL;
308 +        }
309 +        sd->rLambFront.cieY = .0;
310 +        sd->rLambFront.spec.flags = 0;
311 +        sd->rLambBack.cieY = .0;
312 +        sd->rLambBack.spec.flags = 0;
313 +        sd->tLamb.cieY = .0;
314 +        sd->tLamb.spec.flags = 0;
315 + }
316 +
317 + /* Find writeable BSDF by name, or allocate new cache entry if absent */
318 + SDData *
319 + SDgetCache(const char *bname)
320 + {
321 +        struct SDCache_s        *sdl;
322 +        char                    sdnam[SDnameLn];
323 +
324 +        if (bname == NULL)
325 +                return NULL;
326 +
327 +        SDclipName(sdnam, bname);
328 +        for (sdl = SDcacheList; sdl != NULL; sdl = sdl->next)
329 +                if (!strcmp(sdl->bsdf.name, sdnam)) {
330 +                        sdl->refcnt++;
331 +                        return &sdl->bsdf;
332 +                }
333 +
334 +        sdl = (struct SDCache_s *)calloc(1, sizeof(struct SDCache_s));
335 +        if (sdl == NULL)
336 +                return NULL;
337 +
338 +        strcpy(sdl->bsdf.name, sdnam);
339 +        sdl->next = SDcacheList;
340 +        SDcacheList = sdl;
341 +
342 +        sdl->refcnt = 1;
343 +        return &sdl->bsdf;
344 + }
345 +
346 + /* Get loaded BSDF from cache (or load and cache it on first call) */
347 + /* Report any problem to stderr and return NULL on failure */
348 + const SDData *
349 + SDcacheFile(const char *fname)
350 + {
351 +        SDData          *sd;
352 +        SDError         ec;
353 +        
354 +        if (fname == NULL || !*fname)
355 +                return NULL;
356 +        SDerrorDetail[0] = '\0';
357 +        if ((sd = SDgetCache(fname)) == NULL) {
358 +                SDreportEnglish(SDEmemory, stderr);
359 +                return NULL;
360 +        }
361 +        if (!SDisLoaded(sd) && (ec = SDloadFile(sd, fname))) {
362 +                SDreportEnglish(ec, stderr);
363 +                SDfreeCache(sd);
364 +                return NULL;
365 +        }
366 +        return sd;
367 + }
368 +
369 + /* Free a BSDF from our cache (clear all if NULL) */
370 + void
371 + SDfreeCache(const SDData *sd)
372 + {
373 +        struct SDCache_s        *sdl, *sdLast = NULL;
374 +
375 +        if (sd == NULL) {               /* free entire list */
376 +                while ((sdl = SDcacheList) != NULL) {
377 +                        SDcacheList = sdl->next;
378 +                        SDfreeBSDF(&sdl->bsdf);
379 +                        free(sdl);
380 +                }
381 +                return;
382 +        }
383 +        for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
384 +                if (&sdl->bsdf == sd)
385 +                        break;
386 +        if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0)))
387 +                return;                 /* missing or still in use */
388 +                                        /* keep unreferenced data? */
389 +        if (SDisLoaded(sd) && SDretainSet) {
390 +                if (SDretainSet == SDretainAll)
391 +                        return;         /* keep everything */
392 +                                        /* else free cumulative data */
393 +                SDfreeCumulativeCache(sd->rf);
394 +                SDfreeCumulativeCache(sd->rb);
395 +                SDfreeCumulativeCache(sd->tf);
396 +                return;
397 +        }
398 +                                        /* remove from list and free */
399 +        if (sdLast == NULL)
400 +                SDcacheList = sdl->next;
401 +        else
402 +                sdLast->next = sdl->next;
403 +        SDfreeBSDF(&sdl->bsdf);
404 +        free(sdl);
405 + }
406 +
407 + /* Sample an individual BSDF component */
408 + SDError
409 + SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc)
410 + {
411 +        float           coef[SDmaxCh];
412 +        SDError         ec;
413 +        FVECT           inVec;
414 +        const SDCDst    *cd;
415 +        double          d;
416 +        int             n;
417 +                                        /* check arguments */
418 +        if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL))
419 +                return SDEargument;
420 +                                        /* get cumulative distribution */
421 +        VCOPY(inVec, ioVec);
422 +        cd = (*sdc->func->getCDist)(inVec, sdc);
423 +        if (cd == NULL)
424 +                return SDEmemory;
425 +        if (cd->cTotal <= 1e-7) {       /* anything to sample? */
426 +                sv->spec = c_dfcolor;
427 +                sv->cieY = .0;
428 +                memset(ioVec, 0, 3*sizeof(double));
429 +                return SDEnone;
430 +        }
431 +        sv->cieY = cd->cTotal;
432 +                                        /* compute sample direction */
433 +        ec = (*sdc->func->sampCDist)(ioVec, randX, cd);
434 +        if (ec)
435 +                return ec;
436 +                                        /* get BSDF color */
437 +        n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
438 +        if (n <= 0) {
439 +                strcpy(SDerrorDetail, "BSDF sample value error");
440 +                return SDEinternal;
441 +        }
442 +        sv->spec = sdc->cspec[0];
443 +        d = coef[0];
444 +        while (--n) {
445 +                c_cmix(&sv->spec, d, &sv->spec, coef[n], &sdc->cspec[n]);
446 +                d += coef[n];
447 +        }
448 +                                        /* make sure everything is set */
449 +        c_ccvt(&sv->spec, C_CSXY+C_CSSPEC);
450 +        return SDEnone;
451 + }
452 +
453 + #define MS_MAXDIM       15
454 +
455 + /* Convert 1-dimensional random variable to N-dimensional */
456 + void
457 + SDmultiSamp(double t[], int n, double randX)
458 + {
459 +        unsigned        nBits;
460 +        double          scale;
461 +        bitmask_t       ndx, coord[MS_MAXDIM];
462 +        
463 +        while (n > MS_MAXDIM)           /* punt for higher dimensions */
464 +                t[--n] = rand()*(1./(RAND_MAX+.5));
465 +        nBits = (8*sizeof(bitmask_t) - 1) / n;
466 +        ndx = randX * (double)((bitmask_t)1 << (nBits*n));
467 +                                        /* get coordinate on Hilbert curve */
468 +        hilbert_i2c(n, nBits, ndx, coord);
469 +                                        /* convert back to [0,1) range */
470 +        scale = 1. / (double)((bitmask_t)1 << nBits);
471 +        while (n--)
472 +                t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5)));
473 + }
474 +
475 + #undef MS_MAXDIM
476 +
477 + /* Generate diffuse hemispherical sample */
478 + static void
479 + SDdiffuseSamp(FVECT outVec, int outFront, double randX)
480 + {
481 +                                        /* convert to position on hemisphere */
482 +        SDmultiSamp(outVec, 2, randX);
483 +        SDsquare2disk(outVec, outVec[0], outVec[1]);
484 +        outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
485 +        if (outVec[2] > 0)              /* a bit of paranoia */
486 +                outVec[2] = sqrt(outVec[2]);
487 +        if (!outFront)                  /* going out back? */
488 +                outVec[2] = -outVec[2];
489 + }
490 +
491 + /* Query projected solid angle coverage for non-diffuse BSDF direction */
492 + SDError
493 + SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2,
494 +                                int qflags, const SDData *sd)
495 + {
496 +        SDSpectralDF    *rdf, *tdf;
497 +        SDError         ec;
498 +        int             i;
499 +                                        /* check arguments */
500 +        if ((projSA == NULL) | (v1 == NULL) | (sd == NULL))
501 +                return SDEargument;
502 +                                        /* initialize extrema */
503 +        switch (qflags) {
504 +        case SDqueryMax:
505 +                projSA[0] = .0;
506 +                break;
507 +        case SDqueryMin+SDqueryMax:
508 +                projSA[1] = .0;
509 +                /* fall through */
510 +        case SDqueryMin:
511 +                projSA[0] = 10.;
512 +                break;
513 +        case 0:
514 +                return SDEargument;
515 +        }
516 +        if (v1[2] > 0)                  /* front surface query? */
517 +                rdf = sd->rf;
518 +        else
519 +                rdf = sd->rb;
520 +        tdf = sd->tf;
521 +        if (v2 != NULL)                 /* bidirectional? */
522 +                if (v1[2] > 0 ^ v2[2] > 0)
523 +                        rdf = NULL;
524 +                else
525 +                        tdf = NULL;
526 +        ec = SDEdata;                   /* run through components */
527 +        for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
528 +                ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2,
529 +                                                qflags, &rdf->comp[i]);
530 +                if (ec)
531 +                        return ec;
532 +        }
533 +        for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) {
534 +                ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2,
535 +                                                qflags, &tdf->comp[i]);
536 +                if (ec)
537 +                        return ec;
538 +        }
539 +        if (ec) {                       /* all diffuse? */
540 +                projSA[0] = M_PI;
541 +                if (qflags == SDqueryMin+SDqueryMax)
542 +                        projSA[1] = M_PI;
543 +        }
544 +        return SDEnone;
545 + }
546 +
547 + /* Return BSDF for the given incident and scattered ray vectors */
548 + SDError
549 + SDevalBSDF(SDValue *sv, const FVECT outVec, const FVECT inVec, const SDData *sd)
550 + {
551 +        int             inFront, outFront;
552 +        SDSpectralDF    *sdf;
553 +        float           coef[SDmaxCh];
554 +        int             nch, i;
555 +                                        /* check arguments */
556 +        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
557 +                return SDEargument;
558 +                                        /* whose side are we on? */
559 +        inFront = (inVec[2] > 0);
560 +        outFront = (outVec[2] > 0);
561 +                                        /* start with diffuse portion */
562 +        if (inFront & outFront) {
563 +                *sv = sd->rLambFront;
564 +                sdf = sd->rf;
565 +        } else if (!(inFront | outFront)) {
566 +                *sv = sd->rLambBack;
567 +                sdf = sd->rb;
568 +        } else /* inFront ^ outFront */ {
569 +                *sv = sd->tLamb;
570 +                sdf = sd->tf;
571 +        }
572 +        sv->cieY *= 1./M_PI;
573 +                                        /* add non-diffuse components */
574 +        i = (sdf != NULL) ? sdf->ncomp : 0;
575 +        while (i-- > 0) {
576 +                nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
577 +                                                        &sdf->comp[i]);
578 +                while (nch-- > 0) {
579 +                        c_cmix(&sv->spec, sv->cieY, &sv->spec,
580 +                                        coef[nch], &sdf->comp[i].cspec[nch]);
581 +                        sv->cieY += coef[nch];
582 +                }
583 +        }
584 +                                        /* make sure everything is set */
585 +        c_ccvt(&sv->spec, C_CSXY+C_CSSPEC);
586 +        return SDEnone;
587 + }
588 +
589 + /* Compute directional hemispherical scattering at this incident angle */
590 + double
591 + SDdirectHemi(const FVECT inVec, int sflags, const SDData *sd)
592 + {
593 +        double          hsum;
594 +        SDSpectralDF    *rdf;
595 +        const SDCDst    *cd;
596 +        int             i;
597 +                                        /* check arguments */
598 +        if ((inVec == NULL) | (sd == NULL))
599 +                return .0;
600 +                                        /* gather diffuse components */
601 +        if (inVec[2] > 0) {
602 +                hsum = sd->rLambFront.cieY;
603 +                rdf = sd->rf;
604 +        } else /* !inFront */ {
605 +                hsum = sd->rLambBack.cieY;
606 +                rdf = sd->rb;
607 +        }
608 +        if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR)
609 +                hsum = .0;
610 +        if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT)
611 +                hsum += sd->tLamb.cieY;
612 +                                        /* gather non-diffuse components */
613 +        i = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR &&
614 +                        rdf != NULL) ? rdf->ncomp : 0;
615 +        while (i-- > 0) {               /* non-diffuse reflection */
616 +                cd = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]);
617 +                if (cd != NULL)
618 +                        hsum += cd->cTotal;
619 +        }
620 +        i = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT &&
621 +                        sd->tf != NULL) ? sd->tf->ncomp : 0;
622 +        while (i-- > 0) {               /* non-diffuse transmission */
623 +                cd = (*sd->tf->comp[i].func->getCDist)(inVec, &sd->tf->comp[i]);
624 +                if (cd != NULL)
625 +                        hsum += cd->cTotal;
626 +        }
627 +        return hsum;
628 + }
629 +
630 + /* Sample BSDF direction based on the given random variable */
631 + SDError
632 + SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd)
633 + {
634 +        SDError         ec;
635 +        FVECT           inVec;
636 +        int             inFront;
637 +        SDSpectralDF    *rdf;
638 +        double          rdiff;
639 +        float           coef[SDmaxCh];
640 +        int             i, j, n, nr;
641 +        SDComponent     *sdc;
642 +        const SDCDst    **cdarr = NULL;
643 +                                        /* check arguments */
644 +        if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) |
645 +                        (randX < 0) | (randX >= 1.))
646 +                return SDEargument;
647 +                                        /* whose side are we on? */
648 +        VCOPY(inVec, ioVec);
649 +        inFront = (inVec[2] > 0);
650 +                                        /* remember diffuse portions */
651 +        if (inFront) {
652 +                *sv = sd->rLambFront;
653 +                rdf = sd->rf;
654 +        } else /* !inFront */ {
655 +                *sv = sd->rLambBack;
656 +                rdf = sd->rb;
657 +        }
658 +        if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR)
659 +                sv->cieY = .0;
660 +        rdiff = sv->cieY;
661 +        if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT)
662 +                sv->cieY += sd->tLamb.cieY;
663 +                                        /* gather non-diffuse components */
664 +        i = nr = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR &&
665 +                        rdf != NULL) ? rdf->ncomp : 0;
666 +        j = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT &&
667 +                        sd->tf != NULL) ? sd->tf->ncomp : 0;
668 +        n = i + j;
669 +        if (n > 0 && (cdarr = (const SDCDst **)malloc(n*sizeof(SDCDst *))) == NULL)
670 +                return SDEmemory;
671 +        while (j-- > 0) {               /* non-diffuse transmission */
672 +                cdarr[i+j] = (*sd->tf->comp[j].func->getCDist)(inVec, &sd->tf->comp[j]);
673 +                if (cdarr[i+j] == NULL) {
674 +                        free(cdarr);
675 +                        return SDEmemory;
676 +                }
677 +                sv->cieY += cdarr[i+j]->cTotal;
678 +        }
679 +        while (i-- > 0) {               /* non-diffuse reflection */
680 +                cdarr[i] = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]);
681 +                if (cdarr[i] == NULL) {
682 +                        free(cdarr);
683 +                        return SDEmemory;
684 +                }
685 +                sv->cieY += cdarr[i]->cTotal;
686 +        }
687 +        if (sv->cieY <= 1e-7) {         /* anything to sample? */
688 +                sv->cieY = .0;
689 +                memset(ioVec, 0, 3*sizeof(double));
690 +                return SDEnone;
691 +        }
692 +                                        /* scale random variable */
693 +        randX *= sv->cieY;
694 +                                        /* diffuse reflection? */
695 +        if (randX < rdiff) {
696 +                SDdiffuseSamp(ioVec, inFront, randX/rdiff);
697 +                goto done;
698 +        }
699 +        randX -= rdiff;
700 +                                        /* diffuse transmission? */
701 +        if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
702 +                if (randX < sd->tLamb.cieY) {
703 +                        sv->spec = sd->tLamb.spec;
704 +                        SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY);
705 +                        goto done;
706 +                }
707 +                randX -= sd->tLamb.cieY;
708 +        }
709 +                                        /* else one of cumulative dist. */
710 +        for (i = 0; i < n && randX < cdarr[i]->cTotal; i++)
711 +                randX -= cdarr[i]->cTotal;
712 +        if (i >= n)
713 +                return SDEinternal;
714 +                                        /* compute sample direction */
715 +        sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
716 +        ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]);
717 +        if (ec)
718 +                return ec;
719 +                                        /* compute color */
720 +        j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc);
721 +        if (j <= 0) {
722 +                sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
723 +                                sd->name);
724 +                return SDEinternal;
725 +        }
726 +        sv->spec = sdc->cspec[0];
727 +        rdiff = coef[0];
728 +        while (--j) {
729 +                c_cmix(&sv->spec, rdiff, &sv->spec, coef[j], &sdc->cspec[j]);
730 +                rdiff += coef[j];
731 +        }
732 + done:
733 +        if (cdarr != NULL)
734 +                free(cdarr);
735 +                                        /* make sure everything is set */
736 +        c_ccvt(&sv->spec, C_CSXY+C_CSSPEC);
737 +        return SDEnone;
738 + }
739 +
740 + /* Compute World->BSDF transform from surface normal and up (Y) vector */
741 + SDError
742 + SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const FVECT uVec)
743 + {
744 +        if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
745 +                return SDEargument;
746 +        VCOPY(vMtx[2], sNrm);
747 +        if (normalize(vMtx[2]) == 0)
748 +                return SDEargument;
749 +        fcross(vMtx[0], uVec, vMtx[2]);
750 +        if (normalize(vMtx[0]) == 0)
751 +                return SDEargument;
752 +        fcross(vMtx[1], vMtx[2], vMtx[0]);
753 +        return SDEnone;
754 + }
755 +
756 + /* Compute inverse transform */
757 + SDError
758 + SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
759 + {
760 +        RREAL   mTmp[3][3];
761 +        double  d;
762 +
763 +        if ((iMtx == NULL) | (vMtx == NULL))
764 +                return SDEargument;
765 +                                        /* compute determinant */
766 +        mTmp[0][0] = vMtx[2][2]*vMtx[1][1] - vMtx[2][1]*vMtx[1][2];
767 +        mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
768 +        mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
769 +        d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
770 +        if (d == 0) {
771 +                strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
772 +                return SDEargument;
773 +        }
774 +        d = 1./d;                       /* invert matrix */
775 +        mTmp[0][0] *= d; mTmp[0][1] *= d; mTmp[0][2] *= d;
776 +        mTmp[1][0] = d*(vMtx[2][0]*vMtx[1][2] - vMtx[2][2]*vMtx[1][0]);
777 +        mTmp[1][1] = d*(vMtx[2][2]*vMtx[0][0] - vMtx[2][0]*vMtx[0][2]);
778 +        mTmp[1][2] = d*(vMtx[1][0]*vMtx[0][2] - vMtx[1][2]*vMtx[0][0]);
779 +        mTmp[2][0] = d*(vMtx[2][1]*vMtx[1][0] - vMtx[2][0]*vMtx[1][1]);
780 +        mTmp[2][1] = d*(vMtx[2][0]*vMtx[0][1] - vMtx[2][1]*vMtx[0][0]);
781 +        mTmp[2][2] = d*(vMtx[1][1]*vMtx[0][0] - vMtx[1][0]*vMtx[0][1]);
782 +        memcpy(iMtx, mTmp, sizeof(mTmp));
783 +        return SDEnone;
784 + }
785 +
786 + /* Transform and normalize direction (column) vector */
787 + SDError
788 + SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT inpVec)
789 + {
790 +        FVECT   vTmp;
791 +
792 +        if ((resVec == NULL) | (inpVec == NULL))
793 +                return SDEargument;
794 +        if (vMtx == NULL) {             /* assume they just want to normalize */
795 +                if (resVec != inpVec)
796 +                        VCOPY(resVec, inpVec);
797 +                return (normalize(resVec) > 0) ? SDEnone : SDEargument;
798 +        }
799 +        vTmp[0] = DOT(vMtx[0], inpVec);
800 +        vTmp[1] = DOT(vMtx[1], inpVec);
801 +        vTmp[2] = DOT(vMtx[2], inpVec);
802 +        if (normalize(vTmp) == 0)
803 +                return SDEargument;
804 +        VCOPY(resVec, vTmp);
805 +        return SDEnone;
806 + }
807 +
808 + /*################################################################*/
809 + /*######### DEPRECATED ROUTINES AWAITING PERMANENT REMOVAL #######*/
810 +
811 + /*
812   * Routines for handling BSDF data
813   */
814  
815   #include "standard.h"
9 #include "bsdf.h"
816   #include "paths.h"
11 #include "ezxml.h"
12 #include <ctype.h>
817  
818   #define MAXLATS         46              /* maximum number of latitudes */
819  
# Line 23 | Line 827 | typedef struct {
827          }       lat[MAXLATS+1];         /* latitudes */
828   } ANGLE_BASIS;
829  
830 < #define MAXABASES       3               /* limit on defined bases */
830 > #define MAXABASES       7               /* limit on defined bases */
831  
832   static ANGLE_BASIS      abase_list[MAXABASES] = {
833          {
# Line 61 | Line 865 | static ANGLE_BASIS     abase_list[MAXABASES] = {
865  
866   static int      nabases = 3;    /* current number of defined bases */
867  
868 + #define  FEQ(a,b)       ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6)
869  
870   static int
871 + fequal(double a, double b)
872 + {
873 +        if (b != 0)
874 +                a = a/b - 1.;
875 +        return((a <= 1e-6) & (a >= -1e-6));
876 + }
877 +
878 + /* Returns the name of the given tag */
879 + #ifdef ezxml_name
880 + #undef ezxml_name
881 + static char *
882 + ezxml_name(ezxml_t xml)
883 + {
884 +        if (xml == NULL)
885 +                return(NULL);
886 +        return(xml->name);
887 + }
888 + #endif
889 +
890 + /* Returns the given tag's character content or empty string if none */
891 + #ifdef ezxml_txt
892 + #undef ezxml_txt
893 + static char *
894 + ezxml_txt(ezxml_t xml)
895 + {
896 +        if (xml == NULL)
897 +                return("");
898 +        return(xml->txt);
899 + }
900 + #endif
901 +
902 +
903 + static int
904   ab_getvec(              /* get vector for this angle basis index */
905          FVECT v,
906          int ndx,
# Line 95 | Line 933 | ab_getndx(             /* get index corresponding to the given ve
933   {
934          ANGLE_BASIS  *ab = (ANGLE_BASIS *)p;
935          int     li, ndx;
936 <        double  pol, azi, d;
936 >        double  pol, azi;
937  
938          if ((v[2] < -1.0) | (v[2] > 1.0))
939                  return(-1);
# Line 174 | Line 1012 | ab_getndxR(            /* get index corresponding to the reverse
1012  
1013  
1014   static void
1015 + load_angle_basis(       /* load custom BSDF angle basis */
1016 +        ezxml_t wab
1017 + )
1018 + {
1019 +        char    *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName"));
1020 +        ezxml_t wbb;
1021 +        int     i;
1022 +        
1023 +        if (!abname || !*abname)
1024 +                return;
1025 +        for (i = nabases; i--; )
1026 +                if (!strcasecmp(abname, abase_list[i].name))
1027 +                        return;         /* assume it's the same */
1028 +        if (nabases >= MAXABASES)
1029 +                error(INTERNAL, "too many angle bases");
1030 +        strcpy(abase_list[nabases].name, abname);
1031 +        abase_list[nabases].nangles = 0;
1032 +        for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock");
1033 +                        wbb != NULL; i++, wbb = wbb->next) {
1034 +                if (i >= MAXLATS)
1035 +                        error(INTERNAL, "too many latitudes in custom basis");
1036 +                abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt(
1037 +                                ezxml_child(ezxml_child(wbb,
1038 +                                        "ThetaBounds"), "UpperTheta")));
1039 +                if (!i)
1040 +                        abase_list[nabases].lat[i].tmin =
1041 +                                        -abase_list[nabases].lat[i+1].tmin;
1042 +                else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb,
1043 +                                        "ThetaBounds"), "LowerTheta"))),
1044 +                                abase_list[nabases].lat[i].tmin))
1045 +                        error(WARNING, "theta values disagree in custom basis");
1046 +                abase_list[nabases].nangles +=
1047 +                        abase_list[nabases].lat[i].nphis =
1048 +                                atoi(ezxml_txt(ezxml_child(wbb, "nPhis")));
1049 +        }
1050 +        abase_list[nabases++].lat[i].nphis = 0;
1051 + }
1052 +
1053 +
1054 + static void
1055 + load_geometry(          /* load geometric dimensions and description (if any) */
1056 +        struct BSDF_data *dp,
1057 +        ezxml_t wdb
1058 + )
1059 + {
1060 +        ezxml_t         geom;
1061 +        double          cfact;
1062 +        const char      *fmt, *mgfstr;
1063 +
1064 +        dp->dim[0] = dp->dim[1] = dp->dim[2] = 0;
1065 +        dp->mgf = NULL;
1066 +        if ((geom = ezxml_child(wdb, "Width")) != NULL)
1067 +                dp->dim[0] = atof(ezxml_txt(geom)) *
1068 +                                to_meters(ezxml_attr(geom, "unit"));
1069 +        if ((geom = ezxml_child(wdb, "Height")) != NULL)
1070 +                dp->dim[1] = atof(ezxml_txt(geom)) *
1071 +                                to_meters(ezxml_attr(geom, "unit"));
1072 +        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
1073 +                dp->dim[2] = atof(ezxml_txt(geom)) *
1074 +                                to_meters(ezxml_attr(geom, "unit"));
1075 +        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
1076 +                        (mgfstr = ezxml_txt(geom)) == NULL)
1077 +                return;
1078 +        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
1079 +                        strcasecmp(fmt, "MGF")) {
1080 +                sprintf(errmsg, "unrecognized geometry format '%s'", fmt);
1081 +                error(WARNING, errmsg);
1082 +                return;
1083 +        }
1084 +        cfact = to_meters(ezxml_attr(geom, "unit"));
1085 +        dp->mgf = (char *)malloc(strlen(mgfstr)+32);
1086 +        if (dp->mgf == NULL)
1087 +                error(SYSTEM, "out of memory in load_geometry");
1088 +        if (cfact < 0.99 || cfact > 1.01)
1089 +                sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
1090 +        else
1091 +                strcpy(dp->mgf, mgfstr);
1092 + }
1093 +
1094 +
1095 + static void
1096   load_bsdf_data(         /* load BSDF distribution for this wavelength */
1097          struct BSDF_data *dp,
1098          ezxml_t wdb
# Line 184 | Line 1103 | load_bsdf_data(                /* load BSDF distribution for this wa
1103          char  *sdata;
1104          int  i;
1105          
1106 <        if ((cbasis == NULL) | (rbasis == NULL)) {
1106 >        if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) {
1107                  error(WARNING, "missing column/row basis for BSDF");
1108                  return;
1109          }
191        /* XXX need to add routines for loading in foreign bases */
1110          for (i = nabases; i--; )
1111 <                if (!strcmp(cbasis, abase_list[i].name)) {
1111 >                if (!strcasecmp(cbasis, abase_list[i].name)) {
1112                          dp->ninc = abase_list[i].nangles;
1113                          dp->ib_priv = (void *)&abase_list[i];
1114                          dp->ib_vec = ab_getvecR;
# Line 199 | Line 1117 | load_bsdf_data(                /* load BSDF distribution for this wa
1117                          break;
1118                  }
1119          if (i < 0) {
1120 <                sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis);
1120 >                sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis);
1121                  error(WARNING, errmsg);
1122                  return;
1123          }
1124          for (i = nabases; i--; )
1125 <                if (!strcmp(rbasis, abase_list[i].name)) {
1125 >                if (!strcasecmp(rbasis, abase_list[i].name)) {
1126                          dp->nout = abase_list[i].nangles;
1127                          dp->ob_priv = (void *)&abase_list[i];
1128                          dp->ob_vec = ab_getvec;
# Line 213 | Line 1131 | load_bsdf_data(                /* load BSDF distribution for this wa
1131                          break;
1132                  }
1133          if (i < 0) {
1134 <                sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis);
1134 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", rbasis);
1135                  error(WARNING, errmsg);
1136                  return;
1137          }
1138                                  /* read BSDF data */
1139          sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData"));
1140 <        if (sdata == NULL) {
1140 >        if (!sdata || !*sdata) {
1141                  error(WARNING, "missing BSDF ScatteringData");
1142                  return;
1143          }
# Line 243 | Line 1161 | load_bsdf_data(                /* load BSDF distribution for this wa
1161                  sdata++;
1162          if (*sdata) {
1163                  sprintf(errmsg, "%d extra characters after BSDF ScatteringData",
1164 <                                strlen(sdata));
1164 >                                (int)strlen(sdata));
1165                  error(WARNING, errmsg);
1166          }
1167   }
# Line 255 | Line 1173 | check_bsdf_data(       /* check that BSDF data is sane */
1173   )
1174   {
1175          double          *omega_iarr, *omega_oarr;
1176 <        double          dom, contrib, hemi_total;
1176 >        double          dom, hemi_total, full_total;
1177          int             nneg;
1178          FVECT           v;
1179          int             i, o;
# Line 270 | Line 1188 | check_bsdf_data(       /* check that BSDF data is sane */
1188          hemi_total = .0;
1189          for (i = dp->ninc; i--; ) {
1190                  dom = getBSDF_incohm(dp,i);
1191 <                if (dom <= .0) {
1191 >                if (dom <= 0) {
1192                          error(WARNING, "zero/negative incoming solid angle");
1193                          continue;
1194                  }
# Line 293 | Line 1211 | check_bsdf_data(       /* check that BSDF data is sane */
1211          hemi_total = .0;
1212          for (o = dp->nout; o--; ) {
1213                  dom = getBSDF_outohm(dp,o);
1214 <                if (dom <= .0) {
1214 >                if (dom <= 0) {
1215                          error(WARNING, "zero/negative outgoing solid angle");
1216                          continue;
1217                  }
# Line 317 | Line 1235 | check_bsdf_data(       /* check that BSDF data is sane */
1235                  hemi_total = .0;
1236                  for (o = dp->nout; o--; ) {
1237                          double  f = BSDF_value(dp,i,o);
1238 <                        if (f >= .0)
1238 >                        if (f >= 0)
1239                                  hemi_total += f*omega_oarr[o];
1240                          else {
1241                                  nneg += (f < -FTINY);
1242                                  BSDF_value(dp,i,o) = .0f;
1243                          }
1244                  }
1245 <                if (hemi_total > 1.02) {
1245 >                if (hemi_total > 1.01) {
1246                          sprintf(errmsg,
1247                          "incoming BSDF direction %d passes %.1f%% of light",
1248                                          i, 100.*hemi_total);
# Line 335 | Line 1253 | check_bsdf_data(       /* check that BSDF data is sane */
1253                  sprintf(errmsg, "%d negative BSDF values (ignored)", nneg);
1254                  error(WARNING, errmsg);
1255          }
1256 <                                        /* reverse roles and check again */
1256 >        full_total = .0;                /* reverse roles and check again */
1257          for (o = 0; o < dp->nout; o++) {
1258                  hemi_total = .0;
1259                  for (i = dp->ninc; i--; )
1260                          hemi_total += BSDF_value(dp,i,o) * omega_iarr[i];
1261  
1262 <                if (hemi_total > 1.02) {
1262 >                if (hemi_total > 1.01) {
1263                          sprintf(errmsg,
1264                          "outgoing BSDF direction %d collects %.1f%% of light",
1265                                          o, 100.*hemi_total);
1266                          error(WARNING, errmsg);
1267                  }
1268 +                full_total += hemi_total*omega_oarr[o];
1269          }
1270 +        full_total /= PI;
1271 +        if (full_total > 1.00001) {
1272 +                sprintf(errmsg, "BSDF transfers %.4f%% of light",
1273 +                                100.*full_total);
1274 +                error(WARNING, errmsg);
1275 +        }
1276          free(omega_iarr); free(omega_oarr);
1277          return(1);
1278   }
1279  
1280 +
1281   struct BSDF_data *
1282   load_BSDF(              /* load BSDF data from file */
1283          char *fname
# Line 388 | Line 1314 | load_BSDF(             /* load BSDF data from file */
1314                  return(NULL);
1315          }
1316          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
1317 +        if (strcasecmp(ezxml_txt(ezxml_child(ezxml_child(wtl,
1318 +                        "DataDefinition"), "IncidentDataStructure")),
1319 +                        "Columns")) {
1320 +                sprintf(errmsg,
1321 +                        "BSDF \"%s\": unsupported IncidentDataStructure",
1322 +                                path);
1323 +                error(WARNING, errmsg);
1324 +                ezxml_free(fl);
1325 +                return(NULL);
1326 +        }              
1327 +        load_angle_basis(ezxml_child(ezxml_child(wtl,
1328 +                                "DataDefinition"), "AngleBasis"));
1329          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1330 +        load_geometry(dp, ezxml_child(wtl, "Material"));
1331          for (wld = ezxml_child(wtl, "WavelengthData");
1332                                  wld != NULL; wld = wld->next) {
1333 <                if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
1333 >                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1334 >                                "Visible"))
1335                          continue;
1336 <                wdb = ezxml_child(wld, "WavelengthDataBlock");
1337 <                if (wdb == NULL) continue;
1338 <                if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")),
1336 >                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1337 >                                wdb != NULL; wdb = wdb->next)
1338 >                        if (!strcasecmp(ezxml_txt(ezxml_child(wdb,
1339 >                                        "WavelengthDataDirection")),
1340                                          "Transmission Front"))
1341 <                        continue;
1342 <                load_bsdf_data(dp, wdb);        /* load front BTDF */
1343 <                break;                          /* ignore the rest */
1341 >                                break;
1342 >                if (wdb != NULL) {              /* load front BTDF */
1343 >                        load_bsdf_data(dp, wdb);
1344 >                        break;                  /* ignore the rest */
1345 >                }
1346          }
1347          ezxml_free(fl);                         /* done with XML file */
1348          if (!check_bsdf_data(dp)) {
# Line 419 | Line 1362 | free_BSDF(             /* free BSDF data structure */
1362   {
1363          if (b == NULL)
1364                  return;
1365 +        if (b->mgf != NULL)
1366 +                free(b->mgf);
1367          if (b->bsdf != NULL)
1368                  free(b->bsdf);
1369          free(b);
# Line 475 | Line 1420 | r_BSDF_outvec(         /* compute random output vector at giv
1420   }
1421  
1422  
478 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
479
1423   static int
1424   addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */
1425          char *xfarg[],
# Line 527 | Line 1470 | int
1470   getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */
1471          MAT4 xm,
1472          FVECT nrm,
1473 <        UpDir ud
1473 >        UpDir ud,
1474 >        char *xfbuf
1475   )
1476   {
1477          char    *xfargs[7];
1478          XF      myxf;
1479          FVECT   updir, xdest, ydest;
1480 +        int     i;
1481  
1482          updir[0] = updir[1] = updir[2] = 0.;
1483          switch (ud) {
# Line 563 | Line 1508 | getBSDF_xfm(           /* compute BSDF orient. -> world orient.
1508          fcross(ydest, nrm, xdest);
1509          xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs);
1510          copymat4(xm, myxf.xfm);
1511 +        if (xfbuf == NULL)
1512 +                return(1);
1513 +                                /* return xf arguments as well */
1514 +        for (i = 0; xfargs[i] != NULL; i++) {
1515 +                *xfbuf++ = ' ';
1516 +                strcpy(xfbuf, xfargs[i]);
1517 +                while (*xfbuf) ++xfbuf;
1518 +        }
1519          return(1);
1520   }
1521 +
1522 + /*######### END DEPRECATED ROUTINES #######*/
1523 + /*################################################################*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines