blib/lib/Image/Find/Loops.pm

filter: Statementstmt 99.3% 300 / 302 Branchbran 88.3% 76 / 86 Conditioncond 89.5% 94 / 105 MC/DCmcdc 80.3% 41 / 51 Subroutinesub 100% 34 / 34 Podpod 87.5% 14 / 16 totaltotal 94.1% 559 / 594 CC 115 SCAR 25.2
CC
115
Cov
95%
mapPartition
31.0
fillPartition
15.0
removeInteriorOfPartition
14.0
CRAP 12.4
↑ Summary
1 #!/usr/bin/perl
2 #-------------------------------------------------------------------------------
3 # Image::Find::Loops - Find loops in an image.
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc., 2018
5 #-------------------------------------------------------------------------------
6 # Estimate thickness of loop
7 package Image::Find::Loops;
8 1 our $VERSION = "20180506";
9 1 require v5.16;
10 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 60 ▶sub indent{4} # Print indentation amount
Subroutine indent: called CC=1 SCAR=0.0
Pod undocumented
18  
19 #1 Methods # Find loops in an image.
20  
21 sub new($) #S Find loops in an image represented as a string.
22 1 ▶ {my ($string) = @_; # String of blanks; non blanks; new lines defining the image
Subroutine new: called CC=9 SCAR=22.0
Pod documented
23 1 my @lines = split /\n/, $string;
24 1 my $count; # Number of active pixels
25 my %image; # {x}{y} of active pixels
26 1 my $x; # Image dimension in x
27  
28 1 for my $j(0..$#lines) # Load active pixels
29 15 {my $line = $lines[$j];
30 15 ▶ $x = length($line) if !defined($x) or length($line) > $x; # Longest line
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x or length $line > $x 4 11 15
Condition100%3/3
not defined $x or length $line > $x
l!l&&r!l&&!r
1311
MC/DC100%2/2
not defined $x or length $line > $x
not defined $x length $line > $x
Truth table100%3/3
not(A) or B
A= not defined $xB= length $line > $x
ABresult
1X1
011
000
31 15 for my $i(0..length($line)-1) # Parse each line
32 904 ▶ {$image{$i}{$j} = 0, $count++ if substr($line, $i, 1) ne q( ); # Areas not in drawn loops
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, $i, 1) ne " " 239 665 904
33 }
34 }
35  
36 1 my $d = bless{image=>\%image, x=>$x, y=>scalar(@lines), count=>$count, # Create image of loops
37 bounds=>{}, partitions=>{}, partitionLoop=>{}};
38  
39 1 $d->partitionImage(1); # Partition the image ignoring stray pixels
40 1 $d->fillPartition(1); # Fill the interior of each parition
41 1 $d->removeInteriorOfPartition($_) for 1..$d->numberOfLoops; # Remove the interior of each partition except at the edges to leave the exterior loop
42 1 $d->findLongestLoop($_) for 1..$d->numberOfLoops; # Find the longest path in the exterior edge - this must be the loop described by the partition
43 1 $d->widthOfLoop($_) for 1..$d->numberOfLoops; # Add Loop width estimate at each point
44 1 $d # Return new image with path details
45 } # new
46  
47  
48 sub fillPartition($$) #P Remove any interior voids in a partition.
49 1 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine fillPartition: called CC=15 SCAR=27.1
Pod documented
50 1 my $p = $i->partitions->{$partition}; # Pixels in partition
51 1 my $b = $i->bounds->{$partition}; # Rectangular bounding area of partition
52 1 my ($x1, $y1, $x2, $y2) = @$b; # Rectangle bounding the partition
53 1 my %image; # The image of the inverted partition restricted to the bounding arae
54 1 my $count = 0; # Number of pixels in th image of the inverted partition
55  
56 1 for my $x($x1..$x2) # Define the image of the inverted partition
57 6 {for my $y($y1..$y2)
58 42 ▶ {next if defined $p->{$x}{$y};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{$x}{$y} 24 18 42
59 18 $image{$x}{$y} = 0; ++$count;
60 }
61 }
62  
63 1 my $I = bless
64 {%$i, image=>\%image, count=>$count, bounds=>{1=>$b}, partitions=>{}}; # Create image of inverted partition
65  
66 1 $I->partitionImage(0); # Partition the image of the inverted partition considering partitions of all sizes
67  
68 1 my %ignore; # Ignore the partitions of the inverted image that touch the border of the original partition as the touch confirms that they are part of the exterior
69 1 for my $invertedPartition(sort keys %{$I->partitions}) # Each partition of the image of the inverted partition
70 5 {my $p = $I->partitions->{$invertedPartition};
71 First: # Each pixel in the current partition of the inverted partition
72 5 for my $x(keys % $p)
73 8 {for my $y(keys %{$p->{$x}})
74 14 ▶ {if ($x == $x1 || $x == $x2 and $y == $y1 || $y == $y2) # This partition touches the border so we know that all of this partition is part of the exterior
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $x1 || $x == $x2 and $y == $y1 || $y == $y2) 4 10 14
Condition100%9/9
$x == $x1 || $x == $x2 and $y == $y1 || $y == $y2
!ll&&!rl&&r
824
$x == $x1 or $x == $x2
l!l&&r!l&&!r
338
$y == $y1 or $y == $y2
l!l&&r!l&&!r
222
MC/DC100%4/4
$x == $x1 || $x == $x2 and $y == $y1 || $y == $y2
$x == $x1 $x == $x2 $y == $y1 $y == $y2
Truth table100%7/7
A or B and C or D
A= $x == $x1B= $x == $x2C= $y == $y1D= $y == $y2
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
75 4 {$ignore{$invertedPartition}++;
76 4 last First;
77 }
78 }
79 }
80 }
81  
82 1 for my $invertedPartition(sort keys %{$I->partitions}) # Fill in the internal partitions in the original partition so that any internal voids become filled
83 5 ▶ {next if $ignore{$invertedPartition}; # Ignore exterior partitions to leave just the interior partitions
Branch: 1 branch
BranchTrueFalseTotal
if $ignore{$invertedPartition} 4 1 5
84 1 my $P = $I->partitions->{$invertedPartition};
85 1 for my $x(keys % $P) # Each pixel of an interior partition of the inverted original partition
86 2 {for my $y(keys %{$P->{$x}})
87 6 {$p->{$x}{$y} = $partition; # Fill in an interior pixel that was void in the original partition
88 }
89 }
90 }
91 } # fillPartition
92  
93 sub clone($) #P Clone an image.
94 1 ▶ {my ($i) = @_; # Image
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
95  
96 1 my %partitions; # Clone partitions
97 1 for my $p(keys %{$i->partitions})
98 4 {for my $x(keys %{$i->partitions->{$p}})
99 58 {for my $y(keys %{$i->partitions->{$p}{$x}})
100 221 {$partitions{$p}{$x}{$y}++# = $i->partitions->{$p}{$x}{$y};
101 }
102 }
103 }
104  
105 1 bless {%$i, partitions=>\%partitions}; # Cloned image
106 } # clone
107  
108 sub clonePartition($$) #P Clone a partition of an image.
109 12 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine clonePartition: called CC=3 SCAR=11.0
Pod documented
110 12 my %partition; # Cloned partition
111  
112 12 for my $x(keys %{$i->partitions->{$partition}})
113 174 {for my $y(keys %{$i->partitions->{$partition}{$x}})
114 683 {$partition{$x}{$y} = $i->partitions->{$partition}{$x}{$y};
115 }
116 }
117  
118 12 my $I = bless {%$i}; # Clone image quickly
119 12 $I->partitions = {%{$i->partitions}}; # Clone partitions quickly
120 12 $I->partitions->{$partition} = \%partition; # Replace cloned partition
121 12 $I # Return new image
122 } # clonePartition
123  
124 sub numberOfLoops($) # Number of loops in the image. The partitions and loops are numbered from 1.
125 19 ▶ {my ($i) = @_; # Image
Subroutine numberOfLoops: called CC=1 SCAR=0.0
Pod documented
126 19 scalar(keys %{$i->partitions})
127 } # numberOfLoops
128  
129 sub partitionImage($$) #P Partition the images into disjoint sets of connected points.
130 2 ▶ {my ($i, $small) = @_; # Image, minimum size of a partition - smaller partitions will be ignored
Subroutine partitionImage: called CC=5 SCAR=16.1
Pod documented
131  
132 2 for my $x(sort{$a<=>$b} keys %{$i->image}) # Stabilize partition numbers to make testing possible
133 61 {for my $y(sort{$a<=>$b} keys %{$i->image->{$x}})
134 257 {my $p = $i->image->{$x}{$y};
135 257 ▶ $i->mapPartition($x, $y, $small) if defined($p) and $p == 0; # Bucket fill anything that touches this pixels
Branch: 1 branch
BranchTrueFalseTotal
if defined $p and $p == 0 13 244 257
Condition66%2/3
defined $p and $p == 0
!ll&&!rl&&r
024413
MC/DC50%1/2
defined $p and $p == 0
defined $p $p == 0
Truth table66%2/3
A and B
A= defined $pB= $p == 0
ABresult
0X0
100
111
136 }
137 }
138 } # partitionImage
139  
140 sub mapPartition($$$$) #P Locate the pixels in the image that are connected to a pixel with a specified value.
141 13 ▶ {my ($i, $x, $y, $small) = @_; # Image, x coordinate of first point in partition, y coordinate of first point in partition, delete partitions of fewer pixels
Subroutine mapPartition: called CC=31 SCAR=34.3
Pod documented
142 13 my $p = $i->image->{$x}{$y} = $i->numberOfLoops+1; # Next partition
143 13 $i->partitions->{$p}{$x}{$y}++; # Add first pixel to this partition
144 13 my $pixelsInPartition = 0;
145  
146 13 my ($x1, $x2, $y1, $y2); # Rectangle bounding the partition
147 13 for(1..$i->count) # Worst case - each pixel is a separate line
148 68 {my $changed = 0; # Number of pixels added to this partition on this pass
149 68 for my $x(keys %{$i->image}) # Each pixel
150 3097 {for my $y(keys %{$i->image->{$x}})
151 13373 ▶ {next if $i->image->{$x}{$y} == $p; # Already partitioned
Branch: 1 branch
BranchTrueFalseTotal
if $i->image->{$x}{$y} == $p 2163 11210 13373
152 11210 my $I = $i->image;
153 11210 my ($𝘅, $𝕩, $𝘆, $𝕪) = ($x+1, $x-1, $y+1, $y-1);
154 11210 ▶ if (exists($I->{$𝘅}) && exists($I->{$𝘅}{$y}) && $I->{$𝘅}{$y} == $p or # Add this pixel to the partition if a neigboring pixel exists and is already a part of the paritition
Branch: 1 branch
BranchTrueFalseTotal
if (exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p or exists $I->{$𝕩} and exists $I->{$𝕩}{$y} and $I->{$𝕩}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝕪} and $I->{$x}{$𝕪} == $p) 244 10966 11210
Condition93%31/33
exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p or exists $I->{$𝕩} and exists $I->{$𝕩}{$y} and $I->{$𝕩}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝕪} and $I->{$x}{$𝕪} == $p
l!l&&r!l&&!r
1875710966
exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p or exists $I->{$𝕩} and exists $I->{$𝕩}{$y} and $I->{$𝕩}{$y} == $p
l!l&&r!l&&!r
8110611023
exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p
l!l&&r!l&&!r
156611129
exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p
!ll&&!rl&&r
4572662315
exists $I->{$𝘅} and exists $I->{$𝘅}{$y}
!ll&&!rl&&r
104735256638
exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p
!ll&&!rl&&r
4350677966
exists $I->{$x} and exists $I->{$x}{$𝘆}
!ll&&!rl&&r
043506845
exists $I->{$𝕩} and exists $I->{$𝕩}{$y} and $I->{$𝕩}{$y} == $p
!ll&&!rl&&r
44706553106
exists $I->{$𝕩} and exists $I->{$𝕩}{$y}
!ll&&!rl&&r
83036406659
exists $I->{$x} and exists $I->{$x}{$𝕪} and $I->{$x}{$𝕪} == $p
!ll&&!rl&&r
4257670957
exists $I->{$x} and exists $I->{$x}{$𝕪}
!ll&&!rl&&r
042576766
MC/DC83%10/12
exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p or exists $I->{$𝕩} and exists $I->{$𝕩}{$y} and $I->{$𝕩}{$y} == $p or exists $I->{$x} and exists $I->{$x}{$𝕪} and $I->{$x}{$𝕪} == $p
exists $I->{$𝘅} exists $I->{$𝘅}{$y} $I->{$𝘅}{$y} == $p exists $I->{$x} exists $I->{$x}{$𝘆} $I->{$x}{$𝘆} == $p exists $I->{$𝕩} exists $I->{$𝕩}{$y} $I->{$𝕩}{$y} == $p exists $I->{$x} exists $I->{$x}{$𝕪} $I->{$x}{$𝕪} == $p
Truth table37%45/121
A and B and C or D and E and F or G and H and I or J and K and L
A= exists $I->{$𝘅}B= exists $I->{$𝘅}{$y}C= $I->{$𝘅}{$y} == $pD= exists $I->{$x}E= exists $I->{$x}{$𝘆}F= $I->{$x}{$𝘆} == $pG= exists $I->{$𝕩}H= exists $I->{$𝕩}{$y}I= $I->{$𝕩}{$y} == $pJ= exists $I->{$x}K= exists $I->{$x}{$𝕪}L= $I->{$x}{$𝕪} == $p
ABCDEFGHIJKLresult
111XXXXXXXXX1
0XX111XXXXXX1
10X111XXXXXX1
110111XXXXXX1
0XX0XX111XXX1
0XX10X111XXX1
0XX110111XXX1
10X0XX111XXX1
10X10X111XXX1
10X110111XXX1
1100XX111XXX1
11010X111XXX1
110110111XXX1
0XX0XX0XX1111
0XX0XX10X1111
0XX0XX1101111
0XX10X0XX1111
0XX10X10X1111
0XX10X1101111
0XX1100XX1111
0XX11010X1111
0XX1101101111
10X0XX0XX1111
10X0XX10X1111
10X0XX1101111
10X10X0XX1111
10X10X10X1111
10X10X1101111
10X1100XX1111
10X11010X1111
10X1101101111
1100XX0XX1111
1100XX10X1111
1100XX1101111
11010X0XX1111
11010X10X1111
11010X1101111
1101100XX1111
11011010X1111
1101101101111
0XX0XX0XX0XX0
0XX0XX0XX10X0
0XX0XX0XX1100
0XX0XX10X0XX0
0XX0XX10X10X0
0XX0XX10X1100
0XX0XX1100XX0
0XX0XX11010X0
0XX0XX1101100
0XX10X0XX0XX0
0XX10X0XX10X0
0XX10X0XX1100
0XX10X10X0XX0
0XX10X10X10X0
0XX10X10X1100
0XX10X1100XX0
0XX10X11010X0
0XX10X1101100
0XX1100XX0XX0
0XX1100XX10X0
0XX1100XX1100
0XX11010X0XX0
0XX11010X10X0
0XX11010X1100
0XX1101100XX0
0XX11011010X0
0XX1101101100
10X0XX0XX0XX0
10X0XX0XX10X0
10X0XX0XX1100
10X0XX10X0XX0
10X0XX10X10X0
10X0XX10X1100
10X0XX1100XX0
10X0XX11010X0
10X0XX1101100
10X10X0XX0XX0
10X10X0XX10X0
10X10X0XX1100
10X10X10X0XX0
10X10X10X10X0
10X10X10X1100
10X10X1100XX0
10X10X11010X0
10X10X1101100
10X1100XX0XX0
10X1100XX10X0
10X1100XX1100
10X11010X0XX0
10X11010X10X0
10X11010X1100
10X1101100XX0
10X11011010X0
10X1101101100
1100XX0XX0XX0
1100XX0XX10X0
1100XX0XX1100
1100XX10X0XX0
1100XX10X10X0
1100XX10X1100
1100XX1100XX0
1100XX11010X0
1100XX1101100
11010X0XX0XX0
11010X0XX10X0
11010X0XX1100
11010X10X0XX0
11010X10X10X0
11010X10X1100
11010X1100XX0
11010X11010X0
11010X1101100
1101100XX0XX0
1101100XX10X0
1101100XX1100
11011010X0XX0
11011010X10X0
11011010X1100
1101101100XX0
11011011010X0
1101101101100
155 exists($I->{$x}) && exists($I->{$x}{$𝘆}) && $I->{$x}{$𝘆} == $p or
156 exists($I->{$𝕩}) && exists($I->{$𝕩}{$y}) && $I->{$𝕩}{$y} == $p or
157 exists($I->{$x}) && exists($I->{$x}{$𝕪}) && $I->{$x}{$𝕪} == $p)
158 244 {$i->image->{$x}{$y} = $p;
159 244 ++$changed;
160 244 ++$i->partitions->{$p}{$x}{$y}; # Pixels in this partition
161 244 ++$pixelsInPartition;
162 244 ▶ $x1 = $x if !defined($x1) or $x1 > $x; # Rectangular bounds for partition
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x1 or $x1 > $x 10 234 244
Condition100%3/3
not defined $x1 or $x1 > $x
l!l&&r!l&&!r
91234
MC/DC100%2/2
not defined $x1 or $x1 > $x
not defined $x1 $x1 > $x
Truth table100%3/3
not(A) or B
A= not defined $x1B= $x1 > $x
ABresult
1X1
011
000
163 244 ▶ $x2 = $x if !defined($x2) or $x2 < $x;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x2 or $x2 < $x 64 180 244
Condition100%3/3
not defined $x2 or $x2 < $x
l!l&&r!l&&!r
955180
MC/DC100%2/2
not defined $x2 or $x2 < $x
not defined $x2 $x2 < $x
Truth table100%3/3
not(A) or B
A= not defined $x2B= $x2 < $x
ABresult
1X1
011
000
164 244 ▶ $y1 = $y if !defined($y1) or $y1 > $y;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $y1 or $y1 > $y 26 218 244
Condition100%3/3
not defined $y1 or $y1 > $y
l!l&&r!l&&!r
917218
MC/DC100%2/2
not defined $y1 or $y1 > $y
not defined $y1 $y1 > $y
Truth table100%3/3
not(A) or B
A= not defined $y1B= $y1 > $y
ABresult
1X1
011
000
165 244 ▶ $y2 = $y if !defined($y2) or $y2 < $y;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $y2 or $y2 < $y 35 209 244
Condition100%3/3
not defined $y2 or $y2 < $y
l!l&&r!l&&!r
926209
MC/DC100%2/2
not defined $y2 or $y2 < $y
not defined $y2 $y2 < $y
Truth table100%3/3
not(A) or B
A= not defined $y2B= $y2 < $y
ABresult
1X1
011
000
166 }
167 }
168 }
169 68 ▶ last unless $changed; # No more pixels in parition to consider
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 13 55 68
170 }
171  
172 13 ▶ if ($pixelsInPartition <= $small) # Remove small partitions
Branch: 1 branch
BranchTrueFalseTotal
if ($pixelsInPartition <= $small) { } 4 9 13
173 4 {for my $x(keys %{$i->image})
174 214 {for my $y(keys %{$i->image->{$x}})
175 950 ▶ {delete $i->image->{$x}{$y} if $i->image->{$x}{$y} == $p;
Branch: 1 branch
BranchTrueFalseTotal
if $i->image->{$x}{$y} == $p 4 946 950
176 950 ▶ delete $i->image->{$x} unless keys %{$i->image->{$x}}; # Remove containing hash if now empty
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$i->image->{$x};} 4 946 950
177 }
178 }
179 4 delete $i->partitions->{$p}
180 }
181 else
182 9 {$i->bounds->{$p} = [$x1, $y1, $x2, $y2]; # Record bounds
183 }
184 } # mapPartition
185  
186 sub removeInteriorOfPartition($$) #P Remove the interior of a partition to leave the exterior loop.
187 4 ▶ {my ($I, $partition) = @_; # Image, partition
Subroutine removeInteriorOfPartition: called CC=14 SCAR=26.4
Pod documented
188 4 my $i = $I->clonePartition($partition);
189 4 my $p = $i->partitions->{$partition}; # Each point in image
190  
191 4 for my $x(keys % $p) # Zero out the interior
192 58 {for my $y(keys %{$p->{$x}})
193 241 {my ($𝘅, $𝕩, $𝘆, $𝕪) = ($x+1, $x-1, $y+1, $y-1);
194  
195 $p->{$x}{$y} = 0 if # Zero an element theat does not touch the exterior of the partition
196 exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and
197 exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪} and
198 241 ▶ exists $p->{$𝕩}{$y} and exists $p->{$𝕩}{$𝘆} and exists $p->{$𝕩}{$𝕪};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪} and exists $p->{$𝕩}{$y} and exists $p->{$𝕩}{$𝘆} and exists $p->{$𝕩}{$𝕪} 20 221 241
Condition100%21/21
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪} and exists $p->{$𝕩}{$y} and exists $p->{$𝕩}{$𝘆} and exists $p->{$𝕩}{$𝕪}
!ll&&!rl&&r
217420
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪} and exists $p->{$𝕩}{$y} and exists $p->{$𝕩}{$𝘆}
!ll&&!rl&&r
212524
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪} and exists $p->{$𝕩}{$y}
!ll&&!rl&&r
205729
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪}
!ll&&!rl&&r
198736
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆}
!ll&&!rl&&r
191743
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y}
!ll&&!rl&&r
1236850
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆}
!ll&&!rl&&r
8835118
MC/DC100%8/8
exists $p->{$x}{$𝕪} and exists $p->{$x}{$𝘆} and exists $p->{$𝘅}{$y} and exists $p->{$𝘅}{$𝘆} and exists $p->{$𝘅}{$𝕪} and exists $p->{$𝕩}{$y} and exists $p->{$𝕩}{$𝘆} and exists $p->{$𝕩}{$𝕪}
exists $p->{$x}{$𝕪} exists $p->{$x}{$𝘆} exists $p->{$𝘅}{$y} exists $p->{$𝘅}{$𝘆} exists $p->{$𝘅}{$𝕪} exists $p->{$𝕩}{$y} exists $p->{$𝕩}{$𝘆} exists $p->{$𝕩}{$𝕪}
Truth table100%9/9
A and B and C and D and E and F and G and H
A= exists $p->{$x}{$𝕪}B= exists $p->{$x}{$𝘆}C= exists $p->{$𝘅}{$y}D= exists $p->{$𝘅}{$𝘆}E= exists $p->{$𝘅}{$𝕪}F= exists $p->{$𝕩}{$y}G= exists $p->{$𝕩}{$𝘆}H= exists $p->{$𝕩}{$𝕪}
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
199 }
200 }
201  
202 4 for my $x(keys % $p) # The remaining pixels are the exterior edge of the partition
203 62 {for my $y(keys %{$p->{$x}})
204 241 ▶ {delete $I->partitions->{$partition}{$x}{$y} unless $p->{$x}{$y};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{$x}{$y} 20 221 241
205 }
206 }
207 } # removeInteriorOfPartition
208  
209 sub findLongestLoop($$) #P Find the longest loop in a partition.
210 4 ▶ {my ($I, $partition) = @_; # Image, partition
Subroutine findLongestLoop: called CC=11 SCAR=24.0
Pod documented
211 4 my $i = $I->clonePartition($partition);
212 4 my $p = $i->partitions->{$partition}; # Pixels in the partition
213  
214 my $break = sub
215 4 ▶ {for my $x(sort keys % $p) # Break the loop into a path
Subroutine __ANON__: called CC=3 SCAR=11.2
216 4 {for my $y(sort keys %{$p->{$x}})
217 4 {return [$x, $y];
218 }
219 }
220 0 confess "No pixels in partition $partition?"; # This should not happen!
221 4 }->();
222  
223 4 my ($x, $y) = @$break; # The start/end point for this partition
224 4 delete $p->{$x}{$y}; # Break the loop to get to end points that we can find the shortest path between
225 4 my ($start, $end) = $i->searchArea($partition, $x, $y); # Start and end points
226 4 ▶ $start or confess "No start point";
Branch: 1 branch
BranchTrueFalseTotal
$start or confess("No start point") 4 0 4
227 4 ▶ $end or confess "No end point";
Branch: 1 branch
BranchTrueFalseTotal
$end or confess("No end point") 4 0 4
228 4 my ($X, $Y) = @$end; # Coordinates of end point
229 4 my @loop = $start; # Start the path
230 4 my @longestLoop; # Shortest path so far
231 4 my @search = [$i->searchArea($partition, @$start)]; # Initial search area is the pixels around the start pixel
232 4 my %visited; # Pixels we have already visited along the possible path
233  
234 4 ▶ while(@search) # Find the shortest path amongst all the possible paths
Branch: 1 branch
BranchTrueFalseTotal
if (@search) 271378 4 271382
235 271378 ▶ {@loop == @search or confess "Search and path depth mismatch"; # These two arrays must stay in sync because their dimensions reflects the progress along the possible path
Branch: 1 branch
BranchTrueFalseTotal
@loop == @search or confess("Search and path depth mismatch") 271378 0 271378
236 271378 my $search = $search[-1]; # Pixels to search for latest path element
237 271378 ▶ if (!@$search) # Nothing left to search at this level
Branch: 1 branch
BranchTrueFalseTotal
if (not @$search) { } 133751 137627 271378
238 133751 {pop @search; # Remove search level
239 133751 my ($x, $y) = @{pop @loop}; # Pixel to remove from possible path
240 133751 delete $visited{$x}{$y}; # Pixel no longer visited on this possible path
241 }
242 else
243 137627 {my ($x, $y) = @{pop @$search}; # Next pixel to add to path
244 137627 ▶ next if $visited{$x}{$y}; # Pixel has already been vsisited on this path so skip it
Branch: 1 branch
BranchTrueFalseTotal
if $visited{$x}{$y} 0 137627 137627
245 137627 ▶ if ($x == $X and $y == $Y)
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $X and $y == $Y) { } 1940 135687 137627
Condition100%3/3
$x == $X and $y == $Y
!ll&&!rl&&r
13458211051940
MC/DC100%2/2
$x == $X and $y == $Y
$x == $X $y == $Y
Truth table100%3/3
A and B
A= $x == $XB= $y == $Y
ABresult
0X0
100
111
246 1940 ▶ {@longestLoop = @loop if !@longestLoop or @loop > @longestLoop;
Branch: 1 branch
BranchTrueFalseTotal
if not @longestLoop or @loop > @longestLoop 19 1921 1940
Condition100%3/3
not @longestLoop or @loop > @longestLoop
l!l&&r!l&&!r
4151921
MC/DC100%2/2
not @longestLoop or @loop > @longestLoop
not @longestLoop @loop > @longestLoop
Truth table100%3/3
not(A) or B
A= not @longestLoopB= @loop > @longestLoop
ABresult
1X1
011
000
247 1940 my ($x, $y) = @{pop @loop}; # Pixel to remove from possible path
248 1940 pop @search; # any other adjacent pixels will not produce a shorter path
249 1940 delete $visited{$x}{$y}; # Pixel no longer visited on this possible path
250 }
251 else # Extend the search
252 135687 {push @loop, [$x, $y]; # Extend the path
253 135687 $visited{$x}{$y}++;
254 push @search, # Extend the search area to pixels not already visited on this path
255 135687 [grep {my ($x, $y) = @$_; !$visited{$x}{$y}}
256 $i->searchArea($partition, $x, $y)]
257 }
258 }
259 }
260  
261 4 $I->partitionLoop->{$partition} = [$break, @longestLoop, $end] # Return the shortest path from start to end and the break point to make a loop
262 } # findLongestLoop
263  
264 sub searchArea($$$$) #P Return the pixels to search from around a given pixel.
265 135695 ▶ {my ($i, $partition, $x, $y) = @_; # Image, partition, x coordinate of center of search, y coordinate of center of search.
Subroutine searchArea: called CC=5 SCAR=16.1
Pod documented
266 135695 my $p = $i->partitions->{$partition};
267 135695 my ($𝘅, $𝕩, $𝘆, $𝕪) = ($x+1, $x-1, $y+1, $y-1);
268 135695 my @s; # Pixels to search from
269 135695 ▶ push @s, [$𝘅, $y] if exists $p->{$𝘅}{$y};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$𝘅}{$y} 82615 53080 135695
270 135695 ▶ push @s, [$x, $𝘆] if exists $p->{$x}{$𝘆};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$x}{$𝘆} 79462 56233 135695
271 135695 ▶ push @s, [$x, $𝕪] if exists $p->{$x}{$𝕪};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$x}{$𝕪} 79993 55702 135695
272 135695 ▶ push @s, [$𝕩, $y] if exists $p->{$𝕩}{$y};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$𝕩}{$y} 82384 53311 135695
273 @s # Return all possible pixels
274 135695 } # searchArea
275  
276 sub widthOfLoop($$) #P Find the (estimated) width of the loop at each point.
277 4 ▶ {my ($I, $partition) = @_; # Image, partition
Subroutine widthOfLoop: called CC=2 SCAR=6.9
Pod documented
278 4 my $i = $I->clonePartition($partition); # Clone the specified partition so that we can remove pixels once they have been processed to spped up the remaining search
279 4 my $loop = $i->partitionLoop->{$partition}; # Loop in image
280 4 my $maxSteps = @$loop;
281  
282 4 for my $step(keys @$loop) # Each pixel in the path
283 214 {my ($x, $y) = @{$$loop[$step]};
284  
285 my $explore = sub #P Explore away from a point checking that we are still in the partition associated with the Loop
286 1712 ▶ {my ($dx, $dy) = @_; # x direction, y direction
Subroutine __ANON__: called CC=3 SCAR=11.1
287 1712 for my $step(1..$maxSteps) # Maximum possible width
288 {return $step-1 unless $i->partitions->{$partition} # Keep stepping whilst still in partition
289 {$x+$step*$dx}
290 4081 ▶ {$y+$step*$dy};
Branch: 1 branch
BranchTrueFalseTotal
unless $i->partitions->{$partition}{$x + $step * $dx}{$y + $step * $dy} 1712 2369 4081
291 }
292 $maxSteps # We never left the partition
293 0 };
294  
295 214 push @{$I->partitionLoop->{$partition}[$step]}, 1 + min # Explore in opposite directions along 4 lines and take the minimum as the width
296 ($explore->(1, 0) + $explore->(-1, 0),
297 $explore->(1, 1) + $explore->(-1, -1),
298 $explore->(0, 1) + $explore->( 0, -1),
299 $explore->(1, -1) + $explore->(-1, +1));
300 }
301 } # widthOfLoop
302  
303 sub loop($$) # Return an array of arrays [x, y] of sequentially touching pixels describing the largest loop in the specified partition where the loops in an image are numbered from 1.
304 1 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine loop: called CC=1 SCAR=0.0
Pod documented
305 1 $i->partitionLoop->{$partition} # Return the loop
306 } # loop
307  
308 sub printLoop($$) # Print a loop in the image numbering pixels with the estimated thickness of the loop.
309 3 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine printLoop: called CC=8 SCAR=21.0
Pod documented
310 3 my $X = $i->x; my $Y = $i->y; # Image dimensions
311 3 my $s = ' ' x $X; # Image line
312 3 my @s = ($s) x $Y; # Image lines
313 3 my $p = $i->partitionLoop->{$partition}; # Each point in image
314 3 my $c = 0; # Cycle though 0..9 to show loop
315  
316 my $plot = sub # Plot a pixel
317 192 ▶ {my ($x, $y, $symbol) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.1
318 192 ▶ substr($s[$y], $x, 1) = $symbol % 10 if $y< $Y and $x < $X;
Branch: 1 branch
BranchTrueFalseTotal
if $y < $Y and $x < $X 192 0 192
Condition33%1/3
$y < $Y and $x < $X
!ll&&!rl&&r
00192
MC/DC0%0/2
$y < $Y and $x < $X
$y < $Y $x < $X
Truth table33%1/3
A and B
A= $y < $YB= $x < $X
ABresult
0X0
100
111
319 3 };
320  
321 3 $plot->(@$_) for @$p; # Plot each pixel in the loop
322  
323 3 my ($x1, $y1, $x2, $y2) = my @bounds = @{$i->bounds->{$partition}}; # Bounds
324 3 ▶ $x1--, $x2++ while $x1 > 0 and $x2 < $X-1 and $x2 - $x1 <= 10;
Branch: 1 branch
BranchTrueFalseTotal
$x1 > 0 and $x2 < $X - 1 and $x2 - $x1 <= 10 and do { --$x1, ++$x2 } 5 3 8
Condition50%3/6
$x1 > 0 and $x2 < $X - 1 and $x2 - $x1 <= 10
!ll&&!rl&&r
035
$x1 > 0 and $x2 < $X - 1
!ll&&!rl&&r
008
MC/DC33%1/3
$x1 > 0 and $x2 < $X - 1 and $x2 - $x1 <= 10
$x1 > 0 $x2 < $X - 1 $x2 - $x1 <= 10
Truth table50%2/4
A and B and C
A= $x1 > 0B= $x2 < $X - 1C= $x2 - $x1 <= 10
ABCresult
0XX0
10X0
1100
1111
325 3 ▶ $y1--, $y2++ while $y1 > 0 and $y2 < $Y-1;
Branch: 1 branch
BranchTrueFalseTotal
$y1 > 0 and $y2 < $Y - 1 and do { --$y1, ++$y2 } 3 3 6
Condition100%3/3
$y1 > 0 and $y2 < $Y - 1
!ll&&!rl&&r
123
MC/DC100%2/2
$y1 > 0 and $y2 < $Y - 1
$y1 > 0 $y2 < $Y - 1
Truth table100%3/3
A and B
A= $y1 > 0B= $y2 < $Y - 1
ABresult
0X0
100
111
326 3 my ($xl, $yl) = ($x2-$x1+1, $y2-$y1+1); # Lengths
327  
328 my $h = sub # Header layout
329 3 ▶ {my ($space) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
330 3 my $N = 1 + int($X/10);
331 my $s = join '',
332 3 ▶ map{substr($_, -1) ? q( ) : $_ > 9 ? substr($_, -2, 1) : 0} 0..$X;
Branch: 2 branches
BranchTrueFalseTotal
substr($_, -1) ? : 216 24 240
$_ > 9 ? : 21 3 24
333 3 my $t = substr(("0123456789"x(1 + int($X/10))), 0, $X);
334 3 $s = substr($s, $x1, $xl);
335 3 $t = substr($t, $x1, $xl);
336 3 "$space $s\n$space $t\n"
337 3 }->(" " x indent);
338  
339 my $m = # Loop layout
340 join "\n",
341 41 map{sprintf("%".indent."d ", $_).substr($s[$_].(q( )x$X), $x1, $xl)}
342 3 ▶ grep{$_ >= $y1 and $_ <= $y2}
Condition66%2/3
$_ >= $y1 && $_ <= $y2
!ll&&!rl&&r
4041
MC/DC50%1/2
$_ >= $y1 && $_ <= $y2
$_ >= $y1 $_ <= $y2
Truth table66%2/3
A && B
A= $_ >= $y1B= $_ <= $y2
ABresult
0X0
100
111
343 keys @s;
344  
345 3 my $f = "Image: X = $X, Y = $Y, Loop = $partition"; # Footer layout
346  
347 3 join "\n", $h, $m, $f;
348 } # printLoop
349  
350 sub print($) # Print the loops in an image sequentially numbering adjacent pixels in each loop from 0..9.
351 1 ▶ {my ($i) = @_; # Image
Subroutine print: called CC=3 SCAR=11.0
Pod documented
352 1 my $X = $i->x; my $Y = $i->y; # Image dimensions
353 1 my $s = ' ' x $X; # Image line
354 1 my @s = ($s) x $Y; # Image lines
355  
356 1 for my $partition(1..$i->numberOfLoops) # Each partition
357 4 {my $p = $i->partitionLoop->{$partition}; # Each point in image
358 4 my $c = 0; # Cycle though 0..9 to show loop
359  
360 my $plot = sub # Plot a pixel
361 214 ▶ {my ($x, $y) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.1
362 214 ▶ substr($s[$y], $x, 1) = (++$c % 10) if $y < $Y and $x < $X;
Branch: 1 branch
BranchTrueFalseTotal
if $y < $Y and $x < $X 214 0 214
Condition33%1/3
$y < $Y and $x < $X
!ll&&!rl&&r
00214
MC/DC0%0/2
$y < $Y and $x < $X
$y < $Y $x < $X
Truth table33%1/3
A and B
A= $y < $YB= $x < $X
ABresult
0X0
100
111
363 4 };
364  
365 4 $plot->(@$_) for @$p; # Plot each pixel in the loop
366 }
367  
368 my $h = sub # Header layout
369 1 ▶ {my ($space) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
370 1 my $N = 1 + int($X/10);
371 my $s = join '',
372 1 ▶ map{substr($_, -1) ? q( ) : $_ > 9 ? substr($_, -2, 1) : 0} 0..$X;
Branch: 2 branches
BranchTrueFalseTotal
substr($_, -1) ? : 72 8 80
$_ > 9 ? : 7 1 8
373 1 my $t = substr(("0123456789"x(1 + int($X/10))), 0, $X);
374 1 "$space $s\n$space $t\n"
375 1 }->(" " x indent);
376  
377 1 my $m = join "\n", map{sprintf("%".indent."d ", $_).$s[$_]} keys @s; # Loop layout
378  
379 1 my $f = "Image: X = $X, Y = $Y, Loops = ".$i->numberOfLoops; # Footer layout
380  
381 1 join "\n", $h, $m, $f;
382 } # print
383  
384 #1 Attributes # Attributes of an image
385  
386 1 ▶BEGIN{
Subroutine BEGIN: called CC=1 SCAR=0.0
387 1 genLValueScalarMethods(q(bounds)); # The bounds of each partition: [$x1, $y1, $x2, $y2].
388 1 genLValueScalarMethods(q(count)); # Number of points in the image.
389 1 genLValueScalarMethods(q(image)); # Image data points.
390 1 genLValueScalarMethods(q(partitions)); # Number of partitions in the image.
391 1 genLValueScalarMethods(q(partitionLoop)); # Loop for each partition.
392 1 genLValueScalarMethods(q(x)); # Image dimension in x.
393 1 genLValueScalarMethods(q(y)); # Image dimension in y.
394 }
395  
396 #-------------------------------------------------------------------------------
397 # Export
398 #-------------------------------------------------------------------------------
399  
400 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
401  
402 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
403  
404 1 @ISA = qw(Exporter);
405 1 @EXPORT_OK = qw(
406 );
407 1 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
408  
409 # podDocumentation
410  
411 =pod
412  
413 =encoding utf-8
414  
415 =head1 Name
416  
417 Image::Find::Loops - Find loops in an image.
418  
419 =head1 Synopsis
420  
421 Use L<new|/new> to create and analyze a new image, then L<print|/print> to
422 visualize the loops detected, or L<loop|/loop> to get the coordinates of
423 points in each loop in sequential order.
424  
425 =head1 Description
426  
427 Find loops in an image.
428  
429 The following sections describe the methods in each functional area of this
430 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
431  
432  
433  
434 =head1 Methods
435  
436 Find loops in an image.
437  
438 =head2 new($)
439  
440 Find loops in an image represented as a string.
441  
442 Parameter Description
443 1 $string String of blanks; non blanks; new lines defining the image
444  
445 Example:
446  
447  
448 my $d = new (<<END);
449  
450 11 11111111111 11111111111
451 1111 1 1 1 1
452 11 11 1111 1 111 1 1 1 1
453 11 11 11 11 1 1 1 1 1 1
454 11 11 11 11 1 1 1 1 1 1
455 1111 11111 1 1 1 1 1 1
456 11 1111 1 1 1 1111111111 1 1
457 1111 1 1 1 1111111111 1 1
458 11111 1 1 1 1 1 1
459 111111111111111111 1 1 1 1 1 1
460 11 1111111111 1 111 1 1 1 1
461 1 11111111 1 1 1 1
462 11111111111 1 1 1 1
463 111 11111111111 11111111111
464  
465 END
466  
467 ok nws($d->print) eq nws(<<END);
468 0 1 2 3 4 5 6 7
469 0123456789012345678901234567890123456789012345678901234567890123456789012345678
470 0
471 1 56 12345678901 23456789012
472 2 3478 8 2 1 3
473 3 12 90 1234 7 432 3 0 4
474 4 2 1 09 5 6 5 1 4 9 5
475 5 10 32 78 67 5 6 0 5 8 6
476 6 9854 63298 4 7 9 6 7 7
477 7 76 5410 3 8 8 7890123456 8
478 8 0123 2 9 7 0987654321 9
479 9 89 4 1 0 6 1 0 0
480 10 123412367 5678 0 1 5 2 9 1
481 11 0 45 09 9 234 3 8 2
482 12 9 654321 8 4 7 3
483 13 8765432107 7 5 6 4
484 14 98 65432109876 54321098765
485  
486 Image: X = 79, Y = 15, Loops = 4
487 END
488  
489 ok nws($d->printLoop(2)) eq nws(<<END);
490 1 2
491 678901234567890123
492  
493 3 1111
494 4 22 1
495 5 22 22
496 6 22412
497 7 1334
498 8 3411
499 9 11 1
500 10 111111111 1111
501 11 1 22 22
502 12 1 111111
503 13 1111111122
504 14 21
505  
506 Image: X = 79, Y = 15, Loop = 2
507 END
508  
509 ok nws($d->printLoop(3)) eq nws(<<END);
510 3 4 5
511 012345678901234567890123456789
512  
513 1 11111111111 11111111111
514 2 1 1 1 1
515 3 1 1 1 1
516 4 1 1 1 1
517 5 1 1 1 1
518 6 1 1 1 1
519 7 1 1222222221 1
520 8 1 1222222221 1
521 9 1 1 1 1
522 10 1 1 1 1
523 11 1 1 1 1
524 12 1 1 1 1
525 13 1 1 1 1
526 14 11111111111 11111111111
527  
528 Image: X = 79, Y = 15, Loop = 3
529 END
530  
531 ok nws($d->printLoop(4)) eq nws(<<END);
532 3 4
533 9012345678901
534  
535 0
536 1
537 2
538 3 111
539 4 1 1
540 5 1 1
541 6 1 1
542 7 1 1
543 8 1 1
544 9 1 1
545 10 1 1
546 11 111
547 12
548 13
549 14
550  
551 Image: X = 79, Y = 15, Loop = 4
552 END
553  
554  
555 This is a static method and so should be invoked as:
556  
557 Image::Find::Loops::new
558  
559  
560 =head2 numberOfLoops($)
561  
562 Number of loops in the image. The partitions and loops are numbered from 1.
563  
564 Parameter Description
565 1 $i Image
566  
567 Example:
568  
569  
570 is_deeply [$d->count, $d->x, $d->y, $d->numberOfLoops],
571  
572 [239, 79, 15, 4];
573  
574  
575 =head2 loop($$)
576  
577 Return an array of arrays [x, y] of sequentially touching pixels describing the largest loop in the specified partition where the loops in an image are numbered from 1.
578  
579 Parameter Description
580 1 $i Image
581 2 $partition Partition
582  
583 Example:
584  
585  
586 is_deeply [grep{$_->[2] > 2} @{$d->loop(2)}],
587  
588 [[15, 8, 3],
589  
590 [15, 7, 3],
591  
592 [15, 6, 4],
593  
594 [14, 7, 3],
595  
596 [16, 7, 4],
597  
598  
599 =head2 printLoop($$)
600  
601 Print a loop in the image numbering pixels with the estimated thickness of the loop.
602  
603 Parameter Description
604 1 $i Image
605 2 $partition Partition
606  
607 Example:
608  
609  
610 ok nws($d->printLoop(2)) eq nws(<<END);
611 1 2
612 678901234567890123
613  
614 3 1111
615 4 22 1
616 5 22 22
617 6 22412
618 7 1334
619 8 3411
620 9 11 1
621 10 111111111 1111
622 11 1 22 22
623 12 1 111111
624 13 1111111122
625 14 21
626  
627 Image: X = 79, Y = 15, Loop = 2
628 END
629  
630 ok nws($d->printLoop(3)) eq nws(<<END);
631 3 4 5
632 012345678901234567890123456789
633  
634 1 11111111111 11111111111
635 2 1 1 1 1
636 3 1 1 1 1
637 4 1 1 1 1
638 5 1 1 1 1
639 6 1 1 1 1
640 7 1 1222222221 1
641 8 1 1222222221 1
642 9 1 1 1 1
643 10 1 1 1 1
644 11 1 1 1 1
645 12 1 1 1 1
646 13 1 1 1 1
647 14 11111111111 11111111111
648  
649 Image: X = 79, Y = 15, Loop = 3
650 END
651  
652 ok nws($d->printLoop(4)) eq nws(<<END);
653 3 4
654 9012345678901
655  
656 0
657 1
658 2
659 3 111
660 4 1 1
661 5 1 1
662 6 1 1
663 7 1 1
664 8 1 1
665 9 1 1
666 10 1 1
667 11 111
668 12
669 13
670 14
671  
672 Image: X = 79, Y = 15, Loop = 4
673 END
674  
675  
676 =head2 print($)
677  
678 Print the loops in an image sequentially numbering adjacent pixels in each loop from 0..9.
679  
680 Parameter Description
681 1 $i Image
682  
683 Example:
684  
685  
686 ok nws($d->print) eq nws(<<END);
687 0 1 2 3 4 5 6 7
688 0123456789012345678901234567890123456789012345678901234567890123456789012345678
689 0
690 1 56 12345678901 23456789012
691 2 3478 8 2 1 3
692 3 12 90 1234 7 432 3 0 4
693 4 2 1 09 5 6 5 1 4 9 5
694 5 10 32 78 67 5 6 0 5 8 6
695 6 9854 63298 4 7 9 6 7 7
696 7 76 5410 3 8 8 7890123456 8
697 8 0123 2 9 7 0987654321 9
698 9 89 4 1 0 6 1 0 0
699 10 123412367 5678 0 1 5 2 9 1
700 11 0 45 09 9 234 3 8 2
701 12 9 654321 8 4 7 3
702 13 8765432107 7 5 6 4
703 14 98 65432109876 54321098765
704  
705 Image: X = 79, Y = 15, Loops = 4
706 END
707  
708  
709 =head1 Attributes
710  
711 Attributes of an image
712  
713 =head2 bounds :lvalue
714  
715 The bounds of each partition: [$x1, $y1, $x2, $y2].
716  
717  
718 =head2 count :lvalue
719  
720 Number of points in the image.
721  
722  
723 =head2 image :lvalue
724  
725 Image data points.
726  
727  
728 =head2 partitions :lvalue
729  
730 Number of partitions in the image.
731  
732  
733 =head2 partitionLoop :lvalue
734  
735 Loop for each partition.
736  
737  
738 =head2 x :lvalue
739  
740 Image dimension in x.
741  
742  
743 =head2 y :lvalue
744  
745 Image dimension in y.
746  
747  
748  
749 =head1 Private Methods
750  
751 =head2 fillPartition($$)
752  
753 Remove any interior voids in a partition.
754  
755 Parameter Description
756 1 $i Image
757 2 $partition Partition
758  
759 =head2 clone($)
760  
761 Clone an image.
762  
763 Parameter Description
764 1 $i Image
765  
766 Example:
767  
768  
769 is_deeply $d, $d->clone;
770  
771  
772 =head2 clonePartition($$)
773  
774 Clone a partition of an image.
775  
776 Parameter Description
777 1 $i Image
778 2 $partition Partition
779  
780 =head2 partitionImage($$)
781  
782 Partition the images into disjoint sets of connected points.
783  
784 Parameter Description
785 1 $i Image
786 2 $small Minimum size of a partition - smaller partitions will be ignored
787  
788 =head2 mapPartition($$$$)
789  
790 Locate the pixels in the image that are connected to a pixel with a specified value.
791  
792 Parameter Description
793 1 $i Image
794 2 $x X coordinate of first point in partition
795 3 $y Y coordinate of first point in partition
796 4 $small Delete partitions of fewer pixels
797  
798 =head2 removeInteriorOfPartition($$)
799  
800 Remove the interior of a partition to leave the exterior loop.
801  
802 Parameter Description
803 1 $I Image
804 2 $partition Partition
805  
806 =head2 findLongestLoop($$)
807  
808 Find the longest loop in a partition.
809  
810 Parameter Description
811 1 $I Image
812 2 $partition Partition
813  
814 =head2 searchArea($$$$)
815  
816 Return the pixels to search from around a given pixel.
817  
818 Parameter Description
819 1 $i Image
820 2 $partition Partition
821 3 $x X coordinate of center of search
822 4 $y Y coordinate of center of search.
823  
824 =head2 widthOfLoop($$)
825  
826 Find the (estimated) width of the loop at each point.
827  
828 Parameter Description
829 1 $I Image
830 2 $partition Partition
831  
832  
833 =head1 Index
834  
835  
836 1 L<bounds|/bounds>
837  
838 2 L<clone|/clone>
839  
840 3 L<clonePartition|/clonePartition>
841  
842 4 L<count|/count>
843  
844 5 L<fillPartition|/fillPartition>
845  
846 6 L<findLongestLoop|/findLongestLoop>
847  
848 7 L<image|/image>
849  
850 8 L<loop|/loop>
851  
852 9 L<mapPartition|/mapPartition>
853  
854 10 L<new|/new>
855  
856 11 L<numberOfLoops|/numberOfLoops>
857  
858 12 L<partitionImage|/partitionImage>
859  
860 13 L<partitionLoop|/partitionLoop>
861  
862 14 L<partitions|/partitions>
863  
864 15 L<print|/print>
865  
866 16 L<printLoop|/printLoop>
867  
868 17 L<removeInteriorOfPartition|/removeInteriorOfPartition>
869  
870 18 L<searchArea|/searchArea>
871  
872 19 L<widthOfLoop|/widthOfLoop>
873  
874 20 L<x|/x>
875  
876 21 L<y|/y>
877  
878 =head1 Installation
879  
880 This module is written in 100% Pure Perl and, thus, it is easy to read, use,
881 modify and install.
882  
883 Standard L<Module::Build> process for building and installing modules:
884  
885 perl Build.PL
886 ./Build
887 ./Build test
888 ./Build install
889  
890 =head1 Author
891  
892 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
893  
894 L<http://www.appaapps.com|http://www.appaapps.com>
895  
896 =head1 Copyright
897  
898 Copyright (c) 2016-2018 Philip R Brenan.
899  
900 This module is free software. It may be used, redistributed and/or modified
901 under the same terms as Perl itself.
902  
903 =cut
904  
905  
906  
907 # Tests and documentation
908  
909 sub test
910 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=16.2
Pod undocumented
911 1 binmode($_, ":utf8") for *STDOUT, *STDERR;
912 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
913 1 my $s = eval "join('', <${p}::DATA>)";
914 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
915 1 ▶ eval $s;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
916 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
917 }
918  
919 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
920  
921 1 1;
922 # podDocumentation
923 __DATA__
↑ Summary