| 1 | greg | 2.1 | #ifndef lint | 
| 2 | greg | 2.35 | static const char RCSid[] = "$Id: bsdf.c,v 2.34 2011/07/07 15:25:09 greg Exp $"; | 
| 3 | greg | 2.1 | #endif | 
| 4 |  |  | /* | 
| 5 | greg | 2.15 | *  bsdf.c | 
| 6 |  |  | * | 
| 7 |  |  | *  Definitions for bidirectional scattering distribution functions. | 
| 8 |  |  | * | 
| 9 |  |  | *  Created by Greg Ward on 1/10/11. | 
| 10 |  |  | * | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 | greg | 2.30 | #define _USE_MATH_DEFINES | 
| 14 | greg | 2.15 | #include <stdio.h> | 
| 15 |  |  | #include <stdlib.h> | 
| 16 | greg | 2.31 | #include <string.h> | 
| 17 | greg | 2.15 | #include <math.h> | 
| 18 | greg | 2.32 | #include <ctype.h> | 
| 19 | greg | 2.15 | #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 | greg | 2.21 | if (!ec) | 
| 52 |  |  | return SDEnone; | 
| 53 |  |  | if ((ec < SDEnone) | (ec > SDEunknown)) { | 
| 54 |  |  | SDerrorDetail[0] = '\0'; | 
| 55 |  |  | ec = SDEunknown; | 
| 56 |  |  | } | 
| 57 | greg | 2.15 | 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 | greg | 2.16 | SDloadGeometry(SDData *sd, ezxml_t wdb) | 
| 88 | greg | 2.15 | { | 
| 89 |  |  | ezxml_t         geom; | 
| 90 |  |  | double          cfact; | 
| 91 |  |  | const char      *fmt, *mgfstr; | 
| 92 |  |  |  | 
| 93 | greg | 2.16 | if (wdb == NULL)                /* no geometry section? */ | 
| 94 |  |  | return SDEnone; | 
| 95 |  |  | sd->dim[0] = sd->dim[1] = sd->dim[2] = .0; | 
| 96 | greg | 2.15 | if ((geom = ezxml_child(wdb, "Width")) != NULL) | 
| 97 | greg | 2.16 | sd->dim[0] = atof(ezxml_txt(geom)) * | 
| 98 | greg | 2.15 | to_meters(ezxml_attr(geom, "unit")); | 
| 99 |  |  | if ((geom = ezxml_child(wdb, "Height")) != NULL) | 
| 100 | greg | 2.16 | sd->dim[1] = atof(ezxml_txt(geom)) * | 
| 101 | greg | 2.15 | to_meters(ezxml_attr(geom, "unit")); | 
| 102 |  |  | if ((geom = ezxml_child(wdb, "Thickness")) != NULL) | 
| 103 | greg | 2.16 | sd->dim[2] = atof(ezxml_txt(geom)) * | 
| 104 | greg | 2.15 | to_meters(ezxml_attr(geom, "unit")); | 
| 105 | greg | 2.23 | if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { | 
| 106 | greg | 2.17 | sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); | 
| 107 | greg | 2.15 | return SDEdata; | 
| 108 | greg | 2.17 | } | 
| 109 | greg | 2.15 | if ((geom = ezxml_child(wdb, "Geometry")) == NULL || | 
| 110 |  |  | (mgfstr = ezxml_txt(geom)) == NULL) | 
| 111 |  |  | return SDEnone; | 
| 112 | greg | 2.32 | while (isspace(*mgfstr)) | 
| 113 |  |  | ++mgfstr; | 
| 114 |  |  | if (!*mgfstr) | 
| 115 |  |  | return SDEnone; | 
| 116 | greg | 2.15 | if ((fmt = ezxml_attr(geom, "format")) != NULL && | 
| 117 |  |  | strcasecmp(fmt, "MGF")) { | 
| 118 |  |  | sprintf(SDerrorDetail, | 
| 119 |  |  | "Unrecognized geometry format '%s' in \"%s\"", | 
| 120 | greg | 2.16 | fmt, sd->name); | 
| 121 | greg | 2.15 | return SDEsupport; | 
| 122 |  |  | } | 
| 123 |  |  | cfact = to_meters(ezxml_attr(geom, "unit")); | 
| 124 | greg | 2.16 | sd->mgf = (char *)malloc(strlen(mgfstr)+32); | 
| 125 |  |  | if (sd->mgf == NULL) { | 
| 126 | greg | 2.15 | strcpy(SDerrorDetail, "Out of memory in SDloadGeometry"); | 
| 127 |  |  | return SDEmemory; | 
| 128 |  |  | } | 
| 129 |  |  | if (cfact < 0.99 || cfact > 1.01) | 
| 130 | greg | 2.16 | sprintf(sd->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr); | 
| 131 | greg | 2.15 | else | 
| 132 | greg | 2.16 | strcpy(sd->mgf, mgfstr); | 
| 133 | greg | 2.15 | 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 | greg | 2.16 | ezxml_t         fl, wtl; | 
| 142 | greg | 2.15 |  | 
| 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 | greg | 2.16 | 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 | greg | 2.15 | /* load geometry if present */ | 
| 173 | greg | 2.16 | lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material")); | 
| 174 |  |  | if (lastErr) | 
| 175 | greg | 2.15 | return lastErr; | 
| 176 |  |  | /* try loading variable resolution data */ | 
| 177 | greg | 2.16 | lastErr = SDloadTre(sd, wtl); | 
| 178 | greg | 2.15 | /* check our result */ | 
| 179 | greg | 2.29 | if (lastErr == SDEsupport)      /* try matrix BSDF if not tree data */ | 
| 180 | greg | 2.16 | lastErr = SDloadMtx(sd, wtl); | 
| 181 | greg | 2.29 |  | 
| 182 | greg | 2.15 | /* done with XML file */ | 
| 183 |  |  | ezxml_free(fl); | 
| 184 | greg | 2.16 |  | 
| 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 | greg | 2.15 | } | 
| 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 | greg | 2.34 | if (df->comp[n].dist != NULL) | 
| 254 |  |  | (*df->comp[n].func->freeSC)(df->comp[n].dist); | 
| 255 | greg | 2.15 | free(df); | 
| 256 |  |  | } | 
| 257 |  |  |  | 
| 258 |  |  | /* Shorten file path to useable BSDF name, removing suffix */ | 
| 259 |  |  | void | 
| 260 | greg | 2.16 | SDclipName(char *res, const char *fname) | 
| 261 | greg | 2.15 | { | 
| 262 |  |  | const char      *cp, *dot = NULL; | 
| 263 |  |  |  | 
| 264 |  |  | for (cp = fname; *cp; cp++) | 
| 265 |  |  | if (*cp == '.') | 
| 266 |  |  | dot = cp; | 
| 267 |  |  | if ((dot == NULL) | (dot < fname+2)) | 
| 268 |  |  | dot = cp; | 
| 269 |  |  | if (dot - fname >= SDnameLn) | 
| 270 |  |  | fname = dot - SDnameLn + 1; | 
| 271 |  |  | while (fname < dot) | 
| 272 |  |  | *res++ = *fname++; | 
| 273 |  |  | *res = '\0'; | 
| 274 |  |  | } | 
| 275 |  |  |  | 
| 276 |  |  | /* Initialize an unused BSDF struct (simply clears to zeroes) */ | 
| 277 |  |  | void | 
| 278 | greg | 2.20 | SDclearBSDF(SDData *sd, const char *fname) | 
| 279 | greg | 2.15 | { | 
| 280 | greg | 2.20 | if (sd == NULL) | 
| 281 |  |  | return; | 
| 282 |  |  | memset(sd, 0, sizeof(SDData)); | 
| 283 |  |  | if (fname == NULL) | 
| 284 |  |  | return; | 
| 285 |  |  | SDclipName(sd->name, fname); | 
| 286 | greg | 2.15 | } | 
| 287 |  |  |  | 
| 288 |  |  | /* Free data associated with BSDF struct */ | 
| 289 |  |  | void | 
| 290 |  |  | SDfreeBSDF(SDData *sd) | 
| 291 |  |  | { | 
| 292 |  |  | if (sd == NULL) | 
| 293 |  |  | return; | 
| 294 |  |  | if (sd->mgf != NULL) { | 
| 295 |  |  | free(sd->mgf); | 
| 296 |  |  | sd->mgf = NULL; | 
| 297 |  |  | } | 
| 298 |  |  | if (sd->rf != NULL) { | 
| 299 |  |  | SDfreeSpectralDF(sd->rf); | 
| 300 |  |  | sd->rf = NULL; | 
| 301 |  |  | } | 
| 302 |  |  | if (sd->rb != NULL) { | 
| 303 |  |  | SDfreeSpectralDF(sd->rb); | 
| 304 |  |  | sd->rb = NULL; | 
| 305 |  |  | } | 
| 306 |  |  | if (sd->tf != NULL) { | 
| 307 |  |  | SDfreeSpectralDF(sd->tf); | 
| 308 |  |  | sd->tf = NULL; | 
| 309 |  |  | } | 
| 310 |  |  | sd->rLambFront.cieY = .0; | 
| 311 | greg | 2.16 | sd->rLambFront.spec.flags = 0; | 
| 312 | greg | 2.15 | sd->rLambBack.cieY = .0; | 
| 313 | greg | 2.16 | sd->rLambBack.spec.flags = 0; | 
| 314 | greg | 2.15 | sd->tLamb.cieY = .0; | 
| 315 | greg | 2.16 | sd->tLamb.spec.flags = 0; | 
| 316 | greg | 2.15 | } | 
| 317 |  |  |  | 
| 318 |  |  | /* Find writeable BSDF by name, or allocate new cache entry if absent */ | 
| 319 |  |  | SDData * | 
| 320 |  |  | SDgetCache(const char *bname) | 
| 321 |  |  | { | 
| 322 |  |  | struct SDCache_s        *sdl; | 
| 323 |  |  | char                    sdnam[SDnameLn]; | 
| 324 |  |  |  | 
| 325 |  |  | if (bname == NULL) | 
| 326 |  |  | return NULL; | 
| 327 |  |  |  | 
| 328 |  |  | SDclipName(sdnam, bname); | 
| 329 |  |  | for (sdl = SDcacheList; sdl != NULL; sdl = sdl->next) | 
| 330 |  |  | if (!strcmp(sdl->bsdf.name, sdnam)) { | 
| 331 |  |  | sdl->refcnt++; | 
| 332 |  |  | return &sdl->bsdf; | 
| 333 |  |  | } | 
| 334 |  |  |  | 
| 335 |  |  | sdl = (struct SDCache_s *)calloc(1, sizeof(struct SDCache_s)); | 
| 336 |  |  | if (sdl == NULL) | 
| 337 |  |  | return NULL; | 
| 338 |  |  |  | 
| 339 |  |  | strcpy(sdl->bsdf.name, sdnam); | 
| 340 |  |  | sdl->next = SDcacheList; | 
| 341 |  |  | SDcacheList = sdl; | 
| 342 |  |  |  | 
| 343 | greg | 2.21 | sdl->refcnt = 1; | 
| 344 | greg | 2.15 | return &sdl->bsdf; | 
| 345 |  |  | } | 
| 346 |  |  |  | 
| 347 |  |  | /* Get loaded BSDF from cache (or load and cache it on first call) */ | 
| 348 |  |  | /* Report any problem to stderr and return NULL on failure */ | 
| 349 |  |  | const SDData * | 
| 350 |  |  | SDcacheFile(const char *fname) | 
| 351 |  |  | { | 
| 352 |  |  | SDData          *sd; | 
| 353 |  |  | SDError         ec; | 
| 354 |  |  |  | 
| 355 |  |  | if (fname == NULL || !*fname) | 
| 356 |  |  | return NULL; | 
| 357 |  |  | SDerrorDetail[0] = '\0'; | 
| 358 |  |  | if ((sd = SDgetCache(fname)) == NULL) { | 
| 359 |  |  | SDreportEnglish(SDEmemory, stderr); | 
| 360 |  |  | return NULL; | 
| 361 |  |  | } | 
| 362 |  |  | if (!SDisLoaded(sd) && (ec = SDloadFile(sd, fname))) { | 
| 363 |  |  | SDreportEnglish(ec, stderr); | 
| 364 |  |  | SDfreeCache(sd); | 
| 365 |  |  | return NULL; | 
| 366 |  |  | } | 
| 367 |  |  | return sd; | 
| 368 |  |  | } | 
| 369 |  |  |  | 
| 370 |  |  | /* Free a BSDF from our cache (clear all if NULL) */ | 
| 371 |  |  | void | 
| 372 |  |  | SDfreeCache(const SDData *sd) | 
| 373 |  |  | { | 
| 374 |  |  | struct SDCache_s        *sdl, *sdLast = NULL; | 
| 375 |  |  |  | 
| 376 |  |  | if (sd == NULL) {               /* free entire list */ | 
| 377 |  |  | while ((sdl = SDcacheList) != NULL) { | 
| 378 |  |  | SDcacheList = sdl->next; | 
| 379 |  |  | SDfreeBSDF(&sdl->bsdf); | 
| 380 |  |  | free(sdl); | 
| 381 |  |  | } | 
| 382 |  |  | return; | 
| 383 |  |  | } | 
| 384 |  |  | for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) | 
| 385 |  |  | if (&sdl->bsdf == sd) | 
| 386 |  |  | break; | 
| 387 | greg | 2.21 | if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) | 
| 388 | greg | 2.15 | return;                 /* missing or still in use */ | 
| 389 |  |  | /* keep unreferenced data? */ | 
| 390 |  |  | if (SDisLoaded(sd) && SDretainSet) { | 
| 391 |  |  | if (SDretainSet == SDretainAll) | 
| 392 |  |  | return;         /* keep everything */ | 
| 393 |  |  | /* else free cumulative data */ | 
| 394 |  |  | SDfreeCumulativeCache(sd->rf); | 
| 395 |  |  | SDfreeCumulativeCache(sd->rb); | 
| 396 |  |  | SDfreeCumulativeCache(sd->tf); | 
| 397 |  |  | return; | 
| 398 |  |  | } | 
| 399 |  |  | /* remove from list and free */ | 
| 400 |  |  | if (sdLast == NULL) | 
| 401 |  |  | SDcacheList = sdl->next; | 
| 402 |  |  | else | 
| 403 |  |  | sdLast->next = sdl->next; | 
| 404 |  |  | SDfreeBSDF(&sdl->bsdf); | 
| 405 |  |  | free(sdl); | 
| 406 |  |  | } | 
| 407 |  |  |  | 
| 408 |  |  | /* Sample an individual BSDF component */ | 
| 409 |  |  | SDError | 
| 410 | greg | 2.24 | SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) | 
| 411 | greg | 2.15 | { | 
| 412 |  |  | float           coef[SDmaxCh]; | 
| 413 |  |  | SDError         ec; | 
| 414 | greg | 2.24 | FVECT           inVec; | 
| 415 | greg | 2.15 | const SDCDst    *cd; | 
| 416 |  |  | double          d; | 
| 417 |  |  | int             n; | 
| 418 |  |  | /* check arguments */ | 
| 419 | greg | 2.24 | if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) | 
| 420 | greg | 2.15 | return SDEargument; | 
| 421 |  |  | /* get cumulative distribution */ | 
| 422 | greg | 2.24 | VCOPY(inVec, ioVec); | 
| 423 | greg | 2.15 | cd = (*sdc->func->getCDist)(inVec, sdc); | 
| 424 |  |  | if (cd == NULL) | 
| 425 |  |  | return SDEmemory; | 
| 426 | greg | 2.35 | if (cd->cTotal <= 1e-6) {       /* anything to sample? */ | 
| 427 | greg | 2.15 | sv->spec = c_dfcolor; | 
| 428 |  |  | sv->cieY = .0; | 
| 429 | greg | 2.24 | memset(ioVec, 0, 3*sizeof(double)); | 
| 430 | greg | 2.15 | return SDEnone; | 
| 431 |  |  | } | 
| 432 |  |  | sv->cieY = cd->cTotal; | 
| 433 |  |  | /* compute sample direction */ | 
| 434 | greg | 2.24 | ec = (*sdc->func->sampCDist)(ioVec, randX, cd); | 
| 435 | greg | 2.15 | if (ec) | 
| 436 |  |  | return ec; | 
| 437 |  |  | /* get BSDF color */ | 
| 438 | greg | 2.25 | n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); | 
| 439 | greg | 2.15 | if (n <= 0) { | 
| 440 |  |  | strcpy(SDerrorDetail, "BSDF sample value error"); | 
| 441 |  |  | return SDEinternal; | 
| 442 |  |  | } | 
| 443 |  |  | sv->spec = sdc->cspec[0]; | 
| 444 |  |  | d = coef[0]; | 
| 445 |  |  | while (--n) { | 
| 446 |  |  | c_cmix(&sv->spec, d, &sv->spec, coef[n], &sdc->cspec[n]); | 
| 447 |  |  | d += coef[n]; | 
| 448 |  |  | } | 
| 449 |  |  | /* make sure everything is set */ | 
| 450 |  |  | c_ccvt(&sv->spec, C_CSXY+C_CSSPEC); | 
| 451 |  |  | return SDEnone; | 
| 452 |  |  | } | 
| 453 |  |  |  | 
| 454 |  |  | #define MS_MAXDIM       15 | 
| 455 |  |  |  | 
| 456 |  |  | /* Convert 1-dimensional random variable to N-dimensional */ | 
| 457 |  |  | void | 
| 458 |  |  | SDmultiSamp(double t[], int n, double randX) | 
| 459 |  |  | { | 
| 460 |  |  | unsigned        nBits; | 
| 461 |  |  | double          scale; | 
| 462 |  |  | bitmask_t       ndx, coord[MS_MAXDIM]; | 
| 463 |  |  |  | 
| 464 |  |  | while (n > MS_MAXDIM)           /* punt for higher dimensions */ | 
| 465 | greg | 2.19 | t[--n] = rand()*(1./(RAND_MAX+.5)); | 
| 466 | greg | 2.15 | nBits = (8*sizeof(bitmask_t) - 1) / n; | 
| 467 |  |  | ndx = randX * (double)((bitmask_t)1 << (nBits*n)); | 
| 468 |  |  | /* get coordinate on Hilbert curve */ | 
| 469 |  |  | hilbert_i2c(n, nBits, ndx, coord); | 
| 470 |  |  | /* convert back to [0,1) range */ | 
| 471 |  |  | scale = 1. / (double)((bitmask_t)1 << nBits); | 
| 472 |  |  | while (n--) | 
| 473 | greg | 2.19 | t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5))); | 
| 474 | greg | 2.15 | } | 
| 475 |  |  |  | 
| 476 |  |  | #undef MS_MAXDIM | 
| 477 |  |  |  | 
| 478 |  |  | /* Generate diffuse hemispherical sample */ | 
| 479 |  |  | static void | 
| 480 |  |  | SDdiffuseSamp(FVECT outVec, int outFront, double randX) | 
| 481 |  |  | { | 
| 482 |  |  | /* convert to position on hemisphere */ | 
| 483 |  |  | SDmultiSamp(outVec, 2, randX); | 
| 484 |  |  | SDsquare2disk(outVec, outVec[0], outVec[1]); | 
| 485 |  |  | outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; | 
| 486 | greg | 2.23 | if (outVec[2] > 0)              /* a bit of paranoia */ | 
| 487 | greg | 2.15 | outVec[2] = sqrt(outVec[2]); | 
| 488 |  |  | if (!outFront)                  /* going out back? */ | 
| 489 |  |  | outVec[2] = -outVec[2]; | 
| 490 |  |  | } | 
| 491 |  |  |  | 
| 492 |  |  | /* Query projected solid angle coverage for non-diffuse BSDF direction */ | 
| 493 |  |  | SDError | 
| 494 | greg | 2.22 | SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, | 
| 495 |  |  | int qflags, const SDData *sd) | 
| 496 | greg | 2.15 | { | 
| 497 | greg | 2.23 | SDSpectralDF    *rdf, *tdf; | 
| 498 | greg | 2.15 | SDError         ec; | 
| 499 |  |  | int             i; | 
| 500 |  |  | /* check arguments */ | 
| 501 | greg | 2.26 | if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) | 
| 502 | greg | 2.15 | return SDEargument; | 
| 503 |  |  | /* initialize extrema */ | 
| 504 | greg | 2.17 | switch (qflags) { | 
| 505 | greg | 2.15 | case SDqueryMax: | 
| 506 |  |  | projSA[0] = .0; | 
| 507 |  |  | break; | 
| 508 |  |  | case SDqueryMin+SDqueryMax: | 
| 509 |  |  | projSA[1] = .0; | 
| 510 |  |  | /* fall through */ | 
| 511 |  |  | case SDqueryMin: | 
| 512 |  |  | projSA[0] = 10.; | 
| 513 |  |  | break; | 
| 514 |  |  | case 0: | 
| 515 |  |  | return SDEargument; | 
| 516 |  |  | } | 
| 517 | greg | 2.23 | if (v1[2] > 0)                  /* front surface query? */ | 
| 518 | greg | 2.15 | rdf = sd->rf; | 
| 519 |  |  | else | 
| 520 |  |  | rdf = sd->rb; | 
| 521 | greg | 2.26 | tdf = sd->tf; | 
| 522 |  |  | if (v2 != NULL)                 /* bidirectional? */ | 
| 523 |  |  | if (v1[2] > 0 ^ v2[2] > 0) | 
| 524 |  |  | rdf = NULL; | 
| 525 |  |  | else | 
| 526 |  |  | tdf = NULL; | 
| 527 | greg | 2.15 | ec = SDEdata;                   /* run through components */ | 
| 528 |  |  | for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { | 
| 529 | greg | 2.22 | ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, | 
| 530 | greg | 2.25 | qflags, &rdf->comp[i]); | 
| 531 | greg | 2.15 | if (ec) | 
| 532 |  |  | return ec; | 
| 533 |  |  | } | 
| 534 | greg | 2.23 | for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { | 
| 535 |  |  | ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, | 
| 536 | greg | 2.25 | qflags, &tdf->comp[i]); | 
| 537 | greg | 2.15 | if (ec) | 
| 538 |  |  | return ec; | 
| 539 |  |  | } | 
| 540 | greg | 2.17 | if (ec) {                       /* all diffuse? */ | 
| 541 |  |  | projSA[0] = M_PI; | 
| 542 |  |  | if (qflags == SDqueryMin+SDqueryMax) | 
| 543 |  |  | projSA[1] = M_PI; | 
| 544 |  |  | } | 
| 545 |  |  | return SDEnone; | 
| 546 | greg | 2.15 | } | 
| 547 |  |  |  | 
| 548 |  |  | /* Return BSDF for the given incident and scattered ray vectors */ | 
| 549 |  |  | SDError | 
| 550 |  |  | SDevalBSDF(SDValue *sv, const FVECT outVec, const FVECT inVec, const SDData *sd) | 
| 551 |  |  | { | 
| 552 |  |  | int             inFront, outFront; | 
| 553 |  |  | SDSpectralDF    *sdf; | 
| 554 |  |  | float           coef[SDmaxCh]; | 
| 555 |  |  | int             nch, i; | 
| 556 |  |  | /* check arguments */ | 
| 557 |  |  | if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) | 
| 558 |  |  | return SDEargument; | 
| 559 |  |  | /* whose side are we on? */ | 
| 560 | greg | 2.23 | inFront = (inVec[2] > 0); | 
| 561 |  |  | outFront = (outVec[2] > 0); | 
| 562 | greg | 2.15 | /* start with diffuse portion */ | 
| 563 |  |  | if (inFront & outFront) { | 
| 564 |  |  | *sv = sd->rLambFront; | 
| 565 |  |  | sdf = sd->rf; | 
| 566 |  |  | } else if (!(inFront | outFront)) { | 
| 567 |  |  | *sv = sd->rLambBack; | 
| 568 |  |  | sdf = sd->rb; | 
| 569 |  |  | } else /* inFront ^ outFront */ { | 
| 570 |  |  | *sv = sd->tLamb; | 
| 571 |  |  | sdf = sd->tf; | 
| 572 |  |  | } | 
| 573 |  |  | sv->cieY *= 1./M_PI; | 
| 574 |  |  | /* add non-diffuse components */ | 
| 575 |  |  | i = (sdf != NULL) ? sdf->ncomp : 0; | 
| 576 |  |  | while (i-- > 0) { | 
| 577 |  |  | nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, | 
| 578 | greg | 2.25 | &sdf->comp[i]); | 
| 579 | greg | 2.15 | while (nch-- > 0) { | 
| 580 |  |  | c_cmix(&sv->spec, sv->cieY, &sv->spec, | 
| 581 |  |  | coef[nch], &sdf->comp[i].cspec[nch]); | 
| 582 |  |  | sv->cieY += coef[nch]; | 
| 583 |  |  | } | 
| 584 |  |  | } | 
| 585 |  |  | /* make sure everything is set */ | 
| 586 |  |  | c_ccvt(&sv->spec, C_CSXY+C_CSSPEC); | 
| 587 |  |  | return SDEnone; | 
| 588 |  |  | } | 
| 589 |  |  |  | 
| 590 |  |  | /* Compute directional hemispherical scattering at this incident angle */ | 
| 591 |  |  | double | 
| 592 |  |  | SDdirectHemi(const FVECT inVec, int sflags, const SDData *sd) | 
| 593 |  |  | { | 
| 594 |  |  | double          hsum; | 
| 595 |  |  | SDSpectralDF    *rdf; | 
| 596 |  |  | const SDCDst    *cd; | 
| 597 |  |  | int             i; | 
| 598 |  |  | /* check arguments */ | 
| 599 |  |  | if ((inVec == NULL) | (sd == NULL)) | 
| 600 |  |  | return .0; | 
| 601 |  |  | /* gather diffuse components */ | 
| 602 | greg | 2.23 | if (inVec[2] > 0) { | 
| 603 | greg | 2.15 | hsum = sd->rLambFront.cieY; | 
| 604 |  |  | rdf = sd->rf; | 
| 605 |  |  | } else /* !inFront */ { | 
| 606 |  |  | hsum = sd->rLambBack.cieY; | 
| 607 |  |  | rdf = sd->rb; | 
| 608 |  |  | } | 
| 609 |  |  | if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR) | 
| 610 |  |  | hsum = .0; | 
| 611 |  |  | if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) | 
| 612 |  |  | hsum += sd->tLamb.cieY; | 
| 613 |  |  | /* gather non-diffuse components */ | 
| 614 |  |  | i = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR && | 
| 615 |  |  | rdf != NULL) ? rdf->ncomp : 0; | 
| 616 |  |  | while (i-- > 0) {               /* non-diffuse reflection */ | 
| 617 |  |  | cd = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]); | 
| 618 |  |  | if (cd != NULL) | 
| 619 |  |  | hsum += cd->cTotal; | 
| 620 |  |  | } | 
| 621 |  |  | i = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT && | 
| 622 |  |  | sd->tf != NULL) ? sd->tf->ncomp : 0; | 
| 623 |  |  | while (i-- > 0) {               /* non-diffuse transmission */ | 
| 624 |  |  | cd = (*sd->tf->comp[i].func->getCDist)(inVec, &sd->tf->comp[i]); | 
| 625 |  |  | if (cd != NULL) | 
| 626 |  |  | hsum += cd->cTotal; | 
| 627 |  |  | } | 
| 628 |  |  | return hsum; | 
| 629 |  |  | } | 
| 630 |  |  |  | 
| 631 |  |  | /* Sample BSDF direction based on the given random variable */ | 
| 632 |  |  | SDError | 
| 633 | greg | 2.24 | SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) | 
| 634 | greg | 2.15 | { | 
| 635 |  |  | SDError         ec; | 
| 636 | greg | 2.24 | FVECT           inVec; | 
| 637 | greg | 2.15 | int             inFront; | 
| 638 |  |  | SDSpectralDF    *rdf; | 
| 639 |  |  | double          rdiff; | 
| 640 |  |  | float           coef[SDmaxCh]; | 
| 641 |  |  | int             i, j, n, nr; | 
| 642 |  |  | SDComponent     *sdc; | 
| 643 |  |  | const SDCDst    **cdarr = NULL; | 
| 644 |  |  | /* check arguments */ | 
| 645 | greg | 2.24 | if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | | 
| 646 | greg | 2.23 | (randX < 0) | (randX >= 1.)) | 
| 647 | greg | 2.15 | return SDEargument; | 
| 648 |  |  | /* whose side are we on? */ | 
| 649 | greg | 2.24 | VCOPY(inVec, ioVec); | 
| 650 | greg | 2.23 | inFront = (inVec[2] > 0); | 
| 651 | greg | 2.15 | /* remember diffuse portions */ | 
| 652 |  |  | if (inFront) { | 
| 653 |  |  | *sv = sd->rLambFront; | 
| 654 |  |  | rdf = sd->rf; | 
| 655 |  |  | } else /* !inFront */ { | 
| 656 |  |  | *sv = sd->rLambBack; | 
| 657 |  |  | rdf = sd->rb; | 
| 658 |  |  | } | 
| 659 |  |  | if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR) | 
| 660 |  |  | sv->cieY = .0; | 
| 661 |  |  | rdiff = sv->cieY; | 
| 662 |  |  | if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) | 
| 663 |  |  | sv->cieY += sd->tLamb.cieY; | 
| 664 |  |  | /* gather non-diffuse components */ | 
| 665 |  |  | i = nr = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR && | 
| 666 |  |  | rdf != NULL) ? rdf->ncomp : 0; | 
| 667 |  |  | j = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT && | 
| 668 |  |  | sd->tf != NULL) ? sd->tf->ncomp : 0; | 
| 669 |  |  | n = i + j; | 
| 670 |  |  | if (n > 0 && (cdarr = (const SDCDst **)malloc(n*sizeof(SDCDst *))) == NULL) | 
| 671 |  |  | return SDEmemory; | 
| 672 |  |  | while (j-- > 0) {               /* non-diffuse transmission */ | 
| 673 |  |  | cdarr[i+j] = (*sd->tf->comp[j].func->getCDist)(inVec, &sd->tf->comp[j]); | 
| 674 |  |  | if (cdarr[i+j] == NULL) { | 
| 675 |  |  | free(cdarr); | 
| 676 |  |  | return SDEmemory; | 
| 677 |  |  | } | 
| 678 |  |  | sv->cieY += cdarr[i+j]->cTotal; | 
| 679 |  |  | } | 
| 680 |  |  | while (i-- > 0) {               /* non-diffuse reflection */ | 
| 681 |  |  | cdarr[i] = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]); | 
| 682 |  |  | if (cdarr[i] == NULL) { | 
| 683 |  |  | free(cdarr); | 
| 684 |  |  | return SDEmemory; | 
| 685 |  |  | } | 
| 686 |  |  | sv->cieY += cdarr[i]->cTotal; | 
| 687 |  |  | } | 
| 688 | greg | 2.35 | if (sv->cieY <= 1e-6) {         /* anything to sample? */ | 
| 689 | greg | 2.15 | sv->cieY = .0; | 
| 690 | greg | 2.24 | memset(ioVec, 0, 3*sizeof(double)); | 
| 691 | greg | 2.15 | return SDEnone; | 
| 692 |  |  | } | 
| 693 |  |  | /* scale random variable */ | 
| 694 |  |  | randX *= sv->cieY; | 
| 695 |  |  | /* diffuse reflection? */ | 
| 696 |  |  | if (randX < rdiff) { | 
| 697 | greg | 2.24 | SDdiffuseSamp(ioVec, inFront, randX/rdiff); | 
| 698 | greg | 2.15 | goto done; | 
| 699 |  |  | } | 
| 700 |  |  | randX -= rdiff; | 
| 701 |  |  | /* diffuse transmission? */ | 
| 702 |  |  | if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { | 
| 703 |  |  | if (randX < sd->tLamb.cieY) { | 
| 704 |  |  | sv->spec = sd->tLamb.spec; | 
| 705 | greg | 2.24 | SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); | 
| 706 | greg | 2.15 | goto done; | 
| 707 |  |  | } | 
| 708 |  |  | randX -= sd->tLamb.cieY; | 
| 709 |  |  | } | 
| 710 |  |  | /* else one of cumulative dist. */ | 
| 711 |  |  | for (i = 0; i < n && randX < cdarr[i]->cTotal; i++) | 
| 712 |  |  | randX -= cdarr[i]->cTotal; | 
| 713 |  |  | if (i >= n) | 
| 714 |  |  | return SDEinternal; | 
| 715 |  |  | /* compute sample direction */ | 
| 716 |  |  | sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; | 
| 717 | greg | 2.24 | ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); | 
| 718 | greg | 2.15 | if (ec) | 
| 719 |  |  | return ec; | 
| 720 |  |  | /* compute color */ | 
| 721 | greg | 2.25 | j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); | 
| 722 | greg | 2.15 | if (j <= 0) { | 
| 723 |  |  | sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", | 
| 724 |  |  | sd->name); | 
| 725 |  |  | return SDEinternal; | 
| 726 |  |  | } | 
| 727 |  |  | sv->spec = sdc->cspec[0]; | 
| 728 |  |  | rdiff = coef[0]; | 
| 729 |  |  | while (--j) { | 
| 730 |  |  | c_cmix(&sv->spec, rdiff, &sv->spec, coef[j], &sdc->cspec[j]); | 
| 731 |  |  | rdiff += coef[j]; | 
| 732 |  |  | } | 
| 733 |  |  | done: | 
| 734 |  |  | if (cdarr != NULL) | 
| 735 |  |  | free(cdarr); | 
| 736 |  |  | /* make sure everything is set */ | 
| 737 |  |  | c_ccvt(&sv->spec, C_CSXY+C_CSSPEC); | 
| 738 |  |  | return SDEnone; | 
| 739 |  |  | } | 
| 740 |  |  |  | 
| 741 |  |  | /* Compute World->BSDF transform from surface normal and up (Y) vector */ | 
| 742 |  |  | SDError | 
| 743 |  |  | SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const FVECT uVec) | 
| 744 |  |  | { | 
| 745 |  |  | if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) | 
| 746 |  |  | return SDEargument; | 
| 747 |  |  | VCOPY(vMtx[2], sNrm); | 
| 748 | greg | 2.23 | if (normalize(vMtx[2]) == 0) | 
| 749 | greg | 2.15 | return SDEargument; | 
| 750 |  |  | fcross(vMtx[0], uVec, vMtx[2]); | 
| 751 | greg | 2.23 | if (normalize(vMtx[0]) == 0) | 
| 752 | greg | 2.15 | return SDEargument; | 
| 753 |  |  | fcross(vMtx[1], vMtx[2], vMtx[0]); | 
| 754 |  |  | return SDEnone; | 
| 755 |  |  | } | 
| 756 |  |  |  | 
| 757 |  |  | /* Compute inverse transform */ | 
| 758 |  |  | SDError | 
| 759 |  |  | SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3]) | 
| 760 |  |  | { | 
| 761 |  |  | RREAL   mTmp[3][3]; | 
| 762 |  |  | double  d; | 
| 763 |  |  |  | 
| 764 |  |  | if ((iMtx == NULL) | (vMtx == NULL)) | 
| 765 |  |  | return SDEargument; | 
| 766 |  |  | /* compute determinant */ | 
| 767 |  |  | mTmp[0][0] = vMtx[2][2]*vMtx[1][1] - vMtx[2][1]*vMtx[1][2]; | 
| 768 |  |  | mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; | 
| 769 |  |  | mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; | 
| 770 |  |  | d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; | 
| 771 | greg | 2.23 | if (d == 0) { | 
| 772 | greg | 2.15 | strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); | 
| 773 |  |  | return SDEargument; | 
| 774 |  |  | } | 
| 775 |  |  | d = 1./d;                       /* invert matrix */ | 
| 776 |  |  | mTmp[0][0] *= d; mTmp[0][1] *= d; mTmp[0][2] *= d; | 
| 777 |  |  | mTmp[1][0] = d*(vMtx[2][0]*vMtx[1][2] - vMtx[2][2]*vMtx[1][0]); | 
| 778 |  |  | mTmp[1][1] = d*(vMtx[2][2]*vMtx[0][0] - vMtx[2][0]*vMtx[0][2]); | 
| 779 |  |  | mTmp[1][2] = d*(vMtx[1][0]*vMtx[0][2] - vMtx[1][2]*vMtx[0][0]); | 
| 780 |  |  | mTmp[2][0] = d*(vMtx[2][1]*vMtx[1][0] - vMtx[2][0]*vMtx[1][1]); | 
| 781 |  |  | mTmp[2][1] = d*(vMtx[2][0]*vMtx[0][1] - vMtx[2][1]*vMtx[0][0]); | 
| 782 |  |  | mTmp[2][2] = d*(vMtx[1][1]*vMtx[0][0] - vMtx[1][0]*vMtx[0][1]); | 
| 783 |  |  | memcpy(iMtx, mTmp, sizeof(mTmp)); | 
| 784 |  |  | return SDEnone; | 
| 785 |  |  | } | 
| 786 |  |  |  | 
| 787 |  |  | /* Transform and normalize direction (column) vector */ | 
| 788 |  |  | SDError | 
| 789 |  |  | SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT inpVec) | 
| 790 |  |  | { | 
| 791 |  |  | FVECT   vTmp; | 
| 792 |  |  |  | 
| 793 |  |  | if ((resVec == NULL) | (inpVec == NULL)) | 
| 794 |  |  | return SDEargument; | 
| 795 |  |  | if (vMtx == NULL) {             /* assume they just want to normalize */ | 
| 796 |  |  | if (resVec != inpVec) | 
| 797 |  |  | VCOPY(resVec, inpVec); | 
| 798 | greg | 2.23 | return (normalize(resVec) > 0) ? SDEnone : SDEargument; | 
| 799 | greg | 2.15 | } | 
| 800 |  |  | vTmp[0] = DOT(vMtx[0], inpVec); | 
| 801 |  |  | vTmp[1] = DOT(vMtx[1], inpVec); | 
| 802 |  |  | vTmp[2] = DOT(vMtx[2], inpVec); | 
| 803 | greg | 2.23 | if (normalize(vTmp) == 0) | 
| 804 | greg | 2.15 | return SDEargument; | 
| 805 |  |  | VCOPY(resVec, vTmp); | 
| 806 |  |  | return SDEnone; | 
| 807 |  |  | } | 
| 808 |  |  |  | 
| 809 |  |  | /*################################################################*/ | 
| 810 |  |  | /*######### DEPRECATED ROUTINES AWAITING PERMANENT REMOVAL #######*/ | 
| 811 |  |  |  | 
| 812 |  |  | /* | 
| 813 | greg | 2.1 | * Routines for handling BSDF data | 
| 814 |  |  | */ | 
| 815 |  |  |  | 
| 816 |  |  | #include "standard.h" | 
| 817 |  |  | #include "paths.h" | 
| 818 |  |  |  | 
| 819 |  |  | #define MAXLATS         46              /* maximum number of latitudes */ | 
| 820 |  |  |  | 
| 821 |  |  | /* BSDF angle specification */ | 
| 822 |  |  | typedef struct { | 
| 823 |  |  | char    name[64];               /* basis name */ | 
| 824 |  |  | int     nangles;                /* total number of directions */ | 
| 825 |  |  | struct { | 
| 826 |  |  | float   tmin;                   /* starting theta */ | 
| 827 |  |  | short   nphis;                  /* number of phis (0 term) */ | 
| 828 |  |  | }       lat[MAXLATS+1];         /* latitudes */ | 
| 829 |  |  | } ANGLE_BASIS; | 
| 830 |  |  |  | 
| 831 | greg | 2.4 | #define MAXABASES       7               /* limit on defined bases */ | 
| 832 | greg | 2.1 |  | 
| 833 |  |  | static ANGLE_BASIS      abase_list[MAXABASES] = { | 
| 834 |  |  | { | 
| 835 |  |  | "LBNL/Klems Full", 145, | 
| 836 |  |  | { {-5., 1}, | 
| 837 |  |  | {5., 8}, | 
| 838 |  |  | {15., 16}, | 
| 839 |  |  | {25., 20}, | 
| 840 |  |  | {35., 24}, | 
| 841 |  |  | {45., 24}, | 
| 842 |  |  | {55., 24}, | 
| 843 |  |  | {65., 16}, | 
| 844 |  |  | {75., 12}, | 
| 845 |  |  | {90., 0} } | 
| 846 |  |  | }, { | 
| 847 |  |  | "LBNL/Klems Half", 73, | 
| 848 |  |  | { {-6.5, 1}, | 
| 849 |  |  | {6.5, 8}, | 
| 850 |  |  | {19.5, 12}, | 
| 851 |  |  | {32.5, 16}, | 
| 852 |  |  | {46.5, 20}, | 
| 853 |  |  | {61.5, 12}, | 
| 854 |  |  | {76.5, 4}, | 
| 855 |  |  | {90., 0} } | 
| 856 |  |  | }, { | 
| 857 |  |  | "LBNL/Klems Quarter", 41, | 
| 858 |  |  | { {-9., 1}, | 
| 859 |  |  | {9., 8}, | 
| 860 |  |  | {27., 12}, | 
| 861 |  |  | {46., 12}, | 
| 862 |  |  | {66., 8}, | 
| 863 |  |  | {90., 0} } | 
| 864 |  |  | } | 
| 865 |  |  | }; | 
| 866 |  |  |  | 
| 867 |  |  | static int      nabases = 3;    /* current number of defined bases */ | 
| 868 |  |  |  | 
| 869 | greg | 2.9 | #define  FEQ(a,b)       ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6) | 
| 870 |  |  |  | 
| 871 |  |  | static int | 
| 872 |  |  | fequal(double a, double b) | 
| 873 |  |  | { | 
| 874 | greg | 2.23 | if (b != 0) | 
| 875 | greg | 2.9 | a = a/b - 1.; | 
| 876 |  |  | return((a <= 1e-6) & (a >= -1e-6)); | 
| 877 |  |  | } | 
| 878 | greg | 2.3 |  | 
| 879 | greg | 2.14 | /* Returns the name of the given tag */ | 
| 880 | greg | 2.3 | #ifdef ezxml_name | 
| 881 |  |  | #undef ezxml_name | 
| 882 |  |  | static char * | 
| 883 |  |  | ezxml_name(ezxml_t xml) | 
| 884 |  |  | { | 
| 885 |  |  | if (xml == NULL) | 
| 886 |  |  | return(NULL); | 
| 887 |  |  | return(xml->name); | 
| 888 |  |  | } | 
| 889 |  |  | #endif | 
| 890 |  |  |  | 
| 891 | greg | 2.14 | /* Returns the given tag's character content or empty string if none */ | 
| 892 | greg | 2.3 | #ifdef ezxml_txt | 
| 893 |  |  | #undef ezxml_txt | 
| 894 |  |  | static char * | 
| 895 |  |  | ezxml_txt(ezxml_t xml) | 
| 896 |  |  | { | 
| 897 |  |  | if (xml == NULL) | 
| 898 |  |  | return(""); | 
| 899 |  |  | return(xml->txt); | 
| 900 |  |  | } | 
| 901 |  |  | #endif | 
| 902 |  |  |  | 
| 903 | greg | 2.1 |  | 
| 904 |  |  | static int | 
| 905 |  |  | ab_getvec(              /* get vector for this angle basis index */ | 
| 906 |  |  | FVECT v, | 
| 907 |  |  | int ndx, | 
| 908 |  |  | void *p | 
| 909 |  |  | ) | 
| 910 |  |  | { | 
| 911 |  |  | ANGLE_BASIS  *ab = (ANGLE_BASIS *)p; | 
| 912 |  |  | int     li; | 
| 913 | greg | 2.2 | double  pol, azi, d; | 
| 914 | greg | 2.1 |  | 
| 915 |  |  | if ((ndx < 0) | (ndx >= ab->nangles)) | 
| 916 |  |  | return(0); | 
| 917 |  |  | for (li = 0; ndx >= ab->lat[li].nphis; li++) | 
| 918 |  |  | ndx -= ab->lat[li].nphis; | 
| 919 | greg | 2.2 | pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin); | 
| 920 | greg | 2.1 | azi = 2.*PI*ndx/ab->lat[li].nphis; | 
| 921 | greg | 2.2 | v[2] = d = cos(pol); | 
| 922 |  |  | d = sqrt(1. - d*d);     /* sin(pol) */ | 
| 923 | greg | 2.1 | v[0] = cos(azi)*d; | 
| 924 |  |  | v[1] = sin(azi)*d; | 
| 925 |  |  | return(1); | 
| 926 |  |  | } | 
| 927 |  |  |  | 
| 928 |  |  |  | 
| 929 |  |  | static int | 
| 930 |  |  | ab_getndx(              /* get index corresponding to the given vector */ | 
| 931 |  |  | FVECT v, | 
| 932 |  |  | void *p | 
| 933 |  |  | ) | 
| 934 |  |  | { | 
| 935 |  |  | ANGLE_BASIS  *ab = (ANGLE_BASIS *)p; | 
| 936 |  |  | int     li, ndx; | 
| 937 | greg | 2.33 | double  pol, azi; | 
| 938 | greg | 2.1 |  | 
| 939 |  |  | if ((v[2] < -1.0) | (v[2] > 1.0)) | 
| 940 |  |  | return(-1); | 
| 941 | greg | 2.2 | pol = 180.0/PI*acos(v[2]); | 
| 942 | greg | 2.1 | azi = 180.0/PI*atan2(v[1], v[0]); | 
| 943 |  |  | if (azi < 0.0) azi += 360.0; | 
| 944 | greg | 2.2 | for (li = 1; ab->lat[li].tmin <= pol; li++) | 
| 945 | greg | 2.1 | if (!ab->lat[li].nphis) | 
| 946 |  |  | return(-1); | 
| 947 |  |  | --li; | 
| 948 |  |  | ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5); | 
| 949 |  |  | if (ndx >= ab->lat[li].nphis) ndx = 0; | 
| 950 |  |  | while (li--) | 
| 951 |  |  | ndx += ab->lat[li].nphis; | 
| 952 |  |  | return(ndx); | 
| 953 |  |  | } | 
| 954 |  |  |  | 
| 955 |  |  |  | 
| 956 |  |  | static double | 
| 957 |  |  | ab_getohm(              /* get solid angle for this angle basis index */ | 
| 958 |  |  | int ndx, | 
| 959 |  |  | void *p | 
| 960 |  |  | ) | 
| 961 |  |  | { | 
| 962 |  |  | ANGLE_BASIS  *ab = (ANGLE_BASIS *)p; | 
| 963 |  |  | int     li; | 
| 964 |  |  | double  theta, theta1; | 
| 965 |  |  |  | 
| 966 |  |  | if ((ndx < 0) | (ndx >= ab->nangles)) | 
| 967 |  |  | return(0); | 
| 968 |  |  | for (li = 0; ndx >= ab->lat[li].nphis; li++) | 
| 969 |  |  | ndx -= ab->lat[li].nphis; | 
| 970 |  |  | theta1 = PI/180. * ab->lat[li+1].tmin; | 
| 971 |  |  | if (ab->lat[li].nphis == 1) {           /* special case */ | 
| 972 |  |  | if (ab->lat[li].tmin > FTINY) | 
| 973 |  |  | error(USER, "unsupported BSDF coordinate system"); | 
| 974 |  |  | return(2.*PI*(1. - cos(theta1))); | 
| 975 |  |  | } | 
| 976 |  |  | theta = PI/180. * ab->lat[li].tmin; | 
| 977 |  |  | return(2.*PI*(cos(theta) - cos(theta1))/(double)ab->lat[li].nphis); | 
| 978 |  |  | } | 
| 979 |  |  |  | 
| 980 |  |  |  | 
| 981 |  |  | static int | 
| 982 |  |  | ab_getvecR(             /* get reverse vector for this angle basis index */ | 
| 983 |  |  | FVECT v, | 
| 984 |  |  | int ndx, | 
| 985 |  |  | void *p | 
| 986 |  |  | ) | 
| 987 |  |  | { | 
| 988 |  |  | if (!ab_getvec(v, ndx, p)) | 
| 989 |  |  | return(0); | 
| 990 |  |  |  | 
| 991 |  |  | v[0] = -v[0]; | 
| 992 |  |  | v[1] = -v[1]; | 
| 993 |  |  | v[2] = -v[2]; | 
| 994 |  |  |  | 
| 995 |  |  | return(1); | 
| 996 |  |  | } | 
| 997 |  |  |  | 
| 998 |  |  |  | 
| 999 |  |  | static int | 
| 1000 |  |  | ab_getndxR(             /* get index corresponding to the reverse vector */ | 
| 1001 |  |  | FVECT v, | 
| 1002 |  |  | void *p | 
| 1003 |  |  | ) | 
| 1004 |  |  | { | 
| 1005 |  |  | FVECT  v2; | 
| 1006 |  |  |  | 
| 1007 |  |  | v2[0] = -v[0]; | 
| 1008 |  |  | v2[1] = -v[1]; | 
| 1009 |  |  | v2[2] = -v[2]; | 
| 1010 |  |  |  | 
| 1011 |  |  | return ab_getndx(v2, p); | 
| 1012 |  |  | } | 
| 1013 |  |  |  | 
| 1014 |  |  |  | 
| 1015 |  |  | static void | 
| 1016 | greg | 2.4 | load_angle_basis(       /* load custom BSDF angle basis */ | 
| 1017 | greg | 2.3 | ezxml_t wab | 
| 1018 |  |  | ) | 
| 1019 |  |  | { | 
| 1020 |  |  | char    *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName")); | 
| 1021 |  |  | ezxml_t wbb; | 
| 1022 |  |  | int     i; | 
| 1023 |  |  |  | 
| 1024 | greg | 2.4 | if (!abname || !*abname) | 
| 1025 | greg | 2.3 | return; | 
| 1026 |  |  | for (i = nabases; i--; ) | 
| 1027 | greg | 2.12 | if (!strcasecmp(abname, abase_list[i].name)) | 
| 1028 | greg | 2.4 | return;         /* assume it's the same */ | 
| 1029 | greg | 2.3 | if (nabases >= MAXABASES) | 
| 1030 |  |  | error(INTERNAL, "too many angle bases"); | 
| 1031 |  |  | strcpy(abase_list[nabases].name, abname); | 
| 1032 |  |  | abase_list[nabases].nangles = 0; | 
| 1033 |  |  | for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock"); | 
| 1034 |  |  | wbb != NULL; i++, wbb = wbb->next) { | 
| 1035 |  |  | if (i >= MAXLATS) | 
| 1036 |  |  | error(INTERNAL, "too many latitudes in custom basis"); | 
| 1037 |  |  | abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt( | 
| 1038 |  |  | ezxml_child(ezxml_child(wbb, | 
| 1039 |  |  | "ThetaBounds"), "UpperTheta"))); | 
| 1040 |  |  | if (!i) | 
| 1041 |  |  | abase_list[nabases].lat[i].tmin = | 
| 1042 |  |  | -abase_list[nabases].lat[i+1].tmin; | 
| 1043 | greg | 2.9 | else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, | 
| 1044 | greg | 2.3 | "ThetaBounds"), "LowerTheta"))), | 
| 1045 |  |  | abase_list[nabases].lat[i].tmin)) | 
| 1046 |  |  | error(WARNING, "theta values disagree in custom basis"); | 
| 1047 |  |  | abase_list[nabases].nangles += | 
| 1048 |  |  | abase_list[nabases].lat[i].nphis = | 
| 1049 |  |  | atoi(ezxml_txt(ezxml_child(wbb, "nPhis"))); | 
| 1050 |  |  | } | 
| 1051 |  |  | abase_list[nabases++].lat[i].nphis = 0; | 
| 1052 |  |  | } | 
| 1053 |  |  |  | 
| 1054 |  |  |  | 
| 1055 | greg | 2.6 | static void | 
| 1056 |  |  | load_geometry(          /* load geometric dimensions and description (if any) */ | 
| 1057 |  |  | struct BSDF_data *dp, | 
| 1058 |  |  | ezxml_t wdb | 
| 1059 |  |  | ) | 
| 1060 |  |  | { | 
| 1061 |  |  | ezxml_t         geom; | 
| 1062 |  |  | double          cfact; | 
| 1063 |  |  | const char      *fmt, *mgfstr; | 
| 1064 |  |  |  | 
| 1065 |  |  | dp->dim[0] = dp->dim[1] = dp->dim[2] = 0; | 
| 1066 |  |  | dp->mgf = NULL; | 
| 1067 |  |  | if ((geom = ezxml_child(wdb, "Width")) != NULL) | 
| 1068 |  |  | dp->dim[0] = atof(ezxml_txt(geom)) * | 
| 1069 |  |  | to_meters(ezxml_attr(geom, "unit")); | 
| 1070 |  |  | if ((geom = ezxml_child(wdb, "Height")) != NULL) | 
| 1071 |  |  | dp->dim[1] = atof(ezxml_txt(geom)) * | 
| 1072 |  |  | to_meters(ezxml_attr(geom, "unit")); | 
| 1073 |  |  | if ((geom = ezxml_child(wdb, "Thickness")) != NULL) | 
| 1074 |  |  | dp->dim[2] = atof(ezxml_txt(geom)) * | 
| 1075 |  |  | to_meters(ezxml_attr(geom, "unit")); | 
| 1076 |  |  | if ((geom = ezxml_child(wdb, "Geometry")) == NULL || | 
| 1077 |  |  | (mgfstr = ezxml_txt(geom)) == NULL) | 
| 1078 |  |  | return; | 
| 1079 |  |  | if ((fmt = ezxml_attr(geom, "format")) != NULL && | 
| 1080 |  |  | strcasecmp(fmt, "MGF")) { | 
| 1081 |  |  | sprintf(errmsg, "unrecognized geometry format '%s'", fmt); | 
| 1082 |  |  | error(WARNING, errmsg); | 
| 1083 |  |  | return; | 
| 1084 |  |  | } | 
| 1085 |  |  | cfact = to_meters(ezxml_attr(geom, "unit")); | 
| 1086 |  |  | dp->mgf = (char *)malloc(strlen(mgfstr)+32); | 
| 1087 |  |  | if (dp->mgf == NULL) | 
| 1088 |  |  | error(SYSTEM, "out of memory in load_geometry"); | 
| 1089 |  |  | if (cfact < 0.99 || cfact > 1.01) | 
| 1090 |  |  | sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr); | 
| 1091 |  |  | else | 
| 1092 |  |  | strcpy(dp->mgf, mgfstr); | 
| 1093 |  |  | } | 
| 1094 |  |  |  | 
| 1095 |  |  |  | 
| 1096 | greg | 2.3 | static void | 
| 1097 | greg | 2.1 | load_bsdf_data(         /* load BSDF distribution for this wavelength */ | 
| 1098 |  |  | struct BSDF_data *dp, | 
| 1099 |  |  | ezxml_t wdb | 
| 1100 |  |  | ) | 
| 1101 |  |  | { | 
| 1102 |  |  | char  *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); | 
| 1103 |  |  | char  *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); | 
| 1104 |  |  | char  *sdata; | 
| 1105 |  |  | int  i; | 
| 1106 |  |  |  | 
| 1107 | greg | 2.4 | if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { | 
| 1108 | greg | 2.1 | error(WARNING, "missing column/row basis for BSDF"); | 
| 1109 |  |  | return; | 
| 1110 |  |  | } | 
| 1111 |  |  | for (i = nabases; i--; ) | 
| 1112 | greg | 2.12 | if (!strcasecmp(cbasis, abase_list[i].name)) { | 
| 1113 | greg | 2.1 | dp->ninc = abase_list[i].nangles; | 
| 1114 |  |  | dp->ib_priv = (void *)&abase_list[i]; | 
| 1115 |  |  | dp->ib_vec = ab_getvecR; | 
| 1116 |  |  | dp->ib_ndx = ab_getndxR; | 
| 1117 |  |  | dp->ib_ohm = ab_getohm; | 
| 1118 |  |  | break; | 
| 1119 |  |  | } | 
| 1120 |  |  | if (i < 0) { | 
| 1121 | greg | 2.4 | sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis); | 
| 1122 | greg | 2.1 | error(WARNING, errmsg); | 
| 1123 |  |  | return; | 
| 1124 |  |  | } | 
| 1125 |  |  | for (i = nabases; i--; ) | 
| 1126 | greg | 2.12 | if (!strcasecmp(rbasis, abase_list[i].name)) { | 
| 1127 | greg | 2.1 | dp->nout = abase_list[i].nangles; | 
| 1128 |  |  | dp->ob_priv = (void *)&abase_list[i]; | 
| 1129 |  |  | dp->ob_vec = ab_getvec; | 
| 1130 |  |  | dp->ob_ndx = ab_getndx; | 
| 1131 |  |  | dp->ob_ohm = ab_getohm; | 
| 1132 |  |  | break; | 
| 1133 |  |  | } | 
| 1134 |  |  | if (i < 0) { | 
| 1135 | greg | 2.16 | sprintf(errmsg, "undefined RowAngleBasis '%s'", rbasis); | 
| 1136 | greg | 2.1 | error(WARNING, errmsg); | 
| 1137 |  |  | return; | 
| 1138 |  |  | } | 
| 1139 |  |  | /* read BSDF data */ | 
| 1140 |  |  | sdata  = ezxml_txt(ezxml_child(wdb,"ScatteringData")); | 
| 1141 | greg | 2.4 | if (!sdata || !*sdata) { | 
| 1142 | greg | 2.1 | error(WARNING, "missing BSDF ScatteringData"); | 
| 1143 |  |  | return; | 
| 1144 |  |  | } | 
| 1145 |  |  | dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout); | 
| 1146 |  |  | if (dp->bsdf == NULL) | 
| 1147 |  |  | error(SYSTEM, "out of memory in load_bsdf_data"); | 
| 1148 |  |  | for (i = 0; i < dp->ninc*dp->nout; i++) { | 
| 1149 |  |  | char  *sdnext = fskip(sdata); | 
| 1150 |  |  | if (sdnext == NULL) { | 
| 1151 |  |  | error(WARNING, "bad/missing BSDF ScatteringData"); | 
| 1152 |  |  | free(dp->bsdf); dp->bsdf = NULL; | 
| 1153 |  |  | return; | 
| 1154 |  |  | } | 
| 1155 |  |  | while (*sdnext && isspace(*sdnext)) | 
| 1156 |  |  | sdnext++; | 
| 1157 |  |  | if (*sdnext == ',') sdnext++; | 
| 1158 |  |  | dp->bsdf[i] = atof(sdata); | 
| 1159 |  |  | sdata = sdnext; | 
| 1160 |  |  | } | 
| 1161 |  |  | while (isspace(*sdata)) | 
| 1162 |  |  | sdata++; | 
| 1163 |  |  | if (*sdata) { | 
| 1164 |  |  | sprintf(errmsg, "%d extra characters after BSDF ScatteringData", | 
| 1165 | greg | 2.5 | (int)strlen(sdata)); | 
| 1166 | greg | 2.1 | error(WARNING, errmsg); | 
| 1167 |  |  | } | 
| 1168 |  |  | } | 
| 1169 |  |  |  | 
| 1170 |  |  |  | 
| 1171 |  |  | static int | 
| 1172 |  |  | check_bsdf_data(        /* check that BSDF data is sane */ | 
| 1173 |  |  | struct BSDF_data *dp | 
| 1174 |  |  | ) | 
| 1175 |  |  | { | 
| 1176 | greg | 2.2 | double          *omega_iarr, *omega_oarr; | 
| 1177 | greg | 2.33 | double          dom, hemi_total, full_total; | 
| 1178 | greg | 2.1 | int             nneg; | 
| 1179 | greg | 2.2 | FVECT           v; | 
| 1180 | greg | 2.1 | int             i, o; | 
| 1181 |  |  |  | 
| 1182 |  |  | if (dp == NULL || dp->bsdf == NULL) | 
| 1183 |  |  | return(0); | 
| 1184 | greg | 2.2 | omega_iarr = (double *)calloc(dp->ninc, sizeof(double)); | 
| 1185 |  |  | omega_oarr = (double *)calloc(dp->nout, sizeof(double)); | 
| 1186 |  |  | if ((omega_iarr == NULL) | (omega_oarr == NULL)) | 
| 1187 | greg | 2.1 | error(SYSTEM, "out of memory in check_bsdf_data"); | 
| 1188 | greg | 2.2 | /* incoming projected solid angles */ | 
| 1189 |  |  | hemi_total = .0; | 
| 1190 |  |  | for (i = dp->ninc; i--; ) { | 
| 1191 |  |  | dom = getBSDF_incohm(dp,i); | 
| 1192 | greg | 2.23 | if (dom <= 0) { | 
| 1193 | greg | 2.2 | error(WARNING, "zero/negative incoming solid angle"); | 
| 1194 |  |  | continue; | 
| 1195 |  |  | } | 
| 1196 |  |  | if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) { | 
| 1197 |  |  | error(WARNING, "illegal incoming BSDF direction"); | 
| 1198 |  |  | free(omega_iarr); free(omega_oarr); | 
| 1199 |  |  | return(0); | 
| 1200 |  |  | } | 
| 1201 |  |  | hemi_total += omega_iarr[i] = dom * -v[2]; | 
| 1202 |  |  | } | 
| 1203 |  |  | if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) { | 
| 1204 |  |  | sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%", | 
| 1205 |  |  | 100.*(hemi_total/PI - 1.)); | 
| 1206 |  |  | error(WARNING, errmsg); | 
| 1207 |  |  | } | 
| 1208 |  |  | dom = PI / hemi_total;          /* fix normalization */ | 
| 1209 |  |  | for (i = dp->ninc; i--; ) | 
| 1210 |  |  | omega_iarr[i] *= dom; | 
| 1211 |  |  | /* outgoing projected solid angles */ | 
| 1212 | greg | 2.1 | hemi_total = .0; | 
| 1213 |  |  | for (o = dp->nout; o--; ) { | 
| 1214 |  |  | dom = getBSDF_outohm(dp,o); | 
| 1215 | greg | 2.23 | if (dom <= 0) { | 
| 1216 | greg | 2.2 | error(WARNING, "zero/negative outgoing solid angle"); | 
| 1217 | greg | 2.1 | continue; | 
| 1218 |  |  | } | 
| 1219 |  |  | if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) { | 
| 1220 |  |  | error(WARNING, "illegal outgoing BSDF direction"); | 
| 1221 | greg | 2.2 | free(omega_iarr); free(omega_oarr); | 
| 1222 | greg | 2.1 | return(0); | 
| 1223 |  |  | } | 
| 1224 | greg | 2.2 | hemi_total += omega_oarr[o] = dom * v[2]; | 
| 1225 | greg | 2.1 | } | 
| 1226 |  |  | if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) { | 
| 1227 |  |  | sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%", | 
| 1228 |  |  | 100.*(hemi_total/PI - 1.)); | 
| 1229 |  |  | error(WARNING, errmsg); | 
| 1230 |  |  | } | 
| 1231 | greg | 2.2 | dom = PI / hemi_total;          /* fix normalization */ | 
| 1232 | greg | 2.1 | for (o = dp->nout; o--; ) | 
| 1233 | greg | 2.2 | omega_oarr[o] *= dom; | 
| 1234 |  |  | nneg = 0;                       /* check outgoing totals */ | 
| 1235 |  |  | for (i = 0; i < dp->ninc; i++) { | 
| 1236 | greg | 2.1 | hemi_total = .0; | 
| 1237 |  |  | for (o = dp->nout; o--; ) { | 
| 1238 |  |  | double  f = BSDF_value(dp,i,o); | 
| 1239 | greg | 2.23 | if (f >= 0) | 
| 1240 | greg | 2.2 | hemi_total += f*omega_oarr[o]; | 
| 1241 |  |  | else { | 
| 1242 |  |  | nneg += (f < -FTINY); | 
| 1243 |  |  | BSDF_value(dp,i,o) = .0f; | 
| 1244 |  |  | } | 
| 1245 | greg | 2.1 | } | 
| 1246 | greg | 2.8 | if (hemi_total > 1.01) { | 
| 1247 | greg | 2.2 | sprintf(errmsg, | 
| 1248 |  |  | "incoming BSDF direction %d passes %.1f%% of light", | 
| 1249 |  |  | i, 100.*hemi_total); | 
| 1250 | greg | 2.1 | error(WARNING, errmsg); | 
| 1251 |  |  | } | 
| 1252 |  |  | } | 
| 1253 | greg | 2.2 | if (nneg) { | 
| 1254 |  |  | sprintf(errmsg, "%d negative BSDF values (ignored)", nneg); | 
| 1255 | greg | 2.1 | error(WARNING, errmsg); | 
| 1256 |  |  | } | 
| 1257 | greg | 2.7 | full_total = .0;                /* reverse roles and check again */ | 
| 1258 | greg | 2.2 | for (o = 0; o < dp->nout; o++) { | 
| 1259 |  |  | hemi_total = .0; | 
| 1260 |  |  | for (i = dp->ninc; i--; ) | 
| 1261 |  |  | hemi_total += BSDF_value(dp,i,o) * omega_iarr[i]; | 
| 1262 |  |  |  | 
| 1263 | greg | 2.8 | if (hemi_total > 1.01) { | 
| 1264 | greg | 2.2 | sprintf(errmsg, | 
| 1265 |  |  | "outgoing BSDF direction %d collects %.1f%% of light", | 
| 1266 |  |  | o, 100.*hemi_total); | 
| 1267 |  |  | error(WARNING, errmsg); | 
| 1268 |  |  | } | 
| 1269 | greg | 2.7 | full_total += hemi_total*omega_oarr[o]; | 
| 1270 |  |  | } | 
| 1271 |  |  | full_total /= PI; | 
| 1272 | greg | 2.8 | if (full_total > 1.00001) { | 
| 1273 |  |  | sprintf(errmsg, "BSDF transfers %.4f%% of light", | 
| 1274 | greg | 2.7 | 100.*full_total); | 
| 1275 |  |  | error(WARNING, errmsg); | 
| 1276 | greg | 2.2 | } | 
| 1277 |  |  | free(omega_iarr); free(omega_oarr); | 
| 1278 | greg | 2.1 | return(1); | 
| 1279 |  |  | } | 
| 1280 |  |  |  | 
| 1281 | greg | 2.6 |  | 
| 1282 | greg | 2.1 | struct BSDF_data * | 
| 1283 |  |  | load_BSDF(              /* load BSDF data from file */ | 
| 1284 |  |  | char *fname | 
| 1285 |  |  | ) | 
| 1286 |  |  | { | 
| 1287 |  |  | char                    *path; | 
| 1288 |  |  | ezxml_t                 fl, wtl, wld, wdb; | 
| 1289 |  |  | struct BSDF_data        *dp; | 
| 1290 |  |  |  | 
| 1291 |  |  | path = getpath(fname, getrlibpath(), R_OK); | 
| 1292 |  |  | if (path == NULL) { | 
| 1293 |  |  | sprintf(errmsg, "cannot find BSDF file \"%s\"", fname); | 
| 1294 |  |  | error(WARNING, errmsg); | 
| 1295 |  |  | return(NULL); | 
| 1296 |  |  | } | 
| 1297 |  |  | fl = ezxml_parse_file(path); | 
| 1298 |  |  | if (fl == NULL) { | 
| 1299 |  |  | sprintf(errmsg, "cannot open BSDF \"%s\"", path); | 
| 1300 |  |  | error(WARNING, errmsg); | 
| 1301 |  |  | return(NULL); | 
| 1302 |  |  | } | 
| 1303 |  |  | if (ezxml_error(fl)[0]) { | 
| 1304 |  |  | sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl)); | 
| 1305 |  |  | error(WARNING, errmsg); | 
| 1306 |  |  | ezxml_free(fl); | 
| 1307 |  |  | return(NULL); | 
| 1308 |  |  | } | 
| 1309 |  |  | if (strcmp(ezxml_name(fl), "WindowElement")) { | 
| 1310 |  |  | sprintf(errmsg, | 
| 1311 |  |  | "BSDF \"%s\": top level node not 'WindowElement'", | 
| 1312 |  |  | path); | 
| 1313 |  |  | error(WARNING, errmsg); | 
| 1314 |  |  | ezxml_free(fl); | 
| 1315 |  |  | return(NULL); | 
| 1316 |  |  | } | 
| 1317 |  |  | wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); | 
| 1318 | greg | 2.10 | if (strcasecmp(ezxml_txt(ezxml_child(ezxml_child(wtl, | 
| 1319 |  |  | "DataDefinition"), "IncidentDataStructure")), | 
| 1320 |  |  | "Columns")) { | 
| 1321 |  |  | sprintf(errmsg, | 
| 1322 |  |  | "BSDF \"%s\": unsupported IncidentDataStructure", | 
| 1323 |  |  | path); | 
| 1324 |  |  | error(WARNING, errmsg); | 
| 1325 |  |  | ezxml_free(fl); | 
| 1326 |  |  | return(NULL); | 
| 1327 | greg | 2.34 | } | 
| 1328 |  |  | for (wld = ezxml_child(ezxml_child(wtl, | 
| 1329 |  |  | "DataDefinition"), "AngleBasis"); | 
| 1330 |  |  | wld != NULL; wld = wld->next) | 
| 1331 |  |  | load_angle_basis(wld); | 
| 1332 | greg | 2.1 | dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); | 
| 1333 | greg | 2.6 | load_geometry(dp, ezxml_child(wtl, "Material")); | 
| 1334 | greg | 2.1 | for (wld = ezxml_child(wtl, "WavelengthData"); | 
| 1335 |  |  | wld != NULL; wld = wld->next) { | 
| 1336 | greg | 2.13 | if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), | 
| 1337 |  |  | "Visible")) | 
| 1338 | greg | 2.1 | continue; | 
| 1339 | greg | 2.13 | for (wdb = ezxml_child(wld, "WavelengthDataBlock"); | 
| 1340 |  |  | wdb != NULL; wdb = wdb->next) | 
| 1341 |  |  | if (!strcasecmp(ezxml_txt(ezxml_child(wdb, | 
| 1342 |  |  | "WavelengthDataDirection")), | 
| 1343 | greg | 2.1 | "Transmission Front")) | 
| 1344 | greg | 2.13 | break; | 
| 1345 |  |  | if (wdb != NULL) {              /* load front BTDF */ | 
| 1346 |  |  | load_bsdf_data(dp, wdb); | 
| 1347 |  |  | break;                  /* ignore the rest */ | 
| 1348 |  |  | } | 
| 1349 | greg | 2.1 | } | 
| 1350 |  |  | ezxml_free(fl);                         /* done with XML file */ | 
| 1351 |  |  | if (!check_bsdf_data(dp)) { | 
| 1352 |  |  | sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path); | 
| 1353 |  |  | error(WARNING, errmsg); | 
| 1354 |  |  | free_BSDF(dp); | 
| 1355 |  |  | dp = NULL; | 
| 1356 |  |  | } | 
| 1357 |  |  | return(dp); | 
| 1358 |  |  | } | 
| 1359 |  |  |  | 
| 1360 |  |  |  | 
| 1361 |  |  | void | 
| 1362 |  |  | free_BSDF(              /* free BSDF data structure */ | 
| 1363 |  |  | struct BSDF_data *b | 
| 1364 |  |  | ) | 
| 1365 |  |  | { | 
| 1366 |  |  | if (b == NULL) | 
| 1367 |  |  | return; | 
| 1368 | greg | 2.6 | if (b->mgf != NULL) | 
| 1369 |  |  | free(b->mgf); | 
| 1370 | greg | 2.1 | if (b->bsdf != NULL) | 
| 1371 |  |  | free(b->bsdf); | 
| 1372 |  |  | free(b); | 
| 1373 |  |  | } | 
| 1374 |  |  |  | 
| 1375 |  |  |  | 
| 1376 |  |  | int | 
| 1377 |  |  | r_BSDF_incvec(          /* compute random input vector at given location */ | 
| 1378 |  |  | FVECT v, | 
| 1379 |  |  | struct BSDF_data *b, | 
| 1380 |  |  | int i, | 
| 1381 |  |  | double rv, | 
| 1382 |  |  | MAT4 xm | 
| 1383 |  |  | ) | 
| 1384 |  |  | { | 
| 1385 |  |  | FVECT   pert; | 
| 1386 |  |  | double  rad; | 
| 1387 |  |  | int     j; | 
| 1388 |  |  |  | 
| 1389 |  |  | if (!getBSDF_incvec(v, b, i)) | 
| 1390 |  |  | return(0); | 
| 1391 |  |  | rad = sqrt(getBSDF_incohm(b, i) / PI); | 
| 1392 |  |  | multisamp(pert, 3, rv); | 
| 1393 |  |  | for (j = 0; j < 3; j++) | 
| 1394 |  |  | v[j] += rad*(2.*pert[j] - 1.); | 
| 1395 |  |  | if (xm != NULL) | 
| 1396 |  |  | multv3(v, v, xm); | 
| 1397 |  |  | return(normalize(v) != 0.0); | 
| 1398 |  |  | } | 
| 1399 |  |  |  | 
| 1400 |  |  |  | 
| 1401 |  |  | int | 
| 1402 |  |  | r_BSDF_outvec(          /* compute random output vector at given location */ | 
| 1403 |  |  | FVECT v, | 
| 1404 |  |  | struct BSDF_data *b, | 
| 1405 |  |  | int o, | 
| 1406 |  |  | double rv, | 
| 1407 |  |  | MAT4 xm | 
| 1408 |  |  | ) | 
| 1409 |  |  | { | 
| 1410 |  |  | FVECT   pert; | 
| 1411 |  |  | double  rad; | 
| 1412 |  |  | int     j; | 
| 1413 |  |  |  | 
| 1414 |  |  | if (!getBSDF_outvec(v, b, o)) | 
| 1415 |  |  | return(0); | 
| 1416 |  |  | rad = sqrt(getBSDF_outohm(b, o) / PI); | 
| 1417 |  |  | multisamp(pert, 3, rv); | 
| 1418 |  |  | for (j = 0; j < 3; j++) | 
| 1419 |  |  | v[j] += rad*(2.*pert[j] - 1.); | 
| 1420 |  |  | if (xm != NULL) | 
| 1421 |  |  | multv3(v, v, xm); | 
| 1422 |  |  | return(normalize(v) != 0.0); | 
| 1423 |  |  | } | 
| 1424 |  |  |  | 
| 1425 |  |  |  | 
| 1426 |  |  | static int | 
| 1427 |  |  | addrot(                 /* compute rotation (x,y,z) => (xp,yp,zp) */ | 
| 1428 |  |  | char *xfarg[], | 
| 1429 |  |  | FVECT xp, | 
| 1430 |  |  | FVECT yp, | 
| 1431 |  |  | FVECT zp | 
| 1432 |  |  | ) | 
| 1433 |  |  | { | 
| 1434 |  |  | static char     bufs[3][16]; | 
| 1435 |  |  | int     bn = 0; | 
| 1436 |  |  | char    **xfp = xfarg; | 
| 1437 |  |  | double  theta; | 
| 1438 |  |  |  | 
| 1439 |  |  | if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { | 
| 1440 |  |  | /* Special case for X' along Z-axis */ | 
| 1441 |  |  | theta = -atan2(yp[0], yp[1]); | 
| 1442 |  |  | *xfp++ = "-ry"; | 
| 1443 |  |  | *xfp++ = xp[2] < 0.0 ? "90" : "-90"; | 
| 1444 |  |  | *xfp++ = "-rz"; | 
| 1445 |  |  | sprintf(bufs[bn], "%f", theta*(180./PI)); | 
| 1446 |  |  | *xfp++ = bufs[bn++]; | 
| 1447 |  |  | return(xfp - xfarg); | 
| 1448 |  |  | } | 
| 1449 |  |  | theta = atan2(yp[2], zp[2]); | 
| 1450 |  |  | if (!FEQ(theta,0.0)) { | 
| 1451 |  |  | *xfp++ = "-rx"; | 
| 1452 |  |  | sprintf(bufs[bn], "%f", theta*(180./PI)); | 
| 1453 |  |  | *xfp++ = bufs[bn++]; | 
| 1454 |  |  | } | 
| 1455 |  |  | theta = asin(-xp[2]); | 
| 1456 |  |  | if (!FEQ(theta,0.0)) { | 
| 1457 |  |  | *xfp++ = "-ry"; | 
| 1458 |  |  | sprintf(bufs[bn], " %f", theta*(180./PI)); | 
| 1459 |  |  | *xfp++ = bufs[bn++]; | 
| 1460 |  |  | } | 
| 1461 |  |  | theta = atan2(xp[1], xp[0]); | 
| 1462 |  |  | if (!FEQ(theta,0.0)) { | 
| 1463 |  |  | *xfp++ = "-rz"; | 
| 1464 |  |  | sprintf(bufs[bn], "%f", theta*(180./PI)); | 
| 1465 |  |  | *xfp++ = bufs[bn++]; | 
| 1466 |  |  | } | 
| 1467 |  |  | *xfp = NULL; | 
| 1468 |  |  | return(xfp - xfarg); | 
| 1469 |  |  | } | 
| 1470 |  |  |  | 
| 1471 |  |  |  | 
| 1472 |  |  | int | 
| 1473 |  |  | getBSDF_xfm(            /* compute BSDF orient. -> world orient. transform */ | 
| 1474 |  |  | MAT4 xm, | 
| 1475 |  |  | FVECT nrm, | 
| 1476 | greg | 2.6 | UpDir ud, | 
| 1477 |  |  | char *xfbuf | 
| 1478 | greg | 2.1 | ) | 
| 1479 |  |  | { | 
| 1480 |  |  | char    *xfargs[7]; | 
| 1481 |  |  | XF      myxf; | 
| 1482 |  |  | FVECT   updir, xdest, ydest; | 
| 1483 | greg | 2.6 | int     i; | 
| 1484 | greg | 2.1 |  | 
| 1485 |  |  | updir[0] = updir[1] = updir[2] = 0.; | 
| 1486 |  |  | switch (ud) { | 
| 1487 |  |  | case UDzneg: | 
| 1488 |  |  | updir[2] = -1.; | 
| 1489 |  |  | break; | 
| 1490 |  |  | case UDyneg: | 
| 1491 |  |  | updir[1] = -1.; | 
| 1492 |  |  | break; | 
| 1493 |  |  | case UDxneg: | 
| 1494 |  |  | updir[0] = -1.; | 
| 1495 |  |  | break; | 
| 1496 |  |  | case UDxpos: | 
| 1497 |  |  | updir[0] = 1.; | 
| 1498 |  |  | break; | 
| 1499 |  |  | case UDypos: | 
| 1500 |  |  | updir[1] = 1.; | 
| 1501 |  |  | break; | 
| 1502 |  |  | case UDzpos: | 
| 1503 |  |  | updir[2] = 1.; | 
| 1504 |  |  | break; | 
| 1505 |  |  | case UDunknown: | 
| 1506 |  |  | return(0); | 
| 1507 |  |  | } | 
| 1508 |  |  | fcross(xdest, updir, nrm); | 
| 1509 |  |  | if (normalize(xdest) == 0.0) | 
| 1510 |  |  | return(0); | 
| 1511 |  |  | fcross(ydest, nrm, xdest); | 
| 1512 |  |  | xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs); | 
| 1513 |  |  | copymat4(xm, myxf.xfm); | 
| 1514 | greg | 2.6 | if (xfbuf == NULL) | 
| 1515 |  |  | return(1); | 
| 1516 |  |  | /* return xf arguments as well */ | 
| 1517 |  |  | for (i = 0; xfargs[i] != NULL; i++) { | 
| 1518 |  |  | *xfbuf++ = ' '; | 
| 1519 |  |  | strcpy(xfbuf, xfargs[i]); | 
| 1520 |  |  | while (*xfbuf) ++xfbuf; | 
| 1521 |  |  | } | 
| 1522 | greg | 2.1 | return(1); | 
| 1523 |  |  | } | 
| 1524 | greg | 2.15 |  | 
| 1525 |  |  | /*######### END DEPRECATED ROUTINES #######*/ | 
| 1526 |  |  | /*################################################################*/ |