Daniel@0: /* This is based on Daniel@0: http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch04cr12.shtml Daniel@0: Daniel@0: See rectintSparse.m for the matlab version of this code. Daniel@0: Daniel@0: */ Daniel@0: Daniel@0: #include /* Needed for the ceil() prototype. */ Daniel@0: #include "mex.h" Daniel@0: #include Daniel@0: Daniel@0: /* If you are using a compiler that equates NaN to be zero, you Daniel@0: * must compile this example using the flag -DNAN_EQUALS_ZERO. Daniel@0: * For example: Daniel@0: * Daniel@0: * mex -DNAN_EQUALS_ZERO fulltosparse.c Daniel@0: * Daniel@0: * This will correctly define the IsNonZero macro for your C Daniel@0: * compiler. Daniel@0: */ Daniel@0: Daniel@0: #if defined(NAN_EQUALS_ZERO) Daniel@0: #define IsNonZero(d) ((d) != 0.0 || mxIsNaN(d)) Daniel@0: #else Daniel@0: #define IsNonZero(d) ((d) != 0.0) Daniel@0: #endif Daniel@0: Daniel@0: #define MAX(x,y) ((x)>(y) ? (x) : (y)) Daniel@0: #define MIN(x,y) ((x)<(y) ? (x) : (y)) Daniel@0: Daniel@0: void mexFunction( Daniel@0: int nlhs, mxArray *plhs[], Daniel@0: int nrhs, const mxArray *prhs[] Daniel@0: ) Daniel@0: { Daniel@0: /* Declare variables. */ Daniel@0: int j,k,m,n,nzmax,*irs,*jcs, *irs2, *jcs2; Daniel@0: double *overlap, *overlap2, tmp, areaA, areaB; Daniel@0: double percent_sparse; Daniel@0: double *leftA, *rightA, *topA, *bottomA; Daniel@0: double *leftB, *rightB, *topB, *bottomB; Daniel@0: double *verbose; Daniel@0: Daniel@0: /* Get the size and pointers to input data. */ Daniel@0: m = MAX(mxGetM(prhs[0]), mxGetN(prhs[0])); Daniel@0: n = MAX(mxGetM(prhs[4]), mxGetN(prhs[4])); Daniel@0: /* printf("A=%d, B=%d\n", m, n); */ Daniel@0: Daniel@0: leftA = mxGetPr(prhs[0]); Daniel@0: rightA = mxGetPr(prhs[1]); Daniel@0: topA = mxGetPr(prhs[2]); Daniel@0: bottomA = mxGetPr(prhs[3]); Daniel@0: Daniel@0: leftB = mxGetPr(prhs[4]); Daniel@0: rightB = mxGetPr(prhs[5]); Daniel@0: topB = mxGetPr(prhs[6]); Daniel@0: bottomB = mxGetPr(prhs[7]); Daniel@0: Daniel@0: verbose = mxGetPr(prhs[8]); Daniel@0: Daniel@0: /* Allocate space for sparse matrix. Daniel@0: * NOTE: Assume at most 20% of the data is sparse. Use ceil Daniel@0: * to cause it to round up. Daniel@0: */ Daniel@0: Daniel@0: percent_sparse = 0.01; Daniel@0: nzmax = (int)ceil((double)m*(double)n*percent_sparse); Daniel@0: Daniel@0: plhs[0] = mxCreateSparse(m,n,nzmax,0); Daniel@0: overlap = mxGetPr(plhs[0]); Daniel@0: irs = mxGetIr(plhs[0]); Daniel@0: jcs = mxGetJc(plhs[0]); Daniel@0: Daniel@0: plhs[1] = mxCreateSparse(m,n,nzmax,0); Daniel@0: overlap2 = mxGetPr(plhs[1]); Daniel@0: irs2 = mxGetIr(plhs[1]); Daniel@0: jcs2 = mxGetJc(plhs[1]); Daniel@0: Daniel@0: Daniel@0: /* Assign nonzeros. */ Daniel@0: k = 0; Daniel@0: for (j = 0; (j < n); j++) { Daniel@0: int i; Daniel@0: jcs[j] = k; Daniel@0: jcs2[j] = k; Daniel@0: for (i = 0; (i < m); i++) { Daniel@0: tmp = (MAX(0, MIN(rightA[i], rightB[j]) - MAX(leftA[i], leftB[j]) )) * Daniel@0: (MAX(0, MIN(topA[i], topB[j]) - MAX(bottomA[i], bottomB[j]) )); Daniel@0: Daniel@0: if (*verbose) { Daniel@0: printf("j=%d,i=%d,tmp=%5.3f\n", j,i,tmp); Daniel@0: } Daniel@0: Daniel@0: if (IsNonZero(tmp)) { Daniel@0: Daniel@0: /* Check to see if non-zero element will fit in Daniel@0: * allocated output array. If not, increase Daniel@0: * percent_sparse by 20%, recalculate nzmax, and augment Daniel@0: * the sparse array. Daniel@0: */ Daniel@0: if (k >= nzmax) { Daniel@0: int oldnzmax = nzmax; Daniel@0: percent_sparse += 0.2; Daniel@0: nzmax = (int)ceil((double)m*(double)n*percent_sparse); Daniel@0: Daniel@0: /* Make sure nzmax increases atleast by 1. */ Daniel@0: if (oldnzmax == nzmax) Daniel@0: nzmax++; Daniel@0: printf("reallocating from %d to %d\n", oldnzmax, nzmax); Daniel@0: Daniel@0: mxSetNzmax(plhs[0], nzmax); Daniel@0: mxSetPr(plhs[0], mxRealloc(overlap, nzmax*sizeof(double))); Daniel@0: mxSetIr(plhs[0], mxRealloc(irs, nzmax*sizeof(int))); Daniel@0: overlap = mxGetPr(plhs[0]); Daniel@0: irs = mxGetIr(plhs[0]); Daniel@0: Daniel@0: mxSetNzmax(plhs[1], nzmax); Daniel@0: mxSetPr(plhs[1], mxRealloc(overlap2, nzmax*sizeof(double))); Daniel@0: mxSetIr(plhs[1], mxRealloc(irs2, nzmax*sizeof(int))); Daniel@0: overlap2 = mxGetPr(plhs[1]); Daniel@0: irs2 = mxGetIr(plhs[1]); Daniel@0: } Daniel@0: Daniel@0: overlap[k] = tmp; Daniel@0: irs[k] = i; Daniel@0: Daniel@0: areaA = (rightA[i]-leftA[i])*(topA[i]-bottomA[i]); Daniel@0: areaB = (rightB[j]-leftB[j])*(topB[j]-bottomB[j]); Daniel@0: overlap2[k] = MIN(tmp/areaA, tmp/areaB); Daniel@0: irs2[k] = i; Daniel@0: Daniel@0: k++; Daniel@0: } /* IsNonZero */ Daniel@0: } /* for i */ Daniel@0: } Daniel@0: jcs[n] = k; Daniel@0: jcs2[n] = k; Daniel@0: Daniel@0: } Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: