| 1 |
|
| /* -*- Mode: C -*- */ |
| 2 |
|
| |
| 3 |
|
| #define PERL_NO_GET_CONTEXT 1 |
| 4 |
|
| |
| 5 |
|
| #include "EXTERN.h" |
| 6 |
|
| #include "perl.h" |
| 7 |
|
| #include "XSUB.h" |
| 8 |
|
| |
| 9 |
|
| #include "ppport.h" |
| 10 |
|
| |
| 11 |
|
| #define X0 0 |
| 12 |
|
| #define Y0 1 |
| 13 |
|
| #define X1 2 |
| 14 |
|
| #define Y1 3 |
| 15 |
|
| |
| 16 |
|
| #define IDOFFSET (sizeof(NV) * 4) |
| 17 |
|
| #define BRUTEFORCECUTOFF 40 |
| 18 |
|
| |
| 19 |
|
| #if 1 |
| 20 |
|
| #define DP(f) |
| 21 |
|
| #define DUMP(msg, av, start) |
| 22 |
|
| #define my_assert(a) assert(a) |
| 23 |
|
| #else |
| 24 |
|
| |
| 25 |
|
| #define my_assert(a) if(!(a)) _failed_assertion(aTHX_ #a, __LINE__, __FILE__) |
| 26 |
|
| |
| 27 |
|
| #define DP(f) f |
| 28 |
|
| #define DUMP(msg, av, start) _dump(aTHX_ (msg), (av), (start)) |
| 29 |
|
| static void |
| 30 |
|
| _dump(pTHX_ char *msg, AV *rects, I32 start) { |
| 31 |
|
| I32 end = av_len(rects) + 1; |
| 32 |
|
| SV **svs = AvARRAY(rects); |
| 33 |
|
| I32 i; |
| 34 |
|
| fprintf (stderr, "%s = start: %d, end: %d", msg, start, end); |
| 35 |
|
| for (i = start; i < end; i++) { |
| 36 |
|
| SV *sv = svs[i]; |
| 37 |
|
| if (SvOK(sv)) { |
| 38 |
|
| STRLEN len, j; |
| 39 |
|
| NV* nv = (NV*)SvPV(svs[i], len); |
| 40 |
|
| IV* iv = (IV*)(SvPV_nolen(svs[i]) + IDOFFSET); |
| 41 |
|
| len = (len - IDOFFSET) / sizeof(IV); |
| 42 |
|
| fprintf (stderr, " [%.0f %.0f %.0f %.0f |", nv[0], nv[1], nv[2], nv[3]); |
| 43 |
|
| for (j = 0; j < len; j++) |
| 44 |
|
| fprintf(stderr, " %d", iv[j]); |
| 45 |
|
| fprintf(stderr, "]"); |
| 46 |
|
| } |
| 47 |
|
| else |
| 48 |
|
| fprintf(stderr, " undef"); |
| 49 |
|
| } |
| 50 |
|
| fprintf(stderr, "\n"); |
| 51 |
|
| fflush(stderr); |
| 52 |
|
| } |
| 53 |
|
| void _failed_assertion(pTHX_ char *str, int line, char *file) { |
| 54 |
|
| fprintf(stderr, "assertion %s failed at %s line %d\n", str, line, file); |
| 55 |
|
| fflush(stderr); |
| 56 |
|
| exit(1); |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| #endif |
| 60 |
|
| |
| 61 |
|
| static SV * |
| 62 |
0 |
| av_sure_fetch(pTHX_ AV *av, I32 i) { |
| 63 |
0 |
| SV **sv = av_fetch(av, i, 0); |
| 64 |
|
| my_assert(sv); |
| 65 |
0 |
| return *sv; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| static NV |
| 69 |
2130526 |
| sqr (NV a) { |
| 70 |
2130526 |
| return a * a; |
| 71 |
|
| } |
| 72 |
|
| int |
| 73 |
|
| |
| 74 |
19982878 |
| double_cmp(pTHX_ double *a, double *b) { |
| 75 |
19982878 |
| double fa = *a; |
| 76 |
19982878 |
| double fb = *b; |
| 77 |
|
| DP(printf("cmp(%f, %f) => %d\n", fa, fb, (fa < fb) ? -1 : ((fa > fb) ? 1 : 0))); |
| 78 |
19982878 |
▶ | return (fa < fb) ? -1 : ((fa > fb) ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| return (fa < fb) ? -1 : ((fa > fb) ? 1 : 0); |
5329333 |
14653545 |
19982878 |
|
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| void |
| 82 |
22920 |
| sort_inplace(pTHX_ double **v, int size) { |
| 83 |
22920 |
| sortsv((SV**)v, size, (SVCOMPARE_t)&double_cmp); |
| 84 |
22920 |
| } |
| 85 |
|
| |
| 86 |
|
| static NV |
| 87 |
11460 |
| find_best_cut(pTHX_ AV *rects, I32 start, I32 end, int dir, NV *bestv) { |
| 88 |
|
| NV **v0, **v1, **vc0, **vc1; |
| 89 |
|
| NV v, med, best; |
| 90 |
|
| int op, cl; |
| 91 |
|
| int i; |
| 92 |
|
| SV **svs; |
| 93 |
11460 |
| I32 size = end - start; |
| 94 |
|
| |
| 95 |
|
| my_assert(bestv); |
| 96 |
|
| |
| 97 |
|
| DUMP("fbc in", rects, start); |
| 98 |
|
| DP(fprintf(stderr, "end: %d\n", end)); |
| 99 |
|
| |
| 100 |
11460 |
| Newx(v0, size + 1, NV *); |
| 101 |
11460 |
| Newx(v1, size + 1, NV *); |
| 102 |
|
| |
| 103 |
11460 |
| v0[size] = v1[size] = NULL; |
| 104 |
|
| |
| 105 |
11460 |
| vc0 = v0; vc1 = v1; |
| 106 |
|
| |
| 107 |
11460 |
| svs = AvARRAY(rects) + start; |
| 108 |
11460 |
| size = end - start; |
| 109 |
|
| |
| 110 |
1641266 |
▶ | for (i = 0; i < size; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; i++) { |
1629806 |
11460 |
1641266 |
|
| 111 |
1629806 |
| NV *nv = (NV*)SvPV_nolen(svs[i]); |
| 112 |
1629806 |
▶ | if (dir == 'x') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dir == 'x') { |
814903 |
814903 |
1629806 |
|
| 113 |
814903 |
| v0[i] = nv + X0; |
| 114 |
814903 |
| v1[i] = nv + X1; |
| 115 |
|
| } |
| 116 |
|
| else { |
| 117 |
814903 |
| v0[i] = nv + Y0; |
| 118 |
814903 |
| v1[i] = nv + Y1; |
| 119 |
|
| } |
| 120 |
|
| } |
| 121 |
11460 |
| v0[size] = v1[size] = NULL; |
| 122 |
|
| |
| 123 |
11460 |
| sort_inplace(aTHX_ v0, size); |
| 124 |
11460 |
| sort_inplace(aTHX_ v1, size); |
| 125 |
|
| |
| 126 |
11460 |
| op = cl = 0; |
| 127 |
11460 |
| med = 0.5 * size; |
| 128 |
11460 |
| best = .24 * sqr(size); |
| 129 |
|
| |
| 130 |
|
| my_assert(best >= 0); |
| 131 |
|
| |
| 132 |
239489 |
▶ | while (*v0 && *v1) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (*v0 && *v1) { |
228029 |
11460 |
239489 |
| while (*v0 && *v1) { |
228029 |
0 |
228029 |
|
| 133 |
|
| NV v, good; |
| 134 |
|
| NV l, r; |
| 135 |
|
| |
| 136 |
228029 |
▶ | v = (**v0 <= **v1) ? **v0 : **v1; |
Branch: 1 branch
| Branch | True | False | Total |
| v = (**v0 <= **v1) ? **v0 : **v1; |
214698 |
13331 |
228029 |
|
| 137 |
|
| |
| 138 |
1857835 |
▶ | while (*v0 && v == **v0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (*v0 && v == **v0) { |
1846375 |
11460 |
1857835 |
| while (*v0 && v == **v0) { |
1629806 |
216569 |
1846375 |
|
| 139 |
1629806 |
| op++; |
| 140 |
1629806 |
| v0++; |
| 141 |
|
| } |
| 142 |
1754165 |
▶ | while (*v1 && v == **v1) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (*v1 && v == **v1) { |
1754165 |
0 |
1754165 |
| while (*v1 && v == **v1) { |
1526136 |
228029 |
1754165 |
|
| 143 |
1526136 |
| cl++; |
| 144 |
1526136 |
| v1++; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| my_assert(op > 0 && op <= size); |
| 148 |
|
| my_assert(cl >= 0 && cl <= size); |
| 149 |
|
| |
| 150 |
228029 |
| l = op - med; |
| 151 |
228029 |
| r = size - cl - med; |
| 152 |
228029 |
| good = sqr(l) + sqr(r); |
| 153 |
|
| |
| 154 |
|
| my_assert(good >= 0); |
| 155 |
|
| |
| 156 |
228029 |
▶ | if (good < best) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (good < best) { |
68690 |
159339 |
228029 |
|
| 157 |
|
| DP(fprintf(stderr, "find_best_cut l: %.2f, r: %.2f, good: %.2f\n", l, r, good)); |
| 158 |
68690 |
| best = good; |
| 159 |
68690 |
| *bestv = v; |
| 160 |
|
| } |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
11460 |
| Safefree(vc0); |
| 164 |
11460 |
| Safefree(vc1); |
| 165 |
|
| |
| 166 |
11460 |
| return best; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| static void |
| 170 |
|
| _break(pTHX_ AV *rects, I32 start, AV *parts); |
| 171 |
|
| |
| 172 |
|
| static void |
| 173 |
163384 |
| _brute_force_merge(pTHX_ AV *rects, I32 start, AV *parts) { |
| 174 |
|
| I32 i, len; |
| 175 |
4658816 |
▶ | for (i = start; i <= av_len(rects); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i <= av_len(rects); i++) { |
4495432 |
163384 |
4658816 |
|
| 176 |
4495432 |
| SV *svr = (AvARRAY(rects))[i]; |
| 177 |
|
| I32 j; |
| 178 |
16600557 |
▶ | for (j=0; j <= av_len(parts);) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j=0; j <= av_len(parts);) { |
12105125 |
4495432 |
16600557 |
|
| 179 |
12105125 |
| NV *r = (NV*) SvPV_nolen(svr); |
| 180 |
12105125 |
▶ | SV *svp = (AvARRAY(parts))[j]; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *svp = (AvARRAY(parts))[j]; |
12105125 |
0 |
12105125 |
|
| 181 |
12105125 |
| NV *p = (NV*) SvPV_nolen(svp); |
| 182 |
|
| |
| 183 |
12105125 |
▶ | if ((r[X1] > p[X0]) && |
Branch: 1 branch
| Branch | True | False | Total |
| if ((r[X1] > p[X0]) && |
7139316 |
4965809 |
12105125 |
|
| 184 |
7139316 |
▶ | (r[X0] < p[X1]) && |
Branch: 1 branch
| Branch | True | False | Total |
| (r[X0] < p[X1]) && |
2179668 |
4959648 |
7139316 |
|
| 185 |
2179668 |
▶ | (r[Y1] > p[Y0]) && |
Branch: 1 branch
| Branch | True | False | Total |
| (r[Y1] > p[Y0]) && |
1425684 |
753984 |
2179668 |
|
| 186 |
1425684 |
▶ | (r[Y0] < p[Y1])) { |
Branch: 1 branch
| Branch | True | False | Total |
| (r[Y0] < p[Y1])) { |
710082 |
715602 |
1425684 |
|
| 187 |
710082 |
▶ | if ((r[X0] == p[X0]) && |
Branch: 1 branch
| Branch | True | False | Total |
| if ((r[X0] == p[X0]) && |
532376 |
177706 |
710082 |
|
| 188 |
532376 |
▶ | (r[Y0] == p[Y0]) && |
Branch: 1 branch
| Branch | True | False | Total |
| (r[Y0] == p[Y0]) && |
424807 |
107569 |
532376 |
|
| 189 |
424807 |
▶ | (r[X1] == p[X1]) && |
Branch: 1 branch
| Branch | True | False | Total |
| (r[X1] == p[X1]) && |
347674 |
77133 |
424807 |
|
| 190 |
642004 |
▶ | (r[Y1] == p[Y1])) { |
Branch: 1 branch
| Branch | True | False | Total |
| (r[Y1] == p[Y1])) { |
294330 |
53344 |
347674 |
|
| 191 |
|
| SV *last; |
| 192 |
|
| STRLEN len; |
| 193 |
294330 |
| char *pv = SvPV(svp, len); |
| 194 |
294330 |
| sv_catpvn(svr, pv + IDOFFSET, len - IDOFFSET); |
| 195 |
294330 |
| last = av_pop(parts); |
| 196 |
294330 |
▶ | if (last == svp) |
Branch: 1 branch
| Branch | True | False | Total |
| if (last == svp) |
158498 |
135832 |
294330 |
|
| 197 |
158498 |
| SvREFCNT_dec(last); |
| 198 |
|
| else |
| 199 |
135832 |
| av_store(parts, j, last); |
| 200 |
|
| } |
| 201 |
415752 |
| else { |
| 202 |
|
| NV x[4], y[4]; |
| 203 |
|
| /* sort xs and ys */ |
| 204 |
415752 |
▶ | if (r[X0] < p[X0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r[X0] < p[X0]) { |
61657 |
354095 |
415752 |
|
| 205 |
61657 |
| x[0] = r[X0]; x[1] = p[X0]; |
| 206 |
|
| } |
| 207 |
|
| else { |
| 208 |
354095 |
| x[0] = p[X0]; x[1] = r[X0]; |
| 209 |
|
| } |
| 210 |
415752 |
▶ | if (r[X1] < p[X1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r[X1] < p[X1]) { |
114573 |
301179 |
415752 |
|
| 211 |
114573 |
| x[2] = r[X1]; x[3] = p[X1]; |
| 212 |
|
| } |
| 213 |
|
| else { |
| 214 |
301179 |
| x[2] = p[X1]; x[3] = r[X1]; |
| 215 |
|
| } |
| 216 |
415752 |
▶ | if (r[Y0] < p[Y0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r[Y0] < p[Y0]) { |
59450 |
356302 |
415752 |
|
| 217 |
59450 |
| y[0] = r[Y0]; y[1] = p[Y0]; |
| 218 |
|
| } |
| 219 |
|
| else { |
| 220 |
356302 |
| y[0] = p[Y0]; y[1] = r[Y0]; |
| 221 |
|
| } |
| 222 |
415752 |
▶ | if (r[Y1] < p[Y1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r[Y1] < p[Y1]) { |
114399 |
301353 |
415752 |
|
| 223 |
114399 |
| y[2] = r[Y1]; y[3] = p[Y1]; |
| 224 |
|
| } |
| 225 |
|
| else { |
| 226 |
301353 |
| y[2] = p[Y1]; y[3] = r[Y1]; |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
415752 |
▶ | if ( ( ((x[3] - x[0]) > (y[3] - y[0])) && |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ( ((x[3] - x[0]) > (y[3] - y[0])) && |
206167 |
209585 |
415752 |
|
| 230 |
206167 |
▶ | ((x[0] != x[1]) || (x[2] != x[3])) ) || |
Branch: 2 branches
| Branch | True | False | Total |
| ((x[0] != x[1]) || (x[2] != x[3])) ) || |
108131 |
98036 |
206167 |
| ((x[0] != x[1]) || (x[2] != x[3])) ) || |
44397 |
63734 |
108131 |
|
| 231 |
463341 |
▶ | ((y[0] == y[1]) && (y[2] == y[3])) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| ((y[0] == y[1]) && (y[2] == y[3])) ) { |
130659 |
123323 |
253982 |
| ((y[0] == y[1]) && (y[2] == y[3])) ) { |
47589 |
83070 |
130659 |
|
| 232 |
209359 |
| NV b = ((sqr(x[1] - x[0]) + sqr(x[1] - x[3])) |
| 233 |
209359 |
▶ | < (sqr(x[2] - x[0]) + sqr(x[2] - x[3])) ? x[1] : x[2]); |
Branch: 1 branch
| Branch | True | False | Total |
| < (sqr(x[2] - x[0]) + sqr(x[2] - x[3])) ? x[1] : x[2]); |
94986 |
114373 |
209359 |
|
| 234 |
209359 |
▶ | if ((r[X0] < b) && (r[X1] > b)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((r[X0] < b) && (r[X1] > b)) { |
147275 |
62084 |
209359 |
| if ((r[X0] < b) && (r[X1] > b)) { |
75409 |
71866 |
147275 |
|
| 235 |
75409 |
| SV *svcp = newSVsv(svr); |
| 236 |
75409 |
| NV *cp = (NV*)SvPV_nolen(svcp); |
| 237 |
75409 |
| cp[X0] = b; |
| 238 |
75409 |
| av_push(rects, svcp); |
| 239 |
|
| |
| 240 |
|
| /* fprintf(stderr, "[%f %f %f %f] -> [%f %f %f %f], [%f %f %f %f]\n", |
| 241 |
|
| r[0], r[1], r[2], r[3], |
| 242 |
|
| cp[0], cp[1], cp[2], cp[3], |
| 243 |
|
| b, r[1], r[2], r[3]); fflush(stderr); */ |
| 244 |
|
| |
| 245 |
75409 |
| r[X1] = b; |
| 246 |
|
| } |
| 247 |
209359 |
▶ | if ((p[X0] < b) && (p[X1] > b)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((p[X0] < b) && (p[X1] > b)) { |
176314 |
33045 |
209359 |
| if ((p[X0] < b) && (p[X1] > b)) { |
133950 |
42364 |
176314 |
|
| 248 |
133950 |
| SV *svcp = newSVsv(svp); |
| 249 |
133950 |
| NV *cp = (NV*)SvPV_nolen(svcp); |
| 250 |
133950 |
| cp[X0] = b; |
| 251 |
133950 |
| av_push(parts, svcp); |
| 252 |
|
| |
| 253 |
|
| /* fprintf(stderr, "[%f %f %f %f] -> [%f %f %f %f], [%f %f %f %f]\n", |
| 254 |
|
| p[0], p[1], p[2], p[3], |
| 255 |
|
| cp[0], cp[1], cp[2], cp[3], |
| 256 |
|
| p[0], p[1], b, p[3]); fflush(stderr); */ |
| 257 |
|
| |
| 258 |
133950 |
| p[X1] = b; |
| 259 |
|
| } |
| 260 |
|
| } |
| 261 |
|
| else { |
| 262 |
206393 |
| NV b = ((sqr(y[1] - y[0]) + sqr(y[1] - y[3])) |
| 263 |
206393 |
▶ | < (sqr(y[2] - y[0]) + sqr(y[2] - y[3])) ? y[1] : y[2]); |
Branch: 1 branch
| Branch | True | False | Total |
| < (sqr(y[2] - y[0]) + sqr(y[2] - y[3])) ? y[1] : y[2]); |
93625 |
112768 |
206393 |
|
| 264 |
206393 |
▶ | if ((r[Y0] < b) && (r[Y1] > b)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((r[Y0] < b) && (r[Y1] > b)) { |
144812 |
61581 |
206393 |
| if ((r[Y0] < b) && (r[Y1] > b)) { |
72992 |
71820 |
144812 |
|
| 265 |
72992 |
| SV *svcp = newSVsv(svr); |
| 266 |
72992 |
| NV *cp = (NV*)SvPV_nolen(svcp); |
| 267 |
72992 |
| cp[Y0] = b; |
| 268 |
72992 |
| av_push(rects, svcp); |
| 269 |
|
| |
| 270 |
|
| /* fprintf(stderr, "[%f %f %f %f] -> [%f %f %f %f], [%f %f %f %f]\n", |
| 271 |
|
| r[0], r[1], r[2], r[3], |
| 272 |
|
| cp[0], cp[1], cp[2], cp[3], |
| 273 |
|
| r[0], b, r[2], r[3]); fflush(stderr); */ |
| 274 |
|
| |
| 275 |
72992 |
| r[Y1] = b; |
| 276 |
|
| } |
| 277 |
206393 |
▶ | if ((p[Y0] < b) && (p[Y1] > b)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((p[Y0] < b) && (p[Y1] > b)) { |
174302 |
32091 |
206393 |
| if ((p[Y0] < b) && (p[Y1] > b)) { |
133401 |
40901 |
174302 |
|
| 278 |
133401 |
| SV *svcp = newSVsv(svp); |
| 279 |
133401 |
| NV *cp = (NV*)SvPV_nolen(svcp); |
| 280 |
133401 |
| cp[Y0] = b; |
| 281 |
133401 |
| av_push(parts, svcp); |
| 282 |
|
| |
| 283 |
|
| /* fprintf(stderr, "[%f %f %f %f] -> [%f %f %f %f], [%f %f %f %f]\n", |
| 284 |
|
| p[0], p[1], p[2], p[3], |
| 285 |
|
| cp[0], cp[1], cp[2], cp[3], |
| 286 |
|
| p[0], p[1], p[2], b); fflush(stderr); */ |
| 287 |
|
| |
| 288 |
133401 |
| p[Y1] = b; |
| 289 |
|
| } |
| 290 |
|
| } |
| 291 |
415752 |
| continue; |
| 292 |
|
| } |
| 293 |
|
| } |
| 294 |
11689373 |
| j++; |
| 295 |
|
| } |
| 296 |
|
| } |
| 297 |
|
| /* |
| 298 |
|
| for (i = av_len(parts); i >= 0; i--) |
| 299 |
|
| av_push(rects, av_pop(parts)); |
| 300 |
|
| */ |
| 301 |
163384 |
▶ | if ((len = av_len(parts)) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((len = av_len(parts)) >= 0) { |
106011 |
57373 |
163384 |
|
| 302 |
|
| SV **svrs, **svps; |
| 303 |
106011 |
| I32 start = av_len(rects) + 1; |
| 304 |
106011 |
| av_extend(rects, start + len); |
| 305 |
106011 |
| svrs = AvARRAY(rects) + start; |
| 306 |
106011 |
| svps = AvARRAY(parts); |
| 307 |
106011 |
| AvFILLp(parts) = -1; |
| 308 |
106011 |
| AvFILLp(rects) = start + len; |
| 309 |
|
| |
| 310 |
|
| do { |
| 311 |
136405 |
| svrs[len] = svps[len]; |
| 312 |
136405 |
| svps[len] = &PL_sv_undef; |
| 313 |
136405 |
▶ | } while (--len >= 0); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (--len >= 0); |
30394 |
106011 |
136405 |
|
| 314 |
|
| } |
| 315 |
163384 |
| } |
| 316 |
|
| |
| 317 |
|
| static void |
| 318 |
5735 |
| _brute_force_break(pTHX_ AV *rects, I32 start, AV *parts) { |
| 319 |
|
| I32 i, j, end1; |
| 320 |
|
| |
| 321 |
|
| DUMP("bfb in", rects, start); |
| 322 |
|
| |
| 323 |
5735 |
| end1 = av_len(rects); |
| 324 |
5735 |
▶ | if (end1 < start + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (end1 < start + 1) |
0 |
5735 |
5735 |
|
| 325 |
0 |
| return; |
| 326 |
|
| |
| 327 |
5735 |
▶ | if (end1 - start > 2 * BRUTEFORCECUTOFF ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (end1 - start > 2 * BRUTEFORCECUTOFF ) { |
0 |
5735 |
5735 |
|
| 328 |
|
| SV **svs; |
| 329 |
|
| I32 end; |
| 330 |
0 |
| I32 middle = (start + end1 + 1) / 2; |
| 331 |
0 |
| _break(aTHX_ rects, middle, parts); |
| 332 |
|
| |
| 333 |
0 |
| svs = AvARRAY(rects); |
| 334 |
|
| |
| 335 |
0 |
| end = av_len(rects) + 1; |
| 336 |
|
| |
| 337 |
0 |
| i = start; |
| 338 |
0 |
| j = end; |
| 339 |
0 |
▶ | while (i < middle && j > middle) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < middle && j > middle) { |
0 |
0 |
0 |
| while (i < middle && j > middle) { |
0 |
0 |
0 |
|
| 340 |
0 |
| j--; |
| 341 |
0 |
| SV *tmp = svs[i]; |
| 342 |
0 |
| svs[i] = svs[j]; |
| 343 |
0 |
| svs[j] = tmp; |
| 344 |
0 |
| i++; |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
0 |
| middle = start + end - middle; |
| 348 |
0 |
| _break(aTHX_ rects, middle, parts); |
| 349 |
|
| |
| 350 |
0 |
| end = av_len(rects); |
| 351 |
|
| |
| 352 |
0 |
▶ | if ((end - start) > (end1 - start) * 1.2) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((end - start) > (end1 - start) * 1.2) |
0 |
0 |
0 |
|
| 353 |
0 |
| return _break(aTHX_ rects, start, parts); |
| 354 |
|
| |
| 355 |
0 |
▶ | while (end-- >= middle) |
Branch: 1 branch
| Branch | True | False | Total |
| while (end-- >= middle) |
0 |
0 |
0 |
|
| 356 |
0 |
| av_push(parts, av_pop(rects)); |
| 357 |
|
| |
| 358 |
0 |
| return _brute_force_merge(aTHX_ rects, start, parts); |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
169119 |
▶ | while (--end1 >= start) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (--end1 >= start) { |
163384 |
5735 |
169119 |
|
| 362 |
|
| SV *last, *next; |
| 363 |
|
| SV **svs; |
| 364 |
|
| |
| 365 |
|
| DP(fprintf(stderr, "bfb: start: %d, end1: %d, end: %d\n", start, end1, av_len(rects) + 1)); |
| 366 |
|
| |
| 367 |
163384 |
| svs = AvARRAY(rects); |
| 368 |
163384 |
| last = svs[AvFILLp(rects)]; |
| 369 |
163384 |
| svs[AvFILLp(rects)--] = &PL_sv_undef; |
| 370 |
163384 |
| next = svs[end1]; |
| 371 |
163384 |
| svs[end1] = last; |
| 372 |
163384 |
| av_push(parts, next); |
| 373 |
|
| |
| 374 |
163384 |
| _brute_force_merge(aTHX_ rects, end1, parts); |
| 375 |
|
| } |
| 376 |
5735 |
| return; |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| static void |
| 380 |
5735 |
| _break(pTHX_ AV *rects, I32 start, AV *parts) { |
| 381 |
|
| NV bestx, bestxx, besty, bestyy, div; |
| 382 |
|
| int off; |
| 383 |
|
| I32 i, j, middle, end; |
| 384 |
|
| SV **svs; |
| 385 |
|
| |
| 386 |
|
| DUMP("break", rects, start); |
| 387 |
|
| |
| 388 |
|
| while (1) { |
| 389 |
|
| |
| 390 |
11465 |
| end = av_len(rects) + 1; |
| 391 |
|
| |
| 392 |
11465 |
▶ | if ((end - start) <= BRUTEFORCECUTOFF) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((end - start) <= BRUTEFORCECUTOFF) |
5735 |
5730 |
11465 |
|
| 393 |
5735 |
| return _brute_force_break(aTHX_ rects, start, parts); |
| 394 |
|
| |
| 395 |
5730 |
| bestx = find_best_cut(aTHX_ rects, start, end, 'x', &bestxx); |
| 396 |
5730 |
▶ | besty = ((bestx == 0) ? 1 : find_best_cut(aTHX_ rects, start, end, 'y', &bestyy)); |
Branch: 1 branch
| Branch | True | False | Total |
| besty = ((bestx == 0) ? 1 : find_best_cut(aTHX_ rects, start, end, 'y', &bestyy)); |
5730 |
0 |
5730 |
|
| 397 |
|
| |
| 398 |
5730 |
▶ | if (bestx < besty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bestx < besty) { |
2833 |
2897 |
5730 |
|
| 399 |
2833 |
| off = X0; |
| 400 |
2833 |
| div = bestxx; |
| 401 |
|
| DP(fprintf(stderr, "cutting at x=%.0f, best=%.2f\n", bestxx, bestx)); |
| 402 |
|
| } |
| 403 |
|
| else { |
| 404 |
2897 |
| off = Y0; |
| 405 |
2897 |
| div = bestyy; |
| 406 |
|
| DP(fprintf(stderr, "cutting at y=%.0f, best=%.2f\n", bestyy, besty)); |
| 407 |
|
| } |
| 408 |
|
| |
| 409 |
5730 |
| svs = AvARRAY(rects); |
| 410 |
5730 |
| i = start; |
| 411 |
5730 |
| middle = end; |
| 412 |
820633 |
▶ | while (i < middle) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < middle) { |
814903 |
5730 |
820633 |
|
| 413 |
814903 |
| SV *sv = svs[i]; |
| 414 |
814903 |
| NV n0 = ((NV*)SvPV_nolen(sv))[off]; |
| 415 |
814903 |
▶ | if (n0 < div) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n0 < div) { |
412627 |
402276 |
814903 |
|
| 416 |
412627 |
| middle--; |
| 417 |
412627 |
| svs[i] = svs[middle]; |
| 418 |
412627 |
| svs[middle] = sv; |
| 419 |
|
| } |
| 420 |
|
| else |
| 421 |
402276 |
| i++; |
| 422 |
|
| } |
| 423 |
|
| |
| 424 |
|
| DUMP("b0", rects, start); |
| 425 |
|
| |
| 426 |
5730 |
▶ | if (middle == start || middle == end) |
Branch: 2 branches
| Branch | True | False | Total |
| if (middle == start || middle == end) |
5730 |
0 |
5730 |
| if (middle == start || middle == end) |
0 |
5730 |
5730 |
|
| 427 |
0 |
| return _brute_force_break(aTHX_ rects, start, parts); |
| 428 |
|
| |
| 429 |
|
| |
| 430 |
5730 |
| _break(aTHX_ rects, middle, parts); |
| 431 |
|
| |
| 432 |
|
| DUMP("b1", rects, start); |
| 433 |
|
| |
| 434 |
5730 |
| svs = AvARRAY(rects); |
| 435 |
|
| |
| 436 |
5730 |
| end = av_len(rects) + 1; |
| 437 |
|
| |
| 438 |
5730 |
| i = start; |
| 439 |
5730 |
| j = end; |
| 440 |
406779 |
▶ | while (i < middle && j > middle) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < middle && j > middle) { |
401305 |
5474 |
406779 |
| while (i < middle && j > middle) { |
401049 |
256 |
401305 |
|
| 441 |
401049 |
| j--; |
| 442 |
401049 |
| SV *tmp = svs[i]; |
| 443 |
401049 |
| svs[i] = svs[j]; |
| 444 |
401049 |
| svs[j] = tmp; |
| 445 |
401049 |
| i++; |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
|
| DUMP("b2", rects, start); |
| 449 |
|
| |
| 450 |
5730 |
| end += start - middle; |
| 451 |
|
| |
| 452 |
5730 |
| off += 2; |
| 453 |
5730 |
| i = start; |
| 454 |
5730 |
| middle = end; |
| 455 |
|
| DP(fprintf(stderr, "i: %d, middle: %d\n", i, middle)); |
| 456 |
1078338 |
▶ | while (i < middle) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < middle) { |
1072608 |
5730 |
1078338 |
|
| 457 |
1072608 |
| SV *sv = svs[i]; |
| 458 |
1072608 |
| NV n0 = ((NV*)SvPV_nolen(sv))[off]; |
| 459 |
1072608 |
▶ | if (n0 > div) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n0 > div) { |
168114 |
904494 |
1072608 |
|
| 460 |
168114 |
| middle--; |
| 461 |
168114 |
| svs[i] = svs[middle]; |
| 462 |
168114 |
| svs[middle] = sv; |
| 463 |
|
| } |
| 464 |
|
| else |
| 465 |
904494 |
| i++; |
| 466 |
|
| } |
| 467 |
|
| |
| 468 |
|
| DUMP("b3", rects, start); |
| 469 |
|
| |
| 470 |
5730 |
▶ | if (middle == start) |
Branch: 1 branch
| Branch | True | False | Total |
| if (middle == start) |
0 |
5730 |
5730 |
|
| 471 |
0 |
| return _brute_force_break(aTHX_ rects, start, parts); |
| 472 |
|
| |
| 473 |
5730 |
| start = middle; |
| 474 |
|
| } |
| 475 |
|
| /* _break(aTHX_ rects, middle); */ |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
|
| MODULE = Algorithm::BreakOverlappingRectangles PACKAGE = Algorithm::BreakOverlappingRectangles |
| 479 |
|
| PROTOTYPES: DISABLE |
| 480 |
|
| |
| 481 |
|
| void |
| 482 |
|
| _break_rectangles(rects) |
| 483 |
|
| AV *rects; |
| 484 |
|
| CODE: |
| 485 |
5 |
▶ | if (SvMAGICAL((SV*)rects)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvMAGICAL((SV*)rects)) |
0 |
5 |
5 |
|
| 486 |
0 |
| Perl_croak(aTHX_ "internal error: unacceptable magic AV found"); |
| 487 |
5 |
| _break(aTHX_ rects, 0, (AV*)sv_2mortal((SV*)newAV())); |