--- ray/src/util/rttree_reduce.c 2013/10/03 03:36:11 2.8 +++ ray/src/util/rttree_reduce.c 2017/05/31 03:26:46 2.17 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rttree_reduce.c,v 2.8 2013/10/03 03:36:11 greg Exp $"; +static const char RCSid[] = "$Id: rttree_reduce.c,v 2.17 2017/05/31 03:26:46 greg Exp $"; #endif /* * A utility called by genBSDF.pl to reduce tensor tree samples and output @@ -44,7 +44,7 @@ double tthresh; /* acceptance threshold (TBD) */ static void new_kids(TNODE *pn) { - pn->kid = (TNODE *)calloc(1<kid = (TNODE *)calloc((size_t)1<kid == NULL) error(SYSTEM, "out of memory in new_kids"); } @@ -110,7 +110,7 @@ build_tree(TNODE *tp, const int bmin[], int l2s) /* Set our trimming threshold */ static void -set_threshold() +set_threshold(void) { int hsum = 0; int i; @@ -155,7 +155,7 @@ print_tree(const TNODE *tp, const int bmin[], int l2s) bkmin[j] = bmin[j] + (i>>(ttrank-1-j) & 1); val = (ttrank == 3) ? dval3(bkmin[0],bkmin[1],bkmin[2]) : dval4(bkmin[0],bkmin[1],bkmin[2],bkmin[3]); - printf(" %.4e", val); + printf((0.001<=val)&(val<10.) ? " %.7f" : " %.3e", val); } fputs(" }\n", stdout); return; @@ -201,7 +201,7 @@ read_float(float *rowp, int n) if ((rowp == NULL) | (n <= 0)) return(0); - nread = fread(rowp, sizeof(float), n, stdin); + nread = getbinary(rowp, sizeof(float), n, stdin); if (nread != n) error(USER, "unexpected EOF on float input"); return(nread); @@ -223,11 +223,12 @@ read_double(float *rowp, int n) return(0); } if (rblen < n) { - rowbuf = (double *)realloc(rowbuf, sizeof(double)*(rblen=n)); + if (rblen) free(rowbuf); + rowbuf = (double *)malloc(sizeof(double)*(rblen=n)); if (rowbuf == NULL) error(SYSTEM, "out of memory in read_double"); } - nread = fread(rowbuf, sizeof(double), n, stdin); + nread = getbinary(rowbuf, sizeof(double), n, stdin); if (nread != n) error(USER, "unexpected EOF on double input"); for (i = 0; i < nread; i++) @@ -235,9 +236,30 @@ read_double(float *rowp, int n) return(nread); } +/* Truncate any negative values to zero */ +static void +noneg(float *varr, int n) +{ + int nnan = 0; + + while (n-- > 0) { +#ifdef isnan + if (isnan(*varr)) { + *varr = 0; + ++nnan; + } else +#endif + if (*varr < 0) *varr = 0; + ++varr; + } + if (nnan) + fprintf(stderr, "Warning: BSDF data contains %d NaN values\n", + nnan); +} + /* Load data array, filling zeroes for rank 3 demi-tensor */ static void -load_data() +load_data(void) { int (*readf)(float *, int) = NULL; @@ -255,23 +277,20 @@ load_data() error(COMMAND, "unsupported input format"); break; } - datarr = (float *)calloc(1<<(log2g*ttrank), sizeof(float)); + datarr = (float *)calloc((size_t)1<<(log2g*ttrank), sizeof(float)); if (datarr == NULL) error(SYSTEM, "out of memory in load_data"); if (ttrank == 3) { int ix, ox; for (ix = 0; ix < 1<<(log2g-1); ix++) for (ox = 0; ox < 1< 0; ox++) + for (oy = 0; oy < siz; oy++) { v1p = &dval4(siz-1-ix,siz-1-iy,ox,oy); v2p = &dval4(siz-1-ox,siz-1-oy,ix,iy); *v1p = *v2p = .5f*( *v1p + *v2p ); + if (--cnt <= 0) + break; } + } } } @@ -364,12 +389,15 @@ main(int argc, char *argv[]) if (i < argc-1) goto userr; /* load input data */ - if (i == argc-1 && freopen(argv[i], "rb", stdin) == NULL) { + if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) { sprintf(errmsg, "cannot open input file '%s'", argv[i]); error(SYSTEM, errmsg); } if (infmt != 'a') SET_FILE_BINARY(stdin); +#ifdef getc_unlocked /* avoid lock/unlock overhead */ + flockfile(stdin); +#endif load_data(); if (recipavg) do_reciprocity();