Repository navigation
Expand file tree
/
Copy pathgenOp.cpp
More file actions
64 lines (54 loc) · 1.46 KB
/
Copy pathgenOp.cpp
File metadata and controls
64 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <math.h>
#include <matrix.h>
#include <mex.h>
#include <Eigen/Dense>
#include <Eigen/Sparse>
typedef Eigen::Triplet<double, int> ETriplet;
typedef Eigen::SparseMatrix<double> SpMat;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Input
double* G = mxGetPr(prhs[0]);
mxLogical* mask = mxGetLogicals(prhs[1]);
//figure out dimensions
const mwSize* dims = mxGetDimensions(prhs[0]);
const int S = (int) dims[0];
const int size = S * S;
const int neighborhood[13] = {0, 1, -1, S, -S, -S-1, S-1, S+1, -S+1, 2, -2, 2*S, -2*S}; // Neighborhood indices
const double kernel[13] = {20.0, -8.0, -8.0, -8.0, -8.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0};
Eigen::VectorXd b(size);
Eigen::VectorXd x(size);
std::vector<ETriplet> tripletList;
//tripletList.reserve(n / 2); TODO
for (int i = 0; i < size; i++)
{
if (mask[i])
{
tripletList.push_back(ETriplet(i, i, 1.0));
b[i] = G[i];
}
else
{
for (int j = 0; j < 13; j++)
{
tripletList.push_back(ETriplet(i, i + neighborhood[j], kernel[j]));
}
b[i] = 0.0;
}
}
// Sparse init
SpMat L(size, size);
L.setFromTriplets(tripletList.begin(), tripletList.end());
// Solve
Eigen::SparseLU<SpMat> llt;
llt.compute(L);
x = llt.solve(b);
// Output
plhs[0] = mxCreateDoubleMatrix(S, S, mxREAL);
double* Gs = mxGetPr(plhs[0]);
// Extract
for (int i = 0; i < size; i++)
{
Gs[i] = x[i];
}
}