#include "mex.h" #include #include #include void pad(double *pm, double *wmap, mwSize dim[3], double *krnl, mwSize kdim[3], double *opm, double *owmap); int getindex(mwSignedIndex i, mwSignedIndex j, mwSignedIndex k, mwSize dim[3]); void pad(double *pm, double *wmap, mwSize dim[3], double *krnl, mwSize kdim[3], double *opm, double *owmap) { mwIndex i=0, j=0, k=0; mwIndex ki=0, kj=0, kk=0; int n = 0; int ndx=0, kndx=0; double p = 0.0; for (i=0; i -1) { if (wmap[kndx]) { p += krnl[getindex(ki,kj,kk,kdim)] * pm[kndx]; n++; } } } } } if (n) { opm[ndx] = p/n; owmap[ndx] = 1; } else { opm[ndx]=pm[ndx]; owmap[ndx]=wmap[ndx]; } } else { opm[ndx]=pm[ndx]; owmap[ndx]=wmap[ndx]; } } } } return; } /* Utility function that returns index into */ /* 1D array with range checking. */ int getindex(mwSignedIndex i, mwSignedIndex j, mwSignedIndex k, mwSize dim[3]) { if ((i<0) | (i>(dim[0]-1)) | (j<0) | (j>(dim[1]-1)) | (k<0) | (k>(dim[2]-1))) return(-1); else return(k*dim[0]*dim[1]+j*dim[0]+i); } /* Gateway function with error check. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize ndim, wmap_ndim, krn_ndim; mwIndex n, i; const mwSize *cdim = NULL, *wmap_cdim = NULL, *krn_cdim = NULL; mwSize dim[3], kdim[3]; double *pm = NULL; double *wmap = NULL; double *opm = NULL; double *owmap = NULL; double *krnl = NULL; if (nrhs == 0) mexErrMsgTxt("usage: [pm,wmap] = pm_pad(pm,wmap,kernel)"); if (nrhs != 3) mexErrMsgTxt("pm_pad: 3 input arguments required"); if (nlhs != 2) mexErrMsgTxt("pm_pad: 2 output argument required"); /* Get phase map. */ if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) || mxIsSparse(prhs[0]) || !mxIsDouble(prhs[0])) { mexErrMsgTxt("pm_pad: pm must be numeric, real, full and double"); } ndim = mxGetNumberOfDimensions(prhs[0]); if ((ndim < 2) | (ndim > 3)) { mexErrMsgTxt("pm_pad: pm must be 2 or 3-dimensional"); } cdim = mxGetDimensions(prhs[0]); pm = mxGetPr(prhs[0]); /* Get wrap-map. */ if (!mxIsNumeric(prhs[1]) || mxIsComplex(prhs[1]) || mxIsSparse(prhs[1]) || !mxIsDouble(prhs[1])) { mexErrMsgTxt("pm_pad: wmap must be numeric, real, full and double"); } wmap_ndim = mxGetNumberOfDimensions(prhs[1]); if (wmap_ndim != ndim) { mexErrMsgTxt("pm_pad: pm and wmap must have same dimensionality"); } wmap_cdim = mxGetDimensions(prhs[1]); for (i=0; i