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.2 by greg, Tue May 31 20:50:26 2011 UTC vs.
Revision 2.6 by greg, Mon Nov 21 20:07:50 2011 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
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  pctcull = 99.;          /* target culling percentile */
20 > double  pctcull = 95.;          /* target culling percentile */
21  
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
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  
31 #define above_threshold(tp)     ((tp)->vmax - (tp)->vmin > tthresh)
32
25   /* Tensor tree node */
26   typedef struct ttree_s {
27          float           vmin, vmax;     /* value extrema */
# Line 37 | Line 29 | typedef struct ttree_s {
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)
# Line 85 | Line 88 | build_tree(TNODE *tp, const int bmin[], int l2s)
88                  }
89                  tp->vavg /= (float)(1<<ttrank);
90                                          /* record stats */
91 <                i = (HISTLEN/HISTMAX) * (tp->vmax - tp->vmin);
91 >                i = (HISTLEN/HISTMAX) * var_measure(tp);
92                  if (i >= HISTLEN) i = HISTLEN-1;
93                  ++histo[i];
94                  return;
# Line 149 | Line 152 | print_tree(const TNODE *tp, const int bmin[], int l2s)
152                  for (i = 0; i < 1<<ttrank; i++) {
153                          float   val;
154                          for (j = ttrank; j--; )
155 <                                bkmin[j] = bmin[j] + (i>>j & 1);
155 >                                bkmin[j] = bmin[j] + (i>>(ttrank-1-j) & 1);
156                          val = (ttrank == 3) ? dval3(bkmin[0],bkmin[1],bkmin[2])
157                                  : dval4(bkmin[0],bkmin[1],bkmin[2],bkmin[3]);
158                          printf(" %.4e", val);
# Line 257 | Line 260 | load_data()
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));
265 >                                (*readf)(datarr+(((ix<<log2g)+ox)<<log2g),
266 >                                                1<<log2g);
267          } else /* ttrank == 4 */ {
268                  int     ix, iy, ox;
269                  for (ix = 0; ix < 1<<log2g; ix++)
270                      for (iy = 0; iy < 1<<log2g; iy++)
271                          for (ox = 0; ox < 1<<log2g; ox++)
272                                  (*readf)(datarr +
273 <                                ((((((ix)<<log2g)+(iy))<<log2g)+(ox))<<log2g),
273 >                                (((((ix<<log2g)+iy)<<log2g)+ox)<<log2g),
274                                                  1<<log2g);
275          }
276          (*readf)(NULL, 0);      /* releases any buffers */
# Line 288 | Line 291 | load_data()
291                  error(WARNING, "binary data past end of expected input");
292   }
293  
294 + /* Enforce reciprocity by averaging data values */
295 + static void
296 + do_reciprocity()
297 + {
298 +        float   *v1p, *v2p;
299 +
300 +        if (ttrank == 3) {
301 +                int     ix, ox, oy;
302 +                for (ix = 0; ix < 1<<(log2g-1); ix++)
303 +                    for (ox = 0; ox < 1<<log2g; ox++)
304 +                        for (oy = 0; oy < 1<<(log2g-1); oy++) {
305 +                                v1p = &dval3(ix,ox,oy);
306 +                                v2p = &dval3(ix,ox,(1<<log2g)-1-oy);
307 +                                *v1p = *v2p = .5f*( *v1p + *v2p );
308 +                        }
309 +        } else /* ttrank == 4 */ {
310 +                int     ix, iy, ox, oy;
311 +                for (ix = 1; ix < 1<<log2g; ix++)
312 +                    for (iy = 1; iy < 1<<log2g; iy++)
313 +                        for (ox = 0; ox < ix; ox++)
314 +                            for (oy = 0; oy < iy; oy++) {
315 +                                v1p = &dval4(ix,iy,ox,oy);
316 +                                v2p = &dval4(ox,oy,ix,iy);
317 +                                *v1p = *v2p = .5f*( *v1p + *v2p );
318 +                            }
319 +        }
320 + }
321 +
322   /* Load BSDF array, coalesce uniform regions and format as tensor tree */
323   int
324   main(int argc, char *argv[])
325   {
326          int     doheader = 1;
327 +        int     recipavg = 0;
328          int     bmin[4];
329          TNODE   gtree;
330          int     i;
331                                          /* get options and parameters */
332          for (i = 1; i < argc && argv[i][0] == '-'; i++)
333                  switch (argv[i][1]) {
334 +                case 'a':
335 +                        recipavg = !recipavg;
336 +                        break;
337                  case 'h':
338                          doheader = !doheader;
339                          break;
# Line 335 | Line 370 | main(int argc, char *argv[])
370          if (infmt != 'a')
371                  SET_FILE_BINARY(stdin);
372          load_data();
373 +        if (recipavg)
374 +                do_reciprocity();
375          if (doheader) {
376                  for (i = 0; i < argc; i++) {
377                          fputs(argv[i], stdout);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines