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.9 by greg, Mon Jan 3 19:27:00 2011 UTC vs.
Revision 2.20 by greg, Mon Apr 11 03:47:46 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 + #include <stdio.h>
14 + #include <stdlib.h>
15 + #include <math.h>
16 + #include "ezxml.h"
17 + #include "hilbert.h"
18 + #include "bsdf.h"
19 + #include "bsdf_m.h"
20 + #include "bsdf_t.h"
21 +
22 + /* English ASCII strings corresponding to ennumerated errors */
23 + const char              *SDerrorEnglish[] = {
24 +                                "No error",
25 +                                "Memory error",
26 +                                "File input/output error",
27 +                                "File format error",
28 +                                "Illegal argument",
29 +                                "Invalid data",
30 +                                "Unsupported feature",
31 +                                "Internal program error",
32 +                                "Unknown error"
33 +                        };
34 +
35 + /* Additional information on last error (ASCII English) */
36 + char                    SDerrorDetail[256];
37 +
38 + /* Cache of loaded BSDFs */
39 + struct SDCache_s        *SDcacheList = NULL;
40 +
41 + /* Retain BSDFs in cache list */
42 + int                     SDretainSet = SDretainNone;
43 +
44 + /* Report any error to the indicated stream (in English) */
45 + SDError
46 + SDreportEnglish(SDError ec, FILE *fp)
47 + {
48 +        if (fp == NULL)
49 +                return ec;
50 +        if (!ec)
51 +                return SDEnone;
52 +        fputs(SDerrorEnglish[ec], fp);
53 +        if (SDerrorDetail[0]) {
54 +                fputs(": ", fp);
55 +                fputs(SDerrorDetail, fp);
56 +        }
57 +        fputc('\n', fp);
58 +        if (fp != stderr)
59 +                fflush(fp);
60 +        return ec;
61 + }
62 +
63 + static double
64 + to_meters(              /* return factor to convert given unit to meters */
65 +        const char *unit
66 + )
67 + {
68 +        if (unit == NULL) return(1.);           /* safe assumption? */
69 +        if (!strcasecmp(unit, "Meter")) return(1.);
70 +        if (!strcasecmp(unit, "Foot")) return(.3048);
71 +        if (!strcasecmp(unit, "Inch")) return(.0254);
72 +        if (!strcasecmp(unit, "Centimeter")) return(.01);
73 +        if (!strcasecmp(unit, "Millimeter")) return(.001);
74 +        sprintf(SDerrorDetail, "Unknown dimensional unit '%s'", unit);
75 +        return(-1.);
76 + }
77 +
78 + /* Load geometric dimensions and description (if any) */
79 + static SDError
80 + SDloadGeometry(SDData *sd, ezxml_t wdb)
81 + {
82 +        ezxml_t         geom;
83 +        double          cfact;
84 +        const char      *fmt, *mgfstr;
85 +
86 +        if (wdb == NULL)                /* no geometry section? */
87 +                return SDEnone;
88 +        sd->dim[0] = sd->dim[1] = sd->dim[2] = .0;
89 +        if ((geom = ezxml_child(wdb, "Width")) != NULL)
90 +                sd->dim[0] = atof(ezxml_txt(geom)) *
91 +                                to_meters(ezxml_attr(geom, "unit"));
92 +        if ((geom = ezxml_child(wdb, "Height")) != NULL)
93 +                sd->dim[1] = atof(ezxml_txt(geom)) *
94 +                                to_meters(ezxml_attr(geom, "unit"));
95 +        if ((geom = ezxml_child(wdb, "Thickness")) != NULL)
96 +                sd->dim[2] = atof(ezxml_txt(geom)) *
97 +                                to_meters(ezxml_attr(geom, "unit"));
98 +        if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) {
99 +                sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name);
100 +                return SDEdata;
101 +        }
102 +        if ((geom = ezxml_child(wdb, "Geometry")) == NULL ||
103 +                        (mgfstr = ezxml_txt(geom)) == NULL)
104 +                return SDEnone;
105 +        if ((fmt = ezxml_attr(geom, "format")) != NULL &&
106 +                        strcasecmp(fmt, "MGF")) {
107 +                sprintf(SDerrorDetail,
108 +                        "Unrecognized geometry format '%s' in \"%s\"",
109 +                                        fmt, sd->name);
110 +                return SDEsupport;
111 +        }
112 +        cfact = to_meters(ezxml_attr(geom, "unit"));
113 +        sd->mgf = (char *)malloc(strlen(mgfstr)+32);
114 +        if (sd->mgf == NULL) {
115 +                strcpy(SDerrorDetail, "Out of memory in SDloadGeometry");
116 +                return SDEmemory;
117 +        }
118 +        if (cfact < 0.99 || cfact > 1.01)
119 +                sprintf(sd->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr);
120 +        else
121 +                strcpy(sd->mgf, mgfstr);
122 +        return SDEnone;
123 + }
124 +
125 + /* Load a BSDF struct from the given file (free first and keep name) */
126 + SDError
127 + SDloadFile(SDData *sd, const char *fname)
128 + {
129 +        SDError         lastErr;
130 +        ezxml_t         fl, wtl;
131 +
132 +        if ((sd == NULL) | (fname == NULL || !*fname))
133 +                return SDEargument;
134 +                                /* free old data, keeping name */
135 +        SDfreeBSDF(sd);
136 +                                /* parse XML file */
137 +        fl = ezxml_parse_file(fname);
138 +        if (fl == NULL) {
139 +                sprintf(SDerrorDetail, "Cannot open BSDF \"%s\"", fname);
140 +                return SDEfile;
141 +        }
142 +        if (ezxml_error(fl)[0]) {
143 +                sprintf(SDerrorDetail, "BSDF \"%s\" %s", fname, ezxml_error(fl));
144 +                ezxml_free(fl);
145 +                return SDEformat;
146 +        }
147 +        if (strcmp(ezxml_name(fl), "WindowElement")) {
148 +                sprintf(SDerrorDetail,
149 +                        "BSDF \"%s\": top level node not 'WindowElement'",
150 +                                sd->name);
151 +                ezxml_free(fl);
152 +                return SDEformat;
153 +        }
154 +        wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
155 +        if (wtl == NULL) {
156 +                sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'",
157 +                                sd->name);
158 +                ezxml_free(fl);
159 +                return SDEformat;
160 +        }
161 +                                /* load geometry if present */
162 +        lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material"));
163 +        if (lastErr)
164 +                return lastErr;
165 +                                /* try loading variable resolution data */
166 +        lastErr = SDloadTre(sd, wtl);
167 +                                /* check our result */
168 +        switch (lastErr) {
169 +        case SDEformat:
170 +        case SDEdata:
171 +        case SDEsupport:        /* possibly we just tried the wrong format */
172 +                lastErr = SDloadMtx(sd, wtl);
173 +                break;
174 +        default:                /* variable res. OK else serious error */
175 +                break;
176 +        }
177 +                                /* done with XML file */
178 +        ezxml_free(fl);
179 +        
180 +        if (lastErr) {          /* was there a load error? */
181 +                SDfreeBSDF(sd);
182 +                return lastErr;
183 +        }
184 +                                /* remove any insignificant components */
185 +        if (sd->rf != NULL && sd->rf->maxHemi <= .001) {
186 +                SDfreeSpectralDF(sd->rf); sd->rf = NULL;
187 +        }
188 +        if (sd->rb != NULL && sd->rb->maxHemi <= .001) {
189 +                SDfreeSpectralDF(sd->rb); sd->rb = NULL;
190 +        }
191 +        if (sd->tf != NULL && sd->tf->maxHemi <= .001) {
192 +                SDfreeSpectralDF(sd->tf); sd->tf = NULL;
193 +        }
194 +                                /* return success */
195 +        return SDEnone;
196 + }
197 +
198 + /* Allocate new spectral distribution function */
199 + SDSpectralDF *
200 + SDnewSpectralDF(int nc)
201 + {
202 +        SDSpectralDF    *df;
203 +        
204 +        if (nc <= 0) {
205 +                strcpy(SDerrorDetail, "Zero component spectral DF request");
206 +                return NULL;
207 +        }
208 +        df = (SDSpectralDF *)malloc(sizeof(SDSpectralDF) +
209 +                                        (nc-1)*sizeof(SDComponent));
210 +        if (df == NULL) {
211 +                sprintf(SDerrorDetail,
212 +                                "Cannot allocate %d component spectral DF", nc);
213 +                return NULL;
214 +        }
215 +        df->minProjSA = .0;
216 +        df->maxHemi = .0;
217 +        df->ncomp = nc;
218 +        memset(df->comp, 0, nc*sizeof(SDComponent));
219 +        return df;
220 + }
221 +
222 + /* Free cached cumulative distributions for BSDF component */
223 + void
224 + SDfreeCumulativeCache(SDSpectralDF *df)
225 + {
226 +        int     n;
227 +        SDCDst  *cdp;
228 +
229 +        if (df == NULL)
230 +                return;
231 +        for (n = df->ncomp; n-- > 0; )
232 +                while ((cdp = df->comp[n].cdList) != NULL) {
233 +                        df->comp[n].cdList = cdp->next;
234 +                        free(cdp);
235 +                }
236 + }
237 +
238 + /* Free a spectral distribution function */
239 + void
240 + SDfreeSpectralDF(SDSpectralDF *df)
241 + {
242 +        int     n;
243 +
244 +        if (df == NULL)
245 +                return;
246 +        SDfreeCumulativeCache(df);
247 +        for (n = df->ncomp; n-- > 0; )
248 +                (*df->comp[n].func->freeSC)(df->comp[n].dist);
249 +        free(df);
250 + }
251 +
252 + /* Shorten file path to useable BSDF name, removing suffix */
253 + void
254 + SDclipName(char *res, const char *fname)
255 + {
256 +        const char      *cp, *dot = NULL;
257 +        
258 +        for (cp = fname; *cp; cp++)
259 +                if (*cp == '.')
260 +                        dot = cp;
261 +        if ((dot == NULL) | (dot < fname+2))
262 +                dot = cp;
263 +        if (dot - fname >= SDnameLn)
264 +                fname = dot - SDnameLn + 1;
265 +        while (fname < dot)
266 +                *res++ = *fname++;
267 +        *res = '\0';
268 + }
269 +
270 + /* Initialize an unused BSDF struct (simply clears to zeroes) */
271 + void
272 + SDclearBSDF(SDData *sd, const char *fname)
273 + {
274 +        if (sd == NULL)
275 +                return;
276 +        memset(sd, 0, sizeof(SDData));
277 +        if (fname == NULL)
278 +                return;
279 +        SDclipName(sd->name, fname);
280 + }
281 +
282 + /* Free data associated with BSDF struct */
283 + void
284 + SDfreeBSDF(SDData *sd)
285 + {
286 +        if (sd == NULL)
287 +                return;
288 +        if (sd->mgf != NULL) {
289 +                free(sd->mgf);
290 +                sd->mgf = NULL;
291 +        }
292 +        if (sd->rf != NULL) {
293 +                SDfreeSpectralDF(sd->rf);
294 +                sd->rf = NULL;
295 +        }
296 +        if (sd->rb != NULL) {
297 +                SDfreeSpectralDF(sd->rb);
298 +                sd->rb = NULL;
299 +        }
300 +        if (sd->tf != NULL) {
301 +                SDfreeSpectralDF(sd->tf);
302 +                sd->tf = NULL;
303 +        }
304 +        sd->rLambFront.cieY = .0;
305 +        sd->rLambFront.spec.flags = 0;
306 +        sd->rLambBack.cieY = .0;
307 +        sd->rLambBack.spec.flags = 0;
308 +        sd->tLamb.cieY = .0;
309 +        sd->tLamb.spec.flags = 0;
310 + }
311 +
312 + /* Find writeable BSDF by name, or allocate new cache entry if absent */
313 + SDData *
314 + SDgetCache(const char *bname)
315 + {
316 +        struct SDCache_s        *sdl;
317 +        char                    sdnam[SDnameLn];
318 +
319 +        if (bname == NULL)
320 +                return NULL;
321 +
322 +        SDclipName(sdnam, bname);
323 +        for (sdl = SDcacheList; sdl != NULL; sdl = sdl->next)
324 +                if (!strcmp(sdl->bsdf.name, sdnam)) {
325 +                        sdl->refcnt++;
326 +                        return &sdl->bsdf;
327 +                }
328 +
329 +        sdl = (struct SDCache_s *)calloc(1, sizeof(struct SDCache_s));
330 +        if (sdl == NULL)
331 +                return NULL;
332 +
333 +        strcpy(sdl->bsdf.name, sdnam);
334 +        sdl->next = SDcacheList;
335 +        SDcacheList = sdl;
336 +
337 +        sdl->refcnt++;
338 +        return &sdl->bsdf;
339 + }
340 +
341 + /* Get loaded BSDF from cache (or load and cache it on first call) */
342 + /* Report any problem to stderr and return NULL on failure */
343 + const SDData *
344 + SDcacheFile(const char *fname)
345 + {
346 +        SDData          *sd;
347 +        SDError         ec;
348 +        
349 +        if (fname == NULL || !*fname)
350 +                return NULL;
351 +        SDerrorDetail[0] = '\0';
352 +        if ((sd = SDgetCache(fname)) == NULL) {
353 +                SDreportEnglish(SDEmemory, stderr);
354 +                return NULL;
355 +        }
356 +        if (!SDisLoaded(sd) && (ec = SDloadFile(sd, fname))) {
357 +                SDreportEnglish(ec, stderr);
358 +                SDfreeCache(sd);
359 +                return NULL;
360 +        }
361 +        return sd;
362 + }
363 +
364 + /* Free a BSDF from our cache (clear all if NULL) */
365 + void
366 + SDfreeCache(const SDData *sd)
367 + {
368 +        struct SDCache_s        *sdl, *sdLast = NULL;
369 +
370 +        if (sd == NULL) {               /* free entire list */
371 +                while ((sdl = SDcacheList) != NULL) {
372 +                        SDcacheList = sdl->next;
373 +                        SDfreeBSDF(&sdl->bsdf);
374 +                        free(sdl);
375 +                }
376 +                return;
377 +        }
378 +        for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next)
379 +                if (&sdl->bsdf == sd)
380 +                        break;
381 +        if (sdl == NULL || --sdl->refcnt)
382 +                return;                 /* missing or still in use */
383 +                                        /* keep unreferenced data? */
384 +        if (SDisLoaded(sd) && SDretainSet) {
385 +                if (SDretainSet == SDretainAll)
386 +                        return;         /* keep everything */
387 +                                        /* else free cumulative data */
388 +                SDfreeCumulativeCache(sd->rf);
389 +                SDfreeCumulativeCache(sd->rb);
390 +                SDfreeCumulativeCache(sd->tf);
391 +                return;
392 +        }
393 +                                        /* remove from list and free */
394 +        if (sdLast == NULL)
395 +                SDcacheList = sdl->next;
396 +        else
397 +                sdLast->next = sdl->next;
398 +        SDfreeBSDF(&sdl->bsdf);
399 +        free(sdl);
400 + }
401 +
402 + /* Sample an individual BSDF component */
403 + SDError
404 + SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec,
405 +                        double randX, SDComponent *sdc)
406 + {
407 +        float           coef[SDmaxCh];
408 +        SDError         ec;
409 +        const SDCDst    *cd;
410 +        double          d;
411 +        int             n;
412 +                                        /* check arguments */
413 +        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL))
414 +                return SDEargument;
415 +                                        /* get cumulative distribution */
416 +        cd = (*sdc->func->getCDist)(inVec, sdc);
417 +        if (cd == NULL)
418 +                return SDEmemory;
419 +        if (cd->cTotal <= 1e-7) {       /* anything to sample? */
420 +                sv->spec = c_dfcolor;
421 +                sv->cieY = .0;
422 +                memset(outVec, 0, 3*sizeof(double));
423 +                return SDEnone;
424 +        }
425 +        sv->cieY = cd->cTotal;
426 +                                        /* compute sample direction */
427 +        ec = (*sdc->func->sampCDist)(outVec, randX, cd);
428 +        if (ec)
429 +                return ec;
430 +                                        /* get BSDF color */
431 +        n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
432 +        if (n <= 0) {
433 +                strcpy(SDerrorDetail, "BSDF sample value error");
434 +                return SDEinternal;
435 +        }
436 +        sv->spec = sdc->cspec[0];
437 +        d = coef[0];
438 +        while (--n) {
439 +                c_cmix(&sv->spec, d, &sv->spec, coef[n], &sdc->cspec[n]);
440 +                d += coef[n];
441 +        }
442 +                                        /* make sure everything is set */
443 +        c_ccvt(&sv->spec, C_CSXY+C_CSSPEC);
444 +        return SDEnone;
445 + }
446 +
447 + #define MS_MAXDIM       15
448 +
449 + /* Convert 1-dimensional random variable to N-dimensional */
450 + void
451 + SDmultiSamp(double t[], int n, double randX)
452 + {
453 +        unsigned        nBits;
454 +        double          scale;
455 +        bitmask_t       ndx, coord[MS_MAXDIM];
456 +        
457 +        while (n > MS_MAXDIM)           /* punt for higher dimensions */
458 +                t[--n] = rand()*(1./(RAND_MAX+.5));
459 +        nBits = (8*sizeof(bitmask_t) - 1) / n;
460 +        ndx = randX * (double)((bitmask_t)1 << (nBits*n));
461 +                                        /* get coordinate on Hilbert curve */
462 +        hilbert_i2c(n, nBits, ndx, coord);
463 +                                        /* convert back to [0,1) range */
464 +        scale = 1. / (double)((bitmask_t)1 << nBits);
465 +        while (n--)
466 +                t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5)));
467 + }
468 +
469 + #undef MS_MAXDIM
470 +
471 + /* Generate diffuse hemispherical sample */
472 + static void
473 + SDdiffuseSamp(FVECT outVec, int outFront, double randX)
474 + {
475 +                                        /* convert to position on hemisphere */
476 +        SDmultiSamp(outVec, 2, randX);
477 +        SDsquare2disk(outVec, outVec[0], outVec[1]);
478 +        outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1];
479 +        if (outVec[2] > .0)             /* a bit of paranoia */
480 +                outVec[2] = sqrt(outVec[2]);
481 +        if (!outFront)                  /* going out back? */
482 +                outVec[2] = -outVec[2];
483 + }
484 +
485 + /* Query projected solid angle coverage for non-diffuse BSDF direction */
486 + SDError
487 + SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd)
488 + {
489 +        SDSpectralDF    *rdf;
490 +        SDError         ec;
491 +        int             i;
492 +                                        /* check arguments */
493 +        if ((projSA == NULL) | (vec == NULL) | (sd == NULL))
494 +                return SDEargument;
495 +                                        /* initialize extrema */
496 +        switch (qflags) {
497 +        case SDqueryMax:
498 +                projSA[0] = .0;
499 +                break;
500 +        case SDqueryMin+SDqueryMax:
501 +                projSA[1] = .0;
502 +                /* fall through */
503 +        case SDqueryMin:
504 +                projSA[0] = 10.;
505 +                break;
506 +        case 0:
507 +                return SDEargument;
508 +        }
509 +        if (vec[2] > .0)                /* front surface query? */
510 +                rdf = sd->rf;
511 +        else
512 +                rdf = sd->rb;
513 +        ec = SDEdata;                   /* run through components */
514 +        for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) {
515 +                ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags,
516 +                                                        rdf->comp[i].dist);
517 +                if (ec)
518 +                        return ec;
519 +        }
520 +        for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) {
521 +                ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags,
522 +                                                        sd->tf->comp[i].dist);
523 +                if (ec)
524 +                        return ec;
525 +        }
526 +        if (ec) {                       /* all diffuse? */
527 +                projSA[0] = M_PI;
528 +                if (qflags == SDqueryMin+SDqueryMax)
529 +                        projSA[1] = M_PI;
530 +        }
531 +        return SDEnone;
532 + }
533 +
534 + /* Return BSDF for the given incident and scattered ray vectors */
535 + SDError
536 + SDevalBSDF(SDValue *sv, const FVECT outVec, const FVECT inVec, const SDData *sd)
537 + {
538 +        int             inFront, outFront;
539 +        SDSpectralDF    *sdf;
540 +        float           coef[SDmaxCh];
541 +        int             nch, i;
542 +                                        /* check arguments */
543 +        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL))
544 +                return SDEargument;
545 +                                        /* whose side are we on? */
546 +        inFront = (inVec[2] > .0);
547 +        outFront = (outVec[2] > .0);
548 +                                        /* start with diffuse portion */
549 +        if (inFront & outFront) {
550 +                *sv = sd->rLambFront;
551 +                sdf = sd->rf;
552 +        } else if (!(inFront | outFront)) {
553 +                *sv = sd->rLambBack;
554 +                sdf = sd->rb;
555 +        } else /* inFront ^ outFront */ {
556 +                *sv = sd->tLamb;
557 +                sdf = sd->tf;
558 +        }
559 +        sv->cieY *= 1./M_PI;
560 +                                        /* add non-diffuse components */
561 +        i = (sdf != NULL) ? sdf->ncomp : 0;
562 +        while (i-- > 0) {
563 +                nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec,
564 +                                                        sdf->comp[i].dist);
565 +                while (nch-- > 0) {
566 +                        c_cmix(&sv->spec, sv->cieY, &sv->spec,
567 +                                        coef[nch], &sdf->comp[i].cspec[nch]);
568 +                        sv->cieY += coef[nch];
569 +                }
570 +        }
571 +                                        /* make sure everything is set */
572 +        c_ccvt(&sv->spec, C_CSXY+C_CSSPEC);
573 +        return SDEnone;
574 + }
575 +
576 + /* Compute directional hemispherical scattering at this incident angle */
577 + double
578 + SDdirectHemi(const FVECT inVec, int sflags, const SDData *sd)
579 + {
580 +        double          hsum;
581 +        SDSpectralDF    *rdf;
582 +        const SDCDst    *cd;
583 +        int             i;
584 +                                        /* check arguments */
585 +        if ((inVec == NULL) | (sd == NULL))
586 +                return .0;
587 +                                        /* gather diffuse components */
588 +        if (inVec[2] > .0) {
589 +                hsum = sd->rLambFront.cieY;
590 +                rdf = sd->rf;
591 +        } else /* !inFront */ {
592 +                hsum = sd->rLambBack.cieY;
593 +                rdf = sd->rb;
594 +        }
595 +        if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR)
596 +                hsum = .0;
597 +        if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT)
598 +                hsum += sd->tLamb.cieY;
599 +                                        /* gather non-diffuse components */
600 +        i = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR &&
601 +                        rdf != NULL) ? rdf->ncomp : 0;
602 +        while (i-- > 0) {               /* non-diffuse reflection */
603 +                cd = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]);
604 +                if (cd != NULL)
605 +                        hsum += cd->cTotal;
606 +        }
607 +        i = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT &&
608 +                        sd->tf != NULL) ? sd->tf->ncomp : 0;
609 +        while (i-- > 0) {               /* non-diffuse transmission */
610 +                cd = (*sd->tf->comp[i].func->getCDist)(inVec, &sd->tf->comp[i]);
611 +                if (cd != NULL)
612 +                        hsum += cd->cTotal;
613 +        }
614 +        return hsum;
615 + }
616 +
617 + /* Sample BSDF direction based on the given random variable */
618 + SDError
619 + SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec,
620 +                        double randX, int sflags, const SDData *sd)
621 + {
622 +        SDError         ec;
623 +        int             inFront;
624 +        SDSpectralDF    *rdf;
625 +        double          rdiff;
626 +        float           coef[SDmaxCh];
627 +        int             i, j, n, nr;
628 +        SDComponent     *sdc;
629 +        const SDCDst    **cdarr = NULL;
630 +                                        /* check arguments */
631 +        if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) |
632 +                        (randX < .0) | (randX >= 1.))
633 +                return SDEargument;
634 +                                        /* whose side are we on? */
635 +        inFront = (inVec[2] > .0);
636 +                                        /* remember diffuse portions */
637 +        if (inFront) {
638 +                *sv = sd->rLambFront;
639 +                rdf = sd->rf;
640 +        } else /* !inFront */ {
641 +                *sv = sd->rLambBack;
642 +                rdf = sd->rb;
643 +        }
644 +        if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR)
645 +                sv->cieY = .0;
646 +        rdiff = sv->cieY;
647 +        if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT)
648 +                sv->cieY += sd->tLamb.cieY;
649 +                                        /* gather non-diffuse components */
650 +        i = nr = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR &&
651 +                        rdf != NULL) ? rdf->ncomp : 0;
652 +        j = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT &&
653 +                        sd->tf != NULL) ? sd->tf->ncomp : 0;
654 +        n = i + j;
655 +        if (n > 0 && (cdarr = (const SDCDst **)malloc(n*sizeof(SDCDst *))) == NULL)
656 +                return SDEmemory;
657 +        while (j-- > 0) {               /* non-diffuse transmission */
658 +                cdarr[i+j] = (*sd->tf->comp[j].func->getCDist)(inVec, &sd->tf->comp[j]);
659 +                if (cdarr[i+j] == NULL) {
660 +                        free(cdarr);
661 +                        return SDEmemory;
662 +                }
663 +                sv->cieY += cdarr[i+j]->cTotal;
664 +        }
665 +        while (i-- > 0) {               /* non-diffuse reflection */
666 +                cdarr[i] = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]);
667 +                if (cdarr[i] == NULL) {
668 +                        free(cdarr);
669 +                        return SDEmemory;
670 +                }
671 +                sv->cieY += cdarr[i]->cTotal;
672 +        }
673 +        if (sv->cieY <= 1e-7) {         /* anything to sample? */
674 +                sv->cieY = .0;
675 +                memset(outVec, 0, 3*sizeof(double));
676 +                return SDEnone;
677 +        }
678 +                                        /* scale random variable */
679 +        randX *= sv->cieY;
680 +                                        /* diffuse reflection? */
681 +        if (randX < rdiff) {
682 +                SDdiffuseSamp(outVec, inFront, randX/rdiff);
683 +                goto done;
684 +        }
685 +        randX -= rdiff;
686 +                                        /* diffuse transmission? */
687 +        if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) {
688 +                if (randX < sd->tLamb.cieY) {
689 +                        sv->spec = sd->tLamb.spec;
690 +                        SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY);
691 +                        goto done;
692 +                }
693 +                randX -= sd->tLamb.cieY;
694 +        }
695 +                                        /* else one of cumulative dist. */
696 +        for (i = 0; i < n && randX < cdarr[i]->cTotal; i++)
697 +                randX -= cdarr[i]->cTotal;
698 +        if (i >= n)
699 +                return SDEinternal;
700 +                                        /* compute sample direction */
701 +        sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr];
702 +        ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]);
703 +        if (ec)
704 +                return ec;
705 +                                        /* compute color */
706 +        j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist);
707 +        if (j <= 0) {
708 +                sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
709 +                                sd->name);
710 +                return SDEinternal;
711 +        }
712 +        sv->spec = sdc->cspec[0];
713 +        rdiff = coef[0];
714 +        while (--j) {
715 +                c_cmix(&sv->spec, rdiff, &sv->spec, coef[j], &sdc->cspec[j]);
716 +                rdiff += coef[j];
717 +        }
718 + done:
719 +        if (cdarr != NULL)
720 +                free(cdarr);
721 +                                        /* make sure everything is set */
722 +        c_ccvt(&sv->spec, C_CSXY+C_CSSPEC);
723 +        return SDEnone;
724 + }
725 +
726 + /* Compute World->BSDF transform from surface normal and up (Y) vector */
727 + SDError
728 + SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const FVECT uVec)
729 + {
730 +        if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL))
731 +                return SDEargument;
732 +        VCOPY(vMtx[2], sNrm);
733 +        if (normalize(vMtx[2]) == .0)
734 +                return SDEargument;
735 +        fcross(vMtx[0], uVec, vMtx[2]);
736 +        if (normalize(vMtx[0]) == .0)
737 +                return SDEargument;
738 +        fcross(vMtx[1], vMtx[2], vMtx[0]);
739 +        return SDEnone;
740 + }
741 +
742 + /* Compute inverse transform */
743 + SDError
744 + SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3])
745 + {
746 +        RREAL   mTmp[3][3];
747 +        double  d;
748 +
749 +        if ((iMtx == NULL) | (vMtx == NULL))
750 +                return SDEargument;
751 +                                        /* compute determinant */
752 +        mTmp[0][0] = vMtx[2][2]*vMtx[1][1] - vMtx[2][1]*vMtx[1][2];
753 +        mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1];
754 +        mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2];
755 +        d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2];
756 +        if (d == .0) {
757 +                strcpy(SDerrorDetail, "Zero determinant in matrix inversion");
758 +                return SDEargument;
759 +        }
760 +        d = 1./d;                       /* invert matrix */
761 +        mTmp[0][0] *= d; mTmp[0][1] *= d; mTmp[0][2] *= d;
762 +        mTmp[1][0] = d*(vMtx[2][0]*vMtx[1][2] - vMtx[2][2]*vMtx[1][0]);
763 +        mTmp[1][1] = d*(vMtx[2][2]*vMtx[0][0] - vMtx[2][0]*vMtx[0][2]);
764 +        mTmp[1][2] = d*(vMtx[1][0]*vMtx[0][2] - vMtx[1][2]*vMtx[0][0]);
765 +        mTmp[2][0] = d*(vMtx[2][1]*vMtx[1][0] - vMtx[2][0]*vMtx[1][1]);
766 +        mTmp[2][1] = d*(vMtx[2][0]*vMtx[0][1] - vMtx[2][1]*vMtx[0][0]);
767 +        mTmp[2][2] = d*(vMtx[1][1]*vMtx[0][0] - vMtx[1][0]*vMtx[0][1]);
768 +        memcpy(iMtx, mTmp, sizeof(mTmp));
769 +        return SDEnone;
770 + }
771 +
772 + /* Transform and normalize direction (column) vector */
773 + SDError
774 + SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT inpVec)
775 + {
776 +        FVECT   vTmp;
777 +
778 +        if ((resVec == NULL) | (inpVec == NULL))
779 +                return SDEargument;
780 +        if (vMtx == NULL) {             /* assume they just want to normalize */
781 +                if (resVec != inpVec)
782 +                        VCOPY(resVec, inpVec);
783 +                return (normalize(resVec) > .0) ? SDEnone : SDEargument;
784 +        }
785 +        vTmp[0] = DOT(vMtx[0], inpVec);
786 +        vTmp[1] = DOT(vMtx[1], inpVec);
787 +        vTmp[2] = DOT(vMtx[2], inpVec);
788 +        if (normalize(vTmp) == .0)
789 +                return SDEargument;
790 +        VCOPY(resVec, vTmp);
791 +        return SDEnone;
792 + }
793 +
794 + /*################################################################*/
795 + /*######### DEPRECATED ROUTINES AWAITING PERMANENT REMOVAL #######*/
796 +
797 + /*
798   * Routines for handling BSDF data
799   */
800  
801   #include "standard.h"
9 #include "bsdf.h"
802   #include "paths.h"
11 #include "ezxml.h"
803   #include <ctype.h>
804  
805   #define MAXLATS         46              /* maximum number of latitudes */
# Line 71 | Line 862 | fequal(double a, double b)
862          return((a <= 1e-6) & (a >= -1e-6));
863   }
864  
865 < // returns the name of the given tag
865 > /* Returns the name of the given tag */
866   #ifdef ezxml_name
867   #undef ezxml_name
868   static char *
# Line 83 | Line 874 | ezxml_name(ezxml_t xml)
874   }
875   #endif
876  
877 < // returns the given tag's character content or empty string if none
877 > /* Returns the given tag's character content or empty string if none */
878   #ifdef ezxml_txt
879   #undef ezxml_txt
880   static char *
# Line 219 | Line 1010 | load_angle_basis(      /* load custom BSDF angle basis */
1010          if (!abname || !*abname)
1011                  return;
1012          for (i = nabases; i--; )
1013 <                if (!strcmp(abname, abase_list[i].name))
1013 >                if (!strcasecmp(abname, abase_list[i].name))
1014                          return;         /* assume it's the same */
1015          if (nabases >= MAXABASES)
1016                  error(INTERNAL, "too many angle bases");
# Line 247 | Line 1038 | load_angle_basis(      /* load custom BSDF angle basis */
1038   }
1039  
1040  
250 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        if (!strcasecmp(unit, "Millimeter")) return(.001);
261        sprintf(errmsg, "unknown dimensional unit '%s'", unit);
262        error(USER, errmsg);
263 }
264
265
1041   static void
1042   load_geometry(          /* load geometric dimensions and description (if any) */
1043          struct BSDF_data *dp,
# Line 320 | Line 1095 | load_bsdf_data(                /* load BSDF distribution for this wa
1095                  return;
1096          }
1097          for (i = nabases; i--; )
1098 <                if (!strcmp(cbasis, abase_list[i].name)) {
1098 >                if (!strcasecmp(cbasis, abase_list[i].name)) {
1099                          dp->ninc = abase_list[i].nangles;
1100                          dp->ib_priv = (void *)&abase_list[i];
1101                          dp->ib_vec = ab_getvecR;
# Line 334 | Line 1109 | load_bsdf_data(                /* load BSDF distribution for this wa
1109                  return;
1110          }
1111          for (i = nabases; i--; )
1112 <                if (!strcmp(rbasis, abase_list[i].name)) {
1112 >                if (!strcasecmp(rbasis, abase_list[i].name)) {
1113                          dp->nout = abase_list[i].nangles;
1114                          dp->ob_priv = (void *)&abase_list[i];
1115                          dp->ob_vec = ab_getvec;
# Line 343 | Line 1118 | load_bsdf_data(                /* load BSDF distribution for this wa
1118                          break;
1119                  }
1120          if (i < 0) {
1121 <                sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis);
1121 >                sprintf(errmsg, "undefined RowAngleBasis '%s'", rbasis);
1122                  error(WARNING, errmsg);
1123                  return;
1124          }
# Line 526 | Line 1301 | load_BSDF(             /* load BSDF data from file */
1301                  return(NULL);
1302          }
1303          wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
1304 +        if (strcasecmp(ezxml_txt(ezxml_child(ezxml_child(wtl,
1305 +                        "DataDefinition"), "IncidentDataStructure")),
1306 +                        "Columns")) {
1307 +                sprintf(errmsg,
1308 +                        "BSDF \"%s\": unsupported IncidentDataStructure",
1309 +                                path);
1310 +                error(WARNING, errmsg);
1311 +                ezxml_free(fl);
1312 +                return(NULL);
1313 +        }              
1314          load_angle_basis(ezxml_child(ezxml_child(wtl,
1315                                  "DataDefinition"), "AngleBasis"));
1316          dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data));
1317          load_geometry(dp, ezxml_child(wtl, "Material"));
1318          for (wld = ezxml_child(wtl, "WavelengthData");
1319                                  wld != NULL; wld = wld->next) {
1320 <                if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible"))
1320 >                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1321 >                                "Visible"))
1322                          continue;
1323 <                wdb = ezxml_child(wld, "WavelengthDataBlock");
1324 <                if (wdb == NULL) continue;
1325 <                if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")),
1323 >                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1324 >                                wdb != NULL; wdb = wdb->next)
1325 >                        if (!strcasecmp(ezxml_txt(ezxml_child(wdb,
1326 >                                        "WavelengthDataDirection")),
1327                                          "Transmission Front"))
1328 <                        continue;
1329 <                load_bsdf_data(dp, wdb);        /* load front BTDF */
1330 <                break;                          /* ignore the rest */
1328 >                                break;
1329 >                if (wdb != NULL) {              /* load front BTDF */
1330 >                        load_bsdf_data(dp, wdb);
1331 >                        break;                  /* ignore the rest */
1332 >                }
1333          }
1334          ezxml_free(fl);                         /* done with XML file */
1335          if (!check_bsdf_data(dp)) {
# Line 716 | Line 1505 | getBSDF_xfm(           /* compute BSDF orient. -> world orient.
1505          }
1506          return(1);
1507   }
1508 +
1509 + /*######### END DEPRECATED ROUTINES #######*/
1510 + /*################################################################*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines