| 1 |
+ |
#ifndef lint |
| 2 |
+ |
static const char RCSid[] = "$Id$"; |
| 3 |
+ |
#endif |
| 4 |
|
/* |
| 5 |
|
* bsdf_t.c |
| 6 |
|
* |
| 10 |
|
* |
| 11 |
|
*/ |
| 12 |
|
|
| 13 |
< |
#include <stdio.h> |
| 13 |
> |
#include "rtio.h" |
| 14 |
|
#include <stdlib.h> |
| 15 |
+ |
#include <math.h> |
| 16 |
+ |
#include <ctype.h> |
| 17 |
|
#include "ezxml.h" |
| 18 |
|
#include "bsdf.h" |
| 19 |
|
#include "bsdf_t.h" |
| 37 |
|
st = (SDNode *)malloc(sizeof(SDNode) + |
| 38 |
|
((1<<nd) - 1)*sizeof(st->u.t[0])); |
| 39 |
|
if (st != NULL) |
| 40 |
< |
memset(st->u.t, 0, (1<<nd)*sizeof(st->u.t[0])); |
| 40 |
> |
memset(st->u.t, 0, sizeof(st->u.t[0])<<nd); |
| 41 |
|
} else |
| 42 |
|
st = (SDNode *)malloc(sizeof(SDNode) + |
| 43 |
|
((1 << nd*lg) - 1)*sizeof(st->u.v[0])); |
| 58 |
|
|
| 59 |
|
/* Free an SD tree */ |
| 60 |
|
static void |
| 61 |
< |
SDfreeTree(void *p) |
| 61 |
> |
SDfreeTre(void *p) |
| 62 |
|
{ |
| 63 |
|
SDNode *st = (SDNode *)p; |
| 64 |
|
int i; |
| 66 |
|
if (st == NULL) |
| 67 |
|
return; |
| 68 |
|
for (i = (st->log2GR < 0) << st->ndim; i--; ) |
| 69 |
< |
SDfreeTree(st->u.t[i]); |
| 69 |
> |
SDfreeTre(st->u.t[i]); |
| 70 |
|
free((void *)st); |
| 71 |
|
} |
| 72 |
|
|
| 73 |
+ |
#if 0 /* Unused and untested routines */ |
| 74 |
+ |
|
| 75 |
|
/* Add up N-dimensional hypercube array values over the given box */ |
| 76 |
|
static double |
| 77 |
|
SDiterSum(const float *va, int nd, int siz, const int *imin, const int *imax) |
| 150 |
|
(double)n; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
+ |
#endif /* 0 */ |
| 154 |
+ |
|
| 155 |
+ |
/* Look up tree value at the given grid position */ |
| 156 |
+ |
static float |
| 157 |
+ |
SDlookupTre(const SDNode *st, const double *pos) |
| 158 |
+ |
{ |
| 159 |
+ |
double spos[SD_MAXDIM]; |
| 160 |
+ |
int i, n, t; |
| 161 |
+ |
/* climb the tree */ |
| 162 |
+ |
while (st->log2GR < 0) { |
| 163 |
+ |
n = 0; /* move to appropriate branch */ |
| 164 |
+ |
for (i = st->ndim; i--; ) { |
| 165 |
+ |
spos[i] = 2.*pos[i]; |
| 166 |
+ |
t = (spos[i] >= 1.); |
| 167 |
+ |
n |= t<<i; |
| 168 |
+ |
spos[i] -= (double)t; |
| 169 |
+ |
} |
| 170 |
+ |
st = st->u.t[n]; /* avoids tail recursion */ |
| 171 |
+ |
pos = spos; |
| 172 |
+ |
} |
| 173 |
+ |
n = t = 0; /* find grid array index */ |
| 174 |
+ |
for (i = st->ndim; i--; ) { |
| 175 |
+ |
n += (int)((1<<st->log2GR)*pos[i]) << t; |
| 176 |
+ |
t += st->log2GR; |
| 177 |
+ |
} |
| 178 |
+ |
return st->u.v[n]; /* XXX no interpolation */ |
| 179 |
+ |
} |
| 180 |
+ |
|
| 181 |
+ |
/* Compute non-diffuse component for variable-resolution BSDF */ |
| 182 |
+ |
static int |
| 183 |
+ |
SDgetTreBSDF(float coef[SDmaxCh], const FVECT outVec, |
| 184 |
+ |
const FVECT inVec, const void *dist) |
| 185 |
+ |
{ |
| 186 |
+ |
const SDNode *st = (const SDNode *)dist; |
| 187 |
+ |
double gridPos[4]; |
| 188 |
+ |
/* convert vector coordinates */ |
| 189 |
+ |
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 */ |
| 200 |
+ |
/* get nearest BSDF value */ |
| 201 |
+ |
coef[0] = SDlookupTre(st, gridPos); |
| 202 |
+ |
return 1; /* monochromatic for now */ |
| 203 |
+ |
} |
| 204 |
+ |
|
| 205 |
|
/* Load a variable-resolution BSDF tree from an open XML file */ |
| 206 |
|
SDError |
| 207 |
< |
SDloadTre(SDData *sd, ezxml_t fl) |
| 207 |
> |
SDloadTre(SDData *sd, ezxml_t wtl) |
| 208 |
|
{ |
| 209 |
|
return SDEsupport; |
| 210 |
|
} |
| 211 |
|
|
| 212 |
|
/* Variable resolution BSDF methods */ |
| 213 |
< |
const SDFunc SDhandleTre = { |
| 213 |
> |
SDFunc SDhandleTre = { |
| 214 |
> |
&SDgetTreBSDF, |
| 215 |
|
NULL, |
| 216 |
|
NULL, |
| 217 |
|
NULL, |
| 218 |
< |
NULL, |
| 159 |
< |
&SDfreeTree, |
| 218 |
> |
&SDfreeTre, |
| 219 |
|
}; |