/* Compute (and allocate) migration price matrix for optimization */ static void price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) { FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); int i, j; pm->nrows = from_rbf->nrbf; pm->ncols = to_rbf->nrbf; pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) { fprintf(stderr, "%s: Out of memory in migration_costs()\n", progname); exit(1); } for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */ ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy); for (i = from_rbf->nrbf; i--; ) { const double from_ang = R2ANG(from_rbf->rbfa[i].crad); FVECT vfrom; ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); 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; } qsort_r(psortrow(pm,i), pm->ncols, sizeof(short), pm, &msrt_cmp); } free(vto); }
/* Cull points for more uniform distribution, leave all nval 0 or 1 */ static void cull_values(void) { FVECT ovec0, ovec1; double maxang, maxang2; int i, j, ii, jj, r; /* simple greedy algorithm */ for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) { if (!dsf_grid[i][j].nval) continue; if (!dsf_grid[i][j].crad) continue; /* shouldn't happen */ ovec_from_pos(ovec0, i, j); maxang = 2.*R2ANG(dsf_grid[i][j].crad); if (maxang > ovec0[2]) /* clamp near horizon */ maxang = ovec0[2]; r = maxang*(2.*GRIDRES/M_PI) + 1; maxang2 = maxang*maxang; for (ii = i-r; ii <= i+r; ii++) { if (ii < 0) continue; if (ii >= GRIDRES) break; for (jj = j-r; jj <= j+r; jj++) { if (jj < 0) continue; if (jj >= GRIDRES) break; if (!dsf_grid[ii][jj].nval) continue; if ((ii == i) & (jj == j)) continue; /* don't get self-absorbed */ ovec_from_pos(ovec1, ii, jj); if (2. - 2.*DOT(ovec0,ovec1) >= maxang2) continue; /* absorb sum */ dsf_grid[i][j].vsum += dsf_grid[ii][jj].vsum; dsf_grid[i][j].nval += dsf_grid[ii][jj].nval; /* keep value, though */ dsf_grid[ii][jj].vsum /= (float)dsf_grid[ii][jj].nval; dsf_grid[ii][jj].nval = 0; } } } /* final averaging pass */ for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) if (dsf_grid[i][j].nval > 1) { dsf_grid[i][j].vsum /= (float)dsf_grid[i][j].nval; dsf_grid[i][j].nval = 1; } }
/* Evaluate RBF for DSF at the given normalized outgoing direction */ double eval_rbfrep(const RBFNODE *rp, const FVECT outvec) { double minval = bsdf_min*output_orient*outvec[2]; double res = 0; const RBFVAL *rbfp; FVECT odir; double sig2; int n; /* use minimum if no information avail. */ if (rp == NULL) { if (outvec[2] > 0 ^ output_orient > 0) return(.0); return(minval); } rbfp = rp->rbfa; for (n = rp->nrbf; n--; rbfp++) { ovec_from_pos(odir, rbfp->gx, rbfp->gy); sig2 = R2ANG(rbfp->crad); sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2); if (sig2 > -19.) res += rbfp->peak * exp(sig2); } if (res < minval) /* never return less than minval */ return(minval); return(res); }
/* Compute distance between two RBF lobes */ double lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2) { FVECT vfrom, vto; double d, res; /* quadratic cost function */ ovec_from_pos(vfrom, rbf1->gx, rbf1->gy); ovec_from_pos(vto, rbf2->gx, rbf2->gy); d = Acos(DOT(vfrom, vto)); res = d*d; d = R2ANG(rbf2->crad) - R2ANG(rbf1->crad); res += d*d; /* neighborhood difference */ res += NEIGH_FACT2 * neighborhood_dist2( rbf1->gx, rbf1->gy, rbf2->gx, rbf2->gy ); return(res); }
/* 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); } }
/* Rotate RBF to correspond to given incident vector */ void rotate_rbf(RBFNODE *rbf, const FVECT invec) { static const FVECT vnorm = {.0, .0, 1.}; const double phi = atan2(invec[1],invec[0]) - atan2(rbf->invec[1],rbf->invec[0]); FVECT outvec; int pos[2]; int n; for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) { ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); spinvector(outvec, outvec, vnorm, phi); pos_from_vec(pos, outvec); rbf->rbfa[n].gx = pos[0]; rbf->rbfa[n].gy = pos[1]; } VCOPY(rbf->invec, invec); }
/* (distance to furthest empty bin for which non-empty bin is the closest) */ static void compute_radii(void) { unsigned int fill_grid[GRIDRES][GRIDRES]; unsigned short fill_cnt[GRIDRES][GRIDRES]; FVECT ovec0, ovec1; double ang2, lastang2; int r, i, j, jn, ii, jj, inear, jnear; r = GRIDRES/2; /* proceed in zig-zag */ for (i = 0; i < GRIDRES; i++) for (jn = 0; jn < GRIDRES; jn++) { j = (i&1) ? jn : GRIDRES-1-jn; if (dsf_grid[i][j].nval) /* find empty grid pos. */ continue; ovec_from_pos(ovec0, i, j); inear = jnear = -1; /* find nearest non-empty */ lastang2 = M_PI*M_PI; for (ii = i-r; ii <= i+r; ii++) { if (ii < 0) continue; if (ii >= GRIDRES) break; for (jj = j-r; jj <= j+r; jj++) { if (jj < 0) continue; if (jj >= GRIDRES) break; if (!dsf_grid[ii][jj].nval) continue; ovec_from_pos(ovec1, ii, jj); ang2 = 2. - 2.*DOT(ovec0,ovec1); if (ang2 >= lastang2) continue; lastang2 = ang2; inear = ii; jnear = jj; } } if (inear < 0) { fprintf(stderr, "%s: Could not find non-empty neighbor!\n", progname); exit(1); } ang2 = sqrt(lastang2); r = ANG2R(ang2); /* record if > previous */ if (r > dsf_grid[inear][jnear].crad) dsf_grid[inear][jnear].crad = r; /* next search radius */ r = ang2*(2.*GRIDRES/M_PI) + 3; } /* blur radii over hemisphere */ memset(fill_grid, 0, sizeof(fill_grid)); memset(fill_cnt, 0, sizeof(fill_cnt)); for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) { if (!dsf_grid[i][j].crad) continue; /* missing distance */ r = R2ANG(dsf_grid[i][j].crad)*(2.*RSCA*GRIDRES/M_PI); for (ii = i-r; ii <= i+r; ii++) { if (ii < 0) continue; if (ii >= GRIDRES) break; for (jj = j-r; jj <= j+r; jj++) { if (jj < 0) continue; if (jj >= GRIDRES) break; if ((ii-i)*(ii-i) + (jj-j)*(jj-j) > r*r) continue; fill_grid[ii][jj] += dsf_grid[i][j].crad; fill_cnt[ii][jj]++; } } } /* copy back blurred radii */ for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) if (fill_cnt[i][j]) dsf_grid[i][j].crad = fill_grid[i][j]/fill_cnt[i][j]; }
/* Count up filled nodes and build RBF representation from current grid */ RBFNODE * make_rbfrep(void) { int niter = 16; double lastVar, thisVar = 100.; int nn; RBFNODE *newnode; RBFVAL *itera; int i, j; /* compute RBF radii */ compute_radii(); /* coagulate lobes */ cull_values(); nn = 0; /* count selected bins */ for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) nn += dsf_grid[i][j].nval; /* compute minimum BSDF */ comp_bsdf_min(); /* allocate RBF array */ newnode = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(nn-1)); if (newnode == NULL) goto memerr; newnode->ord = -1; newnode->next = NULL; newnode->ejl = NULL; newnode->invec[2] = sin((M_PI/180.)*theta_in_deg); newnode->invec[0] = cos((M_PI/180.)*phi_in_deg)*newnode->invec[2]; newnode->invec[1] = sin((M_PI/180.)*phi_in_deg)*newnode->invec[2]; newnode->invec[2] = input_orient*sqrt(1. - newnode->invec[2]*newnode->invec[2]); newnode->vtotal = 0; newnode->nrbf = nn; nn = 0; /* fill RBF array */ for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) if (dsf_grid[i][j].nval) { newnode->rbfa[nn].peak = dsf_grid[i][j].vsum; newnode->rbfa[nn].crad = RSCA*dsf_grid[i][j].crad + .5; newnode->rbfa[nn].gx = i; newnode->rbfa[nn].gy = j; ++nn; } /* iterate to improve interpolation accuracy */ itera = (RBFVAL *)malloc(sizeof(RBFVAL)*newnode->nrbf); if (itera == NULL) goto memerr; memcpy(itera, newnode->rbfa, sizeof(RBFVAL)*newnode->nrbf); do { double dsum = 0, dsum2 = 0; nn = 0; for (i = 0; i < GRIDRES; i++) for (j = 0; j < GRIDRES; j++) if (dsf_grid[i][j].nval) { FVECT odir; double corr; ovec_from_pos(odir, i, j); itera[nn++].peak *= corr = dsf_grid[i][j].vsum / eval_rbfrep(newnode, odir); dsum += 1. - corr; dsum2 += (1.-corr)*(1.-corr); } memcpy(newnode->rbfa, itera, sizeof(RBFVAL)*newnode->nrbf); lastVar = thisVar; thisVar = dsum2/(double)nn; #ifdef DEBUG fprintf(stderr, "Avg., RMS error: %.1f%% %.1f%%\n", 100.*dsum/(double)nn, 100.*sqrt(thisVar)); #endif } while (--niter > 0 && lastVar-thisVar > 0.02*lastVar); free(itera); nn = 0; /* compute sum for normalization */ while (nn < newnode->nrbf) newnode->vtotal += rbf_volume(&newnode->rbfa[nn++]); #ifdef DEBUG fprintf(stderr, "Integrated DSF at (%.1f,%.1f) deg. is %.2f\n", get_theta180(newnode->invec), get_phi360(newnode->invec), newnode->vtotal); #endif insert_dsf(newnode); return(newnode); memerr: fprintf(stderr, "%s: Out of memory in make_rbfrep()\n", progname); exit(1); }