comparison toolboxes/FullBNT-1.0.7/KPMtools/rectintSparseLoopC.c @ 0:e9a9cd732c1e tip

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