--- ray/src/cv/bsdfmesh.c 2013/03/20 01:00:22 2.8 +++ ray/src/cv/bsdfmesh.c 2014/03/12 00:39:43 2.23 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfmesh.c,v 2.8 2013/03/20 01:00:22 greg Exp $"; +static const char RCSid[] = "$Id: bsdfmesh.c,v 2.23 2014/03/12 00:39:43 greg Exp $"; #endif /* * Create BSDF advection mesh from radial basis functions. @@ -18,6 +18,10 @@ static const char RCSid[] = "$Id: bsdfmesh.c,v 2.8 201 #include #include #include "bsdfrep.h" + +#ifndef NEIGH_FACT2 +#define NEIGH_FACT2 0.1 /* empirical neighborhood distance weight */ +#endif /* number of processes to run */ int nprocs = 1; /* number of children (-1 in child) */ @@ -27,6 +31,7 @@ typedef struct { int nrows, ncols; /* array size (matches migration) */ float *price; /* migration prices */ short *sord; /* sort for each row, low to high */ + float *prow; /* current price row */ } PRICEMAT; /* sorted pricing matrix */ #define pricerow(p,i) ((p)->price + (i)*(p)->ncols) @@ -134,14 +139,57 @@ run_subprocess(void) #endif /* ! _WIN32 */ +/* Compute normalized distribution scattering functions for comparison */ +static void +compute_nDSFs(const RBFNODE *rbf0, const RBFNODE *rbf1) +{ + const double nf0 = (GRIDRES*GRIDRES) / rbf0->vtotal; + const double nf1 = (GRIDRES*GRIDRES) / rbf1->vtotal; + int x, y; + FVECT dv; + + for (x = GRIDRES; x--; ) + for (y = GRIDRES; y--; ) { + ovec_from_pos(dv, x, y); /* cube root (brightness) */ + dsf_grid[x][y].val[0] = pow(nf0*eval_rbfrep(rbf0, dv), .3333); + dsf_grid[x][y].val[1] = pow(nf1*eval_rbfrep(rbf1, dv), .3333); + } +} + +/* Compute neighborhood distance-squared (dissimilarity) */ +static double +neighborhood_dist2(int x0, int y0, int x1, int y1) +{ + int rad = GRIDRES>>5; + double sum2 = 0.; + double d; + int p[4]; + int i, j; + + if ((x0 == x1) & (y0 == y1)) + return(0.); + /* check radius */ + p[0] = x0; p[1] = y0; p[2] = x1; p[3] = y1; + for (i = 4; i--; ) { + if (p[i] < rad) rad = p[i]; + if (GRIDRES-1-p[i] < rad) rad = GRIDRES-1-p[i]; + } + for (i = -rad; i <= rad; i++) + for (j = -rad; j <= rad; j++) { + d = dsf_grid[x0+i][y0+j].val[0] - + dsf_grid[x1+i][y1+j].val[1]; + sum2 += d*d; + } + return(sum2 / (4*rad*(rad+1) + 1)); +} + /* Comparison routine needed for sorting price row */ static int msrt_cmp(void *b, const void *p1, const void *p2) { PRICEMAT *pm = (PRICEMAT *)b; - int ri = ((const short *)p1 - pm->sord) / pm->ncols; - float c1 = pricerow(pm,ri)[*(const short *)p1]; - float c2 = pricerow(pm,ri)[*(const short *)p2]; + float c1 = pm->prow[*(const short *)p1]; + float c2 = pm->prow[*(const short *)p2]; if (c1 > c2) return(1); if (c1 < c2) return(-1); @@ -155,6 +203,7 @@ price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, co FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); int i, j; + compute_nDSFs(from_rbf, to_rbf); pm->nrows = from_rbf->nrbf; pm->ncols = to_rbf->nrbf; pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); @@ -171,14 +220,23 @@ price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, co for (i = from_rbf->nrbf; i--; ) { const double from_ang = R2ANG(from_rbf->rbfa[i].crad); FVECT vfrom; + short *srow; ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); + pm->prow = pricerow(pm,i); + srow = psortrow(pm,i); for (j = to_rbf->nrbf; 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; + double d; /* quadratic cost function */ + d = Acos(DOT(vfrom, vto[j])); + pm->prow[j] = d*d; + d = R2ANG(to_rbf->rbfa[j].crad) - from_ang; + pm->prow[j] += d*d; + /* neighborhood difference */ + pm->prow[j] += NEIGH_FACT2 * neighborhood_dist2( + from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy, + to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy ); + srow[j] = j; } - qsort_r(psortrow(pm,i), pm->ncols, sizeof(short), pm, &msrt_cmp); + qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); } free(vto); } @@ -195,75 +253,126 @@ free_routes(PRICEMAT *pm) static double min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) { + const short *srow = psortrow(pm,s); + const float *prow = pricerow(pm,s); double total_cost = 0; int j; - - if (amt2move <= FTINY) /* pre-emptive check */ - return(.0); /* move cheapest first */ - for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { - int d = psortrow(pm,s)[j]; + for (j = 0; (j < pm->ncols) & (amt2move > FTINY); j++) { + int d = srow[j]; double amt = (amt2move < avail[d]) ? amt2move : avail[d]; - total_cost += amt * pricerow(pm,s)[d]; + total_cost += amt * prow[d]; amt2move -= amt; } return(total_cost); } -/* Take a step in migration by choosing optimal bucket to transfer */ +typedef struct { + short s, d; /* source and destination */ + float dc; /* discount to push inventory */ +} ROWSENT; /* row sort entry */ + +/* Compare entries by discounted moving price */ +static int +rmovcmp(void *b, const void *p1, const void *p2) +{ + PRICEMAT *pm = (PRICEMAT *)b; + const ROWSENT *re1 = (const ROWSENT *)p1; + const ROWSENT *re2 = (const ROWSENT *)p2; + double price_diff; + + if (re1->d < 0) return(re2->d >= 0); + if (re2->d < 0) return(-1); + price_diff = re1->dc*pricerow(pm,re1->s)[re1->d] - + re2->dc*pricerow(pm,re2->s)[re2->d]; + if (price_diff > 0) return(1); + if (price_diff < 0) return(-1); + return(0); +} + +/* Take a step in migration by choosing reasonable bucket to transfer */ static double -migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) +migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, PRICEMAT *pm) { + const int max2check = 100; const double maxamt = 1./(double)pm->ncols; - const double minamt = maxamt*5e-6; + const double minamt = maxamt*1e-4; double *src_cost; + ROWSENT *rord; struct { int s, d; /* source and destination */ - double price; /* price estimate per amount moved */ + double price; /* cost per amount moved */ double amt; /* amount we can move */ } cur, best; - int i; + int r2check, i, ri; + /* + * Check cheapest available routes only -- a higher adjusted + * destination price implies that another source is closer, so + * we can hold off considering more expensive options until + * some other (hopefully better) moves have been made. + * A discount based on source remaining is supposed to prioritize + * movement from large lobes, but it doesn't seem to do much, + * so we have it set to 1.0 at the moment. + */ +#define discount(qr) 1.0 + /* most promising row order */ + rord = (ROWSENT *)malloc(sizeof(ROWSENT)*pm->nrows); + if (rord == NULL) + goto memerr; + for (ri = pm->nrows; ri--; ) { + rord[ri].s = ri; + rord[ri].d = -1; + rord[ri].dc = 1.f; + if (src_rem[ri] <= minamt) /* enough source material? */ + continue; + for (i = 0; i < pm->ncols; i++) + if (dst_rem[ rord[ri].d = psortrow(pm,ri)[i] ] > minamt) + break; + if (i >= pm->ncols) { /* moved all we can? */ + free(rord); + return(.0); + } + rord[ri].dc = discount(src_rem[ri]); + } + if (pm->nrows > max2check) /* sort if too many sources */ + qsort_r(rord, pm->nrows, sizeof(ROWSENT), pm, &rmovcmp); /* allocate cost array */ src_cost = (double *)malloc(sizeof(double)*pm->nrows); - if (src_cost == NULL) { - fprintf(stderr, "%s: Out of memory in migration_step()\n", - progname); - exit(1); - } + if (src_cost == NULL) + goto memerr; for (i = pm->nrows; i--; ) /* starting costs for diff. */ src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); - /* find best source & dest. */ best.s = best.d = -1; best.price = FHUGE; best.amt = 0; - for (cur.s = pm->nrows; cur.s--; ) { + if ((r2check = pm->nrows) > max2check) + r2check = max2check; /* put a limit on search */ + for (ri = 0; ri < r2check; ri++) { /* check each source row */ double cost_others = 0; - - if (src_rem[cur.s] <= minamt) - continue; - /* examine cheapest dest. */ - for (i = 0; i < pm->ncols; i++) - if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt) - break; - if (i >= pm->ncols) - break; - if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) - continue; /* no point checking further */ + cur.s = rord[ri].s; + if ((cur.d = rord[ri].d) < 0 || + rord[ri].dc*pricerow(pm,cur.s)[cur.d] >= best.price) { + if (pm->nrows > max2check) break; /* sorted end */ + continue; /* else skip this one */ + } cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? src_rem[cur.s] : dst_rem[cur.d]; - if (cur.amt > maxamt) cur.amt = maxamt; - dst_rem[cur.d] -= cur.amt; /* add up differential costs */ + /* don't just leave smidgen */ + if (cur.amt > maxamt*1.02) cur.amt = maxamt; + dst_rem[cur.d] -= cur.amt; /* add up opportunity costs */ for (i = pm->nrows; i--; ) if (i != cur.s) - cost_others += min_cost(src_rem[i], dst_rem, pm, i) + cost_others += min_cost(src_rem[i], dst_rem, pm, i) - src_cost[i]; dst_rem[cur.d] += cur.amt; /* undo trial move */ - cur.price += cost_others/cur.amt; /* adjust effective price */ + /* discount effective price */ + cur.price = ( pricerow(pm,cur.s)[cur.d] + cost_others/cur.amt ) * + rord[ri].dc; if (cur.price < best.price) /* are we better than best? */ - best = cur; + best = cur; } - free(src_cost); /* finish up */ - + free(src_cost); /* clean up */ + free(rord); if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ return(.0); /* else make the actual move */ @@ -271,6 +380,10 @@ migration_step(MIGRATION *mig, double *src_rem, double src_rem[best.s] -= best.amt; dst_rem[best.d] -= best.amt; return(best.amt); +memerr: + fprintf(stderr, "%s: Out of memory in migration_step()\n", progname); + exit(1); +#undef discount } /* Compute and insert migration along directed edge (may fork child) */ @@ -290,10 +403,10 @@ create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) return(NULL); /* else allocate */ #ifdef DEBUG - fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ", + fprintf(stderr, "Building path from (theta,phi) (%.1f,%.1f) ", get_theta180(from_rbf->invec), get_phi360(from_rbf->invec)); - fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n", + fprintf(stderr, "to (%.1f,%.1f) with %d x %d matrix\n", get_theta180(to_rbf->invec), get_phi360(to_rbf->invec), from_rbf->nrbf, to_rbf->nrbf); @@ -361,7 +474,7 @@ overlaps_tri(const RBFNODE *bv0, const RBFNODE *bv1, c return(vother[im_rev] != NULL); } -/* Find context hull vertex to complete triangle (oriented call) */ +/* Find convex hull vertex to complete triangle (oriented call) */ static RBFNODE * find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rbf1) { @@ -382,7 +495,7 @@ find_chull_vert(const RBFNODE *rbf0, const RBFNODE *rb if (DOT(vp, vmid) <= FTINY) continue; /* wrong orientation */ area2 = .25*DOT(vp,vp); - VSUB(vp, rbf->invec, rbf0->invec); + VSUB(vp, rbf->invec, vmid); dprod = -DOT(vp, vejn); VSUM(vp, vp, vejn, dprod); /* above guarantees non-zero */ dprod = DOT(vp, vmid) / VLEN(vp); @@ -439,23 +552,78 @@ mesh_from_edge(MIGRATION *edge) } } -/* Compute minimum BSDF from histogram and clear it */ +/* Add normal direction if missing */ static void -comp_bsdf_min() +check_normal_incidence(void) { - int cnt; - int i, target; + static const FVECT norm_vec = {.0, .0, 1.}; + const int saved_nprocs = nprocs; + RBFNODE *near_rbf, *mir_rbf, *rbf; + double bestd; + int n; - 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)); + if (dsf_list == NULL) + return; /* XXX should be error? */ + near_rbf = dsf_list; + bestd = input_orient*near_rbf->invec[2]; + if (single_plane_incident) { /* ordered plane incidence? */ + if (bestd >= 1.-2.*FTINY) + return; /* already have normal */ + } else { + switch (inp_coverage) { + case INP_QUAD1: + case INP_QUAD2: + case INP_QUAD3: + case INP_QUAD4: + break; /* quadrilateral symmetry? */ + default: + return; /* else we can interpolate */ + } + for (rbf = near_rbf->next; rbf != NULL; rbf = rbf->next) { + const double d = input_orient*rbf->invec[2]; + if (d >= 1.-2.*FTINY) + return; /* seems we have normal */ + if (d > bestd) { + near_rbf = rbf; + bestd = d; + } + } + } + if (mig_list != NULL) { /* need to be called first */ + fprintf(stderr, "%s: Late call to check_normal_incidence()\n", + progname); + exit(1); + } +#ifdef DEBUG + fprintf(stderr, "Interpolating normal incidence by mirroring (%.1f,%.1f)\n", + get_theta180(near_rbf->invec), get_phi360(near_rbf->invec)); +#endif + /* mirror nearest incidence */ + n = sizeof(RBFNODE) + sizeof(RBFVAL)*(near_rbf->nrbf-1); + mir_rbf = (RBFNODE *)malloc(n); + if (mir_rbf == NULL) + goto memerr; + memcpy(mir_rbf, near_rbf, n); + mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */ + mir_rbf->next = NULL; + mir_rbf->ejl = NULL; + rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y); + nprocs = 1; /* compute migration matrix */ + if (create_migration(mir_rbf, near_rbf) == NULL) + exit(1); /* XXX should never happen! */ + /* interpolate normal dist. */ + rbf = e_advect_rbf(mig_list, norm_vec, 2*near_rbf->nrbf); + nprocs = saved_nprocs; /* final clean-up */ + free(mir_rbf); + free(mig_list); + mig_list = near_rbf->ejl = NULL; + insert_dsf(rbf); /* insert interpolated normal */ + return; +memerr: + fprintf(stderr, "%s: Out of memory in check_normal_incidence()\n", + progname); + exit(1); } /* Build our triangle mesh from recorded RBFs */ @@ -465,6 +633,8 @@ build_mesh(void) double best2 = M_PI*M_PI; RBFNODE *shrt_edj[2]; RBFNODE *rbf0, *rbf1; + /* add normal if needed */ + check_normal_incidence(); /* check if isotropic */ if (single_plane_incident) { for (rbf0 = dsf_list; rbf0 != NULL; rbf0 = rbf0->next) @@ -492,8 +662,6 @@ 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); }