annotate toolboxes/FullBNT-1.0.7/KPMtools/rectintSparseLoopC.c @ 0:cc4b1211e677 tip

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