/** * main entry * * Input * 1: the reference points to construct the kd-tree (d x n double matrix) * 2: the query points (d x nq double matrix) * 3: the option struct * Output * 1: the nearest neighbor index matrix (k x nq int32 matrix) * 2: the distance matrix (k x nq double matrix) * Here, * d - the point dimension * n - the number of reference points * nq - the number of query points * k - the number of maximum neighbors for each point * Note, * The responsibility of checking the validity of the input arguments * is with the invoker. The annsearch.m does the checking. * The mex-function in itself does not conduct the checking again. */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // take inputs const mxArray *mxRefPts = prhs[0]; const mxArray *mxQuery = prhs[1]; const mxArray *mxOpts = prhs[2]; // parse options AnnBuildOptions opts_build; AnnSearchOptions opts_search; parse_tree_building_options(mxOpts, opts_build); parse_search_options(mxOpts, opts_search); // construct the tree int d = 0; int n = 0; ANNpointArray pts = createPointArray(mxRefPts, d, n); ANNkd_tree *kd_tree = createKdTree(d, n, pts, opts_build); // perform the search mxArray *mxInds = NULL; mxArray *mxDists = NULL; performAnnkSearch(kd_tree, mxQuery, opts_search, mxInds, mxDists); // release the kd-tree delete kd_tree; annDeallocPts(pts); annClose(); // set outputs plhs[0] = mxInds; plhs[1] = mxDists; }
// cd L:\ann_mwrapper; Untitled2 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize dims[3]; char command[1024]; int strlen = mxGetN(prhs[0]); strlen = strlen < mxGetM(prhs[0]) ? mxGetN(prhs[0]) : strlen; strlen += 1; mxGetString (prhs[0], command, strlen); // Initialize tree if (nrhs > 0 && !strcmp(command, "createKdTree")) { const mxArray *mxRefPts = prhs[1]; const mxArray *mxOpts = prhs[2]; // parse options AnnBuildOptions opts_build; parse_tree_building_options(mxOpts, opts_build); // construct the tree int d = 0; int n = 0; ANNpointArray *pts = createPointArray(mxRefPts, d, n); ANNkd_tree *kd_tree = createKdTree(d, n, pts[0], opts_build); //mexPrintf("%lld\n", kd_tree); //mexPrintf("dim = %d\n", kd_tree->theDim()); dims[0] = 2; dims[1] = 1; plhs[0] = mxCreateNumericArray(2,dims,mxUINT64_CLASS,mxREAL); ((unsigned long long *)(mxGetData( plhs[0])))[0] = (unsigned long long)kd_tree; ((unsigned long long *)(mxGetData( plhs[0])))[1] = (unsigned long long)pts; // mexPrintf("Creating:\n"); // mexPrintf("Pts: "); // for (int i =0;i<MEX_DEBUG_WND;++i){ // mexPrintf("%x",pts[i]); // } // mexPrintf("\n"); // mexPrintf("kdTree: "); // for (int i =0;i<10;++i){ // mexPrintf("%x",((char *)kd_tree)[i]); // } // mexPrintf("\n"); } // Search else if (nrhs > 0 && !strcmp(command, "performAnnkSearch")) { // take inputs const mxArray *mxQuery = prhs[2]; const mxArray *mxOpts = prhs[3]; // parse options AnnSearchOptions opts_search; parse_search_options(mxOpts, opts_search); // get pointer to tree ANNkd_tree *kd_tree = (ANNkd_tree *)(((unsigned long long *)mxGetData( prhs[1]))[0]); // mexPrintf("%lld\n", kd_tree); // mexPrintf("dim = %d\n", kd_tree->theDim()); // perform the search mxArray *mxInds = NULL; mxArray *mxDists = NULL; // mexPrintf("Pts: "); // for (int i =0;i<MEX_DEBUG_WND;++i){ // mexPrintf("%x",pts[i]); // } // mexPrintf("\n"); // mexPrintf("kdTree: "); // for (int i =0;i<10;++i){ // mexPrintf("%x",((char *)kd_tree)[i]); // } // mexPrintf("\n"); // performAnnkSearch(kd_tree, mxQuery, opts_search, mxInds, mxDists); // set outputs plhs[0] = mxInds; plhs[1] = mxDists; } // Deinitialize tree else if (nrhs > 0 && !strcmp(command, "deleteKdTree")) { ANNkd_tree *kd_tree = (ANNkd_tree *)(((unsigned long long *)mxGetData( prhs[1]))[0]); ANNpointArray *pts = (ANNpointArray *)(((unsigned long long *)mxGetData( prhs[1]))[1]); // mexPrintf("Deleting:\n"); // mexPrintf("Pts: "); // for (int i =0;i<MEX_DEBUG_WND;++i){ // mexPrintf("%x",pts[i]); // } // mexPrintf("\n"); // mexPrintf("kdTree: "); // for (int i =0;i<10;++i){ // mexPrintf("%x",((char *)kd_tree)[i]); // } // mexPrintf("\n"); // release the kd-tree delete kd_tree; annDeallocPts(pts[0]); delete pts; } // Close else if (nrhs > 0 && !strcmp(command, "annClose")) { annClose(); } else { mexPrintf("INVALID COMMAND %s\n", command); return; } }