1 
2 #include "mongoose_mex.hpp"
3 
4 using namespace Mongoose;
5 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])6 void mexFunction
7 (
8     int nargout,
9     mxArray *pargout [ ],
10     int nargin,
11     const mxArray *pargin [ ]
12 )
13 {
14     const char* usage = "Usage: [G_coarse, A_coarse] = coarsen(G, (O, A))";
15     if(nargout > 3 || nargin < 1 || nargin > 3) mexErrMsgTxt(usage);
16 
17     const mxArray *matGraph = pargin[0];
18     const mxArray *matOptions = (nargin >= 2 ? pargin[1] : NULL);
19     const mxArray *matNodeWeights = (nargin == 3 ? pargin[2] : NULL);
20 
21     /* Get the options from the MATLAB inputs. */
22     EdgeCut_Options *O = mex_get_options(matOptions);
23     if(!O)
24     {
25         mexErrMsgTxt("Unable to get Options struct");
26     }
27 
28     /* Get the graph from the MATLAB inputs. */
29     EdgeCutProblem *G = mex_get_graph(matGraph, matNodeWeights);
30 
31     if(!G)
32     {
33         O->~EdgeCut_Options();
34         mexErrMsgTxt("Unable to get Graph struct");
35     }
36 
37     G->initialize(O);
38     match(G, O);
39     EdgeCutProblem *G_coarse = coarsen(G, O);
40 
41     cs *G_matrix = cs_spalloc(G_coarse->n, G_coarse->n, G_coarse->nz, 0, 0);
42     G_matrix->i = G_coarse->i;
43     G_matrix->p = G_coarse->p;
44     G_matrix->x = G_coarse->x;
45 
46     /* Copy the coarsened graph back to MATLAB. */
47     pargout[0] = cs_mex_put_sparse(&G_matrix);
48     gp_mex_put_double(G_coarse->n, G_coarse->w, &pargout[1]);
49     pargout[2] = gp_mex_put_int(G->matchmap, G->n, 1, 0);
50 
51     /* Cleanup */
52     G->~EdgeCutProblem();
53     G_coarse->i = NULL;
54     G_coarse->p = NULL;
55     G_coarse->x = NULL;
56     G_coarse->w = NULL;
57     G_coarse->matchmap = NULL;
58     G_coarse->~EdgeCutProblem();
59     O->~EdgeCut_Options();
60 }
61