| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: bsdftrans.cpp,v 2.2 2014/03/26 22:29:08 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[mtx_nrows(mig)]; |
| 22 |
double dst[mtx_ncols(mig)]; |
| 23 |
TsSignature<RBFVAL> srcSig(mtx_nrows(mig), mig->rbfv[0]->rbfa, src); |
| 24 |
TsSignature<RBFVAL> dstSig(mtx_ncols(mig), mig->rbfv[1]->rbfa, dst); |
| 25 |
TsFlow flow[mtx_nrows(mig)+mtx_ncols(mig)-1]; |
| 26 |
int n; |
| 27 |
/* clear flow matrix */ |
| 28 |
memset(mig->mtx, 0, sizeof(float)*mtx_nrows(mig)*mtx_ncols(mig)); |
| 29 |
/* set supplies & demands */ |
| 30 |
for (n = mtx_nrows(mig); n--; ) |
| 31 |
src[n] = rbf_volume(&mig->rbfv[0]->rbfa[n]) / |
| 32 |
mig->rbfv[0]->vtotal; |
| 33 |
for (n = mtx_ncols(mig); n--; ) |
| 34 |
dst[n] = rbf_volume(&mig->rbfv[1]->rbfa[n]) / |
| 35 |
mig->rbfv[1]->vtotal; |
| 36 |
|
| 37 |
n = 0; /* minimize EMD */ |
| 38 |
try { |
| 39 |
transportSimplex(&srcSig, &dstSig, &lobe_distance, flow, &n); |
| 40 |
} catch (...) { |
| 41 |
fprintf(stderr, "%s: caught exception from transportSimplex()!\n", |
| 42 |
progname); |
| 43 |
exit(1); |
| 44 |
} |
| 45 |
if (n > mtx_nrows(mig)+mtx_ncols(mig)-1) { |
| 46 |
fprintf(stderr, "%s: signature overflow in plan_transport()!\n", |
| 47 |
progname); |
| 48 |
exit(1); |
| 49 |
} |
| 50 |
while (n-- > 0) /* assign sparse matrix */ |
| 51 |
mtx_coef(mig, flow[n].from, flow[n].to) = flow[n].amount; |
| 52 |
} |