1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.20 |
static const char RCSid[] = "$Id: bsdf.c,v 2.19 2011/04/09 15:39:16 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 |
|
|
#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 |
greg |
2.16 |
SDloadGeometry(SDData *sd, ezxml_t wdb) |
81 |
greg |
2.15 |
{ |
82 |
|
|
ezxml_t geom; |
83 |
|
|
double cfact; |
84 |
|
|
const char *fmt, *mgfstr; |
85 |
|
|
|
86 |
greg |
2.16 |
if (wdb == NULL) /* no geometry section? */ |
87 |
|
|
return SDEnone; |
88 |
|
|
sd->dim[0] = sd->dim[1] = sd->dim[2] = .0; |
89 |
greg |
2.15 |
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
90 |
greg |
2.16 |
sd->dim[0] = atof(ezxml_txt(geom)) * |
91 |
greg |
2.15 |
to_meters(ezxml_attr(geom, "unit")); |
92 |
|
|
if ((geom = ezxml_child(wdb, "Height")) != NULL) |
93 |
greg |
2.16 |
sd->dim[1] = atof(ezxml_txt(geom)) * |
94 |
greg |
2.15 |
to_meters(ezxml_attr(geom, "unit")); |
95 |
|
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
96 |
greg |
2.16 |
sd->dim[2] = atof(ezxml_txt(geom)) * |
97 |
greg |
2.15 |
to_meters(ezxml_attr(geom, "unit")); |
98 |
greg |
2.17 |
if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) { |
99 |
|
|
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
100 |
greg |
2.15 |
return SDEdata; |
101 |
greg |
2.17 |
} |
102 |
greg |
2.15 |
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 |
greg |
2.16 |
fmt, sd->name); |
110 |
greg |
2.15 |
return SDEsupport; |
111 |
|
|
} |
112 |
|
|
cfact = to_meters(ezxml_attr(geom, "unit")); |
113 |
greg |
2.16 |
sd->mgf = (char *)malloc(strlen(mgfstr)+32); |
114 |
|
|
if (sd->mgf == NULL) { |
115 |
greg |
2.15 |
strcpy(SDerrorDetail, "Out of memory in SDloadGeometry"); |
116 |
|
|
return SDEmemory; |
117 |
|
|
} |
118 |
|
|
if (cfact < 0.99 || cfact > 1.01) |
119 |
greg |
2.16 |
sprintf(sd->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr); |
120 |
greg |
2.15 |
else |
121 |
greg |
2.16 |
strcpy(sd->mgf, mgfstr); |
122 |
greg |
2.15 |
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 |
greg |
2.16 |
ezxml_t fl, wtl; |
131 |
greg |
2.15 |
|
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 |
greg |
2.16 |
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 |
greg |
2.15 |
/* load geometry if present */ |
162 |
greg |
2.16 |
lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material")); |
163 |
|
|
if (lastErr) |
164 |
greg |
2.15 |
return lastErr; |
165 |
|
|
/* try loading variable resolution data */ |
166 |
greg |
2.16 |
lastErr = SDloadTre(sd, wtl); |
167 |
greg |
2.15 |
/* check our result */ |
168 |
|
|
switch (lastErr) { |
169 |
|
|
case SDEformat: |
170 |
|
|
case SDEdata: |
171 |
|
|
case SDEsupport: /* possibly we just tried the wrong format */ |
172 |
greg |
2.16 |
lastErr = SDloadMtx(sd, wtl); |
173 |
greg |
2.15 |
break; |
174 |
|
|
default: /* variable res. OK else serious error */ |
175 |
|
|
break; |
176 |
|
|
} |
177 |
|
|
/* done with XML file */ |
178 |
|
|
ezxml_free(fl); |
179 |
greg |
2.16 |
|
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 |
greg |
2.15 |
} |
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 |
greg |
2.16 |
SDclipName(char *res, const char *fname) |
255 |
greg |
2.15 |
{ |
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 |
greg |
2.20 |
SDclearBSDF(SDData *sd, const char *fname) |
273 |
greg |
2.15 |
{ |
274 |
greg |
2.20 |
if (sd == NULL) |
275 |
|
|
return; |
276 |
|
|
memset(sd, 0, sizeof(SDData)); |
277 |
|
|
if (fname == NULL) |
278 |
|
|
return; |
279 |
|
|
SDclipName(sd->name, fname); |
280 |
greg |
2.15 |
} |
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 |
greg |
2.16 |
sd->rLambFront.spec.flags = 0; |
306 |
greg |
2.15 |
sd->rLambBack.cieY = .0; |
307 |
greg |
2.16 |
sd->rLambBack.spec.flags = 0; |
308 |
greg |
2.15 |
sd->tLamb.cieY = .0; |
309 |
greg |
2.16 |
sd->tLamb.spec.flags = 0; |
310 |
greg |
2.15 |
} |
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 |
greg |
2.19 |
t[--n] = rand()*(1./(RAND_MAX+.5)); |
459 |
greg |
2.15 |
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 |
greg |
2.19 |
t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5))); |
467 |
greg |
2.15 |
} |
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 |
greg |
2.17 |
switch (qflags) { |
497 |
greg |
2.15 |
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 |
greg |
2.17 |
if (ec) { /* all diffuse? */ |
527 |
|
|
projSA[0] = M_PI; |
528 |
|
|
if (qflags == SDqueryMin+SDqueryMax) |
529 |
|
|
projSA[1] = M_PI; |
530 |
|
|
} |
531 |
|
|
return SDEnone; |
532 |
greg |
2.15 |
} |
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 |
greg |
2.1 |
* Routines for handling BSDF data |
799 |
|
|
*/ |
800 |
|
|
|
801 |
|
|
#include "standard.h" |
802 |
|
|
#include "paths.h" |
803 |
|
|
#include <ctype.h> |
804 |
|
|
|
805 |
|
|
#define MAXLATS 46 /* maximum number of latitudes */ |
806 |
|
|
|
807 |
|
|
/* BSDF angle specification */ |
808 |
|
|
typedef struct { |
809 |
|
|
char name[64]; /* basis name */ |
810 |
|
|
int nangles; /* total number of directions */ |
811 |
|
|
struct { |
812 |
|
|
float tmin; /* starting theta */ |
813 |
|
|
short nphis; /* number of phis (0 term) */ |
814 |
|
|
} lat[MAXLATS+1]; /* latitudes */ |
815 |
|
|
} ANGLE_BASIS; |
816 |
|
|
|
817 |
greg |
2.4 |
#define MAXABASES 7 /* limit on defined bases */ |
818 |
greg |
2.1 |
|
819 |
|
|
static ANGLE_BASIS abase_list[MAXABASES] = { |
820 |
|
|
{ |
821 |
|
|
"LBNL/Klems Full", 145, |
822 |
|
|
{ {-5., 1}, |
823 |
|
|
{5., 8}, |
824 |
|
|
{15., 16}, |
825 |
|
|
{25., 20}, |
826 |
|
|
{35., 24}, |
827 |
|
|
{45., 24}, |
828 |
|
|
{55., 24}, |
829 |
|
|
{65., 16}, |
830 |
|
|
{75., 12}, |
831 |
|
|
{90., 0} } |
832 |
|
|
}, { |
833 |
|
|
"LBNL/Klems Half", 73, |
834 |
|
|
{ {-6.5, 1}, |
835 |
|
|
{6.5, 8}, |
836 |
|
|
{19.5, 12}, |
837 |
|
|
{32.5, 16}, |
838 |
|
|
{46.5, 20}, |
839 |
|
|
{61.5, 12}, |
840 |
|
|
{76.5, 4}, |
841 |
|
|
{90., 0} } |
842 |
|
|
}, { |
843 |
|
|
"LBNL/Klems Quarter", 41, |
844 |
|
|
{ {-9., 1}, |
845 |
|
|
{9., 8}, |
846 |
|
|
{27., 12}, |
847 |
|
|
{46., 12}, |
848 |
|
|
{66., 8}, |
849 |
|
|
{90., 0} } |
850 |
|
|
} |
851 |
|
|
}; |
852 |
|
|
|
853 |
|
|
static int nabases = 3; /* current number of defined bases */ |
854 |
|
|
|
855 |
greg |
2.9 |
#define FEQ(a,b) ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6) |
856 |
|
|
|
857 |
|
|
static int |
858 |
|
|
fequal(double a, double b) |
859 |
|
|
{ |
860 |
|
|
if (b != .0) |
861 |
|
|
a = a/b - 1.; |
862 |
|
|
return((a <= 1e-6) & (a >= -1e-6)); |
863 |
|
|
} |
864 |
greg |
2.3 |
|
865 |
greg |
2.14 |
/* Returns the name of the given tag */ |
866 |
greg |
2.3 |
#ifdef ezxml_name |
867 |
|
|
#undef ezxml_name |
868 |
|
|
static char * |
869 |
|
|
ezxml_name(ezxml_t xml) |
870 |
|
|
{ |
871 |
|
|
if (xml == NULL) |
872 |
|
|
return(NULL); |
873 |
|
|
return(xml->name); |
874 |
|
|
} |
875 |
|
|
#endif |
876 |
|
|
|
877 |
greg |
2.14 |
/* Returns the given tag's character content or empty string if none */ |
878 |
greg |
2.3 |
#ifdef ezxml_txt |
879 |
|
|
#undef ezxml_txt |
880 |
|
|
static char * |
881 |
|
|
ezxml_txt(ezxml_t xml) |
882 |
|
|
{ |
883 |
|
|
if (xml == NULL) |
884 |
|
|
return(""); |
885 |
|
|
return(xml->txt); |
886 |
|
|
} |
887 |
|
|
#endif |
888 |
|
|
|
889 |
greg |
2.1 |
|
890 |
|
|
static int |
891 |
|
|
ab_getvec( /* get vector for this angle basis index */ |
892 |
|
|
FVECT v, |
893 |
|
|
int ndx, |
894 |
|
|
void *p |
895 |
|
|
) |
896 |
|
|
{ |
897 |
|
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
898 |
|
|
int li; |
899 |
greg |
2.2 |
double pol, azi, d; |
900 |
greg |
2.1 |
|
901 |
|
|
if ((ndx < 0) | (ndx >= ab->nangles)) |
902 |
|
|
return(0); |
903 |
|
|
for (li = 0; ndx >= ab->lat[li].nphis; li++) |
904 |
|
|
ndx -= ab->lat[li].nphis; |
905 |
greg |
2.2 |
pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin); |
906 |
greg |
2.1 |
azi = 2.*PI*ndx/ab->lat[li].nphis; |
907 |
greg |
2.2 |
v[2] = d = cos(pol); |
908 |
|
|
d = sqrt(1. - d*d); /* sin(pol) */ |
909 |
greg |
2.1 |
v[0] = cos(azi)*d; |
910 |
|
|
v[1] = sin(azi)*d; |
911 |
|
|
return(1); |
912 |
|
|
} |
913 |
|
|
|
914 |
|
|
|
915 |
|
|
static int |
916 |
|
|
ab_getndx( /* get index corresponding to the given vector */ |
917 |
|
|
FVECT v, |
918 |
|
|
void *p |
919 |
|
|
) |
920 |
|
|
{ |
921 |
|
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
922 |
|
|
int li, ndx; |
923 |
greg |
2.2 |
double pol, azi, d; |
924 |
greg |
2.1 |
|
925 |
|
|
if ((v[2] < -1.0) | (v[2] > 1.0)) |
926 |
|
|
return(-1); |
927 |
greg |
2.2 |
pol = 180.0/PI*acos(v[2]); |
928 |
greg |
2.1 |
azi = 180.0/PI*atan2(v[1], v[0]); |
929 |
|
|
if (azi < 0.0) azi += 360.0; |
930 |
greg |
2.2 |
for (li = 1; ab->lat[li].tmin <= pol; li++) |
931 |
greg |
2.1 |
if (!ab->lat[li].nphis) |
932 |
|
|
return(-1); |
933 |
|
|
--li; |
934 |
|
|
ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5); |
935 |
|
|
if (ndx >= ab->lat[li].nphis) ndx = 0; |
936 |
|
|
while (li--) |
937 |
|
|
ndx += ab->lat[li].nphis; |
938 |
|
|
return(ndx); |
939 |
|
|
} |
940 |
|
|
|
941 |
|
|
|
942 |
|
|
static double |
943 |
|
|
ab_getohm( /* get solid angle for this angle basis index */ |
944 |
|
|
int ndx, |
945 |
|
|
void *p |
946 |
|
|
) |
947 |
|
|
{ |
948 |
|
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
949 |
|
|
int li; |
950 |
|
|
double theta, theta1; |
951 |
|
|
|
952 |
|
|
if ((ndx < 0) | (ndx >= ab->nangles)) |
953 |
|
|
return(0); |
954 |
|
|
for (li = 0; ndx >= ab->lat[li].nphis; li++) |
955 |
|
|
ndx -= ab->lat[li].nphis; |
956 |
|
|
theta1 = PI/180. * ab->lat[li+1].tmin; |
957 |
|
|
if (ab->lat[li].nphis == 1) { /* special case */ |
958 |
|
|
if (ab->lat[li].tmin > FTINY) |
959 |
|
|
error(USER, "unsupported BSDF coordinate system"); |
960 |
|
|
return(2.*PI*(1. - cos(theta1))); |
961 |
|
|
} |
962 |
|
|
theta = PI/180. * ab->lat[li].tmin; |
963 |
|
|
return(2.*PI*(cos(theta) - cos(theta1))/(double)ab->lat[li].nphis); |
964 |
|
|
} |
965 |
|
|
|
966 |
|
|
|
967 |
|
|
static int |
968 |
|
|
ab_getvecR( /* get reverse vector for this angle basis index */ |
969 |
|
|
FVECT v, |
970 |
|
|
int ndx, |
971 |
|
|
void *p |
972 |
|
|
) |
973 |
|
|
{ |
974 |
|
|
if (!ab_getvec(v, ndx, p)) |
975 |
|
|
return(0); |
976 |
|
|
|
977 |
|
|
v[0] = -v[0]; |
978 |
|
|
v[1] = -v[1]; |
979 |
|
|
v[2] = -v[2]; |
980 |
|
|
|
981 |
|
|
return(1); |
982 |
|
|
} |
983 |
|
|
|
984 |
|
|
|
985 |
|
|
static int |
986 |
|
|
ab_getndxR( /* get index corresponding to the reverse vector */ |
987 |
|
|
FVECT v, |
988 |
|
|
void *p |
989 |
|
|
) |
990 |
|
|
{ |
991 |
|
|
FVECT v2; |
992 |
|
|
|
993 |
|
|
v2[0] = -v[0]; |
994 |
|
|
v2[1] = -v[1]; |
995 |
|
|
v2[2] = -v[2]; |
996 |
|
|
|
997 |
|
|
return ab_getndx(v2, p); |
998 |
|
|
} |
999 |
|
|
|
1000 |
|
|
|
1001 |
|
|
static void |
1002 |
greg |
2.4 |
load_angle_basis( /* load custom BSDF angle basis */ |
1003 |
greg |
2.3 |
ezxml_t wab |
1004 |
|
|
) |
1005 |
|
|
{ |
1006 |
|
|
char *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName")); |
1007 |
|
|
ezxml_t wbb; |
1008 |
|
|
int i; |
1009 |
|
|
|
1010 |
greg |
2.4 |
if (!abname || !*abname) |
1011 |
greg |
2.3 |
return; |
1012 |
|
|
for (i = nabases; i--; ) |
1013 |
greg |
2.12 |
if (!strcasecmp(abname, abase_list[i].name)) |
1014 |
greg |
2.4 |
return; /* assume it's the same */ |
1015 |
greg |
2.3 |
if (nabases >= MAXABASES) |
1016 |
|
|
error(INTERNAL, "too many angle bases"); |
1017 |
|
|
strcpy(abase_list[nabases].name, abname); |
1018 |
|
|
abase_list[nabases].nangles = 0; |
1019 |
|
|
for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock"); |
1020 |
|
|
wbb != NULL; i++, wbb = wbb->next) { |
1021 |
|
|
if (i >= MAXLATS) |
1022 |
|
|
error(INTERNAL, "too many latitudes in custom basis"); |
1023 |
|
|
abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt( |
1024 |
|
|
ezxml_child(ezxml_child(wbb, |
1025 |
|
|
"ThetaBounds"), "UpperTheta"))); |
1026 |
|
|
if (!i) |
1027 |
|
|
abase_list[nabases].lat[i].tmin = |
1028 |
|
|
-abase_list[nabases].lat[i+1].tmin; |
1029 |
greg |
2.9 |
else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, |
1030 |
greg |
2.3 |
"ThetaBounds"), "LowerTheta"))), |
1031 |
|
|
abase_list[nabases].lat[i].tmin)) |
1032 |
|
|
error(WARNING, "theta values disagree in custom basis"); |
1033 |
|
|
abase_list[nabases].nangles += |
1034 |
|
|
abase_list[nabases].lat[i].nphis = |
1035 |
|
|
atoi(ezxml_txt(ezxml_child(wbb, "nPhis"))); |
1036 |
|
|
} |
1037 |
|
|
abase_list[nabases++].lat[i].nphis = 0; |
1038 |
|
|
} |
1039 |
|
|
|
1040 |
|
|
|
1041 |
greg |
2.6 |
static void |
1042 |
|
|
load_geometry( /* load geometric dimensions and description (if any) */ |
1043 |
|
|
struct BSDF_data *dp, |
1044 |
|
|
ezxml_t wdb |
1045 |
|
|
) |
1046 |
|
|
{ |
1047 |
|
|
ezxml_t geom; |
1048 |
|
|
double cfact; |
1049 |
|
|
const char *fmt, *mgfstr; |
1050 |
|
|
|
1051 |
|
|
dp->dim[0] = dp->dim[1] = dp->dim[2] = 0; |
1052 |
|
|
dp->mgf = NULL; |
1053 |
|
|
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
1054 |
|
|
dp->dim[0] = atof(ezxml_txt(geom)) * |
1055 |
|
|
to_meters(ezxml_attr(geom, "unit")); |
1056 |
|
|
if ((geom = ezxml_child(wdb, "Height")) != NULL) |
1057 |
|
|
dp->dim[1] = atof(ezxml_txt(geom)) * |
1058 |
|
|
to_meters(ezxml_attr(geom, "unit")); |
1059 |
|
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
1060 |
|
|
dp->dim[2] = atof(ezxml_txt(geom)) * |
1061 |
|
|
to_meters(ezxml_attr(geom, "unit")); |
1062 |
|
|
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
1063 |
|
|
(mgfstr = ezxml_txt(geom)) == NULL) |
1064 |
|
|
return; |
1065 |
|
|
if ((fmt = ezxml_attr(geom, "format")) != NULL && |
1066 |
|
|
strcasecmp(fmt, "MGF")) { |
1067 |
|
|
sprintf(errmsg, "unrecognized geometry format '%s'", fmt); |
1068 |
|
|
error(WARNING, errmsg); |
1069 |
|
|
return; |
1070 |
|
|
} |
1071 |
|
|
cfact = to_meters(ezxml_attr(geom, "unit")); |
1072 |
|
|
dp->mgf = (char *)malloc(strlen(mgfstr)+32); |
1073 |
|
|
if (dp->mgf == NULL) |
1074 |
|
|
error(SYSTEM, "out of memory in load_geometry"); |
1075 |
|
|
if (cfact < 0.99 || cfact > 1.01) |
1076 |
|
|
sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr); |
1077 |
|
|
else |
1078 |
|
|
strcpy(dp->mgf, mgfstr); |
1079 |
|
|
} |
1080 |
|
|
|
1081 |
|
|
|
1082 |
greg |
2.3 |
static void |
1083 |
greg |
2.1 |
load_bsdf_data( /* load BSDF distribution for this wavelength */ |
1084 |
|
|
struct BSDF_data *dp, |
1085 |
|
|
ezxml_t wdb |
1086 |
|
|
) |
1087 |
|
|
{ |
1088 |
|
|
char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
1089 |
|
|
char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
1090 |
|
|
char *sdata; |
1091 |
|
|
int i; |
1092 |
|
|
|
1093 |
greg |
2.4 |
if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { |
1094 |
greg |
2.1 |
error(WARNING, "missing column/row basis for BSDF"); |
1095 |
|
|
return; |
1096 |
|
|
} |
1097 |
|
|
for (i = nabases; i--; ) |
1098 |
greg |
2.12 |
if (!strcasecmp(cbasis, abase_list[i].name)) { |
1099 |
greg |
2.1 |
dp->ninc = abase_list[i].nangles; |
1100 |
|
|
dp->ib_priv = (void *)&abase_list[i]; |
1101 |
|
|
dp->ib_vec = ab_getvecR; |
1102 |
|
|
dp->ib_ndx = ab_getndxR; |
1103 |
|
|
dp->ib_ohm = ab_getohm; |
1104 |
|
|
break; |
1105 |
|
|
} |
1106 |
|
|
if (i < 0) { |
1107 |
greg |
2.4 |
sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis); |
1108 |
greg |
2.1 |
error(WARNING, errmsg); |
1109 |
|
|
return; |
1110 |
|
|
} |
1111 |
|
|
for (i = nabases; i--; ) |
1112 |
greg |
2.12 |
if (!strcasecmp(rbasis, abase_list[i].name)) { |
1113 |
greg |
2.1 |
dp->nout = abase_list[i].nangles; |
1114 |
|
|
dp->ob_priv = (void *)&abase_list[i]; |
1115 |
|
|
dp->ob_vec = ab_getvec; |
1116 |
|
|
dp->ob_ndx = ab_getndx; |
1117 |
|
|
dp->ob_ohm = ab_getohm; |
1118 |
|
|
break; |
1119 |
|
|
} |
1120 |
|
|
if (i < 0) { |
1121 |
greg |
2.16 |
sprintf(errmsg, "undefined RowAngleBasis '%s'", rbasis); |
1122 |
greg |
2.1 |
error(WARNING, errmsg); |
1123 |
|
|
return; |
1124 |
|
|
} |
1125 |
|
|
/* read BSDF data */ |
1126 |
|
|
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
1127 |
greg |
2.4 |
if (!sdata || !*sdata) { |
1128 |
greg |
2.1 |
error(WARNING, "missing BSDF ScatteringData"); |
1129 |
|
|
return; |
1130 |
|
|
} |
1131 |
|
|
dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout); |
1132 |
|
|
if (dp->bsdf == NULL) |
1133 |
|
|
error(SYSTEM, "out of memory in load_bsdf_data"); |
1134 |
|
|
for (i = 0; i < dp->ninc*dp->nout; i++) { |
1135 |
|
|
char *sdnext = fskip(sdata); |
1136 |
|
|
if (sdnext == NULL) { |
1137 |
|
|
error(WARNING, "bad/missing BSDF ScatteringData"); |
1138 |
|
|
free(dp->bsdf); dp->bsdf = NULL; |
1139 |
|
|
return; |
1140 |
|
|
} |
1141 |
|
|
while (*sdnext && isspace(*sdnext)) |
1142 |
|
|
sdnext++; |
1143 |
|
|
if (*sdnext == ',') sdnext++; |
1144 |
|
|
dp->bsdf[i] = atof(sdata); |
1145 |
|
|
sdata = sdnext; |
1146 |
|
|
} |
1147 |
|
|
while (isspace(*sdata)) |
1148 |
|
|
sdata++; |
1149 |
|
|
if (*sdata) { |
1150 |
|
|
sprintf(errmsg, "%d extra characters after BSDF ScatteringData", |
1151 |
greg |
2.5 |
(int)strlen(sdata)); |
1152 |
greg |
2.1 |
error(WARNING, errmsg); |
1153 |
|
|
} |
1154 |
|
|
} |
1155 |
|
|
|
1156 |
|
|
|
1157 |
|
|
static int |
1158 |
|
|
check_bsdf_data( /* check that BSDF data is sane */ |
1159 |
|
|
struct BSDF_data *dp |
1160 |
|
|
) |
1161 |
|
|
{ |
1162 |
greg |
2.2 |
double *omega_iarr, *omega_oarr; |
1163 |
greg |
2.7 |
double dom, contrib, hemi_total, full_total; |
1164 |
greg |
2.1 |
int nneg; |
1165 |
greg |
2.2 |
FVECT v; |
1166 |
greg |
2.1 |
int i, o; |
1167 |
|
|
|
1168 |
|
|
if (dp == NULL || dp->bsdf == NULL) |
1169 |
|
|
return(0); |
1170 |
greg |
2.2 |
omega_iarr = (double *)calloc(dp->ninc, sizeof(double)); |
1171 |
|
|
omega_oarr = (double *)calloc(dp->nout, sizeof(double)); |
1172 |
|
|
if ((omega_iarr == NULL) | (omega_oarr == NULL)) |
1173 |
greg |
2.1 |
error(SYSTEM, "out of memory in check_bsdf_data"); |
1174 |
greg |
2.2 |
/* incoming projected solid angles */ |
1175 |
|
|
hemi_total = .0; |
1176 |
|
|
for (i = dp->ninc; i--; ) { |
1177 |
|
|
dom = getBSDF_incohm(dp,i); |
1178 |
|
|
if (dom <= .0) { |
1179 |
|
|
error(WARNING, "zero/negative incoming solid angle"); |
1180 |
|
|
continue; |
1181 |
|
|
} |
1182 |
|
|
if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) { |
1183 |
|
|
error(WARNING, "illegal incoming BSDF direction"); |
1184 |
|
|
free(omega_iarr); free(omega_oarr); |
1185 |
|
|
return(0); |
1186 |
|
|
} |
1187 |
|
|
hemi_total += omega_iarr[i] = dom * -v[2]; |
1188 |
|
|
} |
1189 |
|
|
if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) { |
1190 |
|
|
sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%", |
1191 |
|
|
100.*(hemi_total/PI - 1.)); |
1192 |
|
|
error(WARNING, errmsg); |
1193 |
|
|
} |
1194 |
|
|
dom = PI / hemi_total; /* fix normalization */ |
1195 |
|
|
for (i = dp->ninc; i--; ) |
1196 |
|
|
omega_iarr[i] *= dom; |
1197 |
|
|
/* outgoing projected solid angles */ |
1198 |
greg |
2.1 |
hemi_total = .0; |
1199 |
|
|
for (o = dp->nout; o--; ) { |
1200 |
|
|
dom = getBSDF_outohm(dp,o); |
1201 |
|
|
if (dom <= .0) { |
1202 |
greg |
2.2 |
error(WARNING, "zero/negative outgoing solid angle"); |
1203 |
greg |
2.1 |
continue; |
1204 |
|
|
} |
1205 |
|
|
if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) { |
1206 |
|
|
error(WARNING, "illegal outgoing BSDF direction"); |
1207 |
greg |
2.2 |
free(omega_iarr); free(omega_oarr); |
1208 |
greg |
2.1 |
return(0); |
1209 |
|
|
} |
1210 |
greg |
2.2 |
hemi_total += omega_oarr[o] = dom * v[2]; |
1211 |
greg |
2.1 |
} |
1212 |
|
|
if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) { |
1213 |
|
|
sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%", |
1214 |
|
|
100.*(hemi_total/PI - 1.)); |
1215 |
|
|
error(WARNING, errmsg); |
1216 |
|
|
} |
1217 |
greg |
2.2 |
dom = PI / hemi_total; /* fix normalization */ |
1218 |
greg |
2.1 |
for (o = dp->nout; o--; ) |
1219 |
greg |
2.2 |
omega_oarr[o] *= dom; |
1220 |
|
|
nneg = 0; /* check outgoing totals */ |
1221 |
|
|
for (i = 0; i < dp->ninc; i++) { |
1222 |
greg |
2.1 |
hemi_total = .0; |
1223 |
|
|
for (o = dp->nout; o--; ) { |
1224 |
|
|
double f = BSDF_value(dp,i,o); |
1225 |
greg |
2.2 |
if (f >= .0) |
1226 |
|
|
hemi_total += f*omega_oarr[o]; |
1227 |
|
|
else { |
1228 |
|
|
nneg += (f < -FTINY); |
1229 |
|
|
BSDF_value(dp,i,o) = .0f; |
1230 |
|
|
} |
1231 |
greg |
2.1 |
} |
1232 |
greg |
2.8 |
if (hemi_total > 1.01) { |
1233 |
greg |
2.2 |
sprintf(errmsg, |
1234 |
|
|
"incoming BSDF direction %d passes %.1f%% of light", |
1235 |
|
|
i, 100.*hemi_total); |
1236 |
greg |
2.1 |
error(WARNING, errmsg); |
1237 |
|
|
} |
1238 |
|
|
} |
1239 |
greg |
2.2 |
if (nneg) { |
1240 |
|
|
sprintf(errmsg, "%d negative BSDF values (ignored)", nneg); |
1241 |
greg |
2.1 |
error(WARNING, errmsg); |
1242 |
|
|
} |
1243 |
greg |
2.7 |
full_total = .0; /* reverse roles and check again */ |
1244 |
greg |
2.2 |
for (o = 0; o < dp->nout; o++) { |
1245 |
|
|
hemi_total = .0; |
1246 |
|
|
for (i = dp->ninc; i--; ) |
1247 |
|
|
hemi_total += BSDF_value(dp,i,o) * omega_iarr[i]; |
1248 |
|
|
|
1249 |
greg |
2.8 |
if (hemi_total > 1.01) { |
1250 |
greg |
2.2 |
sprintf(errmsg, |
1251 |
|
|
"outgoing BSDF direction %d collects %.1f%% of light", |
1252 |
|
|
o, 100.*hemi_total); |
1253 |
|
|
error(WARNING, errmsg); |
1254 |
|
|
} |
1255 |
greg |
2.7 |
full_total += hemi_total*omega_oarr[o]; |
1256 |
|
|
} |
1257 |
|
|
full_total /= PI; |
1258 |
greg |
2.8 |
if (full_total > 1.00001) { |
1259 |
|
|
sprintf(errmsg, "BSDF transfers %.4f%% of light", |
1260 |
greg |
2.7 |
100.*full_total); |
1261 |
|
|
error(WARNING, errmsg); |
1262 |
greg |
2.2 |
} |
1263 |
|
|
free(omega_iarr); free(omega_oarr); |
1264 |
greg |
2.1 |
return(1); |
1265 |
|
|
} |
1266 |
|
|
|
1267 |
greg |
2.6 |
|
1268 |
greg |
2.1 |
struct BSDF_data * |
1269 |
|
|
load_BSDF( /* load BSDF data from file */ |
1270 |
|
|
char *fname |
1271 |
|
|
) |
1272 |
|
|
{ |
1273 |
|
|
char *path; |
1274 |
|
|
ezxml_t fl, wtl, wld, wdb; |
1275 |
|
|
struct BSDF_data *dp; |
1276 |
|
|
|
1277 |
|
|
path = getpath(fname, getrlibpath(), R_OK); |
1278 |
|
|
if (path == NULL) { |
1279 |
|
|
sprintf(errmsg, "cannot find BSDF file \"%s\"", fname); |
1280 |
|
|
error(WARNING, errmsg); |
1281 |
|
|
return(NULL); |
1282 |
|
|
} |
1283 |
|
|
fl = ezxml_parse_file(path); |
1284 |
|
|
if (fl == NULL) { |
1285 |
|
|
sprintf(errmsg, "cannot open BSDF \"%s\"", path); |
1286 |
|
|
error(WARNING, errmsg); |
1287 |
|
|
return(NULL); |
1288 |
|
|
} |
1289 |
|
|
if (ezxml_error(fl)[0]) { |
1290 |
|
|
sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl)); |
1291 |
|
|
error(WARNING, errmsg); |
1292 |
|
|
ezxml_free(fl); |
1293 |
|
|
return(NULL); |
1294 |
|
|
} |
1295 |
|
|
if (strcmp(ezxml_name(fl), "WindowElement")) { |
1296 |
|
|
sprintf(errmsg, |
1297 |
|
|
"BSDF \"%s\": top level node not 'WindowElement'", |
1298 |
|
|
path); |
1299 |
|
|
error(WARNING, errmsg); |
1300 |
|
|
ezxml_free(fl); |
1301 |
|
|
return(NULL); |
1302 |
|
|
} |
1303 |
|
|
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
1304 |
greg |
2.10 |
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 |
greg |
2.3 |
load_angle_basis(ezxml_child(ezxml_child(wtl, |
1315 |
|
|
"DataDefinition"), "AngleBasis")); |
1316 |
greg |
2.1 |
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
1317 |
greg |
2.6 |
load_geometry(dp, ezxml_child(wtl, "Material")); |
1318 |
greg |
2.1 |
for (wld = ezxml_child(wtl, "WavelengthData"); |
1319 |
|
|
wld != NULL; wld = wld->next) { |
1320 |
greg |
2.13 |
if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), |
1321 |
|
|
"Visible")) |
1322 |
greg |
2.1 |
continue; |
1323 |
greg |
2.13 |
for (wdb = ezxml_child(wld, "WavelengthDataBlock"); |
1324 |
|
|
wdb != NULL; wdb = wdb->next) |
1325 |
|
|
if (!strcasecmp(ezxml_txt(ezxml_child(wdb, |
1326 |
|
|
"WavelengthDataDirection")), |
1327 |
greg |
2.1 |
"Transmission Front")) |
1328 |
greg |
2.13 |
break; |
1329 |
|
|
if (wdb != NULL) { /* load front BTDF */ |
1330 |
|
|
load_bsdf_data(dp, wdb); |
1331 |
|
|
break; /* ignore the rest */ |
1332 |
|
|
} |
1333 |
greg |
2.1 |
} |
1334 |
|
|
ezxml_free(fl); /* done with XML file */ |
1335 |
|
|
if (!check_bsdf_data(dp)) { |
1336 |
|
|
sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path); |
1337 |
|
|
error(WARNING, errmsg); |
1338 |
|
|
free_BSDF(dp); |
1339 |
|
|
dp = NULL; |
1340 |
|
|
} |
1341 |
|
|
return(dp); |
1342 |
|
|
} |
1343 |
|
|
|
1344 |
|
|
|
1345 |
|
|
void |
1346 |
|
|
free_BSDF( /* free BSDF data structure */ |
1347 |
|
|
struct BSDF_data *b |
1348 |
|
|
) |
1349 |
|
|
{ |
1350 |
|
|
if (b == NULL) |
1351 |
|
|
return; |
1352 |
greg |
2.6 |
if (b->mgf != NULL) |
1353 |
|
|
free(b->mgf); |
1354 |
greg |
2.1 |
if (b->bsdf != NULL) |
1355 |
|
|
free(b->bsdf); |
1356 |
|
|
free(b); |
1357 |
|
|
} |
1358 |
|
|
|
1359 |
|
|
|
1360 |
|
|
int |
1361 |
|
|
r_BSDF_incvec( /* compute random input vector at given location */ |
1362 |
|
|
FVECT v, |
1363 |
|
|
struct BSDF_data *b, |
1364 |
|
|
int i, |
1365 |
|
|
double rv, |
1366 |
|
|
MAT4 xm |
1367 |
|
|
) |
1368 |
|
|
{ |
1369 |
|
|
FVECT pert; |
1370 |
|
|
double rad; |
1371 |
|
|
int j; |
1372 |
|
|
|
1373 |
|
|
if (!getBSDF_incvec(v, b, i)) |
1374 |
|
|
return(0); |
1375 |
|
|
rad = sqrt(getBSDF_incohm(b, i) / PI); |
1376 |
|
|
multisamp(pert, 3, rv); |
1377 |
|
|
for (j = 0; j < 3; j++) |
1378 |
|
|
v[j] += rad*(2.*pert[j] - 1.); |
1379 |
|
|
if (xm != NULL) |
1380 |
|
|
multv3(v, v, xm); |
1381 |
|
|
return(normalize(v) != 0.0); |
1382 |
|
|
} |
1383 |
|
|
|
1384 |
|
|
|
1385 |
|
|
int |
1386 |
|
|
r_BSDF_outvec( /* compute random output vector at given location */ |
1387 |
|
|
FVECT v, |
1388 |
|
|
struct BSDF_data *b, |
1389 |
|
|
int o, |
1390 |
|
|
double rv, |
1391 |
|
|
MAT4 xm |
1392 |
|
|
) |
1393 |
|
|
{ |
1394 |
|
|
FVECT pert; |
1395 |
|
|
double rad; |
1396 |
|
|
int j; |
1397 |
|
|
|
1398 |
|
|
if (!getBSDF_outvec(v, b, o)) |
1399 |
|
|
return(0); |
1400 |
|
|
rad = sqrt(getBSDF_outohm(b, o) / PI); |
1401 |
|
|
multisamp(pert, 3, rv); |
1402 |
|
|
for (j = 0; j < 3; j++) |
1403 |
|
|
v[j] += rad*(2.*pert[j] - 1.); |
1404 |
|
|
if (xm != NULL) |
1405 |
|
|
multv3(v, v, xm); |
1406 |
|
|
return(normalize(v) != 0.0); |
1407 |
|
|
} |
1408 |
|
|
|
1409 |
|
|
|
1410 |
|
|
static int |
1411 |
|
|
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
1412 |
|
|
char *xfarg[], |
1413 |
|
|
FVECT xp, |
1414 |
|
|
FVECT yp, |
1415 |
|
|
FVECT zp |
1416 |
|
|
) |
1417 |
|
|
{ |
1418 |
|
|
static char bufs[3][16]; |
1419 |
|
|
int bn = 0; |
1420 |
|
|
char **xfp = xfarg; |
1421 |
|
|
double theta; |
1422 |
|
|
|
1423 |
|
|
if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { |
1424 |
|
|
/* Special case for X' along Z-axis */ |
1425 |
|
|
theta = -atan2(yp[0], yp[1]); |
1426 |
|
|
*xfp++ = "-ry"; |
1427 |
|
|
*xfp++ = xp[2] < 0.0 ? "90" : "-90"; |
1428 |
|
|
*xfp++ = "-rz"; |
1429 |
|
|
sprintf(bufs[bn], "%f", theta*(180./PI)); |
1430 |
|
|
*xfp++ = bufs[bn++]; |
1431 |
|
|
return(xfp - xfarg); |
1432 |
|
|
} |
1433 |
|
|
theta = atan2(yp[2], zp[2]); |
1434 |
|
|
if (!FEQ(theta,0.0)) { |
1435 |
|
|
*xfp++ = "-rx"; |
1436 |
|
|
sprintf(bufs[bn], "%f", theta*(180./PI)); |
1437 |
|
|
*xfp++ = bufs[bn++]; |
1438 |
|
|
} |
1439 |
|
|
theta = asin(-xp[2]); |
1440 |
|
|
if (!FEQ(theta,0.0)) { |
1441 |
|
|
*xfp++ = "-ry"; |
1442 |
|
|
sprintf(bufs[bn], " %f", theta*(180./PI)); |
1443 |
|
|
*xfp++ = bufs[bn++]; |
1444 |
|
|
} |
1445 |
|
|
theta = atan2(xp[1], xp[0]); |
1446 |
|
|
if (!FEQ(theta,0.0)) { |
1447 |
|
|
*xfp++ = "-rz"; |
1448 |
|
|
sprintf(bufs[bn], "%f", theta*(180./PI)); |
1449 |
|
|
*xfp++ = bufs[bn++]; |
1450 |
|
|
} |
1451 |
|
|
*xfp = NULL; |
1452 |
|
|
return(xfp - xfarg); |
1453 |
|
|
} |
1454 |
|
|
|
1455 |
|
|
|
1456 |
|
|
int |
1457 |
|
|
getBSDF_xfm( /* compute BSDF orient. -> world orient. transform */ |
1458 |
|
|
MAT4 xm, |
1459 |
|
|
FVECT nrm, |
1460 |
greg |
2.6 |
UpDir ud, |
1461 |
|
|
char *xfbuf |
1462 |
greg |
2.1 |
) |
1463 |
|
|
{ |
1464 |
|
|
char *xfargs[7]; |
1465 |
|
|
XF myxf; |
1466 |
|
|
FVECT updir, xdest, ydest; |
1467 |
greg |
2.6 |
int i; |
1468 |
greg |
2.1 |
|
1469 |
|
|
updir[0] = updir[1] = updir[2] = 0.; |
1470 |
|
|
switch (ud) { |
1471 |
|
|
case UDzneg: |
1472 |
|
|
updir[2] = -1.; |
1473 |
|
|
break; |
1474 |
|
|
case UDyneg: |
1475 |
|
|
updir[1] = -1.; |
1476 |
|
|
break; |
1477 |
|
|
case UDxneg: |
1478 |
|
|
updir[0] = -1.; |
1479 |
|
|
break; |
1480 |
|
|
case UDxpos: |
1481 |
|
|
updir[0] = 1.; |
1482 |
|
|
break; |
1483 |
|
|
case UDypos: |
1484 |
|
|
updir[1] = 1.; |
1485 |
|
|
break; |
1486 |
|
|
case UDzpos: |
1487 |
|
|
updir[2] = 1.; |
1488 |
|
|
break; |
1489 |
|
|
case UDunknown: |
1490 |
|
|
return(0); |
1491 |
|
|
} |
1492 |
|
|
fcross(xdest, updir, nrm); |
1493 |
|
|
if (normalize(xdest) == 0.0) |
1494 |
|
|
return(0); |
1495 |
|
|
fcross(ydest, nrm, xdest); |
1496 |
|
|
xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs); |
1497 |
|
|
copymat4(xm, myxf.xfm); |
1498 |
greg |
2.6 |
if (xfbuf == NULL) |
1499 |
|
|
return(1); |
1500 |
|
|
/* return xf arguments as well */ |
1501 |
|
|
for (i = 0; xfargs[i] != NULL; i++) { |
1502 |
|
|
*xfbuf++ = ' '; |
1503 |
|
|
strcpy(xfbuf, xfargs[i]); |
1504 |
|
|
while (*xfbuf) ++xfbuf; |
1505 |
|
|
} |
1506 |
greg |
2.1 |
return(1); |
1507 |
|
|
} |
1508 |
greg |
2.15 |
|
1509 |
|
|
/*######### END DEPRECATED ROUTINES #######*/ |
1510 |
|
|
/*################################################################*/ |