10 |
|
* |
11 |
|
*/ |
12 |
|
|
13 |
+ |
#define _USE_MATH_DEFINES |
14 |
|
#include "rtio.h" |
15 |
|
#include <stdlib.h> |
16 |
|
#include <math.h> |
18 |
|
#include "ezxml.h" |
19 |
|
#include "bsdf.h" |
20 |
|
#include "bsdf_t.h" |
21 |
+ |
#include "hilbert.h" |
22 |
|
|
23 |
+ |
/* Callback function type for SDtraverseTre() */ |
24 |
+ |
typedef int SDtreCallback(float val, const double *cmin, |
25 |
+ |
double csiz, void *cptr); |
26 |
+ |
|
27 |
+ |
/* reference width maximum (1.0) */ |
28 |
+ |
static const unsigned iwbits = sizeof(unsigned)*4; |
29 |
+ |
static const unsigned iwmax = (1<<(sizeof(unsigned)*4))-1; |
30 |
+ |
/* maximum cumulative value */ |
31 |
+ |
static const unsigned cumlmax = ~0; |
32 |
+ |
/* constant z-vector */ |
33 |
+ |
static const FVECT zvec = {.0, .0, 1.}; |
34 |
+ |
|
35 |
+ |
/* Struct used for our distribution-building callback */ |
36 |
+ |
typedef struct { |
37 |
+ |
int nic; /* number of input coordinates */ |
38 |
+ |
unsigned alen; /* current array length */ |
39 |
+ |
unsigned nall; /* number of allocated entries */ |
40 |
+ |
unsigned wmin; /* minimum square size so far */ |
41 |
+ |
unsigned wmax; /* maximum square size */ |
42 |
+ |
struct outdir_s { |
43 |
+ |
unsigned hent; /* entering Hilbert index */ |
44 |
+ |
int wid; /* this square size */ |
45 |
+ |
float bsdf; /* BSDF for this square */ |
46 |
+ |
} *darr; /* output direction array */ |
47 |
+ |
} SDdistScaffold; |
48 |
+ |
|
49 |
|
/* Allocate a new scattering distribution node */ |
50 |
|
static SDNode * |
51 |
|
SDnewNode(int nd, int lg) |
63 |
|
} |
64 |
|
if (lg < 0) { |
65 |
|
st = (SDNode *)malloc(sizeof(SDNode) + |
66 |
< |
((1<<nd) - 1)*sizeof(st->u.t[0])); |
67 |
< |
if (st != NULL) |
68 |
< |
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd); |
69 |
< |
} else |
66 |
> |
sizeof(st->u.t[0])*((1<<nd) - 1)); |
67 |
> |
if (st == NULL) { |
68 |
> |
sprintf(SDerrorDetail, |
69 |
> |
"Cannot allocate %d branch BSDF tree", 1<<nd); |
70 |
> |
return NULL; |
71 |
> |
} |
72 |
> |
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd); |
73 |
> |
} else { |
74 |
|
st = (SDNode *)malloc(sizeof(SDNode) + |
75 |
< |
((1 << nd*lg) - 1)*sizeof(st->u.v[0])); |
76 |
< |
|
45 |
< |
if (st == NULL) { |
46 |
< |
if (lg < 0) |
75 |
> |
sizeof(st->u.v[0])*((1 << nd*lg) - 1)); |
76 |
> |
if (st == NULL) { |
77 |
|
sprintf(SDerrorDetail, |
48 |
– |
"Cannot allocate %d branch BSDF tree", nd); |
49 |
– |
else |
50 |
– |
sprintf(SDerrorDetail, |
78 |
|
"Cannot allocate %d BSDF leaves", 1 << nd*lg); |
79 |
< |
return NULL; |
79 |
> |
return NULL; |
80 |
> |
} |
81 |
|
} |
82 |
|
st->ndim = nd; |
83 |
|
st->log2GR = lg; |
86 |
|
|
87 |
|
/* Free an SD tree */ |
88 |
|
static void |
89 |
< |
SDfreeTre(void *p) |
89 |
> |
SDfreeTre(SDNode *st) |
90 |
|
{ |
91 |
< |
SDNode *st = (SDNode *)p; |
64 |
< |
int i; |
91 |
> |
int n; |
92 |
|
|
93 |
|
if (st == NULL) |
94 |
|
return; |
95 |
< |
for (i = (st->log2GR < 0) << st->ndim; i--; ) |
96 |
< |
SDfreeTre(st->u.t[i]); |
97 |
< |
free((void *)st); |
95 |
> |
for (n = (st->log2GR < 0) << st->ndim; n--; ) |
96 |
> |
SDfreeTre(st->u.t[n]); |
97 |
> |
free(st); |
98 |
|
} |
99 |
|
|
100 |
< |
#if 0 /* Unused and untested routines */ |
100 |
> |
/* Free a variable-resolution BSDF */ |
101 |
> |
static void |
102 |
> |
SDFreeBTre(void *p) |
103 |
> |
{ |
104 |
> |
SDTre *sdt = (SDTre *)p; |
105 |
|
|
106 |
+ |
if (sdt == NULL) |
107 |
+ |
return; |
108 |
+ |
SDfreeTre(sdt->st); |
109 |
+ |
free(sdt); |
110 |
+ |
} |
111 |
+ |
|
112 |
+ |
/* Fill branch's worth of grid values from subtree */ |
113 |
+ |
static void |
114 |
+ |
fill_grid_branch(float *dptr, const float *sptr, int nd, int shft) |
115 |
+ |
{ |
116 |
+ |
unsigned n = 1 << (shft-1); |
117 |
+ |
|
118 |
+ |
if (!--nd) { /* end on the line */ |
119 |
+ |
memcpy(dptr, sptr, sizeof(*dptr)*n); |
120 |
+ |
return; |
121 |
+ |
} |
122 |
+ |
while (n--) /* recurse on each slice */ |
123 |
+ |
fill_grid_branch(dptr + (n << shft*nd), |
124 |
+ |
sptr + (n << (shft-1)*nd), nd, shft); |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
/* Get pointer at appropriate offset for the given branch */ |
128 |
+ |
static float * |
129 |
+ |
grid_branch_start(SDNode *st, int n) |
130 |
+ |
{ |
131 |
+ |
unsigned skipsiz = 1 << (st->log2GR - 1); |
132 |
+ |
float *vptr = st->u.v; |
133 |
+ |
int i; |
134 |
+ |
|
135 |
+ |
for (i = st->ndim; i--; skipsiz <<= st->log2GR) |
136 |
+ |
if (1<<i & n) |
137 |
+ |
vptr += skipsiz; |
138 |
+ |
return vptr; |
139 |
+ |
} |
140 |
+ |
|
141 |
+ |
/* Simplify (consolidate) a tree by flattening uniform depth regions */ |
142 |
+ |
static SDNode * |
143 |
+ |
SDsimplifyTre(SDNode *st) |
144 |
+ |
{ |
145 |
+ |
int match, n; |
146 |
+ |
|
147 |
+ |
if (st == NULL) /* check for invalid tree */ |
148 |
+ |
return NULL; |
149 |
+ |
if (st->log2GR >= 0) /* grid just returns unaltered */ |
150 |
+ |
return st; |
151 |
+ |
match = 1; /* check if grids below match */ |
152 |
+ |
for (n = 0; n < 1<<st->ndim; n++) { |
153 |
+ |
if ((st->u.t[n] = SDsimplifyTre(st->u.t[n])) == NULL) |
154 |
+ |
return NULL; /* propogate error up call stack */ |
155 |
+ |
match &= (st->u.t[n]->log2GR == st->u.t[0]->log2GR); |
156 |
+ |
} |
157 |
+ |
if (match && (match = st->u.t[0]->log2GR) >= 0) { |
158 |
+ |
SDNode *stn = SDnewNode(st->ndim, match + 1); |
159 |
+ |
if (stn == NULL) /* out of memory? */ |
160 |
+ |
return st; |
161 |
+ |
/* transfer values to new grid */ |
162 |
+ |
for (n = 1 << st->ndim; n--; ) |
163 |
+ |
fill_grid_branch(grid_branch_start(stn, n), |
164 |
+ |
st->u.t[n]->u.v, stn->ndim, stn->log2GR); |
165 |
+ |
SDfreeTre(st); /* free old tree */ |
166 |
+ |
st = stn; /* return new one */ |
167 |
+ |
} |
168 |
+ |
return st; |
169 |
+ |
} |
170 |
+ |
|
171 |
+ |
/* Find smallest leaf in tree */ |
172 |
+ |
static double |
173 |
+ |
SDsmallestLeaf(const SDNode *st) |
174 |
+ |
{ |
175 |
+ |
if (st->log2GR < 0) { /* tree branches */ |
176 |
+ |
double lmin = 1.; |
177 |
+ |
int n; |
178 |
+ |
for (n = 1<<st->ndim; n--; ) { |
179 |
+ |
double lsiz = SDsmallestLeaf(st->u.t[n]); |
180 |
+ |
if (lsiz < lmin) |
181 |
+ |
lmin = lsiz; |
182 |
+ |
} |
183 |
+ |
return .5*lmin; |
184 |
+ |
} |
185 |
+ |
/* leaf grid width */ |
186 |
+ |
return 1. / (double)(1 << st->log2GR); |
187 |
+ |
} |
188 |
+ |
|
189 |
|
/* Add up N-dimensional hypercube array values over the given box */ |
190 |
|
static double |
191 |
< |
SDiterSum(const float *va, int nd, int siz, const int *imin, const int *imax) |
191 |
> |
SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax) |
192 |
|
{ |
193 |
+ |
const unsigned skipsiz = 1 << --nd*shft; |
194 |
|
double sum = .0; |
80 |
– |
unsigned skipsiz = 1; |
195 |
|
int i; |
196 |
< |
|
197 |
< |
for (i = nd; --i > 0; ) |
198 |
< |
skipsiz *= siz; |
196 |
> |
|
197 |
> |
va += *imin * skipsiz; |
198 |
> |
|
199 |
|
if (skipsiz == 1) |
200 |
|
for (i = *imin; i < *imax; i++) |
201 |
< |
sum += va[i]; |
201 |
> |
sum += *va++; |
202 |
|
else |
203 |
< |
for (i = *imin; i < *imax; i++) |
204 |
< |
sum += SDiterSum(va + i*skipsiz, |
91 |
< |
nd-1, siz, imin+1, imax+1); |
203 |
> |
for (i = *imin; i < *imax; i++, va += skipsiz) |
204 |
> |
sum += SDiterSum(va, nd, shft, imin+1, imax+1); |
205 |
|
return sum; |
206 |
|
} |
207 |
|
|
208 |
|
/* Average BSDF leaves over an orthotope defined by the unit hypercube */ |
209 |
|
static double |
210 |
< |
SDavgBox(const SDNode *st, const double *bmin, const double *bmax) |
210 |
> |
SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax) |
211 |
|
{ |
99 |
– |
int imin[SD_MAXDIM], imax[SD_MAXDIM]; |
212 |
|
unsigned n; |
213 |
|
int i; |
214 |
|
|
218 |
|
for (i = st->ndim; i--; ) { |
219 |
|
if (bmin[i] >= 1.) |
220 |
|
return .0; |
221 |
< |
if (bmax[i] <= .0) |
221 |
> |
if (bmax[i] <= 0) |
222 |
|
return .0; |
223 |
|
if (bmin[i] >= bmax[i]) |
224 |
|
return .0; |
226 |
|
if (st->log2GR < 0) { /* iterate on subtree */ |
227 |
|
double sum = .0, wsum = 1e-20; |
228 |
|
double sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w; |
117 |
– |
|
229 |
|
for (n = 1 << st->ndim; n--; ) { |
230 |
|
w = 1.; |
231 |
|
for (i = st->ndim; i--; ) { |
237 |
|
} |
238 |
|
if (sbmin[i] < .0) sbmin[i] = .0; |
239 |
|
if (sbmax[i] > 1.) sbmax[i] = 1.; |
240 |
+ |
if (sbmin[i] >= sbmax[i]) { |
241 |
+ |
w = .0; |
242 |
+ |
break; |
243 |
+ |
} |
244 |
|
w *= sbmax[i] - sbmin[i]; |
245 |
|
} |
246 |
|
if (w > 1e-10) { |
247 |
< |
sum += w * SDavgBox(st->u.t[n], sbmin, sbmax); |
247 |
> |
sum += w * SDavgTreBox(st->u.t[n], sbmin, sbmax); |
248 |
|
wsum += w; |
249 |
|
} |
250 |
|
} |
251 |
|
return sum / wsum; |
252 |
+ |
} else { /* iterate over leaves */ |
253 |
+ |
int imin[SD_MAXDIM], imax[SD_MAXDIM]; |
254 |
+ |
|
255 |
+ |
n = 1; |
256 |
+ |
for (i = st->ndim; i--; ) { |
257 |
+ |
imin[i] = (bmin[i] <= 0) ? 0 : |
258 |
+ |
(int)((1 << st->log2GR)*bmin[i]); |
259 |
+ |
imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) : |
260 |
+ |
(int)((1 << st->log2GR)*bmax[i] + .999999); |
261 |
+ |
n *= imax[i] - imin[i]; |
262 |
+ |
} |
263 |
+ |
if (n) |
264 |
+ |
return SDiterSum(st->u.v, st->ndim, |
265 |
+ |
st->log2GR, imin, imax) / (double)n; |
266 |
|
} |
267 |
< |
n = 1; /* iterate over leaves */ |
268 |
< |
for (i = st->ndim; i--; ) { |
269 |
< |
imin[i] = (bmin[i] <= .0) ? 0 |
270 |
< |
: (int)((1 << st->log2GR)*bmin[i]); |
271 |
< |
imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) |
272 |
< |
: (int)((1 << st->log2GR)*bmax[i] + .999999); |
273 |
< |
n *= imax[i] - imin[i]; |
267 |
> |
return .0; |
268 |
> |
} |
269 |
> |
|
270 |
> |
/* Recursive call for SDtraverseTre() */ |
271 |
> |
static int |
272 |
> |
SDdotravTre(const SDNode *st, const double *pos, int cmask, |
273 |
> |
SDtreCallback *cf, void *cptr, |
274 |
> |
const double *cmin, double csiz) |
275 |
> |
{ |
276 |
> |
int rv, rval = 0; |
277 |
> |
double bmin[SD_MAXDIM]; |
278 |
> |
int i, n; |
279 |
> |
/* in branches? */ |
280 |
> |
if (st->log2GR < 0) { |
281 |
> |
unsigned skipmask = 0; |
282 |
> |
csiz *= .5; |
283 |
> |
for (i = st->ndim; i--; ) |
284 |
> |
if (1<<i & cmask) |
285 |
> |
if (pos[i] < cmin[i] + csiz) |
286 |
> |
for (n = 1 << st->ndim; n--; ) { |
287 |
> |
if (n & 1<<i) |
288 |
> |
skipmask |= 1<<n; |
289 |
> |
} |
290 |
> |
else |
291 |
> |
for (n = 1 << st->ndim; n--; ) { |
292 |
> |
if (!(n & 1<<i)) |
293 |
> |
skipmask |= 1<<n; |
294 |
> |
} |
295 |
> |
for (n = 1 << st->ndim; n--; ) { |
296 |
> |
if (1<<n & skipmask) |
297 |
> |
continue; |
298 |
> |
for (i = st->ndim; i--; ) |
299 |
> |
if (1<<i & n) |
300 |
> |
bmin[i] = cmin[i] + csiz; |
301 |
> |
else |
302 |
> |
bmin[i] = cmin[i]; |
303 |
> |
|
304 |
> |
rval += rv = SDdotravTre(st->u.t[n], pos, cmask, |
305 |
> |
cf, cptr, bmin, csiz); |
306 |
> |
if (rv < 0) |
307 |
> |
return rv; |
308 |
> |
} |
309 |
> |
} else { /* else traverse leaves */ |
310 |
> |
int clim[SD_MAXDIM][2]; |
311 |
> |
int cpos[SD_MAXDIM]; |
312 |
> |
|
313 |
> |
if (st->log2GR == 0) /* short cut */ |
314 |
> |
return (*cf)(st->u.v[0], cmin, csiz, cptr); |
315 |
> |
|
316 |
> |
csiz /= (double)(1 << st->log2GR); |
317 |
> |
/* assign coord. ranges */ |
318 |
> |
for (i = st->ndim; i--; ) |
319 |
> |
if (1<<i & cmask) { |
320 |
> |
clim[i][0] = (pos[i] - cmin[i])/csiz; |
321 |
> |
/* check overflow from f.p. error */ |
322 |
> |
clim[i][0] -= clim[i][0] >> st->log2GR; |
323 |
> |
clim[i][1] = clim[i][0] + 1; |
324 |
> |
} else { |
325 |
> |
clim[i][0] = 0; |
326 |
> |
clim[i][1] = 1 << st->log2GR; |
327 |
> |
} |
328 |
> |
#if (SD_MAXDIM == 4) |
329 |
> |
bmin[0] = cmin[0] + csiz*clim[0][0]; |
330 |
> |
for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) { |
331 |
> |
bmin[1] = cmin[1] + csiz*clim[1][0]; |
332 |
> |
for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) { |
333 |
> |
bmin[2] = cmin[2] + csiz*clim[2][0]; |
334 |
> |
if (st->ndim == 3) { |
335 |
> |
cpos[2] = clim[2][0]; |
336 |
> |
n = cpos[0]; |
337 |
> |
for (i = 1; i < 3; i++) |
338 |
> |
n = (n << st->log2GR) + cpos[i]; |
339 |
> |
for ( ; cpos[2] < clim[2][1]; cpos[2]++) { |
340 |
> |
rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr); |
341 |
> |
if (rv < 0) |
342 |
> |
return rv; |
343 |
> |
bmin[2] += csiz; |
344 |
> |
} |
345 |
> |
} else { |
346 |
> |
for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) { |
347 |
> |
bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]); |
348 |
> |
n = cpos[0]; |
349 |
> |
for (i = 1; i < 4; i++) |
350 |
> |
n = (n << st->log2GR) + cpos[i]; |
351 |
> |
for ( ; cpos[3] < clim[3][1]; cpos[3]++) { |
352 |
> |
rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr); |
353 |
> |
if (rv < 0) |
354 |
> |
return rv; |
355 |
> |
bmin[3] += csiz; |
356 |
> |
} |
357 |
> |
bmin[2] += csiz; |
358 |
> |
} |
359 |
> |
} |
360 |
> |
bmin[1] += csiz; |
361 |
> |
} |
362 |
> |
bmin[0] += csiz; |
363 |
> |
} |
364 |
> |
#else |
365 |
> |
_!_ "broken code segment!" |
366 |
> |
#endif |
367 |
|
} |
368 |
< |
if (!n) |
147 |
< |
return .0; |
148 |
< |
|
149 |
< |
return SDiterSum(st->u.v, st->ndim, 1 << st->log2GR, imin, imax) / |
150 |
< |
(double)n; |
368 |
> |
return rval; |
369 |
|
} |
370 |
|
|
371 |
< |
#endif /* 0 */ |
371 |
> |
/* Traverse a tree, visiting nodes in a slice that fits partial position */ |
372 |
> |
static int |
373 |
> |
SDtraverseTre(const SDNode *st, const double *pos, int cmask, |
374 |
> |
SDtreCallback *cf, void *cptr) |
375 |
> |
{ |
376 |
> |
static double czero[SD_MAXDIM]; |
377 |
> |
int i; |
378 |
> |
/* check arguments */ |
379 |
> |
if ((st == NULL) | (cf == NULL)) |
380 |
> |
return -1; |
381 |
> |
for (i = st->ndim; i--; ) |
382 |
> |
if (1<<i & cmask && (pos[i] < 0) | (pos[i] >= 1.)) |
383 |
> |
return -1; |
384 |
|
|
385 |
+ |
return SDdotravTre(st, pos, cmask, cf, cptr, czero, 1.); |
386 |
+ |
} |
387 |
+ |
|
388 |
|
/* Look up tree value at the given grid position */ |
389 |
|
static float |
390 |
< |
SDlookupTre(const SDNode *st, const double *pos) |
390 |
> |
SDlookupTre(const SDNode *st, const double *pos, double *hcube) |
391 |
|
{ |
392 |
|
double spos[SD_MAXDIM]; |
393 |
|
int i, n, t; |
394 |
+ |
/* initialize voxel return */ |
395 |
+ |
if (hcube) { |
396 |
+ |
hcube[i = st->ndim] = 1.; |
397 |
+ |
while (i--) |
398 |
+ |
hcube[i] = .0; |
399 |
+ |
} |
400 |
|
/* climb the tree */ |
401 |
|
while (st->log2GR < 0) { |
402 |
|
n = 0; /* move to appropriate branch */ |
403 |
+ |
if (hcube) hcube[st->ndim] *= .5; |
404 |
|
for (i = st->ndim; i--; ) { |
405 |
|
spos[i] = 2.*pos[i]; |
406 |
|
t = (spos[i] >= 1.); |
407 |
|
n |= t<<i; |
408 |
|
spos[i] -= (double)t; |
409 |
+ |
if (hcube) hcube[i] += (double)t * hcube[st->ndim]; |
410 |
|
} |
411 |
|
st = st->u.t[n]; /* avoids tail recursion */ |
412 |
|
pos = spos; |
413 |
|
} |
414 |
+ |
if (st->log2GR == 0) /* short cut */ |
415 |
+ |
return st->u.v[0]; |
416 |
|
n = t = 0; /* find grid array index */ |
417 |
|
for (i = st->ndim; i--; ) { |
418 |
|
n += (int)((1<<st->log2GR)*pos[i]) << t; |
419 |
|
t += st->log2GR; |
420 |
|
} |
421 |
< |
return st->u.v[n]; /* XXX no interpolation */ |
421 |
> |
if (hcube) { /* compute final hypercube */ |
422 |
> |
hcube[st->ndim] /= (double)(1<<st->log2GR); |
423 |
> |
for (i = st->ndim; i--; ) |
424 |
> |
hcube[i] += floor((1<<st->log2GR)*pos[i])*hcube[st->ndim]; |
425 |
> |
} |
426 |
> |
return st->u.v[n]; /* no interpolation */ |
427 |
|
} |
428 |
|
|
429 |
+ |
/* Query BSDF value and sample hypercube for the given vectors */ |
430 |
+ |
static float |
431 |
+ |
SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc) |
432 |
+ |
{ |
433 |
+ |
FVECT rOutVec; |
434 |
+ |
double gridPos[4]; |
435 |
+ |
|
436 |
+ |
switch (sdt->sidef) { /* whose side are you on? */ |
437 |
+ |
case SD_UFRONT: |
438 |
+ |
if ((outVec[2] < 0) | (inVec[2] < 0)) |
439 |
+ |
return -1.; |
440 |
+ |
break; |
441 |
+ |
case SD_UBACK: |
442 |
+ |
if ((outVec[2] > 0) | (inVec[2] > 0)) |
443 |
+ |
return -1.; |
444 |
+ |
break; |
445 |
+ |
case SD_XMIT: |
446 |
+ |
if ((outVec[2] > 0) == (inVec[2] > 0)) |
447 |
+ |
return -1.; |
448 |
+ |
break; |
449 |
+ |
default: |
450 |
+ |
return -1.; |
451 |
+ |
} |
452 |
+ |
/* convert vector coordinates */ |
453 |
+ |
if (sdt->st->ndim == 3) { |
454 |
+ |
spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0])); |
455 |
+ |
gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
456 |
+ |
SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]); |
457 |
+ |
} else if (sdt->st->ndim == 4) { |
458 |
+ |
SDdisk2square(gridPos, -inVec[0], -inVec[1]); |
459 |
+ |
SDdisk2square(gridPos+2, outVec[0], outVec[1]); |
460 |
+ |
} else |
461 |
+ |
return -1.; /* should be internal error */ |
462 |
+ |
|
463 |
+ |
return SDlookupTre(sdt->st, gridPos, hc); |
464 |
+ |
} |
465 |
+ |
|
466 |
|
/* Compute non-diffuse component for variable-resolution BSDF */ |
467 |
|
static int |
468 |
|
SDgetTreBSDF(float coef[SDmaxCh], const FVECT outVec, |
469 |
< |
const FVECT inVec, const void *dist) |
469 |
> |
const FVECT inVec, SDComponent *sdc) |
470 |
|
{ |
471 |
< |
const SDNode *st = (const SDNode *)dist; |
472 |
< |
double gridPos[4]; |
473 |
< |
/* convert vector coordinates */ |
474 |
< |
if (st->ndim == 3) { /* reduce for isotropic BSDF? */ |
190 |
< |
static const FVECT zvec = {.0, .0, 1.}; |
191 |
< |
FVECT rOutVec; |
192 |
< |
spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0])); |
193 |
< |
SDdisk2square(gridPos, rOutVec[0], rOutVec[1]); |
194 |
< |
gridPos[2] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
195 |
< |
} else if (st->ndim == 4) { /* XXX no component identity checks */ |
196 |
< |
SDdisk2square(gridPos, outVec[0], outVec[1]); |
197 |
< |
SDdisk2square(gridPos+2, -inVec[0], -inVec[1]); |
198 |
< |
} else |
199 |
< |
return 0; /* should be internal error */ |
471 |
> |
/* check arguments */ |
472 |
> |
if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL) |
473 |
> |
|| sdc->dist == NULL) |
474 |
> |
return 0; |
475 |
|
/* get nearest BSDF value */ |
476 |
< |
coef[0] = SDlookupTre(st, gridPos); |
477 |
< |
return 1; /* monochromatic for now */ |
476 |
> |
coef[0] = SDqueryTre((SDTre *)sdc->dist, outVec, inVec, NULL); |
477 |
> |
return (coef[0] >= 0); /* monochromatic for now */ |
478 |
|
} |
479 |
|
|
480 |
+ |
/* Callback to build cumulative distribution using SDtraverseTre() */ |
481 |
+ |
static int |
482 |
+ |
build_scaffold(float val, const double *cmin, double csiz, void *cptr) |
483 |
+ |
{ |
484 |
+ |
SDdistScaffold *sp = (SDdistScaffold *)cptr; |
485 |
+ |
int wid = csiz*(double)iwmax + .5; |
486 |
+ |
bitmask_t bmin[2], bmax[2]; |
487 |
+ |
|
488 |
+ |
cmin += sp->nic; /* skip to output coords */ |
489 |
+ |
if (wid < sp->wmin) /* new minimum width? */ |
490 |
+ |
sp->wmin = wid; |
491 |
+ |
if (wid > sp->wmax) /* new maximum? */ |
492 |
+ |
sp->wmax = wid; |
493 |
+ |
if (sp->alen >= sp->nall) { /* need more space? */ |
494 |
+ |
struct outdir_s *ndarr; |
495 |
+ |
sp->nall += 1024; |
496 |
+ |
ndarr = (struct outdir_s *)realloc(sp->darr, |
497 |
+ |
sizeof(struct outdir_s)*sp->nall); |
498 |
+ |
if (ndarr == NULL) { |
499 |
+ |
sprintf(SDerrorDetail, |
500 |
+ |
"Cannot grow scaffold to %u entries", sp->nall); |
501 |
+ |
return -1; /* abort build */ |
502 |
+ |
} |
503 |
+ |
sp->darr = ndarr; |
504 |
+ |
} |
505 |
+ |
/* find Hilbert entry index */ |
506 |
+ |
bmin[0] = cmin[0]*(double)iwmax + .5; |
507 |
+ |
bmin[1] = cmin[1]*(double)iwmax + .5; |
508 |
+ |
bmax[0] = bmin[0] + wid-1; |
509 |
+ |
bmax[1] = bmin[1] + wid-1; |
510 |
+ |
hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax); |
511 |
+ |
sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin); |
512 |
+ |
sp->darr[sp->alen].wid = wid; |
513 |
+ |
sp->darr[sp->alen].bsdf = val; |
514 |
+ |
sp->alen++; /* on to the next entry */ |
515 |
+ |
return 0; |
516 |
+ |
} |
517 |
+ |
|
518 |
+ |
/* Scaffold comparison function for qsort -- ascending Hilbert index */ |
519 |
+ |
static int |
520 |
+ |
sscmp(const void *p1, const void *p2) |
521 |
+ |
{ |
522 |
+ |
unsigned h1 = (*(const struct outdir_s *)p1).hent; |
523 |
+ |
unsigned h2 = (*(const struct outdir_s *)p2).hent; |
524 |
+ |
|
525 |
+ |
if (h1 > h2) |
526 |
+ |
return 1; |
527 |
+ |
if (h1 < h2) |
528 |
+ |
return -1; |
529 |
+ |
return 0; |
530 |
+ |
} |
531 |
+ |
|
532 |
+ |
/* Create a new cumulative distribution for the given input direction */ |
533 |
+ |
static SDTreCDst * |
534 |
+ |
make_cdist(const SDTre *sdt, const double *pos) |
535 |
+ |
{ |
536 |
+ |
SDdistScaffold myScaffold; |
537 |
+ |
SDTreCDst *cd; |
538 |
+ |
struct outdir_s *sp; |
539 |
+ |
double scale, cursum; |
540 |
+ |
int i; |
541 |
+ |
/* initialize scaffold */ |
542 |
+ |
myScaffold.wmin = iwmax; |
543 |
+ |
myScaffold.wmax = 0; |
544 |
+ |
myScaffold.nic = sdt->st->ndim - 2; |
545 |
+ |
myScaffold.alen = 0; |
546 |
+ |
myScaffold.nall = 512; |
547 |
+ |
myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) * |
548 |
+ |
myScaffold.nall); |
549 |
+ |
if (myScaffold.darr == NULL) |
550 |
+ |
return NULL; |
551 |
+ |
/* grow the distribution */ |
552 |
+ |
if (SDtraverseTre(sdt->st, pos, (1<<myScaffold.nic)-1, |
553 |
+ |
&build_scaffold, &myScaffold) < 0) { |
554 |
+ |
free(myScaffold.darr); |
555 |
+ |
return NULL; |
556 |
+ |
} |
557 |
+ |
/* allocate result holder */ |
558 |
+ |
cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) + |
559 |
+ |
sizeof(cd->carr[0])*myScaffold.alen); |
560 |
+ |
if (cd == NULL) { |
561 |
+ |
sprintf(SDerrorDetail, |
562 |
+ |
"Cannot allocate %u entry cumulative distribution", |
563 |
+ |
myScaffold.alen); |
564 |
+ |
free(myScaffold.darr); |
565 |
+ |
return NULL; |
566 |
+ |
} |
567 |
+ |
cd->isodist = (myScaffold.nic == 1); |
568 |
+ |
/* sort the distribution */ |
569 |
+ |
qsort(myScaffold.darr, cd->calen = myScaffold.alen, |
570 |
+ |
sizeof(struct outdir_s), &sscmp); |
571 |
+ |
|
572 |
+ |
/* record input range */ |
573 |
+ |
scale = myScaffold.wmin / (double)iwmax; |
574 |
+ |
for (i = myScaffold.nic; i--; ) { |
575 |
+ |
cd->clim[i][0] = floor(pos[i]/scale) * scale; |
576 |
+ |
cd->clim[i][1] = cd->clim[i][0] + scale; |
577 |
+ |
} |
578 |
+ |
if (cd->isodist) { /* avoid issue in SDqueryTreProjSA() */ |
579 |
+ |
cd->clim[1][0] = cd->clim[0][0]; |
580 |
+ |
cd->clim[1][1] = cd->clim[0][1]; |
581 |
+ |
} |
582 |
+ |
cd->max_psa = myScaffold.wmax / (double)iwmax; |
583 |
+ |
cd->max_psa *= cd->max_psa * M_PI; |
584 |
+ |
cd->sidef = sdt->sidef; |
585 |
+ |
cd->cTotal = 1e-20; /* compute directional total */ |
586 |
+ |
sp = myScaffold.darr; |
587 |
+ |
for (i = myScaffold.alen; i--; sp++) |
588 |
+ |
cd->cTotal += sp->bsdf * (double)sp->wid * sp->wid; |
589 |
+ |
cursum = .0; /* go back and get cumulative values */ |
590 |
+ |
scale = (double)cumlmax / cd->cTotal; |
591 |
+ |
sp = myScaffold.darr; |
592 |
+ |
for (i = 0; i < cd->calen; i++, sp++) { |
593 |
+ |
cd->carr[i].hndx = sp->hent; |
594 |
+ |
cd->carr[i].cuml = scale*cursum + .5; |
595 |
+ |
cursum += sp->bsdf * (double)sp->wid * sp->wid; |
596 |
+ |
} |
597 |
+ |
cd->carr[i].hndx = ~0; /* make final entry */ |
598 |
+ |
cd->carr[i].cuml = cumlmax; |
599 |
+ |
cd->cTotal *= M_PI/(double)iwmax/iwmax; |
600 |
+ |
/* all done, clean up and return */ |
601 |
+ |
free(myScaffold.darr); |
602 |
+ |
return cd; |
603 |
+ |
} |
604 |
+ |
|
605 |
+ |
/* Find or allocate a cumulative distribution for the given incoming vector */ |
606 |
+ |
const SDCDst * |
607 |
+ |
SDgetTreCDist(const FVECT inVec, SDComponent *sdc) |
608 |
+ |
{ |
609 |
+ |
const SDTre *sdt; |
610 |
+ |
double inCoord[2]; |
611 |
+ |
int i; |
612 |
+ |
SDTreCDst *cd, *cdlast; |
613 |
+ |
/* check arguments */ |
614 |
+ |
if ((inVec == NULL) | (sdc == NULL) || |
615 |
+ |
(sdt = (SDTre *)sdc->dist) == NULL) |
616 |
+ |
return NULL; |
617 |
+ |
if (sdt->st->ndim == 3) /* isotropic BSDF? */ |
618 |
+ |
inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
619 |
+ |
else if (sdt->st->ndim == 4) |
620 |
+ |
SDdisk2square(inCoord, -inVec[0], -inVec[1]); |
621 |
+ |
else |
622 |
+ |
return NULL; /* should be internal error */ |
623 |
+ |
cdlast = NULL; /* check for direction in cache list */ |
624 |
+ |
for (cd = (SDTreCDst *)sdc->cdList; cd != NULL; |
625 |
+ |
cdlast = cd, cd = (SDTreCDst *)cd->next) { |
626 |
+ |
for (i = sdt->st->ndim - 2; i--; ) |
627 |
+ |
if ((cd->clim[i][0] > inCoord[i]) | |
628 |
+ |
(inCoord[i] >= cd->clim[i][1])) |
629 |
+ |
break; |
630 |
+ |
if (i < 0) |
631 |
+ |
break; /* means we have a match */ |
632 |
+ |
} |
633 |
+ |
if (cd == NULL) /* need to create new entry? */ |
634 |
+ |
cdlast = cd = make_cdist(sdt, inCoord); |
635 |
+ |
if (cdlast != NULL) { /* move entry to head of cache list */ |
636 |
+ |
cdlast->next = cd->next; |
637 |
+ |
cd->next = sdc->cdList; |
638 |
+ |
sdc->cdList = (SDCDst *)cd; |
639 |
+ |
} |
640 |
+ |
return (SDCDst *)cd; /* ready to go */ |
641 |
+ |
} |
642 |
+ |
|
643 |
+ |
/* Query solid angle for vector(s) */ |
644 |
+ |
static SDError |
645 |
+ |
SDqueryTreProjSA(double *psa, const FVECT v1, const RREAL *v2, |
646 |
+ |
int qflags, SDComponent *sdc) |
647 |
+ |
{ |
648 |
+ |
double myPSA[2]; |
649 |
+ |
/* check arguments */ |
650 |
+ |
if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) || |
651 |
+ |
sdc->dist == NULL) |
652 |
+ |
return SDEargument; |
653 |
+ |
/* get projected solid angle(s) */ |
654 |
+ |
if (v2 != NULL) { |
655 |
+ |
const SDTre *sdt = (SDTre *)sdc->dist; |
656 |
+ |
double hcube[SD_MAXDIM]; |
657 |
+ |
if (SDqueryTre(sdt, v1, v2, hcube) < 0) { |
658 |
+ |
strcpy(SDerrorDetail, "Bad call to SDqueryTreProjSA"); |
659 |
+ |
return SDEinternal; |
660 |
+ |
} |
661 |
+ |
myPSA[0] = hcube[sdt->st->ndim]; |
662 |
+ |
myPSA[1] = myPSA[0] *= myPSA[0] * M_PI; |
663 |
+ |
} else { |
664 |
+ |
const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc); |
665 |
+ |
if (cd == NULL) |
666 |
+ |
return SDEmemory; |
667 |
+ |
myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) * |
668 |
+ |
(cd->clim[1][1] - cd->clim[1][0]); |
669 |
+ |
myPSA[1] = cd->max_psa; |
670 |
+ |
} |
671 |
+ |
switch (qflags) { /* record based on flag settings */ |
672 |
+ |
case SDqueryVal: |
673 |
+ |
*psa = myPSA[0]; |
674 |
+ |
break; |
675 |
+ |
case SDqueryMax: |
676 |
+ |
if (myPSA[1] > *psa) |
677 |
+ |
*psa = myPSA[1]; |
678 |
+ |
break; |
679 |
+ |
case SDqueryMin+SDqueryMax: |
680 |
+ |
if (myPSA[1] > psa[1]) |
681 |
+ |
psa[1] = myPSA[1]; |
682 |
+ |
/* fall through */ |
683 |
+ |
case SDqueryMin: |
684 |
+ |
if (myPSA[0] < psa[0]) |
685 |
+ |
psa[0] = myPSA[0]; |
686 |
+ |
break; |
687 |
+ |
} |
688 |
+ |
return SDEnone; |
689 |
+ |
} |
690 |
+ |
|
691 |
+ |
/* Sample cumulative distribution */ |
692 |
+ |
static SDError |
693 |
+ |
SDsampTreCDist(FVECT ioVec, double randX, const SDCDst *cdp) |
694 |
+ |
{ |
695 |
+ |
const unsigned nBitsC = 4*sizeof(bitmask_t); |
696 |
+ |
const unsigned nExtraBits = 8*(sizeof(bitmask_t)-sizeof(unsigned)); |
697 |
+ |
const SDTreCDst *cd = (const SDTreCDst *)cdp; |
698 |
+ |
const unsigned target = randX*cumlmax; |
699 |
+ |
bitmask_t hndx, hcoord[2]; |
700 |
+ |
double gpos[3], rotangle; |
701 |
+ |
int i, iupper, ilower; |
702 |
+ |
/* check arguments */ |
703 |
+ |
if ((ioVec == NULL) | (cd == NULL)) |
704 |
+ |
return SDEargument; |
705 |
+ |
if (ioVec[2] > 0) { |
706 |
+ |
if (!(cd->sidef & SD_UFRONT)) |
707 |
+ |
return SDEargument; |
708 |
+ |
} else if (!(cd->sidef & SD_UBACK)) |
709 |
+ |
return SDEargument; |
710 |
+ |
/* binary search to find position */ |
711 |
+ |
ilower = 0; iupper = cd->calen; |
712 |
+ |
while ((i = (iupper + ilower) >> 1) != ilower) |
713 |
+ |
if (target >= cd->carr[i].cuml) |
714 |
+ |
ilower = i; |
715 |
+ |
else |
716 |
+ |
iupper = i; |
717 |
+ |
/* localize random position */ |
718 |
+ |
randX = (randX*cumlmax - cd->carr[ilower].cuml) / |
719 |
+ |
(double)(cd->carr[iupper].cuml - cd->carr[ilower].cuml); |
720 |
+ |
/* index in longer Hilbert curve */ |
721 |
+ |
hndx = (randX*cd->carr[iupper].hndx + (1.-randX)*cd->carr[ilower].hndx) |
722 |
+ |
* (double)((bitmask_t)1 << nExtraBits); |
723 |
+ |
/* convert Hilbert index to vector */ |
724 |
+ |
hilbert_i2c(2, nBitsC, hndx, hcoord); |
725 |
+ |
for (i = 2; i--; ) |
726 |
+ |
gpos[i] = ((double)hcoord[i] + rand()*(1./(RAND_MAX+.5))) / |
727 |
+ |
(double)((bitmask_t)1 << nBitsC); |
728 |
+ |
SDsquare2disk(gpos, gpos[0], gpos[1]); |
729 |
+ |
/* compute Z-coordinate */ |
730 |
+ |
gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1]; |
731 |
+ |
if (gpos[2] > 0) /* paranoia, I hope */ |
732 |
+ |
gpos[2] = sqrt(gpos[2]); |
733 |
+ |
/* emit from back? */ |
734 |
+ |
if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT) |
735 |
+ |
gpos[2] = -gpos[2]; |
736 |
+ |
if (cd->isodist) { /* rotate isotropic result */ |
737 |
+ |
rotangle = atan2(-ioVec[1],-ioVec[0]); |
738 |
+ |
VCOPY(ioVec, gpos); |
739 |
+ |
spinvector(ioVec, ioVec, zvec, rotangle); |
740 |
+ |
} else |
741 |
+ |
VCOPY(ioVec, gpos); |
742 |
+ |
return SDEnone; |
743 |
+ |
} |
744 |
+ |
|
745 |
+ |
/* Advance pointer to the next non-white character in the string (or nul) */ |
746 |
+ |
static int |
747 |
+ |
next_token(char **spp) |
748 |
+ |
{ |
749 |
+ |
while (isspace(**spp)) |
750 |
+ |
++*spp; |
751 |
+ |
return **spp; |
752 |
+ |
} |
753 |
+ |
|
754 |
+ |
/* Advance pointer past matching token (or any token if c==0) */ |
755 |
+ |
#define eat_token(spp,c) (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0) |
756 |
+ |
|
757 |
+ |
/* Count words from this point in string to '}' */ |
758 |
+ |
static int |
759 |
+ |
count_values(char *cp) |
760 |
+ |
{ |
761 |
+ |
int n = 0; |
762 |
+ |
|
763 |
+ |
while (next_token(&cp) != '}' && *cp) { |
764 |
+ |
while (!isspace(*cp) & (*cp != ',') & (*cp != '}')) |
765 |
+ |
if (!*++cp) |
766 |
+ |
break; |
767 |
+ |
++n; |
768 |
+ |
eat_token(&cp, ','); |
769 |
+ |
} |
770 |
+ |
return n; |
771 |
+ |
} |
772 |
+ |
|
773 |
+ |
/* Load an array of real numbers, returning total */ |
774 |
+ |
static int |
775 |
+ |
load_values(char **spp, float *va, int n) |
776 |
+ |
{ |
777 |
+ |
float *v = va; |
778 |
+ |
char *svnext; |
779 |
+ |
|
780 |
+ |
while (n-- > 0 && (svnext = fskip(*spp)) != NULL) { |
781 |
+ |
*v++ = atof(*spp); |
782 |
+ |
*spp = svnext; |
783 |
+ |
eat_token(spp, ','); |
784 |
+ |
} |
785 |
+ |
return v - va; |
786 |
+ |
} |
787 |
+ |
|
788 |
+ |
/* Load BSDF tree data */ |
789 |
+ |
static SDNode * |
790 |
+ |
load_tree_data(char **spp, int nd) |
791 |
+ |
{ |
792 |
+ |
SDNode *st; |
793 |
+ |
int n; |
794 |
+ |
|
795 |
+ |
if (!eat_token(spp, '{')) { |
796 |
+ |
strcpy(SDerrorDetail, "Missing '{' in tensor tree"); |
797 |
+ |
return NULL; |
798 |
+ |
} |
799 |
+ |
if (next_token(spp) == '{') { /* tree branches */ |
800 |
+ |
st = SDnewNode(nd, -1); |
801 |
+ |
if (st == NULL) |
802 |
+ |
return NULL; |
803 |
+ |
for (n = 0; n < 1<<nd; n++) |
804 |
+ |
if ((st->u.t[n] = load_tree_data(spp, nd)) == NULL) { |
805 |
+ |
SDfreeTre(st); |
806 |
+ |
return NULL; |
807 |
+ |
} |
808 |
+ |
} else { /* else load value grid */ |
809 |
+ |
int bsiz; |
810 |
+ |
n = count_values(*spp); /* see how big the grid is */ |
811 |
+ |
for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd) |
812 |
+ |
if (1<<bsiz == n) |
813 |
+ |
break; |
814 |
+ |
if (bsiz >= 8*sizeof(size_t)) { |
815 |
+ |
strcpy(SDerrorDetail, "Illegal value count in tensor tree"); |
816 |
+ |
return NULL; |
817 |
+ |
} |
818 |
+ |
st = SDnewNode(nd, bsiz/nd); |
819 |
+ |
if (st == NULL) |
820 |
+ |
return NULL; |
821 |
+ |
if (load_values(spp, st->u.v, n) != n) { |
822 |
+ |
strcpy(SDerrorDetail, "Real format error in tensor tree"); |
823 |
+ |
SDfreeTre(st); |
824 |
+ |
return NULL; |
825 |
+ |
} |
826 |
+ |
} |
827 |
+ |
if (!eat_token(spp, '}')) { |
828 |
+ |
strcpy(SDerrorDetail, "Missing '}' in tensor tree"); |
829 |
+ |
SDfreeTre(st); |
830 |
+ |
return NULL; |
831 |
+ |
} |
832 |
+ |
eat_token(spp, ','); |
833 |
+ |
return st; |
834 |
+ |
} |
835 |
+ |
|
836 |
+ |
/* Compute min. proj. solid angle and max. direct hemispherical scattering */ |
837 |
+ |
static SDError |
838 |
+ |
get_extrema(SDSpectralDF *df) |
839 |
+ |
{ |
840 |
+ |
SDNode *st = (*(SDTre *)df->comp[0].dist).st; |
841 |
+ |
double stepWidth, dhemi, bmin[4], bmax[4]; |
842 |
+ |
|
843 |
+ |
stepWidth = SDsmallestLeaf(st); |
844 |
+ |
df->minProjSA = M_PI*stepWidth*stepWidth; |
845 |
+ |
if (stepWidth < .03125) |
846 |
+ |
stepWidth = .03125; /* 1/32 resolution good enough */ |
847 |
+ |
df->maxHemi = .0; |
848 |
+ |
if (st->ndim == 3) { /* isotropic BSDF */ |
849 |
+ |
bmin[1] = bmin[2] = .0; |
850 |
+ |
bmax[1] = bmax[2] = 1.; |
851 |
+ |
for (bmin[0] = .0; bmin[0] < .5-FTINY; bmin[0] += stepWidth) { |
852 |
+ |
bmax[0] = bmin[0] + stepWidth; |
853 |
+ |
dhemi = SDavgTreBox(st, bmin, bmax); |
854 |
+ |
if (dhemi > df->maxHemi) |
855 |
+ |
df->maxHemi = dhemi; |
856 |
+ |
} |
857 |
+ |
} else if (st->ndim == 4) { /* anisotropic BSDF */ |
858 |
+ |
bmin[2] = bmin[3] = .0; |
859 |
+ |
bmax[2] = bmax[3] = 1.; |
860 |
+ |
for (bmin[0] = .0; bmin[0] < 1.-FTINY; bmin[0] += stepWidth) { |
861 |
+ |
bmax[0] = bmin[0] + stepWidth; |
862 |
+ |
for (bmin[1] = .0; bmin[1] < 1.-FTINY; bmin[1] += stepWidth) { |
863 |
+ |
bmax[1] = bmin[1] + stepWidth; |
864 |
+ |
dhemi = SDavgTreBox(st, bmin, bmax); |
865 |
+ |
if (dhemi > df->maxHemi) |
866 |
+ |
df->maxHemi = dhemi; |
867 |
+ |
} |
868 |
+ |
} |
869 |
+ |
} else |
870 |
+ |
return SDEinternal; |
871 |
+ |
/* correct hemispherical value */ |
872 |
+ |
df->maxHemi *= M_PI; |
873 |
+ |
return SDEnone; |
874 |
+ |
} |
875 |
+ |
|
876 |
+ |
/* Load BSDF distribution for this wavelength */ |
877 |
+ |
static SDError |
878 |
+ |
load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim) |
879 |
+ |
{ |
880 |
+ |
SDSpectralDF *df; |
881 |
+ |
SDTre *sdt; |
882 |
+ |
char *sdata; |
883 |
+ |
/* allocate BSDF component */ |
884 |
+ |
sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); |
885 |
+ |
if (!sdata) |
886 |
+ |
return SDEnone; |
887 |
+ |
/* |
888 |
+ |
* Remember that front and back are reversed from WINDOW 6 orientations |
889 |
+ |
*/ |
890 |
+ |
if (!strcasecmp(sdata, "Transmission")) { |
891 |
+ |
if (sd->tf != NULL) |
892 |
+ |
SDfreeSpectralDF(sd->tf); |
893 |
+ |
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
894 |
+ |
return SDEmemory; |
895 |
+ |
df = sd->tf; |
896 |
+ |
} else if (!strcasecmp(sdata, "Reflection Front")) { |
897 |
+ |
if (sd->rb != NULL) /* note back-front reversal */ |
898 |
+ |
SDfreeSpectralDF(sd->rb); |
899 |
+ |
if ((sd->rb = SDnewSpectralDF(1)) == NULL) |
900 |
+ |
return SDEmemory; |
901 |
+ |
df = sd->rb; |
902 |
+ |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
903 |
+ |
if (sd->rf != NULL) /* note front-back reversal */ |
904 |
+ |
SDfreeSpectralDF(sd->rf); |
905 |
+ |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
906 |
+ |
return SDEmemory; |
907 |
+ |
df = sd->rf; |
908 |
+ |
} else |
909 |
+ |
return SDEnone; |
910 |
+ |
/* XXX should also check "ScatteringDataType" for consistency? */ |
911 |
+ |
/* get angle bases */ |
912 |
+ |
sdata = ezxml_txt(ezxml_child(wdb,"AngleBasis")); |
913 |
+ |
if (!sdata || strcasecmp(sdata, "LBNL/Shirley-Chiu")) { |
914 |
+ |
sprintf(SDerrorDetail, "%s angle basis for BSDF '%s'", |
915 |
+ |
!sdata ? "Missing" : "Unsupported", sd->name); |
916 |
+ |
return !sdata ? SDEformat : SDEsupport; |
917 |
+ |
} |
918 |
+ |
/* allocate BSDF tree */ |
919 |
+ |
sdt = (SDTre *)malloc(sizeof(SDTre)); |
920 |
+ |
if (sdt == NULL) |
921 |
+ |
return SDEmemory; |
922 |
+ |
if (df == sd->rf) |
923 |
+ |
sdt->sidef = SD_UFRONT; |
924 |
+ |
else if (df == sd->rb) |
925 |
+ |
sdt->sidef = SD_UBACK; |
926 |
+ |
else |
927 |
+ |
sdt->sidef = SD_XMIT; |
928 |
+ |
sdt->st = NULL; |
929 |
+ |
df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ |
930 |
+ |
df->comp[0].dist = sdt; |
931 |
+ |
df->comp[0].func = &SDhandleTre; |
932 |
+ |
/* read BSDF data */ |
933 |
+ |
sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData")); |
934 |
+ |
if (!sdata || !next_token(&sdata)) { |
935 |
+ |
sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'", |
936 |
+ |
sd->name); |
937 |
+ |
return SDEformat; |
938 |
+ |
} |
939 |
+ |
sdt->st = load_tree_data(&sdata, ndim); |
940 |
+ |
if (sdt->st == NULL) |
941 |
+ |
return SDEformat; |
942 |
+ |
if (next_token(&sdata)) { /* check for unconsumed characters */ |
943 |
+ |
sprintf(SDerrorDetail, |
944 |
+ |
"Extra characters at end of ScatteringData in '%s'", |
945 |
+ |
sd->name); |
946 |
+ |
return SDEformat; |
947 |
+ |
} |
948 |
+ |
/* flatten branches where possible */ |
949 |
+ |
sdt->st = SDsimplifyTre(sdt->st); |
950 |
+ |
if (sdt->st == NULL) |
951 |
+ |
return SDEinternal; |
952 |
+ |
return get_extrema(df); /* compute global quantities */ |
953 |
+ |
} |
954 |
+ |
|
955 |
+ |
/* Find minimum value in tree */ |
956 |
+ |
static float |
957 |
+ |
SDgetTreMin(const SDNode *st) |
958 |
+ |
{ |
959 |
+ |
float vmin = FHUGE; |
960 |
+ |
int n; |
961 |
+ |
|
962 |
+ |
if (st->log2GR < 0) { |
963 |
+ |
for (n = 1<<st->ndim; n--; ) { |
964 |
+ |
float v = SDgetTreMin(st->u.t[n]); |
965 |
+ |
if (v < vmin) |
966 |
+ |
vmin = v; |
967 |
+ |
} |
968 |
+ |
} else { |
969 |
+ |
for (n = 1<<(st->ndim*st->log2GR); n--; ) |
970 |
+ |
if (st->u.v[n] < vmin) |
971 |
+ |
vmin = st->u.v[n]; |
972 |
+ |
} |
973 |
+ |
return vmin; |
974 |
+ |
} |
975 |
+ |
|
976 |
+ |
/* Subtract the given value from all tree nodes */ |
977 |
+ |
static void |
978 |
+ |
SDsubtractTreVal(SDNode *st, float val) |
979 |
+ |
{ |
980 |
+ |
int n; |
981 |
+ |
|
982 |
+ |
if (st->log2GR < 0) { |
983 |
+ |
for (n = 1<<st->ndim; n--; ) |
984 |
+ |
SDsubtractTreVal(st->u.t[n], val); |
985 |
+ |
} else { |
986 |
+ |
for (n = 1<<(st->ndim*st->log2GR); n--; ) |
987 |
+ |
if ((st->u.v[n] -= val) < 0) |
988 |
+ |
st->u.v[n] = .0f; |
989 |
+ |
} |
990 |
+ |
} |
991 |
+ |
|
992 |
+ |
/* Subtract minimum value from BSDF */ |
993 |
+ |
static double |
994 |
+ |
subtract_min(SDNode *st) |
995 |
+ |
{ |
996 |
+ |
float vmin; |
997 |
+ |
/* be sure to skip unused portion */ |
998 |
+ |
if (st->ndim == 3) { |
999 |
+ |
int n; |
1000 |
+ |
vmin = 1./M_PI; |
1001 |
+ |
if (st->log2GR < 0) { |
1002 |
+ |
for (n = 0; n < 8; n += 2) { |
1003 |
+ |
float v = SDgetTreMin(st->u.t[n]); |
1004 |
+ |
if (v < vmin) |
1005 |
+ |
vmin = v; |
1006 |
+ |
} |
1007 |
+ |
} else if (st->log2GR) { |
1008 |
+ |
for (n = 1 << (3*st->log2GR - 1); n--; ) |
1009 |
+ |
if (st->u.v[n] < vmin) |
1010 |
+ |
vmin = st->u.v[n]; |
1011 |
+ |
} else |
1012 |
+ |
vmin = st->u.v[0]; |
1013 |
+ |
} else /* anisotropic covers entire tree */ |
1014 |
+ |
vmin = SDgetTreMin(st); |
1015 |
+ |
|
1016 |
+ |
if (vmin <= FTINY) |
1017 |
+ |
return .0; |
1018 |
+ |
|
1019 |
+ |
SDsubtractTreVal(st, vmin); |
1020 |
+ |
|
1021 |
+ |
return M_PI * vmin; /* return hemispherical value */ |
1022 |
+ |
} |
1023 |
+ |
|
1024 |
+ |
/* Extract and separate diffuse portion of BSDF */ |
1025 |
+ |
static void |
1026 |
+ |
extract_diffuse(SDValue *dv, SDSpectralDF *df) |
1027 |
+ |
{ |
1028 |
+ |
int n; |
1029 |
+ |
|
1030 |
+ |
if (df == NULL || df->ncomp <= 0) { |
1031 |
+ |
dv->spec = c_dfcolor; |
1032 |
+ |
dv->cieY = .0; |
1033 |
+ |
return; |
1034 |
+ |
} |
1035 |
+ |
dv->spec = df->comp[0].cspec[0]; |
1036 |
+ |
dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st); |
1037 |
+ |
/* in case of multiple components */ |
1038 |
+ |
for (n = df->ncomp; --n; ) { |
1039 |
+ |
double ymin = subtract_min((*(SDTre *)df->comp[n].dist).st); |
1040 |
+ |
c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]); |
1041 |
+ |
dv->cieY += ymin; |
1042 |
+ |
} |
1043 |
+ |
df->maxHemi -= dv->cieY; /* adjust maximum hemispherical */ |
1044 |
+ |
/* make sure everything is set */ |
1045 |
+ |
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); |
1046 |
+ |
} |
1047 |
+ |
|
1048 |
|
/* Load a variable-resolution BSDF tree from an open XML file */ |
1049 |
|
SDError |
1050 |
|
SDloadTre(SDData *sd, ezxml_t wtl) |
1051 |
|
{ |
1052 |
< |
return SDEsupport; |
1052 |
> |
SDError ec; |
1053 |
> |
ezxml_t wld, wdb; |
1054 |
> |
int rank; |
1055 |
> |
char *txt; |
1056 |
> |
/* basic checks and tensor rank */ |
1057 |
> |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
1058 |
> |
"DataDefinition"), "IncidentDataStructure")); |
1059 |
> |
if (txt == NULL || !*txt) { |
1060 |
> |
sprintf(SDerrorDetail, |
1061 |
> |
"BSDF \"%s\": missing IncidentDataStructure", |
1062 |
> |
sd->name); |
1063 |
> |
return SDEformat; |
1064 |
> |
} |
1065 |
> |
if (!strcasecmp(txt, "TensorTree3")) |
1066 |
> |
rank = 3; |
1067 |
> |
else if (!strcasecmp(txt, "TensorTree4")) |
1068 |
> |
rank = 4; |
1069 |
> |
else { |
1070 |
> |
sprintf(SDerrorDetail, |
1071 |
> |
"BSDF \"%s\": unsupported IncidentDataStructure", |
1072 |
> |
sd->name); |
1073 |
> |
return SDEsupport; |
1074 |
> |
} |
1075 |
> |
/* load BSDF components */ |
1076 |
> |
for (wld = ezxml_child(wtl, "WavelengthData"); |
1077 |
> |
wld != NULL; wld = wld->next) { |
1078 |
> |
if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), |
1079 |
> |
"Visible")) |
1080 |
> |
continue; /* just visible for now */ |
1081 |
> |
for (wdb = ezxml_child(wld, "WavelengthDataBlock"); |
1082 |
> |
wdb != NULL; wdb = wdb->next) |
1083 |
> |
if ((ec = load_bsdf_data(sd, wdb, rank)) != SDEnone) |
1084 |
> |
return ec; |
1085 |
> |
} |
1086 |
> |
/* separate diffuse components */ |
1087 |
> |
extract_diffuse(&sd->rLambFront, sd->rf); |
1088 |
> |
extract_diffuse(&sd->rLambBack, sd->rb); |
1089 |
> |
extract_diffuse(&sd->tLamb, sd->tf); |
1090 |
> |
/* return success */ |
1091 |
> |
return SDEnone; |
1092 |
|
} |
1093 |
|
|
1094 |
|
/* Variable resolution BSDF methods */ |
1095 |
|
SDFunc SDhandleTre = { |
1096 |
|
&SDgetTreBSDF, |
1097 |
< |
NULL, |
1098 |
< |
NULL, |
1099 |
< |
NULL, |
1100 |
< |
&SDfreeTre, |
1097 |
> |
&SDqueryTreProjSA, |
1098 |
> |
&SDgetTreCDist, |
1099 |
> |
&SDsampTreCDist, |
1100 |
> |
&SDFreeBTre, |
1101 |
|
}; |