blib/lib/Math/Geometry/Planar/Offset.pm

filter: Statementstmt 82.7% 274 / 331 Branchbran 58.5% 75 / 128 Conditioncond 70.0% 21 / 30 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 75.1% 386 / 514 CC 75 SCAR 47.5
CC
75
Cov
76%
OffsetPolygon
144.5
_find_direction
11.2
_do_cross
3.0
CRAP 115.7
↑ Summary
1 package Math::Geometry::Planar::Offset;
2  
3 3 require 5.006;
4  
5 3 our $VERSION = '1.05';
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $debug = 0;
13 3 our $precision = 7;
14  
15 3 require Exporter;
16 3 our @ISA = qw(Exporter);
17 3 our @EXPORT = qw();
18 3 our @EXPORT_OK = qw(
19 OffsetPolygon
20 $precision
21 $debug
22 );
23  
24 =pod
25  
26 =head1 NAME
27  
28 Math::Geometry::Planar::Offset - Calculate offset polygons
29  
30 =head1 SYNOPSIS
31  
32 use Math::Geometry::Planar::Offset;
33  
34 my (@results) = OffsetPolygon(\@points, $distance);
35 foreach my $polygon (@results) {
36 # do something with @$polygon
37 }
38  
39 =head1 AUTHOR
40  
41 Eric Wilhelm @ <ewilhelm at cpan dot org>
42  
43 =head1 COPYRIGHT NOTICE
44  
45 Copyright (C) 2003-2007 Eric L. Wilhelm. All rights reserved.
46  
47 =head1 NO WARRANTY
48  
49 Absolutely, positively NO WARRANTY, neither express or implied, is
50 offered with this software. You use this software at your own risk. In
51 case of loss, neither Eric Wilhelm, nor anyone else, owes you anything
52 whatseover. You have been warned.
53  
54 Note that this includes NO GUARANTEE of MATHEMATICAL CORRECTNESS. If
55 you are going to use this code in a production environment, it is YOUR
56 RESPONSIBILITY to verify that the methods return the correct values.
57  
58 =head1 LICENSE
59  
60 You may use this software under one of the following licenses:
61  
62 (1) GNU General Public License
63 (found at http://www.gnu.org/copyleft/gpl.html)
64 (2) Artistic License
65 (found at http://www.perl.com/pub/language/misc/Artistic.html)
66  
67 =head1 BUGS
68  
69 There are currently some problems with concurrent edge events on outward
70 (and maybe inward) offsets. Some significant changes need to be made.
71  
72 =cut
73  
74 =head1 METHODS
75  
76 These methods are actually defined in Math::Geometry::Planar, which uses
77 this module.
78  
79 =head2 offset_polygon
80  
81 Returns reference to an array of polygons representing the original polygon
82 offsetted by $distance
83  
84 $polygon->offset_polygon($distance);
85  
86 =cut
87  
88 3 my $delta = 10 ** (-$precision);
89  
90 3 my $offset_depth = 0;
91 3 my $screen_height = 600;
92 3 my $flag;
93 my @bisectors;
94  
95 =head1 Functions
96  
97 Only OffsetPolygon is exported.
98  
99 =head2 pi
100  
101 Returns the constant pi
102  
103 =cut
104 148 ▶sub pi {
Subroutine pi: called CC=1 SCAR=0.0
Pod documented
105 148 atan2(1,1) * 4;
106 }
107  
108 =head2 OffsetPolygon
109  
110 Make offset polygon subroutine.
111  
112 Call with offset distance and ref to array of points for original
113 polygon polygon input must be pre-wrapped so point[n]=point[0]
114  
115 Will return a list of polygons (as refs.) The number of polygons in the
116 output depends on the shape of your input polygon. It may split into
117 several pieces during the offset.
118  
119 my (@results) = OffsetPolygon(\@points, $distance);
120 foreach my $polygon (@results) {
121 # do something with @$polygon
122 }
123  
124 =cut
125 6 ▶sub OffsetPolygon {
Subroutine OffsetPolygon: called CC=64 SCAR=49.7
Pod documented
126 6 my ($points, $offset, $canvas) = @_;
127 6 my %intersects;
128 6 my ($count,$n,$n2);
129 6 my ($time, $time_static);
130 # my $key1,$key2;
131 6 my @angles;
132 6 my @directions;
133 6 my @phi;
134 6 my @bis_dir;
135 6 my @bis_end;
136 6 my @bis_scale;
137 6 my @result1;
138 6 my @result2;
139 6 my @outlist;
140 6 my ($cut1,$cut2,$cut);
141 6 $#outlist = 0;
142 6 my @newpoints;
143 my @newpoints1;
144  
145 # set limit on number of recursions
146 # does perl have its own limit to the number of scopes generated?
147 6 ▶ if($offset_depth > 100){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset_depth > 100) 0 6 6
148 0 print "reached recursion depth_limit\n";
149 0 return();
150 }
151 #ew: $offset_depth would have had to be reset by calling script
152 # (now is incremented and decremented on each side of recursive call)
153 6 my $npoints = @{$points};
154 6 ▶ $debug && print "points: ", $npoints, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "points: ", $npoints, "\n" 0 6 6
155 6 ▶ $debug && print "number of points $npoints\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "number of points $npoints\n" 0 6 6
156 # print "points: @{$points->[0]}\n";
157 # exit;
158 # my $bis_length = 15;
159 # All polygons should be counter-clockwise !!!
160 6 _find_direction($points,\@angles,\@directions);
161 6 ▶ $debug and print "starting recurse: ",$offset_depth -1,"\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "starting recurse: ", $offset_depth - 1, "\n" 0 6 6
162 6 ▶ $debug and print "offset: $offset\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "offset: $offset\n" 0 6 6
163 # ew removed junk related to null points
164 # bail out if not at least a triangle
165 6 ▶ if($npoints < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($npoints < 3) 0 6 6
166 0 carp("Need at least 3 non-colinear points to offset");
167 0 return;
168 }
169 # Now to make the bisectors
170 6 ▶ for($n = 0;$n < $npoints; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 37 6 43
171 37 $phi[$n] = ((pi) - $angles[$n]) / 2;
172 37 $bis_scale[$n] = abs(1 / sin($phi[$n]));
173 37 $bis_dir[$n] = $directions[$n] + $phi[$n];
174 37 $bis_end[$n][0] = $points->[$n][0] + $offset * $bis_scale[$n] * cos($bis_dir[$n]); # X-coordinate
175 37 $bis_end[$n][1] = $points->[$n][1] + $offset * $bis_scale[$n] * sin($bis_dir[$n]); # Y-coordinate
176 }
177  
178 # Draw bisectors
179 6 ▶ if ($canvas) {
Branch: 1 branch
BranchTrueFalseTotal
if ($canvas) 0 6 6
180 # first delete existing bisectors
181 0 ▶ for($n = 0; $n<@bisectors;$n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @bisectors) 0 0 0
182 0 $canvas ->delete($bisectors[$n]);
183 }
184 0 @bisectors = ();
185 # then draw the bisector points from the vertices.
186 0 ▶ for($n = 0; $n < $npoints; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 0 0 0
187 0 my $x1 = $points->[$n][0];
188 0 my $y1 = $screen_height - $points->[$n][1];
189 0 my $x2 = $bis_end[$n][0];
190 0 my $y2 = $screen_height-$bis_end[$n][1];
191 0 $bisectors[$n] = $canvas->create('line', $x1,$y1,$x2,$y2, '-fill' => 'blue','-arrow'=>'last');
192 }
193 }
194
195 # Need to find whether a bisector first intersects a bisector,
196 # or a bisector first intersects a "ghost" edge. Also, must know
197 # which one and at what time:
198 6 my ($Ax1,$Ay1,$Ax2,$Ay2,$Bx1,$By1,$Bx2,$By2);
199 6 my ($delAx,$delAy,$delBx,$delBy,$Am,$Ab,$Bm,$Bb);
200 6 my ($x_int,$y_int);
201 6 my ($first_join,$first_split,$split_seg);
202 6 my $first_event = "";
203 6 my $first_time = abs($offset)+1;
204 6 my $time_dir = $offset / abs($offset); # +1 or -1 to give direction
205 6 my $new_offset;
206 6 my $join_time = $first_time;
207 6 my $split_time = $first_time;
208
209 # first find intersections of adjacent bisectors (if any)
210 6 ▶ for($n = 0;$n <$npoints;$n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 37 6 43
211 37 $Ax1 = $points->[$n][0];
212 37 $Ay1 = $points->[$n][1];
213 37 $Ax2 = $bis_end[$n][0];
214 37 $Ay2 = $bis_end[$n][1];
215 37 $delAx = $Ax2 - $Ax1;
216 37 $delAy = $Ay2 - $Ay1;
217 # elw: what amateur wrote this!
218 37 ▶ if($delAx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delAx) { } 37 0 37
219 37 $Am = $delAy / $delAx; $Ab = $Ay1 - $Ax1 * $Am;
220 }
221 else {
222 0 $Am = "inf";
223 }
224 # Check against the next bisector:
225 37 $Bx1 = $points->[$n+1-$npoints][0];
226 37 $By1 = $points->[$n+1-$npoints][1];
227 37 $Bx2 = $bis_end[$n+1-$npoints][0];
228 37 $By2 = $bis_end[$n+1-$npoints][1];
229 37 $delBx = $Bx2 - $Bx1;
230 37 $delBy = $By2 - $By1;
231 # elw: maybe you are getting closer to zen now:)
232 37 ▶ if($delBx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delBx) { } 37 0 37
233 37 $Bm = $delBy / $delBx;$Bb = $By1 - $Bx1 * $Bm;
234 }
235 else{
236 0 $Bm = "inf";
237 }
238 37 ▶ if( ($Am == $Bm) || ($Am eq $Bm) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Am == $Bm or $Am eq $Bm) 0 37 37
Condition33%1/3
$Am == $Bm or $Am eq $Bm
l!l&&r!l&&!r
0037
MC/DC0%0/2
$Am == $Bm or $Am eq $Bm
$Am == $Bm $Am eq $Bm
Truth table33%1/3
A or B
A= $Am == $BmB= $Am eq $Bm
ABresult
1X1
011
000
239 0 next;
240 }
241 # Calculate determinants to find if intersection is within the bisector segment.
242 37 ▶ if (! _do_cross($Ax1,$Ay1,$Ax2,$Ay2,$Bx1,$By1,$Bx2,$By2) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (_do_cross($Ax1, $Ay1, $Ax2, $Ay2, $Bx1, $By1, $Bx2, $By2)) 35 2 37
243 35 next;
244 }
245 # if we have an intersection of the skeleton and only a triangle,
246 # then it has collapsed to a point:
247 2 ▶ if($npoints < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($npoints < 4) 0 2 2
248 0 ▶ $debug && print "collapsed\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "collapsed\n" 0 0 0
249 0 return();
250 }
251 2 ▶ if($Am eq "inf") { # Slope of first line is infinite, so use Ax1.
Branch: 2 branches
BranchTrueFalseTotal
if ($Am eq 'inf') { } 0 2 2
elsif ($Bm eq 'inf') { } 0 2 2
252 0 $x_int = $Ax1; # Will always be on vertical line.
253 0 $y_int = $Bm * $x_int + $Bb;
254 }
255 elsif($Bm eq "inf") { # Slope of second line is infinite, so use Bx1.
256 0 $x_int = $Bx1;$y_int = $Am * $x_int + $Ab;
257 }
258 else {
259 2 $x_int = ($Bb - $Ab) / ($Am - $Bm);$y_int = $Am *$x_int + $Ab;
260 }
261 # Now, if the lines are not parallel, and the intersection
262 # happens on the line segments, we are here with
263 # the x and y coordinates of the intersection of the two lines.
264 ## $debug and printf ("intersection: %6.0f,%6.0f\n",$x_int, $y_int);
265  
266 # Let's find the time of intersect.
267 # distance formula with adjustment for speed
268 2 $time = sqrt( ($x_int - $points->[$n][0])**2 + ($y_int - $points->[$n][1])**2 ) / $bis_scale[$n];
269 2 ▶ if( (abs($time) < $first_time) )
Branch: 1 branch
BranchTrueFalseTotal
if (abs $time < $first_time) 1 1 2
270 # && ( $time / abs($time)== $offset / abs($offset) ) )
271 {
272 # note that none of the times loaded here have a sign
273 1 $first_time = abs($time);
274 1 $join_time = $first_time;
275 1 $first_join=$n;
276 1 $first_event="join";
277 }
278  
279  
280 } # end for n (for each bisector vs next neighbor)
281 # Time is smallest relevant offset distance before first join.
282 # first join is address of point to be joined.
283 # first event is "join" if controlling (could be over-ridden by split)
284 # If this is the controlling case, create the joined polygon at that offset time
285 # and make a recursive call.
286 #
287 #
288 # Now to check for intersections of bisectors with edges
289 # Have to check against all segments except adjacent ones.
290 # Nothing will happen if it is a triangle.
291 6 ▶ if($npoints > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($npoints > 3) 6 0 6
292 6 ▶ for($n = 0; $n < $npoints; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 37 6 43
293 37 $Ax1 = $points->[$n][0]; # Get bisector endpoints
294 37 $Ay1 = $points->[$n][1];
295 37 $Ax2 = $bis_end[$n][0];
296 37 $Ay2 = $bis_end[$n][1];
297 37 ▶ $debug && print "starting at $Ax1 $Ay1\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "starting at $Ax1 $Ay1\n" 0 37 37
298 37 ▶ $debug && print "bisector toward $Ax2 $Ay2\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "bisector toward $Ax2 $Ay2\n" 0 37 37
299 # I need it to be a ray, so I am just making a really long segment here.
300 37 $delAx = 1000* $offset* ($Ax2 - $Ax1);
301 37 $delAy = 1000* $offset* ($Ay2 - $Ay1);
302 37 $Ax2 = $Ax1+ $delAx;
303 37 $Ay2 = $Ay1 + $delAy;
304 0 ▶ if($delAx) {$Am = $delAy / $delAx; $Ab = $Ay1 - $Ax1 * $Am;}else{$Am = "inf";};
Branch: 1 branch
BranchTrueFalseTotal
if ($delAx) { } 37 0 37
305 # this loop has to compare to all of the polygon sides except the adjacent ones
306 37 ▶ for($n2 = 0; $n2 < $npoints; $n2++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n2 < $npoints) 249 37 286
307 249 $time = abs($offset) +1;
308 # Can't split adjacent segments
309 249 ▶ if( ($n2 == $n) || ($n2 == $n-1) || ( ($n==0)&&($n2 == $npoints-1) ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($n2 == $n or $n2 == $n - 1 or $n == 0 and $n2 == $npoints - 1) 74 175 249
Condition100%9/9
$n2 == $n or $n2 == $n - 1 or $n == 0 and $n2 == $npoints - 1
l!l&&r!l&&!r
686175
$n2 == $n or $n2 == $n - 1
l!l&&r!l&&!r
3731181
$n == 0 and $n2 == $npoints - 1
!ll&&!rl&&r
150256
MC/DC100%4/4
$n2 == $n or $n2 == $n - 1 or $n == 0 and $n2 == $npoints - 1
$n2 == $n $n2 == $n - 1 $n == 0 $n2 == $npoints - 1
Truth table100%5/5
A or B or C and D
A= $n2 == $nB= $n2 == $n - 1C= $n == 0D= $n2 == $npoints - 1
ABCDresult
1XXX1
01XX1
00111
000X0
00100
310 74 next;
311 }
312 175 ▶ $debug && print "inner loop at n2 = $n2\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "inner loop at n2 = $n2\n" 0 175 175
313 175 $Bx1 = $points->[$n2][0]; # Get edge endpoints
314 175 $By1 = $points->[$n2][1];
315 175 $Bx2 = $points->[$n2 + 1 - $npoints][0];
316 175 $By2 = $points->[$n2+1-$npoints][1];
317 175 $delBx = $Bx2 - $Bx1;
318 175 $delBy = $By2 - $By1;
319 175 ▶ if($delBx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delBx) { } 89 86 175
320 89 $Bm = $delBy / $delBx;$Bb = $By1 - $Bx1 * $Bm;
321 }
322 else {
323 86 $Bm = "inf";
324 }
325 175 ▶ if( ($Am == $Bm) || ($Am eq $Bm) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($Am == $Bm or $Am eq $Bm) 0 175 175
Condition33%1/3
$Am == $Bm or $Am eq $Bm
l!l&&r!l&&!r
00175
MC/DC0%0/2
$Am == $Bm or $Am eq $Bm
$Am == $Bm $Am eq $Bm
Truth table33%1/3
A or B
A= $Am == $BmB= $Am eq $Bm
ABresult
1X1
011
000
326 0 next;
327 }
328 # Note that I am not using the dot product here (it
329 # shows up below. I need to know where the infinite ray
330 # of the bisector crosses the infinite line of the
331 # polygon edge (but this may cause problems as well)
332 175 ▶ if($Am eq "inf") {
Branch: 2 branches
BranchTrueFalseTotal
if ($Am eq 'inf') { } 0 175 175
elsif ($Bm eq 'inf') { } 86 89 175
333 # Slope of first line is infinite, so use Ax1.
334 0 $x_int = $Ax1;$y_int = $Bm * $x_int + $Bb;
335 }
336 elsif($Bm eq "inf") {
337 # Slope of second line is infinite, so use Bx1.
338 86 $x_int = $Bx1;$y_int = $Am * $x_int + $Ab;
339 }
340 else {
341 89 $x_int = ($Bb - $Ab) / ($Am - $Bm);$y_int = $Am *$x_int + $Ab;
342 }
343 # now make sure that the intersection happened on the right side
344 # (point the ray in the offset direction)
345 175 my $delxto_int = $x_int - $Ax1;
346 175 my $delyto_int = $y_int - $Ay1;
347 175 my $next = $n2+1;
348 175 ▶ if ($next == $npoints) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next == $npoints) 25 150 175
349 25 $next = 0;
350 }
351 # This method is not handling the split which happens to edges terminating
352 # at a concave point. This event is a third case because the time calculated
353 # below will be smaller for the extension of the edge before the concavity than
354 # for the edge after the concavity, which is the one to properly split.
355  
356  
357 # I have suspicions about the accuracy here:
358 # Original intent was to make sure that the ray was properly treated
359 # now handled by using a really long segment (1000*offset) for the ray.
360 # if( $bis_dir[$n] == (atan2($delyto_int,$delxto_int)))
361 {
362 # direction is good, calculate distance
363 # note that the int_scale should be naturally signed
364 # to prevent strangeness on internal offsets.
365 # dvdp:
366 # $int_scale = 1 / (sin( $directions[$n2] - $bis_dir[$n])) ;
367 # This could lead to a devision by 0 so we calculate the reverse and check first
368 175 my $int_scale = (sin( $directions[$n2] - $bis_dir[$n])) ;
369 175 ▶ $debug && print "bisector scale: $bis_scale[$n]\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "bisector scale: $bis_scale[$n]\n" 0 175 175
370 175 ▶ $debug && print "intersection scale: 1 / $int_scale\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "intersection scale: 1 / $int_scale\n" 0 175 175
371 175 ▶ if ($int_scale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int_scale) { } 175 0 175
372 175 $int_scale = 1 / $int_scale;
373 175 ▶ if($bis_scale[$n]+$int_scale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bis_scale[$n] + $int_scale) { } 167 8 175
374 167 $time_static = sqrt( ($x_int - $points->[$n][0])**2 +
375 ($y_int - $points->[$n][1])**2 ) / ($bis_scale[$n]+$int_scale);
376 }
377 else {
378 # intersection happens only at infinite offset time
379 # die "PGON offset dying at 281 with $bis_scale[$n] and $int_scale\n";
380 }
381 }
382 else {
383 # 1 / $int_scale is arbitrary large so division reduces to 0
384 0 $time_static = 0;
385 }
386 # once you have calculated the time, you need to see if the
387 # intersected segment is still in that place at that time to be struck by the ray
388 # Does time_dir belong here? (yes for test case A)
389 175 $Bx1 = $points->[$n2][0] + $time_static * $bis_scale[$n2] * cos($bis_dir[$n2]);
390 175 $By1 = $points->[$n2][1] + $time_static * $bis_scale[$n2] * sin($bis_dir[$n2]);
391 175 $Bx2 = $points->[$n2 + 1 - $npoints][0] + $time_static * $bis_scale[$next] * cos($bis_dir[$next]);
392 175 $By2 = $points->[$n2 + 1 - $npoints][1] + $time_static * $bis_scale[$next] * sin($bis_dir[$next]);
393 175 ▶ if(! _do_cross($Ax1,$Ay1,$Ax2,$Ay2,$Bx1,$By1,$Bx2,$By2) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_do_cross($Ax1, $Ay1, $Ax2, $Ay2, $Bx1, $By1, $Bx2, $By2)) 125 50 175
394 125 next;
395 }
396 50 $time = $time_static;
397 # make sure the case controls and is in the right direction. (required for case A)
398 # dvdp: replaced division by abs($time) by a multiplication to overcome
399 # potential divide by 0
400 50 ▶ if( (abs($time)<$first_time) and ( $time == $time_dir * abs($time) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $time < $first_time and $time == $time_dir * abs($time)) 4 46 50
Condition66%2/3
abs $time < $first_time and $time == $time_dir * abs($time)
!ll&&!rl&&r
4604
MC/DC50%1/2
abs $time < $first_time and $time == $time_dir * abs($time)
abs $time < $first_time $time == $time_dir * abs($time)
Truth table66%2/3
A and B
A= abs $time < $first_timeB= $time == $time_dir * abs($time)
ABresult
0X0
100
111
401 # note that none of the times loaded here have a sign
402 4 $first_time = abs($time);
403 4 $split_time = $first_time;
404 4 $first_split=$n;
405 4 $split_seg = $n2;
406 4 $first_event="split";
407 }
408 }
409 } # end for n2
410 } # end for n (each bisector verses each edge)
411 } # End if not triangle
412
413 # Time is the smallest relavent split time (unless join controls)
414 # first_split is address of point to be split.
415 # first_event is "split" if controlling (could be controlled by join)(or nothing)
416 # If this is the controlling case, create the split polygons at that offset time
417 # and make a recursive call.
418
419 6 ▶ if($first_time <= abs($offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_time <= abs $offset) { } 2 4 6
420 2 ▶ if($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::Geometry::Planar::Offset::debug) 0 2 2
421 0 ▶ if($first_event eq "join") {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_event eq "join") 0 0 0
422 0 printf("join vertex %d at time %6.2f\n",$first_join,$first_time);
423 }
424 0 ▶ if($first_event eq "split") {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_event eq "split") 0 0 0
425 0 printf(
426 "split segment %d with vertex %d at time %6.2f\n",
427 $split_seg,$first_split,$first_time
428 );
429 }
430 0 print "\ttime: $first_time\n";
431 }
432 # get a list of points for the offset polygon at first_time
433  
434 2 ▶ if($first_event eq "join") {
Branch: 2 branches
BranchTrueFalseTotal
if ($first_event eq 'join') { } 1 1 2
elsif ($first_event eq 'split') { } 1 0 1
435 # How are coincident events handled?
436 # remove the offending point and call yourself with time adjusted.
437 1 @newpoints = ();
438 1 ▶ for($n = 0; $n<$npoints; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 5 1 6
439 5 $newpoints[$n][0] = $points->[$n][0] + $first_time *
440 $time_dir* $bis_scale[$n] * cos($bis_dir[$n]);
441 5 $newpoints[$n][1] = $points->[$n][1] + $first_time *
442 $time_dir* $bis_scale[$n] * sin($bis_dir[$n]);
443 }
444 # keep the one with the smaller scale.
445 1 ▶ if($bis_scale[$first_join]<$bis_scale[$first_join+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bis_scale[$first_join] < $bis_scale[$first_join + 1]) { } 1 0 1
446 1 splice(@newpoints, $first_join + 1, 1);
447 }
448 else {
449 0 splice(@newpoints, $first_join , 1);
450 }
451 1 $npoints--;
452 1 $new_offset = $offset - $first_time * $time_dir; # put a direction on it.
453 1 $offset_depth++;
454 1 @outlist = OffsetPolygon(\@newpoints,$new_offset) ;
455 1 $offset_depth--;
456 1 return(@outlist);
457 }
458 elsif ($first_event eq "split") {
459 # make two polygons and call yourself with time adjusted.
460 1 @newpoints = ();
461 1 my @split_check;
462 my @split_check1;
463 1 ▶ for($n = 0; $n<$npoints; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 8 1 9
464 8 $newpoints[$n][0] = $points->[$n][0] + $first_time *
465 $time_dir * $bis_scale[$n] * cos($bis_dir[$n]);
466 8 $newpoints[$n][1] = $points->[$n][1] + $first_time *
467 $time_dir * $bis_scale[$n] * sin($bis_dir[$n]);
468 8 $split_check[$n] = $n;
469 }
470 1 $new_offset = $offset - $first_time * $time_dir;
471 1 @newpoints1 = ();
472 1 @newpoints1 = @newpoints;
473 1 @split_check1 = ();
474 1 @split_check1 = @split_check;
475 # have first_split and split_seg, must find a way to wrap around.
476 1 my $cut_startA;
477 my $cut_startB;
478 1 my $cutB;
479 1 ▶ if($split_seg < $first_split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split_seg < $first_split) { } 0 1 1
480 0 $cut1 = $#newpoints - $first_split;
481 0 $cut2 = $split_seg+1;
482 0 $cut_startA=$first_split+1;
483 0 $cut_startB = $split_seg+1;
484 0 $cutB = $first_split - $split_seg -1;
485 }
486 else {
487 1 $cut1 = $#newpoints - $split_seg;
488 1 $cut2 = $first_split;
489 1 $cut_startA = $split_seg +1;
490 1 $cut_startB = $first_split + 1;
491 1 $cutB = $split_seg - $first_split;
492 }
493 1 splice(@newpoints,$cut_startA,$cut1);
494 1 splice(@newpoints,0,$cut2);
495 1 splice(@split_check,$cut_startA,$cut1);
496 1 splice(@split_check,0,$cut2);
497 1 splice(@newpoints1,$cut_startB,$cutB);
498 1 splice(@split_check1,$cut_startB,$cutB);
499 1 my $num = @newpoints;
500 1 my $num1 = @newpoints1;
501  
502 1 ▶ if($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::Geometry::Planar::Offset::debug) 0 1 1
503 0 print "split was:\n@split_check\n@split_check1\n";
504 0 print "first splitted:\n";
505 0 ▶ for($n = 0; $n<$num;$n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $num) 0 0 0
506 0 printf ("point: %d (%6.2f,%6.2f)\n",$n,@{$newpoints[$n]});
507 }
508 0 print "second splitted:\n";
509 0 ▶ for($n = 0; $n < $num1;$n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $num1) 0 0 0
510 0 printf ("point: %d (%6.2f,%6.2f)\n",$n,@{$newpoints1[$n]});
511 }
512 }
513 1 $#result1 = 0;
514 1 shift(@result1);
515 1 $#result2 = 0;
516 1 shift(@result2);
517 1 ▶ if($num>2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 2) { } 1 0 1
518 1 $offset_depth++;
519 1 @result1 = OffsetPolygon(\@newpoints,$new_offset);
520 1 $offset_depth--;
521 }
522 else {
523 0 ▶ $debug and print "too few on first\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "too few on first\n" 0 0 0
524 }
525 1 ▶ if($num1>2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num1 > 2) { } 1 0 1
526 1 $offset_depth++;
527 1 @result2 = OffsetPolygon(\@newpoints1,$new_offset);
528 1 $offset_depth--;
529 }
530 else {
531 0 ▶ $debug and print "too few on second\n";
Branch: 1 branch
BranchTrueFalseTotal
$Math::Geometry::Planar::Offset::debug and print "too few on second\n" 0 0 0
532 }
533 1 return(@result1, @result2); # This concatenates the list.
534 } # end elsif split
535 } # end if time <= offset
536 else {
537 # No splits or joins needed, so offset the thing.
538 # Bisector endpoints should be available.
539 4 @newpoints = ();
540 4 ▶ for($n = 0; $n<$npoints; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $npoints) 24 4 28
541 24 $newpoints[$n][0] = $points->[$n][0] + $offset * $bis_scale[$n] * cos($bis_dir[$n]);
542 24 $newpoints[$n][1] = $points->[$n][1] + $offset * $bis_scale[$n] * sin($bis_dir[$n]);
543 }
544 4 return( \@newpoints);
545 }
546 } # End offset polygon subroutine definition
547 #########################################################################
548 # Find direction subroutine.
549 # Called with (<Polygon points>,<changes in angle>, <angles>).
550 # Returns total change in angle (as radians (everything is as radians)).
551 # Second two array refs are optional.
552 # Forcing counter-clockwise currently not done here, but maybe should.
553 6 ▶sub _find_direction {
Subroutine _find_direction: called CC=10 SCAR=24.1
554 6 my($coords,$del_theta,$thetas) = @_;
555 6 my $sum_theta=0;
556 6 my $sum_delta_theta = 0;
557 6 @{$del_theta} = (); # Clear arrays referenced for in-place modification.
558 6 @{$thetas} = ();
559 6 my $n = 0;
560 6 for(;;) {
561 43 ▶ last if ($n == @{$coords});
Branch: 1 branch
BranchTrueFalseTotal
if $n == @{$coords;} 6 37 43
562 # dvdp: take advantage of negative indexing in Perl
563 37 my $n2 = $n - @{$coords} + 1;
564 37 my $n3 = $n - 1;
565 37 my $Ax1 = ${$coords}[$n][0]; # Line leaving point
566 37 my $Ay1 = ${$coords}[$n][1]; # this is the one belonging to the point
567 37 my $Ax2 = ${$coords}[$n2][0];
568 37 my $Ay2 = ${$coords}[$n2][1];
569 37 my $delAx = $Ax2 - $Ax1;
570 37 my $delAy = $Ay2 - $Ay1;
571 37 my $Bx1 = ${$coords}[$n3][0]; # Line coming to point n
572 37 my $By1 = ${$coords}[$n3][1];
573 37 my $Bx2 = ${$coords}[$n][0];
574 37 my $By2 = ${$coords}[$n][1];
575 37 my $delBx = $Bx2 - $Bx1;
576 37 my $delBy = $By2 - $By1;
577 # dvdp remove coinciding points
578 37 ▶ if ( ((abs($delAx) < $delta) && (abs($delAy) < $delta)) ||
Branch: 1 branch
BranchTrueFalseTotal
if (abs $delAx < $delta and abs $delAy < $delta or abs $delBx < $delta and abs $delBy < $delta) 0 37 37
Condition55%5/9
abs $delAx < $delta and abs $delAy < $delta or abs $delBx < $delta and abs $delBy < $delta
l!l&&r!l&&!r
0037
abs $delAx < $delta and abs $delAy < $delta
!ll&&!rl&&r
19180
abs $delBx < $delta and abs $delBy < $delta
!ll&&!rl&&r
19180
MC/DC0%0/4
abs $delAx < $delta and abs $delAy < $delta or abs $delBx < $delta and abs $delBy < $delta
abs $delAx < $delta abs $delAy < $delta abs $delBx < $delta abs $delBy < $delta
Truth table42%3/7
A and B or C and D
A= abs $delAx < $deltaB= abs $delAy < $deltaC= abs $delBx < $deltaD= abs $delBy < $delta
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
579 ((abs($delBx) < $delta) && (abs($delBy) < $delta)) ) {
580 0 splice @{$coords},$n,1;
581 0 next;
582 }
583 37 my $theta_A = atan2($delAy,$delAx); # Angle of leaving line
584 37 my $theta_B = atan2($delBy,$delBx); # Angle of coming line
585 37 my $theta_AB = $theta_A - $theta_B;
586 37 ▶ if ($theta_AB < -(pi)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($theta_AB < -pi()) { } 6 31 37
elsif ($theta_AB > pi()) { } 0 31 31
587 6 $theta_AB += 2 * (pi);
588 }
589 elsif ($theta_AB > (pi)) {
590 0 $theta_AB -= 2 * (pi);
591 }
592 37 ▶ if( (abs($theta_AB - (pi)) < $delta) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $theta_AB - pi() < $delta) 0 37 37
593 0 splice @{$coords},$n,1;
594 0 ▶ $n and $n--; # need to recalc thetas if spike removed !
Branch: 1 branch
BranchTrueFalseTotal
$n and --$n 0 0 0
595 0 next;
596 }
597 37 ${$thetas}[$n] = $theta_A;
598 37 ${$del_theta}[$n] = $theta_AB;
599 37 $n++
600 }
601 } # End _find_direction sub
602 #########################################################################
603 # Determines if a pair of line segments have an intersection.
604 212 ▶sub _do_cross {
Subroutine _do_cross: called CC=3 SCAR=11.0
605 212 my ($Ax1,$Ay1,$Ax2,$Ay2,$Bx1,$By1,$Bx2,$By2) = @_;
606 # Calculate four relevant minor determinants
607 212 my $det_123=($Ax2 - $Ax1)*($By1 - $Ay1) - ($Bx1 - $Ax1)*($Ay2 - $Ay1);
608 212 my $det_124=($Ax2 - $Ax1)*($By2 - $Ay1) - ($Bx2 - $Ax1)*($Ay2 - $Ay1);
609 212 my $det_341=($Bx1 - $Ax1)*($By2 - $Ay1) - ($Bx2 - $Ax1)*($By1 - $Ay1);
610 212 my $det_342 =$det_123-$det_124+$det_341;
611 212 ▶ if( ($det_123*$det_124 > 0) || ($det_341*$det_342 > 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($det_123 * $det_124 > 0 or $det_341 * $det_342 > 0) { } 160 52 212
Condition100%3/3
$det_123 * $det_124 > 0 or $det_341 * $det_342 > 0
l!l&&r!l&&!r
1501052
MC/DC100%2/2
$det_123 * $det_124 > 0 or $det_341 * $det_342 > 0
$det_123 * $det_124 > 0 $det_341 * $det_342 > 0
Truth table100%3/3
A or B
A= $det_123 * $det_124 > 0B= $det_341 * $det_342 > 0
ABresult
1X1
011
000
612 160 return(0); # segments only intersect if the above two products are both negative
613 }
614 else {
615 52 return(1);
616 }
617 } # End _do_cross subroutine definition
618 ########################################################################
619  
620 3 1;
↑ Summary