--- ray/src/cv/bsdfmesh.c 2012/11/08 23:32:30 2.5 +++ ray/src/cv/bsdfmesh.c 2013/03/20 01:00:22 2.8 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfmesh.c,v 2.5 2012/11/08 23:32:30 greg Exp $"; +static const char RCSid[] = "$Id: bsdfmesh.c,v 2.8 2013/03/20 01:00:22 greg Exp $"; #endif /* * Create BSDF advection mesh from radial basis functions. @@ -119,6 +119,7 @@ run_subprocess(void) if (pid < 0) { fprintf(stderr, "%s: cannot fork subprocess\n", progname); + await_children(nchild); exit(1); } ++nchild; /* subprocess started */ @@ -172,7 +173,8 @@ price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, co FVECT vfrom; ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); for (j = to_rbf->nrbf; j--; ) { - pricerow(pm,i)[j] = acos(DOT(vfrom, vto[j])) + + double dprod = DOT(vfrom, vto[j]); + pricerow(pm,i)[j] = ((dprod >= 1.) ? .0 : acos(dprod)) + fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang); psortrow(pm,i)[j] = j; } @@ -271,21 +273,6 @@ migration_step(MIGRATION *mig, double *src_rem, double return(best.amt); } -#ifdef DEBUG -static char * -thetaphi(const FVECT v) -{ - static char buf[128]; - double theta, phi; - - theta = 180./M_PI*acos(v[2]); - phi = 180./M_PI*atan2(v[1],v[0]); - sprintf(buf, "(%.0f,%.0f)", theta, phi); - - return(buf); -} -#endif - /* Compute and insert migration along directed edge (may fork child) */ static MIGRATION * create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) @@ -295,13 +282,22 @@ create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) MIGRATION *newmig; double *src_rem, *dst_rem; double total_rem = 1., move_amt; - int i; + int i, j; /* check if exists already */ for (newmig = from_rbf->ejl; newmig != NULL; newmig = nextedge(from_rbf,newmig)) if (newmig->rbfv[1] == to_rbf) return(NULL); /* else allocate */ +#ifdef DEBUG + fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ", + get_theta180(from_rbf->invec), + get_phi360(from_rbf->invec)); + fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n", + get_theta180(to_rbf->invec), + get_phi360(to_rbf->invec), + from_rbf->nrbf, to_rbf->nrbf); +#endif newmig = new_migration(from_rbf, to_rbf); if (run_subprocess()) return(newmig); /* child continues */ @@ -313,38 +309,24 @@ create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) progname); exit(1); } -#ifdef DEBUG - fprintf(stderr, "Building path from (theta,phi) %s ", - thetaphi(from_rbf->invec)); - fprintf(stderr, "to %s with %d x %d matrix\n", - thetaphi(to_rbf->invec), - from_rbf->nrbf, to_rbf->nrbf); -#endif /* starting quantities */ memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); for (i = from_rbf->nrbf; i--; ) src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; - for (i = to_rbf->nrbf; i--; ) - dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; + for (j = to_rbf->nrbf; j--; ) + dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; + do { /* move a bit at a time */ move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); total_rem -= move_amt; -#ifdef DEBUG - if (!nchild) - fprintf(stderr, "\r%.9f remaining...", total_rem); -#endif } while ((total_rem > end_thresh) & (move_amt > 0)); -#ifdef DEBUG - if (!nchild) fputs("done.\n", stderr); - else fprintf(stderr, "finished with %.9f remaining\n", total_rem); -#endif + for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ - float nf = rbf_volume(&from_rbf->rbfa[i]); - int j; + double nf = rbf_volume(&from_rbf->rbfa[i]); if (nf <= FTINY) continue; nf = from_rbf->vtotal / nf; for (j = to_rbf->nrbf; j--; ) - mtx_coef(newmig,i,j) *= nf; + mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ } end_subprocess(); /* exit here if subprocess */ free_routes(&pmtx); /* free working arrays */ @@ -456,6 +438,25 @@ mesh_from_edge(MIGRATION *edge) } } } + +/* Compute minimum BSDF from histogram and clear it */ +static void +comp_bsdf_min() +{ + int cnt; + int i, target; + + cnt = 0; + for (i = HISTLEN; i--; ) + cnt += bsdf_hist[i]; + + target = cnt/100; /* ignore bottom 1% */ + cnt = 0; + for (i = 0; cnt <= target; i++) + cnt += bsdf_hist[i]; + bsdf_min = histval(i-1); + memset(bsdf_hist, 0, sizeof(bsdf_hist)); +} /* Build our triangle mesh from recorded RBFs */ void @@ -491,6 +492,8 @@ build_mesh(void) mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1])); else mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0])); + /* compute minimum BSDF */ + comp_bsdf_min(); /* complete migrations */ await_children(nchild); }