569 |
|
return 0; /* should be internal error */ |
570 |
|
/* get BSDF value */ |
571 |
|
yval = SDlookupTre(sdt->stc[tt_Y], gridPos, hc); |
572 |
+ |
if (coef == NULL) /* just getting hypercube? */ |
573 |
+ |
return 1; |
574 |
|
if (sdt->stc[tt_u] == NULL || sdt->stc[tt_v] == NULL) { |
575 |
< |
if (coef != NULL) *coef = yval; |
575 |
> |
*coef = yval; |
576 |
|
return 1; /* no color */ |
577 |
|
} |
576 |
– |
if (coef == NULL) /* just getting hypercube? */ |
577 |
– |
return 1; |
578 |
|
/* else decode color */ |
579 |
|
SDyuv2rgb(yval, SDlookupTre(sdt->stc[tt_u], gridPos, NULL), |
580 |
|
SDlookupTre(sdt->stc[tt_v], gridPos, NULL), coef); |
744 |
|
const SDCDst * |
745 |
|
SDgetTreCDist(const FVECT inVec, SDComponent *sdc) |
746 |
|
{ |
747 |
+ |
unsigned long cacheLeft = SDmaxCache; |
748 |
|
const SDTre *sdt; |
749 |
|
double inCoord[2]; |
750 |
|
int i; |
751 |
|
int mode; |
752 |
< |
SDTreCDst *cd, *cdlast; |
752 |
> |
SDTreCDst *cd, *cdlast, *cdlimit; |
753 |
|
/* check arguments */ |
754 |
|
if ((inVec == NULL) | (sdc == NULL) || |
755 |
|
(sdt = (SDTre *)sdc->dist) == NULL) |
789 |
|
/* quantize to avoid f.p. errors */ |
790 |
|
for (i = sdt->stc[tt_Y]->ndim - 2; i--; ) |
791 |
|
inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum; |
792 |
< |
cdlast = NULL; /* check for direction in cache list */ |
792 |
> |
cdlast = cdlimit = NULL; /* check for direction in cache list */ |
793 |
> |
/* PLACE MUTEX LOCK HERE FOR THREAD-SAFE */ |
794 |
|
for (cd = (SDTreCDst *)sdc->cdList; cd != NULL; |
795 |
|
cdlast = cd, cd = cd->next) { |
796 |
+ |
if (cacheLeft) { /* check cache size limit */ |
797 |
+ |
long csiz = sizeof(SDTreCDst) + |
798 |
+ |
sizeof(cd->carr[0])*cd->calen; |
799 |
+ |
if (cacheLeft > csiz) |
800 |
+ |
cacheLeft -= csiz; |
801 |
+ |
else { |
802 |
+ |
cdlimit = cdlast; |
803 |
+ |
cacheLeft = 0; |
804 |
+ |
} |
805 |
+ |
} |
806 |
|
if (cd->sidef != mode) |
807 |
|
continue; |
808 |
|
for (i = sdt->stc[tt_Y]->ndim - 2; i--; ) |
812 |
|
if (i < 0) |
813 |
|
break; /* means we have a match */ |
814 |
|
} |
815 |
< |
if (cd == NULL) /* need to create new entry? */ |
815 |
> |
if (cd == NULL) { /* need to create new entry? */ |
816 |
> |
if (cdlimit != NULL) /* exceeded cache size limit? */ |
817 |
> |
while ((cd = cdlimit->next) != NULL) { |
818 |
> |
cdlimit->next = cd->next; |
819 |
> |
free(cd); |
820 |
> |
} |
821 |
|
cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef); |
822 |
+ |
} |
823 |
|
if (cdlast != NULL) { /* move entry to head of cache list */ |
824 |
|
cdlast->next = cd->next; |
825 |
|
cd->next = (SDTreCDst *)sdc->cdList; |
826 |
|
sdc->cdList = (SDCDst *)cd; |
827 |
|
} |
828 |
+ |
/* END MUTEX LOCK */ |
829 |
|
return (SDCDst *)cd; /* ready to go */ |
830 |
|
} |
831 |
|
|
943 |
|
} |
944 |
|
|
945 |
|
/* Advance pointer past matching token (or any token if c==0) */ |
946 |
< |
#define eat_token(spp,c) (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0) |
946 |
> |
#define eat_token(spp,c) ((next_token(spp)==(c)) ^ !(c) ? *(*(spp))++ : 0) |
947 |
|
|
948 |
|
/* Count words from this point in string to '}' */ |
949 |
|
static int |
1193 |
|
static double |
1194 |
|
subtract_min_Y(SDNode *st) |
1195 |
|
{ |
1196 |
< |
float vmin; |
1196 |
> |
const float vmaxmin = 1.5/M_PI; |
1197 |
> |
float vmin; |
1198 |
|
/* be sure to skip unused portion */ |
1199 |
|
if (st->ndim == 3) { |
1200 |
|
int n; |
1201 |
< |
vmin = 1./M_PI; |
1201 |
> |
vmin = vmaxmin; |
1202 |
|
if (st->log2GR < 0) { |
1203 |
|
for (n = 0; n < 8; n += 2) { |
1204 |
|
float v = SDgetTreMin(st->u.t[n]); |
1214 |
|
} else /* anisotropic covers entire tree */ |
1215 |
|
vmin = SDgetTreMin(st); |
1216 |
|
|
1217 |
< |
if (vmin <= FTINY) |
1218 |
< |
return .0; |
1217 |
> |
if ((vmin >= vmaxmin) | (vmin <= .01/M_PI)) |
1218 |
> |
return .0; /* not worth bothering about */ |
1219 |
|
|
1220 |
|
SDsubtractTreVal(st, vmin); |
1221 |
|
|
1334 |
|
my_min.new_stu = my_min.new_stv = NULL; |
1335 |
|
/* get minimum RGB value */ |
1336 |
|
SDtraverseTre(stc[tt_Y], NULL, 0, get_min_RGB, &my_min); |
1337 |
< |
ymin = tt_RGB_coef[0]*my_min.rgb[0] + |
1338 |
< |
tt_RGB_coef[1]*my_min.rgb[1] + |
1339 |
< |
tt_RGB_coef[2]*my_min.rgb[2]; |
1340 |
< |
if (ymin <= 1e-5) { |
1341 |
< |
*cs = c_dfcolor; |
1322 |
< |
return .0; /* not worth bothering about */ |
1323 |
< |
} |
1324 |
< |
/* adjust u' & v' values */ |
1337 |
> |
/* convert to C_COLOR */ |
1338 |
> |
ymin = c_fromSharpRGB(my_min.rgb, cs); |
1339 |
> |
if ((ymin >= .5*FHUGE) | (ymin <= .01/M_PI)) |
1340 |
> |
return .0; /* close to zero or no tree */ |
1341 |
> |
/* adjust u' & v' trees */ |
1342 |
|
SDtraverseTre(stc[tt_u], NULL, 0, adjust_utree, &my_min); |
1343 |
|
SDtraverseTre(stc[tt_v], NULL, 0, adjust_vtree, &my_min); |
1344 |
|
SDfreeTre(stc[tt_u]); SDfreeTre(stc[tt_v]); |
1345 |
|
stc[tt_u] = SDsimplifyTre(my_min.new_stu); |
1346 |
|
stc[tt_v] = SDsimplifyTre(my_min.new_stv); |
1347 |
< |
/* finally, subtract Y value */ |
1347 |
> |
/* subtract Y & return hemispherical */ |
1348 |
|
SDsubtractTreVal(stc[tt_Y], ymin); |
1349 |
< |
/* return color and Y */ |
1350 |
< |
c_fromSharpRGB(my_min.rgb, cs); |
1334 |
< |
return M_PI*ymin; |
1349 |
> |
|
1350 |
> |
return M_PI * ymin; |
1351 |
|
} |
1352 |
|
|
1353 |
|
/* Extract and separate diffuse portion of BSDF */ |
1374 |
|
memcpy(df->comp[0].cspec, tt_RGB_prim, sizeof(tt_RGB_prim)); |
1375 |
|
dv->cieY = subtract_min_RGB(&dv->spec, sdt->stc); |
1376 |
|
} else { |
1377 |
< |
df->comp[0].cspec[0] = c_dfcolor; |
1377 |
> |
df->comp[0].cspec[0] = dv->spec = c_dfcolor; |
1378 |
|
dv->cieY = subtract_min_Y(sdt->stc[tt_Y]); |
1379 |
|
} |
1380 |
|
df->maxHemi -= dv->cieY; /* adjust maximum hemispherical */ |
1381 |
< |
/* make sure everything is set */ |
1382 |
< |
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); |
1381 |
> |
|
1382 |
> |
c_ccvt(&dv->spec, C_CSXY); /* make sure (x,y) is set */ |
1383 |
|
} |
1384 |
|
|
1385 |
|
/* Load a variable-resolution BSDF tree from an open XML file */ |
1430 |
|
/* separate diffuse components */ |
1431 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
1432 |
|
extract_diffuse(&sd->rLambBack, sd->rb); |
1433 |
< |
if (sd->tf != NULL) |
1434 |
< |
extract_diffuse(&sd->tLamb, sd->tf); |
1419 |
< |
if (sd->tb != NULL) |
1420 |
< |
extract_diffuse(&sd->tLamb, sd->tb); |
1433 |
> |
extract_diffuse(&sd->tLambFront, sd->tf); |
1434 |
> |
extract_diffuse(&sd->tLambBack, sd->tb); |
1435 |
|
/* return success */ |
1436 |
|
return SDEnone; |
1437 |
|
} |
1438 |
|
|
1439 |
|
/* Variable resolution BSDF methods */ |
1440 |
< |
SDFunc SDhandleTre = { |
1440 |
> |
const SDFunc SDhandleTre = { |
1441 |
|
&SDgetTreBSDF, |
1442 |
|
&SDqueryTreProjSA, |
1443 |
|
&SDgetTreCDist, |