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) */ |
139 |
|
|
140 |
|
#endif /* ! _WIN32 */ |
141 |
|
|
142 |
+ |
/* Compute normalized distribution scattering functions for comparison */ |
143 |
+ |
static void |
144 |
+ |
compute_nDSFs(const RBFNODE *rbf0, const RBFNODE *rbf1) |
145 |
+ |
{ |
146 |
+ |
const double nf0 = (GRIDRES*GRIDRES) / rbf0->vtotal; |
147 |
+ |
const double nf1 = (GRIDRES*GRIDRES) / rbf1->vtotal; |
148 |
+ |
int x, y; |
149 |
+ |
FVECT dv; |
150 |
+ |
|
151 |
+ |
for (x = GRIDRES; x--; ) |
152 |
+ |
for (y = GRIDRES; y--; ) { |
153 |
+ |
ovec_from_pos(dv, x, y); /* cube root (brightness) */ |
154 |
+ |
dsf_grid[x][y].val[0] = pow(nf0*eval_rbfrep(rbf0, dv), .3333); |
155 |
+ |
dsf_grid[x][y].val[1] = pow(nf1*eval_rbfrep(rbf1, dv), .3333); |
156 |
+ |
} |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
/* Compute neighborhood distance-squared (dissimilarity) */ |
160 |
+ |
static double |
161 |
+ |
neighborhood_dist2(int x0, int y0, int x1, int y1) |
162 |
+ |
{ |
163 |
+ |
int rad = GRIDRES>>5; |
164 |
+ |
double sum2 = 0.; |
165 |
+ |
double d; |
166 |
+ |
int p[4]; |
167 |
+ |
int i, j; |
168 |
+ |
/* check radius */ |
169 |
+ |
p[0] = x0; p[1] = y0; p[2] = x1; p[3] = y1; |
170 |
+ |
for (i = 4; i--; ) { |
171 |
+ |
if (p[i] < rad) rad = p[i]; |
172 |
+ |
if (GRIDRES-1-p[i] < rad) rad = GRIDRES-1-p[i]; |
173 |
+ |
} |
174 |
+ |
for (i = -rad; i <= rad; i++) |
175 |
+ |
for (j = -rad; j <= rad; j++) { |
176 |
+ |
d = dsf_grid[x0+i][y0+j].val[0] - |
177 |
+ |
dsf_grid[x1+i][y1+j].val[1]; |
178 |
+ |
sum2 += d*d; |
179 |
+ |
} |
180 |
+ |
return(sum2 / (4*rad*(rad+1) + 1)); |
181 |
+ |
} |
182 |
+ |
|
183 |
|
/* Comparison routine needed for sorting price row */ |
184 |
|
static int |
185 |
|
msrt_cmp(void *b, const void *p1, const void *p2) |
200 |
|
FVECT *vto = (FVECT *)malloc(sizeof(FVECT) * to_rbf->nrbf); |
201 |
|
int i, j; |
202 |
|
|
203 |
+ |
compute_nDSFs(from_rbf, to_rbf); |
204 |
|
pm->nrows = from_rbf->nrbf; |
205 |
|
pm->ncols = to_rbf->nrbf; |
206 |
|
pm->price = (float *)malloc(sizeof(float) * pm->nrows*pm->ncols); |
222 |
|
pm->prow = pricerow(pm,i); |
223 |
|
srow = psortrow(pm,i); |
224 |
|
for (j = to_rbf->nrbf; j--; ) { |
225 |
< |
double d; /* quadratic cost function */ |
225 |
> |
double d; /* quadratic cost function */ |
226 |
|
d = Acos(DOT(vfrom, vto[j])); |
227 |
|
pm->prow[j] = d*d; |
228 |
|
d = R2ANG(to_rbf->rbfa[j].crad) - from_ang; |
229 |
< |
pm->prow[j] += d*d; |
229 |
> |
pm->prow[j] += d*d; |
230 |
> |
/* neighborhood difference */ |
231 |
> |
pm->prow[j] += NEIGH_FACT2 * neighborhood_dist2( |
232 |
> |
from_rbf->rbfa[i].gx, from_rbf->rbfa[i].gy, |
233 |
> |
to_rbf->rbfa[j].gx, to_rbf->rbfa[j].gy ); |
234 |
|
srow[j] = j; |
235 |
|
} |
236 |
|
qsort_r(srow, pm->ncols, sizeof(short), pm, &msrt_cmp); |
383 |
|
#undef discount |
384 |
|
} |
385 |
|
|
386 |
+ |
#ifdef DUMP_MATRIX |
387 |
+ |
/* Dump transport plan and corresponding price matrix to a text file */ |
388 |
+ |
static void |
389 |
+ |
dump_matrix(const MIGRATION *me, const PRICEMAT *pm) |
390 |
+ |
{ |
391 |
+ |
char fname[256]; |
392 |
+ |
FILE *fp; |
393 |
+ |
int i, j; |
394 |
+ |
|
395 |
+ |
sprintf(fname, "edge_%d-%d.txt", me->rbfv[0]->ord, me->rbfv[1]->ord); |
396 |
+ |
if ((fp = fopen(fname, "w")) == NULL) |
397 |
+ |
return; |
398 |
+ |
for (j = 0; j < 2; j++) { |
399 |
+ |
fprintf(fp, "Available from %d source RBF lobes in node %d:\n", |
400 |
+ |
me->rbfv[j]->nrbf, me->rbfv[j]->ord); |
401 |
+ |
for (i = 0; i < me->rbfv[j]->nrbf; i++) |
402 |
+ |
fprintf(fp, " %.4e", rbf_volume(&me->rbfv[j]->rbfa[i]) / |
403 |
+ |
me->rbfv[j]->vtotal); |
404 |
+ |
fputc('\n', fp); |
405 |
+ |
} |
406 |
+ |
fprintf(fp, "Price (quadratic distance metric) matrix:\n"); |
407 |
+ |
for (i = 0; i < pm->nrows; i++) { |
408 |
+ |
for (j = 0; j < pm->ncols; j++) |
409 |
+ |
fprintf(fp, " %.4e", pricerow(pm,i)[j]); |
410 |
+ |
fputc('\n', fp); |
411 |
+ |
} |
412 |
+ |
fprintf(fp, "Solution matrix (transport plan):\n"); |
413 |
+ |
for (i = 0; i < mtx_nrows(me); i++) { |
414 |
+ |
for (j = 0; j < mtx_ncols(me); j++) |
415 |
+ |
fprintf(fp, " %.4e", mtx_coef(me,i,j)); |
416 |
+ |
fputc('\n', fp); |
417 |
+ |
} |
418 |
+ |
fclose(fp); |
419 |
+ |
} |
420 |
+ |
#endif |
421 |
+ |
|
422 |
|
/* Compute and insert migration along directed edge (may fork child) */ |
423 |
|
static MIGRATION * |
424 |
|
create_migration(RBFNODE *from_rbf, RBFNODE *to_rbf) |
474 |
|
for (j = to_rbf->nrbf; j--; ) |
475 |
|
mtx_coef(newmig,i,j) *= nf; /* row now sums to 1.0 */ |
476 |
|
} |
477 |
+ |
#ifdef DUMP_MATRIX |
478 |
+ |
dump_matrix(newmig, &pmtx); |
479 |
+ |
#endif |
480 |
|
end_subprocess(); /* exit here if subprocess */ |
481 |
|
free_routes(&pmtx); /* free working arrays */ |
482 |
|
free(src_rem); |
592 |
|
static void |
593 |
|
check_normal_incidence(void) |
594 |
|
{ |
595 |
< |
static const FVECT norm_vec = {.0, .0, 1.}; |
595 |
> |
static FVECT norm_vec = {.0, .0, 1.}; |
596 |
|
const int saved_nprocs = nprocs; |
597 |
|
RBFNODE *near_rbf, *mir_rbf, *rbf; |
598 |
|
double bestd; |
642 |
|
memcpy(mir_rbf, near_rbf, n); |
643 |
|
mir_rbf->ord = near_rbf->ord - 1; /* not used, I think */ |
644 |
|
mir_rbf->next = NULL; |
645 |
+ |
mir_rbf->ejl = NULL; |
646 |
|
rev_rbf_symmetry(mir_rbf, MIRROR_X|MIRROR_Y); |
647 |
|
nprocs = 1; /* compute migration matrix */ |
648 |
< |
if (mig_list != create_migration(mir_rbf, near_rbf)) |
648 |
> |
if (create_migration(mir_rbf, near_rbf) == NULL) |
649 |
|
exit(1); /* XXX should never happen! */ |
650 |
< |
/* interpolate normal dist. */ |
650 |
> |
norm_vec[2] = input_orient; /* interpolate normal dist. */ |
651 |
|
rbf = e_advect_rbf(mig_list, norm_vec, 2*near_rbf->nrbf); |
652 |
|
nprocs = saved_nprocs; /* final clean-up */ |
653 |
|
free(mir_rbf); |