ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rttree_reduce.c
(Generate patch)

Comparing ray/src/util/rttree_reduce.c (file contents):
Revision 2.1 by greg, Thu May 26 15:32:02 2011 UTC vs.
Revision 2.2 by greg, Tue May 31 20:50:26 2011 UTC

# Line 16 | Line 16 | float  *datarr;                /* our loaded BSDF data array */
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 {
# Line 77 | Line 84 | build_tree(TNODE *tp, const int bmin[], int l2s)
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 */
# Line 92 | Line 103 | build_tree(TNODE *tp, const int bmin[], int l2s)
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)
# Line 275 | Line 313 | main(int argc, char *argv[])
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':
# Line 307 | Line 345 | main(int argc, char *argv[])
345          gtree.kid = NULL;               /* create our tree */
346          bmin[0] = bmin[1] = bmin[2] = bmin[3] = 0;
347          build_tree(&gtree, bmin, log2g);
348 +                                        /* compute threshold & trim tree */
349 +        set_threshold();
350 +        trim_tree(&gtree);
351                                          /* format to stdout */
352          print_tree(&gtree, bmin, log2g);
353          /* Clean up isn't necessary for main()...
# Line 315 | Line 356 | main(int argc, char *argv[])
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines