blib/lib/Math/Geometry/Planar.pm

filter: Statementstmt 84.9% 1749 / 2058 Branchbran 75.2% 545 / 724 Conditioncond 55.3% 171 / 309 MC/DCmcdc 38.5% 69 / 179 Subroutinesub 98.0% 100 / 102 Podpod 69.8% 44 / 63 totaltotal 77.9% 2678 / 3435 CC 479 SCAR 40.6
CC
479
Cov
80%
_traverse_polygon
302.7
_add_segment
204.0
IsInsidePolygon
33.0
CRAP 58.0
↑ Summary
1 #
2 # Copyright (c) 2002 Danny Van de Pol - Alcatel Telecom Belgium
3 # danny.vandepol@alcatel.be
4 #
5 # Free usage under the same Perl Licence condition.
6 #
7  
8 package Math::Geometry::Planar;
9  
10 1 $VERSION = '1.18';
11  
12 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 $VERSION
14 @ISA
15 @EXPORT
16 @EXPORT_OK
17 $precision
18 1 );
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Math::Geometry::Planar::GPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Math::Geometry::Planar::Offset;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 $precision = 7;
27  
28 1 require Exporter;
29 1 @ISA = qw(Exporter);
30 1 @EXPORT = qw(
31 SegmentLength Determinant DotProduct CrossProduct
32 TriangleArea Colinear
33 SegmentIntersection LineIntersection RayIntersection
34 SegmentLineIntersection RayLineIntersection
35 SegmentRayIntersection
36 Perpendicular PerpendicularFoot
37 DistanceToLine DistanceToSegment
38 Gpc2Polygons GpcClip
39 CircleToPoly ArcToPoly CalcAngle
40 );
41 1 @EXPORT_OK = qw($precision);
42  
43 =pod
44  
45 =head1 NAME
46  
47 Math::Geometry::Planar - A collection of planar geometry functions
48  
49 =head1 SYNOPSIS
50  
51 use Math::Geometry::Planar;
52 $polygon = Math::Geometry::Planar->new; creates a new polygon object;
53 $contour = Math::Geometry::Planar->new; creates a new contour object;
54  
55 =head4 Formats
56  
57 A point is a reference to an array holding the x and y coordinates of the point.
58  
59 $point = [$x_coord,$y_coord];
60  
61 A polygon is a reference to an (ordered) array of points. The first point is the
62 begin and end point of the polygon. The points can be given in any direction
63 (clockwise or counter clockwise).
64  
65 A contour is a reference to an array of polygons. By convention, the first polygon
66 is the outer shape, all other polygons represent holes in the outer shape. The outer
67 shape must enclose all holes !
68 Using this convention, the points can be given in any direction, however, keep
69 in mind that some functions (e.g. triangulation) require that the outer polygons
70 are entered in counter clockwise order and the inner polygons (holes) in clock
71 wise order. The points, polygons, add_polygons methods will automatically set the
72 right order of points.
73 No points can be assigned to an object that already has polygons assigned to and
74 vice versa.
75  
76 $points = [[$x1,$y1],[$x2,$y2], ... ];
77 $polygon->points($points); # assign points to polygon object
78 $points1 = [[$x1,$y1],[$x2,$y2], ... ];
79 $points2 = [[ax1,by1],[ax2,by2], ... ];
80 $contour->polygons([$points1,$points2, ...]); # assign polgyons to contour object
81  
82 =head1 METHODS
83  
84 The available methods are:
85  
86 =head4 $polygon->points(arg);
87  
88 Returns the polygon points if no argument is entered.
89 If the argument is a refence to a points array, sets the points for a polygon object.
90  
91 =head4 $contour->polygons(arg);
92  
93 Returns the contour polygons if no argument is entered.
94 If the argument is a refence to a polygons array, sets the polygons for a contour object.
95  
96 =head4 $contour->num_polygons;
97  
98 Returns the total number of polygons in the contour.
99  
100 =head4 $contour->add_polygons(arg);
101  
102 Adds a list of polygons to a contour object (if the contour object doesn't have any
103 polygons yet, the very first polygon reference from the list is used as the outer
104 shape). Returns the total number of polygons in the contour.
105  
106 =head4 $contour->get_polygons(arg_1,arg_2, ... );
107  
108 Returns a list of polygons where each element of the list corresponds to the polygon
109 at index arg_x - starting at 0, the outer shape. If the index arg_x is out of range,
110 the corresponding value in the result list wil be undefined. If no argument is
111 entered, a full list of all polygons is returned. Please note that this method returns
112 a list rather then a reference.
113  
114 =head4 $polygon->cleanup;
115  
116 Remove colinear points from the polygon/contour.
117  
118 =head4 $polygon->isconvex;
119  
120 Returns true if the polygon/contour is convex. A contour is considered to be convex if
121 the outer shape is convex.
122  
123 =head4 $polygon->issimple;
124  
125 Returns true if the polygon/contour is simple. A contour is considered to be simple if
126 all it's polygons are simple.
127  
128 =head4 $polygon->perimeter;
129  
130 Returns the perimeter of the polygon/contour. The perimeter of a contour is the perimeter
131 of the outer shape.
132  
133 =head4 $polygon->area;
134  
135 Returns the signed area of the polygon/contour (positive if the points are in counter
136 clockwise order). The area of a contour is the area of the outer shape minus the sum
137 of the area of the holes.
138  
139 =head4 $polygon->centroid;
140  
141 Returns the centroid (center of gravity) of the polygon/contour.
142  
143 =head4 $polygon->isinside($point);
144  
145 Returns true if point is inside the polygon/contour (a point is inside a contour if
146 it is inside the outer polygon and not inside a hole).
147  
148 =head4 $polygon->rotate($angle,$center);
149  
150 Returns polygon/contour rotated $angle (in radians) around $center.
151 If no center is entered, rotates around the origin.
152  
153 =head4 $polygon->move($dx,$dy);
154  
155 Returns polygon/contour moved $dx in x direction and $dy in y direction.
156  
157 =head4 $polygon->mirrorx($center);
158  
159 Returns polygon/contour mirrored in x direction
160 with (vertical) axis of reflection through point $center.
161 If no center is entered, axis is the Y-axis.
162  
163 =head4 $polygon->mirrory($center);
164  
165 Returns polygon/contour mirrored in y direction
166 with (horizontal) axis of reflection through point $center.
167 If no center is entered, axis is the X-axis.
168  
169 =head4 $polygon->mirror($axos);
170  
171 Returns polygon mirrored/contour along axis $axis (= array with 2 points defining
172 axis of reflection).
173  
174 =head4 $polygon->scale($csale,$center);
175  
176 Returns polygon/contour scaled by a factor $scale, center of scaling is $scale.
177 If no center is entered, center of scaling is the origin.
178  
179 =head4 $polygon->bbox;
180  
181 Returns the polygon's/contour's bounding box.
182  
183 =head4 $polygon->minrectangle;
184  
185 Returns the polygon's/contour's minimal (area) enclosing rectangle.
186  
187 =head4 $polygon->convexhull;
188  
189 Returns a polygon representing the convex hull of the polygon/contour.
190  
191 =head4 $polygon->convexhull2;
192  
193 Returns a polygon representing the convex hull of an arbitrary set of points
194 (works also on a contour, however a contour is a set of polygons and polygons
195 are ordered sets of points so the method above will be faster)
196  
197 =head4 $polygon->triangulate;
198  
199 Triangulates a polygon/contour based on Raimund Seidel's algorithm:
200 'A simple and fast incremental randomized algorithm for computing trapezoidal
201 decompositions and for triangulating polygons'
202 Returns a list of polygons (= the triangles)
203  
204 =head4 $polygon->offset_polygon($distance);
205  
206 Returns reference to an array of polygons representing the original polygon
207 offsetted by $distance
208  
209 =head4 $polygon->convert2gpc;
210  
211 Converts a polygon/contour to a gpc structure and returns the resulting gpc structure
212  
213 =head1 EXPORTS
214  
215 =head4 SegmentLength[$p1,$p2];
216  
217 Returns the length of the segment (vector) p1p2
218  
219 =head4 Determinant(x1,y1,x2,y2);
220  
221 Returns the determinant of the matrix with rows x1,y1 and x2,y2 which is x1*y2 - y1*x2
222  
223 =head4 DotProduct($p1,$p2,$p3,$p4);
224  
225 Returns the vector dot product of vectors p1p2 and p3p4
226 or the dot product of p1p2 and p2p3 if $p4 is ommited from the argument list
227  
228 =head4 CrossProduct($p1,$p2,$p3);
229  
230 Returns the vector cross product of vectors p1p2 and p1p3
231  
232 =head4 TriangleArea($p1,$p2,$p3);
233  
234 Returns the signed area of the triangle p1p2p3
235  
236 =head4 Colinear($p1,$p2,$p3);
237  
238 Returns true if p1,p2 and p3 are colinear
239  
240 =head4 SegmentIntersection($p1,$p2,$p3,$p4);
241  
242 Returns the intersection point of segments p1p2 and p3p4,
243 false if segments don't intersect
244  
245 =head4 LineIntersection($p1,$p2,$p3,$p4);
246  
247 Returns the intersection point of lines p1p2 and p3p4,
248 false if lines don't intersect (parallel lines)
249  
250 =head4 RayIntersection($p1,$p2,$p3,$p4);
251  
252 Returns the intersection point of rays p1p2 and p3p4,
253 false if lines don't intersect (parallel rays)
254 p1 (p3) is the startpoint of the ray and p2 (p4) is
255 a point on the ray.
256  
257 =head4 RayLineIntersection($p1,$p2,$p3,$p4);
258  
259 Returns the intersection point of ray p1p2 and line p3p4,
260 false if lines don't intersect (parallel rays)
261 p1 is the startpoint of the ray and p2 is a point on the ray.
262  
263 =head4 SegmentLineIntersection($p1,$p2,$p3,$p4);
264  
265 Returns the intersection point of segment p1p2 and line p3p4,
266 false if lines don't intersect (parallel rays)
267  
268 =head4 SegmentRayIntersection($p1,$p2,$p3,$p4);
269  
270 Returns the intersection point of segment p1p2 and ray p3p4,
271 false if lines don't intersect (parallel rays)
272 p3 is the startpoint of the ray and p4 is a point on the ray.
273  
274 =head4 Perpendicular($p1,$p2,$p3,$p4);
275  
276 Returns true if lines (segments) p1p2 and p3p4 are perpendicular
277  
278 =head4 PerpendicularFoot($p1,$p2,$p3);
279  
280 Returns the perpendicular foot of p3 on line p1p2
281  
282 =head4 DistanceToLine($p1,$p2,$p3);
283  
284 Returns the perpendicular distance of p3 to line p1p2
285  
286 =head4 DistanceToSegment($p1,$p2,$p3);
287  
288 Returns the distance of p3 to segment p1p2. Depending on the point's
289 position, this is the distance to one of the endpoints or the
290 perpendicular distance to the segment.
291  
292 =head4 Gpc2Polygons($gpc_contour);
293  
294 Converts a gpc contour structure to an array of contours and returns the array
295  
296 =head4 GpcClip($operation,$gpc_contour_1,$gpc_contour_2);
297  
298 $operation is DIFFERENCE, INTERSECTION, XOR or UNION
299 $gpc_polygon_1 is the source polygon
300 $gpc_polygon_2 is the clip polygon
301  
302 Returns a gpc polygon structure which is the result of the gpc clipping operation
303  
304 =head4 CircleToPoly($i,$p1,$p2,$p3);
305  
306 Converts the circle through points p1p2p3 to a polygon with i segments
307  
308 =head4 CircleToPoly($i,$center,$p1);
309  
310 Converts the circle with center through point p1 to a polygon with i segments
311  
312 =head4 CircleToPoly($i,$center,$radius);
313  
314 Converts the circle with center and radius to a polygon with i segments
315  
316 =head4 ArcToPoly($i,$p1,$p2,$p3);
317  
318 Converts the arc with begin point p1, intermediate point p2 and end point p3
319 to a (non-closed !) polygon with i segments
320  
321 =head4 ArcToPoly($i,$center,$p1,$p2,$direction);
322  
323 Converts the arc with center, begin point p1 and end point p2 to a
324 (non-closed !) polygon with i segments. If direction is 0, the arc
325 is traversed counter clockwise from p1 to p2, clockwise if direction is 1
326  
327 =cut
328  
329 1 require 5.005;
330  
331 1 my $delta = 10 ** (-$precision);
332  
333 ################################################################################
334 #
335 # calculate length of a line segment
336 #
337 # args : reference to array with 2 points defining line segment
338 #
339 16 ▶sub SegmentLength {
Subroutine SegmentLength: called CC=2 SCAR=7.2
Pod undocumented
340 16 my $pointsref = $_[0];
341 16 my @points = @$pointsref;
342 16 ▶ if (@points != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 2) 0 16 16
343 0 carp("Need 2 points for a segment length calculation");
344 0 return;
345 }
346 16 my @a = @{$points[0]};
347 16 my @b = @{$points[1]};
348 16 my $length = sqrt(DotProduct([$points[0],$points[1],$points[0],$points[1]]));
349 16 return $length;
350 }
351 ################################################################################
352 #
353 # The determinant for the matrix | x1 y1 |
354 # | x2 y2 |
355 #
356 # args : x1,y1,x2,y2
357 #
358 468 ▶sub Determinant {
Subroutine Determinant: called CC=1 SCAR=0.0
Pod documented
359 468 my ($x1,$y1,$x2,$y2) = @_;
360 468 return ($x1*$y2 - $x2*$y1);
361 }
362 ################################################################################
363 #
364 # vector dot product
365 # calculates dotproduct vectors p1p2 and p3p4
366 # The dot product of a and b is written as a.b and is
367 # defined by a.b = |a|*|b|*cos q
368 #
369 # args : reference to an array with 4 points p1,p2,p3,p4 defining 2 vectors
370 # a = vector p1p2 and b = vector p3p4
371 # or
372 # reference to an array with 3 points p1,p2,p3 defining 2 vectors
373 # a = vector p1p2 and b = vector p1p3
374 #
375 27 ▶sub DotProduct {
Subroutine DotProduct: called CC=3 SCAR=13.9
Pod documented
376 27 my $pointsref = $_[0];
377 27 my @points = @$pointsref;
378 27 my (@p1,@p2,@p3,@p4);
379 27 ▶ if (@points == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (@points == 4) { } 27 0 27
elsif (@points == 3) { } 0 0 0
380 27 @p1 = @{$points[0]};
381 27 @p2 = @{$points[1]};
382 27 @p3 = @{$points[2]};
383 27 @p4 = @{$points[3]};
384 } elsif (@points == 3) {
385 0 @p1 = @{$points[0]};
386 0 @p2 = @{$points[1]};
387 0 @p3 = @{$points[0]};
388 0 @p4 = @{$points[2]};
389 } else {
390 0 carp("Need 3 or 4 points for a dot product");
391 0 return;
392 }
393 27 return ($p2[0]-$p1[0])*($p4[0]-$p3[0]) + ($p2[1]-$p1[1])*($p4[1]-$p3[1]);
394 }
395 ################################################################################
396 #
397 # returns vector cross product of vectors p1p2 and p1p3
398 # using Cramer's rule
399 #
400 # args : reference to an array with 3 points p1,p2 and p3
401 #
402 128 ▶sub CrossProduct {
Subroutine CrossProduct: called CC=2 SCAR=7.0
Pod documented
403 128 my $pointsref = $_[0];
404 128 my @points = @$pointsref;
405 128 ▶ if (@points != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 3) 0 128 128
406 0 carp("Need 3 points for a cross product");
407 0 return;
408 }
409 128 my @p1 = @{$points[0]};
410 128 my @p2 = @{$points[1]};
411 128 my @p3 = @{$points[2]};
412 128 my $det_p2p3 = &Determinant($p2[0], $p2[1], $p3[0], $p3[1]);
413 128 my $det_p1p3 = &Determinant($p1[0], $p1[1], $p3[0], $p3[1]);
414 128 my $det_p1p2 = &Determinant($p1[0], $p1[1], $p2[0], $p2[1]);
415 128 return ($det_p2p3-$det_p1p3+$det_p1p2);
416 }
417 ################################################################################
418 #
419 # The Cramer's Rule for area of a triangle is
420 # | x1 y1 1 |
421 # A = 1/2 * | x2 y2 1 |
422 # | x3 y3 1 |
423 # Which is 'half of the cross product of vectors ab and ac.
424 # The cross product of the vectors ab and ac is a vector perpendicular to the
425 # plane defined by ab and bc with a magnitude equal to the area of the
426 # parallelogram defined by a, b, c and ab + bc (vector sum)
427 # Don't forget that: (ab x ac) = - (ac x ab) (x = cross product)
428 # Which just means that if you reverse the vectors in the cross product,
429 # the resulting vector points in the opposite direction
430 # The direction of the resulting vector can be found with the "right hand rule"
431 # This can be used to determine the order of points a, b and c:
432 # clockwise or counter clockwise
433 #
434 # args : reference to an array with 3 points p1.p2,p3
435 #
436 7 ▶sub TriangleArea {
Subroutine TriangleArea: called CC=2 SCAR=7.9
Pod documented
437 7 my $pointsref = $_[0];
438 7 my @points = @$pointsref;
439 7 ▶ if (@points != 3) { # need 3 points for a triangle ...
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 3) 0 7 7
440 0 carp("A triangle should have exactly 3 points");
441 0 return;
442 }
443 7 return CrossProduct($pointsref)/2;
444 }
445 ################################################################################
446 #
447 # Check if 3 points are colinear
448 # Points are colinear if triangle area is 0
449 # Triangle area is crossproduct/2 so we can check the crossproduct instead
450 #
451 # args : reference to an array with 3 points p1.p2,p3
452 #
453 12 ▶sub Colinear {
Subroutine Colinear: called CC=2 SCAR=7.9
Pod documented
454 12 my $pointsref = $_[0];
455 12 my @points = @$pointsref;
456 12 ▶ if (@points != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 3) 0 12 12
457 0 carp("Colinear only checks colinearity for 3 points");
458 0 return;
459 }
460 # check the area of the triangle to find
461 12 return (abs(CrossProduct($pointsref)) < $delta);
462 }
463 ################################################################################
464 #
465 # calculate intersection point of 2 line segments
466 # returns false if segments don't intersect
467 # The theory:
468 #
469 # Parametric representation of a line
470 # if p1 (x1,y1) and p2 (x2,y2) are 2 points on a line and
471 # P1 is the vector from (0,0) to (x1,y1)
472 # P2 is the vector from (0,0) to (x2,y2)
473 # then the parametric representation of the line is P = P1 + k (P2 - P1)
474 # where k is an arbitrary scalar constant.
475 # for a point on the line segement (p1,p2) value of k is between 0 and 1
476 #
477 # for the 2 line segements we get
478 # Pa = P1 + k (P2 - P1)
479 # Pb = P3 + l (P4 - P3)
480 #
481 # For the intersection point Pa = Pb so we get the following equations
482 # x1 + k (x2 - x1) = x3 + l (x4 - x3)
483 # y1 + k (y2 - y1) = y3 + l (y4 - y3)
484 # Which using Cramer's Rule results in
485 # (x4 - x3)(y1 - y3) - (y4 - x3)(x1 - x3)
486 # k = ---------------------------------------
487 # (y4 - y3)(x2 - x1) - (x4 - x3)(y2 - y1)
488 # and
489 # (x2 - x1)(y1 - y3) - (y2 - y1)(x1 - x3)
490 # l = ---------------------------------------
491 # (y4 - y3)(x2 - x1) - (x4 - x3)(y2 - y1)
492 #
493 # Note that the denominators are equal. If the denominator is 9,
494 # the lines are parallel. Intersection is detected by checking if
495 # both k and l are between 0 and 1.
496 #
497 # The intersection point p5 (x5,y5) is:
498 # x5 = x1 + k (x2 - x1)
499 # y5 = y1 + k (y2 - y1)
500 #
501 # 'Touching' segments are considered as not intersecting
502 #
503 # args : reference to an array with 4 points p1,p2,p3,p4
504 #
505 11 ▶sub SegmentIntersection {
Subroutine SegmentIntersection: called CC=7 SCAR=19.7
Pod documented
506 11 my $pointsref = $_[0];
507 11 my @points = @$pointsref;
508 11 ▶ if (@points != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 4) 0 11 11
509 0 carp("SegmentIntersection needs 4 points");
510 0 return;
511 }
512 11 my @p1 = @{$points[0]}; # p1,p2 = segment 1
513 11 my @p2 = @{$points[1]};
514 11 my @p3 = @{$points[2]}; # p3,p4 = segment 2
515 11 my @p4 = @{$points[3]};
516 11 my @p5;
517 11 my $n1 = Determinant(($p3[0]-$p1[0]),($p3[0]-$p4[0]),($p3[1]-$p1[1]),($p3[1]-$p4[1]));
518 11 my $n2 = Determinant(($p2[0]-$p1[0]),($p3[0]-$p1[0]),($p2[1]-$p1[1]),($p3[1]-$p1[1]));
519 11 my $d = Determinant(($p2[0]-$p1[0]),($p3[0]-$p4[0]),($p2[1]-$p1[1]),($p3[1]-$p4[1]));
520 11 ▶ if (abs($d) < $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $d < $delta) 0 11 11
521 0 return 0; # parallel
522 }
523 11 ▶ if (!(($n1/$d < 1) && ($n2/$d < 1) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($n1 / $d < 1 and $n2 / $d < 1 and $n1 / $d > 0 and $n2 / $d > 0) 8 3 11
Condition88%8/9
$n1 / $d < 1 and $n2 / $d < 1 and $n1 / $d > 0 and $n2 / $d > 0
!ll&&!rl&&r
803
$n1 / $d < 1 and $n2 / $d < 1 and $n1 / $d > 0
!ll&&!rl&&r
713
$n1 / $d < 1 and $n2 / $d < 1
!ll&&!rl&&r
434
MC/DC75%3/4
$n1 / $d < 1 and $n2 / $d < 1 and $n1 / $d > 0 and $n2 / $d > 0
$n1 / $d < 1 $n2 / $d < 1 $n1 / $d > 0 $n2 / $d > 0
Truth table80%4/5
A and B and C and D
A= $n1 / $d < 1B= $n2 / $d < 1C= $n1 / $d > 0D= $n2 / $d > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
524 ($n1/$d > 0) && ($n2/$d > 0))) {
525 8 return 0;
526 }
527 3 $p5[0] = $p1[0] + $n1/$d * ($p2[0] - $p1[0]);
528 3 $p5[1] = $p1[1] + $n1/$d * ($p2[1] - $p1[1]);
529 3 return \@p5; # intersection point
530 }
531 ################################################################################
532 #
533 # Intersection point of 2 lines - (almost) identical as for Segments
534 # each line is defined by 2 points
535 #
536 # args : reference to an array with 4 points p1,p2,p3,p4
537 #
538 15 ▶sub LineIntersection {
Subroutine LineIntersection: called CC=3 SCAR=11.0
Pod documented
539 15 my $pointsref = $_[0];
540 15 my @points = @$pointsref;
541 15 ▶ if (@points < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 4) 0 15 15
542 0 carp("LineIntersection needs 4 points");
543 0 return;
544 }
545 15 my @p1 = @{$points[0]}; # p1,p2 = line 1
546 15 my @p2 = @{$points[1]};
547 15 my @p3 = @{$points[2]}; # p3,p4 = line 2
548 15 my @p4 = @{$points[3]};
549 15 my @p5;
550 15 my $n1 = Determinant(($p3[0]-$p1[0]),($p3[0]-$p4[0]),($p3[1]-$p1[1]),($p3[1]-$p4[1]));
551 15 my $d = Determinant(($p2[0]-$p1[0]),($p3[0]-$p4[0]),($p2[1]-$p1[1]),($p3[1]-$p4[1]));
552 15 ▶ if (abs($d) < $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $d < $delta) 1 14 15
553 1 return 0; # parallel
554 }
555 14 $p5[0] = $p1[0] + $n1/$d * ($p2[0] - $p1[0]);
556 14 $p5[1] = $p1[1] + $n1/$d * ($p2[1] - $p1[1]);
557 14 return \@p5; # intersection point
558 }
559 ################################################################################
560 #
561 # Intersection point of 2 rays
562 #
563 # args : reference to an array with 4 points p1,p2,p3,p4
564 #
565 # Parametric representation of a ray
566 # if p1 (x1,y1) is the startpoint of the ray
567 # and p2 (x2,y2) are is a point on the ray then
568 # P1 is the vector from (0,0) to (x1,y1)
569 # P2 is the vector from (0,0) to (x2,y2)
570 # then the parametric representation of the ray is P = P1 + k (P2 - P1)
571 # where k is an arbitrary scalar constant.
572 # for a point on the line segement (p1,p2) value of k is positive
573 #
574 # (A ray is often represented as a single point and a direction # 'theta'
575 # in this case, one can easily define a second point as
576 # x2 = x1 + cos(theta) and y2 = y2 + sin(theta) )
577 #
578 # for the 2 rays we get
579 # Pa = P1 + k (P2 - P1)
580 # Pb = P3 + l (P4 - P3)
581 #
582 # Touching rays are considered as not intersectin
583 #
584 2 ▶sub RayIntersection {
Subroutine RayIntersection: called CC=5 SCAR=16.4
Pod documented
585 2 my $pointsref = $_[0];
586 2 my @points = @$pointsref;
587 2 ▶ if (@points != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 4) 0 2 2
588 0 carp("RayIntersection needs 4 points");
589 0 return;
590 }
591 2 my @p1 = @{$points[0]}; # p1,p2 = segment 1 (startpoint is p1)
592 2 my @p2 = @{$points[1]};
593 2 my @p3 = @{$points[2]}; # p3,p4 = segment 2 (startpoint is p3)
594 2 my @p4 = @{$points[3]};
595 2 my @p5;
596 2 my $n1 = Determinant(($p3[0]-$p1[0]),($p3[0]-$p4[0]),($p3[1]-$p1[1]),($p3[1]-$p4[1]));
597 2 my $n2 = Determinant(($p2[0]-$p1[0]),($p3[0]-$p1[0]),($p2[1]-$p1[1]),($p3[1]-$p1[1]));
598 2 my $d = Determinant(($p2[0]-$p1[0]),($p3[0]-$p4[0]),($p2[1]-$p1[1]),($p3[1]-$p4[1]));
599 2 ▶ if (abs($d) < $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $d < $delta) 0 2 2
600 0 return 0; # parallel
601 }
602 2 ▶ if (!( ($n1/$d > 0) && ($n2/$d > 0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n1 / $d > 0 and $n2 / $d > 0) 1 1 2
Condition66%2/3
$n1 / $d > 0 and $n2 / $d > 0
!ll&&!rl&&r
101
MC/DC50%1/2
$n1 / $d > 0 and $n2 / $d > 0
$n1 / $d > 0 $n2 / $d > 0
Truth table66%2/3
A and B
A= $n1 / $d > 0B= $n2 / $d > 0
ABresult
0X0
100
111
603 1 return 0;
604 }
605 1 $p5[0] = $p1[0] + $n1/$d * ($p2[0] - $p1[0]);
606 1 $p5[1] = $p1[1] + $n1/$d * ($p2[1] - $p1[1]);
607 1 return \@p5; # intersection point
608 }
609 ################################################################################
610 #
611 # Intersection point of a segment and a line
612 #
613 # args : reference to an array with 4 points p1,p2,p3,p4
614 #
615 2 ▶sub SegmentLineIntersection {
Subroutine SegmentLineIntersection: called CC=5 SCAR=16.4
Pod documented
616 2 my $pointsref = $_[0];
617 2 my @points = @$pointsref;
618 2 ▶ if (@points != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 4) 0 2 2
619 0 carp("SegmentLineIntersection needs 4 points");
620 0 return;
621 }
622 2 my @p1 = @{$points[0]}; # p1,p2 = segment
623 2 my @p2 = @{$points[1]};
624 2 my @p3 = @{$points[2]}; # p3,p4 = line
625 2 my @p4 = @{$points[3]};
626 2 my @p5;
627 2 my $n1 = Determinant(($p3[0]-$p1[0]),($p3[0]-$p4[0]),($p3[1]-$p1[1]),($p3[1]-$p4[1]));
628 2 my $d = Determinant(($p2[0]-$p1[0]),($p3[0]-$p4[0]),($p2[1]-$p1[1]),($p3[1]-$p4[1]));
629 2 ▶ if (abs($d) < $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $d < $delta) 0 2 2
630 0 return 0; # parallel
631 }
632 2 ▶ if (!(($n1/$d < 1) && ($n1/$d > 0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n1 / $d < 1 and $n1 / $d > 0) 1 1 2
Condition66%2/3
$n1 / $d < 1 and $n1 / $d > 0
!ll&&!rl&&r
101
MC/DC50%1/2
$n1 / $d < 1 and $n1 / $d > 0
$n1 / $d < 1 $n1 / $d > 0
Truth table66%2/3
A and B
A= $n1 / $d < 1B= $n1 / $d > 0
ABresult
0X0
100
111
633 1 return 0;
634 }
635 1 $p5[0] = $p1[0] + $n1/$d * ($p2[0] - $p1[0]);
636 1 $p5[1] = $p1[1] + $n1/$d * ($p2[1] - $p1[1]);
637 1 return \@p5; # intersection point
638 }
639 ################################################################################
640 #
641 # Intersection point of a ray and a line
642 #
643 # args : reference to an array with 4 points p1,p2,p3,p4
644 #
645 2 ▶sub RayLineIntersection {
Subroutine RayLineIntersection: called CC=4 SCAR=14.1
Pod documented
646 2 my $pointsref = $_[0];
647 2 my @points = @$pointsref;
648 2 ▶ if (@points != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 4) 0 2 2
649 0 carp("RayLineIntersection needs 4 points");
650 0 return;
651 }
652 2 my @p1 = @{$points[0]}; # p1,p2 = ray (startpoint p1)
653 2 my @p2 = @{$points[1]};
654 2 my @p3 = @{$points[2]}; # p3,p4 = line
655 2 my @p4 = @{$points[3]};
656 2 my @p5;
657 2 my $n1 = Determinant(($p3[0]-$p1[0]),($p3[0]-$p4[0]),($p3[1]-$p1[1]),($p3[1]-$p4[1]));
658 2 my $d = Determinant(($p2[0]-$p1[0]),($p3[0]-$p4[0]),($p2[1]-$p1[1]),($p3[1]-$p4[1]));
659 2 ▶ if (abs($d) < $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $d < $delta) 0 2 2
660 0 return 0; # parallel
661 }
662 2 ▶ if (!($n1/$d > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n1 / $d > 0) 1 1 2
663 1 return 0;
664 }
665 1 $p5[0] = $p1[0] + $n1/$d * ($p2[0] - $p1[0]);
666 1 $p5[1] = $p1[1] + $n1/$d * ($p2[1] - $p1[1]);
667 1 return \@p5; # intersection point
668 }
669 ################################################################################
670 #
671 # Intersection point of a segment and a ray
672 #
673 # args : reference to an array with 4 points p1,p2,p3,p4
674 #
675 2 ▶sub SegmentRayIntersection {
Subroutine SegmentRayIntersection: called CC=6 SCAR=18.6
Pod documented
676 2 my $pointsref = $_[0];
677 2 my @points = @$pointsref;
678 2 ▶ if (@points != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 4) 0 2 2
679 0 carp("SegmentRayIntersection needs 4 points");
680 0 return;
681 }
682 2 my @p1 = @{$points[0]}; # p1,p2 = segment
683 2 my @p2 = @{$points[1]};
684 2 my @p3 = @{$points[2]}; # p3,p4 = ray (startpoint p3)
685 2 my @p4 = @{$points[3]};
686 2 my @p5;
687 2 my $n1 = Determinant(($p3[0]-$p1[0]),($p3[0]-$p4[0]),($p3[1]-$p1[1]),($p3[1]-$p4[1]));
688 2 my $n2 = Determinant(($p2[0]-$p1[0]),($p3[0]-$p1[0]),($p2[1]-$p1[1]),($p3[1]-$p1[1]));
689 2 my $d = Determinant(($p2[0]-$p1[0]),($p3[0]-$p4[0]),($p2[1]-$p1[1]),($p3[1]-$p4[1]));
690 2 ▶ if (abs($d) < $delta) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $d < $delta) 0 2 2
691 0 return 0; # parallel
692 }
693 2 ▶ if (!(($n1/$d < 1) && ($n1/$d > 0) && ($n2/$d > 0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n1 / $d < 1 and $n1 / $d > 0 and $n2 / $d > 0) 1 1 2
Condition50%3/6
$n1 / $d < 1 and $n1 / $d > 0 and $n2 / $d > 0
!ll&&!rl&&r
011
$n1 / $d < 1 and $n1 / $d > 0
!ll&&!rl&&r
002
MC/DC33%1/3
$n1 / $d < 1 and $n1 / $d > 0 and $n2 / $d > 0
$n1 / $d < 1 $n1 / $d > 0 $n2 / $d > 0
Truth table50%2/4
A and B and C
A= $n1 / $d < 1B= $n1 / $d > 0C= $n2 / $d > 0
ABCresult
0XX0
10X0
1100
1111
694 1 return 0;
695 }
696 1 $p5[0] = $p1[0] + $n1/$d * ($p2[0] - $p1[0]);
697 1 $p5[1] = $p1[1] + $n1/$d * ($p2[1] - $p1[1]);
698 1 return \@p5; # intersection point
699 }
700 ################################################################################
701 #
702 # returns true if 2 lines (segments) are perpendicular
703 # Lines are perpendicular if dot product is 0
704 #
705 # args : reference to an array with 4 points p1,p2,p3,p4
706 # p1p2 = line 1
707 # p3p4 = line 2
708 #
709 2 ▶sub Perpendicular {
Subroutine Perpendicular: called CC=2 SCAR=7.9
Pod documented
710 2 my $pointsref = $_[0];
711 2 my @points = @$pointsref;
712 2 ▶ if (@points != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 4) 0 2 2
713 0 carp("Perpendicular needs 4 points defining 2 lines or segments");
714 0 return;
715 }
716 2 return (abs(DotProduct([$points[0],$points[1],$points[2],$points[3]])) < $delta);
717 }
718 ################################################################################
719 #
720 # Calculates the 'perpendicular foot' of a point on a line
721 #
722 # args: reference to array with 3 points p1,p2,p3
723 # p1p2 = line
724 # p3 = point for which perpendicular foot is to be calculated
725 #
726 13 ▶sub PerpendicularFoot {
Subroutine PerpendicularFoot: called CC=2 SCAR=7.0
Pod documented
727 13 my $pointsref = $_[0];
728 13 my @points = @$pointsref;
729 13 ▶ if (@points != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points != 3) 0 13 13
730 0 carp("PerpendicularFoot needs 3 points defining a line and a point");
731 0 return;
732 }
733 13 my @p1 = @{$points[0]}; # p1,p2 = line
734 13 my @p2 = @{$points[1]};
735 13 my @p3 = @{$points[2]}; # p3 point
736 # vector penpenidular to line
737 13 my @v;
738 13 $v[0] = $p2[1] - $p1[1]; # y2-y1
739 13 $v[1] = - ($p2[0] - $p1[0]); # -(x2-x1);
740 # p4 = v + p3 is a second point of the line perpendicular to p1p2 going through p3
741 13 my @p4;
742 13 $p4[0] = $p3[0] + $v[0];
743 13 $p4[1] = $p3[1] + $v[1];
744 13 return LineIntersection([\@p1,\@p2,\@p3,\@p4]);
745 }
746 ################################################################################
747 #
748 # Calculate distance from point p to line segment p1p2
749 #
750 # args: reference to array with 3 points: p1,p2,p3
751 # p1p2 = segment
752 # p3 = point for which distance is to be calculated
753 # returns distance from p3 to line segment p1p2
754 # which is the smallest value from:
755 # distance p3p1
756 # distance p3p2
757 # perpendicular distance from p3 to line p1p2
758 #
759 3 ▶sub DistanceToSegment {
Subroutine DistanceToSegment: called CC=4 SCAR=14.0
Pod documented
760 3 my $pointsref = $_[0];
761 3 my @points = @$pointsref;
762 3 ▶ if (@points < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 3 3
763 0 carp("DistanceToSegment needs 3 points defining a segment and a point");
764 0 return;
765 }
766 # the perpendicular distance is the height of the parallelogram defined
767 # by the 3 points devided by the base
768 # Note the this is a signed value so it can be used to check at which
769 # side the point is located
770 # we use dot products to find out where point is located1G/dotpro
771 3 my $d1 = DotProduct([$points[0],$points[1],$points[0],$points[2]]);
772 3 my $d2 = DotProduct([$points[0],$points[1],$points[0],$points[1]]);
773 3 my $dp = CrossProduct([$points[2],$points[0],$points[1]]) / sqrt $d2;
774 3 ▶ if ($d1 <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d1 <= 0) { } 1 2 3
elsif ($d2 <= $d1) { } 1 1 2
775 1 return SegmentLength([$points[2],$points[0]]);
776 } elsif ($d2 <= $d1) {
777 1 return SegmentLength([$points[2],$points[1]]);
778 } else {
779 1 return $dp;
780 }
781 }
782 ################################################################################
783 #
784 # Calculate distance from point p to line p1p2
785 #
786 # args: reference to array with 3 points: p1,p2,p3
787 # p1p2 = line
788 # p3 = point for which distance is to be calculated
789 # returns 2 numbers
790 # - perpendicular distance from p3 to line p1p2
791 # - distance from p3 to line segment p1p2
792 # which is the smallest value from:
793 # distance p3p1
794 # distance p3p2
795 #
796 1 ▶sub DistanceToLine {
Subroutine DistanceToLine: called CC=2 SCAR=7.5
Pod documented
797 1 my $pointsref = $_[0];
798 1 my @points = @$pointsref;
799 1 ▶ if (@points < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 1 1
800 0 carp("DistanceToLine needs 3 points defining a line and a point");
801 0 return;
802 }
803 # the perpendicular distance is the height of the parallelogram defined
804 # by the 3 points devided by the base
805 # Note the this is a signed value so it can be used to check at which
806 # side the point is located
807 # we use dot products to find out where point is located1G/dotpro
808 1 my $d = DotProduct([$points[0],$points[1],$points[0],$points[1]]);
809 1 my $dp = CrossProduct([$points[2],$points[0],$points[1]]) / sqrt $d;
810 1 return $dp;
811 }
812 ################################################################################
813 #
814 # Initializer
815 #
816 43 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
817 43 my $invocant = shift;
818 43 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
0430
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
819 43 my $self = { @_ };
820 43 bless($self,$class);
821 43 return $self;
822 }
823 ################################################################################
824 #
825 # args: reference to polygon object
826 #
827 144 ▶sub points {
Subroutine points: called CC=4 SCAR=14.0
Pod documented
828 144 my Math::Geometry::Planar $self = shift;
829 144 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 49 95 144
830 49 ▶ if ($self->get_polygons) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_polygons) { } 0 49 49
831 0 carp("Object is a contour - can't add points");
832 0 return;
833 } else {
834 # delete existing info
835 49 $self->{points} = ();
836 49 my $pointsref = shift;
837 # normalize (a single polygon has only an outer shape
838 # -> make points order counter clockwise)
839 49 ▶ if (PolygonArea($pointsref) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PolygonArea($pointsref) > 0) { } 29 20 49
840 29 $self->{points} = $pointsref;
841 } else {
842 20 $self->{points} = [reverse @{$pointsref}];
843 }
844 }
845 }
846 144 return $self->{points};
847 }
848 ################################################################################
849 #
850 # args: reference to polygon object
851 #
852 22 ▶sub polygons {
Subroutine polygons: called CC=3 SCAR=11.2
Pod documented
853 22 my Math::Geometry::Planar $self = shift;
854 22 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 9 22
855 13 ▶ if ($self->points) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->points) { } 0 13 13
856 0 carp("Object is a polygon - can't add polygons");
857 0 return;
858 } else {
859 # delete existing info
860 13 $self->{polygons} = ();
861 13 my $polygons = shift;
862 13 my @polygonrefs = @{$polygons};
863 13 $self->add_polygons(@polygonrefs);
864 }
865 }
866 22 return $self->{polygons};
867 }
868 ################################################################################
869 #
870 # args: none
871 # returns the number of polygons in the contour
872 #
873 32 ▶sub num_polygons {
Subroutine num_polygons: called CC=2 SCAR=6.9
Pod documented
874 32 my Math::Geometry::Planar $self = shift;
875 32 my $polygons = $self->{polygons};
876 32 ▶ return 0 if (! $polygons);
Branch: 1 branch
BranchTrueFalseTotal
unless $polygons 19 13 32
877 13 return scalar @{$polygons};
878 }
879 ################################################################################
880 #
881 # args: list of references to polygons
882 # returns the number of polygons in the contour
883 #
884 23 ▶sub add_polygons {
Subroutine add_polygons: called CC=7 SCAR=19.6
Pod documented
885 23 my Math::Geometry::Planar $self = shift;
886 23 ▶ return if (! @_); # nothing to add
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 23 23
887 # can't add polygons to a polygon object
888 23 ▶ if ($self->points) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->points) 0 23 23
889 0 carp("Object is a polygon - can't add polygons");
890 0 return;
891 }
892 # first polygon is outer polygon
893 23 ▶ if (! $self->num_polygons) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->num_polygons) 19 4 23
894 19 my $outer = shift;
895 # counter clockwise for outer polygon
896 19 ▶ if (PolygonArea($outer) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PolygonArea($outer) < 0) { } 12 7 19
897 12 push @{$self->{polygons}}, [reverse @{$outer}];
898 } else {
899 7 push @{$self->{polygons}}, $outer;
900 }
901 }
902 # inner polygon(s)
903 23 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 23 33
904 # clockwise for inner polygon
905 10 my $inner = shift;
906 10 ▶ if (PolygonArea($inner) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (PolygonArea($inner) > 0) { } 3 7 10
907 3 push @{$self->{polygons}}, [reverse @{$inner}];
908 } else {
909 7 push @{$self->{polygons}}, $inner;
910 }
911 }
912 23 return scalar @{$self->{polygons}};
913 }
914 ################################################################################
915 #
916 # args: list of indices
917 # returns list of polygons indicated by indices
918 # (list value at position n is undefined if the index at position
919 # n is out of range)
920 # list of all polygons indicated by indices
921 #
922 67 ▶sub get_polygons {
Subroutine get_polygons: called CC=6 SCAR=18.1
Pod documented
923 67 my Math::Geometry::Planar $self = shift;
924 67 my @result;
925 67 my $polygons = $self->{polygons};
926 67 ▶ return if (! $polygons);
Branch: 1 branch
BranchTrueFalseTotal
unless $polygons 49 18 67
927 18 my $i = 0;
928 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 6 12 18
929 6 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 6 13
930 7 my $index = int shift;
931 7 ▶ if ($index >= 0 && $index < num_polygons($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0 and $index < num_polygons($self)) { } 7 0 7
Condition33%1/3
$index >= 0 and $index < num_polygons($self)
!ll&&!rl&&r
007
MC/DC0%0/2
$index >= 0 and $index < num_polygons($self)
$index >= 0 $index < num_polygons($self)
Truth table33%1/3
A and B
A= $index >= 0B= $index < num_polygons($self)
ABresult
0X0
100
111
932 7 $result[$i] = ${$polygons}[$index];
933 } else {
934 0 $result[$i] = undef;
935 }
936 7 $i++;
937 }
938 6 return @result;
939 } else {
940 12 return @{$polygons};
941 }
942 }
943 ################################################################################
944 # cleanup polygon = remove colinear points
945 #
946 # args: reference to polygon or contour object
947 #
948 2 ▶sub cleanup {
Subroutine cleanup: called CC=9 SCAR=22.0
Pod documented
949 2 my ($self) = @_;
950 2 my $pointsref = $self->points;
951 2 ▶ if ($pointsref) { # polygon object
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 1 2
952 1 my @points = @$pointsref;
953 1 ▶ for (my $i=0 ; $i< @points && @points > 2 ;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points and @points > 2) 5 1 6
Condition66%2/3
$i < @points and @points > 2
!ll&&!rl&&r
105
MC/DC50%1/2
$i < @points and @points > 2
$i < @points @points > 2
Truth table66%2/3
A and B
A= $i < @pointsB= @points > 2
ABresult
0X0
100
111
954 5 ▶ if (Colinear([$points[$i-2],$points[$i-1],$points[$i]])) {
Branch: 1 branch
BranchTrueFalseTotal
if (Colinear([$points[$i - 2], $points[$i - 1], $points[$i]])) 1 4 5
955 1 splice @points,$i-1,1;
956 1 $i--;
957 }
958 }
959 # replace polygon points
960 1 $self->points([@points]);
961 1 return [@points];
962 } else { # contour object
963 1 my @polygonrefs = $self->get_polygons;
964 1 ▶ for (my $j = 0; $j < @polygonrefs; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @polygonrefs) 1 1 2
965 1 $pointsref = $polygonrefs[$j];
966 1 my @points = @$pointsref;
967 1 ▶ for (my $i=0 ; $i< @points && @points > 2 ;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points and @points > 2) 5 1 6
Condition66%2/3
$i < @points and @points > 2
!ll&&!rl&&r
105
MC/DC50%1/2
$i < @points and @points > 2
$i < @points @points > 2
Truth table66%2/3
A and B
A= $i < @pointsB= @points > 2
ABresult
0X0
100
111
968 5 ▶ if (Colinear([$points[$i-2],$points[$i-1],$points[$i]])) {
Branch: 1 branch
BranchTrueFalseTotal
if (Colinear([$points[$i - 2], $points[$i - 1], $points[$i]])) 1 4 5
969 1 splice @points,$i-1,1;
970 1 $i--;
971 }
972 }
973 1 $polygonrefs[$j] = [@points];
974 }
975 1 $self->polygons([@polygonrefs]);
976 1 return [@polygonrefs];
977 }
978 }
979 ################################################################################
980 #
981 # Ah - more vector algebra
982 # We consider every set of 3 subsequent points p1,p2,p3 on the polygon and calculate
983 # the vector product of the vectors p1p2 and p1p3. All these products should
984 # have the same sign. If the sign changes, the polygon is not convex
985 #
986 # make sure to remove colinear points first before calling perimeter
987 # (I prefer not to include the call to cleanup)
988 #
989 # args: reference to polygon or contour object
990 # (for a contour we only check the outer shape)
991 #
992 3 ▶sub isconvex {
Subroutine isconvex: called CC=9 SCAR=22.1
Pod documented
993 3 my ($self) = @_;
994 3 my $pointsref = $self->points;
995 3 ▶ if (! $pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointsref) 1 2 3
996 1 $pointsref = ($self->get_polygons(0))[0];
997 1 ▶ return if (! $pointsref); # empty object
Branch: 1 branch
BranchTrueFalseTotal
unless $pointsref 0 1 1
998 }
999 3 my @points = @$pointsref;
1000 3 ▶ return 1 if (@points < 5); # every poly with a less then 5 points is convex
Branch: 1 branch
BranchTrueFalseTotal
if @points < 5 1 2 3
1001 2 my $prev = 0;
1002 2 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 9 1 10
1003 9 my $tmp = CrossProduct([$points[$i-2],$points[$i-1],$points[$i]]);
1004 # check if sign is different from pervious one(s)
1005 9 ▶ if ( ($prev < 0 && $tmp > 0) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($prev < 0 and $tmp > 0 or $prev > 0 and $tmp < 0) 1 8 9
Condition66%6/9
$prev < 0 and $tmp > 0 or $prev > 0 and $tmp < 0
l!l&&r!l&&!r
018
$prev < 0 and $tmp > 0
!ll&&!rl&&r
900
$prev > 0 and $tmp < 0
!ll&&!rl&&r
351
MC/DC50%2/4
$prev < 0 and $tmp > 0 or $prev > 0 and $tmp < 0
$prev < 0 $tmp > 0 $prev > 0 $tmp < 0
Truth table42%3/7
A and B or C and D
A= $prev < 0B= $tmp > 0C= $prev > 0D= $tmp < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1006 ($prev > 0 && $tmp < 0) ) {
1007 1 return 0;
1008 }
1009 8 $prev = $tmp;
1010 }
1011 1 return 1;
1012 }
1013 ################################################################################
1014 #
1015 # Brute force attack:
1016 # just check intersection for every segment versus every other segment
1017 # so for a polygon with n ponts this will take n**2 intersection calculations
1018 # I added a few simple improvements: to boost speed:
1019 # - don't check adjacant segments
1020 # - don't check against 'previous' segments (if we checked segment x versus y,
1021 # we don't need to check y versus x anymore)
1022 # Results in (n-2)*(n-1)/2 - 1 checks which is close to n**2/2 for large n
1023 #
1024 # make sure to remove colinear points first before calling perimeter
1025 # (I prefer not to include the call to cleanup)
1026 #
1027 # args: reference to polygon or contour object
1028 # (a contour is considered to be simple if all it's shapes are simple)
1029 #
1030 5 ▶sub IsSimplePolygon {
Subroutine IsSimplePolygon: called CC=7 SCAR=19.5
Pod undocumented
1031 5 my ($pointsref) = @_;
1032 5 my @points = @$pointsref;
1033 5 ▶ return 1 if (@points < 4); # triangles are simple polygons ...
Branch: 1 branch
BranchTrueFalseTotal
if @points < 4 0 5 5
1034 5 ▶ for (my $i = 0 ; $i < @points-2 ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points - 2) 8 3 11
1035 # check versus all next non-adjacant edges
1036 8 ▶ for (my $j = $i+2 ; $j < @points ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @points) 11 6 17
1037 # don't check first versus last segment (adjacant)
1038 11 ▶ next if ($i == 0 && $j == @points-1);
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $j == @points - 1 3 8 11
Condition100%3/3
$i == 0 and $j == @points - 1
!ll&&!rl&&r
353
MC/DC100%2/2
$i == 0 and $j == @points - 1
$i == 0 $j == @points - 1
Truth table100%3/3
A and B
A= $i == 0B= $j == @points - 1
ABresult
0X0
100
111
1039 8 ▶ if (SegmentIntersection([$points[$i-1],$points[$i],$points[$j-1],$points[$j]])) {
Branch: 1 branch
BranchTrueFalseTotal
if (SegmentIntersection([$points[$i - 1], $points[$i], $points[$j - 1], $points[$j]])) 2 6 8
1040 2 return 0;
1041 }
1042 }
1043 }
1044 3 return 1;
1045 }
1046 ################################################################################
1047 #
1048 # Check if polyogn or contour is simple
1049 4 ▶sub issimple {
Subroutine issimple: called CC=4 SCAR=13.9
Pod documented
1050 4 my ($self) = @_;
1051 4 my $pointsref = $self->points;
1052 4 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 2 2 4
1053 2 return IsSimplePolygon($pointsref);
1054 } else {
1055 2 my @polygonrefs = $self->get_polygons;
1056 2 my @result;
1057 2 foreach (@polygonrefs) {
1058 3 ▶ return 0 if (! IsSimplePolygon($_));
Branch: 1 branch
BranchTrueFalseTotal
unless IsSimplePolygon($_) 1 2 3
1059 }
1060 1 return 1;
1061 }
1062 }
1063 ################################################################################
1064 # makes only sense for simple polygons
1065 # make sure to remove colinear points first before calling perimeter
1066 # (I prefer not to include the call to colinear)
1067 #
1068 # args: reference to polygon or contour object
1069 # returns the perimeter of the polygon or the perimeter of the outer shape of
1070 # the contour
1071 #
1072 2 ▶sub perimeter {
Subroutine perimeter: called CC=6 SCAR=18.4
Pod documented
1073 2 my ($self) = @_;
1074 2 my $pointsref = $self->points;
1075 2 ▶ if (! $pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointsref) 1 1 2
1076 1 $pointsref = ($self->get_polygons(0))[0];
1077 1 ▶ return if (! $pointsref); # empty object
Branch: 1 branch
BranchTrueFalseTotal
unless $pointsref 0 1 1
1078 }
1079 2 my @points = @$pointsref;
1080 2 my $perimeter = 0;
1081 2 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) 2 0 2
1082 2 my @points = @$pointsref;
1083 2 ▶ if (@points < 3) { # no perimeter for lines and points
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 2 2
1084 0 carp("Can't calculate perimeter: polygon should have at least 3 points");
1085 0 return;
1086 }
1087 2 ▶ for (my $index=0;$index < @points; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @points) 8 2 10
1088 8 $perimeter += SegmentLength([$points[$index-1],$points[$index]]);
1089 }
1090 }
1091 2 return $perimeter;
1092 }
1093 ################################################################################
1094 # makes only sense for simple polygons
1095 # make sure to remove colinear points first before calling area
1096 # returns a signed value, can be used to find out whether
1097 # the order of points is clockwise or counter clockwise
1098 # (I prefer not to include the call to colinear)
1099 #
1100 # args: reference to an array of points
1101 #
1102 80 ▶sub PolygonArea {
Subroutine PolygonArea: called CC=3 SCAR=11.2
Pod undocumented
1103 80 my $pointsref = $_[0];
1104 80 my @points = @$pointsref;
1105 80 ▶ if (@points < 3) { # no area for lines and points
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 80 80
1106 0 carp("Can't calculate area: polygon should have at least 3 points");
1107 0 return;
1108 }
1109 80 push @points,$points[0]; # provide closure
1110 80 my $area = 0;
1111 80 ▶ while(@points >= 2){
Branch: 1 branch
BranchTrueFalseTotal
if (@points >= 2) 361 80 441
1112 361 $area += $points[0]->[0]*$points[1]->[1] - $points[1]->[0]*$points[0]->[1];
1113 361 shift @points;
1114 }
1115 80 return $area/2.0;
1116 }
1117 ################################################################################
1118 # Calculates the area of a polygon or a contour
1119 # Makes only sense for simple polygons
1120 # Returns a signed value so it can be used to find out whether
1121 # the order of points in a polygon is clockwise or counter
1122 # clockwise.
1123 #
1124 # args: reference to polygon or contour object
1125 #
1126 2 ▶sub area {
Subroutine area: called CC=3 SCAR=11.0
Pod documented
1127 2 my ($self) = @_;
1128 2 my $pointsref = $self->points;
1129 2 my $area = 0;
1130 2 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 1 2
1131 1 $area = PolygonArea($pointsref);
1132 } else {
1133 1 my @polygonrefs = $self->get_polygons;
1134 1 foreach (@polygonrefs) {
1135 1 $area += PolygonArea($_);
1136 }
1137 }
1138 2 return $area;
1139 }
1140 ################################################################################
1141 #
1142 # calculate the centroid of a polygon or contour
1143 # (a.k.a. the center of mass a.k.a. the center of gravity)
1144 #
1145 # The centroid is calculated as the weighted sum of the centroids
1146 # of a partition of the polygon into triangles. The centroid of a
1147 # triangle is simply the average of its three vertices, i.e., it
1148 # has coordinates (x1 + x2 + x3)/3 and (y1 + y2 + y3)/3.
1149 # In fact, the triangulation need not be a partition, but rather
1150 # can use positively and negatively oriented triangles (with positive
1151 # and negative areas), as is used when computing the area of a polygon
1152 #
1153 # makes only sense for simple polygons
1154 # make sure to remove colinear points first before calling centroid
1155 # (I prefer not to include the call to cleanup)
1156 #
1157 # args: reference to polygon object
1158 #
1159 1 ▶sub centroid {
Subroutine centroid: called CC=3 SCAR=11.1
Pod documented
1160 1 my ($self) = @_;
1161 1 my @triangles = $self->triangulate;
1162  
1163 1 ▶ if (! @triangles) { # no result from triangulation
Branch: 1 branch
BranchTrueFalseTotal
unless (@triangles) 0 1 1
1164 0 carp("Nothing to calculate centroid for");
1165 0 return;
1166 }
1167  
1168 1 my @c;
1169 my $total_area;
1170 # triangulate
1171 1 foreach my $triangleref (@triangles) {
1172 6 my @triangle = @{$triangleref->points};
1173 6 my $area = TriangleArea([$triangle[0],$triangle[1],$triangle[2]]);
1174 # weighted centroid = area * centroid = area * sum / 3
1175 # we postpone division by 3 till we divide by total area to
1176 # minimize number of calculations
1177 6 $c[0] += ($triangle[0][0]+$triangle[1][0]+$triangle[2][0]) * $area;
1178 6 $c[1] += ($triangle[0][1]+$triangle[1][1]+$triangle[2][1]) * $area;
1179 6 $total_area += $area;
1180 }
1181 1 $c[0] = $c[0]/($total_area*3);
1182 1 $c[1] = $c[1]/($total_area*3);
1183 1 return \@c;
1184 }
1185 ################################################################################
1186 #
1187 # The winding number method has been cused here. Seems to
1188 # be the most accurate one and, if well written, it matches
1189 # the performance of the crossing number method.
1190 # The winding number method counts the number of times a polygon
1191 # winds around the point. If the result is 0, the points is outside
1192 # the polygon.
1193 #
1194 # args: reference to polygon object
1195 # reference to a point
1196 #
1197 10 ▶sub IsInsidePolygon {
Subroutine IsInsidePolygon: called CC=18 SCAR=35.0
Pod undocumented
1198 10 my ($pointsref,$pointref) = @_;
1199 10 my @points = @$pointsref;
1200 10 ▶ if (@points < 3) { # polygon should at least have 3 points ...
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 10 10
1201 0 carp("Can't run inpolygon: polygon should have at least 3 points");
1202 0 return;
1203 }
1204 10 ▶ if (! $pointref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointref) 0 10 10
1205 0 carp("Can't run inpolygon: no point entered");
1206 0 return;
1207 }
1208 10 my @point = @$pointref;
1209 10 my $wn; # thw winding number counter
1210 10 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 52 10 62
1211 52 my $cp = CrossProduct([$points[$i-1],$points[$i],$pointref]);
1212 # if colinear and in between the 2 points of the polygon
1213 # segment, it's on the perimeter and considered inside
1214 52 ▶ if ($cp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp == 0) 3 49 52
1215 3 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$i - 1][0] <= $point[0] && $point[0] <= $points[$i][0] || $points[$i - 1][0] >= $point[0] && $point[0] >= $points[$i][0] and $points[$i - 1][1] <= $pointref->[1] && $point[1] <= $points[$i][1] || $points[$i - 1][1] >= $point[1] && $point[1] >= $points[$i][1]) 0 3 3
Condition28%6/21
$points[$i - 1][0] <= $point[0] && $point[0] <= $points[$i][0] || $points[$i - 1][0] >= $point[0] && $point[0] >= $points[$i][0] and $points[$i - 1][1] <= $pointref->[1] && $point[1] <= $points[$i][1] || $points[$i - 1][1] >= $point[1] && $point[1] >= $points[$i][1]
!ll&&!rl&&r
300
$points[$i - 1][0] <= $point[0] and $point[0] <= $points[$i][0] or $points[$i - 1][0] >= $point[0] and $point[0] >= $points[$i][0]
l!l&&r!l&&!r
003
$points[$i - 1][0] <= $point[0] and $point[0] <= $points[$i][0]
!ll&&!rl&&r
120
$points[$i - 1][0] >= $point[0] and $point[0] >= $points[$i][0]
!ll&&!rl&&r
210
$points[$i - 1][1] <= $pointref->[1] and $point[1] <= $points[$i][1] or $points[$i - 1][1] >= $point[1] and $point[1] >= $points[$i][1]
l!l&&r!l&&!r
000
$points[$i - 1][1] <= $pointref->[1] and $point[1] <= $points[$i][1]
!ll&&!rl&&r
000
$points[$i - 1][1] >= $point[1] and $point[1] >= $points[$i][1]
!ll&&!rl&&r
000
MC/DC0%0/8
$points[$i - 1][0] <= $point[0] && $point[0] <= $points[$i][0] || $points[$i - 1][0] >= $point[0] && $point[0] >= $points[$i][0] and $points[$i - 1][1] <= $pointref->[1] && $point[1] <= $points[$i][1] || $points[$i - 1][1] >= $point[1] && $point[1] >= $points[$i][1]
$points[$i - 1][0] <= $point[0] $point[0] <= $points[$i][0] $points[$i - 1][0] >= $point[0] $point[0] >= $points[$i][0] $points[$i - 1][1] <= $pointref->[1] $point[1] <= $points[$i][1] $points[$i - 1][1] >= $point[1] $point[1] >= $points[$i][1]
Truth table8%2/25
A and B or C and D and E and F or G and H
A= $points[$i - 1][0] <= $point[0]B= $point[0] <= $points[$i][0]C= $points[$i - 1][0] >= $point[0]D= $point[0] >= $points[$i][0]E= $points[$i - 1][1] <= $pointref->[1]F= $point[1] <= $points[$i][1]G= $points[$i - 1][1] >= $point[1]H= $point[1] >= $points[$i][1]
ABCDEFGHresult
0X0XXXXX0
0X10XXXX0
100XXXXX0
1010XXXX0
11XX0X0X0
11XX0X100
11XX100X0
11XX10100
0X110X0X0
0X110X100
0X11100X0
0X1110100
10110X0X0
10110X100
1011100X0
101110100
11XX11XX1
11XX0X111
11XX10111
0X1111XX1
0X110X111
0X1110111
101111XX1
10110X111
101110111
1216 ((($points[$i-1][0] <= $point[0] &&
1217 $point[0] <= $points[$i][0])) ||
1218 (($points[$i-1][0] >= $point[0] &&
1219 $point[0] >= $points[$i][0])))
1220 &&
1221 ((($points[$i-1][1] <= $$pointref[1] &&
1222 $point[1] <= $points[$i][1])) ||
1223 (($points[$i-1][1] >= $point[1] &&
1224 $point[1] >= $points[$i][1])))
1225 ) {
1226 0 return 1;
1227 }
1228 }
1229 52 ▶ if ($points[$i-1][1] <= $point[1]) { # start y <= P.y
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$i - 1][1] <= $point[1]) { } 29 23 52
1230 29 ▶ if ($points[$i][1] > $point[1]) { # // an upward crossing
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$i][1] > $point[1]) 8 21 29
1231 8 ▶ if ($cp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp > 0) 6 2 8
1232 # point left of edge
1233 6 $wn++; # have a valid up intersect
1234 }
1235 }
1236 } else { # start y > P.y (no test needed)
1237 23 ▶ if ($points[$i][1] <= $point[1]) { # a downward crossing
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$i][1] <= $point[1]) 8 15 23
1238 8 ▶ if ($cp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp < 0) 2 6 8
1239 # point right of edge
1240 2 $wn--; # have a valid down intersect
1241 }
1242 }
1243 }
1244 }
1245 10 return $wn;
1246 }
1247 ################################################################################
1248 #
1249 # Check if polygon inside polygon or contour
1250 # (for a contour, a point is inside when it's within the outer shape and
1251 # not within one of the inner shapes (holes) )
1252 8 ▶sub isinside {
Subroutine isinside: called CC=5 SCAR=16.1
Pod documented
1253 8 my ($self,$pointref) = @_;
1254 8 my $pointsref = $self->points;
1255 8 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 2 6 8
1256 2 return IsInsidePolygon($pointsref,$pointref);
1257 } else {
1258 6 my @polygonrefs = $self->get_polygons;
1259 6 ▶ return 0 if (! IsInsidePolygon($polygonrefs[0],$pointref));
Branch: 1 branch
BranchTrueFalseTotal
unless IsInsidePolygon($polygonrefs[0], $pointref) 2 4 6
1260 4 my @result;
1261 4 ▶ for (my $i = 1; $i <@polygonrefs; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @polygonrefs) 2 3 5
1262 2 ▶ return 0 if (IsInsidePolygon($polygonrefs[$i],$pointref));
Branch: 1 branch
BranchTrueFalseTotal
if IsInsidePolygon($polygonrefs[$i], $pointref) 1 1 2
1263 }
1264 3 return 1;
1265 }
1266 }
1267 ################################################################################
1268 #
1269 # a counter clockwise rotation over an angle a is given by the formula
1270 #
1271 # / x2 \ / cos(a) -sin(a) \ / x1 \
1272 # | | = | | | |
1273 # \ y2 / \ sin(a) cos(a) / \ y1 /
1274 #
1275 # args: reference to polygon object
1276 # angle (in radians)
1277 # reference to center point (use origin if no center point entered)
1278 #
1279 1 ▶sub RotatePolygon {
Subroutine RotatePolygon: called CC=4 SCAR=13.9
Pod undocumented
1280 1 my ($pointsref,$angle,$center) = @_;
1281 1 my $xc = 0;
1282 1 my $yc = 0;
1283 1 ▶ if ($center) {
Branch: 1 branch
BranchTrueFalseTotal
if ($center) 1 0 1
1284 1 my @point = @$center;
1285 1 $xc = $point[0];
1286 1 $yc = $point[1];
1287 }
1288 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) 1 0 1
1289 1 my @points = @$pointsref;
1290 1 my @result;
1291 1 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1292 4 my $x = $xc + cos($angle)*($points[$i][0] - $xc) - sin($angle)*($points[$i][1] - $yc);
1293 4 my $y = $yc + sin($angle)*($points[$i][0] - $xc) + cos($angle)*($points[$i][1] - $yc);
1294 4 $result[$i][0] = $x;
1295 4 $result[$i][1] = $y;
1296 }
1297 1 return [@result];
1298 }
1299 }
1300 ################################################################################
1301 #
1302 # rotate jpolygon or contour
1303 #
1304 1 ▶sub rotate {
Subroutine rotate: called CC=3 SCAR=13.0
Pod documented
1305 1 my ($self,$angle,$center) = @_;
1306 1 my $rotate = Math::Geometry::Planar->new;
1307 1 my $pointsref = $self->points;
1308 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 0 1
1309 1 $rotate->points(RotatePolygon($pointsref,$angle,$center));
1310 } else {
1311 0 my @polygonrefs = $self->get_polygons;
1312 0 my @result;
1313 0 foreach (@polygonrefs) {
1314 0 $rotate->add_polygons(RotatePolygon($_,$angle,$center));
1315 }
1316 }
1317 1 return $rotate;
1318 }
1319 ################################################################################
1320 #
1321 # move a polygon over a distance in x and y direction
1322 #
1323 # args: reference to polygon object
1324 # X offset
1325 # y offset
1326 #
1327 1 ▶sub MovePolygon {
Subroutine MovePolygon: called CC=3 SCAR=11.0
Pod undocumented
1328 1 my ($pointsref,$dx,$dy) = @_;
1329 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) 1 0 1
1330 1 my @points = @$pointsref;
1331 1 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1332 4 $points[$i][0] = $points[$i][0] + $dx;
1333 4 $points[$i][1] = $points[$i][1] + $dy;
1334 }
1335 1 return [@points];
1336 }
1337 }
1338 ################################################################################
1339 #
1340 # Move polygon or contour
1341 #
1342 1 ▶sub move {
Subroutine move: called CC=3 SCAR=13.0
Pod documented
1343 1 my ($self,$dx,$dy) = @_;
1344 1 my $move = Math::Geometry::Planar->new;
1345 1 my $pointsref = $self->points;
1346 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 0 1
1347 1 $move->points(MovePolygon($pointsref,$dx,$dy));
1348 } else {
1349 0 my @polygonrefs = $self->get_polygons;
1350 0 my @result;
1351 0 foreach (@polygonrefs) {
1352 0 $move->add_polygons(MovePolygon($_,$dx,$dy));
1353 }
1354 }
1355 1 return $move;
1356 }
1357 ################################################################################
1358 #
1359 # mirror in x direction - vertical axis through point referenced by $center
1360 # if no center entered, use y axis
1361 #
1362 # args: reference to polygon object
1363 # reference to center
1364 #
1365 1 ▶sub MirrorXPolygon {
Subroutine MirrorXPolygon: called CC=4 SCAR=14.2
Pod undocumented
1366 1 my ($pointsref,$center) = @_;
1367 1 my @points = @$pointsref;
1368 1 ▶ if (@points == 0) { # nothing to mirror
Branch: 1 branch
BranchTrueFalseTotal
if (@points == 0) 0 1 1
1369 0 carp("Nothing to mirror ...");
1370 0 return;
1371 }
1372 1 my $xc = 0;
1373 1 my $yc = 0;
1374 1 ▶ if ($center) {
Branch: 1 branch
BranchTrueFalseTotal
if ($center) 1 0 1
1375 1 my @point = @$center;
1376 1 $xc = $point[0];
1377 1 $yc = $point[1];
1378 }
1379 1 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1380 4 $points[$i][0] = 2*$xc - $points[$i][0];
1381 }
1382 1 return [@points];
1383 }
1384 ################################################################################
1385 #
1386 # mirror polygon or contour in x direction
1387 # (vertical axis through point referenced by $center)
1388 1 ▶sub mirrorx {
Subroutine mirrorx: called CC=3 SCAR=13.0
Pod documented
1389 1 my ($self,$dx,$dy) = @_;
1390 1 my $mirrorx = Math::Geometry::Planar->new;
1391 1 my $pointsref = $self->points;
1392 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 0 1
1393 1 $mirrorx->points(MirrorXPolygon($pointsref,$dx,$dy));
1394 } else {
1395 0 my @polygonrefs = $self->get_polygons;
1396 0 my @result;
1397 0 foreach (@polygonrefs) {
1398 0 $mirrorx->add_polygons(MirrorXPolygon($_,$dx,$dy));
1399 }
1400 }
1401 1 return $mirrorx;
1402 }
1403 ################################################################################
1404 #
1405 # mirror in y direction - horizontal axis through point referenced by $center
1406 # if no center entered, use x axis
1407 #
1408 # args: reference to polygon object
1409 # reference to center
1410 #
1411 1 ▶sub MirrorYPolygon {
Subroutine MirrorYPolygon: called CC=4 SCAR=14.2
Pod undocumented
1412 1 my ($pointsref,$center) = @_;
1413 1 my @points = @$pointsref;
1414 1 ▶ if (@points == 0) { # nothing to mirror
Branch: 1 branch
BranchTrueFalseTotal
if (@points == 0) 0 1 1
1415 0 carp("Nothing to mirror ...");
1416 0 return;
1417 }
1418 1 my $xc = 0;
1419 1 my $yc = 0;
1420 1 ▶ if ($center) {
Branch: 1 branch
BranchTrueFalseTotal
if ($center) 1 0 1
1421 1 my @point = @$center;
1422 1 $xc = $point[0];
1423 1 $yc = $point[1];
1424 }
1425 1 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1426 4 $points[$i][1] = 2*$yc - $points[$i][1];
1427 }
1428 1 return [@points];
1429 }
1430 ################################################################################
1431 #
1432 # mirror polygon or contour in x direction
1433 # (vertical axis through point referenced by $center)
1434 1 ▶sub mirrory {
Subroutine mirrory: called CC=3 SCAR=13.0
Pod documented
1435 1 my ($self,$dx,$dy) = @_;
1436 1 my $mirrory = Math::Geometry::Planar->new;
1437 1 my $pointsref = $self->points;
1438 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 0 1
1439 1 $mirrory->points(MirrorYPolygon($pointsref,$dx,$dy));
1440 } else {
1441 0 my @polygonrefs = $self->get_polygons;
1442 0 my @result;
1443 0 foreach (@polygonrefs) {
1444 0 $mirrory->add_polygons(MirrorYPolygon($_,$dx,$dy));
1445 }
1446 }
1447 1 return $mirrory;
1448 }
1449 ################################################################################
1450 #
1451 # mirror around axis determined by 2 points (p1p2)
1452 #
1453 # args: reference to polygon object
1454 # reference to array with to points defining reflection axis
1455 #
1456 1 ▶sub MirrorPolygon {
Subroutine MirrorPolygon: called CC=4 SCAR=14.4
Pod undocumented
1457 1 my ($pointsref,$axisref) = @_;
1458 1 my @points = @$pointsref;
1459 1 my @axis = @$axisref;
1460 1 ▶ if (@axis != 2) { # need 2 points defining axis
Branch: 1 branch
BranchTrueFalseTotal
if (@axis != 2) 0 1 1
1461 0 carp("Can't mirror: 2 points need to define axis");
1462 0 return;
1463 }
1464 1 my $p1ref = $axis[0];
1465 1 my $p2ref = $axis[1];
1466 1 my @p1 = @$p1ref;
1467 1 my @p2 = @$p2ref;
1468 1 ▶ if (@points == 0) { # nothing to mirror
Branch: 1 branch
BranchTrueFalseTotal
if (@points == 0) 0 1 1
1469 0 carp("Nothing to mirror ...");
1470 0 return;
1471 }
1472 1 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1473 4 my $footref = PerpendicularFoot([\@p1,\@p2,$points[$i]]);
1474 4 my @foot = @$footref;
1475 4 $points[$i][0] = $foot[0] - ($points[$i][0] - $foot[0]);
1476 4 $points[$i][1] = $foot[1] - ($points[$i][1] - $foot[1]);
1477 }
1478 1 return [@points];
1479 }
1480 ################################################################################
1481 #
1482 # mirror polygon or contour around axis determined by 2 points (p1p2)
1483 #
1484 1 ▶sub mirror {
Subroutine mirror: called CC=3 SCAR=13.0
Pod documented
1485 1 my ($self,$axisref) = @_;
1486 1 my $mirror = Math::Geometry::Planar->new;
1487 1 my $pointsref = $self->points;
1488 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 0 1
1489 1 $mirror->points(MirrorPolygon($pointsref,$axisref));
1490 } else {
1491 0 my @polygonrefs = $self->get_polygons;
1492 0 my @result;
1493 0 foreach (@polygonrefs) {
1494 0 $mirror->add_polygons(MirrorPolygon($_,$axisref));
1495 }
1496 }
1497 1 return $mirror;
1498 }
1499 ################################################################################
1500 #
1501 # scale polygon from center
1502 # I would choose the centroid ...
1503 #
1504 # args: reference to polygon object
1505 # scale factor
1506 # reference to center point
1507 #
1508 1 ▶sub ScalePolygon {
Subroutine ScalePolygon: called CC=4 SCAR=14.1
Pod undocumented
1509 1 my ($pointsref,$scale,$center) = @_;
1510 1 my @points = @$pointsref;
1511 1 ▶ if (@points == 0) { # nothing to scale
Branch: 1 branch
BranchTrueFalseTotal
if (@points == 0) 0 1 1
1512 0 carp("Nothing to scale ...");
1513 0 return;
1514 }
1515 1 my $xc = 0;
1516 1 my $yc = 0;
1517 1 ▶ if ($center) {
Branch: 1 branch
BranchTrueFalseTotal
if ($center) 1 0 1
1518 1 my @point = @$center;
1519 1 $xc = $point[0];
1520 1 $yc = $point[1];
1521 }
1522 # subtract center, scale and add center again
1523 1 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1524 4 $points[$i][0] = $scale * ($points[$i][0] - $xc) + $xc;
1525 4 $points[$i][1] = $scale * ($points[$i][1] - $yc) + $yc;
1526 }
1527 1 return [@points];
1528 }
1529 ################################################################################
1530 #
1531 # scale polygon from center
1532 # I would choose the centroid ...
1533 #
1534 1 ▶sub scale {
Subroutine scale: called CC=3 SCAR=13.0
Pod documented
1535 1 my ($self,$factor,$center) = @_;
1536 1 my $scale = Math::Geometry::Planar->new;
1537 1 my $pointsref = $self->points;
1538 1 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 0 1
1539 1 $scale->points(ScalePolygon($pointsref,$factor,$center));
1540 } else {
1541 0 my @polygonrefs = $self->get_polygons;
1542 0 my @result;
1543 0 foreach (@polygonrefs) {
1544 0 $scale->add_polygons(ScalePolygon($_,$factor,$center));
1545 }
1546 }
1547 1 return $scale;
1548 }
1549 ################################################################################
1550 #
1551 # The "bounding box" of a set of points is the box with horizontal
1552 # and vertical edges that contains all points
1553 #
1554 # args: reference to array of points or a contour
1555 # returns reference to array of 4 points representing bounding box
1556 #
1557 1 ▶sub bbox {
Subroutine bbox: called CC=9 SCAR=22.6
Pod documented
1558 1 my ($self) = @_;
1559 1 my $bbox = Math::Geometry::Planar->new;
1560 1 my $pointsref = $self->points;
1561 1 ▶ if (! $pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointsref) 1 0 1
1562 1 $pointsref = ($self->get_polygons(0))[0];
1563 1 ▶ return if (! $pointsref); # empty object
Branch: 1 branch
BranchTrueFalseTotal
unless $pointsref 0 1 1
1564 }
1565 1 my @points = @$pointsref;
1566 1 ▶ if (@points < 3) { # polygon should at least have 3 points ...
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 1 1
1567 0 carp("Can't determine bbox: polygon should have at least 3 points");
1568 0 return;
1569 }
1570 1 my $min_x = $points[0][0];
1571 1 my $min_y = $points[0][1];
1572 1 my $max_x = $points[0][0];
1573 1 my $max_y = $points[0][1];
1574 1 ▶ for (my $i = 1 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 1 5
1575 4 ▶ $min_x = $points[$i][0] if ($points[$i][0] < $min_x);
Branch: 1 branch
BranchTrueFalseTotal
if $points[$i][0] < $min_x 0 4 4
1576 4 ▶ $min_y = $points[$i][1] if ($points[$i][1] < $min_y);
Branch: 1 branch
BranchTrueFalseTotal
if $points[$i][1] < $min_y 0 4 4
1577 4 ▶ $max_x = $points[$i][0] if ($points[$i][0] > $max_x);
Branch: 1 branch
BranchTrueFalseTotal
if $points[$i][0] > $max_x 1 3 4
1578 4 ▶ $max_y = $points[$i][1] if ($points[$i][1] > $max_y);
Branch: 1 branch
BranchTrueFalseTotal
if $points[$i][1] > $max_y 2 2 4
1579 }
1580 1 $bbox->points([[$min_x,$min_y],
1581 [$min_x,$max_y],
1582 [$max_x,$max_y],
1583 [$max_x,$min_y]]);
1584 1 return $bbox;
1585 }
1586 ################################################################################
1587 #
1588 # The "minimal enclosing rectangle" of a set of points is the box with minimal area
1589 # that contains all points.
1590 # We'll use the rotating calipers method here which works only on convex polygons
1591 # so before calling minbbox, create the convex hull first for the set of points
1592 # (taking into account whether or not the set of points represents a polygon).
1593 #
1594 # args: reference to array of points representing a convex polygon
1595 # returns reference to array of 4 points representing minimal bounding rectangle
1596 #
1597 2 ▶sub minrectangle {
Subroutine minrectangle: called CC=14 SCAR=26.4
Pod documented
1598 2 my ($self) = @_;
1599 2 my $minrectangle = Math::Geometry::Planar->new;
1600 2 my $pointsref = $self->points;
1601 2 ▶ if (! $pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointsref) 2 0 2
1602 2 $pointsref = ($self->get_polygons(0))[0];
1603 2 ▶ return if (! $pointsref); # empty object
Branch: 1 branch
BranchTrueFalseTotal
unless $pointsref 0 2 2
1604 }
1605 2 my @points = @$pointsref;
1606 2 ▶ if (@points < 3) { # polygon should at least have 3 points ...
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 2 2
1607 0 carp("Can't determine minrectangle: polygon should have at least 3 points");
1608 0 return;
1609 }
1610 2 my $d;
1611 # scan all segments and for each segment, calculate the area of the bounding
1612 # box that has one side coinciding with the segment
1613 2 my $min_area = 0;
1614 2 my @indices;
1615 2 ▶ for (my $i = 0 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 12 2 14
1616 # for each segment, find the point (vertex) at the largest perpendicular distance
1617 # the opposite side of the current rectangle runs through this point
1618 12 my $mj; # index of point at maximum distance
1619 12 my $maxj = 0; # maximum distance (squared)
1620 # Get coefficients of the implicit line equation ax + by +c = 0
1621 # Do NOT normalize since scaling by a constant
1622 # is irrelevant for just comparing distances.
1623 12 my $a = $points[$i-1][1] - $points[$i][1];
1624 12 my $b = $points[$i][0] - $points[$i-1][0];
1625 12 my $c = $points[$i-1][0] * $points[$i][1] - $points[$i][0] * $points[$i-1][1];
1626 # loop through point array testing for max distance to current segment
1627 12 ▶ for (my $j = -1 ; $j < @points-1 ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @points - 1) 74 12 86
1628 74 ▶ next if ($j == $i || $j == $i-1); # exclude points of current segment
Branch: 1 branch
BranchTrueFalseTotal
if $j == $i or $j == $i - 1 22 52 74
Condition100%3/3
$j == $i or $j == $i - 1
l!l&&r!l&&!r
101252
MC/DC100%2/2
$j == $i or $j == $i - 1
$j == $i $j == $i - 1
Truth table100%3/3
A or B
A= $j == $iB= $j == $i - 1
ABresult
1X1
011
000
1629 # just use dist squared (sqrt not needed for comparison)
1630 # since the polygon is convex, all points are at the same side
1631 # so we don't need to take the absolute value for dist
1632 52 my $dist = $a * $points[$j][0] + $b * $points[$j][1] + $c;
1633 52 ▶ if ($dist > $maxj) { # this point is further
Branch: 1 branch
BranchTrueFalseTotal
if ($dist > $maxj) 25 27 52
1634 25 $mj = $j; # so have a new maximum
1635 25 $maxj = $dist;
1636 }
1637 }
1638 # the line -bx+ay+c=0 is perpendicular to ax+by+c=0
1639 # now find index of extreme points corresponding to perpendicular line
1640 # initialize to first point (note that points of current segment could
1641 # be one or even both of the extreme points)
1642 12 my $mk = 0;
1643 12 my $ml = 0;
1644 12 my $mink = -$b * $points[0][0] + $a * $points[0][1] + $c;
1645 12 my $maxl = -$b * $points[0][0] + $a * $points[0][1] + $c;
1646 12 ▶ for (my $j = 1 ; $j < @points ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @points) 62 12 74
1647 # use signed dist to get extreme points
1648 62 my $dist = -$b * $points[$j][0] + $a * $points[$j][1] + $c;
1649 62 ▶ if ($dist < $mink) { # this point is further
Branch: 1 branch
BranchTrueFalseTotal
if ($dist < $mink) 15 47 62
1650 15 $mk = $j; # so have a new maximum
1651 15 $mink = $dist;
1652 }
1653 62 ▶ if ($dist > $maxl) { # this point is further
Branch: 1 branch
BranchTrueFalseTotal
if ($dist > $maxl) 15 47 62
1654 15 $ml = $j; # so have a new maximum
1655 15 $maxl = $dist;
1656 }
1657 }
1658 # now $maxj/sqrt(a**2+b**2) is the height of the current rectangle
1659 # and (|$mink| + |$maxl|)/sqrt(a**2+b**2) is the width
1660 # since area is width*height we can waste the costly sqrt function
1661 12 my $area = abs($maxj * ($mink-$maxl)) / ($a**2 +$b**2);
1662 12 ▶ if ($area < $min_area || ! $min_area) {
Branch: 1 branch
BranchTrueFalseTotal
if ($area < $min_area or not $min_area) 3 9 12
Condition100%3/3
$area < $min_area or not $min_area
l!l&&r!l&&!r
129
MC/DC100%2/2
$area < $min_area or not $min_area
$area < $min_area not $min_area
Truth table100%3/3
A or not(B)
A= $area < $min_areaB= not $min_area
ABresult
1X1
011
000
1663 3 $min_area = $area;
1664 3 @indices = ($i,$mj,$mk,$ml);
1665 }
1666 }
1667 2 my ($i,$j,$k,$l) = @indices;
1668 # Finally, get the corners of the minimum enclosing rectangle
1669 2 my $p1 = PerpendicularFoot([$points[$i-1],$points[$i],$points[$k]]);
1670 2 my $p2 = PerpendicularFoot([$points[$i-1],$points[$i],$points[$l]]);
1671 # now we calculate the second point on the line parallel to
1672 # the segment i going through the vertex j
1673 2 my $p = [$points[$j][0]+$points[$i-1][0]-$points[$i][0],
1674 $points[$j][1]+$points[$i-1][1]-$points[$i][1]];
1675 2 my $p3 = PerpendicularFoot([$points[$j],$p,$points[$l]]);
1676 2 my $p4 = PerpendicularFoot([$points[$j],$p,$points[$k]]);
1677 2 $minrectangle->points([$p1,$p2,$p3,$p4]);
1678 2 return $minrectangle;
1679 }
1680 ################################################################################
1681 #
1682 # triangulate polygon or contour
1683 #
1684 # args: polygon or contour object
1685 # returns a reference to an array triangles
1686 #
1687 2 ▶sub triangulate {
Subroutine triangulate: called CC=4 SCAR=13.9
Pod documented
1688 2 my ($self) = @_;
1689 2 my $pointsref = $self->points;
1690 2 my @triangles;
1691 2 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 1 1 2
1692 1 @triangles = @{TriangulatePolygon([$pointsref])};
1693 } else {
1694 1 my $polygonrefs = $self->polygons;
1695 1 ▶ if ($polygonrefs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($polygonrefs) 1 0 1
1696 1 @triangles = @{TriangulatePolygon($polygonrefs)};
1697 }
1698 }
1699 2 my @result;
1700 2 foreach (@triangles) {
1701 14 my $triangle = Math::Geometry::Planar->new;
1702 14 $triangle->points($_);
1703 14 push @result,$triangle;
1704 }
1705 2 return @result;
1706 }
1707 ################################################################################
1708 #
1709 # convexhull using the Melkman algorithm
1710 # (the set of input points represent a polygon and are thus ordered
1711 #
1712 # args: reference to ordered array of points representing a polygon
1713 # or contour (for a contour, we calculate the hull for the
1714 # outer shape)
1715 # returns a reference to an array of the convex hull vertices
1716 #
1717 1 ▶sub convexhull {
Subroutine convexhull: called CC=11 SCAR=24.9
Pod documented
1718 1 my ($self) = @_;
1719 1 my $pointsref = $self->points;
1720 1 ▶ if (! $pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointsref) 0 1 1
1721 0 $pointsref = ($self->get_polygons(0))[0];
1722 0 ▶ return if (! $pointsref); # empty object
Branch: 1 branch
BranchTrueFalseTotal
unless $pointsref 0 0 0
1723 }
1724 1 my @points = @$pointsref;
1725 1 ▶ return ([@points]) if (@points < 5); # need at least 5 points
Branch: 1 branch
BranchTrueFalseTotal
if @points < 5 0 1 1
1726 # initialize a deque D[] from bottom to top so that the
1727 # 1st tree vertices of V[] are a counterclockwise triangle
1728 1 my @result;
1729 1 my $bot = @points-2;
1730 1 my $top = $bot+3; # initial bottom and top deque indices
1731 1 $result[$bot] = $points[2]; # 3rd vertex is at both bot and top
1732 1 $result[$top] = $points[2]; # 3rd vertex is at both bot and top
1733 1 ▶ if (CrossProduct([$points[0], $points[1], $points[2]]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$points[0], $points[1], $points[2]]) > 0) { } 0 1 1
1734 0 $result[$bot+1] = $points[0];
1735 0 $result[$bot+2] = $points[1]; # ccw vertices are: 2,0,1,2
1736 } else {
1737 1 $result[$bot+1] = $points[1];
1738 1 $result[$bot+2] = $points[0]; # ccw vertices are: 2,1,0,2
1739 }
1740  
1741 # compute the hull on the deque D[]
1742 1 ▶ for (my $i=3; $i < @points; $i++) { # process the rest of vertices
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 4 0 4
1743 # test if next vertex is inside the deque hull
1744 4 ▶ if ((CrossProduct([$result[$bot], $result[$bot+1], $points[$i]]) > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$result[$bot], $result[$bot + 1], $points[$i]]) > 0 and CrossProduct([$result[$top - 1], $result[$top], $points[$i]]) > 0) 1 3 4
Condition66%2/3
CrossProduct([$result[$bot], $result[$bot + 1], $points[$i]]) > 0 and CrossProduct([$result[$top - 1], $result[$top], $points[$i]]) > 0
!ll&&!rl&&r
301
MC/DC50%1/2
CrossProduct([$result[$bot], $result[$bot + 1], $points[$i]]) > 0 and CrossProduct([$result[$top - 1], $result[$top], $points[$i]]) > 0
CrossProduct([$result[$bot], $result[$bot + 1], $points[$i]]) > 0 CrossProduct([$result[$top - 1], $result[$top], $points[$i]]) > 0
Truth table66%2/3
A and B
A= CrossProduct([$result[$bot], $result[$bot + 1], $points[$i]]) > 0B= CrossProduct([$result[$top - 1], $result[$top], $points[$i]]) > 0
ABresult
0X0
100
111
1745 (CrossProduct([$result[$top-1], $result[$top], $points[$i]]) > 0) ) {
1746 1 last; # skip an interior vertex
1747 }
1748  
1749 # incrementally add an exterior vertex to the deque hull
1750 # get the rightmost tangent at the deque bot
1751 3 ▶ while (CrossProduct([$result[$bot], $result[$bot+1], $points[$i]]) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$result[$bot], $result[$bot + 1], $points[$i]]) <= 0) 4 3 7
1752 4 ++$bot; # remove bot of deque
1753 }
1754 3 $result[--$bot] = $points[$i]; # insert $points[i] at bot of deque
1755  
1756 # get the leftmost tangent at the deque top
1757 3 ▶ while (CrossProduct([$result[$top-1], $result[$top], $points[$i]]) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$result[$top - 1], $result[$top], $points[$i]]) <= 0) 1 3 4
1758 1 --$top; # pop top of deque
1759 }
1760 3 $result[++$top] = $points[$i]; #/ push $points[i] onto top of deque
1761 }
1762  
1763 # transcribe deque D[] to the output hull array H[]
1764 1 my @returnval;
1765 1 ▶ for (my $h = 0; $h <= ($top-$bot-1); $h++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h <= $top - $bot - 1) 4 1 5
1766 4 $returnval[$h] = $result[$bot + $h];
1767 }
1768 1 my $hull = Math::Geometry::Planar->new;
1769 1 $hull->points([@returnval]);
1770 1 return $hull;
1771 }
1772 ################################################################################
1773 #
1774 # convexhull using Andrew's monotone chain 2D convex hull algorithm
1775 # returns a reference to an array of the convex hull vertices
1776 #
1777 # args: reference to array of points (doesn't really need to be a polygon)
1778 # (also works for a contour - however, since a contour should consist
1779 # of polygons - which are ordered sets of points - the algorithm
1780 # above will be faster)
1781 # returns a reference to an array of the convex hull vertices
1782 #
1783 1 ▶sub convexhull2 {
Subroutine convexhull2: called CC=22 SCAR=32.4
Pod documented
1784 1 my ($self) = @_;
1785 1 my $pointsref = $self->points;
1786 1 ▶ if (! $pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pointsref) 0 1 1
1787 0 $pointsref = ($self->get_polygons(0))[0];
1788 0 ▶ return if (! $pointsref); # empty object
Branch: 1 branch
BranchTrueFalseTotal
unless $pointsref 0 0 0
1789 }
1790 1 my @points = @$pointsref;
1791 1 ▶ return ([@points]) if (@points < 5); # need at least 5 points
Branch: 1 branch
BranchTrueFalseTotal
if @points < 5 0 1 1
1792 # first, sort the points by increasing x and y-coordinates
1793 1 @points = sort ByXY (@points);
1794 # Get the indices of points with min x-coord and min|max y-coord
1795 1 my @hull;
1796 1 my $bot = 0;
1797 1 my $top = -1;
1798 1 my $minmin = 0;
1799 1 my $minmax;
1800 1 my $xmin = $points[0][0];
1801 1 ▶ for (my $i = 1 ; $i < @points ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 2 0 2
1802 2 ▶ if ($points[$i][0] != $xmin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$i][0] != $xmin) 1 1 2
1803 1 $minmax = $i - 1;
1804 last
1805 1 }
1806 }
1807 1 ▶ if ($minmax == @points-1) { # degenerate case: all x-coords == xmin
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax == @points - 1) 0 1 1
1808 0 $hull[++$top] = $points[$minmin];
1809 0 ▶ if ($points[$minmax][1] != $points[$minmin][1]) { # a nontrivial segment
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$minmax][1] != $points[$minmin][1]) 0 0 0
1810 0 $hull[$==$top] = $points[$minmax];
1811 0 return [@points];
1812 }
1813 }
1814  
1815 # Get the indices of points with max x-coord and min|max y-coord
1816 1 my $maxmin = 0;
1817 1 my $maxmax = @points - 1;
1818 1 my $xmax = $points[@points-1][0];
1819 1 ▶ for (my $i = @points - 2 ; $i >= 0 ; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 1 0 1
1820 1 ▶ if ($points[$i][0] != $xmax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($points[$i][0] != $xmax) 1 0 1
1821 1 $maxmin = $i + 1;
1822 1 last;
1823 }
1824 }
1825  
1826 # Compute the lower hull on the stack @lower
1827 1 $hull[++$top] = $points[$minmin]; # push minmin point onto stack
1828 1 my $i = $minmax;
1829 1 ▶ while (++$i <= $maxmin) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i <= $maxmin) 8 1 9
1830 # the lower line joins points[minmin] with points[maxmin]
1831 8 ▶ if (CrossProduct([$points[$minmin],$points[$maxmin],$points[$i]]) >= 0 && $i < $maxmin) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$points[$minmin], $points[$maxmin], $points[$i]]) >= 0 and $i < $maxmin) 3 5 8
Condition100%3/3
CrossProduct([$points[$minmin], $points[$maxmin], $points[$i]]) >= 0 and $i < $maxmin
!ll&&!rl&&r
413
MC/DC100%2/2
CrossProduct([$points[$minmin], $points[$maxmin], $points[$i]]) >= 0 and $i < $maxmin
CrossProduct([$points[$minmin], $points[$maxmin], $points[$i]]) >= 0 $i < $maxmin
Truth table100%3/3
A and B
A= CrossProduct([$points[$minmin], $points[$maxmin], $points[$i]]) >= 0B= $i < $maxmin
ABresult
0X0
100
111
1832 3 next; # ignore points[i] above or on the lower line
1833 }
1834 5 ▶ while ($top > 0) { # there are at least 2 points on the stack
Branch: 1 branch
BranchTrueFalseTotal
if ($top > 0) 6 1 7
1835 # test if points[i] is left of the line at the stack top
1836 6 ▶ if (CrossProduct([$hull[$top-1], $hull[$top], $points[$i]]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$hull[$top - 1], $hull[$top], $points[$i]]) > 0) { } 4 2 6
1837 4 last; # points[i] is a new hull vertex
1838 } else {
1839 2 $top--;
1840 }
1841 }
1842 5 $hull[++$top] = $points[$i]; # push points[i] onto stack
1843 }
1844  
1845 # Next, compute the upper hull on the stack above the bottom hull
1846 1 ▶ if ($maxmax != $maxmin) { # if distinct xmax points
Branch: 1 branch
BranchTrueFalseTotal
if ($maxmax != $maxmin) 0 1 1
1847 0 $hull[++$top] = $points[$maxmax]; # push maxmax point onto stack
1848 }
1849 1 $bot = $top;
1850 1 $i = $maxmin;
1851 1 ▶ while (--$i >= $minmax) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i >= $minmax) 8 1 9
1852 # the upper line joins points[maxmax] with points[minmax]
1853 8 ▶ if (CrossProduct([$points[$maxmax],$points[$minmax],$points[$i]]) >= 0 && $i > $minmax) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$points[$maxmax], $points[$minmax], $points[$i]]) >= 0 and $i > $minmax) 4 4 8
Condition100%3/3
CrossProduct([$points[$maxmax], $points[$minmax], $points[$i]]) >= 0 and $i > $minmax
!ll&&!rl&&r
314
MC/DC100%2/2
CrossProduct([$points[$maxmax], $points[$minmax], $points[$i]]) >= 0 and $i > $minmax
CrossProduct([$points[$maxmax], $points[$minmax], $points[$i]]) >= 0 $i > $minmax
Truth table100%3/3
A and B
A= CrossProduct([$points[$maxmax], $points[$minmax], $points[$i]]) >= 0B= $i > $minmax
ABresult
0X0
100
111
1854 4 next; # ignore points[i] below or on the upper line
1855 }
1856 4 ▶ while ($top > $bot) { # at least 2 points on the upper stack
Branch: 1 branch
BranchTrueFalseTotal
if ($top > $bot) 4 2 6
1857 # test if points[i] is left of the line at the stack top
1858 4 ▶ if (CrossProduct([$hull[$top-1],$hull[$top],$points[$i]]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (CrossProduct([$hull[$top - 1], $hull[$top], $points[$i]]) > 0) { } 2 2 4
1859 2 last; # points[i] is a new hull vertex
1860 } else {
1861 2 $top--;
1862 }
1863 }
1864 4 $hull[++$top] = $points[$i]; # push points[i] onto stack
1865 }
1866 1 $#hull = $top;
1867 1 ▶ if ($minmax == $minmin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minmax == $minmin) 0 1 1
1868 0 shift @hull; # remove joining endpoint from stack
1869 }
1870 1 my $hull = Math::Geometry::Planar->new;
1871 1 $hull->points([@hull]);
1872 1 return $hull;
1873 }
1874 ################################################################################
1875 #
1876 # Offset polygons
1877 #
1878 0 ▶sub offset_polygon {
Subroutine offset_polygon: not called CC=2 SCAR=17.9
Pod documented
1879 0 my ($self,$offset,$canvas) = @_;
1880 0 my $offset_polygons;
1881 0 my $pointsref = $self->points;
1882 0 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 0 0 0
1883 0 return [OffsetPolygon($pointsref,$offset,$canvas)];
1884 } else {
1885 0 carp("Can't offset contours - only polygons");
1886 0 return;
1887 }
1888 }
1889 ################################################################################
1890 #
1891 # Sorting function to surt points first by X coordinate, then by Y coordinate
1892 #
1893 22 ▶sub ByXY {
Subroutine ByXY: called CC=2 SCAR=6.9
Pod undocumented
1894 22 my @p1 = @$a;
1895 22 my @p2 = @$b;
1896 22 my $result = $p1[0] <=> $p2[0];
1897 22 ▶ if ($result){
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 19 3 22
1898 19 return $result;
1899 } else {
1900 3 return $p1[1] <=> $p2[1];
1901 }
1902 }
1903 ################################################################################
1904 #
1905 # convert polygon/contour to gpc contour
1906 #
1907 5 ▶sub convert2gpc {
Subroutine convert2gpc: called CC=7 SCAR=19.5
Pod documented
1908 5 my ($self,$dx,$dy) = @_;
1909 5 my @polygons;
1910 5 my $pointsref = $self->points;
1911 5 ▶ if ($pointsref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointsref) { } 3 2 5
1912 3 push @polygons,$pointsref;
1913 } else {
1914 2 @polygons = $self->get_polygons;
1915 }
1916 5 foreach (@polygons) {
1917 8 my @points = @{$_};
1918 8 ▶ if (@points < 3) { # need at least 3 points
Branch: 1 branch
BranchTrueFalseTotal
if (@points < 3) 0 8 8
1919 0 carp("Can't convert to gpc structure: polygon should have at least 3 points");
1920 0 return;
1921 }
1922 }
1923 5 my $contour = Math::Geometry::Planar::GPC::new_gpc_polygon();
1924 5 Math::Geometry::Planar::GPC::gpc_polygon_num_contours_set($contour,scalar(@polygons));
1925 # array for hole pointers
1926 5 my $hole_array = Math::Geometry::Planar::GPC::int_array(scalar(@polygons));
1927 5 Math::Geometry::Planar::GPC::gpc_polygon_hole_set($contour,$hole_array);
1928 5 my $vlist = Math::Geometry::Planar::GPC::gpc_vertex_list_array(scalar(@polygons));
1929 5 ▶ for (my $i = 0; $i < @polygons; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @polygons) 8 5 13
1930 8 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 5 3 8
1931 5 Math::Geometry::Planar::GPC::int_set($hole_array,$i,0);
1932 } else {
1933 3 Math::Geometry::Planar::GPC::int_set($hole_array,$i,1);
1934 }
1935 8 my @points = @{$polygons[$i]};
1936 8 my @gpc_vertexlist;
1937 8 foreach my $vertex (@points) {
1938 32 my $v = Math::Geometry::Planar::GPC::new_gpc_vertex();
1939 32 Math::Geometry::Planar::GPC::gpc_vertex_x_set($v,$$vertex[0]);
1940 32 Math::Geometry::Planar::GPC::gpc_vertex_y_set($v,$$vertex[1]);
1941 32 push @gpc_vertexlist,$v;
1942 }
1943 8 my $va = create_gpc_vertex_array(@gpc_vertexlist);
1944 8 my $vl = Math::Geometry::Planar::GPC::new_gpc_vertex_list();
1945 8 Math::Geometry::Planar::GPC::gpc_vertex_list_vertex_set($vl,$va);
1946 8 Math::Geometry::Planar::GPC::gpc_vertex_list_num_vertices_set($vl,scalar(@points));
1947 8 Math::Geometry::Planar::GPC::gpc_vertex_list_set($vlist,$i,$vl);
1948 }
1949 5 Math::Geometry::Planar::GPC::gpc_polygon_contour_set($contour,$vlist);
1950 5 return $contour;
1951 }
1952 ################################################################################
1953 #
1954 # convert gpc object to a set of contours
1955 # A gpc contour object can consist of multiple outer shapes each having holes,
1956 #
1957 6 ▶sub Gpc2Polygons {
Subroutine Gpc2Polygons: called CC=8 SCAR=20.8
Pod documented
1958 6 my ($gpc) = @_;
1959 6 my @result; # array with contours
1960 my @inner; # array holding the inner polygons
1961 6 my @outer; # array holding the outer polygons
1962 6 my $num_contours = Math::Geometry::Planar::GPC::gpc_polygon_num_contours_get($gpc);
1963 6 my $contour = Math::Geometry::Planar::GPC::gpc_polygon_contour_get($gpc);
1964 6 my $hole_array = Math::Geometry::Planar::GPC::gpc_polygon_hole_get($gpc);
1965 # for each shape of the gpc object
1966 6 ▶ for (my $i = 0 ; $i < $num_contours ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_contours) 10 6 16
1967 10 my @polygon;
1968 # get the hole flag
1969 10 my $hole = Math::Geometry::Planar::GPC::int_get($hole_array,$i);
1970 # get the vertices
1971 10 my $vl = Math::Geometry::Planar::GPC::gpc_vertex_list_get($contour,$i);
1972 10 my $num_vertices = Math::Geometry::Planar::GPC::gpc_vertex_list_num_vertices_get($vl);
1973 10 my $va = Math::Geometry::Planar::GPC::gpc_vertex_list_vertex_get($vl);
1974 10 ▶ for (my $j = 0 ; $j < $num_vertices ; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $num_vertices) 60 10 70
1975 60 my $v = Math::Geometry::Planar::GPC::gpc_vertex_get($va,$j);
1976 60 my $x = Math::Geometry::Planar::GPC::gpc_vertex_x_get($v);
1977 60 my $y = Math::Geometry::Planar::GPC::gpc_vertex_y_get($v);
1978 60 push @polygon,[$x,$y];
1979 }
1980 # create lists of inner and outer shapes
1981 10 ▶ if ($hole) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hole) { } 2 8 10
1982 2 push @inner,[@polygon];
1983 } else {
1984 8 push @outer,[@polygon];
1985 }
1986 }
1987 # shortcut: if there is only one outer shape, we're done
1988 6 ▶ if (@outer == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@outer == 1) { } 4 2 6
1989 4 my $obj = Math::Geometry::Planar->new;
1990 4 $obj->add_polygons(@outer,@inner);
1991 4 push @result,$obj;
1992 } else {
1993 2 foreach (@outer) {
1994 # create contour for each outer shape
1995 4 my $obj = Math::Geometry::Planar->new;
1996 4 $obj->polygons([$_]);
1997 4 push @result,$obj;
1998 # if an inner shape has at least one point inside this
1999 # outer shape, it belongs to this outer shape (so all
2000 # points are inside it)
2001 4 my $i = 0;
2002 4 ▶ while ($i < @inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @inner) 3 4 7
2003 3 my @polygon = @{$inner[$i]};
2004 3 ▶ if ($obj->isinside($polygon[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isinside($polygon[0])) { } 2 1 3
2005 2 $obj->add_polygons($inner[$i]);
2006 2 splice @inner,$i,1;
2007 } else {
2008 1 $i++;
2009 }
2010 }
2011 }
2012 }
2013 6 return @result;
2014 }
2015 ################################################################################
2016 #
2017 # gpc polygon clipping operatins
2018 #
2019 6 ▶sub GpcClip {
Subroutine GpcClip: called CC=5 SCAR=16.1
Pod documented
2020 6 my ($op,$gpc_poly_1,$gpc_poly_2) = @_;
2021 6 my $result = Math::Geometry::Planar::GPC::new_gpc_polygon();
2022 SWITCH: {
2023 6 ▶ ($op eq "DIFFERENCE") && do {
Branch: 1 branch
BranchTrueFalseTotal
$op eq "DIFFERENCE" and do { Math::Geometry::Planar::GPC::gpc_polygon_clip(0, $gpc_poly_1, $gpc_poly_2, $result); return $result } 2 4 6
2024 2 Math::Geometry::Planar::GPC::gpc_polygon_clip(0,$gpc_poly_1,$gpc_poly_2,$result);
2025 2 return $result;
2026 };
2027 4 ▶ ($op eq "INTERSECTION") && do {
Branch: 1 branch
BranchTrueFalseTotal
$op eq "INTERSECTION" and do { Math::Geometry::Planar::GPC::gpc_polygon_clip(1, $gpc_poly_1, $gpc_poly_2, $result); return $result } 1 3 4
2028 1 Math::Geometry::Planar::GPC::gpc_polygon_clip(1,$gpc_poly_1,$gpc_poly_2,$result);
2029 1 return $result;
2030 };
2031 3 ▶ ($op eq "XOR") && do {
Branch: 1 branch
BranchTrueFalseTotal
$op eq "XOR" and do { Math::Geometry::Planar::GPC::gpc_polygon_clip(2, $gpc_poly_1, $gpc_poly_2, $result); return $result } 1 2 3
2032 1 Math::Geometry::Planar::GPC::gpc_polygon_clip(2,$gpc_poly_1,$gpc_poly_2,$result);
2033 1 return $result;
2034 };
2035 2 ▶ ($op eq "UNION") && do {
Branch: 1 branch
BranchTrueFalseTotal
$op eq "UNION" and do { Math::Geometry::Planar::GPC::gpc_polygon_clip(3, $gpc_poly_1, $gpc_poly_2, $result); return $result } 2 0 2
2036 2 Math::Geometry::Planar::GPC::gpc_polygon_clip(3,$gpc_poly_1,$gpc_poly_2,$result);
2037 2 return $result;
2038 };
2039 0 return;
2040 }
2041 }
2042 ###############################################################################
2043 #
2044 # create gpc vertex array pointer
2045 #
2046 8 ▶sub create_gpc_vertex_array {
Subroutine create_gpc_vertex_array: called CC=2 SCAR=6.9
Pod undocumented
2047 8 my $len = scalar(@_);
2048 8 my $va = Math::Geometry::Planar::GPC::gpc_vertex_array($len);
2049 8 ▶ for (my $i=0; $i<$len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 32 8 40
2050 32 my $val = shift;
2051 32 Math::Geometry::Planar::GPC::gpc_vertex_set($va,$i,$val);
2052 }
2053 8 return $va;
2054 }
2055 ################################################################################
2056 #
2057 1 my $pi = atan2(1,1) * 4;
2058 #
2059 ################################################################################
2060 #
2061 # convert a circle to a polygon
2062 # arguments: first argument is the number of segments,
2063 # the other arguments are:
2064 # p1,p2,p3 : 3 points on the circle
2065 # or
2066 # center,p1 : center and a point on the circle
2067 # or
2068 # center,radius : the center and the radius of the circle
2069 #
2070 3 ▶sub CircleToPoly {
Subroutine CircleToPoly: called CC=5 SCAR=16.1
Pod documented
2071 3 my @args = @_;
2072 3 my @result;
2073 3 my ($segments,$p1,$p2,$p3,$center,$radius);
2074 3 ▶ if (@args == 4) { # 3 points
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 4) { } 1 2 3
elsif (@args == 3) { } 2 0 2
2075 1 ($segments,$p1,$p2,$p3) = @args;
2076 1 $center = CalcCenter($p1,$p2,$p3);
2077 1 $radius = SegmentLength([$p1,$center]);
2078 } elsif (@args == 3) {
2079 2 ▶ if (ref $args[2]) { # center + 1 point
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[2]) { } 1 1 2
2080 1 ($segments,$center,$p1) = @args;
2081 1 $radius = SegmentLength([$p1,$center]);
2082 } else { # center + radius
2083 1 ($segments,$center,$radius) = @args;
2084 }
2085 } else {
2086 0 return;
2087 }
2088 3 my $angle = ($pi * 2) / $segments;
2089 3 ▶ for (my $i = 0 ; $i < $segments ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $segments) 24 3 27
2090 24 push @result, [${$center}[0] + $radius * cos($angle * $i),
2091 24 ${$center}[1] + $radius * sin($angle * $i)]
2092 }
2093 3 my $poly = Math::Geometry::Planar->new;
2094 3 $poly->points([@result]);
2095 3 return $poly;
2096 }
2097 ################################################################################
2098 #
2099 # convert an arc to a polygon
2100 # arguments: first argument is the number of segments,
2101 # the other arguments are:
2102 # p1,p2,p3 : startpoint, intermediate point, endpoint
2103 # or
2104 # $center,p1,p2,$dir : center, startpoint, endpoint, direction
2105 # direction 0 counter clockwise
2106 # 1 clockwise
2107 # Note: the return value is a set of points, NOT a closed polygon !!!
2108 #
2109 3 ▶sub ArcToPoly {
Subroutine ArcToPoly: called CC=13 SCAR=27.6
Pod documented
2110 3 my @args = @_;
2111 3 my @result;
2112 3 my ($segments,$p1,$p2,$p3,$center,$direction);
2113 3 my ($radius,$angle);
2114 3 my ($start_angle, $end_angle);
2115 3 ▶ if (@args == 4) { # 3 points
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 4) { } 1 2 3
elsif (@args == 5) { } 2 0 2
2116 1 ($segments,$p1,$p2,$p3) = @args;
2117 1 $center = CalcCenter($p1,$p2,$p3);
2118 1 $radius = SegmentLength([$p1,$center]);
2119 # calculate start and end angles
2120 1 $start_angle = CalcAngle($center,$p1);
2121 1 my $mid_angle = CalcAngle($center,$p2);
2122 1 $end_angle = CalcAngle($center,$p3);
2123 1 ▶ if ( (($mid_angle < $start_angle) && ($start_angle < $end_angle)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($mid_angle < $start_angle and $start_angle < $end_angle or $start_angle < $end_angle and $end_angle < $mid_angle or $end_angle < $mid_angle and $mid_angle < $start_angle) 1 0 1
Condition20%3/15
$mid_angle < $start_angle and $start_angle < $end_angle or $start_angle < $end_angle and $end_angle < $mid_angle or $end_angle < $mid_angle and $mid_angle < $start_angle
l!l&&r!l&&!r
100
$mid_angle < $start_angle and $start_angle < $end_angle or $start_angle < $end_angle and $end_angle < $mid_angle
l!l&&r!l&&!r
100
$mid_angle < $start_angle and $start_angle < $end_angle
!ll&&!rl&&r
001
$start_angle < $end_angle and $end_angle < $mid_angle
!ll&&!rl&&r
000
$end_angle < $mid_angle and $mid_angle < $start_angle
!ll&&!rl&&r
000
MC/DC0%0/6
$mid_angle < $start_angle and $start_angle < $end_angle or $start_angle < $end_angle and $end_angle < $mid_angle or $end_angle < $mid_angle and $mid_angle < $start_angle
$mid_angle < $start_angle $start_angle < $end_angle $start_angle < $end_angle $end_angle < $mid_angle $end_angle < $mid_angle $mid_angle < $start_angle
Truth table6%1/15
A and B or C and D or E and F
A= $mid_angle < $start_angleB= $start_angle < $end_angleC= $start_angle < $end_angleD= $end_angle < $mid_angleE= $end_angle < $mid_angleF= $mid_angle < $start_angle
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
2124 (($start_angle < $end_angle) && ($end_angle < $mid_angle)) ||
2125 (($end_angle < $mid_angle) && ($mid_angle < $start_angle)) ) {
2126 1 $direction = 1;
2127 }
2128 1 $angle = $end_angle - $start_angle;
2129 } elsif (@args == 5) { # center, begin, end, direction
2130 2 ($segments,$center,$p1,$p3,$direction) = @args;
2131 2 $radius = SegmentLength([$p1,$center]);
2132 # calculate start and end angles
2133 2 $start_angle = CalcAngle($center,$p1);
2134 2 $end_angle = CalcAngle($center,$p3);
2135 2 $angle = $end_angle - $start_angle;
2136 } else {
2137 0 return;
2138 }
2139  
2140 3 ▶ if ($direction) { # clockwise
Branch: 1 branch
BranchTrueFalseTotal
if ($direction) { } 2 1 3
2141 2 ▶ if ($angle > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($angle > 0) 1 1 2
2142 1 $angle = $angle - ($pi * 2);
2143 }
2144 } else {
2145 1 ▶ if ($angle < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($angle < 0) 1 0 1
2146 1 $angle = $angle + ($pi * 2);
2147 }
2148 }
2149 3 $angle = $angle / $segments;
2150  
2151 3 push @result,$p1; # start point
2152 3 ▶ for (my $i = 1 ; $i < $segments ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $segments) 11 3 14
2153 11 push @result, [${$center}[0] + $radius * cos($start_angle + $angle * $i),
2154 11 ${$center}[1] + $radius * sin($start_angle + $angle * $i)]
2155 }
2156 3 push @result,$p3; # end point
2157 3 return [@result];
2158 }
2159 ################################################################################
2160 #
2161 # Calculate the center of a circle going through 3 points
2162 #
2163 2 ▶sub CalcCenter {
Subroutine CalcCenter: called CC=9 SCAR=23.5
Pod undocumented
2164 2 my ($p1_ref, $p2_ref, $p3_ref) = @_;
2165 2 my ($x1,$y1) = @{$p1_ref};
2166 2 my ($x2,$y2) = @{$p2_ref};
2167 2 my ($x3,$y3) = @{$p3_ref};
2168 # calculate midpoints of line segments p1p2 p2p3
2169 2 my $u1 = ($x1 + $x2)/2;
2170 2 my $v1 = ($y1 + $y2)/2;
2171 2 my $u2 = ($x2 + $x3)/2;
2172 2 my $v2 = ($y2 + $y3)/2;
2173 # linear equations y = a + bx
2174 2 my ($a1,$a2);
2175 2 my ($b1,$b2);
2176 # intersect (center) coordinates
2177 2 my ($xi,$yi);
2178 # slope of perpendicular = -1/slope
2179 2 ▶ if ($y1 != $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 != $y2) { } 2 0 2
2180 2 $b1 = - ($x1 - $x2)/($y1 - $y2);
2181 2 $a1 = $v1 - $b1 * $u1;
2182 } else {
2183 0 $xi = $u1;
2184 }
2185 2 ▶ if ($y2 != $y3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 != $y3) { } 2 0 2
2186 2 $b2 = - ($x2 - $x3)/($y2 - $y3);
2187 2 $a2 = $v2 - $b2 * $u2;
2188 } else {
2189 0 $xi = $u2;
2190 }
2191 # parallel lines (colinear is also parallel)
2192 2 ▶ return if ($b1 == $b2 || (!$b1 && !$b2));
Branch: 1 branch
BranchTrueFalseTotal
if $b1 == $b2 or not $b1 || $b2 0 2 2
Condition33%2/6
$b1 == $b2 or not $b1 || $b2
l!l&&r!l&&!r
002
$b1 || $b2
l!l&&r!l&&!r
200
MC/DC0%0/3
$b1 == $b2 or not $b1 || $b2
$b1 == $b2 $b1 $b2
Truth table25%1/4
A or not(B || C)
A= $b1 == $b2B= $b1C= $b2
ABCresult
1XX1
0001
01X0
0010
2193 2 ▶ $xi = - ($a1 - $a2)/($b1 - $b2) if (!$xi);
Branch: 1 branch
BranchTrueFalseTotal
unless $xi 2 0 2
2194 2 ▶ $yi = $a1 + $b1 * $xi if ($b1);
Branch: 1 branch
BranchTrueFalseTotal
if $b1 2 0 2
2195 2 ▶ $yi = $a2 + $b2 * $xi if ($b1);
Branch: 1 branch
BranchTrueFalseTotal
if $b1 2 0 2
2196 2 return [($xi,$yi)];
2197 }
2198 ################################################################################
2199 #
2200 # calculate angel of vector p1p2
2201 #
2202 7 ▶sub CalcAngle {
Subroutine CalcAngle: called CC=11 SCAR=26.5
Pod undocumented
2203 7 my ($p1_ref,$p2_ref) = @_;
2204 7 my ($x1,$y1) = @{$p1_ref};
2205 7 my ($x2,$y2) = @{$p2_ref};
2206 7 ▶ return 0 if ($y1 == $y2 && $x1 == $x2);
Branch: 1 branch
BranchTrueFalseTotal
if $y1 == $y2 and $x1 == $x2 0 7 7
Condition66%2/3
$y1 == $y2 and $x1 == $x2
!ll&&!rl&&r
430
MC/DC0%0/2
$y1 == $y2 and $x1 == $x2
$y1 == $y2 $x1 == $x2
Truth table66%2/3
A and B
A= $y1 == $y2B= $x1 == $x2
ABresult
0X0
100
111
2207 7 ▶ return 0 if ($y1 == $y2 && $x1 < $x2);
Branch: 1 branch
BranchTrueFalseTotal
if $y1 == $y2 and $x1 < $x2 0 7 7
Condition66%2/3
$y1 == $y2 and $x1 < $x2
!ll&&!rl&&r
430
MC/DC0%0/2
$y1 == $y2 and $x1 < $x2
$y1 == $y2 $x1 < $x2
Truth table66%2/3
A and B
A= $y1 == $y2B= $x1 < $x2
ABresult
0X0
100
111
2208 7 ▶ return $pi if ($y1 == $y2 && $x1 > $x2);
Branch: 1 branch
BranchTrueFalseTotal
if $y1 == $y2 and $x1 > $x2 3 4 7
Condition66%2/3
$y1 == $y2 and $x1 > $x2
!ll&&!rl&&r
403
MC/DC50%1/2
$y1 == $y2 and $x1 > $x2
$y1 == $y2 $x1 > $x2
Truth table66%2/3
A and B
A= $y1 == $y2B= $x1 > $x2
ABresult
0X0
100
111
2209 4 ▶ return $pi/2 if ($x1 == $x2 && $y1 < $y2);
Branch: 1 branch
BranchTrueFalseTotal
if $x1 == $x2 and $y1 < $y2 3 1 4
Condition66%2/3
$x1 == $x2 and $y1 < $y2
!ll&&!rl&&r
013
MC/DC50%1/2
$x1 == $x2 and $y1 < $y2
$x1 == $x2 $y1 < $y2
Truth table66%2/3
A and B
A= $x1 == $x2B= $y1 < $y2
ABresult
0X0
100
111
2210 1 ▶ return ($pi *3)/2 if ($x1 == $x2 && $y1 > $y2);
Branch: 1 branch
BranchTrueFalseTotal
if $x1 == $x2 and $y1 > $y2 1 0 1
Condition33%1/3
$x1 == $x2 and $y1 > $y2
!ll&&!rl&&r
001
MC/DC0%0/2
$x1 == $x2 and $y1 > $y2
$x1 == $x2 $y1 > $y2
Truth table33%1/3
A and B
A= $x1 == $x2B= $y1 > $y2
ABresult
0X0
100
111
2211 0 my $angle = atan2($y2-$y1,$x2-$x1);
2212 0 return $angle;
2213 }
2214 ################################################################################
2215 #
2216 # This program is an implementation of a fast polygon
2217 # triangulation algorithm based on the paper "A simple and fast
2218 # incremental randomized algorithm for computing trapezoidal
2219 # decompositions and for triangulating polygons" by Raimund Seidel.
2220 #
2221 # The algorithm handles simple polygons with holes. The input is
2222 # specified as contours. The outermost contour is anti-clockwise, while
2223 # all the inner contours must be clockwise. No point should be repeated
2224 # in the input. A sample input file 'data_1' is provided.
2225 #
2226 # The output is a reference to a list of triangles. Each triangle
2227 # is ar reference to an array fo three points, each point is a reference
2228 # to an array holdign the x and y coordinates of the point.
2229 # The number of output triangles produced for a polygon with n points is,
2230 # (n - 2) + 2*(#holes)
2231 #
2232 # The program is a translation to perl of the C program written by
2233 # Narkhede A. and Manocha D., Fast polygon triangulation algorithm based
2234 # on Seidel's Algorithm, UNC-CH, 1994.
2235 # Note that in this perl version, there are no statically allocated arrays
2236 # so the only limit is the amount of (virtual) memory available.
2237 #
2238 # See also:
2239 #
2240 # R. Seidel
2241 # "A simple and Fast Randomized Algorithm for Computing Trapezoidal
2242 # Decompositions and for Triangulating Polygons"
2243 # "Computational Geometry Theory & Applications"
2244 # Number = 1, Year 1991, Volume 1, Pages 51-64
2245 #
2246 # J. O'Rourke
2247 # "Computational Geometry in {C}"
2248 # Cambridge University Press - 1994
2249 #
2250 # Input specified as a contour with the restrictions mentioned above:
2251 # - first polygon is the outer shape and must be anti-clockwise.
2252 # - next polygons are inner shapels (holes) must be clockwise.
2253 # - Inner and outer shapes must be simple .
2254 #
2255 # Every contour is specified by giving all its points in order. No
2256 # point shoud be repeated. i.e. if the outer contour is a square,
2257 # only the four distinct endpoints shopudl be specified in order.
2258 #
2259 # Returns a reference to an array holding the triangles.
2260 #
2261  
2262 1 my $C_EPS = 1e-10; # tolerance value: Used for making
2263 # all decisions about collinearity or
2264 # left/right of segment. Decrease
2265 # this value if the input points are
2266 # spaced very close together
2267  
2268 1 my $INFINITY = 1<<29;
2269  
2270 1 my $TRUE = 1;
2271 1 my $FALSE = 0;
2272  
2273 1 my $T_X = 1;
2274 1 my $T_Y = 2;
2275 1 my $T_SINK = 3;
2276  
2277 1 my $ST_VALID = 1;
2278 1 my $ST_INVALID = 2;
2279  
2280 1 my $FIRSTPT = 1;
2281 1 my $LASTPT = 2;
2282  
2283 1 my $S_LEFT = 1;
2284 1 my $S_RIGHT = 2;
2285  
2286 1 my $SP_SIMPLE_LRUP = 1; # for splitting trapezoids
2287 1 my $SP_SIMPLE_LRDN = 2;
2288 1 my $SP_2UP_2DN = 3;
2289 1 my $SP_2UP_LEFT = 4;
2290 1 my $SP_2UP_RIGHT = 5;
2291 1 my $SP_2DN_LEFT = 6;
2292 1 my $SP_2DN_RIGHT = 7;
2293 1 my $SP_NOSPLIT = -1;
2294  
2295 1 my $TRI_LHS = 1;
2296 1 my $TRI_RHS = 2;
2297 1 my $TR_FROM_UP = 1; # for traverse-direction
2298 1 my $TR_FROM_DN = 2;
2299  
2300 1 my $choose_idx = 1;
2301 1 my @permute;
2302 my $q_idx;
2303 1 my $tr_idx;
2304 1 my @qs; # Query structure
2305 1 my @tr; # Trapezoid structure
2306 1 my @seg; # Segment table
2307  
2308 1 my @mchain; # Table to hold all the monotone
2309 # polygons . Each monotone polygon
2310 # is a circularly linked list
2311 1 my @vert; # chain init. information. This
2312 # is used to decide which
2313 # monotone polygon to split if
2314 # there are several other
2315 # polygons touching at the same
2316 # vertex
2317 1 my @mon; # contains position of any vertex in
2318 # the monotone chain for the polygon
2319 1 my @visited;
2320 1 my @op; # contains the resulting list of triangles
2321 # and their vertex number
2322 1 my ($chain_idx, $op_idx, $mon_idx);
2323  
2324 2 ▶sub TriangulatePolygon {
Subroutine TriangulatePolygon: called CC=6 SCAR=17.9
Pod undocumented
2325  
2326 2 $choose_idx = 1;
2327 2 @seg = ();
2328 2 @mchain = ();
2329 2 @vert = ();
2330 2 @mon = ();
2331 2 @visited = ();
2332 2 @op = ();
2333  
2334 2 my ($polygonrefs) = @_;
2335 2 my @polygons = @{$polygonrefs};
2336  
2337 2 my $ccount = 0;
2338 2 my $i = 1;
2339 2 ▶ while ($ccount < @polygons) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ccount < @polygons) 3 2 5
2340 3 my @vertexarray = @{$polygons[$ccount]};
2341 3 my $npoints = @vertexarray;
2342 3 my $first = $i;
2343 3 my $last = $first + $npoints - 1;
2344 3 ▶ for (my $j = 0; $j < $npoints; $j++, $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $npoints) 16 3 19
2345 16 my @vertex = @{$vertexarray[$j]};
2346 16 $seg[$i]{v0}{x} = $vertex[0];
2347 16 $seg[$i]{v0}{y} = $vertex[1];
2348 16 ▶ if ($i == $last) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == $last) { } 3 13 16
elsif ($i == $first) { } 3 10 13
2349 3 $seg[$i]{next} = $first;
2350 3 $seg[$i]{prev} = $i-1;
2351 3 my %tmp = %{$seg[$i]{v0}};
2352 3 $seg[$i-1]{v1} = \%tmp;
2353 } elsif ($i == $first) {
2354 3 $seg[$i]{next} = $i+1;
2355 3 $seg[$i]{prev} = $last;
2356 3 my %tmp = %{$seg[$i]{v0}};
2357 3 $seg[$last]{v1} = \%tmp;
2358 } else {
2359 10 $seg[$i]{prev} = $i-1;
2360 10 $seg[$i]{next} = $i+1;
2361 10 my %tmp = %{$seg[$i]{v0}};
2362 10 $seg[$i-1]{v1} = \%tmp;
2363 }
2364 16 $seg[$i]{is_inserted} = $FALSE;
2365 }
2366 3 $ccount++;
2367 }
2368  
2369 2 my $n = $i-1;
2370  
2371 2 _generate_random_ordering($n);
2372 2 _construct_trapezoids($n);
2373 2 my $nmonpoly = _monotonate_trapezoids($n);
2374 2 my $ntriangles = _triangulate_monotone_polygons($n, $nmonpoly);
2375 # now get the coordinates for all the triangles
2376 2 my @result;
2377 2 ▶ for (my $i = 0; $i < $ntriangles; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ntriangles) 14 2 16
2378 14 my @vertices = @{$op[$i]};
2379 my $triangle = [[$seg[$vertices[0]]{v0}{x},$seg[$vertices[0]]{v0}{y}],
2380 [$seg[$vertices[1]]{v0}{x},$seg[$vertices[1]]{v0}{y}],
2381 14 [$seg[$vertices[2]]{v0}{x},$seg[$vertices[2]]{v0}{y}]];
2382 14 push @result,$triangle;
2383 }
2384 2 return [@result];;
2385 }
2386  
2387 # Generate a random permutation of the segments 1..n
2388 2 ▶sub _generate_random_ordering {
Subroutine _generate_random_ordering: called CC=3 SCAR=11.0
2389 2 @permute = ();
2390 2 my ($n) = @_;
2391 2 my @input;
2392 2 ▶ for (my $i = 1 ; $i <= $n ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 16 2 18
2393 16 $input[$i] = $i;
2394 }
2395 2 my $i = 1;
2396 2 ▶ for (my $i = 1 ; $i <= $n ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 16 2 18
2397 16 my $m = int rand($#input) + 1;
2398 16 $permute[$i] = $input[$m];
2399 16 splice @input,$m,1;
2400 }
2401 }
2402  
2403 # Return the next segment in the generated random ordering of all the
2404 # segments in S
2405 16 ▶sub _choose_segment {
Subroutine _choose_segment: called CC=1 SCAR=0.0
2406 16 return $permute[$choose_idx++];
2407 }
2408  
2409 # Return a new node to be added into the query tree
2410 88 ▶sub _newnode {
Subroutine _newnode: called CC=1 SCAR=0.0
2411 88 return $q_idx++;
2412 }
2413  
2414 # Return a free trapezoid
2415 45 ▶sub _newtrap {
Subroutine _newtrap: called CC=1 SCAR=0.0
2416 45 $tr[$tr_idx]{lseg} = -1;
2417 45 $tr[$tr_idx]{rseg} = -1;
2418 45 $tr[$tr_idx]{state} = $ST_VALID;
2419 # next statements added to prevent 'uninitialized' warnings
2420 45 $tr[$tr_idx]{d0} = 0;
2421 45 $tr[$tr_idx]{d1} = 0;
2422 45 $tr[$tr_idx]{u0} = 0;
2423 45 $tr[$tr_idx]{u1} = 0;
2424 45 $tr[$tr_idx]{usave} = 0;
2425 45 $tr[$tr_idx]{uside} = 0;
2426 45 return $tr_idx++;
2427 }
2428  
2429 # Floating point number comparison
2430 339 ▶sub _fp_equal {
Subroutine _fp_equal: called CC=1 SCAR=0.0
2431 339 my ($X, $Y, $POINTS) = @_;
2432 339 my ($tX, $tY);
2433 339 $tX = sprintf("%.${POINTS}g", $X);
2434 339 $tY = sprintf("%.${POINTS}g", $Y);
2435 339 return $tX eq $tY;
2436 }
2437  
2438 # Return the maximum of the two points
2439 2 ▶sub _max {
Subroutine _max: called CC=4 SCAR=18.5
2440 2 my ($v0_ref, $v1_ref) = @_;
2441 2 my %v0 = %{$v0_ref};
2442 2 my %v1 = %{$v1_ref};
2443 2 ▶ if ($v0{y} > $v1{y} + $C_EPS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v0{'y'} > $v1{'y'} + $C_EPS) { } 2 0 2
elsif (_fp_equal($v0{'y'}, $v1{'y'}, $Math::Geometry::Planar::precision)) { } 0 0 0
2444 2 return \%v0;
2445 } elsif (_fp_equal($v0{y}, $v1{y}, $precision)) {
2446 0 ▶ if ($v0{x} > $v1{x} + $C_EPS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v0{'x'} > $v1{'x'} + $C_EPS) { } 0 0 0
2447 0 return \%v0;
2448 } else {
2449 0 return \%v1;
2450 }
2451 } else {
2452 0 return \%v1;
2453 }
2454 }
2455  
2456 # Return the minimum of the two points
2457 2 ▶sub _min {
Subroutine _min: called CC=4 SCAR=17.3
2458 2 my ($v0_ref, $v1_ref) = @_;
2459 2 my %v0 = %{$v0_ref};
2460 2 my %v1 = %{$v1_ref};
2461 2 ▶ if ($v0{y} < $v1{y} - $C_EPS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v0{'y'} < $v1{'y'} - $C_EPS) { } 0 2 2
elsif (_fp_equal($v0{'y'}, $v1{'y'}, $Math::Geometry::Planar::precision)) { } 0 2 2
2462 0 return \%v0;
2463 } elsif (_fp_equal($v0{y}, $v1{y}, $precision)) {
2464 0 ▶ if ($v0{x} < $v1{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v0{'x'} < $v1{'x'}) { } 0 0 0
2465 0 return \%v0;
2466 } else {
2467 0 return \%v1;
2468 }
2469 } else {
2470 2 return \%v1;
2471 }
2472 }
2473  
2474 172 ▶sub _greater_than {
Subroutine _greater_than: called CC=3 SCAR=11.0
2475 172 my ($v0_ref, $v1_ref) = @_;
2476 172 my %v0 = %{$v0_ref};
2477 172 my %v1 = %{$v1_ref};
2478 172 ▶ if ($v0{y} > $v1{y} + $C_EPS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v0{'y'} > $v1{'y'} + $C_EPS) { } 55 117 172
elsif ($v0{'y'} < $v1{'y'} - $C_EPS) { } 71 46 117
2479 55 return 1;
2480 } elsif ($v0{y} < $v1{y} - $C_EPS) {
2481 71 return 0;
2482 } else {
2483 46 return ($v0{x} > $v1{x});
2484 }
2485 }
2486  
2487 163 ▶sub _equal_to {
Subroutine _equal_to: called CC=2 SCAR=6.9
2488 163 my ($v0_ref, $v1_ref) = @_;
2489 163 my %v0 = %{$v0_ref};
2490 163 my %v1 = %{$v1_ref};
2491 return ( _fp_equal($v0{y}, $v1{y}, $precision) &&
2492 163 ▶ _fp_equal($v0{x}, $v1{x}, $precision) );
Condition100%3/3
_fp_equal($v0{'y'}, $v1{'y'}, $Math::Geometry::Planar::precision) && _fp_equal($v0{'x'}, $v1{'x'}, $Math::Geometry::Planar::precision)
!ll&&!rl&&r
892252
MC/DC100%2/2
_fp_equal($v0{'y'}, $v1{'y'}, $Math::Geometry::Planar::precision) && _fp_equal($v0{'x'}, $v1{'x'}, $Math::Geometry::Planar::precision)
_fp_equal($v0{'y'}, $v1{'y'}, $Math::Geometry::Planar::precision) _fp_equal($v0{'x'}, $v1{'x'}, $Math::Geometry::Planar::precision)
Truth table100%3/3
A && B
A= _fp_equal($v0{'y'}, $v1{'y'}, $Math::Geometry::Planar::precision)B= _fp_equal($v0{'x'}, $v1{'x'}, $Math::Geometry::Planar::precision)
ABresult
0X0
100
111
2493 }
2494  
2495 82 ▶sub _greater_than_equal_to {
Subroutine _greater_than_equal_to: called CC=3 SCAR=11.0
2496 82 my ($v0_ref, $v1_ref) = @_;
2497 82 my %v0 = %{$v0_ref};
2498 82 my %v1 = %{$v1_ref};
2499 82 ▶ if ($v0{y} > $v1{y} + $C_EPS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v0{'y'} > $v1{'y'} + $C_EPS) { } 24 58 82
elsif ($v0{'y'} < $v1{'y'} - $C_EPS) { } 6 52 58
2500 24 return 1;
2501 } elsif ($v0{y} < $v1{y} - $C_EPS) {
2502 6 return 0;
2503 } else {
2504 52 return ($v0{x} >= $v1{x});
2505 }
2506 }
2507  
2508 28 ▶sub _less_than {
Subroutine _less_than: called CC=3 SCAR=11.0
2509 28 my ($v0_ref, $v1_ref) = @_;
2510 28 my %v0 = %{$v0_ref};
2511 28 my %v1 = %{$v1_ref};
2512 28 ▶ if ($v0{y} < $v1{y} - $C_EPS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v0{'y'} < $v1{'y'} - $C_EPS) { } 4 24 28
elsif ($v0{'y'} > $v1{'y'} + $C_EPS) { } 14 10 24
2513 4 return 1;
2514 } elsif ($v0{y} > $v1{y} + $C_EPS) {
2515 14 return 0;
2516 } else {
2517 10 return ($v0{x} < $v1{x});
2518 }
2519 }
2520  
2521 # Initilialise the query structure (Q) and the trapezoid table (T)
2522 # when the first segment is added to start the trapezoidation. The
2523 # query-tree starts out with 4 trapezoids, one S-node and 2 Y-nodes
2524 #
2525 # 4
2526 # -----------------------------------
2527 # \
2528 # 1 \ 2
2529 # \
2530 # -----------------------------------
2531 # 3
2532 #
2533  
2534 2 ▶sub _init_query_structure {
Subroutine _init_query_structure: called CC=1 SCAR=0.0
2535 2 my ($segnum) = @_;
2536  
2537 2 my ($i1,$i2,$i3,$i4,$i5,$i6,$i7,$root);
2538 2 my ($t1,$t2,$t3,$t4);
2539  
2540 2 @qs = ();
2541 2 @tr = ();
2542  
2543 2 $q_idx = $tr_idx = 1;
2544  
2545 2 $i1 = _newnode();
2546 2 $qs[$i1]{nodetype} = $T_Y;
2547  
2548 2 my %tmpmax = %{_max($seg[$segnum]{v0}, $seg[$segnum]{v1})}; # root
2549 2 $qs[$i1]{yval} = {x => $tmpmax{x} , y => $tmpmax{y}};
2550 2 $root = $i1;
2551  
2552 2 $qs[$i1]{right} = $i2 = _newnode();
2553 2 $qs[$i2]{nodetype} = $T_SINK;
2554 2 $qs[$i2]{parent} = $i1;
2555  
2556 2 $qs[$i1]{left} = $i3 = _newnode();
2557 2 $qs[$i3]{nodetype} = $T_Y;
2558 2 my %tmpmin = %{_min($seg[$segnum]{v0}, $seg[$segnum]{v1})}; # root
2559 2 $qs[$i3]{yval} = {x => $tmpmin{x} , y => $tmpmin{y}};
2560 2 $qs[$i3]{parent} = $i1;
2561  
2562 2 $qs[$i3]{left} = $i4 = _newnode();
2563 2 $qs[$i4]{nodetype} = $T_SINK;
2564 2 $qs[$i4]{parent} = $i3;
2565  
2566 2 $qs[$i3]{right} = $i5 = _newnode();
2567 2 $qs[$i5]{nodetype} = $T_X;
2568 2 $qs[$i5]{segnum} = $segnum;
2569 2 $qs[$i5]{parent} = $i3;
2570  
2571 2 $qs[$i5]{left} = $i6 = _newnode();
2572 2 $qs[$i6]{nodetype} = $T_SINK;
2573 2 $qs[$i6]{parent} = $i5;
2574  
2575 2 $qs[$i5]{right} = $i7 = _newnode();
2576 2 $qs[$i7]{nodetype} = $T_SINK;
2577 2 $qs[$i7]{parent} = $i5;
2578  
2579 2 $t1 = _newtrap(); # middle left
2580 2 $t2 = _newtrap(); # middle right
2581 2 $t3 = _newtrap(); # bottom-most
2582 2 $t4 = _newtrap(); # topmost
2583  
2584 2 $tr[$t1]{hi} = {x => $qs[$i1]{yval}{x} , y => $qs[$i1]{yval}{y}};
2585 2 $tr[$t2]{hi} = {x => $qs[$i1]{yval}{x} , y => $qs[$i1]{yval}{y}};
2586 2 $tr[$t4]{lo} = {x => $qs[$i1]{yval}{x} , y => $qs[$i1]{yval}{y}};
2587 2 $tr[$t1]{lo} = {x => $qs[$i3]{yval}{x} , y => $qs[$i3]{yval}{y}};
2588 2 $tr[$t2]{lo} = {x => $qs[$i3]{yval}{x} , y => $qs[$i3]{yval}{y}};
2589 2 $tr[$t3]{hi} = {x => $qs[$i3]{yval}{x} , y => $qs[$i3]{yval}{y}};
2590 2 $tr[$t4]{hi} = {x => $INFINITY , y => $INFINITY};
2591 2 $tr[$t3]{lo} = {x => -1 * $INFINITY , y => -1 * $INFINITY};
2592 2 $tr[$t1]{rseg} = $tr[$t2]{lseg} = $segnum;
2593 2 $tr[$t1]{u0} = $tr[$t2]{u0} = $t4;
2594 2 $tr[$t1]{d0} = $tr[$t2]{d0} = $t3;
2595 2 $tr[$t4]{d0} = $tr[$t3]{u0} = $t1;
2596 2 $tr[$t4]{d1} = $tr[$t3]{u1} = $t2;
2597  
2598 2 $tr[$t1]{sink} = $i6;
2599 2 $tr[$t2]{sink} = $i7;
2600 2 $tr[$t3]{sink} = $i4;
2601 2 $tr[$t4]{sink} = $i2;
2602  
2603 2 $tr[$t1]{state} = $tr[$t2]{state} = $ST_VALID;
2604 2 $tr[$t3]{state} = $tr[$t4]{state} = $ST_VALID;
2605  
2606 2 $qs[$i2]{trnum} = $t4;
2607 2 $qs[$i4]{trnum} = $t3;
2608 2 $qs[$i6]{trnum} = $t1;
2609 2 $qs[$i7]{trnum} = $t2;
2610  
2611 2 $seg[$segnum]{is_inserted} = $TRUE;
2612 2 return $root;
2613 }
2614  
2615 # Update the roots stored for each of the endpoints of the segment.
2616 # This is done to speed up the location-query for the endpoint when
2617 # the segment is inserted into the trapezoidation subsequently
2618 #
2619 32 ▶sub _find_new_roots {
Subroutine _find_new_roots: called CC=2 SCAR=6.9
2620 32 my ($segnum) = @_;
2621  
2622 32 ▶ return if ($seg[$segnum]{is_inserted});
Branch: 1 branch
BranchTrueFalseTotal
if $seg[$segnum]{"is_inserted"} 18 14 32
2623  
2624 14 $seg[$segnum]{root0} = _locate_endpoint($seg[$segnum]{v0}, $seg[$segnum]{v1}, $seg[$segnum]{root0});
2625 14 $seg[$segnum]{root0} = $tr[$seg[$segnum]{root0}]{sink};
2626  
2627 14 $seg[$segnum]{root1} = _locate_endpoint($seg[$segnum]{v1}, $seg[$segnum]{v0}, $seg[$segnum]{root1});
2628 14 $seg[$segnum]{root1} = $tr[$seg[$segnum]{root1}]{sink};
2629 }
2630  
2631 # Main routine to perform trapezoidation
2632 2 ▶sub _construct_trapezoids {
Subroutine _construct_trapezoids: called CC=6 SCAR=17.9
2633 2 my ($nseg) = @_; #
2634  
2635 # Add the first segment and get the query structure and trapezoid
2636 # list initialised
2637  
2638 2 my $root = _init_query_structure(_choose_segment());
2639  
2640 2 ▶ for (my $i = 1 ; $i <= $nseg; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nseg) 16 2 18
2641 16 $seg[$i]{root0} = $seg[$i]{root1} = $root;
2642 }
2643 2 ▶ for (my $h = 1; $h <= _math_logstar_n($nseg); $h++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h <= _math_logstar_n($nseg)) 4 2 6
2644 4 ▶ for (my $i = _math_N($nseg, $h -1) + 1; $i <= _math_N($nseg, $h); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= _math_N($nseg, $h)) 10 4 14
2645 10 _add_segment(_choose_segment());
2646 }
2647 # Find a new root for each of the segment endpoints
2648 4 ▶ for (my $i = 1; $i <= $nseg; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nseg) 32 4 36
2649 32 _find_new_roots($i);
2650 }
2651 }
2652 2 ▶ for (my $i = _math_N($nseg, _math_logstar_n($nseg)) + 1; $i <= $nseg; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nseg) 4 2 6
2653 4 _add_segment(_choose_segment());
2654 }
2655 }
2656  
2657 # Add in the new segment into the trapezoidation and update Q and T
2658 # structures. First locate the two endpoints of the segment in the
2659 # Q-structure. Then start from the topmost trapezoid and go down to
2660 # the lower trapezoid dividing all the trapezoids in between .
2661 #
2662  
2663 14 ▶sub _add_segment {
Subroutine _add_segment: called CC=81 SCAR=53.2
2664 14 my ($segnum) = @_;
2665  
2666 14 my ($tu, $tl, $sk, $tfirst, $tlast, $tnext);
2667 14 my ($tfirstr, $tlastr, $tfirstl, $tlastl);
2668 14 my ($i1, $i2, $t, $t1, $t2, $tn);
2669 14 my $tritop = 0;
2670 14 my $tribot = 0;
2671 14 my $is_swapped = 0;
2672 14 my $tmptriseg;
2673 14 my %s = %{$seg[$segnum]};
2674  
2675 14 ▶ if (_greater_than($s{v1}, $s{v0})) { # Get higher vertex in v0
Branch: 1 branch
BranchTrueFalseTotal
if (_greater_than($s{"v1"}, $s{"v0"})) 8 6 14
2676 8 my %tmp;
2677 8 %tmp = %{$s{v0}};
2678 8 $s{v0} = {x => $s{v1}{x} , y => $s{v1}{y}};
2679 8 $s{v1} = {x => $tmp{x} , y => $tmp{y}};
2680 8 my $tmp = $s{root0};
2681 8 $s{root0} = $s{root1};
2682 8 $s{root1} = $tmp;
2683 8 $is_swapped = 1;
2684 }
2685  
2686 14 ▶ if (($is_swapped) ? !_inserted($segnum, $LASTPT) :
Branch: 2 branches
BranchTrueFalseTotal
if ($is_swapped ? !_inserted($segnum, $LASTPT) : !_inserted($segnum, $FIRSTPT)) { } 6 8 14
$is_swapped ? : 8 6 14
2687 !_inserted($segnum, $FIRSTPT)) { # insert v0 in the tree
2688 6 my $tmp_d;
2689  
2690 6 $tu = _locate_endpoint($s{v0}, $s{v1}, $s{root0});
2691 6 $tl = _newtrap(); # tl is the new lower trapezoid
2692 6 $tr[$tl]{state} = $ST_VALID;
2693 6 my %tmp = %{$tr[$tu]};
2694 6 my %tmphi = %{$tmp{hi}};
2695 6 my %tmplo = %{$tmp{lo}};
2696 6 $tr[$tl] = \%tmp;
2697 6 $tr[$tl]{hi} = {x => $tmphi{x} , y => $tmphi{y}};
2698 6 $tr[$tl]{lo} = {x => $tmplo{x} , y => $tmplo{y}};
2699 6 $tr[$tu]{lo} = {x => $s{v0}{x} , y => $s{v0}{y}};
2700 6 $tr[$tl]{hi} = {x => $s{v0}{x} , y => $s{v0}{y}};
2701 6 $tr[$tu]{d0} = $tl;
2702 6 $tr[$tu]{d1} = 0;
2703 6 $tr[$tl]{u0} = $tu;
2704 6 $tr[$tl]{u1} = 0;
2705  
2706 6 ▶ if ((($tmp_d = $tr[$tl]{d0}) > 0) && ($tr[$tmp_d]{u0} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u0"} == $tu) 4 2 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
!ll&&!rl&&r
024
MC/DC50%1/2
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
($tmp_d = $tr[$tl]{"d0"}) > 0 $tr[$tmp_d]{"u0"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d0"}) > 0B= $tr[$tmp_d]{"u0"} == $tu
ABresult
0X0
100
111
2707 4 $tr[$tmp_d]{u0} = $tl;
2708 }
2709 6 ▶ if ((($tmp_d = $tr[$tl]{d0}) > 0) && ($tr[$tmp_d]{u1} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u1"} == $tu) 2 4 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
!ll&&!rl&&r
042
MC/DC50%1/2
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
($tmp_d = $tr[$tl]{"d0"}) > 0 $tr[$tmp_d]{"u1"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d0"}) > 0B= $tr[$tmp_d]{"u1"} == $tu
ABresult
0X0
100
111
2710 2 $tr[$tmp_d]{u1} = $tl;
2711 }
2712  
2713 6 ▶ if ((($tmp_d = $tr[$tl]{d1}) > 0) && ($tr[$tmp_d]{u0} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u0"} == $tu) 3 3 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
!ll&&!rl&&r
303
MC/DC50%1/2
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
($tmp_d = $tr[$tl]{"d1"}) > 0 $tr[$tmp_d]{"u0"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d1"}) > 0B= $tr[$tmp_d]{"u0"} == $tu
ABresult
0X0
100
111
2714 3 $tr[$tmp_d]{u0} = $tl;
2715 }
2716 6 ▶ if ((($tmp_d = $tr[$tl]{d1}) > 0) && ($tr[$tmp_d]{u1} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u1"} == $tu) 0 6 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
!ll&&!rl&&r
330
MC/DC0%0/2
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
($tmp_d = $tr[$tl]{"d1"}) > 0 $tr[$tmp_d]{"u1"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d1"}) > 0B= $tr[$tmp_d]{"u1"} == $tu
ABresult
0X0
100
111
2717 0 $tr[$tmp_d]{u1} = $tl;
2718 }
2719  
2720 # Now update the query structure and obtain the sinks for the
2721 # two trapezoids
2722  
2723 6 $i1 = _newnode(); # Upper trapezoid sink
2724 6 $i2 = _newnode(); # Lower trapezoid sink
2725 6 $sk = $tr[$tu]{sink};
2726  
2727 6 $qs[$sk]{nodetype} = $T_Y;
2728 6 $qs[$sk]{yval} = {x => $s{v0}{x} , y=> $s{v0}{y}};
2729 6 $qs[$sk]{segnum} = $segnum; # not really reqd ... maybe later
2730 6 $qs[$sk]{left} = $i2;
2731 6 $qs[$sk]{right} = $i1;
2732  
2733 6 $qs[$i1]{nodetype} = $T_SINK;
2734 6 $qs[$i1]{trnum} = $tu;
2735 6 $qs[$i1]{parent} = $sk;
2736  
2737 6 $qs[$i2]{nodetype} = $T_SINK;
2738 6 $qs[$i2]{trnum} = $tl;
2739 6 $qs[$i2]{parent} = $sk;
2740  
2741 6 $tr[$tu]{sink} = $i1;
2742 6 $tr[$tl]{sink} = $i2;
2743 6 $tfirst = $tl;
2744 } else { # v0 already present
2745 # Get the topmost intersecting trapezoid
2746 8 $tfirst = _locate_endpoint($s{v0}, $s{v1}, $s{root0});
2747 8 $tritop = 1;
2748 }
2749  
2750  
2751 14 ▶ if (($is_swapped) ? !_inserted($segnum, $FIRSTPT) :
Branch: 2 branches
BranchTrueFalseTotal
if ($is_swapped ? !_inserted($segnum, $FIRSTPT) : !_inserted($segnum, $LASTPT)) { } 6 8 14
$is_swapped ? : 8 6 14
2752 !_inserted($segnum, $LASTPT)) { # insert v1 in the tree
2753 6 my $tmp_d;
2754  
2755 6 $tu = _locate_endpoint($s{v1}, $s{v0}, $s{root1});
2756 6 $tl = _newtrap(); # tl is the new lower trapezoid
2757 6 $tr[$tl]{state} = $ST_VALID;
2758 6 my %tmp = %{$tr[$tu]};
2759 6 my %tmphi = %{$tmp{hi}};
2760 6 my %tmplo = %{$tmp{lo}};
2761 6 $tr[$tl] = \%tmp;
2762 6 $tr[$tl]{hi} = {x => $tmphi{x} , y => $tmphi{y}};
2763 6 $tr[$tl]{lo} = {x => $tmplo{x} , y => $tmplo{y}};
2764 6 $tr[$tu]{lo} = {x => $s{v1}{x} , y => $s{v1}{y}};
2765 6 $tr[$tl]{hi} = {x => $s{v1}{x} , y => $s{v1}{y}};
2766 6 $tr[$tu]{d0} = $tl;
2767 6 $tr[$tu]{d1} = 0;
2768 6 $tr[$tl]{u0} = $tu;
2769 6 $tr[$tl]{u1} = 0;
2770  
2771 6 ▶ if ((($tmp_d = $tr[$tl]{d0}) > 0) && ($tr[$tmp_d]{u0} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u0"} == $tu) 3 3 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
!ll&&!rl&&r
303
MC/DC50%1/2
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
($tmp_d = $tr[$tl]{"d0"}) > 0 $tr[$tmp_d]{"u0"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d0"}) > 0B= $tr[$tmp_d]{"u0"} == $tu
ABresult
0X0
100
111
2772 3 $tr[$tmp_d]{u0} = $tl;
2773 }
2774 6 ▶ if ((($tmp_d = $tr[$tl]{d0}) > 0) && ($tr[$tmp_d]{u1} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u1"} == $tu) 0 6 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
!ll&&!rl&&r
330
MC/DC0%0/2
($tmp_d = $tr[$tl]{"d0"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
($tmp_d = $tr[$tl]{"d0"}) > 0 $tr[$tmp_d]{"u1"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d0"}) > 0B= $tr[$tmp_d]{"u1"} == $tu
ABresult
0X0
100
111
2775 0 $tr[$tmp_d]{u1} = $tl;
2776 }
2777  
2778 6 ▶ if ((($tmp_d = $tr[$tl]{d1}) > 0) && ($tr[$tmp_d]{u0} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u0"} == $tu) 2 4 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
!ll&&!rl&&r
402
MC/DC50%1/2
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u0"} == $tu
($tmp_d = $tr[$tl]{"d1"}) > 0 $tr[$tmp_d]{"u0"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d1"}) > 0B= $tr[$tmp_d]{"u0"} == $tu
ABresult
0X0
100
111
2779 2 $tr[$tmp_d]{u0} = $tl;
2780 }
2781 6 ▶ if ((($tmp_d = $tr[$tl]{d1}) > 0) && ($tr[$tmp_d]{u1} == $tu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u1"} == $tu) 0 6 6
Condition66%2/3
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
!ll&&!rl&&r
420
MC/DC0%0/2
($tmp_d = $tr[$tl]{"d1"}) > 0 and $tr[$tmp_d]{"u1"} == $tu
($tmp_d = $tr[$tl]{"d1"}) > 0 $tr[$tmp_d]{"u1"} == $tu
Truth table66%2/3
A and B
A= ($tmp_d = $tr[$tl]{"d1"}) > 0B= $tr[$tmp_d]{"u1"} == $tu
ABresult
0X0
100
111
2782 0 $tr[$tmp_d]{u1} = $tl;
2783 }
2784  
2785 # Now update the query structure and obtain the sinks for the
2786 # two trapezoids
2787  
2788 6 $i1 = _newnode(); # Upper trapezoid sink
2789 6 $i2 = _newnode(); # Lower trapezoid sink
2790 6 $sk = $tr[$tu]{sink};
2791  
2792 6 $qs[$sk]{nodetype} = $T_Y;
2793 6 $qs[$sk]{yval} = {x => $s{v1}{x} , y => $s{v1}{y}};
2794 6 $qs[$sk]{segnum} = $segnum; # not really reqd ... maybe later
2795 6 $qs[$sk]{left} = $i2;
2796 6 $qs[$sk]{right} = $i1;
2797  
2798 6 $qs[$i1]{nodetype} = $T_SINK;
2799 6 $qs[$i1]{trnum} = $tu;
2800 6 $qs[$i1]{parent} = $sk;
2801  
2802 6 $qs[$i2]{nodetype} = $T_SINK;
2803 6 $qs[$i2]{trnum} = $tl;
2804 6 $qs[$i2]{parent} = $sk;
2805  
2806 6 $tr[$tu]{sink} = $i1;
2807 6 $tr[$tl]{sink} = $i2;
2808 6 $tlast = $tu;
2809 } else { # v1 already present
2810 # Get the lowermost intersecting trapezoid
2811 8 $tlast = _locate_endpoint($s{v1}, $s{v0}, $s{root1});
2812 8 $tribot = 1;
2813 }
2814  
2815 # Thread the segment into the query tree creating a new X-node
2816 # First, split all the trapezoids which are intersected by s into
2817 # two
2818  
2819 14 $t = $tfirst; # topmost trapezoid
2820  
2821 14 ▶ while (($t > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($t > 0 and _greater_than_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"})) 25 14 39
Condition100%3/3
$t > 0 and _greater_than_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"})
!ll&&!rl&&r
7725
MC/DC100%2/2
$t > 0 and _greater_than_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"})
$t > 0 _greater_than_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"})
Truth table100%3/3
A and B
A= $t > 0B= _greater_than_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"})
ABresult
0X0
100
111
2822 _greater_than_equal_to($tr[$t]{lo}, $tr[$tlast]{lo})) {
2823 # traverse from top to bot
2824 25 my ($t_sav, $tn_sav);
2825 25 $sk = $tr[$t]{sink};
2826 25 $i1 = _newnode(); # left trapezoid sink
2827 25 $i2 = _newnode(); # right trapezoid sink
2828  
2829 25 $qs[$sk]{nodetype} = $T_X;
2830 25 $qs[$sk]{segnum} = $segnum;
2831 25 $qs[$sk]{left} = $i1;
2832 25 $qs[$sk]{right} = $i2;
2833  
2834 25 $qs[$i1]{nodetype} = $T_SINK; # left trapezoid (use existing one)
2835 25 $qs[$i1]{trnum} = $t;
2836 25 $qs[$i1]{parent} = $sk;
2837  
2838 25 $qs[$i2]{nodetype} = $T_SINK; # right trapezoid (allocate new)
2839 25 $qs[$i2]{trnum} = $tn = _newtrap();
2840 25 $tr[$tn]{state} = $ST_VALID;
2841 25 $qs[$i2]{parent} = $sk;
2842  
2843 25 ▶ if ($t == $tfirst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == $tfirst) 14 11 25
2844 14 $tfirstr = $tn;
2845 }
2846 25 ▶ if (_equal_to($tr[$t]{lo}, $tr[$tlast]{lo})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"})) 14 11 25
2847 14 $tlastr = $tn;
2848 }
2849  
2850 25 my %tmp = %{$tr[$t]};
2851 25 my %tmphi = %{$tmp{hi}};
2852 25 my %tmplo = %{$tmp{lo}};
2853 25 $tr[$tn] = \%tmp;
2854 25 $tr[$tn]{hi} = {x => $tmphi{x} , y => $tmphi{y}};
2855 25 $tr[$tn]{lo} = {x => $tmplo{x} , y => $tmplo{y}};
2856 25 $tr[$t]{sink} = $i1;
2857 25 $tr[$tn]{sink} = $i2;
2858 25 $t_sav = $t;
2859 25 $tn_sav = $tn;
2860  
2861 # error
2862  
2863 25 ▶ if (($tr[$t]{d0} <= 0) && ($tr[$t]{d1} <= 0)) { # case cannot arise
Branch: 3 branches
BranchTrueFalseTotal
if ($tr[$t]{'d0'} <= 0 and $tr[$t]{'d1'} <= 0) { } 0 25 25
elsif ($tr[$t]{'d0'} > 0 and $tr[$t]{'d1'} <= 0) { } 17 8 25
elsif ($tr[$t]{'d0'} <= 0 and $tr[$t]{'d1'} > 0) { } 0 8 8
Condition33%1/3
$tr[$t]{"d0"} <= 0 and $tr[$t]{"d1"} <= 0
!ll&&!rl&&r
2500
MC/DC0%0/2
$tr[$t]{"d0"} <= 0 and $tr[$t]{"d1"} <= 0
$tr[$t]{"d0"} <= 0 $tr[$t]{"d1"} <= 0
Truth table33%1/3
A and B
A= $tr[$t]{"d0"} <= 0B= $tr[$t]{"d1"} <= 0
ABresult
0X0
100
111
2864 0 print "add_segment: error\n";
2865  
2866 # only one trapezoid below. partition t into two and make the
2867 # two resulting trapezoids t and tn as the upper neighbours of
2868 # the sole lower trapezoid
2869  
2870 ▶ } elsif (($tr[$t]{d0} > 0) && ($tr[$t]{d1} <= 0)) { # Only one trapezoid below
Condition66%2/3
$tr[$t]{"d0"} > 0 and $tr[$t]{"d1"} <= 0
!ll&&!rl&&r
0817
MC/DC50%1/2
$tr[$t]{"d0"} > 0 and $tr[$t]{"d1"} <= 0
$tr[$t]{"d0"} > 0 $tr[$t]{"d1"} <= 0
Truth table66%2/3
A and B
A= $tr[$t]{"d0"} > 0B= $tr[$t]{"d1"} <= 0
ABresult
0X0
100
111
2871 17 ▶ if (($tr[$t]{u0} > 0) && ($tr[$t]{u1} > 0)) { # continuation of a chain from abv.
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'u0'} > 0 and $tr[$t]{'u1'} > 0) { } 12 5 17
Condition66%2/3
$tr[$t]{"u0"} > 0 and $tr[$t]{"u1"} > 0
!ll&&!rl&&r
0512
MC/DC50%1/2
$tr[$t]{"u0"} > 0 and $tr[$t]{"u1"} > 0
$tr[$t]{"u0"} > 0 $tr[$t]{"u1"} > 0
Truth table66%2/3
A and B
A= $tr[$t]{"u0"} > 0B= $tr[$t]{"u1"} > 0
ABresult
0X0
100
111
2872 12 ▶ if ($tr[$t]{usave} > 0) { # three upper neighbours
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'usave'} > 0) { } 5 7 12
2873 5 ▶ if ($tr[$t]{uside} == $S_LEFT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'uside'} == $S_LEFT) { } 2 3 5
2874 2 $tr[$tn]{u0} = $tr[$t]{u1};
2875 2 $tr[$t]{u1} = -1;
2876 2 $tr[$tn]{u1} = $tr[$t]{usave};
2877  
2878 2 $tr[$tr[$t]{u0}]{d0} = $t;
2879 2 $tr[$tr[$tn]{u0}]{d0} = $tn;
2880 2 $tr[$tr[$tn]{u1}]{d0} = $tn;
2881 } else { # intersects in the right
2882 3 $tr[$tn]{u1} = -1;
2883 3 $tr[$tn]{u0} = $tr[$t]{u1};
2884 3 $tr[$t]{u1} = $tr[$t]{u0};
2885 3 $tr[$t]{u0} = $tr[$t]{usave};
2886  
2887 3 $tr[$tr[$t]{u0}]{d0} = $t;
2888 3 $tr[$tr[$t]{u1}]{d0} = $t;
2889 3 $tr[$tr[$tn]{u0}]{d0} = $tn;
2890 }
2891  
2892 5 $tr[$t]{usave} = $tr[$tn]{usave} = 0;
2893 } else { # No usave.... simple case
2894 7 $tr[$tn]{u0} = $tr[$t]{u1};
2895 7 $tr[$t]{u1} = $tr[$tn]{u1} = -1;
2896 7 $tr[$tr[$tn]{u0}]{d0} = $tn;
2897 }
2898 } else { # fresh seg. or upward cusp
2899 5 my $tmp_u = $tr[$t]{u0};
2900 5 my ($td0, $td1);
2901 5 ▶ if ((($td0 = $tr[$tmp_u]{d0}) > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if (($td0 = $tr[$tmp_u]{'d0'}) > 0 and ($td1 = $tr[$tmp_u]{'d1'}) > 0) { } 1 4 5
Condition66%2/3
($td0 = $tr[$tmp_u]{"d0"}) > 0 and ($td1 = $tr[$tmp_u]{"d1"}) > 0
!ll&&!rl&&r
041
MC/DC50%1/2
($td0 = $tr[$tmp_u]{"d0"}) > 0 and ($td1 = $tr[$tmp_u]{"d1"}) > 0
($td0 = $tr[$tmp_u]{"d0"}) > 0 ($td1 = $tr[$tmp_u]{"d1"}) > 0
Truth table66%2/3
A and B
A= ($td0 = $tr[$tmp_u]{"d0"}) > 0B= ($td1 = $tr[$tmp_u]{"d1"}) > 0
ABresult
0X0
100
111
2902 (($td1 = $tr[$tmp_u]{d1}) > 0)) { # upward cusp
2903 1 ▶ if (($tr[$td0]{rseg} > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$td0]{'rseg'} > 0 and not _is_left_of($tr[$td0]{'rseg'}, $s{'v1'})) { } 0 1 1
Condition33%1/3
$tr[$td0]{"rseg"} > 0 and not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
!ll&&!rl&&r
010
MC/DC0%0/2
$tr[$td0]{"rseg"} > 0 and not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
$tr[$td0]{"rseg"} > 0 not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
Truth table33%1/3
A and not(B)
A= $tr[$td0]{"rseg"} > 0B= not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
ABresult
0X0
100
111
2904 !_is_left_of($tr[$td0]{rseg}, $s{v1})) {
2905 0 $tr[$t]{u0} = $tr[$t]{u1} = $tr[$tn]{u1} = -1;
2906 0 $tr[$tr[$tn]{u0}]{d1} = $tn;
2907 } else { # cusp going leftwards
2908 1 $tr[$tn]{u0} = $tr[$tn]{u1} = $tr[$t]{u1} = -1;
2909 1 $tr[$tr[$t]{u0}]{d0} = $t;
2910 }
2911 } else { # fresh segment
2912 4 $tr[$tr[$t]{u0}]{d0} = $t;
2913 4 $tr[$tr[$t]{u0}]{d1} = $tn;
2914 }
2915 }
2916  
2917 17 ▶ if (_fp_equal($tr[$t]{lo}{y}, $tr[$tlast]{lo}{y}, $precision) &&
Branch: 1 branch
BranchTrueFalseTotal
if (_fp_equal($tr[$t]{'lo'}{'y'}, $tr[$tlast]{'lo'}{'y'}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{'lo'}{'x'}, $tr[$tlast]{'lo'}{'x'}, $Math::Geometry::Planar::precision) and $tribot) { } 3 14 17
Condition100%6/6
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) and $tribot
!ll&&!rl&&r
863
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision)
!ll&&!rl&&r
539
MC/DC100%3/3
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) and $tribot
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) $tribot
Truth table100%4/4
A and B and C
A= _fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision)B= _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision)C= $tribot
ABCresult
0XX0
10X0
1100
1111
2918 _fp_equal($tr[$t]{lo}{x}, $tr[$tlast]{lo}{x}, $precision) && $tribot) {
2919 # bottom forms a triangle
2920  
2921 3 ▶ if ($is_swapped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_swapped) { } 1 2 3
2922 1 $tmptriseg = $seg[$segnum]{prev};
2923 } else {
2924 2 $tmptriseg = $seg[$segnum]{next};
2925 }
2926  
2927 3 ▶ if (($tmptriseg > 0) && _is_left_of($tmptriseg, $s{v0})) { # L-R downward cusp
Branch: 1 branch
BranchTrueFalseTotal
if ($tmptriseg > 0 and _is_left_of($tmptriseg, $s{'v0'})) { } 1 2 3
Condition66%2/3
$tmptriseg > 0 and _is_left_of($tmptriseg, $s{"v0"})
!ll&&!rl&&r
021
MC/DC50%1/2
$tmptriseg > 0 and _is_left_of($tmptriseg, $s{"v0"})
$tmptriseg > 0 _is_left_of($tmptriseg, $s{"v0"})
Truth table66%2/3
A and B
A= $tmptriseg > 0B= _is_left_of($tmptriseg, $s{"v0"})
ABresult
0X0
100
111
2928 1 $tr[$tr[$t]{d0}]{u0} = $t;
2929 1 $tr[$tn]{d0} = $tr[$tn]{d1} = -1;
2930 } else { # R-L downward cusp
2931 2 $tr[$tr[$tn]{d0}]{u1} = $tn;
2932 2 $tr[$t]{d0} = $tr[$t]{d1} = -1;
2933 }
2934 } else {
2935 14 ▶ if (($tr[$tr[$t]{d0}]{u0} > 0) && ($tr[$tr[$t]{d0}]{u1} > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$tr[$t]{"d0"}]{"u0"} > 0 and $tr[$tr[$t]{"d0"}]{"u1"} > 0) 6 8 14
Condition66%2/3
$tr[$tr[$t]{"d0"}]{"u0"} > 0 and $tr[$tr[$t]{"d0"}]{"u1"} > 0
!ll&&!rl&&r
086
MC/DC50%1/2
$tr[$tr[$t]{"d0"}]{"u0"} > 0 and $tr[$tr[$t]{"d0"}]{"u1"} > 0
$tr[$tr[$t]{"d0"}]{"u0"} > 0 $tr[$tr[$t]{"d0"}]{"u1"} > 0
Truth table66%2/3
A and B
A= $tr[$tr[$t]{"d0"}]{"u0"} > 0B= $tr[$tr[$t]{"d0"}]{"u1"} > 0
ABresult
0X0
100
111
2936 6 ▶ if ($tr[$tr[$t]{d0}]{u0} == $t) { # passes thru LHS
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$tr[$t]{'d0'}]{'u0'} == $t) { } 3 3 6
2937 3 $tr[$tr[$t]{d0}]{usave} = $tr[$tr[$t]{d0}]{u1};
2938 3 $tr[$tr[$t]{d0}]{uside} = $S_LEFT;
2939 } else {
2940 3 $tr[$tr[$t]{d0}]{usave} = $tr[$tr[$t]{d0}]{u0};
2941 3 $tr[$tr[$t]{d0}]{uside} = $S_RIGHT;
2942 }
2943 }
2944 14 $tr[$tr[$t]{d0}]{u0} = $t;
2945 14 $tr[$tr[$t]{d0}]{u1} = $tn;
2946 }
2947  
2948 17 $t = $tr[$t]{d0};
2949  
2950 ▶ } elsif (($tr[$t]{d0} <= 0) && ($tr[$t]{d1} > 0)) { # Only one trapezoid below
Condition33%1/3
$tr[$t]{"d0"} <= 0 and $tr[$t]{"d1"} > 0
!ll&&!rl&&r
800
MC/DC0%0/2
$tr[$t]{"d0"} <= 0 and $tr[$t]{"d1"} > 0
$tr[$t]{"d0"} <= 0 $tr[$t]{"d1"} > 0
Truth table33%1/3
A and B
A= $tr[$t]{"d0"} <= 0B= $tr[$t]{"d1"} > 0
ABresult
0X0
100
111
2951 0 ▶ if (($tr[$t]{u0} > 0) && ($tr[$t]{u1} > 0)) { # continuation of a chain from abv.
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'u0'} > 0 and $tr[$t]{'u1'} > 0) { } 0 0 0
Condition0%0/3
$tr[$t]{"u0"} > 0 and $tr[$t]{"u1"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$tr[$t]{"u0"} > 0 and $tr[$t]{"u1"} > 0
$tr[$t]{"u0"} > 0 $tr[$t]{"u1"} > 0
Truth table0%0/3
A and B
A= $tr[$t]{"u0"} > 0B= $tr[$t]{"u1"} > 0
ABresult
0X0
100
111
2952 0 ▶ if ($tr[$t]{usave} > 0) { # three upper neighbours
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'usave'} > 0) { } 0 0 0
2953 0 ▶ if ($tr[$t]{uside} == $S_LEFT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'uside'} == $S_LEFT) { } 0 0 0
2954 0 $tr[$tn]{u0} = $tr[$t]{u1};
2955 0 $tr[$t]{u1} = -1;
2956 0 $tr[$tn]{u1} = $tr[$t]{usave};
2957  
2958 0 $tr[$tr[$t]{u0}]{d0} = $t;
2959 0 $tr[$tr[$tn]{u0}]{d0} = $tn;
2960 0 $tr[$tr[$tn]{u1}]{d0} = $tn;
2961 } else { # intersects in the right
2962 0 $tr[$tn]{u1} = -1;
2963 0 $tr[$tn]{u0} = $tr[$t]{u1};
2964 0 $tr[$t]{u1} = $tr[$t]{u0};
2965 0 $tr[$t]{u0} = $tr[$t]{usave};
2966  
2967 0 $tr[$tr[$t]{u0}]{d0} = $t;
2968 0 $tr[$tr[$t]{u1}]{d0} = $t;
2969 0 $tr[$tr[$tn]{u0}]{d0} = $tn;
2970 }
2971  
2972 0 $tr[$t]{usave} = $tr[$tn]{usave} = 0;
2973  
2974 } else { # No usave.... simple case
2975 0 $tr[$tn]{u0} = $tr[$t]{u1};
2976 0 $tr[$t]{u1} = $tr[$tn]{u1} = -1;
2977 0 $tr[$tr[$tn]{u0}]{d0} = $tn;
2978 }
2979 } else { # fresh seg. or upward cusp
2980 0 my $tmp_u = $tr[$t]{u0};
2981 0 my ($td0,$td1);
2982 0 ▶ if ((($td0 = $tr[$tmp_u]{d0}) > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if (($td0 = $tr[$tmp_u]{'d0'}) > 0 and ($td1 = $tr[$tmp_u]{'d1'}) > 0) { } 0 0 0
Condition0%0/3
($td0 = $tr[$tmp_u]{"d0"}) > 0 and ($td1 = $tr[$tmp_u]{"d1"}) > 0
!ll&&!rl&&r
000
MC/DC0%0/2
($td0 = $tr[$tmp_u]{"d0"}) > 0 and ($td1 = $tr[$tmp_u]{"d1"}) > 0
($td0 = $tr[$tmp_u]{"d0"}) > 0 ($td1 = $tr[$tmp_u]{"d1"}) > 0
Truth table0%0/3
A and B
A= ($td0 = $tr[$tmp_u]{"d0"}) > 0B= ($td1 = $tr[$tmp_u]{"d1"}) > 0
ABresult
0X0
100
111
2983 (($td1 = $tr[$tmp_u]{d1}) > 0)) { # upward cusp
2984 0 ▶ if (($tr[$td0]{rseg} > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$td0]{'rseg'} > 0 and not _is_left_of($tr[$td0]{'rseg'}, $s{'v1'})) { } 0 0 0
Condition0%0/3
$tr[$td0]{"rseg"} > 0 and not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
!ll&&!rl&&r
000
MC/DC0%0/2
$tr[$td0]{"rseg"} > 0 and not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
$tr[$td0]{"rseg"} > 0 not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
Truth table0%0/3
A and not(B)
A= $tr[$td0]{"rseg"} > 0B= not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
ABresult
0X0
100
111
2985 !_is_left_of($tr[$td0]{rseg}, $s{v1})) {
2986 0 $tr[$t]{u0} = $tr[$t]{u1} = $tr[$tn]{u1} = -1;
2987 0 $tr[$tr[$tn]{u0}]{d1} = $tn;
2988 } else {
2989 0 $tr[$tn]{u0} = $tr[$tn]{u1} = $tr[$t]{u1} = -1;
2990 0 $tr[$tr[$t]{u0}]{d0} = $t;
2991 }
2992 } else { # fresh segment
2993 0 $tr[$tr[$t]{u0}]{d0} = $t;
2994 0 $tr[$tr[$t]{u0}]{d1} = $tn;
2995 }
2996 }
2997  
2998 0 ▶ if (_fp_equaL($tr[$t]{lo}{y}, $tr[$tlast]{lo}{y}, $precision) &&
Branch: 1 branch
BranchTrueFalseTotal
if (_fp_equaL($tr[$t]{'lo'}{'y'}, $tr[$tlast]{'lo'}{'y'}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{'lo'}{'x'}, $tr[$tlast]{'lo'}{'x'}, $Math::Geometry::Planar::precision) and $tribot) { } 0 0 0
Condition0%0/6
_fp_equaL($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) and $tribot
!ll&&!rl&&r
000
_fp_equaL($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision)
!ll&&!rl&&r
000
MC/DC0%0/3
_fp_equaL($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) and $tribot
_fp_equaL($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) $tribot
Truth table0%0/4
A and B and C
A= _fp_equaL($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision)B= _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision)C= $tribot
ABCresult
0XX0
10X0
1100
1111
2999 _fp_equal($tr[$t]{lo}{x}, $tr[$tlast]{lo}{x}, $precision) && $tribot) {
3000 # bottom forms a triangle
3001 0 my $tmpseg;
3002  
3003 0 ▶ if ($is_swapped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_swapped) { } 0 0 0
3004 0 $tmptriseg = $seg[$segnum]{prev};
3005 } else {
3006 0 $tmptriseg = $seg[$segnum]{next};
3007 }
3008  
3009 0 ▶ if (($tmpseg > 0) && _is_left_of($tmpseg, $s{v0})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpseg > 0 and _is_left_of($tmpseg, $s{'v0'})) { } 0 0 0
Condition0%0/3
$tmpseg > 0 and _is_left_of($tmpseg, $s{"v0"})
!ll&&!rl&&r
000
MC/DC0%0/2
$tmpseg > 0 and _is_left_of($tmpseg, $s{"v0"})
$tmpseg > 0 _is_left_of($tmpseg, $s{"v0"})
Truth table0%0/3
A and B
A= $tmpseg > 0B= _is_left_of($tmpseg, $s{"v0"})
ABresult
0X0
100
111
3010 # L-R downward cusp
3011 0 $tr[$tr[$t]{d1}]{u0} = $t;
3012 0 $tr[$tn]{d0} = $tr[$tn]{d1} = -1;
3013 } else {
3014 # R-L downward cusp
3015 0 $tr[$tr[$tn]{d1}]{u1} = $tn;
3016 0 $tr[$t]{d0} = $tr[$t]{d1} = -1;
3017 }
3018 } else {
3019 0 ▶ if (($tr[$tr[$t]{d1}]{u0} > 0) && ($tr[$tr[$t]{d1}]{u1} > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$tr[$t]{"d1"}]{"u0"} > 0 and $tr[$tr[$t]{"d1"}]{"u1"} > 0) 0 0 0
Condition0%0/3
$tr[$tr[$t]{"d1"}]{"u0"} > 0 and $tr[$tr[$t]{"d1"}]{"u1"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$tr[$tr[$t]{"d1"}]{"u0"} > 0 and $tr[$tr[$t]{"d1"}]{"u1"} > 0
$tr[$tr[$t]{"d1"}]{"u0"} > 0 $tr[$tr[$t]{"d1"}]{"u1"} > 0
Truth table0%0/3
A and B
A= $tr[$tr[$t]{"d1"}]{"u0"} > 0B= $tr[$tr[$t]{"d1"}]{"u1"} > 0
ABresult
0X0
100
111
3020 0 ▶ if ($tr[$tr[$t]{d1}]{u0} == $t) { # passes thru LHS
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$tr[$t]{'d1'}]{'u0'} == $t) { } 0 0 0
3021 0 $tr[$tr[$t]{d1}]{usave} = $tr[$tr[$t]{d1}]{u1};
3022 0 $tr[$tr[$t]{d1}]{uside} = $S_LEFT;
3023 } else {
3024 0 $tr[$tr[$t]{d1}]{usave} = $tr[$tr[$t]{d1}]{u0};
3025 0 $tr[$tr[$t]{d1}]{uside} = $S_RIGHT;
3026 }
3027 }
3028 0 $tr[$tr[$t]{d1}]{u0} = $t;
3029 0 $tr[$tr[$t]{d1}]{u1} = $tn;
3030 }
3031  
3032 0 $t = $tr[$t]{d1};
3033  
3034 # two trapezoids below. Find out which one is intersected by
3035 # this segment and proceed down that one
3036  
3037 } else {
3038 8 my $tmpseg = $tr[$tr[$t]{d0}]{rseg};
3039 8 my ($y0,$yt);
3040 8 my %tmppt;
3041 8 my ($tnext, $i_d0, $i_d1);
3042  
3043 8 $i_d0 = $i_d1 = $FALSE;
3044 8 ▶ if (_fp_equal($tr[$t]{lo}{y}, $s{v0}{y}, $precision)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_fp_equal($tr[$t]{'lo'}{'y'}, $s{'v0'}{'y'}, $Math::Geometry::Planar::precision)) { } 2 6 8
3045 2 ▶ if ($tr[$t]{lo}{x} > $s{v0}{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'lo'}{'x'} > $s{'v0'}{'x'}) { } 0 2 2
3046 0 $i_d0 = $TRUE;
3047 } else {
3048 2 $i_d1 = $TRUE;
3049 }
3050 } else {
3051 6 $tmppt{y} = $y0 = $tr[$t]{lo}{y};
3052 6 $yt = ($y0 - $s{v0}{y})/($s{v1}{y} - $s{v0}{y});
3053 6 $tmppt{x} = $s{v0}{x} + $yt * ($s{v1}{x} - $s{v0}{x});
3054  
3055 6 ▶ if (_less_than(\%tmppt, $tr[$t]{lo})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_less_than(\%tmppt, $tr[$t]{'lo'})) { } 1 5 6
3056 1 $i_d0 = $TRUE;
3057 } else {
3058 5 $i_d1 = $TRUE;
3059 }
3060 }
3061  
3062 # check continuity from the top so that the lower-neighbour
3063 # values are properly filled for the upper trapezoid
3064  
3065 8 ▶ if (($tr[$t]{u0} > 0) && ($tr[$t]{u1} > 0)) { # continuation of a chain from abv.
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'u0'} > 0 and $tr[$t]{'u1'} > 0) { } 4 4 8
Condition66%2/3
$tr[$t]{"u0"} > 0 and $tr[$t]{"u1"} > 0
!ll&&!rl&&r
044
MC/DC50%1/2
$tr[$t]{"u0"} > 0 and $tr[$t]{"u1"} > 0
$tr[$t]{"u0"} > 0 $tr[$t]{"u1"} > 0
Truth table66%2/3
A and B
A= $tr[$t]{"u0"} > 0B= $tr[$t]{"u1"} > 0
ABresult
0X0
100
111
3066 4 ▶ if ($tr[$t]{usave} > 0) { # three upper neighbours
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'usave'} > 0) { } 1 3 4
3067 1 ▶ if ($tr[$t]{uside} == $S_LEFT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'uside'} == $S_LEFT) { } 1 0 1
3068 1 $tr[$tn]{u0} = $tr[$t]{u1};
3069 1 $tr[$t]{u1} = -1;
3070 1 $tr[$tn]{u1} = $tr[$t]{usave};
3071  
3072 1 $tr[$tr[$t]{u0}]{d0} = $t;
3073 1 $tr[$tr[$tn]{u0}]{d0} = $tn;
3074 1 $tr[$tr[$tn]{u1}]{d0} = $tn;
3075 } else { # intersects in the right
3076 0 $tr[$tn]{u1} = -1;
3077 0 $tr[$tn]{u0} = $tr[$t]{u1};
3078 0 $tr[$t]{u1} = $tr[$t]{u0};
3079 0 $tr[$t]{u0} = $tr[$t]{usave};
3080  
3081 0 $tr[$tr[$t]{u0}]{d0} = $t;
3082 0 $tr[$tr[$t]{u1}]{d0} = $t;
3083 0 $tr[$tr[$tn]{u0}]{d0} = $tn;
3084 }
3085  
3086 1 $tr[$t]{usave} = $tr[$tn]{usave} = 0;
3087 } else { # No usave.... simple case
3088 3 $tr[$tn]{u0} = $tr[$t]{u1};
3089 3 $tr[$tn]{u1} = -1;
3090 3 $tr[$t]{u1} = -1;
3091 3 $tr[$tr[$tn]{u0}]{d0} = $tn;
3092 }
3093 } else { # fresh seg. or upward cusp
3094 4 my $tmp_u = $tr[$t]{u0};
3095 4 my ($td0, $td1);
3096 4 ▶ if ((($td0 = $tr[$tmp_u]{d0}) > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if (($td0 = $tr[$tmp_u]{'d0'}) > 0 and ($td1 = $tr[$tmp_u]{'d1'}) > 0) { } 2 2 4
Condition66%2/3
($td0 = $tr[$tmp_u]{"d0"}) > 0 and ($td1 = $tr[$tmp_u]{"d1"}) > 0
!ll&&!rl&&r
022
MC/DC50%1/2
($td0 = $tr[$tmp_u]{"d0"}) > 0 and ($td1 = $tr[$tmp_u]{"d1"}) > 0
($td0 = $tr[$tmp_u]{"d0"}) > 0 ($td1 = $tr[$tmp_u]{"d1"}) > 0
Truth table66%2/3
A and B
A= ($td0 = $tr[$tmp_u]{"d0"}) > 0B= ($td1 = $tr[$tmp_u]{"d1"}) > 0
ABresult
0X0
100
111
3097 (($td1 = $tr[$tmp_u]{d1}) > 0)) { # upward cusp
3098 2 ▶ if (($tr[$td0]{rseg} > 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$td0]{'rseg'} > 0 and not _is_left_of($tr[$td0]{'rseg'}, $s{'v1'})) { } 1 1 2
Condition66%2/3
$tr[$td0]{"rseg"} > 0 and not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
!ll&&!rl&&r
011
MC/DC50%1/2
$tr[$td0]{"rseg"} > 0 and not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
$tr[$td0]{"rseg"} > 0 not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
Truth table66%2/3
A and not(B)
A= $tr[$td0]{"rseg"} > 0B= not _is_left_of($tr[$td0]{"rseg"}, $s{"v1"})
ABresult
0X0
100
111
3099 !_is_left_of($tr[$td0]{rseg}, $s{v1})) {
3100 1 $tr[$t]{u0} = $tr[$t]{u1} = $tr[$tn]{u1} = -1;
3101 1 $tr[$tr[$tn]{u0}]{d1} = $tn;
3102 } else {
3103 1 $tr[$tn]{u0} = $tr[$tn]{u1} = $tr[$t]{u1} = -1;
3104 1 $tr[$tr[$t]{u0}]{d0} = $t;
3105 }
3106 } else { # fresh segment
3107 2 $tr[$tr[$t]{u0}]{d0} = $t;
3108 2 $tr[$tr[$t]{u0}]{d1} = $tn;
3109 }
3110 }
3111  
3112 8 ▶ if (_fp_equal($tr[$t]{lo}{y}, $tr[$tlast]{lo}{y}, $precision) &&
Branch: 2 branches
BranchTrueFalseTotal
if (_fp_equal($tr[$t]{'lo'}{'y'}, $tr[$tlast]{'lo'}{'y'}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{'lo'}{'x'}, $tr[$tlast]{'lo'}{'x'}, $Math::Geometry::Planar::precision) and $tribot) { } 5 3 8
elsif ($i_d0) { } 1 2 3
Condition66%4/6
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) and $tribot
!ll&&!rl&&r
305
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision)
!ll&&!rl&&r
305
MC/DC33%1/3
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) and _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) and $tribot
_fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision) _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision) $tribot
Truth table50%2/4
A and B and C
A= _fp_equal($tr[$t]{"lo"}{"y"}, $tr[$tlast]{"lo"}{"y"}, $Math::Geometry::Planar::precision)B= _fp_equal($tr[$t]{"lo"}{"x"}, $tr[$tlast]{"lo"}{"x"}, $Math::Geometry::Planar::precision)C= $tribot
ABCresult
0XX0
10X0
1100
1111
3113 _fp_equal($tr[$t]{lo}{x}, $tr[$tlast]{lo}{x}, $precision) && $tribot) {
3114 # this case arises only at the lowest trapezoid.. i.e.
3115 # tlast, if the lower endpoint of the segment is
3116 # already inserted in the structure
3117  
3118 5 $tr[$tr[$t]{d0}]{u0} = $t;
3119 5 $tr[$tr[$t]{d0}]{u1} = -1;
3120 5 $tr[$tr[$t]{d1}]{u0} = $tn;
3121 5 $tr[$tr[$t]{d1}]{u1} = -1;
3122  
3123 5 $tr[$tn]{d0} = $tr[$t]{d1};
3124 5 $tr[$t]{d1} = $tr[$tn]{d1} = -1;
3125  
3126 5 $tnext = $tr[$t]{d1};
3127 } elsif ($i_d0) { # intersecting d0
3128 1 $tr[$tr[$t]{d0}]{u0} = $t;
3129 1 $tr[$tr[$t]{d0}]{u1} = $tn;
3130 1 $tr[$tr[$t]{d1}]{u0} = $tn;
3131 1 $tr[$tr[$t]{d1}]{u1} = -1;
3132  
3133 # new code to determine the bottom neighbours of the
3134 # newly partitioned trapezoid
3135  
3136 1 $tr[$t]{d1} = -1;
3137  
3138 1 $tnext = $tr[$t]{d0};
3139 } else { # intersecting d1
3140 2 $tr[$tr[$t]{d0}]{u0} = $t;
3141 2 $tr[$tr[$t]{d0}]{u1} = -1;
3142 2 $tr[$tr[$t]{d1}]{u0} = $t;
3143 2 $tr[$tr[$t]{d1}]{u1} = $tn;
3144  
3145 # new code to determine the bottom neighbours of the
3146 # newly partitioned trapezoid
3147  
3148 2 $tr[$tn]{d0} = $tr[$t]{d1};
3149 2 $tr[$tn]{d1} = -1;
3150  
3151 2 $tnext = $tr[$t]{d1};
3152 }
3153  
3154 8 $t = $tnext;
3155 }
3156  
3157 25 $tr[$t_sav]{rseg} = $tr[$tn_sav]{lseg} = $segnum;
3158 } # end-while
3159  
3160 # Now combine those trapezoids which share common segments. We can
3161 # use the pointers to the parent to connect these together. This
3162 # works only because all these new trapezoids have been formed
3163 # due to splitting by the segment, and hence have only one parent
3164  
3165 14 $tfirstl = $tfirst;
3166 14 $tlastl = $tlast;
3167 14 merge_trapezoids($segnum, $tfirstl, $tlastl, $S_LEFT);
3168 14 merge_trapezoids($segnum, $tfirstr, $tlastr, $S_RIGHT);
3169  
3170 14 $seg[$segnum]{is_inserted} = $TRUE;
3171 }
3172  
3173 # Returns true if the corresponding endpoint of the given segment is
3174 # already inserted into the segment tree. Use the simple test of
3175 # whether the segment which shares this endpoint is already inserted
3176  
3177 28 ▶sub _inserted {
Subroutine _inserted: called CC=2 SCAR=6.9
3178 28 my ($segnum, $whichpt) = @_;
3179 28 ▶ if ($whichpt == $FIRSTPT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whichpt == $FIRSTPT) { } 14 14 28
3180 14 return $seg[$seg[$segnum]{prev}]{is_inserted};
3181 } else {
3182 14 return $seg[$seg[$segnum]{next}]{is_inserted};
3183 }
3184 }
3185  
3186 # This is query routine which determines which trapezoid does the
3187 # point v lie in. The return value is the trapezoid number.
3188 #
3189  
3190 170 ▶sub _locate_endpoint {
Subroutine _locate_endpoint: called CC=13 SCAR=25.7
3191 170 my ($v_ref, $vo_ref, $r) = @_;
3192 170 my %v = %{$v_ref};
3193 170 my %vo = %{$vo_ref};
3194 170 my %rptr = %{$qs[$r]};
3195  
3196 SWITCH: {
3197 170 ▶ ($rptr{nodetype} == $T_SINK) && do {
Branch: 1 branch
BranchTrueFalseTotal
$rptr{"nodetype"} == $T_SINK and do { return $rptr{"trnum"} } 56 114 170
3198 56 return $rptr{trnum};
3199 };
3200 114 ▶ ($rptr{nodetype} == $T_Y) && do {
Branch: 1 branch
BranchTrueFalseTotal
$rptr{"nodetype"} == $T_Y and do { if (_greater_than(\%v, $rptr{"yval"})) { return _locate_endpoint(\%v, \%vo, $rptr{"right"}); } elsif (_equal_to(\%v, $rptr{"yval"})) { if (_greater_than(\%vo, $rptr{"yval"})) { return _locate_endpoint(\%v, \%vo, $rptr{"right"}); } else { return _locate_endpoint(\%v, \%vo, $rptr{"left"}); } ; } else { return _locate_endpoint(\%v, \%vo, $rptr{"left"}); } } 86 28 114
3201 86 ▶ if (_greater_than(\%v, $rptr{yval})) { # above
Branch: 2 branches
BranchTrueFalseTotal
if (_greater_than(\%v, $rptr{'yval'})) { } 29 57 86
elsif (_equal_to(\%v, $rptr{'yval'})) { } 16 41 57
3202 29 return _locate_endpoint(\%v, \%vo, $rptr{right});
3203 } elsif (_equal_to(\%v, $rptr{yval})) { # the point is already
3204 # inserted.
3205 16 ▶ if (_greater_than(\%vo, $rptr{yval})) { # above
Branch: 1 branch
BranchTrueFalseTotal
if (_greater_than(\%vo, $rptr{'yval'})) { } 8 8 16
3206 8 return _locate_endpoint(\%v, \%vo, $rptr{right});
3207 } else {
3208 8 return _locate_endpoint(\%v, \%vo, $rptr{left}); # below
3209 }
3210 } else {
3211 41 return _locate_endpoint(\%v, \%vo, $rptr{left}); # below
3212 }
3213 };
3214 28 ▶ ($rptr{nodetype} == $T_X) && do {
Branch: 1 branch
BranchTrueFalseTotal
$rptr{"nodetype"} == $T_X and do { if (_equal_to(\%v, $seg[$rptr{"segnum"}]{"v0"}) or _equal_to(\%v, $seg[$rptr{"segnum"}]{"v1"})) { if (_fp_equal($v{"y"}, $vo{"y"}, $Math::Geometry::Planar::precision)) { if ($vo{"x"} < $v{"x"}) { return _locate_endpoint(\%v, \%vo, $rptr{"left"}); } else { return _locate_endpoint(\%v, \%vo, $rptr{"right"}); } ; } elsif (_is_left_of($rptr{"segnum"}, \%vo)) { return _locate_endpoint(\%v, \%vo, $rptr{"left"}); } else { return _locate_endpoint(\%v, \%vo, $rptr{"right"}); } ; } elsif (_is_left_of($rptr{"segnum"}, \%v)) { return _locate_endpoint(\%v, \%vo, $rptr{"left"}); } else { return _locate_endpoint(\%v, \%vo, $rptr{"right"}); } } 28 0 28
3215 28 ▶ if (_equal_to(\%v, $seg[$rptr{segnum}]{v0}) ||
Branch: 2 branches
BranchTrueFalseTotal
if (_equal_to(\%v, $seg[$rptr{'segnum'}]{'v0'}) or _equal_to(\%v, $seg[$rptr{'segnum'}]{'v1'})) { } 6 22 28
elsif (_is_left_of($rptr{'segnum'}, \%v)) { } 10 12 22
Condition100%3/3
_equal_to(\%v, $seg[$rptr{"segnum"}]{"v0"}) or _equal_to(\%v, $seg[$rptr{"segnum"}]{"v1"})
l!l&&r!l&&!r
4222
MC/DC100%2/2
_equal_to(\%v, $seg[$rptr{"segnum"}]{"v0"}) or _equal_to(\%v, $seg[$rptr{"segnum"}]{"v1"})
_equal_to(\%v, $seg[$rptr{"segnum"}]{"v0"}) _equal_to(\%v, $seg[$rptr{"segnum"}]{"v1"})
Truth table100%3/3
A or B
A= _equal_to(\%v, $seg[$rptr{"segnum"}]{"v0"})B= _equal_to(\%v, $seg[$rptr{"segnum"}]{"v1"})
ABresult
1X1
011
000
3216 _equal_to(\%v, $seg[$rptr{segnum}]{v1})) {
3217 6 ▶ if (_fp_equal($v{y}, $vo{y}, $precision)) { # horizontal segment
Branch: 2 branches
BranchTrueFalseTotal
if (_fp_equal($v{'y'}, $vo{'y'}, $Math::Geometry::Planar::precision)) { } 2 4 6
elsif (_is_left_of($rptr{'segnum'}, \%vo)) { } 2 2 4
3218 2 ▶ if ($vo{x} < $v{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vo{'x'} < $v{'x'}) { } 1 1 2
3219 1 return _locate_endpoint(\%v, \%vo, $rptr{left}); # left
3220 } else {
3221 1 return _locate_endpoint(\%v, \%vo, $rptr{right}); # right
3222 }
3223 } elsif (_is_left_of($rptr{segnum}, \%vo)) {
3224 2 return _locate_endpoint(\%v, \%vo, $rptr{left}); # left
3225 } else {
3226 2 return _locate_endpoint(\%v, \%vo, $rptr{right}); # right
3227 }
3228 } elsif (_is_left_of($rptr{segnum}, \%v)) {
3229 10 return _locate_endpoint(\%v, \%vo, $rptr{left}); # left
3230 } else {
3231 12 return _locate_endpoint(\%v, \%vo, $rptr{right}); # right
3232 }
3233 };
3234 # default
3235 0 croak("Haggu !!!!!");
3236 }
3237 }
3238  
3239 # Thread in the segment into the existing trapezoidation. The
3240 # limiting trapezoids are given by tfirst and tlast (which are the
3241 # trapezoids containing the two endpoints of the segment. Merges all
3242 # possible trapezoids which flank this segment and have been recently
3243 # divided because of its insertion
3244 #
3245  
3246 28 ▶sub merge_trapezoids {
Subroutine merge_trapezoids: called CC=20 SCAR=31.6
Pod undocumented
3247 28 my ($segnum, $tfirst, $tlast, $side) = @_;
3248 28 my ($t, $tnext, $cond);
3249 28 my $ptnext;
3250  
3251 # First merge polys on the LHS
3252 28 $t = $tfirst;
3253 # while (($t > 0) && _greater_than_equal_to($tr[$t]{lo}, $tr[$tlast]{lo})) {
3254 28 ▶ while ($t > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t > 0) 50 28 78
3255 50 ▶ last if (! _greater_than_equal_to($tr[$t]{lo}, $tr[$tlast]{lo}));
Branch: 1 branch
BranchTrueFalseTotal
unless _greater_than_equal_to($tr[$t]{"lo"}, $tr[$tlast]{"lo"}) 0 50 50
3256 50 ▶ if ($side == $S_LEFT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($side == $S_LEFT) { } 25 25 50
3257 $cond = (((($tnext = $tr[$t]{d0}) > 0) && ($tr[$tnext]{rseg} == $segnum)) ||
3258 25 ▶ ((($tnext = $tr[$t]{d1}) > 0) && ($tr[$tnext]{rseg} == $segnum)));
Condition88%8/9
($tnext = $tr[$t]{'d0'}) > 0 && $tr[$tnext]{'rseg'} == $segnum || ($tnext = $tr[$t]{'d1'}) > 0 && $tr[$tnext]{'rseg'} == $segnum
l!l&&r!l&&!r
9214
($tnext = $tr[$t]{"d0"}) > 0 and $tr[$tnext]{"rseg"} == $segnum
!ll&&!rl&&r
2149
($tnext = $tr[$t]{"d1"}) > 0 and $tr[$tnext]{"rseg"} == $segnum
!ll&&!rl&&r
1402
MC/DC75%3/4
($tnext = $tr[$t]{'d0'}) > 0 && $tr[$tnext]{'rseg'} == $segnum || ($tnext = $tr[$t]{'d1'}) > 0 && $tr[$tnext]{'rseg'} == $segnum
($tnext = $tr[$t]{"d0"}) > 0 $tr[$tnext]{"rseg"} == $segnum ($tnext = $tr[$t]{"d1"}) > 0 $tr[$tnext]{"rseg"} == $segnum
Truth table57%4/7
A and B || C and D
A= ($tnext = $tr[$t]{"d0"}) > 0B= $tr[$tnext]{"rseg"} == $segnumC= ($tnext = $tr[$t]{"d1"}) > 0D= $tr[$tnext]{"rseg"} == $segnum
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
3259 } else {
3260 $cond = (((($tnext = $tr[$t]{d0}) > 0) && ($tr[$tnext]{lseg} == $segnum)) ||
3261 25 ▶ ((($tnext = $tr[$t]{d1}) > 0) && ($tr[$tnext]{lseg} == $segnum)));
Condition66%6/9
($tnext = $tr[$t]{'d0'}) > 0 && $tr[$tnext]{'lseg'} == $segnum || ($tnext = $tr[$t]{'d1'}) > 0 && $tr[$tnext]{'lseg'} == $segnum
l!l&&r!l&&!r
11014
($tnext = $tr[$t]{"d0"}) > 0 and $tr[$tnext]{"lseg"} == $segnum
!ll&&!rl&&r
11311
($tnext = $tr[$t]{"d1"}) > 0 and $tr[$tnext]{"lseg"} == $segnum
!ll&&!rl&&r
1400
MC/DC50%2/4
($tnext = $tr[$t]{'d0'}) > 0 && $tr[$tnext]{'lseg'} == $segnum || ($tnext = $tr[$t]{'d1'}) > 0 && $tr[$tnext]{'lseg'} == $segnum
($tnext = $tr[$t]{"d0"}) > 0 $tr[$tnext]{"lseg"} == $segnum ($tnext = $tr[$t]{"d1"}) > 0 $tr[$tnext]{"lseg"} == $segnum
Truth table42%3/7
A and B || C and D
A= ($tnext = $tr[$t]{"d0"}) > 0B= $tr[$tnext]{"lseg"} == $segnumC= ($tnext = $tr[$t]{"d1"}) > 0D= $tr[$tnext]{"lseg"} == $segnum
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
3262 }
3263 50 ▶ if ($cond) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond) { } 22 28 50
3264 22 ▶ if (($tr[$t]{lseg} == $tr[$tnext]{lseg}) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tr[$t]{'lseg'} == $tr[$tnext]{'lseg'} and $tr[$t]{'rseg'} == $tr[$tnext]{'rseg'}) { } 11 11 22
Condition100%3/3
$tr[$t]{"lseg"} == $tr[$tnext]{"lseg"} and $tr[$t]{"rseg"} == $tr[$tnext]{"rseg"}
!ll&&!rl&&r
6511
MC/DC100%2/2
$tr[$t]{"lseg"} == $tr[$tnext]{"lseg"} and $tr[$t]{"rseg"} == $tr[$tnext]{"rseg"}
$tr[$t]{"lseg"} == $tr[$tnext]{"lseg"} $tr[$t]{"rseg"} == $tr[$tnext]{"rseg"}
Truth table100%3/3
A and B
A= $tr[$t]{"lseg"} == $tr[$tnext]{"lseg"}B= $tr[$t]{"rseg"} == $tr[$tnext]{"rseg"}
ABresult
0X0
100
111
3265 ($tr[$t]{rseg} == $tr[$tnext]{rseg})) { # good neighbours
3266 # merge them
3267 # Use the upper node as the new node i.e. t
3268 11 $ptnext = $qs[$tr[$tnext]{sink}]{parent};
3269 11 ▶ if ($qs[$ptnext]{left} == $tr[$tnext]{sink}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qs[$ptnext]{'left'} == $tr[$tnext]{'sink'}) { } 5 6 11
3270 5 $qs[$ptnext]{left} = $tr[$t]{sink};
3271 } else {
3272 6 $qs[$ptnext]{right} = $tr[$t]{sink}; # redirect parent
3273 }
3274 # Change the upper neighbours of the lower trapezoids
3275 11 ▶ if (($tr[$t]{d0} = $tr[$tnext]{d0}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tr[$t]{"d0"} = $tr[$tnext]{"d0"}) > 0) 11 0 11
3276 11 ▶ if ($tr[$tr[$t]{d0}]{u0} == $tnext) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tr[$tr[$t]{'d0'}]{'u0'} == $tnext) { } 7 4 11
elsif ($tr[$tr[$t]{'d0'}]{'u1'} == $tnext) { } 4 0 4
3277 7 $tr[$tr[$t]{d0}]{u0} = $t;
3278 } elsif ($tr[$tr[$t]{d0}]{u1} == $tnext) {
3279 4 $tr[$tr[$t]{d0}]{u1} = $t;
3280 }
3281 }
3282 11 ▶ if (($tr[$t]{d1} = $tr[$tnext]{d1}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($tr[$t]{"d1"} = $tr[$tnext]{"d1"}) > 0) 0 11 11
3283 0 ▶ if ($tr[$tr[$t]{d1}]{u0} == $tnext) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tr[$tr[$t]{'d1'}]{'u0'} == $tnext) { } 0 0 0
elsif ($tr[$tr[$t]{'d1'}]{'u1'} == $tnext) { } 0 0 0
3284 0 $tr[$tr[$t]{d1}]{u0} = $t;
3285 } elsif ($tr[$tr[$t]{d1}]{u1} == $tnext) {
3286 0 $tr[$tr[$t]{d1}]{u1} = $t;
3287 }
3288 }
3289 11 $tr[$t]{lo} = {x => $tr[$tnext]{lo}{x} , y=> $tr[$tnext]{lo}{y}};
3290 11 $tr[$tnext]{state} = 2; # invalidate the lower
3291 # trapezium
3292 } else { #* not good neighbours
3293 11 $t = $tnext;
3294 }
3295 } else { #* do not satisfy the outer if
3296 28 $t = $tnext;
3297 }
3298 } # end-while
3299 }
3300  
3301 # Retun TRUE if the vertex v is to the left of line segment no.
3302 # segnum. Takes care of the degenerate cases when both the vertices
3303 # have the same y--cood, etc.
3304 #
3305  
3306 32 ▶sub _is_left_of {
Subroutine _is_left_of: called CC=11 SCAR=24.5
3307 32 my ($segnum, $v_ref) = @_;
3308 32 my %s = %{$seg[$segnum]};
3309 32 my $area;
3310 32 my %v = %{$v_ref};
3311  
3312 32 ▶ if (_greater_than($s{v1}, $s{v0})) { # seg. going upwards
Branch: 1 branch
BranchTrueFalseTotal
if (_greater_than($s{'v1'}, $s{'v0'})) { } 16 16 32
3313 16 ▶ if (_fp_equal($s{v1}{y}, $v{y}, $precision)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_fp_equal($s{'v1'}{'y'}, $v{'y'}, $Math::Geometry::Planar::precision)) { } 10 6 16
elsif (_fp_equal($s{'v0'}{'y'}, $v{'y'}, $Math::Geometry::Planar::precision)) { } 1 5 6
3314 10 ▶ if ($v{x} < $s{v1}{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v{'x'} < $s{'v1'}{'x'}) { } 8 2 10
3315 8 $area = 1;
3316 } else {
3317 2 $area = -1;
3318 }
3319 } elsif (_fp_equal($s{v0}{y}, $v{y}, $precision)) {
3320 1 ▶ if ($v{x} < $s{v0}{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v{'x'} < $s{'v0'}{'x'}) { } 0 1 1
3321 0 $area = 1;
3322 } else{
3323 1 $area = -1;
3324 }
3325 } else {
3326 5 $area = _Cross($s{v0}, $s{v1}, \%v);
3327 }
3328 } else { # v0 > v1
3329 16 ▶ if (_fp_equal($s{v1}{y}, $v{y}, $precision)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_fp_equal($s{'v1'}{'y'}, $v{'y'}, $Math::Geometry::Planar::precision)) { } 10 6 16
elsif (_fp_equal($s{'v0'}{'y'}, $v{'y'}, $Math::Geometry::Planar::precision)) { } 0 6 6
3330 10 ▶ if ($v{x} < $s{v1}{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v{'x'} < $s{'v1'}{'x'}) { } 2 8 10
3331 2 $area = 1;
3332 } else {
3333 8 $area = -1;
3334 }
3335 } elsif (_fp_equal($s{v0}{y}, $v{y}, $precision)) {
3336 0 ▶ if ($v{x} < $s{v0}{x}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v{'x'} < $s{'v0'}{'x'}) { } 0 0 0
3337 0 $area = 1;
3338 } else {
3339 0 $area = -1;
3340 }
3341 } else {
3342 6 $area = _Cross($s{v1}, $s{v0}, \%v);
3343 }
3344 }
3345 32 ▶ if ($area > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($area > 0) { } 15 17 32
3346 15 return $TRUE;
3347 } else {
3348 17 return $FALSE;
3349 };
3350 }
3351  
3352 19 ▶sub _Cross {
Subroutine _Cross: called CC=1 SCAR=0.0
3353 19 my ($v0_ref, $v1_ref, $v2_ref) = @_;
3354 19 my %v0 = %{$v0_ref};
3355 19 my %v1 = %{$v1_ref};
3356 19 my %v2 = %{$v2_ref};
3357 return ( ($v1{x} - $v0{x}) * ($v2{y} - $v0{y}) -
3358 19 ($v1{y} - $v0{y}) * ($v2{x} - $v0{x}) );
3359 }
3360  
3361 # Get log*n for given n
3362 8 ▶sub _math_logstar_n {
Subroutine _math_logstar_n: called CC=2 SCAR=6.9
3363 8 my ($n) = @_;
3364 8 my $i = 0;
3365 8 ▶ for ($i = 0 ; $n >= 1 ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1) 24 8 32
3366 24 $n = log($n)/log(2); # log2
3367 }
3368 8 return ($i - 1);
3369 }
3370  
3371 20 ▶sub _math_N {
Subroutine _math_N: called CC=2 SCAR=6.9
3372 20 my ($n,$h) = @_;
3373 20 my $v = $n;
3374 20 ▶ for (my $i = 0 ; $i < $h; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $h) 28 20 48
3375 28 $v = log($v)/log(2); # log2
3376 }
3377 20 return (ceil($n/$v));
3378 }
3379  
3380 # This function returns TRUE or FALSE depending upon whether the
3381 # vertex is inside the polygon or not. The polygon must already have
3382 # been triangulated before this routine is called.
3383 # This routine will always detect all the points belonging to the
3384 # set (polygon-area - polygon-boundary). The return value for points
3385 # on the boundary is not consistent!!!
3386 #
3387  
3388 0 ▶sub is_point_inside_polygon {
Subroutine is_point_inside_polygon: not called CC=4 SCAR=30.0
Pod undocumented
3389 0 my @vertex = @_;
3390 0 my %v;
3391 0 my ($trnum, $rseg);
3392  
3393 0 %v = {x => $vertex[0] , y => $vertex[1]};
3394  
3395 0 $trnum = _locate_endpoint(&v, &v, 1);
3396 0 my %t = %{$tr[$trnum]};
3397  
3398 0 ▶ if ($t{state} == $ST_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"state"} == $ST_INVALID) 0 0 0
3399 0 return $FALSE;
3400 }
3401  
3402 0 ▶ if (($t{lseg} <= 0) || ($t{rseg} <= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"lseg"} <= 0 or $t{"rseg"} <= 0) 0 0 0
Condition0%0/3
$t{"lseg"} <= 0 or $t{"rseg"} <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$t{"lseg"} <= 0 or $t{"rseg"} <= 0
$t{"lseg"} <= 0 $t{"rseg"} <= 0
Truth table0%0/3
A or B
A= $t{"lseg"} <= 0B= $t{"rseg"} <= 0
ABresult
1X1
011
000
3403 0 return $FALSE;
3404 }
3405 0 $rseg = $t{rseg};
3406 0 return _greater_than_equal_to($seg[$rseg]{v1}, $seg[$rseg]{v0});
3407 }
3408  
3409 18 ▶sub _Cross_Sine {
Subroutine _Cross_Sine: called CC=1 SCAR=0.0
3410 18 my ($v0_ref, $v1_ref) = @_;
3411 18 my %v0 = %{$v0_ref};
3412 18 my %v1 = %{$v1_ref};
3413 18 return ($v0{x} * $v1{y} - $v1{x} * $v0{y});
3414 }
3415  
3416 36 ▶sub _Length {
Subroutine _Length: called CC=1 SCAR=0.0
3417 36 my ($v0_ref) = @_;
3418 36 my %v0 = %{$v0_ref};
3419 36 return (sqrt($v0{x} * $v0{x} + $v0{y} * $v0{y}));
3420 }
3421  
3422 18 ▶sub _Dot {
Subroutine _Dot: called CC=1 SCAR=0.0
3423 18 my ($v0_ref, $v1_ref) = @_;
3424 18 my %v0 = %{$v0_ref};
3425 18 my %v1 = %{$v1_ref};
3426 return ($v0{x} * $v1{x} + $v0{y} * $v1{y})
3427 18 }
3428  
3429 # Function returns TRUE if the trapezoid lies inside the polygon
3430 26 ▶sub inside_polygon {
Subroutine inside_polygon: called CC=8 SCAR=20.8
Pod undocumented
3431 26 my ($t_ref) = @_;
3432 26 my %t = %{$t_ref};
3433 26 my $rseg = $t{rseg};
3434 26 ▶ if ($t{state} == $ST_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"state"} == $ST_INVALID) 7 19 26
3435 7 return 0;
3436 }
3437 19 ▶ if (($t{lseg} <= 0) || ($t{rseg} <= 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"lseg"} <= 0 or $t{"rseg"} <= 0) 12 7 19
Condition100%3/3
$t{"lseg"} <= 0 or $t{"rseg"} <= 0
l!l&&r!l&&!r
1027
MC/DC100%2/2
$t{"lseg"} <= 0 or $t{"rseg"} <= 0
$t{"lseg"} <= 0 $t{"rseg"} <= 0
Truth table100%3/3
A or B
A= $t{"lseg"} <= 0B= $t{"rseg"} <= 0
ABresult
1X1
011
000
3438 12 return 0;
3439 }
3440 7 ▶ if ((($t{u0} <= 0) && ($t{u1} <= 0)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"u0"} <= 0 and $t{"u1"} <= 0 or $t{"d0"} <= 0 and $t{"d1"} <= 0) 2 5 7
Condition77%7/9
$t{"u0"} <= 0 and $t{"u1"} <= 0 or $t{"d0"} <= 0 and $t{"d1"} <= 0
l!l&&r!l&&!r
115
$t{"u0"} <= 0 and $t{"u1"} <= 0
!ll&&!rl&&r
601
$t{"d0"} <= 0 and $t{"d1"} <= 0
!ll&&!rl&&r
501
MC/DC50%2/4
$t{"u0"} <= 0 and $t{"u1"} <= 0 or $t{"d0"} <= 0 and $t{"d1"} <= 0
$t{"u0"} <= 0 $t{"u1"} <= 0 $t{"d0"} <= 0 $t{"d1"} <= 0
Truth table42%3/7
A and B or C and D
A= $t{"u0"} <= 0B= $t{"u1"} <= 0C= $t{"d0"} <= 0D= $t{"d1"} <= 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
3441 (($t{d0} <= 0) && ($t{d1} <= 0))) { # triangle
3442 2 return (_greater_than($seg[$rseg]{v1}, $seg[$rseg]{v0}));
3443 }
3444 5 return 0;
3445 }
3446  
3447 # return a new mon structure from the table
3448 9 ▶sub _newmon {
Subroutine _newmon: called CC=1 SCAR=0.0
3449 9 return ++$mon_idx;
3450 }
3451  
3452 # return a new chain element from the table
3453 14 ▶sub _new_chain_element {
Subroutine _new_chain_element: called CC=1 SCAR=0.0
3454 14 return ++$chain_idx;
3455 }
3456  
3457 18 ▶sub _get_angle {
Subroutine _get_angle: called CC=2 SCAR=6.9
3458 18 my ($vp0_ref, $vpnext_ref, $vp1_ref) = @_;
3459 18 my %vp0 = %{$vp0_ref};
3460 18 my %vpnext = %{$vpnext_ref};
3461 18 my %vp1 = %{$vp1_ref};
3462  
3463 18 my ($v0, $v1);
3464  
3465 18 $v0 = {x => $vpnext{x} - $vp0{x} , y => $vpnext{y} - $vp0{y}};
3466 18 $v1 = {x => $vp1{x} - $vp0{x} , y => $vp1{y} - $vp0{y}};
3467  
3468 18 ▶ if (_Cross_Sine($v0, $v1) >= 0) { # sine is positive
Branch: 1 branch
BranchTrueFalseTotal
if (_Cross_Sine($v0, $v1) >= 0) { } 15 3 18
3469 15 return _Dot($v0, $v1)/_Length($v0)/_Length($v1);
3470 } else {
3471 3 return (-1 * _Dot($v0, $v1)/_Length($v0)/_Length($v1) - 2);
3472 }
3473 }
3474  
3475 # (v0, v1) is the new diagonal to be added to the polygon. Find which
3476 # chain to use and return the positions of v0 and v1 in p and q
3477 7 ▶sub _get_vertex_positions {
Subroutine _get_vertex_positions: called CC=9 SCAR=22.3
3478 7 my ($v0, $v1) = @_;
3479  
3480 7 my (%vp0, %vp1);
3481 7 my ($angle, $temp);
3482 7 my ($tp, $tq);
3483  
3484 7 %vp0 = %{$vert[$v0]};
3485 7 %vp1 = %{$vert[$v1]};
3486  
3487 # p is identified as follows. Scan from (v0, v1) rightwards till
3488 # you hit the first segment starting from v0. That chain is the
3489 # chain of our interest
3490  
3491 7 $angle = -4.0;
3492 7 ▶ for (my $i = 0; $i < 4; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 28 7 35
3493 28 ▶ next if (! $vp0{vnext}[$i]); # prevents 'uninitialized' warnings
Branch: 1 branch
BranchTrueFalseTotal
unless $vp0{"vnext"}[$i] 19 9 28
3494 9 ▶ if ($vp0{vnext}[$i] <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vp0{"vnext"}[$i] <= 0) 0 9 9
3495 0 next;
3496 }
3497 9 ▶ if (($temp = _get_angle($vp0{pt}, $vert[$vp0{vnext}[$i]]{pt}, $vp1{pt})) > $angle) {
Branch: 1 branch
BranchTrueFalseTotal
if (($temp = _get_angle($vp0{"pt"}, $vert[$vp0{"vnext"}[$i]]{"pt"}, $vp1{"pt"})) > $angle) 9 0 9
3498 9 $angle = $temp;
3499 9 $tp = $i;
3500 }
3501 }
3502  
3503 # $ip_ref = \$tp;
3504  
3505 # Do similar actions for q
3506  
3507 7 $angle = -4.0;
3508 7 ▶ for (my $i = 0; $i < 4; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 28 7 35
3509 28 ▶ next if (! $vp1{vnext}[$i]); # prevents 'uninitialized' warnings
Branch: 1 branch
BranchTrueFalseTotal
unless $vp1{"vnext"}[$i] 19 9 28
3510 9 ▶ if ($vp1{vnext}[$i] <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vp1{"vnext"}[$i] <= 0) 0 9 9
3511 0 next;
3512 }
3513 9 ▶ if (($temp = _get_angle($vp1{pt}, $vert[$vp1{vnext}[$i]]{pt}, $vp0{pt})) > $angle) {
Branch: 1 branch
BranchTrueFalseTotal
if (($temp = _get_angle($vp1{"pt"}, $vert[$vp1{"vnext"}[$i]]{"pt"}, $vp0{"pt"})) > $angle) 9 0 9
3514 9 $angle = $temp;
3515 9 $tq = $i;
3516 }
3517 }
3518  
3519 # $iq_ref = \$tq;
3520  
3521 7 return ($tp,$tq);
3522  
3523 }
3524  
3525 # v0 and v1 are specified in anti-clockwise order with respect to
3526 # the current monotone polygon mcur. Split the current polygon into
3527 # two polygons using the diagonal (v0, v1)
3528 #
3529 7 ▶sub _make_new_monotone_poly {
Subroutine _make_new_monotone_poly: called CC=1 SCAR=0.0
3530 7 my ($mcur, $v0, $v1) = @_;
3531  
3532 7 my ($p, $q, $ip, $iq);
3533 7 my $mnew = _newmon;
3534 7 my ($i, $j, $nf0, $nf1);
3535  
3536 7 my %vp0 = %{$vert[$v0]};
3537 7 my %vp1 = %{$vert[$v1]};
3538  
3539 7 ($ip,$iq) = _get_vertex_positions($v0, $v1);
3540  
3541 7 $p = $vp0{vpos}[$ip];
3542 7 $q = $vp1{vpos}[$iq];
3543  
3544 # At this stage, we have got the positions of v0 and v1 in the
3545 # desired chain. Now modify the linked lists
3546  
3547 7 $i = _new_chain_element; # for the new list
3548 7 $j = _new_chain_element;
3549  
3550 7 $mchain[$i]{vnum} = $v0;
3551 7 $mchain[$j]{vnum} = $v1;
3552  
3553 7 $mchain[$i]{next} = $mchain[$p]{next};
3554 7 $mchain[$mchain[$p]{next}]{prev} = $i;
3555 7 $mchain[$i]{prev} = $j;
3556 7 $mchain[$j]{next} = $i;
3557 7 $mchain[$j]{prev} = $mchain[$q]{prev};
3558 7 $mchain[$mchain[$q]{prev}]{next} = $j;
3559  
3560 7 $mchain[$p]{next} = $q;
3561 7 $mchain[$q]{prev} = $p;
3562  
3563 7 $nf0 = $vp0{nextfree};
3564 7 $nf1 = $vp1{nextfree};
3565  
3566 7 $vert[$v0]{vnext}[$ip] = $v1;
3567  
3568 7 $vert[$v0]{vpos}[$nf0] = $i;
3569 7 $vert[$v0]{vnext}[$nf0] = $mchain[$mchain[$i]{next}]{vnum};
3570 7 $vert[$v1]{vpos}[$nf1] = $j;
3571 7 $vert[$v1]{vnext}[$nf1] = $v0;
3572  
3573 7 $vert[$v0]{nextfree}++;
3574 7 $vert[$v1]{nextfree}++;
3575  
3576 7 $mon[$mcur] = $p;
3577 7 $mon[$mnew] = $i;
3578 7 return $mnew;
3579 }
3580  
3581 # Main routine to get monotone polygons from the trapezoidation of
3582 # the polygon.
3583 #
3584  
3585 2 ▶sub _monotonate_trapezoids {
Subroutine _monotonate_trapezoids: called CC=6 SCAR=17.9
3586 2 my ($n) = @_;
3587  
3588 2 my $tr_start;
3589  
3590 # First locate a trapezoid which lies inside the polygon
3591 # and which is triangular
3592 my $i;
3593 2 ▶ for ($i = 1; $i < $#tr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#tr) 26 0 26
3594 26 ▶ if (inside_polygon($tr[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (inside_polygon($tr[$i])) 2 24 26
3595 2 last;
3596 }
3597 }
3598 2 $tr_start = $i;
3599  
3600 # Initialise the mon data-structure and start spanning all the
3601 # trapezoids within the polygon
3602  
3603 2 ▶ for (my $i = 1; $i <= $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 16 2 18
3604 16 $mchain[$i]{prev} = $seg[$i]{prev};
3605 16 $mchain[$i]{next} = $seg[$i]{next};
3606 16 $mchain[$i]{vnum} = $i;
3607 16 $vert[$i]{pt} = {x => $seg[$i]{v0}{x} , y => $seg[$i]{v0}{y}};
3608 16 $vert[$i]{vnext}[0] = $seg[$i]{next}; # next vertex
3609 16 $vert[$i]{vpos}[0] = $i; # locn. of next vertex
3610 16 $vert[$i]{nextfree} = 1;
3611 }
3612  
3613 2 $chain_idx = $n;
3614 2 $mon_idx = 0;
3615 2 $mon[0] = 1; # position of any vertex in the first chain
3616  
3617 # traverse the polygon
3618 2 ▶ if ($tr[$tr_start]{u0} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tr[$tr_start]{'u0'} > 0) { } 1 1 2
elsif ($tr[$tr_start]{'d0'} > 0) { } 1 0 1
3619 1 _traverse_polygon(0, $tr_start, $tr[$tr_start]{u0}, $TR_FROM_UP);
3620 } elsif ($tr[$tr_start]{d0} > 0) {
3621 1 _traverse_polygon(0, $tr_start, $tr[$tr_start]{d0}, $TR_FROM_DN);
3622 }
3623  
3624 # return the number of polygons created
3625 2 return _newmon;
3626 }
3627  
3628 # recursively visit all the trapezoids
3629 62 ▶sub _traverse_polygon {
Subroutine _traverse_polygon: called CC=42 SCAR=57.1
3630 62 my ($mcur, $trnum, $from, $dir) = @_;
3631  
3632 62 ▶ if (!$trnum) { # patch dvdp
Branch: 1 branch
BranchTrueFalseTotal
unless ($trnum) 7 55 62
3633 7 return 0;
3634 }
3635 55 my %t = %{$tr[$trnum]};
3636 55 my ($howsplit, $mnew);
3637 55 my ($v0, $v1, $v0next, $v1next);
3638 55 my ($retval, $tmp);
3639 55 my $do_switch = $FALSE;
3640  
3641 55 ▶ if (($trnum <= 0) || $visited[$trnum]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trnum <= 0 or $visited[$trnum]) 40 15 55
Condition100%3/3
$trnum <= 0 or $visited[$trnum]
l!l&&r!l&&!r
251515
MC/DC100%2/2
$trnum <= 0 or $visited[$trnum]
$trnum <= 0 $visited[$trnum]
Truth table100%3/3
A or B
A= $trnum <= 0B= $visited[$trnum]
ABresult
1X1
011
000
3642 40 return 0;
3643 }
3644  
3645 15 $visited[$trnum] = $TRUE;
3646  
3647 # We have much more information available here.
3648 # rseg: goes upwards
3649 # lseg: goes downwards
3650  
3651 # Initially assume that dir = TR_FROM_DN (from the left)
3652 # Switch v0 and v1 if necessary afterwards
3653  
3654 # special cases for triangles with cusps at the opposite ends.
3655 # take care of this first
3656 15 ▶ if (($t{u0} <= 0) && ($t{u1} <= 0)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($t{'u0'} <= 0 and $t{'u1'} <= 0) { } 2 13 15
elsif ($t{'d0'} <= 0 and $t{'d1'} <= 0) { } 2 11 13
elsif ($t{'u0'} > 0 and $t{'u1'} > 0) { } 1 10 11
elsif ($t{'u0'} > 0 or $t{'u1'} > 0) { } 10 0 10
Condition66%2/3
$t{"u0"} <= 0 and $t{"u1"} <= 0
!ll&&!rl&&r
1302
MC/DC50%1/2
$t{"u0"} <= 0 and $t{"u1"} <= 0
$t{"u0"} <= 0 $t{"u1"} <= 0
Truth table66%2/3
A and B
A= $t{"u0"} <= 0B= $t{"u1"} <= 0
ABresult
0X0
100
111
3657 2 ▶ if (($t{d0} > 0) && ($t{d1} > 0)) { # downward opening triangle
Branch: 1 branch
BranchTrueFalseTotal
if ($t{'d0'} > 0 and $t{'d1'} > 0) { } 0 2 2
Condition33%1/3
$t{"d0"} > 0 and $t{"d1"} > 0
!ll&&!rl&&r
020
MC/DC0%0/2
$t{"d0"} > 0 and $t{"d1"} > 0
$t{"d0"} > 0 $t{"d1"} > 0
Truth table33%1/3
A and B
A= $t{"d0"} > 0B= $t{"d1"} > 0
ABresult
0X0
100
111
3658 0 $v0 = $tr[$t{d1}]{lseg};
3659 0 $v1 = $t{lseg};
3660 0 ▶ if ($from == $t{d1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from == $t{'d1'}) { } 0 0 0
3661 0 $do_switch = $TRUE;
3662 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3663 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3664 0 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3665 } else {
3666 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3667 0 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3668 0 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3669 }
3670 } else {
3671 2 $retval = $SP_NOSPLIT; # Just traverse all neighbours
3672 2 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3673 2 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3674 2 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3675 2 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3676 }
3677 ▶ } elsif (($t{d0} <= 0) && ($t{d1} <= 0)) {
Condition66%2/3
$t{"d0"} <= 0 and $t{"d1"} <= 0
!ll&&!rl&&r
1102
MC/DC50%1/2
$t{"d0"} <= 0 and $t{"d1"} <= 0
$t{"d0"} <= 0 $t{"d1"} <= 0
Truth table66%2/3
A and B
A= $t{"d0"} <= 0B= $t{"d1"} <= 0
ABresult
0X0
100
111
3678 2 ▶ if (($t{u0} > 0) && ($t{u1} > 0)) { # upward opening triangle
Branch: 1 branch
BranchTrueFalseTotal
if ($t{'u0'} > 0 and $t{'u1'} > 0) { } 0 2 2
Condition33%1/3
$t{"u0"} > 0 and $t{"u1"} > 0
!ll&&!rl&&r
020
MC/DC0%0/2
$t{"u0"} > 0 and $t{"u1"} > 0
$t{"u0"} > 0 $t{"u1"} > 0
Truth table33%1/3
A and B
A= $t{"u0"} > 0B= $t{"u1"} > 0
ABresult
0X0
100
111
3679 0 $v0 = $t{rseg};
3680 0 $v1 = $tr[$t{u0}]{rseg};
3681 0 ▶ if ($from == $t{u1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from == $t{'u1'}) { } 0 0 0
3682 0 $do_switch = $TRUE;
3683 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3684 0 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3685 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3686 } else {
3687 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3688 0 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3689 0 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3690 }
3691 } else {
3692 2 $retval = $SP_NOSPLIT; # Just traverse all neighbours
3693 2 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3694 2 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3695 2 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3696 2 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3697 }
3698 ▶ } elsif (($t{u0} > 0) && ($t{u1} > 0)) {
Condition66%2/3
$t{"u0"} > 0 and $t{"u1"} > 0
!ll&&!rl&&r
0101
MC/DC50%1/2
$t{"u0"} > 0 and $t{"u1"} > 0
$t{"u0"} > 0 $t{"u1"} > 0
Truth table66%2/3
A and B
A= $t{"u0"} > 0B= $t{"u1"} > 0
ABresult
0X0
100
111
3699 1 ▶ if (($t{d0} > 0) && ($t{d1} > 0)) { # downward + upward cusps
Branch: 1 branch
BranchTrueFalseTotal
if ($t{'d0'} > 0 and $t{'d1'} > 0) { } 0 1 1
Condition33%1/3
$t{"d0"} > 0 and $t{"d1"} > 0
!ll&&!rl&&r
010
MC/DC0%0/2
$t{"d0"} > 0 and $t{"d1"} > 0
$t{"d0"} > 0 $t{"d1"} > 0
Truth table33%1/3
A and B
A= $t{"d0"} > 0B= $t{"d1"} > 0
ABresult
0X0
100
111
3700 0 $v0 = $tr[$t{d1}]{lseg};
3701 0 $v1 = $tr[$t{u0}]{rseg};
3702 0 $retval = $SP_2UP_2DN;
3703 0 ▶ if ((($dir == $TR_FROM_DN) && ($t{d1} == $from)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == $TR_FROM_DN and $t{'d1'} == $from or $dir == $TR_FROM_UP and $t{'u1'} == $from) { } 0 0 0
Condition0%0/9
$dir == $TR_FROM_DN and $t{"d1"} == $from or $dir == $TR_FROM_UP and $t{"u1"} == $from
l!l&&r!l&&!r
000
$dir == $TR_FROM_DN and $t{"d1"} == $from
!ll&&!rl&&r
000
$dir == $TR_FROM_UP and $t{"u1"} == $from
!ll&&!rl&&r
000
MC/DC0%0/4
$dir == $TR_FROM_DN and $t{"d1"} == $from or $dir == $TR_FROM_UP and $t{"u1"} == $from
$dir == $TR_FROM_DN $t{"d1"} == $from $dir == $TR_FROM_UP $t{"u1"} == $from
Truth table0%0/7
A and B or C and D
A= $dir == $TR_FROM_DNB= $t{"d1"} == $fromC= $dir == $TR_FROM_UPD= $t{"u1"} == $from
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
3704 (($dir == $TR_FROM_UP) && ($t{u1} == $from))) {
3705 0 $do_switch = $TRUE;
3706 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3707 0 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3708 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3709 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3710 0 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3711 } else {
3712 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3713 0 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3714 0 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3715 0 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3716 0 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3717 }
3718 } else { #* only downward cusp
3719 1 ▶ if (_equal_to($t{lo}, $seg[$t{lseg}]{v1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_equal_to($t{'lo'}, $seg[$t{'lseg'}]{'v1'})) { } 0 1 1
3720 0 $v0 = $tr[$t{u0}]{rseg};
3721 0 $v1 = $seg[$t{lseg}]{next};
3722  
3723 0 $retval = $SP_2UP_LEFT;
3724 0 ▶ if (($dir == $TR_FROM_UP) && ($t{u0} == $from)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == $TR_FROM_UP and $t{'u0'} == $from) { } 0 0 0
Condition0%0/3
$dir == $TR_FROM_UP and $t{"u0"} == $from
!ll&&!rl&&r
000
MC/DC0%0/2
$dir == $TR_FROM_UP and $t{"u0"} == $from
$dir == $TR_FROM_UP $t{"u0"} == $from
Truth table0%0/3
A and B
A= $dir == $TR_FROM_UPB= $t{"u0"} == $from
ABresult
0X0
100
111
3725 0 $do_switch = $TRUE;
3726 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3727 0 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3728 0 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3729 0 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3730 0 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3731 } else {
3732 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3733 0 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3734 0 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3735 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3736 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3737 }
3738 } else {
3739 1 $v0 = $t{rseg};
3740 1 $v1 = $tr[$t{u0}]{rseg};
3741 1 $retval = $SP_2UP_RIGHT;
3742 1 ▶ if (($dir == $TR_FROM_UP) && ($t{u1} == $from)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == $TR_FROM_UP and $t{'u1'} == $from) { } 0 1 1
Condition33%1/3
$dir == $TR_FROM_UP and $t{"u1"} == $from
!ll&&!rl&&r
100
MC/DC0%0/2
$dir == $TR_FROM_UP and $t{"u1"} == $from
$dir == $TR_FROM_UP $t{"u1"} == $from
Truth table33%1/3
A and B
A= $dir == $TR_FROM_UPB= $t{"u1"} == $from
ABresult
0X0
100
111
3743 0 $do_switch = $TRUE;
3744 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3745 0 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3746 0 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3747 0 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3748 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3749 } else {
3750 1 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3751 1 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3752 1 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3753 1 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3754 1 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3755 }
3756 }
3757 }
3758 ▶ } elsif (($t{u0} > 0) || ($t{u1} > 0)) { # no downward cusp
Condition33%1/3
$t{"u0"} > 0 or $t{"u1"} > 0
l!l&&r!l&&!r
1000
MC/DC0%0/2
$t{"u0"} > 0 or $t{"u1"} > 0
$t{"u0"} > 0 $t{"u1"} > 0
Truth table33%1/3
A or B
A= $t{"u0"} > 0B= $t{"u1"} > 0
ABresult
1X1
011
000
3759 10 ▶ if (($t{d0} > 0) && ($t{d1} > 0)) { # only upward cusp
Branch: 1 branch
BranchTrueFalseTotal
if ($t{'d0'} > 0 and $t{'d1'} > 0) { } 1 9 10
Condition66%2/3
$t{"d0"} > 0 and $t{"d1"} > 0
!ll&&!rl&&r
091
MC/DC50%1/2
$t{"d0"} > 0 and $t{"d1"} > 0
$t{"d0"} > 0 $t{"d1"} > 0
Truth table66%2/3
A and B
A= $t{"d0"} > 0B= $t{"d1"} > 0
ABresult
0X0
100
111
3760 1 ▶ if (_equal_to($t{hi}, $seg[$t{lseg}]{v0})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_equal_to($t{'hi'}, $seg[$t{'lseg'}]{'v0'})) { } 1 0 1
3761 1 $v0 = $tr[$t{d1}]{lseg};
3762 1 $v1 = $t{lseg};
3763 1 $retval = $SP_2DN_LEFT;
3764 1 ▶ if (!(($dir == $TR_FROM_DN) && ($t{d0} == $from))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dir == $TR_FROM_DN && $t{'d0'} == $from) { } 0 1 1
Condition33%1/3
$dir == $TR_FROM_DN && $t{'d0'} == $from
!ll&&!rl&&r
001
MC/DC0%0/2
$dir == $TR_FROM_DN && $t{'d0'} == $from
$dir == $TR_FROM_DN $t{'d0'} == $from
Truth table33%1/3
A && B
A= $dir == $TR_FROM_DNB= $t{'d0'} == $from
ABresult
0X0
100
111
3765 0 $do_switch = $TRUE;
3766 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3767 0 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3768 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3769 0 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3770 0 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3771 } else {
3772 1 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3773 1 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3774 1 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3775 1 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3776 1 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3777 }
3778 } else {
3779 0 $v0 = $tr[$t{d1}]{lseg};
3780 0 $v1 = $seg[$t{rseg}]{next};
3781  
3782 0 $retval = $SP_2DN_RIGHT;
3783 0 ▶ if (($dir == $TR_FROM_DN) && ($t{d1} == $from)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == $TR_FROM_DN and $t{'d1'} == $from) { } 0 0 0
Condition0%0/3
$dir == $TR_FROM_DN and $t{"d1"} == $from
!ll&&!rl&&r
000
MC/DC0%0/2
$dir == $TR_FROM_DN and $t{"d1"} == $from
$dir == $TR_FROM_DN $t{"d1"} == $from
Truth table0%0/3
A and B
A= $dir == $TR_FROM_DNB= $t{"d1"} == $from
ABresult
0X0
100
111
3784 0 $do_switch = $TRUE;
3785 0 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3786 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3787 0 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3788 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3789 0 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3790 } else {
3791 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3792 0 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3793 0 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3794 0 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3795 0 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3796 }
3797 }
3798 } else { # no cusp
3799 9 ▶ if (_equal_to($t{hi}, $seg[$t{lseg}]{v0}) &&
Branch: 2 branches
BranchTrueFalseTotal
if (_equal_to($t{'hi'}, $seg[$t{'lseg'}]{'v0'}) and _equal_to($t{'lo'}, $seg[$t{'rseg'}]{'v0'})) { } 2 7 9
elsif (_equal_to($t{'hi'}, $seg[$t{'rseg'}]{'v1'}) and _equal_to($t{'lo'}, $seg[$t{'lseg'}]{'v1'})) { } 3 4 7
Condition100%3/3
_equal_to($t{"hi"}, $seg[$t{"lseg"}]{"v0"}) and _equal_to($t{"lo"}, $seg[$t{"rseg"}]{"v0"})
!ll&&!rl&&r
522
MC/DC100%2/2
_equal_to($t{"hi"}, $seg[$t{"lseg"}]{"v0"}) and _equal_to($t{"lo"}, $seg[$t{"rseg"}]{"v0"})
_equal_to($t{"hi"}, $seg[$t{"lseg"}]{"v0"}) _equal_to($t{"lo"}, $seg[$t{"rseg"}]{"v0"})
Truth table100%3/3
A and B
A= _equal_to($t{"hi"}, $seg[$t{"lseg"}]{"v0"})B= _equal_to($t{"lo"}, $seg[$t{"rseg"}]{"v0"})
ABresult
0X0
100
111
3800 _equal_to($t{lo}, $seg[$t{rseg}]{v0})) {
3801 2 $v0 = $t{rseg};
3802 2 $v1 = $t{lseg};
3803 2 $retval = $SP_SIMPLE_LRDN;
3804 2 ▶ if ($dir == $TR_FROM_UP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == $TR_FROM_UP) { } 2 0 2
3805 2 $do_switch = $TRUE;
3806 2 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3807 2 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3808 2 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3809 2 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3810 2 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3811 } else {
3812 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3813 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3814 0 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3815 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3816 0 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3817 }
3818 ▶ } elsif (_equal_to($t{hi}, $seg[$t{rseg}]{v1}) &&
Condition100%3/3
_equal_to($t{"hi"}, $seg[$t{"rseg"}]{"v1"}) and _equal_to($t{"lo"}, $seg[$t{"lseg"}]{"v1"})
!ll&&!rl&&r
223
MC/DC100%2/2
_equal_to($t{"hi"}, $seg[$t{"rseg"}]{"v1"}) and _equal_to($t{"lo"}, $seg[$t{"lseg"}]{"v1"})
_equal_to($t{"hi"}, $seg[$t{"rseg"}]{"v1"}) _equal_to($t{"lo"}, $seg[$t{"lseg"}]{"v1"})
Truth table100%3/3
A and B
A= _equal_to($t{"hi"}, $seg[$t{"rseg"}]{"v1"})B= _equal_to($t{"lo"}, $seg[$t{"lseg"}]{"v1"})
ABresult
0X0
100
111
3819 _equal_to($t{lo}, $seg[$t{lseg}]{v1})) {
3820 3 $v0 = $seg[$t{rseg}]{next};
3821 3 $v1 = $seg[$t{lseg}]{next};
3822  
3823 3 $retval = $SP_SIMPLE_LRUP;
3824 3 ▶ if ($dir == $TR_FROM_UP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == $TR_FROM_UP) { } 3 0 3
3825 3 $do_switch = $TRUE;
3826 3 $mnew = _make_new_monotone_poly($mcur, $v1, $v0);
3827 3 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3828 3 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3829 3 _traverse_polygon($mnew, $t{d1}, $trnum, $TR_FROM_UP);
3830 3 _traverse_polygon($mnew, $t{d0}, $trnum, $TR_FROM_UP);
3831 } else {
3832 0 $mnew = _make_new_monotone_poly($mcur, $v0, $v1);
3833 0 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3834 0 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3835 0 _traverse_polygon($mnew, $t{u0}, $trnum, $TR_FROM_DN);
3836 0 _traverse_polygon($mnew, $t{u1}, $trnum, $TR_FROM_DN);
3837 }
3838 } else { # no split possible
3839 4 $retval = $SP_NOSPLIT;
3840 4 _traverse_polygon($mcur, $t{u0}, $trnum, $TR_FROM_DN);
3841 4 _traverse_polygon($mcur, $t{d0}, $trnum, $TR_FROM_UP);
3842 4 _traverse_polygon($mcur, $t{u1}, $trnum, $TR_FROM_DN);
3843 4 _traverse_polygon($mcur, $t{d1}, $trnum, $TR_FROM_UP);
3844 }
3845 }
3846 }
3847  
3848 15 return $retval;
3849 }
3850  
3851 # For each monotone polygon, find the ymax and ymin (to determine the
3852 # two y-monotone chains) and pass on this monotone polygon for greedy
3853 # triangulation.
3854 # Take care not to triangulate duplicate monotone polygons
3855  
3856 2 ▶sub _triangulate_monotone_polygons {
Subroutine _triangulate_monotone_polygons: called CC=9 SCAR=22.0
3857 2 my ($nvert, $nmonpoly) = @_;
3858  
3859 2 my ($ymax, $ymin);
3860 2 my ($p, $vfirst, $posmax, $posmin, $v);
3861 2 my ($vcount, $processed);
3862  
3863 2 $op_idx = 0;
3864 2 ▶ for (my $i = 0; $i < $nmonpoly; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $nmonpoly) 9 2 11
3865 9 $vcount = 1;
3866 9 $processed = $FALSE;
3867 9 $vfirst = $mchain[$mon[$i]]{vnum};
3868 9 $ymax = {x => $vert[$vfirst]{pt}{x} , y => $vert[$vfirst]{pt}{y}};
3869 9 $ymin = {x => $vert[$vfirst]{pt}{x} , y => $vert[$vfirst]{pt}{y}};
3870 9 $posmax = $posmin = $mon[$i];
3871 9 $mchain[$mon[$i]]{marked} = $TRUE;
3872 9 $p = $mchain[$mon[$i]]{next};
3873 9 ▶ while (($v = $mchain[$p]{vnum}) != $vfirst) {
Branch: 1 branch
BranchTrueFalseTotal
if (($v = $mchain[$p]{"vnum"}) != $vfirst) 23 8 31
3874 23 ▶ if ($mchain[$p]{marked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mchain[$p]{'marked'}) { } 1 22 23
3875 1 $processed = $TRUE;
3876 1 last; # break from while
3877 } else {
3878 22 $mchain[$p]{marked} = $TRUE;
3879 }
3880  
3881 22 ▶ if (_greater_than($vert[$v]{pt}, $ymax)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_greater_than($vert[$v]{"pt"}, $ymax)) 11 11 22
3882 11 $ymax = {x => $vert[$v]{pt}{x} , y => $vert[$v]{pt}{y}};
3883 11 $posmax = $p;
3884 }
3885 22 ▶ if (_less_than($vert[$v]{pt}, $ymin)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_less_than($vert[$v]{"pt"}, $ymin)) 4 18 22
3886 4 $ymin = {x => $vert[$v]{pt}{x} , y => $vert[$v]{pt}{y}};
3887 4 $posmin = $p;
3888 }
3889 22 $p = $mchain[$p]{next};
3890 22 $vcount++;
3891 }
3892  
3893 9 ▶ if ($processed) { # Go to next polygon
Branch: 1 branch
BranchTrueFalseTotal
if ($processed) 1 8 9
3894 1 next;
3895 }
3896  
3897 8 ▶ if ($vcount == 3) { # already a triangle
Branch: 1 branch
BranchTrueFalseTotal
if ($vcount == 3) { } 6 2 8
3898 6 $op[$op_idx][0] = $mchain[$p]{vnum};
3899 6 $op[$op_idx][1] = $mchain[$mchain[$p]{next}]{vnum};
3900 6 $op[$op_idx][2] = $mchain[$mchain[$p]{prev}]{vnum};
3901 6 $op_idx++;
3902 } else { # triangulate the polygon
3903 2 $v = $mchain[$mchain[$posmax]{next}]{vnum};
3904 2 ▶ if (_equal_to($vert[$v]{pt}, $ymin)) { # LHS is a single line
Branch: 1 branch
BranchTrueFalseTotal
if (_equal_to($vert[$v]{'pt'}, $ymin)) { } 1 1 2
3905 1 _triangulate_single_polygon($nvert, $posmax, $TRI_LHS);
3906 } else {
3907 1 _triangulate_single_polygon($nvert, $posmax, $TRI_RHS);
3908 }
3909 }
3910 }
3911  
3912 2 return $op_idx;
3913 }
3914  
3915 # A greedy corner-cutting algorithm to triangulate a y-monotone
3916 # polygon in O(n) time.
3917 # Joseph O-Rourke, Computational Geometry in C.
3918 #
3919 2 ▶sub _triangulate_single_polygon {
Subroutine _triangulate_single_polygon: called CC=7 SCAR=19.5
3920 2 my ($nvert, $posmax, $side) = @_;
3921  
3922 2 my $v;
3923 my @rc;
3924 2 my $ri = 0; # reflex chain
3925 2 my ($endv, $tmp, $vpos);
3926  
3927 2 ▶ if ($side == $TRI_RHS) { # RHS segment is a single segment
Branch: 1 branch
BranchTrueFalseTotal
if ($side == $TRI_RHS) { } 1 1 2
3928 1 $rc[0] = $mchain[$posmax]{vnum};
3929 1 $tmp = $mchain[$posmax]{next};
3930 1 $rc[1] = $mchain[$tmp]{vnum};
3931 1 $ri = 1;
3932  
3933 1 $vpos = $mchain[$tmp]{next};
3934 1 $v = $mchain[$vpos]{vnum};
3935  
3936 1 ▶ if (($endv = $mchain[$mchain[$posmax]{prev}]{vnum}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($endv = $mchain[$mchain[$posmax]{"prev"}]{"vnum"}) == 0) 0 1 1
3937 0 $endv = $nvert;
3938 }
3939 } else { # LHS is a single segment
3940 1 $tmp = $mchain[$posmax]{next};
3941 1 $rc[0] = $mchain[$tmp]{vnum};
3942 1 $tmp = $mchain[$tmp]{next};
3943 1 $rc[1] = $mchain[$tmp]{vnum};
3944 1 $ri = 1;
3945  
3946 1 $vpos = $mchain[$tmp]{next};
3947 1 $v = $mchain[$vpos]{vnum};
3948  
3949 1 $endv = $mchain[$posmax]{vnum};
3950 }
3951  
3952 2 ▶ while (($v != $endv) || ($ri > 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v != $endv or $ri > 1) 12 2 14
Condition100%3/3
$v != $endv or $ri > 1
l!l&&r!l&&!r
1022
MC/DC100%2/2
$v != $endv or $ri > 1
$v != $endv $ri > 1
Truth table100%3/3
A or B
A= $v != $endvB= $ri > 1
ABresult
1X1
011
000
3953 12 ▶ if ($ri > 0) { # reflex chain is non-empty
Branch: 1 branch
BranchTrueFalseTotal
if ($ri > 0) { } 8 4 12
3954 8 ▶ if (_Cross($vert[$v]{pt}, $vert[$rc[$ri - 1]]{pt}, $vert[$rc[$ri]]{pt}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (_Cross($vert[$v]{'pt'}, $vert[$rc[$ri - 1]]{'pt'}, $vert[$rc[$ri]]{'pt'}) > 0) { } 6 2 8
3955 # convex corner: cut if off
3956 6 $op[$op_idx][0] = $rc[$ri - 1];
3957 6 $op[$op_idx][1] = $rc[$ri];
3958 6 $op[$op_idx][2] = $v;
3959 6 $op_idx++;
3960 6 $ri--;
3961 } else { # non-convex
3962 # add v to the chain
3963 2 $ri++;
3964 2 $rc[$ri] = $v;
3965 2 $vpos = $mchain[$vpos]{next};
3966 2 $v = $mchain[$vpos]{vnum};
3967 }
3968 } else { # reflex-chain empty: add v to the
3969 # reflex chain and advance it
3970 4 $rc[++$ri] = $v;
3971 4 $vpos = $mchain[$vpos]{next};
3972 4 $v = $mchain[$vpos]{vnum};
3973 }
3974 } # end-while
3975  
3976 # reached the bottom vertex. Add in the triangle formed
3977 2 $op[$op_idx][0] = $rc[$ri - 1];
3978 2 $op[$op_idx][1] = $rc[$ri];
3979 2 $op[$op_idx][2] = $v;
3980 2 $op_idx++;
3981 2 $ri--;
3982  
3983 }
3984  
3985 1 1;
↑ Summary