119 |
|
if (pid < 0) { |
120 |
|
fprintf(stderr, "%s: cannot fork subprocess\n", |
121 |
|
progname); |
122 |
+ |
await_children(nchild); |
123 |
|
exit(1); |
124 |
|
} |
125 |
|
++nchild; /* subprocess started */ |
173 |
|
FVECT vfrom; |
174 |
|
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
175 |
|
for (j = to_rbf->nrbf; j--; ) { |
176 |
< |
pricerow(pm,i)[j] = acos(DOT(vfrom, vto[j])) + |
176 |
> |
double dprod = DOT(vfrom, vto[j]); |
177 |
> |
pricerow(pm,i)[j] = ((dprod >= 1.) ? .0 : acos(dprod)) + |
178 |
|
fabs(R2ANG(to_rbf->rbfa[j].crad) - from_ang); |
179 |
|
psortrow(pm,i)[j] = j; |
180 |
|
} |
199 |
|
int j; |
200 |
|
|
201 |
|
if (amt2move <= FTINY) /* pre-emptive check */ |
202 |
< |
return(0.); |
202 |
> |
return(.0); |
203 |
|
/* move cheapest first */ |
204 |
|
for (j = 0; j < pm->ncols && amt2move > FTINY; j++) { |
205 |
|
int d = psortrow(pm,s)[j]; |
215 |
|
static double |
216 |
|
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, const PRICEMAT *pm) |
217 |
|
{ |
218 |
< |
const double maxamt = .1; |
218 |
> |
const double maxamt = 1./(double)pm->ncols; |
219 |
|
const double minamt = maxamt*5e-6; |
220 |
< |
static double *src_cost = NULL; |
219 |
< |
static int n_alloc = 0; |
220 |
> |
double *src_cost; |
221 |
|
struct { |
222 |
|
int s, d; /* source and destination */ |
223 |
|
double price; /* price estimate per amount moved */ |
224 |
|
double amt; /* amount we can move */ |
225 |
|
} cur, best; |
226 |
|
int i; |
227 |
< |
|
228 |
< |
if (pm->nrows > n_alloc) { /* allocate cost array */ |
229 |
< |
if (n_alloc) |
230 |
< |
free(src_cost); |
231 |
< |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
232 |
< |
if (src_cost == NULL) { |
232 |
< |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
233 |
< |
progname); |
234 |
< |
exit(1); |
235 |
< |
} |
236 |
< |
n_alloc = pm->nrows; |
227 |
> |
/* allocate cost array */ |
228 |
> |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
229 |
> |
if (src_cost == NULL) { |
230 |
> |
fprintf(stderr, "%s: Out of memory in migration_step()\n", |
231 |
> |
progname); |
232 |
> |
exit(1); |
233 |
|
} |
234 |
|
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
235 |
|
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
237 |
|
/* find best source & dest. */ |
238 |
|
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
239 |
|
for (cur.s = pm->nrows; cur.s--; ) { |
244 |
– |
const float *price = pricerow(pm,cur.s); |
240 |
|
double cost_others = 0; |
241 |
+ |
|
242 |
|
if (src_rem[cur.s] <= minamt) |
243 |
|
continue; |
244 |
< |
cur.d = -1; /* examine cheapest dest. */ |
245 |
< |
for (i = pm->ncols; i--; ) |
246 |
< |
if (dst_rem[i] > minamt && |
247 |
< |
(cur.d < 0 || price[i] < price[cur.d])) |
248 |
< |
cur.d = i; |
249 |
< |
if (cur.d < 0) |
250 |
< |
return(.0); |
251 |
< |
if ((cur.price = price[cur.d]) >= best.price) |
256 |
< |
continue; /* no point checking further */ |
244 |
> |
/* examine cheapest dest. */ |
245 |
> |
for (i = 0; i < pm->ncols; i++) |
246 |
> |
if (dst_rem[ cur.d = psortrow(pm,cur.s)[i] ] > minamt) |
247 |
> |
break; |
248 |
> |
if (i >= pm->ncols) |
249 |
> |
break; |
250 |
> |
if ((cur.price = pricerow(pm,cur.s)[cur.d]) >= best.price) |
251 |
> |
continue; /* no point checking further */ |
252 |
|
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
253 |
|
src_rem[cur.s] : dst_rem[cur.d]; |
254 |
|
if (cur.amt > maxamt) cur.amt = maxamt; |
262 |
|
if (cur.price < best.price) /* are we better than best? */ |
263 |
|
best = cur; |
264 |
|
} |
265 |
< |
if ((best.s < 0) | (best.d < 0)) |
265 |
> |
free(src_cost); /* finish up */ |
266 |
> |
|
267 |
> |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
268 |
|
return(.0); |
269 |
< |
/* make the actual move */ |
269 |
> |
/* else make the actual move */ |
270 |
|
mtx_coef(mig,best.s,best.d) += best.amt; |
271 |
|
src_rem[best.s] -= best.amt; |
272 |
|
dst_rem[best.d] -= best.amt; |
273 |
|
return(best.amt); |
274 |
|
} |
275 |
|
|
279 |
– |
#ifdef DEBUG |
280 |
– |
static char * |
281 |
– |
thetaphi(const FVECT v) |
282 |
– |
{ |
283 |
– |
static char buf[128]; |
284 |
– |
double theta, phi; |
285 |
– |
|
286 |
– |
theta = 180./M_PI*acos(v[2]); |
287 |
– |
phi = 180./M_PI*atan2(v[1],v[0]); |
288 |
– |
sprintf(buf, "(%.0f,%.0f)", theta, phi); |
289 |
– |
|
290 |
– |
return(buf); |
291 |
– |
} |
292 |
– |
#endif |
293 |
– |
|
276 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
277 |
|
static MIGRATION * |
278 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
282 |
|
MIGRATION *newmig; |
283 |
|
double *src_rem, *dst_rem; |
284 |
|
double total_rem = 1., move_amt; |
285 |
< |
int i; |
285 |
> |
int i, j; |
286 |
|
/* check if exists already */ |
287 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
288 |
|
newmig = nextedge(from_rbf,newmig)) |
289 |
|
if (newmig->rbfv[1] == to_rbf) |
290 |
|
return(NULL); |
291 |
|
/* else allocate */ |
292 |
+ |
#ifdef DEBUG |
293 |
+ |
fprintf(stderr, "Building path from (theta,phi) (%.0f,%.0f) ", |
294 |
+ |
get_theta180(from_rbf->invec), |
295 |
+ |
get_phi360(from_rbf->invec)); |
296 |
+ |
fprintf(stderr, "to (%.0f,%.0f) with %d x %d matrix\n", |
297 |
+ |
get_theta180(to_rbf->invec), |
298 |
+ |
get_phi360(to_rbf->invec), |
299 |
+ |
from_rbf->nrbf, to_rbf->nrbf); |
300 |
+ |
#endif |
301 |
|
newmig = new_migration(from_rbf, to_rbf); |
302 |
|
if (run_subprocess()) |
303 |
|
return(newmig); /* child continues */ |
309 |
|
progname); |
310 |
|
exit(1); |
311 |
|
} |
321 |
– |
#ifdef DEBUG |
322 |
– |
fprintf(stderr, "Building path from (theta,phi) %s ", |
323 |
– |
thetaphi(from_rbf->invec)); |
324 |
– |
fprintf(stderr, "to %s with %d x %d matrix\n", |
325 |
– |
thetaphi(to_rbf->invec), |
326 |
– |
from_rbf->nrbf, to_rbf->nrbf); |
327 |
– |
#endif |
312 |
|
/* starting quantities */ |
313 |
|
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
314 |
|
for (i = from_rbf->nrbf; i--; ) |
315 |
|
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
316 |
< |
for (i = to_rbf->nrbf; i--; ) |
317 |
< |
dst_rem[i] = rbf_volume(&to_rbf->rbfa[i]) / to_rbf->vtotal; |
316 |
> |
for (j = to_rbf->nrbf; j--; ) |
317 |
> |
dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; |
318 |
> |
|
319 |
|
do { /* move a bit at a time */ |
320 |
|
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
321 |
|
total_rem -= move_amt; |
337 |
– |
#ifdef DEBUG |
338 |
– |
if (!nchild) |
339 |
– |
fprintf(stderr, "\r%.9f remaining...", total_rem); |
340 |
– |
#endif |
322 |
|
} while ((total_rem > end_thresh) & (move_amt > 0)); |
323 |
< |
#ifdef DEBUG |
343 |
< |
if (!nchild) fputs("done.\n", stderr); |
344 |
< |
else fprintf(stderr, "finished with %.9f remaining\n", total_rem); |
345 |
< |
#endif |
323 |
> |
|
324 |
|
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
325 |
< |
float nf = rbf_volume(&from_rbf->rbfa[i]); |
348 |
< |
int j; |
325 |
> |
double nf = rbf_volume(&from_rbf->rbfa[i]); |
326 |
|
if (nf <= FTINY) continue; |
327 |
|
nf = from_rbf->vtotal / nf; |
328 |
|
for (j = to_rbf->nrbf; j--; ) |
329 |
< |
mtx_coef(newmig,i,j) *= nf; |
329 |
> |
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
330 |
|
} |
331 |
|
end_subprocess(); /* exit here if subprocess */ |
332 |
|
free_routes(&pmtx); /* free working arrays */ |
438 |
|
} |
439 |
|
} |
440 |
|
} |
441 |
+ |
|
442 |
+ |
/* Compute minimum BSDF from histogram and clear it */ |
443 |
+ |
static void |
444 |
+ |
comp_bsdf_min() |
445 |
+ |
{ |
446 |
+ |
int cnt; |
447 |
+ |
int i, target; |
448 |
+ |
|
449 |
+ |
cnt = 0; |
450 |
+ |
for (i = HISTLEN; i--; ) |
451 |
+ |
cnt += bsdf_hist[i]; |
452 |
+ |
|
453 |
+ |
target = cnt/100; /* ignore bottom 1% */ |
454 |
+ |
cnt = 0; |
455 |
+ |
for (i = 0; cnt <= target; i++) |
456 |
+ |
cnt += bsdf_hist[i]; |
457 |
+ |
bsdf_min = histval(i-1); |
458 |
+ |
memset(bsdf_hist, 0, sizeof(bsdf_hist)); |
459 |
+ |
} |
460 |
|
|
461 |
|
/* Build our triangle mesh from recorded RBFs */ |
462 |
|
void |
492 |
|
mesh_from_edge(create_migration(shrt_edj[0], shrt_edj[1])); |
493 |
|
else |
494 |
|
mesh_from_edge(create_migration(shrt_edj[1], shrt_edj[0])); |
495 |
+ |
/* compute minimum BSDF */ |
496 |
+ |
comp_bsdf_min(); |
497 |
|
/* complete migrations */ |
498 |
|
await_children(nchild); |
499 |
|
} |