16 |
|
int ttrank = 4; /* tensor tree rank */ |
17 |
|
int log2g = 4; /* log2 of grid resolution */ |
18 |
|
int infmt = 'a'; /* input format ('a','f','d') */ |
19 |
< |
double tthresh = .05; /* relative acceptance threshold */ |
19 |
> |
double pctcull = 99.; /* target culling percentile */ |
20 |
|
|
21 |
+ |
#define HISTLEN 300 /* histogram resolution */ |
22 |
+ |
#define HISTMAX 10. /* maximum recorded value in histogram */ |
23 |
+ |
|
24 |
+ |
int histo[HISTLEN]; /* histogram freq. of max-min BSDF */ |
25 |
+ |
|
26 |
+ |
double tthresh; /* acceptance threshold (TBD) */ |
27 |
+ |
|
28 |
|
#define dval3(ix,ox,oy) datarr[((((ix)<<log2g)+(ox))<<log2g)+(oy)] |
29 |
|
#define dval4(ix,iy,ox,oy) datarr[((((((ix)<<log2g)+(iy))<<log2g)+(ox))<<log2g)+(oy)] |
30 |
|
|
31 |
< |
#define above_threshold(tp) ((tp)->vmax - (tp)->vmin > 2.*tthresh*(tp)->vavg) |
31 |
> |
#define above_threshold(tp) ((tp)->vmax - (tp)->vmin > tthresh) |
32 |
|
|
33 |
|
/* Tensor tree node */ |
34 |
|
typedef struct ttree_s { |
84 |
|
tp->vavg += val; |
85 |
|
} |
86 |
|
tp->vavg /= (float)(1<<ttrank); |
87 |
+ |
/* record stats */ |
88 |
+ |
i = (HISTLEN/HISTMAX) * (tp->vmax - tp->vmin); |
89 |
+ |
if (i >= HISTLEN) i = HISTLEN-1; |
90 |
+ |
++histo[i]; |
91 |
|
return; |
92 |
|
} |
93 |
|
--l2s; /* else still branching */ |
103 |
|
tp->vavg += tp->kid[i].vavg; |
104 |
|
} |
105 |
|
tp->vavg /= (float)(1<<ttrank); |
95 |
– |
/* is variation above threshold? */ |
96 |
– |
if (!above_threshold(tp)) |
97 |
– |
free_kids(tp); /* if not, trim branches */ |
106 |
|
} |
107 |
|
|
108 |
+ |
/* Set our trimming threshold */ |
109 |
+ |
static void |
110 |
+ |
set_threshold() |
111 |
+ |
{ |
112 |
+ |
int hsum = 0; |
113 |
+ |
int i; |
114 |
+ |
|
115 |
+ |
for (i = HISTLEN; i--; ) |
116 |
+ |
hsum += histo[i]; |
117 |
+ |
hsum = pctcull*.01 * (double)hsum; |
118 |
+ |
for (i = 0; hsum > 0; i++) |
119 |
+ |
hsum -= histo[i]; |
120 |
+ |
tthresh = (HISTMAX/HISTLEN) * i; |
121 |
+ |
} |
122 |
+ |
|
123 |
+ |
/* Trim our tree according to the current threshold */ |
124 |
+ |
static void |
125 |
+ |
trim_tree(TNODE *tp) |
126 |
+ |
{ |
127 |
+ |
if (tp->kid == NULL) |
128 |
+ |
return; |
129 |
+ |
if (above_threshold(tp)) { /* keeping branches? */ |
130 |
+ |
int i = 1<<ttrank; |
131 |
+ |
while (i--) |
132 |
+ |
trim_tree(tp->kid+i); |
133 |
+ |
return; |
134 |
+ |
} |
135 |
+ |
free_kids(tp); /* else trim at this point */ |
136 |
+ |
} |
137 |
+ |
|
138 |
|
/* Print a tensor tree from the given hypercube */ |
139 |
|
static void |
140 |
|
print_tree(const TNODE *tp, const int bmin[], int l2s) |
313 |
|
goto userr; |
314 |
|
break; |
315 |
|
case 't': |
316 |
< |
tthresh = atof(argv[++i]); |
317 |
< |
if (tthresh <= 0) |
316 |
> |
pctcull = atof(argv[++i]); |
317 |
> |
if ((pctcull < 0) | (pctcull >= 100.)) |
318 |
|
goto userr; |
319 |
|
break; |
320 |
|
case 'f': |
345 |
|
gtree.kid = NULL; /* create our tree */ |
346 |
|
bmin[0] = bmin[1] = bmin[2] = bmin[3] = 0; |
347 |
|
build_tree(>ree, bmin, log2g); |
348 |
+ |
/* compute threshold & trim tree */ |
349 |
+ |
set_threshold(); |
350 |
+ |
trim_tree(>ree); |
351 |
|
/* format to stdout */ |
352 |
|
print_tree(>ree, bmin, log2g); |
353 |
|
/* Clean up isn't necessary for main()... |
356 |
|
*/ |
357 |
|
return(0); |
358 |
|
userr: |
359 |
< |
fprintf(stderr, "Usage: %s [-h][-f{a|f|d}][-r {3|4}][-g log2grid][-t thresh] [input]\n", |
359 |
> |
fprintf(stderr, "Usage: %s [-h][-f{a|f|d}][-r {3|4}][-g log2grid][-t trim%%] [input]\n", |
360 |
|
argv[0]); |
361 |
|
return(1); |
362 |
|
} |