18 |
|
#include <string.h> |
19 |
|
#include <math.h> |
20 |
|
#include "bsdfrep.h" |
21 |
+ |
|
22 |
+ |
#ifndef NEIGH_FACT2 |
23 |
+ |
#define NEIGH_FACT2 0.1 /* empirical neighborhood distance weight */ |
24 |
+ |
#endif |
25 |
|
/* number of processes to run */ |
26 |
|
int nprocs = 1; |
27 |
|
/* number of children (-1 in child) */ |
28 |
|
static int nchild = 0; |
29 |
|
|
26 |
– |
typedef struct { |
27 |
– |
int nrows, ncols; /* array size (matches migration) */ |
28 |
– |
float *price; /* migration prices */ |
29 |
– |
short *sord; /* sort for each row, low to high */ |
30 |
– |
float *prow; /* current price row */ |
31 |
– |
} PRICEMAT; /* sorted pricing matrix */ |
32 |
– |
|
33 |
– |
#define pricerow(p,i) ((p)->price + (i)*(p)->ncols) |
34 |
– |
#define psortrow(p,i) ((p)->sord + (i)*(p)->ncols) |
35 |
– |
|
30 |
|
/* Create a new migration holder (sharing memory for multiprocessing) */ |
31 |
|
static MIGRATION * |
32 |
|
new_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
129 |
|
|
130 |
|
#endif /* ! _WIN32 */ |
131 |
|
|
132 |
< |
/* Comparison routine needed for sorting price row */ |
139 |
< |
static int |
140 |
< |
msrt_cmp(void *b, const void *p1, const void *p2) |
141 |
< |
{ |
142 |
< |
PRICEMAT *pm = (PRICEMAT *)b; |
143 |
< |
float c1 = pm->prow[*(const short *)p1]; |
144 |
< |
float c2 = pm->prow[*(const short *)p2]; |
145 |
< |
|
146 |
< |
if (c1 > c2) return(1); |
147 |
< |
if (c1 < c2) return(-1); |
148 |
< |
return(0); |
149 |
< |
} |
150 |
< |
|
151 |
< |
/* Compute (and allocate) migration price matrix for optimization */ |
132 |
> |
/* Compute normalized distribution scattering functions for comparison */ |
133 |
|
static void |
134 |
< |
price_routes(PRICEMAT *pm, const RBFNODE *from_rbf, const RBFNODE *to_rbf) |
134 |
> |
compute_nDSFs(const RBFNODE *rbf0, const RBFNODE *rbf1) |
135 |
|
{ |
136 |
< |
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
137 |
< |
int i, j; |
136 |
> |
const double nf0 = (GRIDRES*GRIDRES) / rbf0->vtotal; |
137 |
> |
const double nf1 = (GRIDRES*GRIDRES) / rbf1->vtotal; |
138 |
> |
int x, y; |
139 |
> |
FVECT dv; |
140 |
|
|
141 |
< |
pm->nrows = from_rbf->nrbf; |
142 |
< |
pm->ncols = to_rbf->nrbf; |
143 |
< |
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
144 |
< |
pm->sord = (short *)malloc(sizeof(short) * pm->nrows*pm->ncols); |
145 |
< |
|
163 |
< |
if ((pm->price == NULL) | (pm->sord == NULL) | (vto == NULL)) { |
164 |
< |
fprintf(stderr, "%s: Out of memory in migration_costs()\n", |
165 |
< |
progname); |
166 |
< |
exit(1); |
167 |
< |
} |
168 |
< |
for (j = to_rbf->nrbf; j--; ) /* save repetitive ops. */ |
169 |
< |
ovec_from_pos(vto[j], to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy); |
170 |
< |
|
171 |
< |
for (i = from_rbf->nrbf; i--; ) { |
172 |
< |
const double from_ang = R2ANG(from_rbf->rbfa[i].crad); |
173 |
< |
FVECT vfrom; |
174 |
< |
short *srow; |
175 |
< |
ovec_from_pos(vfrom, from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy); |
176 |
< |
pm->prow = pricerow(pm,i); |
177 |
< |
srow = psortrow(pm,i); |
178 |
< |
for (j = to_rbf->nrbf; j--; ) { |
179 |
< |
double d; /* quadratic cost function */ |
180 |
< |
d = Acos(DOT(vfrom, vto[j])); |
181 |
< |
pm->prow[j] = d*d; |
182 |
< |
d = R2ANG(to_rbf->rbfa[j].crad) - from_ang; |
183 |
< |
pm->prow[j] += d*d; |
184 |
< |
srow[j] = j; |
141 |
> |
for (x = GRIDRES; x--; ) |
142 |
> |
for (y = GRIDRES; y--; ) { |
143 |
> |
ovec_from_pos(dv, x, y); /* cube root (brightness) */ |
144 |
> |
dsf_grid[x][y].val[0] = pow(nf0*eval_rbfrep(rbf0, dv), .3333); |
145 |
> |
dsf_grid[x][y].val[1] = pow(nf1*eval_rbfrep(rbf1, dv), .3333); |
146 |
|
} |
147 |
< |
qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); |
187 |
< |
} |
188 |
< |
free(vto); |
189 |
< |
} |
147 |
> |
} |
148 |
|
|
149 |
< |
/* Free price matrix */ |
192 |
< |
static void |
193 |
< |
free_routes(PRICEMAT *pm) |
194 |
< |
{ |
195 |
< |
free(pm->price); pm->price = NULL; |
196 |
< |
free(pm->sord); pm->sord = NULL; |
197 |
< |
} |
198 |
< |
|
199 |
< |
/* Compute minimum (optimistic) cost for moving the given source material */ |
149 |
> |
/* Compute neighborhood distance-squared (dissimilarity) */ |
150 |
|
static double |
151 |
< |
min_cost(double amt2move, const double *avail, const PRICEMAT *pm, int s) |
151 |
> |
neighborhood_dist2(int x0, int y0, int x1, int y1) |
152 |
|
{ |
153 |
< |
const short *srow = psortrow(pm,s); |
154 |
< |
const float *prow = pricerow(pm,s); |
155 |
< |
double total_cost = 0; |
156 |
< |
int j; |
157 |
< |
/* move cheapest first */ |
158 |
< |
for (j = 0; (j < pm->ncols) & (amt2move > FTINY); j++) { |
159 |
< |
int d = srow[j]; |
160 |
< |
double amt = (amt2move < avail[d]) ? amt2move : avail[d]; |
161 |
< |
|
162 |
< |
total_cost += amt * prow[d]; |
213 |
< |
amt2move -= amt; |
153 |
> |
int rad = GRIDRES>>5; |
154 |
> |
double sum2 = 0.; |
155 |
> |
double d; |
156 |
> |
int p[4]; |
157 |
> |
int i, j; |
158 |
> |
/* check radius */ |
159 |
> |
p[0] = x0; p[1] = y0; p[2] = x1; p[3] = y1; |
160 |
> |
for (i = 4; i--; ) { |
161 |
> |
if (p[i] < rad) rad = p[i]; |
162 |
> |
if (GRIDRES-1-p[i] < rad) rad = GRIDRES-1-p[i]; |
163 |
|
} |
164 |
< |
return(total_cost); |
164 |
> |
for (i = -rad; i <= rad; i++) |
165 |
> |
for (j = -rad; j <= rad; j++) { |
166 |
> |
d = dsf_grid[x0+i][y0+j].val[0] - |
167 |
> |
dsf_grid[x1+i][y1+j].val[1]; |
168 |
> |
sum2 += d*d; |
169 |
> |
} |
170 |
> |
return(sum2 / (4*rad*(rad+1) + 1)); |
171 |
|
} |
172 |
|
|
173 |
< |
typedef struct { |
174 |
< |
short s, d; /* source and destination */ |
175 |
< |
float dc; /* discount to push inventory */ |
221 |
< |
} ROWSENT; /* row sort entry */ |
222 |
< |
|
223 |
< |
/* Compare entries by discounted moving price */ |
224 |
< |
static int |
225 |
< |
rmovcmp(void *b, const void *p1, const void *p2) |
173 |
> |
/* Compute distance between two RBF lobes */ |
174 |
> |
double |
175 |
> |
lobe_distance(RBFVAL *rbf1, RBFVAL *rbf2) |
176 |
|
{ |
177 |
< |
PRICEMAT *pm = (PRICEMAT *)b; |
178 |
< |
const ROWSENT *re1 = (const ROWSENT *)p1; |
179 |
< |
const ROWSENT *re2 = (const ROWSENT *)p2; |
180 |
< |
double price_diff; |
181 |
< |
|
182 |
< |
if (re1->d < 0) return(re2->d >= 0); |
183 |
< |
if (re2->d < 0) return(-1); |
184 |
< |
price_diff = re1->dc*pricerow(pm,re1->s)[re1->d] - |
185 |
< |
re2->dc*pricerow(pm,re2->s)[re2->d]; |
186 |
< |
if (price_diff > 0) return(1); |
187 |
< |
if (price_diff < 0) return(-1); |
188 |
< |
return(0); |
177 |
> |
FVECT vfrom, vto; |
178 |
> |
double d, res; |
179 |
> |
/* quadratic cost function */ |
180 |
> |
ovec_from_pos(vfrom, rbf1->gx, rbf1->gy); |
181 |
> |
ovec_from_pos(vto, rbf2->gx, rbf2->gy); |
182 |
> |
d = Acos(DOT(vfrom, vto)); |
183 |
> |
res = d*d; |
184 |
> |
d = R2ANG(rbf2->crad) - R2ANG(rbf1->crad); |
185 |
> |
res += d*d; |
186 |
> |
/* neighborhood difference */ |
187 |
> |
res += NEIGH_FACT2 * neighborhood_dist2( rbf1->gx, rbf1->gy, |
188 |
> |
rbf2->gx, rbf2->gy ); |
189 |
> |
return(res); |
190 |
|
} |
191 |
|
|
241 |
– |
/* Take a step in migration by choosing reasonable bucket to transfer */ |
242 |
– |
static double |
243 |
– |
migration_step(MIGRATION *mig, double *src_rem, double *dst_rem, PRICEMAT *pm) |
244 |
– |
{ |
245 |
– |
const int max2check = 100; |
246 |
– |
const double maxamt = 1./(double)pm->ncols; |
247 |
– |
const double minamt = maxamt*1e-4; |
248 |
– |
double *src_cost; |
249 |
– |
ROWSENT *rord; |
250 |
– |
struct { |
251 |
– |
int s, d; /* source and destination */ |
252 |
– |
double price; /* cost per amount moved */ |
253 |
– |
double amt; /* amount we can move */ |
254 |
– |
} cur, best; |
255 |
– |
int r2check, i, ri; |
256 |
– |
/* |
257 |
– |
* Check cheapest available routes only -- a higher adjusted |
258 |
– |
* destination price implies that another source is closer, so |
259 |
– |
* we can hold off considering more expensive options until |
260 |
– |
* some other (hopefully better) moves have been made. |
261 |
– |
* A discount based on source remaining is supposed to prioritize |
262 |
– |
* movement from large lobes, but it doesn't seem to do much, |
263 |
– |
* so we have it set to 1.0 at the moment. |
264 |
– |
*/ |
265 |
– |
#define discount(qr) 1.0 |
266 |
– |
/* most promising row order */ |
267 |
– |
rord = (ROWSENT *)malloc(sizeof(ROWSENT)*pm->nrows); |
268 |
– |
if (rord == NULL) |
269 |
– |
goto memerr; |
270 |
– |
for (ri = pm->nrows; ri--; ) { |
271 |
– |
rord[ri].s = ri; |
272 |
– |
rord[ri].d = -1; |
273 |
– |
rord[ri].dc = 1.f; |
274 |
– |
if (src_rem[ri] <= minamt) /* enough source material? */ |
275 |
– |
continue; |
276 |
– |
for (i = 0; i < pm->ncols; i++) |
277 |
– |
if (dst_rem[ rord[ri].d = psortrow(pm,ri)[i] ] > minamt) |
278 |
– |
break; |
279 |
– |
if (i >= pm->ncols) { /* moved all we can? */ |
280 |
– |
free(rord); |
281 |
– |
return(.0); |
282 |
– |
} |
283 |
– |
rord[ri].dc = discount(src_rem[ri]); |
284 |
– |
} |
285 |
– |
if (pm->nrows > max2check) /* sort if too many sources */ |
286 |
– |
qsort_r(rord, pm->nrows, sizeof(ROWSENT), pm, &rmovcmp); |
287 |
– |
/* allocate cost array */ |
288 |
– |
src_cost = (double *)malloc(sizeof(double)*pm->nrows); |
289 |
– |
if (src_cost == NULL) |
290 |
– |
goto memerr; |
291 |
– |
for (i = pm->nrows; i--; ) /* starting costs for diff. */ |
292 |
– |
src_cost[i] = min_cost(src_rem[i], dst_rem, pm, i); |
293 |
– |
/* find best source & dest. */ |
294 |
– |
best.s = best.d = -1; best.price = FHUGE; best.amt = 0; |
295 |
– |
if ((r2check = pm->nrows) > max2check) |
296 |
– |
r2check = max2check; /* put a limit on search */ |
297 |
– |
for (ri = 0; ri < r2check; ri++) { /* check each source row */ |
298 |
– |
double cost_others = 0; |
299 |
– |
cur.s = rord[ri].s; |
300 |
– |
if ((cur.d = rord[ri].d) < 0 || |
301 |
– |
rord[ri].dc*pricerow(pm,cur.s)[cur.d] >= best.price) { |
302 |
– |
if (pm->nrows > max2check) break; /* sorted end */ |
303 |
– |
continue; /* else skip this one */ |
304 |
– |
} |
305 |
– |
cur.amt = (src_rem[cur.s] < dst_rem[cur.d]) ? |
306 |
– |
src_rem[cur.s] : dst_rem[cur.d]; |
307 |
– |
/* don't just leave smidgen */ |
308 |
– |
if (cur.amt > maxamt*1.02) cur.amt = maxamt; |
309 |
– |
dst_rem[cur.d] -= cur.amt; /* add up opportunity costs */ |
310 |
– |
for (i = pm->nrows; i--; ) |
311 |
– |
if (i != cur.s) |
312 |
– |
cost_others += min_cost(src_rem[i], dst_rem, pm, i) |
313 |
– |
- src_cost[i]; |
314 |
– |
dst_rem[cur.d] += cur.amt; /* undo trial move */ |
315 |
– |
/* discount effective price */ |
316 |
– |
cur.price = ( pricerow(pm,cur.s)[cur.d] + cost_others/cur.amt ) * |
317 |
– |
rord[ri].dc; |
318 |
– |
if (cur.price < best.price) /* are we better than best? */ |
319 |
– |
best = cur; |
320 |
– |
} |
321 |
– |
free(src_cost); /* clean up */ |
322 |
– |
free(rord); |
323 |
– |
if ((best.s < 0) | (best.d < 0)) /* nothing left to move? */ |
324 |
– |
return(.0); |
325 |
– |
/* else make the actual move */ |
326 |
– |
mtx_coef(mig,best.s,best.d) += best.amt; |
327 |
– |
src_rem[best.s] -= best.amt; |
328 |
– |
dst_rem[best.d] -= best.amt; |
329 |
– |
return(best.amt); |
330 |
– |
memerr: |
331 |
– |
fprintf(stderr, "%s: Out of memory in migration_step()\n", progname); |
332 |
– |
exit(1); |
333 |
– |
#undef discount |
334 |
– |
} |
192 |
|
|
193 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
194 |
|
static MIGRATION * |
195 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
196 |
|
{ |
340 |
– |
const double end_thresh = 5e-6; |
341 |
– |
PRICEMAT pmtx; |
197 |
|
MIGRATION *newmig; |
343 |
– |
double *src_rem, *dst_rem; |
344 |
– |
double total_rem = 1., move_amt; |
198 |
|
int i, j; |
199 |
|
/* check if exists already */ |
200 |
|
for (newmig = from_rbf->ejl; newmig != NULL; |
214 |
|
newmig = new_migration(from_rbf, to_rbf); |
215 |
|
if (run_subprocess()) |
216 |
|
return(newmig); /* child continues */ |
364 |
– |
price_routes(&pmtx, from_rbf, to_rbf); |
365 |
– |
src_rem = (double *)malloc(sizeof(double)*from_rbf->nrbf); |
366 |
– |
dst_rem = (double *)malloc(sizeof(double)*to_rbf->nrbf); |
367 |
– |
if ((src_rem == NULL) | (dst_rem == NULL)) { |
368 |
– |
fprintf(stderr, "%s: Out of memory in create_migration()\n", |
369 |
– |
progname); |
370 |
– |
exit(1); |
371 |
– |
} |
372 |
– |
/* starting quantities */ |
373 |
– |
memset(newmig->mtx, 0, sizeof(float)*from_rbf->nrbf*to_rbf->nrbf); |
374 |
– |
for (i = from_rbf->nrbf; i--; ) |
375 |
– |
src_rem[i] = rbf_volume(&from_rbf->rbfa[i]) / from_rbf->vtotal; |
376 |
– |
for (j = to_rbf->nrbf; j--; ) |
377 |
– |
dst_rem[j] = rbf_volume(&to_rbf->rbfa[j]) / to_rbf->vtotal; |
217 |
|
|
218 |
< |
do { /* move a bit at a time */ |
219 |
< |
move_amt = migration_step(newmig, src_rem, dst_rem, &pmtx); |
220 |
< |
total_rem -= move_amt; |
382 |
< |
} while ((total_rem > end_thresh) & (move_amt > 0)); |
218 |
> |
/* compute transport plan */ |
219 |
> |
compute_nDSFs(from_rbf, to_rbf); |
220 |
> |
plan_transport(newmig); |
221 |
|
|
222 |
|
for (i = from_rbf->nrbf; i--; ) { /* normalize final matrix */ |
223 |
|
double nf = rbf_volume(&from_rbf->rbfa[i]); |
227 |
|
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
228 |
|
} |
229 |
|
end_subprocess(); /* exit here if subprocess */ |
392 |
– |
free_routes(&pmtx); /* free working arrays */ |
393 |
– |
free(src_rem); |
394 |
– |
free(dst_rem); |
230 |
|
return(newmig); |
231 |
|
} |
232 |
|
|
316 |
|
ej1 = create_migration(tvert[0], edge->rbfv[1]); |
317 |
|
mesh_from_edge(ej0); |
318 |
|
mesh_from_edge(ej1); |
319 |
+ |
return; |
320 |
|
} |
321 |
< |
} else if (tvert[1] == NULL) { /* grow mesh on left */ |
321 |
> |
} |
322 |
> |
if (tvert[1] == NULL) { /* grow mesh on left */ |
323 |
|
tvert[1] = find_chull_vert(edge->rbfv[1], edge->rbfv[0]); |
324 |
|
if (tvert[1] != NULL) { |
325 |
|
if (tvert[1]->ord > edge->rbfv[0]->ord) |
340 |
|
static void |
341 |
|
check_normal_incidence(void) |
342 |
|
{ |
343 |
< |
static const FVECT norm_vec = {.0, .0, 1.}; |
343 |
> |
static FVECT norm_vec = {.0, .0, 1.}; |
344 |
|
const int saved_nprocs = nprocs; |
345 |
|
RBFNODE *near_rbf, *mir_rbf, *rbf; |
346 |
|
double bestd; |
390 |
|
memcpy(mir_rbf, near_rbf, n); |
391 |
|
mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */ |
392 |
|
mir_rbf->next = NULL; |
393 |
+ |
mir_rbf->ejl = NULL; |
394 |
|
rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y); |
395 |
|
nprocs = 1; /* compute migration matrix */ |
396 |
< |
if (mig_list != create_migration(mir_rbf, near_rbf)) |
396 |
> |
if (create_migration(mir_rbf, near_rbf) == NULL) |
397 |
|
exit(1); /* XXX should never happen! */ |
398 |
< |
/* interpolate normal dist. */ |
398 |
> |
norm_vec[2] = input_orient; /* interpolate normal dist. */ |
399 |
|
rbf = e_advect_rbf(mig_list, norm_vec, 2*near_rbf->nrbf); |
400 |
|
nprocs = saved_nprocs; /* final clean-up */ |
401 |
|
free(mir_rbf); |