--- ray/src/cv/bsdfmesh.c 2014/02/18 16:06:51 2.15 +++ ray/src/cv/bsdfmesh.c 2014/03/10 20:58:29 2.22 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfmesh.c,v 2.15 2014/02/18 16:06:51 greg Exp $"; +static const char RCSid[] = "$Id: bsdfmesh.c,v 2.22 2014/03/10 20:58:29 greg Exp $"; #endif /* * Create BSDF advection mesh from radial basis functions. @@ -18,6 +18,10 @@ static const char RCSid[] = "$Id: bsdfmesh.c,v 2.15 20 #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) */ @@ -135,6 +139,50 @@ 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) @@ -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); @@ -176,12 +225,15 @@ price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, co pm->prow = pricerow(pm,i); srow = psortrow(pm,i); for (j = to_rbf->nrbf; j--; ) { - double d; /* quadratic cost function */ - d = DOT(vfrom, vto[j]); - d = (d >= 1.) ? .0 : acos(d); + 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; + 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(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); @@ -216,18 +268,24 @@ min_cost(double amt2move, const double *avail, const P return(total_cost); } -/* Compare entries by moving price */ +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 short *ij1 = (const short *)p1; - const short *ij2 = (const short *)p2; - float price_diff; + const ROWSENT *re1 = (const ROWSENT *)p1; + const ROWSENT *re2 = (const ROWSENT *)p2; + double price_diff; - if (ij1[1] < 0) return(ij2[1] >= 0); - if (ij2[1] < 0) return(-1); - price_diff = pricerow(pm,ij1[0])[ij1[1]] - pricerow(pm,ij2[0])[ij2[1]]; + 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); @@ -241,10 +299,10 @@ migration_step(MIGRATION *mig, double *src_rem, double const double maxamt = 1./(double)pm->ncols; const double minamt = maxamt*1e-4; double *src_cost; - short (*rord)[2]; + 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 r2check, i, ri; @@ -253,26 +311,32 @@ migration_step(MIGRATION *mig, double *src_rem, double * 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 = (short (*)[2])malloc(sizeof(short)*2*pm->nrows); + rord = (ROWSENT *)malloc(sizeof(ROWSENT)*pm->nrows); if (rord == NULL) goto memerr; for (ri = pm->nrows; ri--; ) { - rord[ri][0] = ri; - rord[ri][1] = -1; + 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][1] = psortrow(pm,ri)[i] ] > minamt) + 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(short)*2, pm, &rmovcmp); + qsort_r(rord, pm->nrows, sizeof(ROWSENT), pm, &rmovcmp); /* allocate cost array */ src_cost = (double *)malloc(sizeof(double)*pm->nrows); if (src_cost == NULL) @@ -285,9 +349,9 @@ migration_step(MIGRATION *mig, double *src_rem, double r2check = max2check; /* put a limit on search */ for (ri = 0; ri < r2check; ri++) { /* check each source row */ double cost_others = 0; - cur.s = rord[ri][0]; - if ((cur.d = rord[ri][1]) < 0 || - (cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) { + 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 */ } @@ -301,7 +365,9 @@ migration_step(MIGRATION *mig, double *src_rem, double 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; } @@ -317,6 +383,7 @@ migration_step(MIGRATION *mig, double *src_rem, double 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) */ @@ -333,7 +400,9 @@ create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) for (newmig = from_rbf->ejl; newmig != NULL; newmig = nextedge(from_rbf,newmig)) if (newmig->rbfv[1] == to_rbf) +{fprintf(stderr, "Edge already exists!\n"); return(NULL); +} /* else allocate */ #ifdef DEBUG fprintf(stderr, "Building path from (theta,phi) (%.1f,%.1f) ", @@ -489,11 +558,13 @@ mesh_from_edge(MIGRATION *edge) static void check_normal_incidence(void) { - const int saved_nprocs = nprocs; - RBFNODE *near_rbf, *mir_rbf, *rbf; - double bestd; - int n, i, j; + static const FVECT norm_vec = {.0, .0, 1.}; + const int saved_nprocs = nprocs; + RBFNODE *near_rbf, *mir_rbf, *rbf; + double bestd; + int n; + if (dsf_list == NULL) return; /* XXX should be error? */ near_rbf = dsf_list; @@ -538,49 +609,13 @@ check_normal_incidence(void) 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 (mig_list != create_migration(mir_rbf, near_rbf)) + if (create_migration(mir_rbf, near_rbf) == NULL) exit(1); /* XXX should never happen! */ - n = 0; /* count migrating particles */ - for (i = 0; i < mtx_nrows(mig_list); i++) - for (j = 0; j < mtx_ncols(mig_list); j++) - n += (mtx_coef(mig_list,i,j) > FTINY); - rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); - if (rbf == NULL) - goto memerr; - rbf->next = NULL; rbf->ejl = NULL; - rbf->invec[0] = rbf->invec[1] = 0; rbf->invec[2] = 1.; - rbf->nrbf = n; - rbf->vtotal = .5 + .5*mig_list->rbfv[1]->vtotal/mig_list->rbfv[0]->vtotal; - n = 0; /* advect RBF lobes halfway */ - for (i = 0; i < mtx_nrows(mig_list); i++) { - const RBFVAL *rbf0i = &mig_list->rbfv[0]->rbfa[i]; - const float peak0 = rbf0i->peak; - const double rad0 = R2ANG(rbf0i->crad); - FVECT v0; - float mv; - ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); - for (j = 0; j < mtx_ncols(mig_list); j++) - if ((mv = mtx_coef(mig_list,i,j)) > FTINY) { - const RBFVAL *rbf1j = &mig_list->rbfv[1]->rbfa[j]; - double rad2; - FVECT v; - int pos[2]; - rad2 = R2ANG(rbf1j->crad); - rad2 = .5*(rad0*rad0 + rad2*rad2); - rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * - rad0*rad0/rad2; - rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); - ovec_from_pos(v, rbf1j->gx, rbf1j->gy); - geodesic(v, v0, v, .5, GEOD_REL); - pos_from_vec(pos, v); - rbf->rbfa[n].gx = pos[0]; - rbf->rbfa[n].gy = pos[1]; - ++n; - } - } - rbf->vtotal *= mig_list->rbfv[0]->vtotal; + /* 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);