/* * $Id: spm_mesh_utils.c 6694 2016-01-26 17:09:11Z guillaume $ * Guillaume Flandin */ #include #include "mex.h" /* --- Compute geodesic distance using Dijkstra algorithm --- */ void dijkstra(double *nghbr, double *dnghbr, int nv, int nb, int *source, int ns, double maxdist, double *dist) { int i, nQ, u = 0, v, Qu = 0; double dmin, alt; int *Q = mxMalloc(nv*sizeof(int)); for (i=0;i maxdist) break; Q[Qu] = Q[--nQ]; for (i=0;i 1) mexErrMsgTxt("Too many input arguments."); if ((!mxIsStruct(prhs[0])) || (mxIsClass(prhs[0],"gifti"))) mexErrMsgTxt("First argument must be a patch structure."); array = mxGetField(prhs[0], 0, "vertices"); if (!mxIsDouble(array)) mexErrMsgTxt("Vertices have to be stored as double."); nv = mxGetM(array); v = mxGetPr(array); array = mxGetField(prhs[0], 0, "faces"); if (!mxIsDouble(array)) mexErrMsgTxt("Faces have to be stored as double."); nf = mxGetM(array); f = mxGetPr(array); for (i=0,vol=0;i 1) mexErrMsgTxt("Too many input arguments."); if (!mxIsSparse(prhs[0])) mexErrMsgTxt("First argument must be a sparse array."); n = mxGetM(prhs[0]); plhs[0] = mxCreateDoubleMatrix(n, d, mxREAL); N = mxGetPr(plhs[0]); if (nlhs > 1) { plhs[1] = mxCreateDoubleMatrix(n, d, mxREAL); D = mxGetPr(plhs[1]); } Ir = mxGetIr(prhs[0]); Jc = mxGetJc(prhs[0]); pr = mxGetPr(prhs[0]); for (i=0;i d) { N = mxRealloc(N, n*k*sizeof(double)); for (j=d;j 1) { D = mxRealloc(D, n*k*sizeof(double)); for (j=d;j 1) for (j=0;j 1) { mxSetPr(plhs[1],D); mxSetN(plhs[1],d); } } /* Gateway Function for Dijkstra */ void mexFunctionDijkstra(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize nv, nb, ns; mwIndex i; int *source = NULL; double distmax; if (nrhs < 4) mexErrMsgTxt("Not enough input arguments."); if (nrhs > 4) mexErrMsgTxt("Too many input arguments."); if (!mxIsNumeric(prhs[0])) mexErrMsgTxt("First argument must be a neighbour array."); if (!mxIsNumeric(prhs[1])) mexErrMsgTxt("Second argument must be a distance array."); nv = mxGetM(prhs[0]); nb = mxGetN(prhs[0]); ns = mxGetNumberOfElements(prhs[2]); source = mxMalloc(ns*sizeof(int)); for (i=0;i=nv)) mexErrMsgTxt("Invalid vertex index."); } distmax = mxGetScalar(prhs[3]); plhs[0] = mxCreateDoubleMatrix(nv, 1, mxREAL); dijkstra(mxGetPr(prhs[0]), mxGetPr(prhs[1]), nv, nb, source, ns, distmax, mxGetPr(plhs[0])); mxFree(source); } /* Main Gateway Function */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *action = NULL; mwSize strlgh; if (nrhs < 2) mexErrMsgTxt("Not enough input arguments."); if (!mxIsChar(prhs[0])) mexErrMsgTxt("First argument must be an action string."); strlgh = (mxGetM(prhs[0]) * mxGetN(prhs[0]) * sizeof(char)) + 1; action = mxCalloc(strlgh, sizeof(char)); mxGetString(prhs[0], action, strlgh); if (!strcmp(action,"dijkstra")) { mexFunctionDijkstra(nlhs,plhs,nrhs-1,&prhs[1]); } else if (!strcmp(action,"neighbours")) { mexFunctionNeighbours(nlhs,plhs,nrhs-1,&prhs[1]); } else if (!strcmp(action,"volume")) { mexFunctionVolume(nlhs,plhs,nrhs-1,&prhs[1]); } else { mexErrMsgTxt("Unknown action."); } mxFree(action); }