annotate _FullBNT/KPMtools/rectintSparseLoopC.c @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 /* This is based on
matthiasm@8 2 http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/ch04cr12.shtml
matthiasm@8 3
matthiasm@8 4 See rectintSparse.m for the matlab version of this code.
matthiasm@8 5
matthiasm@8 6 */
matthiasm@8 7
matthiasm@8 8 #include <math.h> /* Needed for the ceil() prototype. */
matthiasm@8 9 #include "mex.h"
matthiasm@8 10 #include <stdio.h>
matthiasm@8 11
matthiasm@8 12 /* If you are using a compiler that equates NaN to be zero, you
matthiasm@8 13 * must compile this example using the flag -DNAN_EQUALS_ZERO.
matthiasm@8 14 * For example:
matthiasm@8 15 *
matthiasm@8 16 * mex -DNAN_EQUALS_ZERO fulltosparse.c
matthiasm@8 17 *
matthiasm@8 18 * This will correctly define the IsNonZero macro for your C
matthiasm@8 19 * compiler.
matthiasm@8 20 */
matthiasm@8 21
matthiasm@8 22 #if defined(NAN_EQUALS_ZERO)
matthiasm@8 23 #define IsNonZero(d) ((d) != 0.0 || mxIsNaN(d))
matthiasm@8 24 #else
matthiasm@8 25 #define IsNonZero(d) ((d) != 0.0)
matthiasm@8 26 #endif
matthiasm@8 27
matthiasm@8 28 #define MAX(x,y) ((x)>(y) ? (x) : (y))
matthiasm@8 29 #define MIN(x,y) ((x)<(y) ? (x) : (y))
matthiasm@8 30
matthiasm@8 31 void mexFunction(
matthiasm@8 32 int nlhs, mxArray *plhs[],
matthiasm@8 33 int nrhs, const mxArray *prhs[]
matthiasm@8 34 )
matthiasm@8 35 {
matthiasm@8 36 /* Declare variables. */
matthiasm@8 37 int j,k,m,n,nzmax,*irs,*jcs, *irs2, *jcs2;
matthiasm@8 38 double *overlap, *overlap2, tmp, areaA, areaB;
matthiasm@8 39 double percent_sparse;
matthiasm@8 40 double *leftA, *rightA, *topA, *bottomA;
matthiasm@8 41 double *leftB, *rightB, *topB, *bottomB;
matthiasm@8 42 double *verbose;
matthiasm@8 43
matthiasm@8 44 /* Get the size and pointers to input data. */
matthiasm@8 45 m = MAX(mxGetM(prhs[0]), mxGetN(prhs[0]));
matthiasm@8 46 n = MAX(mxGetM(prhs[4]), mxGetN(prhs[4]));
matthiasm@8 47 /* printf("A=%d, B=%d\n", m, n); */
matthiasm@8 48
matthiasm@8 49 leftA = mxGetPr(prhs[0]);
matthiasm@8 50 rightA = mxGetPr(prhs[1]);
matthiasm@8 51 topA = mxGetPr(prhs[2]);
matthiasm@8 52 bottomA = mxGetPr(prhs[3]);
matthiasm@8 53
matthiasm@8 54 leftB = mxGetPr(prhs[4]);
matthiasm@8 55 rightB = mxGetPr(prhs[5]);
matthiasm@8 56 topB = mxGetPr(prhs[6]);
matthiasm@8 57 bottomB = mxGetPr(prhs[7]);
matthiasm@8 58
matthiasm@8 59 verbose = mxGetPr(prhs[8]);
matthiasm@8 60
matthiasm@8 61 /* Allocate space for sparse matrix.
matthiasm@8 62 * NOTE: Assume at most 20% of the data is sparse. Use ceil
matthiasm@8 63 * to cause it to round up.
matthiasm@8 64 */
matthiasm@8 65
matthiasm@8 66 percent_sparse = 0.01;
matthiasm@8 67 nzmax = (int)ceil((double)m*(double)n*percent_sparse);
matthiasm@8 68
matthiasm@8 69 plhs[0] = mxCreateSparse(m,n,nzmax,0);
matthiasm@8 70 overlap = mxGetPr(plhs[0]);
matthiasm@8 71 irs = mxGetIr(plhs[0]);
matthiasm@8 72 jcs = mxGetJc(plhs[0]);
matthiasm@8 73
matthiasm@8 74 plhs[1] = mxCreateSparse(m,n,nzmax,0);
matthiasm@8 75 overlap2 = mxGetPr(plhs[1]);
matthiasm@8 76 irs2 = mxGetIr(plhs[1]);
matthiasm@8 77 jcs2 = mxGetJc(plhs[1]);
matthiasm@8 78
matthiasm@8 79
matthiasm@8 80 /* Assign nonzeros. */
matthiasm@8 81 k = 0;
matthiasm@8 82 for (j = 0; (j < n); j++) {
matthiasm@8 83 int i;
matthiasm@8 84 jcs[j] = k;
matthiasm@8 85 jcs2[j] = k;
matthiasm@8 86 for (i = 0; (i < m); i++) {
matthiasm@8 87 tmp = (MAX(0, MIN(rightA[i], rightB[j]) - MAX(leftA[i], leftB[j]) )) *
matthiasm@8 88 (MAX(0, MIN(topA[i], topB[j]) - MAX(bottomA[i], bottomB[j]) ));
matthiasm@8 89
matthiasm@8 90 if (*verbose) {
matthiasm@8 91 printf("j=%d,i=%d,tmp=%5.3f\n", j,i,tmp);
matthiasm@8 92 }
matthiasm@8 93
matthiasm@8 94 if (IsNonZero(tmp)) {
matthiasm@8 95
matthiasm@8 96 /* Check to see if non-zero element will fit in
matthiasm@8 97 * allocated output array. If not, increase
matthiasm@8 98 * percent_sparse by 20%, recalculate nzmax, and augment
matthiasm@8 99 * the sparse array.
matthiasm@8 100 */
matthiasm@8 101 if (k >= nzmax) {
matthiasm@8 102 int oldnzmax = nzmax;
matthiasm@8 103 percent_sparse += 0.2;
matthiasm@8 104 nzmax = (int)ceil((double)m*(double)n*percent_sparse);
matthiasm@8 105
matthiasm@8 106 /* Make sure nzmax increases atleast by 1. */
matthiasm@8 107 if (oldnzmax == nzmax)
matthiasm@8 108 nzmax++;
matthiasm@8 109 printf("reallocating from %d to %d\n", oldnzmax, nzmax);
matthiasm@8 110
matthiasm@8 111 mxSetNzmax(plhs[0], nzmax);
matthiasm@8 112 mxSetPr(plhs[0], mxRealloc(overlap, nzmax*sizeof(double)));
matthiasm@8 113 mxSetIr(plhs[0], mxRealloc(irs, nzmax*sizeof(int)));
matthiasm@8 114 overlap = mxGetPr(plhs[0]);
matthiasm@8 115 irs = mxGetIr(plhs[0]);
matthiasm@8 116
matthiasm@8 117 mxSetNzmax(plhs[1], nzmax);
matthiasm@8 118 mxSetPr(plhs[1], mxRealloc(overlap2, nzmax*sizeof(double)));
matthiasm@8 119 mxSetIr(plhs[1], mxRealloc(irs2, nzmax*sizeof(int)));
matthiasm@8 120 overlap2 = mxGetPr(plhs[1]);
matthiasm@8 121 irs2 = mxGetIr(plhs[1]);
matthiasm@8 122 }
matthiasm@8 123
matthiasm@8 124 overlap[k] = tmp;
matthiasm@8 125 irs[k] = i;
matthiasm@8 126
matthiasm@8 127 areaA = (rightA[i]-leftA[i])*(topA[i]-bottomA[i]);
matthiasm@8 128 areaB = (rightB[j]-leftB[j])*(topB[j]-bottomB[j]);
matthiasm@8 129 overlap2[k] = MIN(tmp/areaA, tmp/areaB);
matthiasm@8 130 irs2[k] = i;
matthiasm@8 131
matthiasm@8 132 k++;
matthiasm@8 133 } /* IsNonZero */
matthiasm@8 134 } /* for i */
matthiasm@8 135 }
matthiasm@8 136 jcs[n] = k;
matthiasm@8 137 jcs2[n] = k;
matthiasm@8 138
matthiasm@8 139 }
matthiasm@8 140
matthiasm@8 141
matthiasm@8 142
matthiasm@8 143
matthiasm@8 144
matthiasm@8 145
matthiasm@8 146
matthiasm@8 147