28 |
|
static const unsigned iwmax = (1<<(sizeof(unsigned)*4))-1; |
29 |
|
/* maximum cumulative value */ |
30 |
|
static const unsigned cumlmax = ~0; |
31 |
+ |
/* constant z-vector */ |
32 |
+ |
static const FVECT zvec = {.0, .0, 1.}; |
33 |
|
|
34 |
|
/* Struct used for our distribution-building callback */ |
35 |
|
typedef struct { |
63 |
|
if (lg < 0) { |
64 |
|
st = (SDNode *)malloc(sizeof(SDNode) + |
65 |
|
sizeof(st->u.t[0])*((1<<nd) - 1)); |
66 |
< |
if (st != NULL) |
65 |
< |
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd); |
66 |
< |
} else |
67 |
< |
st = (SDNode *)malloc(sizeof(SDNode) + |
68 |
< |
sizeof(st->u.v[0])*((1 << nd*lg) - 1)); |
69 |
< |
|
70 |
< |
if (st == NULL) { |
71 |
< |
if (lg < 0) |
66 |
> |
if (st == NULL) { |
67 |
|
sprintf(SDerrorDetail, |
68 |
|
"Cannot allocate %d branch BSDF tree", 1<<nd); |
69 |
< |
else |
69 |
> |
return NULL; |
70 |
> |
} |
71 |
> |
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd); |
72 |
> |
} else { |
73 |
> |
st = (SDNode *)malloc(sizeof(SDNode) + |
74 |
> |
sizeof(st->u.v[0])*((1 << nd*lg) - 1)); |
75 |
> |
if (st == NULL) { |
76 |
|
sprintf(SDerrorDetail, |
77 |
|
"Cannot allocate %d BSDF leaves", 1 << nd*lg); |
78 |
< |
return NULL; |
78 |
> |
return NULL; |
79 |
> |
} |
80 |
|
} |
81 |
|
st->ndim = nd; |
82 |
|
st->log2GR = lg; |
87 |
|
static void |
88 |
|
SDfreeTre(SDNode *st) |
89 |
|
{ |
90 |
< |
int i; |
90 |
> |
int n; |
91 |
|
|
92 |
|
if (st == NULL) |
93 |
|
return; |
94 |
< |
for (i = (st->log2GR < 0) << st->ndim; i--; ) |
95 |
< |
SDfreeTre(st->u.t[i]); |
96 |
< |
free((void *)st); |
94 |
> |
for (n = (st->log2GR < 0) << st->ndim; n--; ) |
95 |
> |
SDfreeTre(st->u.t[n]); |
96 |
> |
free(st); |
97 |
|
} |
98 |
|
|
99 |
|
/* Free a variable-resolution BSDF */ |
127 |
|
static float * |
128 |
|
grid_branch_start(SDNode *st, int n) |
129 |
|
{ |
130 |
< |
unsigned skipsiz = 1 << st->log2GR; |
130 |
> |
unsigned skipsiz = 1 << (st->log2GR - 1); |
131 |
|
float *vptr = st->u.v; |
132 |
|
int i; |
133 |
|
|
134 |
|
for (i = st->ndim; i--; skipsiz <<= st->log2GR) |
135 |
|
if (1<<i & n) |
136 |
< |
vptr += skipsiz >> 1; |
136 |
> |
vptr += skipsiz; |
137 |
|
return vptr; |
138 |
|
} |
139 |
|
|
189 |
|
static double |
190 |
|
SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax) |
191 |
|
{ |
192 |
< |
const unsigned skipsiz = 1 << nd*shft; |
192 |
> |
const unsigned skipsiz = 1 << --nd*shft; |
193 |
|
double sum = .0; |
194 |
|
int i; |
195 |
< |
|
195 |
> |
|
196 |
> |
va += *imin * skipsiz; |
197 |
> |
|
198 |
|
if (skipsiz == 1) |
199 |
|
for (i = *imin; i < *imax; i++) |
200 |
< |
sum += va[i]; |
200 |
> |
sum += *va++; |
201 |
|
else |
202 |
< |
for (i = *imin; i < *imax; i++) |
203 |
< |
sum += SDiterSum(va + i*skipsiz, |
200 |
< |
nd-1, shft, imin+1, imax+1); |
202 |
> |
for (i = *imin; i < *imax; i++, va += skipsiz) |
203 |
> |
sum += SDiterSum(va, nd, shft, imin+1, imax+1); |
204 |
|
return sum; |
205 |
|
} |
206 |
|
|
208 |
|
static double |
209 |
|
SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax) |
210 |
|
{ |
208 |
– |
int imin[SD_MAXDIM], imax[SD_MAXDIM]; |
211 |
|
unsigned n; |
212 |
|
int i; |
213 |
|
|
217 |
|
for (i = st->ndim; i--; ) { |
218 |
|
if (bmin[i] >= 1.) |
219 |
|
return .0; |
220 |
< |
if (bmax[i] <= .0) |
220 |
> |
if (bmax[i] <= 0) |
221 |
|
return .0; |
222 |
|
if (bmin[i] >= bmax[i]) |
223 |
|
return .0; |
225 |
|
if (st->log2GR < 0) { /* iterate on subtree */ |
226 |
|
double sum = .0, wsum = 1e-20; |
227 |
|
double sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w; |
226 |
– |
|
228 |
|
for (n = 1 << st->ndim; n--; ) { |
229 |
|
w = 1.; |
230 |
|
for (i = st->ndim; i--; ) { |
236 |
|
} |
237 |
|
if (sbmin[i] < .0) sbmin[i] = .0; |
238 |
|
if (sbmax[i] > 1.) sbmax[i] = 1.; |
239 |
+ |
if (sbmin[i] >= sbmax[i]) { |
240 |
+ |
w = .0; |
241 |
+ |
break; |
242 |
+ |
} |
243 |
|
w *= sbmax[i] - sbmin[i]; |
244 |
|
} |
245 |
|
if (w > 1e-10) { |
248 |
|
} |
249 |
|
} |
250 |
|
return sum / wsum; |
251 |
+ |
} else { /* iterate over leaves */ |
252 |
+ |
int imin[SD_MAXDIM], imax[SD_MAXDIM]; |
253 |
+ |
|
254 |
+ |
n = 1; |
255 |
+ |
for (i = st->ndim; i--; ) { |
256 |
+ |
imin[i] = (bmin[i] <= 0) ? 0 : |
257 |
+ |
(int)((1 << st->log2GR)*bmin[i]); |
258 |
+ |
imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) : |
259 |
+ |
(int)((1 << st->log2GR)*bmax[i] + .999999); |
260 |
+ |
n *= imax[i] - imin[i]; |
261 |
+ |
} |
262 |
+ |
if (n) |
263 |
+ |
return SDiterSum(st->u.v, st->ndim, |
264 |
+ |
st->log2GR, imin, imax) / (double)n; |
265 |
|
} |
266 |
< |
n = 1; /* iterate over leaves */ |
248 |
< |
for (i = st->ndim; i--; ) { |
249 |
< |
imin[i] = (bmin[i] <= 0) ? 0 |
250 |
< |
: (int)((1 << st->log2GR)*bmin[i]); |
251 |
< |
imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) |
252 |
< |
: (int)((1 << st->log2GR)*bmax[i] + .999999); |
253 |
< |
n *= imax[i] - imin[i]; |
254 |
< |
} |
255 |
< |
if (!n) |
256 |
< |
return .0; |
257 |
< |
|
258 |
< |
return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n; |
266 |
> |
return .0; |
267 |
|
} |
268 |
|
|
269 |
|
/* Recursive call for SDtraverseTre() */ |
278 |
|
/* in branches? */ |
279 |
|
if (st->log2GR < 0) { |
280 |
|
unsigned skipmask = 0; |
273 |
– |
|
281 |
|
csiz *= .5; |
282 |
|
for (i = st->ndim; i--; ) |
283 |
|
if (1<<i & cmask) |
284 |
|
if (pos[i] < cmin[i] + csiz) |
285 |
< |
for (n = 1 << st->ndim; n--; ) |
285 |
> |
for (n = 1 << st->ndim; n--; ) { |
286 |
|
if (n & 1<<i) |
287 |
|
skipmask |= 1<<n; |
288 |
+ |
} |
289 |
|
else |
290 |
< |
for (n = 1 << st->ndim; n--; ) |
290 |
> |
for (n = 1 << st->ndim; n--; ) { |
291 |
|
if (!(n & 1<<i)) |
292 |
|
skipmask |= 1<<n; |
293 |
+ |
} |
294 |
|
for (n = 1 << st->ndim; n--; ) { |
295 |
|
if (1<<n & skipmask) |
296 |
|
continue; |
299 |
|
bmin[i] = cmin[i] + csiz; |
300 |
|
else |
301 |
|
bmin[i] = cmin[i]; |
302 |
+ |
for (i = SD_MAXDIM; i-- > st->ndim; ) |
303 |
+ |
bmin[i] = .0; |
304 |
|
|
305 |
|
rval += rv = SDdotravTre(st->u.t[n], pos, cmask, |
306 |
|
cf, cptr, bmin, csiz); |
422 |
|
static float |
423 |
|
SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc) |
424 |
|
{ |
414 |
– |
static const FVECT zvec = {.0, .0, 1.}; |
425 |
|
FVECT rOutVec; |
426 |
|
double gridPos[4]; |
427 |
|
|
443 |
|
} |
444 |
|
/* convert vector coordinates */ |
445 |
|
if (sdt->st->ndim == 3) { |
446 |
< |
spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0])); |
446 |
> |
spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0])); |
447 |
|
gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
448 |
|
SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]); |
449 |
|
} else if (sdt->st->ndim == 4) { |
484 |
|
sp->wmax = wid; |
485 |
|
if (sp->alen >= sp->nall) { /* need more space? */ |
486 |
|
struct outdir_s *ndarr; |
487 |
< |
sp->nall += 8192; |
487 |
> |
sp->nall += 1024; |
488 |
|
ndarr = (struct outdir_s *)realloc(sp->darr, |
489 |
|
sizeof(struct outdir_s)*sp->nall); |
490 |
< |
if (ndarr == NULL) |
490 |
> |
if (ndarr == NULL) { |
491 |
> |
sprintf(SDerrorDetail, |
492 |
> |
"Cannot grow scaffold to %u entries", sp->nall); |
493 |
|
return -1; /* abort build */ |
494 |
+ |
} |
495 |
|
sp->darr = ndarr; |
496 |
|
} |
497 |
|
/* find Hilbert entry index */ |
498 |
|
bmin[0] = cmin[0]*(double)iwmax + .5; |
499 |
|
bmin[1] = cmin[1]*(double)iwmax + .5; |
500 |
< |
bmax[0] = bmin[0] + wid; |
501 |
< |
bmax[1] = bmin[1] + wid; |
500 |
> |
bmax[0] = bmin[0] + wid-1; |
501 |
> |
bmax[1] = bmin[1] + wid-1; |
502 |
|
hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax); |
503 |
|
sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin); |
504 |
|
sp->darr[sp->alen].wid = wid; |
511 |
|
static int |
512 |
|
sscmp(const void *p1, const void *p2) |
513 |
|
{ |
514 |
< |
return (int)((*(const struct outdir_s *)p1).hent - |
515 |
< |
(*(const struct outdir_s *)p2).hent); |
514 |
> |
unsigned h1 = (*(const struct outdir_s *)p1).hent; |
515 |
> |
unsigned h2 = (*(const struct outdir_s *)p2).hent; |
516 |
> |
|
517 |
> |
if (h1 > h2) |
518 |
> |
return 1; |
519 |
> |
if (h1 < h2) |
520 |
> |
return -1; |
521 |
> |
return 0; |
522 |
|
} |
523 |
|
|
524 |
|
/* Create a new cumulative distribution for the given input direction */ |
535 |
|
myScaffold.wmax = 0; |
536 |
|
myScaffold.nic = sdt->st->ndim - 2; |
537 |
|
myScaffold.alen = 0; |
538 |
< |
myScaffold.nall = 8192; |
538 |
> |
myScaffold.nall = 512; |
539 |
|
myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) * |
540 |
|
myScaffold.nall); |
541 |
|
if (myScaffold.darr == NULL) |
550 |
|
cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) + |
551 |
|
sizeof(cd->carr[0])*myScaffold.alen); |
552 |
|
if (cd == NULL) { |
553 |
+ |
sprintf(SDerrorDetail, |
554 |
+ |
"Cannot allocate %u entry cumulative distribution", |
555 |
+ |
myScaffold.alen); |
556 |
|
free(myScaffold.darr); |
557 |
|
return NULL; |
558 |
|
} |
559 |
+ |
cd->isodist = (myScaffold.nic == 1); |
560 |
|
/* sort the distribution */ |
561 |
|
qsort(myScaffold.darr, cd->calen = myScaffold.alen, |
562 |
|
sizeof(struct outdir_s), &sscmp); |
567 |
|
cd->clim[i][0] = floor(pos[i]/scale) * scale; |
568 |
|
cd->clim[i][1] = cd->clim[i][0] + scale; |
569 |
|
} |
570 |
+ |
if (cd->isodist) { /* avoid issue in SDqueryTreProjSA() */ |
571 |
+ |
cd->clim[1][0] = cd->clim[0][0]; |
572 |
+ |
cd->clim[1][1] = cd->clim[0][1]; |
573 |
+ |
} |
574 |
|
cd->max_psa = myScaffold.wmax / (double)iwmax; |
575 |
|
cd->max_psa *= cd->max_psa * M_PI; |
576 |
|
cd->sidef = sdt->sidef; |
690 |
|
const SDTreCDst *cd = (const SDTreCDst *)cdp; |
691 |
|
const unsigned target = randX*cumlmax; |
692 |
|
bitmask_t hndx, hcoord[2]; |
693 |
< |
double gpos[3]; |
693 |
> |
double gpos[3], rotangle; |
694 |
|
int i, iupper, ilower; |
695 |
|
/* check arguments */ |
696 |
|
if ((ioVec == NULL) | (cd == NULL)) |
726 |
|
/* emit from back? */ |
727 |
|
if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT) |
728 |
|
gpos[2] = -gpos[2]; |
729 |
< |
VCOPY(ioVec, gpos); |
729 |
> |
if (cd->isodist) { /* rotate isotropic result */ |
730 |
> |
rotangle = atan2(-ioVec[1],-ioVec[0]); |
731 |
> |
VCOPY(ioVec, gpos); |
732 |
> |
spinvector(ioVec, ioVec, zvec, rotangle); |
733 |
> |
} else |
734 |
> |
VCOPY(ioVec, gpos); |
735 |
|
return SDEnone; |
736 |
|
} |
737 |
|
|
744 |
|
return **spp; |
745 |
|
} |
746 |
|
|
747 |
< |
#define eat_token(spp,c) (next_token(spp)==(c) ? *(*(spp))++ : 0) |
747 |
> |
/* Advance pointer past matching token (or any token if c==0) */ |
748 |
> |
#define eat_token(spp,c) (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0) |
749 |
|
|
750 |
|
/* Count words from this point in string to '}' */ |
751 |
|
static int |
754 |
|
int n = 0; |
755 |
|
|
756 |
|
while (next_token(&cp) != '}' && *cp) { |
757 |
< |
if (*cp == '{') |
758 |
< |
return -1; |
759 |
< |
while (*cp && (*cp != ',') & (*cp != '}') & !isspace(*cp)) |
727 |
< |
++cp; |
757 |
> |
while (!isspace(*cp) & (*cp != ',') & (*cp != '}')) |
758 |
> |
if (!*++cp) |
759 |
> |
break; |
760 |
|
++n; |
761 |
|
eat_token(&cp, ','); |
762 |
|
} |
801 |
|
} else { /* else load value grid */ |
802 |
|
int bsiz; |
803 |
|
n = count_values(*spp); /* see how big the grid is */ |
804 |
< |
if (n <= 0) { |
773 |
< |
strcpy(SDerrorDetail, "Bad tensor tree data"); |
774 |
< |
return NULL; |
775 |
< |
} |
776 |
< |
for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd) |
804 |
> |
for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd) |
805 |
|
if (1<<bsiz == n) |
806 |
|
break; |
807 |
|
if (bsiz >= 8*sizeof(size_t)) { |
950 |
|
static float |
951 |
|
SDgetTreMin(const SDNode *st) |
952 |
|
{ |
953 |
< |
float vmin = 1./M_PI; |
953 |
> |
float vmin = FHUGE; |
954 |
|
int n; |
955 |
|
|
956 |
|
if (st->log2GR < 0) { |
978 |
|
SDsubtractTreVal(st->u.t[n], val); |
979 |
|
} else { |
980 |
|
for (n = 1<<(st->ndim*st->log2GR); n--; ) |
981 |
< |
st->u.v[n] -= val; |
981 |
> |
if ((st->u.v[n] -= val) < 0) |
982 |
> |
st->u.v[n] = .0f; |
983 |
|
} |
984 |
|
} |
985 |
|
|
989 |
|
{ |
990 |
|
float vmin; |
991 |
|
/* be sure to skip unused portion */ |
992 |
< |
if ((st->ndim == 3) & (st->log2GR < 0)) { |
993 |
< |
float v; |
965 |
< |
int i; |
992 |
> |
if (st->ndim == 3) { |
993 |
> |
int n; |
994 |
|
vmin = 1./M_PI; |
995 |
< |
for (i = 0; i < 4; i++) { |
996 |
< |
v = SDgetTreMin(st->u.t[i]); |
997 |
< |
if (v < vmin) |
998 |
< |
vmin = v; |
999 |
< |
} |
995 |
> |
if (st->log2GR < 0) { |
996 |
> |
for (n = 0; n < 8; n += 2) { |
997 |
> |
float v = SDgetTreMin(st->u.t[n]); |
998 |
> |
if (v < vmin) |
999 |
> |
vmin = v; |
1000 |
> |
} |
1001 |
> |
} else if (st->log2GR) { |
1002 |
> |
for (n = 1 << (3*st->log2GR - 1); n--; ) |
1003 |
> |
if (st->u.v[n] < vmin) |
1004 |
> |
vmin = st->u.v[n]; |
1005 |
> |
} else |
1006 |
> |
vmin = st->u.v[0]; |
1007 |
|
} else /* anisotropic covers entire tree */ |
1008 |
|
vmin = SDgetTreMin(st); |
1009 |
|
|