MonotoneChain.xs

filter: Statementstmt 82.4% 47 / 57 Branchbran 76.3% 29 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 76 / 95 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Math/ConvexHull/MonotoneChain.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT /* we want efficiency */
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 typedef struct {
9 double x;
10 double y;
11 } point_t;
12  
13 typedef point_t* point_ptr_t;
14  
15 /* Three points are a counter-clockwise turn if ccw > 0, clockwise if
16 * ccw < 0, and collinear if ccw = 0 because ccw is a determinant that
17 * gives the signed area of the triangle formed by p1, p2 and p3.
18 */
19 STATIC double
20 ccw(point_t* p1, point_t* p2, point_t* p3)
21 {
22 31 return (p2->x - p1->x)*(p3->y - p1->y) - (p2->y - p1->y)*(p3->x - p1->x);
23 }
24  
25 /* Returns a list of points on the convex hull in counter-clockwise order.
26 * Note: the last point in the returned list is the same as the first one.
27 */
28 STATIC void
29 3 convex_hull(point_t* points, SSize_t npoints, point_ptr_t** out_hull, SSize_t* out_hullsize)
30 {
31 point_ptr_t* hull;
32 SSize_t i, t, k = 0;
33  
34 3 hull = *out_hull;
35  
36 /* lower hull */
37 23 ▶ for (i = 0; i < npoints; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < npoints; ++i) { 20 3 23
38 31 ▶ while (k >= 2 && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k;
Branch: 2 branches
BranchTrueFalseTotal
while (k >= 2 && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k; 15 16 31
while (k >= 2 && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k; 11 4 15
39 20 hull[k++] = &points[i];
40 }
41  
42 /* upper hull */
43 20 ▶ for (i = npoints-2, t = k+1; i >= 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = npoints-2, t = k+1; i >= 0; --i) { 17 3 20
44 28 ▶ while (k >= t && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k;
Branch: 2 branches
BranchTrueFalseTotal
while (k >= t && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k; 16 12 28
while (k >= t && ccw(hull[k-2], hull[k-1], &points[i]) <= 0) --k; 11 5 16
45 17 hull[k++] = &points[i];
46 }
47  
48 3 *out_hull = hull;
49 3 *out_hullsize = k;
50 3 }
51  
52 MODULE = Math::ConvexHull::MonotoneChain PACKAGE = Math::ConvexHull::MonotoneChain
53 PROTOTYPES: DISABLE
54  
55 void
56 convex_hull_sorted(points)
57 AV* points
58 PREINIT:
59 AV* hull_points;
60 SSize_t out_hullsize, npoints, arylen, i;
61 point_t* cpoints;
62 point_ptr_t* out_hull;
63 AV* inner_av;
64 AV* out_av;
65 SV** elemptr;
66 SV* elem;
67 PPCODE:
68 6 npoints = av_len(points)+1;
69 6 ▶ if (npoints <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (npoints <= 2) { 3 3 6
70 3 out_av = newAV();
71 3 av_fill(out_av, npoints-1);
72 6 ▶ for (i = 0; i < npoints; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < npoints; ++i) { 3 3 6
73 3 elemptr = av_fetch(points, i, 0);
74 6 ▶ av_store(out_av, i, newSVsv(*elemptr));
Branch: 1 branch
BranchTrueFalseTotal
av_store(out_av, i, newSVsv(*elemptr)); 3 0 3
75 }
76 3 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV*)out_av)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV*)out_av))); 0 3 3
77 3 XSRETURN(1);
78 }
79 else {
80 3 cpoints = (point_t*)malloc(npoints * sizeof(point_t));
81 23 ▶ for (i = 0; i < npoints; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < npoints; ++i) { 20 3 23
82 20 ▶ if (NULL == (elemptr = av_fetch(points, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elemptr = av_fetch(points, i, 0))) { 0 20 20
83 0 free(cpoints);
84 0 croak("Could not fetch element from array");
85 }
86  
87 20 elem = *elemptr;
88 20 ▶ if (SvROK(elem) && SvTYPE(SvRV(elem)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(elem) && SvTYPE(SvRV(elem)) == SVt_PVAV) { 20 0 20
if (SvROK(elem) && SvTYPE(SvRV(elem)) == SVt_PVAV) { 20 0 20
89 inner_av = (AV*)SvRV(elem);
90 20 arylen = av_len(inner_av)+1;
91 20 ▶ if (arylen < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (arylen < 2) { 0 20 20
92 0 free(cpoints);
93 0 croak("Input array does not only contain point-like structures with at least two coordinates? At point %i.", i);
94 }
95 20 ▶ else if (NULL == (elemptr = av_fetch(inner_av, 0, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (NULL == (elemptr = av_fetch(inner_av, 0, 0))) { 0 20 20
96 0 free(cpoints);
97 0 croak("Could not fetch element from array");
98 }
99 20 cpoints[i].x = SvNV(*elemptr);
100  
101 20 ▶ if (NULL == (elemptr = av_fetch(inner_av, 1, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elemptr = av_fetch(inner_av, 1, 0))) { 0 20 20
102 0 free(cpoints);
103 0 croak("Could not fetch element from array");
104 }
105 20 cpoints[i].y = SvNV(*elemptr);
106  
107 }
108 else {
109 0 free(cpoints);
110 0 croak("Input array does not only contain point-like structures?");
111 }
112 }
113 } /* end for i in 0..npoints */
114  
115 3 out_hull = malloc(npoints*2 * sizeof(point_ptr_t));
116 3 convex_hull(cpoints, npoints, &out_hull, &out_hullsize);
117 3 out_av = newAV();
118 3 av_fill(out_av, out_hullsize-2);
119 15 ▶ for (i = 0; i < out_hullsize-1; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < out_hullsize-1; ++i) { 12 3 15
120 12 inner_av = newAV();
121 12 av_fill(inner_av, 1);
122 12 av_store(inner_av, 0, newSVnv(out_hull[i]->x));
123 12 av_store(inner_av, 1, newSVnv(out_hull[i]->y));
124 12 elem = newRV_noinc((SV*)inner_av);
125 12 av_store(out_av, i, elem);
126 }
127 3 free(out_hull);
128 3 free(cpoints);
129 3 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV*)out_av)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV*)out_av))); 0 3 3
130 3 XSRETURN(1);
131  
« blib/lib/Math/ConvexHull/MonotoneChain.pm ↑ Summary