| 1 |
greg |
3.2 |
#ifndef lint
|
| 2 |
greg |
3.41 |
static const char RCSid[] = "$Id: bsdf_t.c,v 3.40 2015/04/06 18:28:35 greg Exp $";
|
| 3 |
greg |
3.2 |
#endif
|
| 4 |
greg |
3.1 |
/*
|
| 5 |
|
|
* bsdf_t.c
|
| 6 |
|
|
*
|
| 7 |
|
|
* Definitions for variable-resolution BSDF trees
|
| 8 |
|
|
*
|
| 9 |
|
|
* Created by Greg Ward on 2/2/11.
|
| 10 |
|
|
*
|
| 11 |
|
|
*/
|
| 12 |
|
|
|
| 13 |
greg |
3.17 |
#define _USE_MATH_DEFINES
|
| 14 |
greg |
3.3 |
#include "rtio.h"
|
| 15 |
greg |
3.1 |
#include <stdlib.h>
|
| 16 |
greg |
3.3 |
#include <math.h>
|
| 17 |
|
|
#include <ctype.h>
|
| 18 |
greg |
3.1 |
#include "ezxml.h"
|
| 19 |
|
|
#include "bsdf.h"
|
| 20 |
|
|
#include "bsdf_t.h"
|
| 21 |
greg |
3.6 |
#include "hilbert.h"
|
| 22 |
|
|
|
| 23 |
|
|
/* Callback function type for SDtraverseTre() */
|
| 24 |
greg |
3.37 |
typedef int SDtreCallback(float val, const double *cmin, double csiz,
|
| 25 |
|
|
void *cptr);
|
| 26 |
greg |
3.6 |
/* reference width maximum (1.0) */
|
| 27 |
greg |
3.7 |
static const unsigned iwbits = sizeof(unsigned)*4;
|
| 28 |
greg |
3.23 |
static const unsigned iwmax = 1<<(sizeof(unsigned)*4);
|
| 29 |
greg |
3.7 |
/* maximum cumulative value */
|
| 30 |
|
|
static const unsigned cumlmax = ~0;
|
| 31 |
greg |
3.15 |
/* constant z-vector */
|
| 32 |
|
|
static const FVECT zvec = {.0, .0, 1.};
|
| 33 |
greg |
3.22 |
/* quantization value */
|
| 34 |
|
|
static double quantum = 1./256.;
|
| 35 |
greg |
3.37 |
/* our RGB primaries */
|
| 36 |
|
|
static C_COLOR tt_RGB_prim[3];
|
| 37 |
|
|
static float tt_RGB_coef[3];
|
| 38 |
|
|
|
| 39 |
|
|
static const double czero[SD_MAXDIM];
|
| 40 |
|
|
|
| 41 |
|
|
enum {tt_Y, tt_u, tt_v}; /* tree components (tt_Y==0) */
|
| 42 |
greg |
3.6 |
|
| 43 |
|
|
/* Struct used for our distribution-building callback */
|
| 44 |
|
|
typedef struct {
|
| 45 |
greg |
3.24 |
short nic; /* number of input coordinates */
|
| 46 |
|
|
short rev; /* reversing query */
|
| 47 |
greg |
3.7 |
unsigned alen; /* current array length */
|
| 48 |
|
|
unsigned nall; /* number of allocated entries */
|
| 49 |
|
|
unsigned wmin; /* minimum square size so far */
|
| 50 |
|
|
unsigned wmax; /* maximum square size */
|
| 51 |
greg |
3.6 |
struct outdir_s {
|
| 52 |
|
|
unsigned hent; /* entering Hilbert index */
|
| 53 |
|
|
int wid; /* this square size */
|
| 54 |
|
|
float bsdf; /* BSDF for this square */
|
| 55 |
|
|
} *darr; /* output direction array */
|
| 56 |
|
|
} SDdistScaffold;
|
| 57 |
greg |
3.1 |
|
| 58 |
|
|
/* Allocate a new scattering distribution node */
|
| 59 |
|
|
static SDNode *
|
| 60 |
|
|
SDnewNode(int nd, int lg)
|
| 61 |
|
|
{
|
| 62 |
|
|
SDNode *st;
|
| 63 |
|
|
|
| 64 |
|
|
if (nd <= 0) {
|
| 65 |
|
|
strcpy(SDerrorDetail, "Zero dimension BSDF node request");
|
| 66 |
|
|
return NULL;
|
| 67 |
|
|
}
|
| 68 |
|
|
if (nd > SD_MAXDIM) {
|
| 69 |
|
|
sprintf(SDerrorDetail, "Illegal BSDF dimension (%d > %d)",
|
| 70 |
|
|
nd, SD_MAXDIM);
|
| 71 |
|
|
return NULL;
|
| 72 |
|
|
}
|
| 73 |
|
|
if (lg < 0) {
|
| 74 |
|
|
st = (SDNode *)malloc(sizeof(SDNode) +
|
| 75 |
greg |
3.7 |
sizeof(st->u.t[0])*((1<<nd) - 1));
|
| 76 |
greg |
3.12 |
if (st == NULL) {
|
| 77 |
greg |
3.1 |
sprintf(SDerrorDetail,
|
| 78 |
greg |
3.6 |
"Cannot allocate %d branch BSDF tree", 1<<nd);
|
| 79 |
greg |
3.12 |
return NULL;
|
| 80 |
|
|
}
|
| 81 |
|
|
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
|
| 82 |
|
|
} else {
|
| 83 |
|
|
st = (SDNode *)malloc(sizeof(SDNode) +
|
| 84 |
|
|
sizeof(st->u.v[0])*((1 << nd*lg) - 1));
|
| 85 |
|
|
if (st == NULL) {
|
| 86 |
greg |
3.1 |
sprintf(SDerrorDetail,
|
| 87 |
|
|
"Cannot allocate %d BSDF leaves", 1 << nd*lg);
|
| 88 |
greg |
3.12 |
return NULL;
|
| 89 |
|
|
}
|
| 90 |
greg |
3.1 |
}
|
| 91 |
|
|
st->ndim = nd;
|
| 92 |
|
|
st->log2GR = lg;
|
| 93 |
|
|
return st;
|
| 94 |
|
|
}
|
| 95 |
|
|
|
| 96 |
|
|
/* Free an SD tree */
|
| 97 |
|
|
static void
|
| 98 |
greg |
3.6 |
SDfreeTre(SDNode *st)
|
| 99 |
greg |
3.1 |
{
|
| 100 |
greg |
3.12 |
int n;
|
| 101 |
greg |
3.1 |
|
| 102 |
|
|
if (st == NULL)
|
| 103 |
|
|
return;
|
| 104 |
greg |
3.12 |
for (n = (st->log2GR < 0) << st->ndim; n--; )
|
| 105 |
|
|
SDfreeTre(st->u.t[n]);
|
| 106 |
greg |
3.14 |
free(st);
|
| 107 |
greg |
3.1 |
}
|
| 108 |
|
|
|
| 109 |
greg |
3.6 |
/* Free a variable-resolution BSDF */
|
| 110 |
|
|
static void
|
| 111 |
|
|
SDFreeBTre(void *p)
|
| 112 |
|
|
{
|
| 113 |
|
|
SDTre *sdt = (SDTre *)p;
|
| 114 |
|
|
|
| 115 |
|
|
if (sdt == NULL)
|
| 116 |
|
|
return;
|
| 117 |
greg |
3.37 |
SDfreeTre(sdt->stc[tt_Y]);
|
| 118 |
|
|
SDfreeTre(sdt->stc[tt_u]);
|
| 119 |
|
|
SDfreeTre(sdt->stc[tt_v]);
|
| 120 |
greg |
3.6 |
free(sdt);
|
| 121 |
|
|
}
|
| 122 |
greg |
3.5 |
|
| 123 |
greg |
3.7 |
/* Fill branch's worth of grid values from subtree */
|
| 124 |
|
|
static void
|
| 125 |
|
|
fill_grid_branch(float *dptr, const float *sptr, int nd, int shft)
|
| 126 |
|
|
{
|
| 127 |
|
|
unsigned n = 1 << (shft-1);
|
| 128 |
|
|
|
| 129 |
|
|
if (!--nd) { /* end on the line */
|
| 130 |
|
|
memcpy(dptr, sptr, sizeof(*dptr)*n);
|
| 131 |
|
|
return;
|
| 132 |
|
|
}
|
| 133 |
|
|
while (n--) /* recurse on each slice */
|
| 134 |
|
|
fill_grid_branch(dptr + (n << shft*nd),
|
| 135 |
|
|
sptr + (n << (shft-1)*nd), nd, shft);
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
/* Get pointer at appropriate offset for the given branch */
|
| 139 |
|
|
static float *
|
| 140 |
|
|
grid_branch_start(SDNode *st, int n)
|
| 141 |
|
|
{
|
| 142 |
greg |
3.14 |
unsigned skipsiz = 1 << (st->log2GR - 1);
|
| 143 |
greg |
3.7 |
float *vptr = st->u.v;
|
| 144 |
|
|
int i;
|
| 145 |
|
|
|
| 146 |
greg |
3.14 |
for (i = st->ndim; i--; skipsiz <<= st->log2GR)
|
| 147 |
|
|
if (1<<i & n)
|
| 148 |
|
|
vptr += skipsiz;
|
| 149 |
greg |
3.7 |
return vptr;
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
/* Simplify (consolidate) a tree by flattening uniform depth regions */
|
| 153 |
|
|
static SDNode *
|
| 154 |
|
|
SDsimplifyTre(SDNode *st)
|
| 155 |
|
|
{
|
| 156 |
|
|
int match, n;
|
| 157 |
|
|
|
| 158 |
|
|
if (st == NULL) /* check for invalid tree */
|
| 159 |
|
|
return NULL;
|
| 160 |
|
|
if (st->log2GR >= 0) /* grid just returns unaltered */
|
| 161 |
|
|
return st;
|
| 162 |
|
|
match = 1; /* check if grids below match */
|
| 163 |
|
|
for (n = 0; n < 1<<st->ndim; n++) {
|
| 164 |
|
|
if ((st->u.t[n] = SDsimplifyTre(st->u.t[n])) == NULL)
|
| 165 |
|
|
return NULL; /* propogate error up call stack */
|
| 166 |
|
|
match &= (st->u.t[n]->log2GR == st->u.t[0]->log2GR);
|
| 167 |
|
|
}
|
| 168 |
greg |
3.9 |
if (match && (match = st->u.t[0]->log2GR) >= 0) {
|
| 169 |
|
|
SDNode *stn = SDnewNode(st->ndim, match + 1);
|
| 170 |
greg |
3.7 |
if (stn == NULL) /* out of memory? */
|
| 171 |
|
|
return st;
|
| 172 |
|
|
/* transfer values to new grid */
|
| 173 |
|
|
for (n = 1 << st->ndim; n--; )
|
| 174 |
|
|
fill_grid_branch(grid_branch_start(stn, n),
|
| 175 |
greg |
3.9 |
st->u.t[n]->u.v, stn->ndim, stn->log2GR);
|
| 176 |
greg |
3.7 |
SDfreeTre(st); /* free old tree */
|
| 177 |
|
|
st = stn; /* return new one */
|
| 178 |
|
|
}
|
| 179 |
|
|
return st;
|
| 180 |
|
|
}
|
| 181 |
|
|
|
| 182 |
greg |
3.37 |
/* Assign the given voxel in tree (produces no grid nodes) */
|
| 183 |
|
|
static SDNode *
|
| 184 |
|
|
SDsetVoxel(SDNode *sroot, int nd, const double *tmin, const double tsiz, float val)
|
| 185 |
|
|
{
|
| 186 |
|
|
double ctrk[SD_MAXDIM];
|
| 187 |
|
|
double csiz = 1.;
|
| 188 |
|
|
SDNode *st;
|
| 189 |
|
|
int i, n;
|
| 190 |
|
|
/* check arguments */
|
| 191 |
|
|
for (i = nd; i-- > 0; )
|
| 192 |
|
|
if ((tmin[i] < .0) | (tmin[i] >= 1.-FTINY))
|
| 193 |
|
|
break;
|
| 194 |
|
|
if ((i >= 0) | (nd <= 0) | (tsiz <= FTINY) | (tsiz > 1.+FTINY) |
|
| 195 |
|
|
(sroot != NULL && sroot->ndim != nd)) {
|
| 196 |
|
|
SDfreeTre(sroot);
|
| 197 |
|
|
return NULL;
|
| 198 |
|
|
}
|
| 199 |
|
|
if (tsiz >= 1.-FTINY) { /* special case when tree is a leaf */
|
| 200 |
|
|
SDfreeTre(sroot);
|
| 201 |
|
|
if ((sroot = SDnewNode(nd, 0)) != NULL)
|
| 202 |
|
|
sroot->u.v[0] = val;
|
| 203 |
|
|
return sroot;
|
| 204 |
|
|
}
|
| 205 |
|
|
/* make sure we have branching root */
|
| 206 |
|
|
if (sroot != NULL && sroot->log2GR >= 0) {
|
| 207 |
|
|
SDfreeTre(sroot); sroot = NULL;
|
| 208 |
|
|
}
|
| 209 |
|
|
if (sroot == NULL && (sroot = SDnewNode(nd, -1)) == NULL)
|
| 210 |
|
|
return NULL;
|
| 211 |
|
|
st = sroot; /* climb/grow tree */
|
| 212 |
|
|
memset(ctrk, 0, sizeof(ctrk));
|
| 213 |
|
|
for ( ; ; ) {
|
| 214 |
|
|
csiz *= .5; /* find appropriate branch */
|
| 215 |
|
|
n = 0;
|
| 216 |
|
|
for (i = nd; i--; )
|
| 217 |
|
|
if (ctrk[i]+csiz <= tmin[i]+FTINY) {
|
| 218 |
|
|
ctrk[i] += csiz;
|
| 219 |
|
|
n |= 1 << i;
|
| 220 |
|
|
}
|
| 221 |
|
|
/* reached desired voxel? */
|
| 222 |
|
|
if (csiz <= tsiz+FTINY) {
|
| 223 |
|
|
SDfreeTre(st->u.t[n]);
|
| 224 |
|
|
st = st->u.t[n] = SDnewNode(nd, 0);
|
| 225 |
|
|
break;
|
| 226 |
|
|
}
|
| 227 |
|
|
/* else grow tree as needed */
|
| 228 |
|
|
if (st->u.t[n] != NULL && st->u.t[n]->log2GR >= 0) {
|
| 229 |
|
|
SDfreeTre(st->u.t[n]); st->u.t[n] = NULL;
|
| 230 |
|
|
}
|
| 231 |
|
|
if (st->u.t[n] == NULL)
|
| 232 |
|
|
st->u.t[n] = SDnewNode(nd, -1);
|
| 233 |
|
|
if ((st = st->u.t[n]) == NULL)
|
| 234 |
|
|
break;
|
| 235 |
|
|
}
|
| 236 |
|
|
if (st == NULL) {
|
| 237 |
|
|
SDfreeTre(sroot);
|
| 238 |
|
|
return NULL;
|
| 239 |
|
|
}
|
| 240 |
|
|
st->u.v[0] = val; /* assign leaf and return root */
|
| 241 |
|
|
return sroot;
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
greg |
3.7 |
/* Find smallest leaf in tree */
|
| 245 |
|
|
static double
|
| 246 |
|
|
SDsmallestLeaf(const SDNode *st)
|
| 247 |
|
|
{
|
| 248 |
|
|
if (st->log2GR < 0) { /* tree branches */
|
| 249 |
|
|
double lmin = 1.;
|
| 250 |
|
|
int n;
|
| 251 |
|
|
for (n = 1<<st->ndim; n--; ) {
|
| 252 |
|
|
double lsiz = SDsmallestLeaf(st->u.t[n]);
|
| 253 |
|
|
if (lsiz < lmin)
|
| 254 |
|
|
lmin = lsiz;
|
| 255 |
|
|
}
|
| 256 |
|
|
return .5*lmin;
|
| 257 |
|
|
}
|
| 258 |
|
|
/* leaf grid width */
|
| 259 |
|
|
return 1. / (double)(1 << st->log2GR);
|
| 260 |
|
|
}
|
| 261 |
|
|
|
| 262 |
greg |
3.1 |
/* Add up N-dimensional hypercube array values over the given box */
|
| 263 |
|
|
static double
|
| 264 |
greg |
3.7 |
SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax)
|
| 265 |
greg |
3.1 |
{
|
| 266 |
greg |
3.10 |
const unsigned skipsiz = 1 << --nd*shft;
|
| 267 |
greg |
3.1 |
double sum = .0;
|
| 268 |
|
|
int i;
|
| 269 |
greg |
3.15 |
|
| 270 |
|
|
va += *imin * skipsiz;
|
| 271 |
|
|
|
| 272 |
greg |
3.1 |
if (skipsiz == 1)
|
| 273 |
|
|
for (i = *imin; i < *imax; i++)
|
| 274 |
greg |
3.15 |
sum += *va++;
|
| 275 |
greg |
3.1 |
else
|
| 276 |
greg |
3.15 |
for (i = *imin; i < *imax; i++, va += skipsiz)
|
| 277 |
|
|
sum += SDiterSum(va, nd, shft, imin+1, imax+1);
|
| 278 |
greg |
3.1 |
return sum;
|
| 279 |
|
|
}
|
| 280 |
|
|
|
| 281 |
|
|
/* Average BSDF leaves over an orthotope defined by the unit hypercube */
|
| 282 |
|
|
static double
|
| 283 |
greg |
3.6 |
SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax)
|
| 284 |
greg |
3.1 |
{
|
| 285 |
|
|
unsigned n;
|
| 286 |
|
|
int i;
|
| 287 |
|
|
|
| 288 |
|
|
if (!st)
|
| 289 |
|
|
return .0;
|
| 290 |
|
|
/* check box limits */
|
| 291 |
|
|
for (i = st->ndim; i--; ) {
|
| 292 |
|
|
if (bmin[i] >= 1.)
|
| 293 |
|
|
return .0;
|
| 294 |
greg |
3.13 |
if (bmax[i] <= 0)
|
| 295 |
greg |
3.1 |
return .0;
|
| 296 |
|
|
if (bmin[i] >= bmax[i])
|
| 297 |
|
|
return .0;
|
| 298 |
|
|
}
|
| 299 |
|
|
if (st->log2GR < 0) { /* iterate on subtree */
|
| 300 |
|
|
double sum = .0, wsum = 1e-20;
|
| 301 |
|
|
double sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w;
|
| 302 |
|
|
for (n = 1 << st->ndim; n--; ) {
|
| 303 |
|
|
w = 1.;
|
| 304 |
|
|
for (i = st->ndim; i--; ) {
|
| 305 |
|
|
sbmin[i] = 2.*bmin[i];
|
| 306 |
|
|
sbmax[i] = 2.*bmax[i];
|
| 307 |
|
|
if (n & 1<<i) {
|
| 308 |
|
|
sbmin[i] -= 1.;
|
| 309 |
|
|
sbmax[i] -= 1.;
|
| 310 |
|
|
}
|
| 311 |
|
|
if (sbmin[i] < .0) sbmin[i] = .0;
|
| 312 |
|
|
if (sbmax[i] > 1.) sbmax[i] = 1.;
|
| 313 |
greg |
3.13 |
if (sbmin[i] >= sbmax[i]) {
|
| 314 |
|
|
w = .0;
|
| 315 |
|
|
break;
|
| 316 |
|
|
}
|
| 317 |
greg |
3.1 |
w *= sbmax[i] - sbmin[i];
|
| 318 |
|
|
}
|
| 319 |
|
|
if (w > 1e-10) {
|
| 320 |
greg |
3.6 |
sum += w * SDavgTreBox(st->u.t[n], sbmin, sbmax);
|
| 321 |
greg |
3.1 |
wsum += w;
|
| 322 |
|
|
}
|
| 323 |
|
|
}
|
| 324 |
|
|
return sum / wsum;
|
| 325 |
greg |
3.15 |
} else { /* iterate over leaves */
|
| 326 |
|
|
int imin[SD_MAXDIM], imax[SD_MAXDIM];
|
| 327 |
|
|
|
| 328 |
|
|
n = 1;
|
| 329 |
|
|
for (i = st->ndim; i--; ) {
|
| 330 |
|
|
imin[i] = (bmin[i] <= 0) ? 0 :
|
| 331 |
|
|
(int)((1 << st->log2GR)*bmin[i]);
|
| 332 |
|
|
imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) :
|
| 333 |
|
|
(int)((1 << st->log2GR)*bmax[i] + .999999);
|
| 334 |
|
|
n *= imax[i] - imin[i];
|
| 335 |
|
|
}
|
| 336 |
|
|
if (n)
|
| 337 |
|
|
return SDiterSum(st->u.v, st->ndim,
|
| 338 |
|
|
st->log2GR, imin, imax) / (double)n;
|
| 339 |
greg |
3.1 |
}
|
| 340 |
greg |
3.15 |
return .0;
|
| 341 |
greg |
3.1 |
}
|
| 342 |
|
|
|
| 343 |
greg |
3.6 |
/* Recursive call for SDtraverseTre() */
|
| 344 |
|
|
static int
|
| 345 |
|
|
SDdotravTre(const SDNode *st, const double *pos, int cmask,
|
| 346 |
|
|
SDtreCallback *cf, void *cptr,
|
| 347 |
|
|
const double *cmin, double csiz)
|
| 348 |
|
|
{
|
| 349 |
|
|
int rv, rval = 0;
|
| 350 |
|
|
double bmin[SD_MAXDIM];
|
| 351 |
|
|
int i, n;
|
| 352 |
greg |
3.37 |
/* paranoia */
|
| 353 |
|
|
if (st == NULL)
|
| 354 |
|
|
return 0;
|
| 355 |
greg |
3.6 |
/* in branches? */
|
| 356 |
|
|
if (st->log2GR < 0) {
|
| 357 |
|
|
unsigned skipmask = 0;
|
| 358 |
|
|
csiz *= .5;
|
| 359 |
|
|
for (i = st->ndim; i--; )
|
| 360 |
greg |
3.32 |
if (1<<i & cmask) {
|
| 361 |
greg |
3.6 |
if (pos[i] < cmin[i] + csiz)
|
| 362 |
greg |
3.13 |
for (n = 1 << st->ndim; n--; ) {
|
| 363 |
greg |
3.6 |
if (n & 1<<i)
|
| 364 |
|
|
skipmask |= 1<<n;
|
| 365 |
greg |
3.13 |
}
|
| 366 |
greg |
3.6 |
else
|
| 367 |
greg |
3.13 |
for (n = 1 << st->ndim; n--; ) {
|
| 368 |
greg |
3.6 |
if (!(n & 1<<i))
|
| 369 |
|
|
skipmask |= 1<<n;
|
| 370 |
greg |
3.13 |
}
|
| 371 |
greg |
3.32 |
}
|
| 372 |
greg |
3.6 |
for (n = 1 << st->ndim; n--; ) {
|
| 373 |
|
|
if (1<<n & skipmask)
|
| 374 |
|
|
continue;
|
| 375 |
|
|
for (i = st->ndim; i--; )
|
| 376 |
|
|
if (1<<i & n)
|
| 377 |
|
|
bmin[i] = cmin[i] + csiz;
|
| 378 |
|
|
else
|
| 379 |
|
|
bmin[i] = cmin[i];
|
| 380 |
|
|
|
| 381 |
|
|
rval += rv = SDdotravTre(st->u.t[n], pos, cmask,
|
| 382 |
|
|
cf, cptr, bmin, csiz);
|
| 383 |
|
|
if (rv < 0)
|
| 384 |
|
|
return rv;
|
| 385 |
|
|
}
|
| 386 |
|
|
} else { /* else traverse leaves */
|
| 387 |
|
|
int clim[SD_MAXDIM][2];
|
| 388 |
|
|
int cpos[SD_MAXDIM];
|
| 389 |
|
|
|
| 390 |
|
|
if (st->log2GR == 0) /* short cut */
|
| 391 |
|
|
return (*cf)(st->u.v[0], cmin, csiz, cptr);
|
| 392 |
|
|
|
| 393 |
|
|
csiz /= (double)(1 << st->log2GR);
|
| 394 |
|
|
/* assign coord. ranges */
|
| 395 |
|
|
for (i = st->ndim; i--; )
|
| 396 |
|
|
if (1<<i & cmask) {
|
| 397 |
|
|
clim[i][0] = (pos[i] - cmin[i])/csiz;
|
| 398 |
|
|
/* check overflow from f.p. error */
|
| 399 |
|
|
clim[i][0] -= clim[i][0] >> st->log2GR;
|
| 400 |
|
|
clim[i][1] = clim[i][0] + 1;
|
| 401 |
|
|
} else {
|
| 402 |
|
|
clim[i][0] = 0;
|
| 403 |
|
|
clim[i][1] = 1 << st->log2GR;
|
| 404 |
|
|
}
|
| 405 |
|
|
#if (SD_MAXDIM == 4)
|
| 406 |
|
|
bmin[0] = cmin[0] + csiz*clim[0][0];
|
| 407 |
|
|
for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) {
|
| 408 |
|
|
bmin[1] = cmin[1] + csiz*clim[1][0];
|
| 409 |
|
|
for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) {
|
| 410 |
|
|
bmin[2] = cmin[2] + csiz*clim[2][0];
|
| 411 |
greg |
3.16 |
if (st->ndim == 3) {
|
| 412 |
|
|
cpos[2] = clim[2][0];
|
| 413 |
greg |
3.6 |
n = cpos[0];
|
| 414 |
greg |
3.16 |
for (i = 1; i < 3; i++)
|
| 415 |
greg |
3.6 |
n = (n << st->log2GR) + cpos[i];
|
| 416 |
greg |
3.16 |
for ( ; cpos[2] < clim[2][1]; cpos[2]++) {
|
| 417 |
greg |
3.6 |
rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
|
| 418 |
|
|
if (rv < 0)
|
| 419 |
|
|
return rv;
|
| 420 |
greg |
3.16 |
bmin[2] += csiz;
|
| 421 |
|
|
}
|
| 422 |
|
|
} else {
|
| 423 |
|
|
for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
|
| 424 |
|
|
bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
|
| 425 |
|
|
n = cpos[0];
|
| 426 |
|
|
for (i = 1; i < 4; i++)
|
| 427 |
|
|
n = (n << st->log2GR) + cpos[i];
|
| 428 |
|
|
for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
|
| 429 |
|
|
rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
|
| 430 |
|
|
if (rv < 0)
|
| 431 |
|
|
return rv;
|
| 432 |
|
|
bmin[3] += csiz;
|
| 433 |
|
|
}
|
| 434 |
|
|
bmin[2] += csiz;
|
| 435 |
greg |
3.6 |
}
|
| 436 |
|
|
}
|
| 437 |
|
|
bmin[1] += csiz;
|
| 438 |
|
|
}
|
| 439 |
|
|
bmin[0] += csiz;
|
| 440 |
|
|
}
|
| 441 |
|
|
#else
|
| 442 |
|
|
_!_ "broken code segment!"
|
| 443 |
|
|
#endif
|
| 444 |
|
|
}
|
| 445 |
|
|
return rval;
|
| 446 |
|
|
}
|
| 447 |
|
|
|
| 448 |
|
|
/* Traverse a tree, visiting nodes in a slice that fits partial position */
|
| 449 |
|
|
static int
|
| 450 |
|
|
SDtraverseTre(const SDNode *st, const double *pos, int cmask,
|
| 451 |
|
|
SDtreCallback *cf, void *cptr)
|
| 452 |
|
|
{
|
| 453 |
|
|
int i;
|
| 454 |
|
|
/* check arguments */
|
| 455 |
|
|
if ((st == NULL) | (cf == NULL))
|
| 456 |
|
|
return -1;
|
| 457 |
|
|
for (i = st->ndim; i--; )
|
| 458 |
|
|
if (1<<i & cmask && (pos[i] < 0) | (pos[i] >= 1.))
|
| 459 |
|
|
return -1;
|
| 460 |
|
|
|
| 461 |
|
|
return SDdotravTre(st, pos, cmask, cf, cptr, czero, 1.);
|
| 462 |
|
|
}
|
| 463 |
greg |
3.5 |
|
| 464 |
|
|
/* Look up tree value at the given grid position */
|
| 465 |
|
|
static float
|
| 466 |
greg |
3.6 |
SDlookupTre(const SDNode *st, const double *pos, double *hcube)
|
| 467 |
greg |
3.5 |
{
|
| 468 |
|
|
double spos[SD_MAXDIM];
|
| 469 |
|
|
int i, n, t;
|
| 470 |
greg |
3.6 |
/* initialize voxel return */
|
| 471 |
|
|
if (hcube) {
|
| 472 |
|
|
hcube[i = st->ndim] = 1.;
|
| 473 |
|
|
while (i--)
|
| 474 |
|
|
hcube[i] = .0;
|
| 475 |
|
|
}
|
| 476 |
greg |
3.5 |
/* climb the tree */
|
| 477 |
greg |
3.37 |
while (st != NULL && st->log2GR < 0) {
|
| 478 |
greg |
3.5 |
n = 0; /* move to appropriate branch */
|
| 479 |
greg |
3.6 |
if (hcube) hcube[st->ndim] *= .5;
|
| 480 |
greg |
3.5 |
for (i = st->ndim; i--; ) {
|
| 481 |
|
|
spos[i] = 2.*pos[i];
|
| 482 |
|
|
t = (spos[i] >= 1.);
|
| 483 |
|
|
n |= t<<i;
|
| 484 |
|
|
spos[i] -= (double)t;
|
| 485 |
greg |
3.6 |
if (hcube) hcube[i] += (double)t * hcube[st->ndim];
|
| 486 |
greg |
3.5 |
}
|
| 487 |
|
|
st = st->u.t[n]; /* avoids tail recursion */
|
| 488 |
|
|
pos = spos;
|
| 489 |
|
|
}
|
| 490 |
greg |
3.37 |
if (st == NULL) /* should never happen? */
|
| 491 |
|
|
return .0;
|
| 492 |
greg |
3.6 |
if (st->log2GR == 0) /* short cut */
|
| 493 |
|
|
return st->u.v[0];
|
| 494 |
greg |
3.5 |
n = t = 0; /* find grid array index */
|
| 495 |
|
|
for (i = st->ndim; i--; ) {
|
| 496 |
|
|
n += (int)((1<<st->log2GR)*pos[i]) << t;
|
| 497 |
|
|
t += st->log2GR;
|
| 498 |
|
|
}
|
| 499 |
greg |
3.6 |
if (hcube) { /* compute final hypercube */
|
| 500 |
|
|
hcube[st->ndim] /= (double)(1<<st->log2GR);
|
| 501 |
|
|
for (i = st->ndim; i--; )
|
| 502 |
|
|
hcube[i] += floor((1<<st->log2GR)*pos[i])*hcube[st->ndim];
|
| 503 |
|
|
}
|
| 504 |
|
|
return st->u.v[n]; /* no interpolation */
|
| 505 |
|
|
}
|
| 506 |
|
|
|
| 507 |
greg |
3.37 |
/* Convert CIE (Y,u',v') color to our RGB */
|
| 508 |
|
|
static void
|
| 509 |
|
|
SDyuv2rgb(double yval, double uprime, double vprime, float rgb[3])
|
| 510 |
|
|
{
|
| 511 |
|
|
const double dfact = 1./(6.*uprime - 16.*vprime + 12.);
|
| 512 |
|
|
C_COLOR cxy;
|
| 513 |
|
|
|
| 514 |
|
|
c_cset(&cxy, 9.*uprime*dfact, 4.*vprime*dfact);
|
| 515 |
|
|
c_toSharpRGB(&cxy, yval, rgb);
|
| 516 |
|
|
}
|
| 517 |
|
|
|
| 518 |
greg |
3.6 |
/* Query BSDF value and sample hypercube for the given vectors */
|
| 519 |
greg |
3.37 |
static int
|
| 520 |
|
|
SDqueryTre(const SDTre *sdt, float *coef,
|
| 521 |
|
|
const FVECT outVec, const FVECT inVec, double *hc)
|
| 522 |
greg |
3.6 |
{
|
| 523 |
greg |
3.24 |
const RREAL *vtmp;
|
| 524 |
greg |
3.37 |
float yval;
|
| 525 |
greg |
3.24 |
FVECT rOutVec;
|
| 526 |
|
|
double gridPos[4];
|
| 527 |
greg |
3.7 |
|
| 528 |
greg |
3.37 |
if (sdt->stc[tt_Y] == NULL) /* paranoia, I hope */
|
| 529 |
|
|
return 0;
|
| 530 |
|
|
|
| 531 |
greg |
3.7 |
switch (sdt->sidef) { /* whose side are you on? */
|
| 532 |
greg |
3.24 |
case SD_FREFL:
|
| 533 |
greg |
3.7 |
if ((outVec[2] < 0) | (inVec[2] < 0))
|
| 534 |
greg |
3.37 |
return 0;
|
| 535 |
greg |
3.7 |
break;
|
| 536 |
greg |
3.24 |
case SD_BREFL:
|
| 537 |
greg |
3.7 |
if ((outVec[2] > 0) | (inVec[2] > 0))
|
| 538 |
greg |
3.37 |
return 0;
|
| 539 |
greg |
3.7 |
break;
|
| 540 |
greg |
3.24 |
case SD_FXMIT:
|
| 541 |
|
|
if (outVec[2] > 0) {
|
| 542 |
|
|
if (inVec[2] > 0)
|
| 543 |
greg |
3.37 |
return 0;
|
| 544 |
greg |
3.24 |
vtmp = outVec; outVec = inVec; inVec = vtmp;
|
| 545 |
|
|
} else if (inVec[2] < 0)
|
| 546 |
greg |
3.37 |
return 0;
|
| 547 |
greg |
3.24 |
break;
|
| 548 |
|
|
case SD_BXMIT:
|
| 549 |
|
|
if (inVec[2] > 0) {
|
| 550 |
|
|
if (outVec[2] > 0)
|
| 551 |
greg |
3.37 |
return 0;
|
| 552 |
greg |
3.24 |
vtmp = outVec; outVec = inVec; inVec = vtmp;
|
| 553 |
|
|
} else if (outVec[2] < 0)
|
| 554 |
greg |
3.37 |
return 0;
|
| 555 |
greg |
3.7 |
break;
|
| 556 |
|
|
default:
|
| 557 |
greg |
3.37 |
return 0;
|
| 558 |
greg |
3.7 |
}
|
| 559 |
greg |
3.6 |
/* convert vector coordinates */
|
| 560 |
greg |
3.37 |
if (sdt->stc[tt_Y]->ndim == 3) {
|
| 561 |
greg |
3.15 |
spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
|
| 562 |
greg |
3.31 |
gridPos[0] = (.5-FTINY) -
|
| 563 |
|
|
.5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
|
| 564 |
greg |
3.6 |
SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
|
| 565 |
greg |
3.37 |
} else if (sdt->stc[tt_Y]->ndim == 4) {
|
| 566 |
greg |
3.6 |
SDdisk2square(gridPos, -inVec[0], -inVec[1]);
|
| 567 |
|
|
SDdisk2square(gridPos+2, outVec[0], outVec[1]);
|
| 568 |
|
|
} else
|
| 569 |
greg |
3.37 |
return 0; /* should be internal error */
|
| 570 |
|
|
/* get BSDF value */
|
| 571 |
|
|
yval = SDlookupTre(sdt->stc[tt_Y], gridPos, hc);
|
| 572 |
greg |
3.41 |
if (coef == NULL) /* just getting hypercube? */
|
| 573 |
|
|
return 1;
|
| 574 |
|
|
if ((sdt->stc[tt_u] == NULL) | (sdt->stc[tt_v] == NULL)) {
|
| 575 |
|
|
*coef = yval;
|
| 576 |
greg |
3.37 |
return 1; /* no color */
|
| 577 |
|
|
}
|
| 578 |
|
|
/* else decode color */
|
| 579 |
|
|
SDyuv2rgb(yval, SDlookupTre(sdt->stc[tt_u], gridPos, NULL),
|
| 580 |
|
|
SDlookupTre(sdt->stc[tt_v], gridPos, NULL), coef);
|
| 581 |
|
|
coef[0] *= tt_RGB_coef[0];
|
| 582 |
|
|
coef[1] *= tt_RGB_coef[1];
|
| 583 |
|
|
coef[2] *= tt_RGB_coef[2];
|
| 584 |
|
|
return 3;
|
| 585 |
greg |
3.5 |
}
|
| 586 |
|
|
|
| 587 |
|
|
/* Compute non-diffuse component for variable-resolution BSDF */
|
| 588 |
|
|
static int
|
| 589 |
|
|
SDgetTreBSDF(float coef[SDmaxCh], const FVECT outVec,
|
| 590 |
greg |
3.6 |
const FVECT inVec, SDComponent *sdc)
|
| 591 |
greg |
3.5 |
{
|
| 592 |
greg |
3.6 |
/* check arguments */
|
| 593 |
|
|
if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)
|
| 594 |
|
|
|| sdc->dist == NULL)
|
| 595 |
|
|
return 0;
|
| 596 |
greg |
3.5 |
/* get nearest BSDF value */
|
| 597 |
greg |
3.37 |
return SDqueryTre((SDTre *)sdc->dist, coef, outVec, inVec, NULL);
|
| 598 |
greg |
3.6 |
}
|
| 599 |
|
|
|
| 600 |
|
|
/* Callback to build cumulative distribution using SDtraverseTre() */
|
| 601 |
|
|
static int
|
| 602 |
|
|
build_scaffold(float val, const double *cmin, double csiz, void *cptr)
|
| 603 |
|
|
{
|
| 604 |
|
|
SDdistScaffold *sp = (SDdistScaffold *)cptr;
|
| 605 |
|
|
int wid = csiz*(double)iwmax + .5;
|
| 606 |
greg |
3.25 |
double revcmin[2];
|
| 607 |
greg |
3.6 |
bitmask_t bmin[2], bmax[2];
|
| 608 |
|
|
|
| 609 |
greg |
3.25 |
if (sp->rev) { /* need to reverse sense? */
|
| 610 |
|
|
revcmin[0] = 1. - cmin[0] - csiz;
|
| 611 |
|
|
revcmin[1] = 1. - cmin[1] - csiz;
|
| 612 |
|
|
cmin = revcmin;
|
| 613 |
|
|
} else {
|
| 614 |
|
|
cmin += sp->nic; /* else skip to output coords */
|
| 615 |
|
|
}
|
| 616 |
greg |
3.6 |
if (wid < sp->wmin) /* new minimum width? */
|
| 617 |
|
|
sp->wmin = wid;
|
| 618 |
|
|
if (wid > sp->wmax) /* new maximum? */
|
| 619 |
|
|
sp->wmax = wid;
|
| 620 |
|
|
if (sp->alen >= sp->nall) { /* need more space? */
|
| 621 |
|
|
struct outdir_s *ndarr;
|
| 622 |
greg |
3.35 |
sp->nall = (int)(1.5*sp->nall) + 256;
|
| 623 |
greg |
3.6 |
ndarr = (struct outdir_s *)realloc(sp->darr,
|
| 624 |
|
|
sizeof(struct outdir_s)*sp->nall);
|
| 625 |
greg |
3.12 |
if (ndarr == NULL) {
|
| 626 |
|
|
sprintf(SDerrorDetail,
|
| 627 |
|
|
"Cannot grow scaffold to %u entries", sp->nall);
|
| 628 |
greg |
3.6 |
return -1; /* abort build */
|
| 629 |
greg |
3.12 |
}
|
| 630 |
greg |
3.6 |
sp->darr = ndarr;
|
| 631 |
|
|
}
|
| 632 |
|
|
/* find Hilbert entry index */
|
| 633 |
|
|
bmin[0] = cmin[0]*(double)iwmax + .5;
|
| 634 |
|
|
bmin[1] = cmin[1]*(double)iwmax + .5;
|
| 635 |
greg |
3.10 |
bmax[0] = bmin[0] + wid-1;
|
| 636 |
|
|
bmax[1] = bmin[1] + wid-1;
|
| 637 |
greg |
3.7 |
hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
|
| 638 |
|
|
sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
|
| 639 |
greg |
3.6 |
sp->darr[sp->alen].wid = wid;
|
| 640 |
|
|
sp->darr[sp->alen].bsdf = val;
|
| 641 |
|
|
sp->alen++; /* on to the next entry */
|
| 642 |
|
|
return 0;
|
| 643 |
|
|
}
|
| 644 |
|
|
|
| 645 |
|
|
/* Scaffold comparison function for qsort -- ascending Hilbert index */
|
| 646 |
|
|
static int
|
| 647 |
|
|
sscmp(const void *p1, const void *p2)
|
| 648 |
|
|
{
|
| 649 |
greg |
3.10 |
unsigned h1 = (*(const struct outdir_s *)p1).hent;
|
| 650 |
|
|
unsigned h2 = (*(const struct outdir_s *)p2).hent;
|
| 651 |
|
|
|
| 652 |
|
|
if (h1 > h2)
|
| 653 |
|
|
return 1;
|
| 654 |
|
|
if (h1 < h2)
|
| 655 |
|
|
return -1;
|
| 656 |
|
|
return 0;
|
| 657 |
greg |
3.6 |
}
|
| 658 |
|
|
|
| 659 |
|
|
/* Create a new cumulative distribution for the given input direction */
|
| 660 |
|
|
static SDTreCDst *
|
| 661 |
greg |
3.24 |
make_cdist(const SDTre *sdt, const double *invec, int rev)
|
| 662 |
greg |
3.6 |
{
|
| 663 |
|
|
SDdistScaffold myScaffold;
|
| 664 |
greg |
3.24 |
double pos[4];
|
| 665 |
|
|
int cmask;
|
| 666 |
greg |
3.6 |
SDTreCDst *cd;
|
| 667 |
|
|
struct outdir_s *sp;
|
| 668 |
|
|
double scale, cursum;
|
| 669 |
|
|
int i;
|
| 670 |
|
|
/* initialize scaffold */
|
| 671 |
|
|
myScaffold.wmin = iwmax;
|
| 672 |
|
|
myScaffold.wmax = 0;
|
| 673 |
greg |
3.37 |
myScaffold.nic = sdt->stc[tt_Y]->ndim - 2;
|
| 674 |
greg |
3.24 |
myScaffold.rev = rev;
|
| 675 |
greg |
3.6 |
myScaffold.alen = 0;
|
| 676 |
greg |
3.12 |
myScaffold.nall = 512;
|
| 677 |
greg |
3.6 |
myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
|
| 678 |
|
|
myScaffold.nall);
|
| 679 |
|
|
if (myScaffold.darr == NULL)
|
| 680 |
|
|
return NULL;
|
| 681 |
greg |
3.24 |
/* set up traversal */
|
| 682 |
|
|
cmask = (1<<myScaffold.nic) - 1;
|
| 683 |
|
|
for (i = myScaffold.nic; i--; )
|
| 684 |
|
|
pos[i+2*rev] = invec[i];
|
| 685 |
|
|
cmask <<= 2*rev;
|
| 686 |
greg |
3.6 |
/* grow the distribution */
|
| 687 |
greg |
3.37 |
if (SDtraverseTre(sdt->stc[tt_Y], pos, cmask,
|
| 688 |
|
|
build_scaffold, &myScaffold) < 0) {
|
| 689 |
greg |
3.6 |
free(myScaffold.darr);
|
| 690 |
|
|
return NULL;
|
| 691 |
|
|
}
|
| 692 |
|
|
/* allocate result holder */
|
| 693 |
|
|
cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
|
| 694 |
|
|
sizeof(cd->carr[0])*myScaffold.alen);
|
| 695 |
|
|
if (cd == NULL) {
|
| 696 |
greg |
3.12 |
sprintf(SDerrorDetail,
|
| 697 |
|
|
"Cannot allocate %u entry cumulative distribution",
|
| 698 |
|
|
myScaffold.alen);
|
| 699 |
greg |
3.6 |
free(myScaffold.darr);
|
| 700 |
|
|
return NULL;
|
| 701 |
|
|
}
|
| 702 |
greg |
3.15 |
cd->isodist = (myScaffold.nic == 1);
|
| 703 |
greg |
3.6 |
/* sort the distribution */
|
| 704 |
|
|
qsort(myScaffold.darr, cd->calen = myScaffold.alen,
|
| 705 |
greg |
3.37 |
sizeof(struct outdir_s), sscmp);
|
| 706 |
greg |
3.6 |
|
| 707 |
|
|
/* record input range */
|
| 708 |
greg |
3.7 |
scale = myScaffold.wmin / (double)iwmax;
|
| 709 |
greg |
3.6 |
for (i = myScaffold.nic; i--; ) {
|
| 710 |
greg |
3.26 |
cd->clim[i][0] = floor(pos[i+2*rev]/scale) * scale;
|
| 711 |
greg |
3.6 |
cd->clim[i][1] = cd->clim[i][0] + scale;
|
| 712 |
|
|
}
|
| 713 |
greg |
3.15 |
if (cd->isodist) { /* avoid issue in SDqueryTreProjSA() */
|
| 714 |
|
|
cd->clim[1][0] = cd->clim[0][0];
|
| 715 |
|
|
cd->clim[1][1] = cd->clim[0][1];
|
| 716 |
|
|
}
|
| 717 |
greg |
3.6 |
cd->max_psa = myScaffold.wmax / (double)iwmax;
|
| 718 |
|
|
cd->max_psa *= cd->max_psa * M_PI;
|
| 719 |
greg |
3.24 |
if (rev)
|
| 720 |
|
|
cd->sidef = (sdt->sidef==SD_BXMIT) ? SD_FXMIT : SD_BXMIT;
|
| 721 |
|
|
else
|
| 722 |
|
|
cd->sidef = sdt->sidef;
|
| 723 |
greg |
3.6 |
cd->cTotal = 1e-20; /* compute directional total */
|
| 724 |
|
|
sp = myScaffold.darr;
|
| 725 |
|
|
for (i = myScaffold.alen; i--; sp++)
|
| 726 |
|
|
cd->cTotal += sp->bsdf * (double)sp->wid * sp->wid;
|
| 727 |
|
|
cursum = .0; /* go back and get cumulative values */
|
| 728 |
|
|
scale = (double)cumlmax / cd->cTotal;
|
| 729 |
|
|
sp = myScaffold.darr;
|
| 730 |
|
|
for (i = 0; i < cd->calen; i++, sp++) {
|
| 731 |
greg |
3.7 |
cd->carr[i].hndx = sp->hent;
|
| 732 |
greg |
3.6 |
cd->carr[i].cuml = scale*cursum + .5;
|
| 733 |
|
|
cursum += sp->bsdf * (double)sp->wid * sp->wid;
|
| 734 |
|
|
}
|
| 735 |
|
|
cd->carr[i].hndx = ~0; /* make final entry */
|
| 736 |
|
|
cd->carr[i].cuml = cumlmax;
|
| 737 |
|
|
cd->cTotal *= M_PI/(double)iwmax/iwmax;
|
| 738 |
|
|
/* all done, clean up and return */
|
| 739 |
|
|
free(myScaffold.darr);
|
| 740 |
|
|
return cd;
|
| 741 |
|
|
}
|
| 742 |
|
|
|
| 743 |
|
|
/* Find or allocate a cumulative distribution for the given incoming vector */
|
| 744 |
|
|
const SDCDst *
|
| 745 |
|
|
SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
|
| 746 |
|
|
{
|
| 747 |
|
|
const SDTre *sdt;
|
| 748 |
greg |
3.22 |
double inCoord[2];
|
| 749 |
greg |
3.6 |
int i;
|
| 750 |
greg |
3.24 |
int mode;
|
| 751 |
greg |
3.6 |
SDTreCDst *cd, *cdlast;
|
| 752 |
|
|
/* check arguments */
|
| 753 |
|
|
if ((inVec == NULL) | (sdc == NULL) ||
|
| 754 |
|
|
(sdt = (SDTre *)sdc->dist) == NULL)
|
| 755 |
|
|
return NULL;
|
| 756 |
greg |
3.24 |
switch (mode = sdt->sidef) { /* check direction */
|
| 757 |
|
|
case SD_FREFL:
|
| 758 |
|
|
if (inVec[2] < 0)
|
| 759 |
|
|
return NULL;
|
| 760 |
|
|
break;
|
| 761 |
|
|
case SD_BREFL:
|
| 762 |
|
|
if (inVec[2] > 0)
|
| 763 |
|
|
return NULL;
|
| 764 |
|
|
break;
|
| 765 |
|
|
case SD_FXMIT:
|
| 766 |
|
|
if (inVec[2] < 0)
|
| 767 |
|
|
mode = SD_BXMIT;
|
| 768 |
|
|
break;
|
| 769 |
|
|
case SD_BXMIT:
|
| 770 |
|
|
if (inVec[2] > 0)
|
| 771 |
|
|
mode = SD_FXMIT;
|
| 772 |
|
|
break;
|
| 773 |
|
|
default:
|
| 774 |
|
|
return NULL;
|
| 775 |
|
|
}
|
| 776 |
greg |
3.37 |
if (sdt->stc[tt_Y]->ndim == 3) { /* isotropic BSDF? */
|
| 777 |
greg |
3.24 |
if (mode != sdt->sidef) /* XXX unhandled reciprocity */
|
| 778 |
|
|
return &SDemptyCD;
|
| 779 |
greg |
3.31 |
inCoord[0] = (.5-FTINY) -
|
| 780 |
|
|
.5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
|
| 781 |
greg |
3.37 |
} else if (sdt->stc[tt_Y]->ndim == 4) {
|
| 782 |
greg |
3.25 |
if (mode != sdt->sidef) /* use reciprocity? */
|
| 783 |
|
|
SDdisk2square(inCoord, inVec[0], inVec[1]);
|
| 784 |
|
|
else
|
| 785 |
|
|
SDdisk2square(inCoord, -inVec[0], -inVec[1]);
|
| 786 |
greg |
3.21 |
} else
|
| 787 |
greg |
3.6 |
return NULL; /* should be internal error */
|
| 788 |
greg |
3.21 |
/* quantize to avoid f.p. errors */
|
| 789 |
greg |
3.37 |
for (i = sdt->stc[tt_Y]->ndim - 2; i--; )
|
| 790 |
greg |
3.21 |
inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum;
|
| 791 |
greg |
3.6 |
cdlast = NULL; /* check for direction in cache list */
|
| 792 |
|
|
for (cd = (SDTreCDst *)sdc->cdList; cd != NULL;
|
| 793 |
greg |
3.20 |
cdlast = cd, cd = cd->next) {
|
| 794 |
greg |
3.24 |
if (cd->sidef != mode)
|
| 795 |
|
|
continue;
|
| 796 |
greg |
3.37 |
for (i = sdt->stc[tt_Y]->ndim - 2; i--; )
|
| 797 |
greg |
3.6 |
if ((cd->clim[i][0] > inCoord[i]) |
|
| 798 |
|
|
(inCoord[i] >= cd->clim[i][1]))
|
| 799 |
|
|
break;
|
| 800 |
|
|
if (i < 0)
|
| 801 |
|
|
break; /* means we have a match */
|
| 802 |
|
|
}
|
| 803 |
|
|
if (cd == NULL) /* need to create new entry? */
|
| 804 |
greg |
3.24 |
cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef);
|
| 805 |
greg |
3.6 |
if (cdlast != NULL) { /* move entry to head of cache list */
|
| 806 |
|
|
cdlast->next = cd->next;
|
| 807 |
greg |
3.20 |
cd->next = (SDTreCDst *)sdc->cdList;
|
| 808 |
greg |
3.6 |
sdc->cdList = (SDCDst *)cd;
|
| 809 |
|
|
}
|
| 810 |
|
|
return (SDCDst *)cd; /* ready to go */
|
| 811 |
|
|
}
|
| 812 |
|
|
|
| 813 |
|
|
/* Query solid angle for vector(s) */
|
| 814 |
|
|
static SDError
|
| 815 |
|
|
SDqueryTreProjSA(double *psa, const FVECT v1, const RREAL *v2,
|
| 816 |
|
|
int qflags, SDComponent *sdc)
|
| 817 |
|
|
{
|
| 818 |
|
|
double myPSA[2];
|
| 819 |
|
|
/* check arguments */
|
| 820 |
|
|
if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) ||
|
| 821 |
|
|
sdc->dist == NULL)
|
| 822 |
|
|
return SDEargument;
|
| 823 |
|
|
/* get projected solid angle(s) */
|
| 824 |
|
|
if (v2 != NULL) {
|
| 825 |
|
|
const SDTre *sdt = (SDTre *)sdc->dist;
|
| 826 |
greg |
3.36 |
double hcube[SD_MAXDIM+1];
|
| 827 |
greg |
3.37 |
if (!SDqueryTre(sdt, NULL, v1, v2, hcube)) {
|
| 828 |
greg |
3.7 |
strcpy(SDerrorDetail, "Bad call to SDqueryTreProjSA");
|
| 829 |
|
|
return SDEinternal;
|
| 830 |
greg |
3.6 |
}
|
| 831 |
greg |
3.37 |
myPSA[0] = hcube[sdt->stc[tt_Y]->ndim];
|
| 832 |
greg |
3.6 |
myPSA[1] = myPSA[0] *= myPSA[0] * M_PI;
|
| 833 |
|
|
} else {
|
| 834 |
|
|
const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc);
|
| 835 |
|
|
if (cd == NULL)
|
| 836 |
greg |
3.29 |
myPSA[0] = myPSA[1] = 0;
|
| 837 |
|
|
else {
|
| 838 |
|
|
myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) *
|
| 839 |
|
|
(cd->clim[1][1] - cd->clim[1][0]);
|
| 840 |
|
|
myPSA[1] = cd->max_psa;
|
| 841 |
|
|
}
|
| 842 |
greg |
3.6 |
}
|
| 843 |
|
|
switch (qflags) { /* record based on flag settings */
|
| 844 |
|
|
case SDqueryVal:
|
| 845 |
|
|
*psa = myPSA[0];
|
| 846 |
|
|
break;
|
| 847 |
|
|
case SDqueryMax:
|
| 848 |
|
|
if (myPSA[1] > *psa)
|
| 849 |
|
|
*psa = myPSA[1];
|
| 850 |
|
|
break;
|
| 851 |
|
|
case SDqueryMin+SDqueryMax:
|
| 852 |
|
|
if (myPSA[1] > psa[1])
|
| 853 |
|
|
psa[1] = myPSA[1];
|
| 854 |
|
|
/* fall through */
|
| 855 |
|
|
case SDqueryMin:
|
| 856 |
greg |
3.30 |
if ((myPSA[0] > 0) & (myPSA[0] < psa[0]))
|
| 857 |
greg |
3.6 |
psa[0] = myPSA[0];
|
| 858 |
|
|
break;
|
| 859 |
|
|
}
|
| 860 |
|
|
return SDEnone;
|
| 861 |
|
|
}
|
| 862 |
|
|
|
| 863 |
|
|
/* Sample cumulative distribution */
|
| 864 |
|
|
static SDError
|
| 865 |
|
|
SDsampTreCDist(FVECT ioVec, double randX, const SDCDst *cdp)
|
| 866 |
|
|
{
|
| 867 |
|
|
const unsigned nBitsC = 4*sizeof(bitmask_t);
|
| 868 |
|
|
const unsigned nExtraBits = 8*(sizeof(bitmask_t)-sizeof(unsigned));
|
| 869 |
|
|
const SDTreCDst *cd = (const SDTreCDst *)cdp;
|
| 870 |
greg |
3.7 |
const unsigned target = randX*cumlmax;
|
| 871 |
greg |
3.6 |
bitmask_t hndx, hcoord[2];
|
| 872 |
greg |
3.15 |
double gpos[3], rotangle;
|
| 873 |
greg |
3.6 |
int i, iupper, ilower;
|
| 874 |
|
|
/* check arguments */
|
| 875 |
|
|
if ((ioVec == NULL) | (cd == NULL))
|
| 876 |
|
|
return SDEargument;
|
| 877 |
greg |
3.24 |
if (!cd->sidef)
|
| 878 |
|
|
return SDEnone; /* XXX should never happen */
|
| 879 |
greg |
3.7 |
if (ioVec[2] > 0) {
|
| 880 |
greg |
3.24 |
if ((cd->sidef != SD_FREFL) & (cd->sidef != SD_FXMIT))
|
| 881 |
greg |
3.7 |
return SDEargument;
|
| 882 |
greg |
3.24 |
} else if ((cd->sidef != SD_BREFL) & (cd->sidef != SD_BXMIT))
|
| 883 |
greg |
3.7 |
return SDEargument;
|
| 884 |
greg |
3.6 |
/* binary search to find position */
|
| 885 |
|
|
ilower = 0; iupper = cd->calen;
|
| 886 |
|
|
while ((i = (iupper + ilower) >> 1) != ilower)
|
| 887 |
greg |
3.19 |
if (target >= cd->carr[i].cuml)
|
| 888 |
greg |
3.6 |
ilower = i;
|
| 889 |
|
|
else
|
| 890 |
|
|
iupper = i;
|
| 891 |
|
|
/* localize random position */
|
| 892 |
greg |
3.7 |
randX = (randX*cumlmax - cd->carr[ilower].cuml) /
|
| 893 |
greg |
3.6 |
(double)(cd->carr[iupper].cuml - cd->carr[ilower].cuml);
|
| 894 |
|
|
/* index in longer Hilbert curve */
|
| 895 |
|
|
hndx = (randX*cd->carr[iupper].hndx + (1.-randX)*cd->carr[ilower].hndx)
|
| 896 |
|
|
* (double)((bitmask_t)1 << nExtraBits);
|
| 897 |
|
|
/* convert Hilbert index to vector */
|
| 898 |
|
|
hilbert_i2c(2, nBitsC, hndx, hcoord);
|
| 899 |
|
|
for (i = 2; i--; )
|
| 900 |
|
|
gpos[i] = ((double)hcoord[i] + rand()*(1./(RAND_MAX+.5))) /
|
| 901 |
|
|
(double)((bitmask_t)1 << nBitsC);
|
| 902 |
|
|
SDsquare2disk(gpos, gpos[0], gpos[1]);
|
| 903 |
greg |
3.7 |
/* compute Z-coordinate */
|
| 904 |
greg |
3.6 |
gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1];
|
| 905 |
greg |
3.32 |
gpos[2] = sqrt(gpos[2]*(gpos[2]>0));
|
| 906 |
greg |
3.7 |
/* emit from back? */
|
| 907 |
greg |
3.24 |
if ((cd->sidef == SD_BREFL) | (cd->sidef == SD_FXMIT))
|
| 908 |
greg |
3.6 |
gpos[2] = -gpos[2];
|
| 909 |
greg |
3.34 |
if (cd->isodist) { /* rotate isotropic sample */
|
| 910 |
greg |
3.15 |
rotangle = atan2(-ioVec[1],-ioVec[0]);
|
| 911 |
greg |
3.34 |
spinvector(ioVec, gpos, zvec, rotangle);
|
| 912 |
greg |
3.15 |
} else
|
| 913 |
|
|
VCOPY(ioVec, gpos);
|
| 914 |
greg |
3.6 |
return SDEnone;
|
| 915 |
greg |
3.5 |
}
|
| 916 |
|
|
|
| 917 |
greg |
3.7 |
/* Advance pointer to the next non-white character in the string (or nul) */
|
| 918 |
|
|
static int
|
| 919 |
|
|
next_token(char **spp)
|
| 920 |
|
|
{
|
| 921 |
|
|
while (isspace(**spp))
|
| 922 |
|
|
++*spp;
|
| 923 |
|
|
return **spp;
|
| 924 |
|
|
}
|
| 925 |
|
|
|
| 926 |
greg |
3.12 |
/* Advance pointer past matching token (or any token if c==0) */
|
| 927 |
|
|
#define eat_token(spp,c) (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
|
| 928 |
greg |
3.9 |
|
| 929 |
greg |
3.7 |
/* Count words from this point in string to '}' */
|
| 930 |
|
|
static int
|
| 931 |
|
|
count_values(char *cp)
|
| 932 |
|
|
{
|
| 933 |
|
|
int n = 0;
|
| 934 |
|
|
|
| 935 |
greg |
3.9 |
while (next_token(&cp) != '}' && *cp) {
|
| 936 |
greg |
3.11 |
while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
|
| 937 |
|
|
if (!*++cp)
|
| 938 |
|
|
break;
|
| 939 |
greg |
3.7 |
++n;
|
| 940 |
greg |
3.9 |
eat_token(&cp, ',');
|
| 941 |
greg |
3.7 |
}
|
| 942 |
|
|
return n;
|
| 943 |
|
|
}
|
| 944 |
|
|
|
| 945 |
|
|
/* Load an array of real numbers, returning total */
|
| 946 |
|
|
static int
|
| 947 |
|
|
load_values(char **spp, float *va, int n)
|
| 948 |
|
|
{
|
| 949 |
|
|
float *v = va;
|
| 950 |
|
|
char *svnext;
|
| 951 |
|
|
|
| 952 |
|
|
while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
|
| 953 |
greg |
3.33 |
if ((*v++ = atof(*spp)) < 0)
|
| 954 |
|
|
v[-1] = 0;
|
| 955 |
greg |
3.7 |
*spp = svnext;
|
| 956 |
greg |
3.9 |
eat_token(spp, ',');
|
| 957 |
greg |
3.7 |
}
|
| 958 |
|
|
return v - va;
|
| 959 |
|
|
}
|
| 960 |
|
|
|
| 961 |
|
|
/* Load BSDF tree data */
|
| 962 |
|
|
static SDNode *
|
| 963 |
|
|
load_tree_data(char **spp, int nd)
|
| 964 |
|
|
{
|
| 965 |
|
|
SDNode *st;
|
| 966 |
|
|
int n;
|
| 967 |
|
|
|
| 968 |
greg |
3.9 |
if (!eat_token(spp, '{')) {
|
| 969 |
greg |
3.7 |
strcpy(SDerrorDetail, "Missing '{' in tensor tree");
|
| 970 |
|
|
return NULL;
|
| 971 |
|
|
}
|
| 972 |
|
|
if (next_token(spp) == '{') { /* tree branches */
|
| 973 |
|
|
st = SDnewNode(nd, -1);
|
| 974 |
|
|
if (st == NULL)
|
| 975 |
|
|
return NULL;
|
| 976 |
|
|
for (n = 0; n < 1<<nd; n++)
|
| 977 |
|
|
if ((st->u.t[n] = load_tree_data(spp, nd)) == NULL) {
|
| 978 |
|
|
SDfreeTre(st);
|
| 979 |
|
|
return NULL;
|
| 980 |
|
|
}
|
| 981 |
|
|
} else { /* else load value grid */
|
| 982 |
|
|
int bsiz;
|
| 983 |
|
|
n = count_values(*spp); /* see how big the grid is */
|
| 984 |
greg |
3.15 |
for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd)
|
| 985 |
greg |
3.7 |
if (1<<bsiz == n)
|
| 986 |
|
|
break;
|
| 987 |
|
|
if (bsiz >= 8*sizeof(size_t)) {
|
| 988 |
|
|
strcpy(SDerrorDetail, "Illegal value count in tensor tree");
|
| 989 |
|
|
return NULL;
|
| 990 |
|
|
}
|
| 991 |
|
|
st = SDnewNode(nd, bsiz/nd);
|
| 992 |
|
|
if (st == NULL)
|
| 993 |
|
|
return NULL;
|
| 994 |
|
|
if (load_values(spp, st->u.v, n) != n) {
|
| 995 |
|
|
strcpy(SDerrorDetail, "Real format error in tensor tree");
|
| 996 |
|
|
SDfreeTre(st);
|
| 997 |
|
|
return NULL;
|
| 998 |
|
|
}
|
| 999 |
|
|
}
|
| 1000 |
greg |
3.9 |
if (!eat_token(spp, '}')) {
|
| 1001 |
greg |
3.7 |
strcpy(SDerrorDetail, "Missing '}' in tensor tree");
|
| 1002 |
|
|
SDfreeTre(st);
|
| 1003 |
|
|
return NULL;
|
| 1004 |
|
|
}
|
| 1005 |
greg |
3.9 |
eat_token(spp, ',');
|
| 1006 |
greg |
3.7 |
return st;
|
| 1007 |
|
|
}
|
| 1008 |
|
|
|
| 1009 |
|
|
/* Compute min. proj. solid angle and max. direct hemispherical scattering */
|
| 1010 |
|
|
static SDError
|
| 1011 |
|
|
get_extrema(SDSpectralDF *df)
|
| 1012 |
|
|
{
|
| 1013 |
greg |
3.37 |
SDNode *st = (*(SDTre *)df->comp[0].dist).stc[tt_Y];
|
| 1014 |
greg |
3.7 |
double stepWidth, dhemi, bmin[4], bmax[4];
|
| 1015 |
|
|
|
| 1016 |
|
|
stepWidth = SDsmallestLeaf(st);
|
| 1017 |
greg |
3.22 |
if (quantum > stepWidth) /* adjust quantization factor */
|
| 1018 |
|
|
quantum = stepWidth;
|
| 1019 |
greg |
3.7 |
df->minProjSA = M_PI*stepWidth*stepWidth;
|
| 1020 |
|
|
if (stepWidth < .03125)
|
| 1021 |
|
|
stepWidth = .03125; /* 1/32 resolution good enough */
|
| 1022 |
|
|
df->maxHemi = .0;
|
| 1023 |
|
|
if (st->ndim == 3) { /* isotropic BSDF */
|
| 1024 |
|
|
bmin[1] = bmin[2] = .0;
|
| 1025 |
|
|
bmax[1] = bmax[2] = 1.;
|
| 1026 |
|
|
for (bmin[0] = .0; bmin[0] < .5-FTINY; bmin[0] += stepWidth) {
|
| 1027 |
|
|
bmax[0] = bmin[0] + stepWidth;
|
| 1028 |
|
|
dhemi = SDavgTreBox(st, bmin, bmax);
|
| 1029 |
|
|
if (dhemi > df->maxHemi)
|
| 1030 |
|
|
df->maxHemi = dhemi;
|
| 1031 |
|
|
}
|
| 1032 |
|
|
} else if (st->ndim == 4) { /* anisotropic BSDF */
|
| 1033 |
|
|
bmin[2] = bmin[3] = .0;
|
| 1034 |
|
|
bmax[2] = bmax[3] = 1.;
|
| 1035 |
|
|
for (bmin[0] = .0; bmin[0] < 1.-FTINY; bmin[0] += stepWidth) {
|
| 1036 |
|
|
bmax[0] = bmin[0] + stepWidth;
|
| 1037 |
|
|
for (bmin[1] = .0; bmin[1] < 1.-FTINY; bmin[1] += stepWidth) {
|
| 1038 |
|
|
bmax[1] = bmin[1] + stepWidth;
|
| 1039 |
|
|
dhemi = SDavgTreBox(st, bmin, bmax);
|
| 1040 |
|
|
if (dhemi > df->maxHemi)
|
| 1041 |
|
|
df->maxHemi = dhemi;
|
| 1042 |
|
|
}
|
| 1043 |
|
|
}
|
| 1044 |
|
|
} else
|
| 1045 |
|
|
return SDEinternal;
|
| 1046 |
|
|
/* correct hemispherical value */
|
| 1047 |
|
|
df->maxHemi *= M_PI;
|
| 1048 |
|
|
return SDEnone;
|
| 1049 |
|
|
}
|
| 1050 |
|
|
|
| 1051 |
|
|
/* Load BSDF distribution for this wavelength */
|
| 1052 |
|
|
static SDError
|
| 1053 |
greg |
3.37 |
load_bsdf_data(SDData *sd, ezxml_t wdb, int ct, int ndim)
|
| 1054 |
greg |
3.7 |
{
|
| 1055 |
|
|
SDSpectralDF *df;
|
| 1056 |
|
|
SDTre *sdt;
|
| 1057 |
|
|
char *sdata;
|
| 1058 |
|
|
/* allocate BSDF component */
|
| 1059 |
|
|
sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
|
| 1060 |
|
|
if (!sdata)
|
| 1061 |
|
|
return SDEnone;
|
| 1062 |
|
|
/*
|
| 1063 |
|
|
* Remember that front and back are reversed from WINDOW 6 orientations
|
| 1064 |
|
|
*/
|
| 1065 |
greg |
3.24 |
if (!strcasecmp(sdata, "Transmission Front")) {
|
| 1066 |
greg |
3.37 |
if (sd->tb == NULL && (sd->tb = SDnewSpectralDF(1)) == NULL)
|
| 1067 |
greg |
3.25 |
return SDEmemory;
|
| 1068 |
|
|
df = sd->tb;
|
| 1069 |
|
|
} else if (!strcasecmp(sdata, "Transmission Back")) {
|
| 1070 |
greg |
3.37 |
if (sd->tf == NULL && (sd->tf = SDnewSpectralDF(1)) == NULL)
|
| 1071 |
greg |
3.7 |
return SDEmemory;
|
| 1072 |
|
|
df = sd->tf;
|
| 1073 |
|
|
} else if (!strcasecmp(sdata, "Reflection Front")) {
|
| 1074 |
greg |
3.37 |
if (sd->rb == NULL && (sd->rb = SDnewSpectralDF(1)) == NULL)
|
| 1075 |
greg |
3.7 |
return SDEmemory;
|
| 1076 |
|
|
df = sd->rb;
|
| 1077 |
|
|
} else if (!strcasecmp(sdata, "Reflection Back")) {
|
| 1078 |
greg |
3.37 |
if (sd->rf == NULL && (sd->rf = SDnewSpectralDF(1)) == NULL)
|
| 1079 |
greg |
3.7 |
return SDEmemory;
|
| 1080 |
|
|
df = sd->rf;
|
| 1081 |
|
|
} else
|
| 1082 |
|
|
return SDEnone;
|
| 1083 |
|
|
/* get angle bases */
|
| 1084 |
|
|
sdata = ezxml_txt(ezxml_child(wdb,"AngleBasis"));
|
| 1085 |
|
|
if (!sdata || strcasecmp(sdata, "LBNL/Shirley-Chiu")) {
|
| 1086 |
|
|
sprintf(SDerrorDetail, "%s angle basis for BSDF '%s'",
|
| 1087 |
|
|
!sdata ? "Missing" : "Unsupported", sd->name);
|
| 1088 |
|
|
return !sdata ? SDEformat : SDEsupport;
|
| 1089 |
|
|
}
|
| 1090 |
greg |
3.37 |
if (df->comp[0].dist == NULL) { /* need to allocate BSDF tree? */
|
| 1091 |
|
|
sdt = (SDTre *)malloc(sizeof(SDTre));
|
| 1092 |
|
|
if (sdt == NULL)
|
| 1093 |
|
|
return SDEmemory;
|
| 1094 |
|
|
if (df == sd->rf)
|
| 1095 |
|
|
sdt->sidef = SD_FREFL;
|
| 1096 |
|
|
else if (df == sd->rb)
|
| 1097 |
|
|
sdt->sidef = SD_BREFL;
|
| 1098 |
|
|
else if (df == sd->tf)
|
| 1099 |
|
|
sdt->sidef = SD_FXMIT;
|
| 1100 |
|
|
else /* df == sd->tb */
|
| 1101 |
|
|
sdt->sidef = SD_BXMIT;
|
| 1102 |
|
|
sdt->stc[tt_Y] = sdt->stc[tt_u] = sdt->stc[tt_v] = NULL;
|
| 1103 |
|
|
df->comp[0].dist = sdt;
|
| 1104 |
|
|
df->comp[0].func = &SDhandleTre;
|
| 1105 |
|
|
} else {
|
| 1106 |
|
|
sdt = (SDTre *)df->comp[0].dist;
|
| 1107 |
|
|
if (sdt->stc[ct] != NULL) {
|
| 1108 |
|
|
SDfreeTre(sdt->stc[ct]);
|
| 1109 |
|
|
sdt->stc[ct] = NULL;
|
| 1110 |
|
|
}
|
| 1111 |
|
|
}
|
| 1112 |
greg |
3.7 |
/* read BSDF data */
|
| 1113 |
|
|
sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
|
| 1114 |
|
|
if (!sdata || !next_token(&sdata)) {
|
| 1115 |
|
|
sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
|
| 1116 |
|
|
sd->name);
|
| 1117 |
|
|
return SDEformat;
|
| 1118 |
|
|
}
|
| 1119 |
greg |
3.37 |
sdt->stc[ct] = load_tree_data(&sdata, ndim);
|
| 1120 |
|
|
if (sdt->stc[ct] == NULL)
|
| 1121 |
greg |
3.7 |
return SDEformat;
|
| 1122 |
|
|
if (next_token(&sdata)) { /* check for unconsumed characters */
|
| 1123 |
|
|
sprintf(SDerrorDetail,
|
| 1124 |
|
|
"Extra characters at end of ScatteringData in '%s'",
|
| 1125 |
|
|
sd->name);
|
| 1126 |
|
|
return SDEformat;
|
| 1127 |
|
|
}
|
| 1128 |
|
|
/* flatten branches where possible */
|
| 1129 |
greg |
3.37 |
sdt->stc[ct] = SDsimplifyTre(sdt->stc[ct]);
|
| 1130 |
|
|
if (sdt->stc[ct] == NULL)
|
| 1131 |
greg |
3.7 |
return SDEinternal;
|
| 1132 |
greg |
3.37 |
/* compute global quantities for Y */
|
| 1133 |
|
|
return (ct == tt_Y) ? get_extrema(df) : SDEnone;
|
| 1134 |
greg |
3.7 |
}
|
| 1135 |
|
|
|
| 1136 |
|
|
/* Find minimum value in tree */
|
| 1137 |
|
|
static float
|
| 1138 |
|
|
SDgetTreMin(const SDNode *st)
|
| 1139 |
|
|
{
|
| 1140 |
greg |
3.10 |
float vmin = FHUGE;
|
| 1141 |
greg |
3.7 |
int n;
|
| 1142 |
|
|
|
| 1143 |
|
|
if (st->log2GR < 0) {
|
| 1144 |
|
|
for (n = 1<<st->ndim; n--; ) {
|
| 1145 |
|
|
float v = SDgetTreMin(st->u.t[n]);
|
| 1146 |
|
|
if (v < vmin)
|
| 1147 |
|
|
vmin = v;
|
| 1148 |
|
|
}
|
| 1149 |
|
|
} else {
|
| 1150 |
|
|
for (n = 1<<(st->ndim*st->log2GR); n--; )
|
| 1151 |
|
|
if (st->u.v[n] < vmin)
|
| 1152 |
|
|
vmin = st->u.v[n];
|
| 1153 |
|
|
}
|
| 1154 |
|
|
return vmin;
|
| 1155 |
|
|
}
|
| 1156 |
|
|
|
| 1157 |
|
|
/* Subtract the given value from all tree nodes */
|
| 1158 |
|
|
static void
|
| 1159 |
|
|
SDsubtractTreVal(SDNode *st, float val)
|
| 1160 |
|
|
{
|
| 1161 |
|
|
int n;
|
| 1162 |
|
|
|
| 1163 |
|
|
if (st->log2GR < 0) {
|
| 1164 |
|
|
for (n = 1<<st->ndim; n--; )
|
| 1165 |
|
|
SDsubtractTreVal(st->u.t[n], val);
|
| 1166 |
|
|
} else {
|
| 1167 |
|
|
for (n = 1<<(st->ndim*st->log2GR); n--; )
|
| 1168 |
greg |
3.15 |
if ((st->u.v[n] -= val) < 0)
|
| 1169 |
|
|
st->u.v[n] = .0f;
|
| 1170 |
greg |
3.7 |
}
|
| 1171 |
|
|
}
|
| 1172 |
|
|
|
| 1173 |
greg |
3.37 |
/* Subtract minimum Y value from BSDF */
|
| 1174 |
greg |
3.7 |
static double
|
| 1175 |
greg |
3.37 |
subtract_min_Y(SDNode *st)
|
| 1176 |
greg |
3.7 |
{
|
| 1177 |
|
|
float vmin;
|
| 1178 |
|
|
/* be sure to skip unused portion */
|
| 1179 |
greg |
3.10 |
if (st->ndim == 3) {
|
| 1180 |
|
|
int n;
|
| 1181 |
greg |
3.7 |
vmin = 1./M_PI;
|
| 1182 |
greg |
3.10 |
if (st->log2GR < 0) {
|
| 1183 |
greg |
3.15 |
for (n = 0; n < 8; n += 2) {
|
| 1184 |
greg |
3.10 |
float v = SDgetTreMin(st->u.t[n]);
|
| 1185 |
|
|
if (v < vmin)
|
| 1186 |
|
|
vmin = v;
|
| 1187 |
|
|
}
|
| 1188 |
|
|
} else if (st->log2GR) {
|
| 1189 |
|
|
for (n = 1 << (3*st->log2GR - 1); n--; )
|
| 1190 |
|
|
if (st->u.v[n] < vmin)
|
| 1191 |
|
|
vmin = st->u.v[n];
|
| 1192 |
|
|
} else
|
| 1193 |
|
|
vmin = st->u.v[0];
|
| 1194 |
greg |
3.7 |
} else /* anisotropic covers entire tree */
|
| 1195 |
|
|
vmin = SDgetTreMin(st);
|
| 1196 |
|
|
|
| 1197 |
greg |
3.39 |
if (vmin <= .01/M_PI)
|
| 1198 |
|
|
return .0; /* not worth bothering about */
|
| 1199 |
greg |
3.7 |
|
| 1200 |
greg |
3.8 |
SDsubtractTreVal(st, vmin);
|
| 1201 |
greg |
3.7 |
|
| 1202 |
|
|
return M_PI * vmin; /* return hemispherical value */
|
| 1203 |
|
|
}
|
| 1204 |
|
|
|
| 1205 |
greg |
3.37 |
/* Struct used in callback to find RGB extrema */
|
| 1206 |
|
|
typedef struct {
|
| 1207 |
|
|
SDNode **stc; /* original Y, u' & v' trees */
|
| 1208 |
|
|
float rgb[3]; /* RGB value */
|
| 1209 |
|
|
SDNode *new_stu, *new_stv; /* replacement u' & v' trees */
|
| 1210 |
|
|
} SDextRGBs;
|
| 1211 |
|
|
|
| 1212 |
|
|
/* Callback to find minimum RGB from Y value plus CIE (u',v') trees */
|
| 1213 |
|
|
static int
|
| 1214 |
|
|
get_min_RGB(float yval, const double *cmin, double csiz, void *cptr)
|
| 1215 |
|
|
{
|
| 1216 |
|
|
SDextRGBs *mp = (SDextRGBs *)cptr;
|
| 1217 |
|
|
double cmax[SD_MAXDIM];
|
| 1218 |
|
|
float rgb[3];
|
| 1219 |
|
|
|
| 1220 |
|
|
if (mp->stc[tt_Y]->ndim == 3) {
|
| 1221 |
|
|
if (cmin[0] + .5*csiz >= .5)
|
| 1222 |
|
|
return 0; /* ignore dead half of isotropic */
|
| 1223 |
|
|
} else
|
| 1224 |
|
|
cmax[3] = cmin[3] + csiz;
|
| 1225 |
|
|
cmax[0] = cmin[0] + csiz;
|
| 1226 |
|
|
cmax[1] = cmin[1] + csiz;
|
| 1227 |
|
|
cmax[2] = cmin[2] + csiz;
|
| 1228 |
|
|
/* average RGB color over voxel */
|
| 1229 |
|
|
SDyuv2rgb(yval, SDavgTreBox(mp->stc[tt_u], cmin, cmax),
|
| 1230 |
|
|
SDavgTreBox(mp->stc[tt_v], cmin, cmax), rgb);
|
| 1231 |
|
|
/* track smallest components */
|
| 1232 |
|
|
if (rgb[0] < mp->rgb[0]) mp->rgb[0] = rgb[0];
|
| 1233 |
|
|
if (rgb[1] < mp->rgb[1]) mp->rgb[1] = rgb[1];
|
| 1234 |
|
|
if (rgb[2] < mp->rgb[2]) mp->rgb[2] = rgb[2];
|
| 1235 |
|
|
return 0;
|
| 1236 |
|
|
}
|
| 1237 |
|
|
|
| 1238 |
|
|
/* Callback to build adjusted u' tree */
|
| 1239 |
|
|
static int
|
| 1240 |
|
|
adjust_utree(float uprime, const double *cmin, double csiz, void *cptr)
|
| 1241 |
|
|
{
|
| 1242 |
|
|
SDextRGBs *mp = (SDextRGBs *)cptr;
|
| 1243 |
|
|
double cmax[SD_MAXDIM];
|
| 1244 |
greg |
3.38 |
double yval;
|
| 1245 |
greg |
3.37 |
float rgb[3];
|
| 1246 |
greg |
3.38 |
C_COLOR clr;
|
| 1247 |
greg |
3.37 |
|
| 1248 |
|
|
if (mp->stc[tt_Y]->ndim == 3) {
|
| 1249 |
|
|
if (cmin[0] + .5*csiz >= .5)
|
| 1250 |
|
|
return 0; /* ignore dead half of isotropic */
|
| 1251 |
|
|
} else
|
| 1252 |
|
|
cmax[3] = cmin[3] + csiz;
|
| 1253 |
|
|
cmax[0] = cmin[0] + csiz;
|
| 1254 |
|
|
cmax[1] = cmin[1] + csiz;
|
| 1255 |
|
|
cmax[2] = cmin[2] + csiz;
|
| 1256 |
|
|
/* average RGB color over voxel */
|
| 1257 |
greg |
3.38 |
SDyuv2rgb(yval=SDavgTreBox(mp->stc[tt_Y], cmin, cmax), uprime,
|
| 1258 |
greg |
3.37 |
SDavgTreBox(mp->stc[tt_v], cmin, cmax), rgb);
|
| 1259 |
greg |
3.38 |
/* subtract minimum (& clamp) */
|
| 1260 |
|
|
if ((rgb[0] -= mp->rgb[0]) < 1e-5*yval) rgb[0] = 1e-5*yval;
|
| 1261 |
|
|
if ((rgb[1] -= mp->rgb[1]) < 1e-5*yval) rgb[1] = 1e-5*yval;
|
| 1262 |
|
|
if ((rgb[2] -= mp->rgb[2]) < 1e-5*yval) rgb[2] = 1e-5*yval;
|
| 1263 |
|
|
c_fromSharpRGB(rgb, &clr); /* compute new u' for adj. RGB */
|
| 1264 |
|
|
uprime = 4.*clr.cx/(-2.*clr.cx + 12.*clr.cy + 3.);
|
| 1265 |
greg |
3.37 |
/* assign in new u' tree */
|
| 1266 |
|
|
mp->new_stu = SDsetVoxel(mp->new_stu, mp->stc[tt_Y]->ndim,
|
| 1267 |
|
|
cmin, csiz, uprime);
|
| 1268 |
|
|
return -(mp->new_stu == NULL);
|
| 1269 |
|
|
}
|
| 1270 |
|
|
|
| 1271 |
|
|
/* Callback to build adjusted v' tree */
|
| 1272 |
|
|
static int
|
| 1273 |
|
|
adjust_vtree(float vprime, const double *cmin, double csiz, void *cptr)
|
| 1274 |
|
|
{
|
| 1275 |
|
|
SDextRGBs *mp = (SDextRGBs *)cptr;
|
| 1276 |
|
|
double cmax[SD_MAXDIM];
|
| 1277 |
greg |
3.38 |
double yval;
|
| 1278 |
greg |
3.37 |
float rgb[3];
|
| 1279 |
greg |
3.38 |
C_COLOR clr;
|
| 1280 |
greg |
3.37 |
|
| 1281 |
|
|
if (mp->stc[tt_Y]->ndim == 3) {
|
| 1282 |
|
|
if (cmin[0] + .5*csiz >= .5)
|
| 1283 |
|
|
return 0; /* ignore dead half of isotropic */
|
| 1284 |
|
|
} else
|
| 1285 |
|
|
cmax[3] = cmin[3] + csiz;
|
| 1286 |
|
|
cmax[0] = cmin[0] + csiz;
|
| 1287 |
|
|
cmax[1] = cmin[1] + csiz;
|
| 1288 |
|
|
cmax[2] = cmin[2] + csiz;
|
| 1289 |
|
|
/* average RGB color over voxel */
|
| 1290 |
greg |
3.38 |
SDyuv2rgb(yval=SDavgTreBox(mp->stc[tt_Y], cmin, cmax),
|
| 1291 |
greg |
3.37 |
SDavgTreBox(mp->stc[tt_u], cmin, cmax),
|
| 1292 |
|
|
vprime, rgb);
|
| 1293 |
greg |
3.38 |
/* subtract minimum (& clamp) */
|
| 1294 |
|
|
if ((rgb[0] -= mp->rgb[0]) < 1e-5*yval) rgb[0] = 1e-5*yval;
|
| 1295 |
|
|
if ((rgb[1] -= mp->rgb[1]) < 1e-5*yval) rgb[1] = 1e-5*yval;
|
| 1296 |
|
|
if ((rgb[2] -= mp->rgb[2]) < 1e-5*yval) rgb[2] = 1e-5*yval;
|
| 1297 |
|
|
c_fromSharpRGB(rgb, &clr); /* compute new v' for adj. RGB */
|
| 1298 |
|
|
vprime = 9.*clr.cy/(-2.*clr.cx + 12.*clr.cy + 3.);
|
| 1299 |
greg |
3.37 |
/* assign in new v' tree */
|
| 1300 |
|
|
mp->new_stv = SDsetVoxel(mp->new_stv, mp->stc[tt_Y]->ndim,
|
| 1301 |
|
|
cmin, csiz, vprime);
|
| 1302 |
|
|
return -(mp->new_stv == NULL);
|
| 1303 |
|
|
}
|
| 1304 |
|
|
|
| 1305 |
|
|
/* Subtract minimum (diffuse) color and return luminance & CIE (x,y) */
|
| 1306 |
|
|
static double
|
| 1307 |
|
|
subtract_min_RGB(C_COLOR *cs, SDNode *stc[])
|
| 1308 |
|
|
{
|
| 1309 |
|
|
SDextRGBs my_min;
|
| 1310 |
|
|
double ymin;
|
| 1311 |
|
|
|
| 1312 |
|
|
my_min.stc = stc;
|
| 1313 |
|
|
my_min.rgb[0] = my_min.rgb[1] = my_min.rgb[2] = FHUGE;
|
| 1314 |
|
|
my_min.new_stu = my_min.new_stv = NULL;
|
| 1315 |
|
|
/* get minimum RGB value */
|
| 1316 |
|
|
SDtraverseTre(stc[tt_Y], NULL, 0, get_min_RGB, &my_min);
|
| 1317 |
greg |
3.40 |
/* convert to C_COLOR */
|
| 1318 |
|
|
ymin = c_fromSharpRGB(my_min.rgb, cs);
|
| 1319 |
|
|
if (ymin <= .01/M_PI) /* not worth bothering about? */
|
| 1320 |
|
|
return .0;
|
| 1321 |
|
|
/* adjust u' & v' trees */
|
| 1322 |
greg |
3.37 |
SDtraverseTre(stc[tt_u], NULL, 0, adjust_utree, &my_min);
|
| 1323 |
|
|
SDtraverseTre(stc[tt_v], NULL, 0, adjust_vtree, &my_min);
|
| 1324 |
|
|
SDfreeTre(stc[tt_u]); SDfreeTre(stc[tt_v]);
|
| 1325 |
|
|
stc[tt_u] = SDsimplifyTre(my_min.new_stu);
|
| 1326 |
|
|
stc[tt_v] = SDsimplifyTre(my_min.new_stv);
|
| 1327 |
greg |
3.40 |
/* subtract Y & return hemispherical */
|
| 1328 |
greg |
3.37 |
SDsubtractTreVal(stc[tt_Y], ymin);
|
| 1329 |
greg |
3.40 |
|
| 1330 |
|
|
return M_PI * ymin;
|
| 1331 |
greg |
3.37 |
}
|
| 1332 |
|
|
|
| 1333 |
greg |
3.7 |
/* Extract and separate diffuse portion of BSDF */
|
| 1334 |
|
|
static void
|
| 1335 |
|
|
extract_diffuse(SDValue *dv, SDSpectralDF *df)
|
| 1336 |
|
|
{
|
| 1337 |
|
|
int n;
|
| 1338 |
greg |
3.37 |
SDTre *sdt;
|
| 1339 |
greg |
3.7 |
|
| 1340 |
|
|
if (df == NULL || df->ncomp <= 0) {
|
| 1341 |
|
|
dv->spec = c_dfcolor;
|
| 1342 |
|
|
dv->cieY = .0;
|
| 1343 |
|
|
return;
|
| 1344 |
|
|
}
|
| 1345 |
greg |
3.37 |
sdt = (SDTre *)df->comp[0].dist;
|
| 1346 |
|
|
/* subtract minimum color/grayscale */
|
| 1347 |
|
|
if (sdt->stc[tt_u] != NULL && sdt->stc[tt_v] != NULL) {
|
| 1348 |
|
|
int i = 3*(tt_RGB_coef[1] < .001);
|
| 1349 |
|
|
while (i--) { /* initialize on first call */
|
| 1350 |
|
|
float rgb[3];
|
| 1351 |
|
|
rgb[0] = rgb[1] = rgb[2] = .0f; rgb[i] = 1.f;
|
| 1352 |
|
|
tt_RGB_coef[i] = c_fromSharpRGB(rgb, &tt_RGB_prim[i]);
|
| 1353 |
|
|
}
|
| 1354 |
|
|
memcpy(df->comp[0].cspec, tt_RGB_prim, sizeof(tt_RGB_prim));
|
| 1355 |
|
|
dv->cieY = subtract_min_RGB(&dv->spec, sdt->stc);
|
| 1356 |
|
|
} else {
|
| 1357 |
|
|
df->comp[0].cspec[0] = c_dfcolor;
|
| 1358 |
|
|
dv->cieY = subtract_min_Y(sdt->stc[tt_Y]);
|
| 1359 |
greg |
3.7 |
}
|
| 1360 |
|
|
df->maxHemi -= dv->cieY; /* adjust maximum hemispherical */
|
| 1361 |
|
|
/* make sure everything is set */
|
| 1362 |
|
|
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
|
| 1363 |
|
|
}
|
| 1364 |
|
|
|
| 1365 |
greg |
3.1 |
/* Load a variable-resolution BSDF tree from an open XML file */
|
| 1366 |
|
|
SDError
|
| 1367 |
greg |
3.4 |
SDloadTre(SDData *sd, ezxml_t wtl)
|
| 1368 |
greg |
3.1 |
{
|
| 1369 |
greg |
3.7 |
SDError ec;
|
| 1370 |
|
|
ezxml_t wld, wdb;
|
| 1371 |
|
|
int rank;
|
| 1372 |
|
|
char *txt;
|
| 1373 |
|
|
/* basic checks and tensor rank */
|
| 1374 |
|
|
txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
|
| 1375 |
|
|
"DataDefinition"), "IncidentDataStructure"));
|
| 1376 |
|
|
if (txt == NULL || !*txt) {
|
| 1377 |
|
|
sprintf(SDerrorDetail,
|
| 1378 |
|
|
"BSDF \"%s\": missing IncidentDataStructure",
|
| 1379 |
|
|
sd->name);
|
| 1380 |
|
|
return SDEformat;
|
| 1381 |
|
|
}
|
| 1382 |
|
|
if (!strcasecmp(txt, "TensorTree3"))
|
| 1383 |
|
|
rank = 3;
|
| 1384 |
|
|
else if (!strcasecmp(txt, "TensorTree4"))
|
| 1385 |
|
|
rank = 4;
|
| 1386 |
|
|
else {
|
| 1387 |
|
|
sprintf(SDerrorDetail,
|
| 1388 |
|
|
"BSDF \"%s\": unsupported IncidentDataStructure",
|
| 1389 |
|
|
sd->name);
|
| 1390 |
|
|
return SDEsupport;
|
| 1391 |
|
|
}
|
| 1392 |
|
|
/* load BSDF components */
|
| 1393 |
|
|
for (wld = ezxml_child(wtl, "WavelengthData");
|
| 1394 |
|
|
wld != NULL; wld = wld->next) {
|
| 1395 |
greg |
3.37 |
const char *cnm = ezxml_txt(ezxml_child(wld,"Wavelength"));
|
| 1396 |
|
|
int ct = -1;
|
| 1397 |
|
|
if (!strcasecmp(cnm, "Visible"))
|
| 1398 |
|
|
ct = tt_Y;
|
| 1399 |
|
|
else if (!strcasecmp(cnm, "CIE-u"))
|
| 1400 |
|
|
ct = tt_u;
|
| 1401 |
|
|
else if (!strcasecmp(cnm, "CIE-v"))
|
| 1402 |
|
|
ct = tt_v;
|
| 1403 |
|
|
else
|
| 1404 |
|
|
continue;
|
| 1405 |
greg |
3.7 |
for (wdb = ezxml_child(wld, "WavelengthDataBlock");
|
| 1406 |
|
|
wdb != NULL; wdb = wdb->next)
|
| 1407 |
greg |
3.37 |
if ((ec = load_bsdf_data(sd, wdb, ct, rank)) != SDEnone)
|
| 1408 |
greg |
3.7 |
return ec;
|
| 1409 |
|
|
}
|
| 1410 |
|
|
/* separate diffuse components */
|
| 1411 |
|
|
extract_diffuse(&sd->rLambFront, sd->rf);
|
| 1412 |
|
|
extract_diffuse(&sd->rLambBack, sd->rb);
|
| 1413 |
greg |
3.27 |
if (sd->tf != NULL)
|
| 1414 |
|
|
extract_diffuse(&sd->tLamb, sd->tf);
|
| 1415 |
|
|
if (sd->tb != NULL)
|
| 1416 |
|
|
extract_diffuse(&sd->tLamb, sd->tb);
|
| 1417 |
greg |
3.7 |
/* return success */
|
| 1418 |
|
|
return SDEnone;
|
| 1419 |
greg |
3.1 |
}
|
| 1420 |
|
|
|
| 1421 |
|
|
/* Variable resolution BSDF methods */
|
| 1422 |
greg |
3.5 |
SDFunc SDhandleTre = {
|
| 1423 |
|
|
&SDgetTreBSDF,
|
| 1424 |
greg |
3.6 |
&SDqueryTreProjSA,
|
| 1425 |
|
|
&SDgetTreCDist,
|
| 1426 |
|
|
&SDsampTreCDist,
|
| 1427 |
|
|
&SDFreeBTre,
|
| 1428 |
greg |
3.1 |
};
|