11 |
|
#include "rterror.h" |
12 |
|
#include "platform.h" |
13 |
|
#include <stdlib.h> |
14 |
+ |
#include <math.h> |
15 |
|
|
16 |
|
float *datarr; /* our loaded BSDF data array */ |
17 |
|
int ttrank = 4; /* tensor tree rank */ |
18 |
|
int log2g = 4; /* log2 of grid resolution */ |
19 |
|
int infmt = 'a'; /* input format ('a','f','d') */ |
20 |
< |
double tthresh = .05; /* relative acceptance threshold */ |
20 |
> |
double pctcull = 95.; /* target culling percentile */ |
21 |
|
|
22 |
|
#define dval3(ix,ox,oy) datarr[((((ix)<<log2g)+(ox))<<log2g)+(oy)] |
23 |
|
#define dval4(ix,iy,ox,oy) datarr[((((((ix)<<log2g)+(iy))<<log2g)+(ox))<<log2g)+(oy)] |
24 |
|
|
24 |
– |
#define above_threshold(tp) ((tp)->vmax - (tp)->vmin > 2.*tthresh*(tp)->vavg) |
25 |
– |
|
25 |
|
/* Tensor tree node */ |
26 |
|
typedef struct ttree_s { |
27 |
|
float vmin, vmax; /* value extrema */ |
29 |
|
struct ttree_s *kid; /* 2^ttrank children */ |
30 |
|
} TNODE; |
31 |
|
|
32 |
+ |
#define HISTLEN 300 /* histogram resolution */ |
33 |
+ |
#define HISTMAX 10. /* maximum recorded measure in histogram */ |
34 |
+ |
|
35 |
+ |
int histo[HISTLEN]; /* histogram freq. of variance measure */ |
36 |
+ |
|
37 |
+ |
double tthresh; /* acceptance threshold (TBD) */ |
38 |
+ |
|
39 |
+ |
#define var_measure(tp) ( ((tp)->vmax - (tp)->vmin) / \ |
40 |
+ |
(sqrt((tp)->vavg) + .03) ) |
41 |
+ |
#define above_threshold(tp) (var_measure(tp) > tthresh) |
42 |
+ |
|
43 |
|
/* Allocate a new set of children for the given node (no checks) */ |
44 |
|
static void |
45 |
|
new_kids(TNODE *pn) |
87 |
|
tp->vavg += val; |
88 |
|
} |
89 |
|
tp->vavg /= (float)(1<<ttrank); |
90 |
+ |
/* record stats */ |
91 |
+ |
i = (HISTLEN/HISTMAX) * var_measure(tp); |
92 |
+ |
if (i >= HISTLEN) i = HISTLEN-1; |
93 |
+ |
++histo[i]; |
94 |
|
return; |
95 |
|
} |
96 |
|
--l2s; /* else still branching */ |
106 |
|
tp->vavg += tp->kid[i].vavg; |
107 |
|
} |
108 |
|
tp->vavg /= (float)(1<<ttrank); |
95 |
– |
/* is variation above threshold? */ |
96 |
– |
if (!above_threshold(tp)) |
97 |
– |
free_kids(tp); /* if not, trim branches */ |
109 |
|
} |
110 |
|
|
111 |
+ |
/* Set our trimming threshold */ |
112 |
+ |
static void |
113 |
+ |
set_threshold() |
114 |
+ |
{ |
115 |
+ |
int hsum = 0; |
116 |
+ |
int i; |
117 |
+ |
|
118 |
+ |
for (i = HISTLEN; i--; ) |
119 |
+ |
hsum += histo[i]; |
120 |
+ |
hsum = pctcull*.01 * (double)hsum; |
121 |
+ |
for (i = 0; hsum > 0; i++) |
122 |
+ |
hsum -= histo[i]; |
123 |
+ |
tthresh = (HISTMAX/HISTLEN) * i; |
124 |
+ |
} |
125 |
+ |
|
126 |
+ |
/* Trim our tree according to the current threshold */ |
127 |
+ |
static void |
128 |
+ |
trim_tree(TNODE *tp) |
129 |
+ |
{ |
130 |
+ |
if (tp->kid == NULL) |
131 |
+ |
return; |
132 |
+ |
if (above_threshold(tp)) { /* keeping branches? */ |
133 |
+ |
int i = 1<<ttrank; |
134 |
+ |
while (i--) |
135 |
+ |
trim_tree(tp->kid+i); |
136 |
+ |
return; |
137 |
+ |
} |
138 |
+ |
free_kids(tp); /* else trim at this point */ |
139 |
+ |
} |
140 |
+ |
|
141 |
|
/* Print a tensor tree from the given hypercube */ |
142 |
|
static void |
143 |
|
print_tree(const TNODE *tp, const int bmin[], int l2s) |
260 |
|
error(SYSTEM, "out of memory in load_data"); |
261 |
|
if (ttrank == 3) { |
262 |
|
int ix, ox; |
263 |
< |
for (ix = 0; ix < 1<<log2g; ix++) |
263 |
> |
for (ix = 0; ix < 1<<(log2g-1); ix++) |
264 |
|
for (ox = 0; ox < 1<<log2g; ox++) |
265 |
|
(*readf)(datarr+((((ix)<<log2g)+(ox))<<log2g), |
266 |
< |
1<<(log2g-1)); |
266 |
> |
1<<log2g); |
267 |
|
} else /* ttrank == 4 */ { |
268 |
|
int ix, iy, ox; |
269 |
|
for (ix = 0; ix < 1<<log2g; ix++) |
316 |
|
goto userr; |
317 |
|
break; |
318 |
|
case 't': |
319 |
< |
tthresh = atof(argv[++i]); |
320 |
< |
if (tthresh <= 0) |
319 |
> |
pctcull = atof(argv[++i]); |
320 |
> |
if ((pctcull < 0) | (pctcull >= 100.)) |
321 |
|
goto userr; |
322 |
|
break; |
323 |
|
case 'f': |
348 |
|
gtree.kid = NULL; /* create our tree */ |
349 |
|
bmin[0] = bmin[1] = bmin[2] = bmin[3] = 0; |
350 |
|
build_tree(>ree, bmin, log2g); |
351 |
+ |
/* compute threshold & trim tree */ |
352 |
+ |
set_threshold(); |
353 |
+ |
trim_tree(>ree); |
354 |
|
/* format to stdout */ |
355 |
|
print_tree(>ree, bmin, log2g); |
356 |
|
/* Clean up isn't necessary for main()... |
359 |
|
*/ |
360 |
|
return(0); |
361 |
|
userr: |
362 |
< |
fprintf(stderr, "Usage: %s [-h][-f{a|f|d}][-r {3|4}][-g log2grid][-t thresh] [input]\n", |
362 |
> |
fprintf(stderr, "Usage: %s [-h][-f{a|f|d}][-r {3|4}][-g log2grid][-t trim%%] [input]\n", |
363 |
|
argv[0]); |
364 |
|
return(1); |
365 |
|
} |