1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdftrans.cpp,v 2.3 2014/03/29 21:51:59 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Compute mass transport plan for RBF lobes |
6 |
*/ |
7 |
|
8 |
#define _USE_MATH_DEFINES |
9 |
#include <stdio.h> |
10 |
#include <string.h> |
11 |
#include "transportSimplex.h" |
12 |
#include "bsdfrep.h" |
13 |
|
14 |
using namespace t_simplex; |
15 |
using namespace std; |
16 |
|
17 |
/* Compute mass transport plan (internal call) */ |
18 |
extern "C" void |
19 |
plan_transport(MIGRATION *mig) |
20 |
{ |
21 |
double *src; |
22 |
double *dst; |
23 |
TsFlow *flow; |
24 |
|
25 |
src = new double[mtx_nrows(mig)]; |
26 |
dst = new double[mtx_ncols(mig)]; |
27 |
|
28 |
TsSignature<RBFVAL> srcSig(mtx_nrows(mig), mig->rbfv[0]->rbfa, src); |
29 |
TsSignature<RBFVAL> dstSig(mtx_ncols(mig), mig->rbfv[1]->rbfa, dst); |
30 |
flow = new TsFlow[mtx_nrows(mig)+mtx_ncols(mig)-1]; |
31 |
int n; |
32 |
/* clear flow matrix */ |
33 |
memset(mig->mtx, 0, sizeof(float)*mtx_nrows(mig)*mtx_ncols(mig)); |
34 |
/* set supplies & demands */ |
35 |
for (n = mtx_nrows(mig); n--; ) |
36 |
src[n] = rbf_volume(&mig->rbfv[0]->rbfa[n]) / |
37 |
mig->rbfv[0]->vtotal; |
38 |
for (n = mtx_ncols(mig); n--; ) |
39 |
dst[n] = rbf_volume(&mig->rbfv[1]->rbfa[n]) / |
40 |
mig->rbfv[1]->vtotal; |
41 |
|
42 |
n = 0; /* minimize EMD */ |
43 |
try { |
44 |
transportSimplex(&srcSig, &dstSig, &lobe_distance, flow, &n); |
45 |
} catch (...) { |
46 |
fprintf(stderr, "%s: caught exception from transportSimplex()!\n", |
47 |
progname); |
48 |
exit(1); |
49 |
} |
50 |
if (n > mtx_nrows(mig)+mtx_ncols(mig)-1) { |
51 |
fprintf(stderr, "%s: signature overflow in plan_transport()!\n", |
52 |
progname); |
53 |
exit(1); |
54 |
} |
55 |
while (n-- > 0) /* assign sparse matrix */ |
56 |
mtx_coef(mig, flow[n].from, flow[n].to) = flow[n].amount; |
57 |
|
58 |
delete[] src; |
59 |
delete[] dst; |
60 |
delete[] flow; |
61 |
} |