61 |
|
if (lg < 0) { |
62 |
|
st = (SDNode *)malloc(sizeof(SDNode) + |
63 |
|
sizeof(st->u.t[0])*((1<<nd) - 1)); |
64 |
< |
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) |
64 |
> |
if (st == NULL) { |
65 |
|
sprintf(SDerrorDetail, |
66 |
|
"Cannot allocate %d branch BSDF tree", 1<<nd); |
67 |
< |
else |
67 |
> |
return NULL; |
68 |
> |
} |
69 |
> |
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd); |
70 |
> |
} else { |
71 |
> |
st = (SDNode *)malloc(sizeof(SDNode) + |
72 |
> |
sizeof(st->u.v[0])*((1 << nd*lg) - 1)); |
73 |
> |
if (st == NULL) { |
74 |
|
sprintf(SDerrorDetail, |
75 |
|
"Cannot allocate %d BSDF leaves", 1 << nd*lg); |
76 |
< |
return NULL; |
76 |
> |
return NULL; |
77 |
> |
} |
78 |
|
} |
79 |
|
st->ndim = nd; |
80 |
|
st->log2GR = lg; |
85 |
|
static void |
86 |
|
SDfreeTre(SDNode *st) |
87 |
|
{ |
88 |
< |
int i; |
88 |
> |
int n; |
89 |
|
|
90 |
|
if (st == NULL) |
91 |
|
return; |
92 |
< |
for (i = (st->log2GR < 0) << st->ndim; i--; ) |
93 |
< |
SDfreeTre(st->u.t[i]); |
92 |
> |
for (n = (st->log2GR < 0) << st->ndim; n--; ) |
93 |
> |
SDfreeTre(st->u.t[n]); |
94 |
|
free((void *)st); |
95 |
|
} |
96 |
|
|
129 |
|
float *vptr = st->u.v; |
130 |
|
int i; |
131 |
|
|
132 |
< |
for (i = st->ndim; i--; skipsiz <<= st->log2GR) |
133 |
< |
if (1<<i & n) |
132 |
> |
for (i = 0; i < st->ndim; skipsiz <<= st->log2GR) |
133 |
> |
if (1<<i++ & n) |
134 |
|
vptr += skipsiz >> 1; |
135 |
|
return vptr; |
136 |
|
} |
187 |
|
static double |
188 |
|
SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax) |
189 |
|
{ |
190 |
< |
const unsigned skipsiz = 1 << nd*shft; |
190 |
> |
const unsigned skipsiz = 1 << --nd*shft; |
191 |
|
double sum = .0; |
192 |
|
int i; |
193 |
|
|
196 |
|
sum += va[i]; |
197 |
|
else |
198 |
|
for (i = *imin; i < *imax; i++) |
199 |
< |
sum += SDiterSum(va + i*skipsiz, |
200 |
< |
nd-1, shft, imin+1, imax+1); |
199 |
> |
sum += SDiterSum(va + i*skipsiz, nd, shft, imin+1, imax+1); |
200 |
|
return sum; |
201 |
|
} |
202 |
|
|
473 |
|
sp->wmax = wid; |
474 |
|
if (sp->alen >= sp->nall) { /* need more space? */ |
475 |
|
struct outdir_s *ndarr; |
476 |
< |
sp->nall += 8192; |
476 |
> |
sp->nall += 1024; |
477 |
|
ndarr = (struct outdir_s *)realloc(sp->darr, |
478 |
|
sizeof(struct outdir_s)*sp->nall); |
479 |
< |
if (ndarr == NULL) |
479 |
> |
if (ndarr == NULL) { |
480 |
> |
sprintf(SDerrorDetail, |
481 |
> |
"Cannot grow scaffold to %u entries", sp->nall); |
482 |
|
return -1; /* abort build */ |
483 |
+ |
} |
484 |
|
sp->darr = ndarr; |
485 |
|
} |
486 |
|
/* find Hilbert entry index */ |
487 |
|
bmin[0] = cmin[0]*(double)iwmax + .5; |
488 |
|
bmin[1] = cmin[1]*(double)iwmax + .5; |
489 |
< |
bmax[0] = bmin[0] + wid; |
490 |
< |
bmax[1] = bmin[1] + wid; |
489 |
> |
bmax[0] = bmin[0] + wid-1; |
490 |
> |
bmax[1] = bmin[1] + wid-1; |
491 |
|
hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax); |
492 |
|
sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin); |
493 |
|
sp->darr[sp->alen].wid = wid; |
500 |
|
static int |
501 |
|
sscmp(const void *p1, const void *p2) |
502 |
|
{ |
503 |
< |
return (int)((*(const struct outdir_s *)p1).hent - |
504 |
< |
(*(const struct outdir_s *)p2).hent); |
503 |
> |
unsigned h1 = (*(const struct outdir_s *)p1).hent; |
504 |
> |
unsigned h2 = (*(const struct outdir_s *)p2).hent; |
505 |
> |
|
506 |
> |
if (h1 > h2) |
507 |
> |
return 1; |
508 |
> |
if (h1 < h2) |
509 |
> |
return -1; |
510 |
> |
return 0; |
511 |
|
} |
512 |
|
|
513 |
|
/* Create a new cumulative distribution for the given input direction */ |
524 |
|
myScaffold.wmax = 0; |
525 |
|
myScaffold.nic = sdt->st->ndim - 2; |
526 |
|
myScaffold.alen = 0; |
527 |
< |
myScaffold.nall = 8192; |
527 |
> |
myScaffold.nall = 512; |
528 |
|
myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) * |
529 |
|
myScaffold.nall); |
530 |
|
if (myScaffold.darr == NULL) |
539 |
|
cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) + |
540 |
|
sizeof(cd->carr[0])*myScaffold.alen); |
541 |
|
if (cd == NULL) { |
542 |
+ |
sprintf(SDerrorDetail, |
543 |
+ |
"Cannot allocate %u entry cumulative distribution", |
544 |
+ |
myScaffold.alen); |
545 |
|
free(myScaffold.darr); |
546 |
|
return NULL; |
547 |
|
} |
723 |
|
return **spp; |
724 |
|
} |
725 |
|
|
726 |
< |
#define eat_token(spp,c) (next_token(spp)==(c) ? *(*(spp))++ : 0) |
726 |
> |
/* Advance pointer past matching token (or any token if c==0) */ |
727 |
> |
#define eat_token(spp,c) (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0) |
728 |
|
|
729 |
|
/* Count words from this point in string to '}' */ |
730 |
|
static int |
733 |
|
int n = 0; |
734 |
|
|
735 |
|
while (next_token(&cp) != '}' && *cp) { |
736 |
< |
if (*cp == '{') |
737 |
< |
return -1; |
738 |
< |
while (*cp && (*cp != ',') & (*cp != '}') & !isspace(*cp)) |
727 |
< |
++cp; |
736 |
> |
while (!isspace(*cp) & (*cp != ',') & (*cp != '}')) |
737 |
> |
if (!*++cp) |
738 |
> |
break; |
739 |
|
++n; |
740 |
|
eat_token(&cp, ','); |
741 |
|
} |
780 |
|
} else { /* else load value grid */ |
781 |
|
int bsiz; |
782 |
|
n = count_values(*spp); /* see how big the grid is */ |
772 |
– |
if (n <= 0) { |
773 |
– |
strcpy(SDerrorDetail, "Bad tensor tree data"); |
774 |
– |
return NULL; |
775 |
– |
} |
783 |
|
for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd) |
784 |
|
if (1<<bsiz == n) |
785 |
|
break; |
929 |
|
static float |
930 |
|
SDgetTreMin(const SDNode *st) |
931 |
|
{ |
932 |
< |
float vmin = 1./M_PI; |
932 |
> |
float vmin = FHUGE; |
933 |
|
int n; |
934 |
|
|
935 |
|
if (st->log2GR < 0) { |
967 |
|
{ |
968 |
|
float vmin; |
969 |
|
/* be sure to skip unused portion */ |
970 |
< |
if ((st->ndim == 3) & (st->log2GR < 0)) { |
971 |
< |
float v; |
965 |
< |
int i; |
970 |
> |
if (st->ndim == 3) { |
971 |
> |
int n; |
972 |
|
vmin = 1./M_PI; |
973 |
< |
for (i = 0; i < 4; i++) { |
974 |
< |
v = SDgetTreMin(st->u.t[i]); |
975 |
< |
if (v < vmin) |
976 |
< |
vmin = v; |
977 |
< |
} |
973 |
> |
if (st->log2GR < 0) { |
974 |
> |
for (n = 0; n < 4; n++) { |
975 |
> |
float v = SDgetTreMin(st->u.t[n]); |
976 |
> |
if (v < vmin) |
977 |
> |
vmin = v; |
978 |
> |
} |
979 |
> |
} else if (st->log2GR) { |
980 |
> |
for (n = 1 << (3*st->log2GR - 1); n--; ) |
981 |
> |
if (st->u.v[n] < vmin) |
982 |
> |
vmin = st->u.v[n]; |
983 |
> |
} else |
984 |
> |
vmin = st->u.v[0]; |
985 |
|
} else /* anisotropic covers entire tree */ |
986 |
|
vmin = SDgetTreMin(st); |
987 |
|
|