blib/lib/Image/Find/Paths.pm

filter: Statementstmt 100% 203 / 203 Branchbran 87.5% 42 / 48 Conditioncond 90.4% 38 / 42 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 100% 25 / 25 Podpod 92.3% 12 / 13 totaltotal 95.4% 333 / 349 CC 57 SCAR 22.8
CC
57
Cov
97%
mapPartition
23.0
shortestPathBetweenEndPoints
9.0
new
8.0
CRAP 9.8
↑ Summary Build »
1 #!/usr/bin/perl
2 #-------------------------------------------------------------------------------
3 # Image::Find::Paths - Locate paths in an image.
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc., 2018
5 #-------------------------------------------------------------------------------
6 package Image::Find::Paths;
7 1 our $VERSION = "20180429";
8 1 require v5.16;
9 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #1 Attributes # Attributes of an image
17  
18 1 genLValueScalarMethods(q(count)); # Number of points in the image.
19 1 genLValueScalarMethods(q(image)); # Image data points.
20 1 genLValueScalarMethods(q(partitions)); # Number of partitions in the image.
21 1 genLValueScalarMethods(q(partitionEnd)); # End points for each path.
22 1 genLValueScalarMethods(q(partitionStart)); # Start points for each path.
23 1 genLValueScalarMethods(q(partitionPath)); # Path for each partition.
24 1 genLValueScalarMethods(q(x)); # Image dimension in x.
25 1 genLValueScalarMethods(q(y)); # Image dimension in y.
26  
27 #1 Methods # Locate paths in an image
28  
29 sub new($) #S Find paths in an image represented as a string.
30 1 ▶ {my ($string) = @_; # String of blanks; non blanks; new lines defining the image
Subroutine new: called CC=8 SCAR=20.8
Pod documented
31 1 my @lines = split /\n/, $string;
32 1 my $count; # Number of active pixels
33 my %image; # {x}{y} of active pixels
34 1 my $x; # Image dimension in x
35 1 for my $j(0..$#lines) # Load active pixels
36 10 {my $line = $lines[$j];
37 10 ▶ $x = length($line) if !defined($x) or length($line) > $x; # Longest line
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x or length $line > $x 1 9 10
Condition66%2/3
not defined $x or length $line > $x
l!l&&r!l&&!r
109
MC/DC50%1/2
not defined $x or length $line > $x
not defined $x length $line > $x
Truth table66%2/3
not(A) or B
A= not defined $xB= length $line > $x
ABresult
1X1
011
000
38 10 for my $i(0..length($line)-1) # Parse each line
39 800 ▶ {$image{$i}{$j} = 0, $count++ if substr($line, $i, 1) ne q( );
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, $i, 1) ne " " 73 727 800
40 }
41 }
42
43 1 my $d = bless{image=>\%image, x=>$x, y=>scalar(@lines), count=>$count, # Create image of paths
44 partitions=>{}, partitionStart=>{}, partitionEnd=>{},
45 partitionPath=>{}};
46  
47 1 $d->partition; # Partition the image
48 1 $d->start($_), $d->end($_) for 1..$d->numberOfPaths; # Find a start point for each partition
49 1 $d->shortestPathBetweenEndPoints($_) for 1..$d->numberOfPaths; # Find the longest path in each partition
50  
51 1 $d # Return new image with path details
52 }
53
54 sub clone($) # Clone an image.
55 19 ▶ {my ($i) = @_; # Image
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
56
57 19 my %partitions; # Clone partitions
58 19 for my $p(keys %{$i->partitions})
59 114 {for my $x(keys %{$i->partitions->{$p}})
60 513 {for my $y(keys %{$i->partitions->{$p}{$x}})
61 1349 {$partitions{$p}{$x}{$y} = $i->partitions->{$p}{$x}{$y};
62 }
63 }
64 }
65
66 19 bless {%$i, partitions=>\%partitions}; # Cloned image
67 }
68
69 sub numberOfPaths($) # Number of paths in the image.
70 11 ▶ {my ($i) = @_; # Image
Subroutine numberOfPaths: called CC=1 SCAR=0.0
Pod documented
71 11 scalar(keys %{$i->partitions})
72 }
73
74 sub partition($) #P Partition == set of connected points.
75 1 ▶ {my ($i) = @_; # Image
Subroutine partition: called CC=4 SCAR=13.9
Pod documented
76 1 for my $x(sort{$a<=>$b} keys %{$i->image}) # Stabilize partition numbers to make testing possible
77 25 {for my $y(sort{$a<=>$b} keys %{$i->image->{$x}})
78 73 ▶ {$i->mapPartition($x, $y) if $i->image->{$x}{$y} == 0; # Bucket fill anything that touches this pixels
Branch: 1 branch
BranchTrueFalseTotal
if $i->image->{$x}{$y} == 0 8 65 73
79 }
80 }
81 }
82  
83 sub mapPartition($$$) #P Locate the pixels in the image that are connected to a pixel with a specified value
84 8 ▶ {my ($i, $x, $y) = @_; # Image, x coordinate of first point in partition, y coordinate of first point in partition
Subroutine mapPartition: called CC=23 SCAR=31.4
Pod documented
85 8 my $p = $i->image->{$x}{$y} = $i->numberOfPaths+1; # Next partition
86 8 $i->partitions->{$p}{$x}{$y}++; # Add first pixel to this partition
87 8 my $pixelsInPartition = 0;
88  
89 8 for(1..$i->count) # Worst case - each pixel is a separate line
90 37 {my $changed = 0; # Number of pixels added to this partition on this pass
91 37 for my $x(keys %{$i->image}) # Each pixel
92 908 {for my $y(keys %{$i->image->{$x}})
93 2684 ▶ {next if $i->image->{$x}{$y} == $p; # Already partitioned
Branch: 1 branch
BranchTrueFalseTotal
if $i->image->{$x}{$y} == $p 367 2317 2684
94 2317 my $I = $i->image;
95 2317 my ($𝘅, $𝕩, $𝘆, $𝕪) = ($x+1, $x-1, $y+1, $y-1);
96 2317 ▶ 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) 49 2252 2301
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
41562220
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
16252276
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
972301
exists $I->{$𝘅} and exists $I->{$𝘅}{$y} and $I->{$𝘅}{$y} == $p
!ll&&!rl&&r
123310759
exists $I->{$𝘅} and exists $I->{$𝘅}{$y}
!ll&&!rl&&r
4777561084
exists $I->{$x} and exists $I->{$x}{$𝘆} and $I->{$x}{$𝘆} == $p
!ll&&!rl&&r
13139887
exists $I->{$x} and exists $I->{$x}{$𝘆}
!ll&&!rl&&r
01313995
exists $I->{$𝕩} and exists $I->{$𝕩}{$y} and $I->{$𝕩}{$y} == $p
!ll&&!rl&&r
1212106425
exists $I->{$𝕩} and exists $I->{$𝕩}{$y}
!ll&&!rl&&r
4527601089
exists $I->{$x} and exists $I->{$x}{$𝕪} and $I->{$x}{$𝕪} == $p
!ll&&!rl&&r
127497824
exists $I->{$x} and exists $I->{$x}{$𝕪}
!ll&&!rl&&r
012741002
MC/DC75%9/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 table22%27/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
97 exists($I->{$x}) && exists($I->{$x}{$𝘆}) && $I->{$x}{$𝘆} == $p or
98 exists($I->{$𝕩}) && exists($I->{$𝕩}{$y}) && $I->{$𝕩}{$y} == $p or
99 exists($I->{$x}) && exists($I->{$x}{$𝕪}) && $I->{$x}{$𝕪} == $p)
100 65 {$i->image->{$x}{$y} = $p;
101 65 ++$changed;
102 65 ++$i->partitions->{$p}{$x}{$y}; # Pixels in this partition
103 65 ++$pixelsInPartition;
104 }
105 }
106 }
107 37 ▶ last unless $changed; # No more pixels in parition to consider
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 8 29 37
108 }
109  
110 8 ▶ if ($pixelsInPartition <= 1) # Remove partitions of just one pixel
Branch: 1 branch
BranchTrueFalseTotal
if ($pixelsInPartition <= 1) 2 6 8
111 2 {for my $x(keys %{$i->image})
112 49 {for my $y(keys %{$i->image->{$x}})
113 145 ▶ {delete $i->image->{$x}{$y} if $i->image->{$x}{$y} == $p;
Branch: 1 branch
BranchTrueFalseTotal
if $i->image->{$x}{$y} == $p 2 143 145
114 145 ▶ delete $i->image->{$x} unless keys %{$i->image->{$x}}; # Remove containing hash if now empty
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$i->image->{$x};} 2 143 145
115 }
116 }
117 2 delete $i->partitions->{$p}
118 }
119 }
120  
121 sub traverseToOtherEnd($$$$) #P Traverse to the other end of a partition.
122 12 ▶ {my ($I, $partition, $x, $y) = @_; # Image, partition, start x coordinate, start y coordinate
Subroutine traverseToOtherEnd: called CC=2 SCAR=6.9
Pod documented
123 12 my $i = $I->clone; # Clone the image so that we can remove pixels once they have been processed to spped up the remaining search
124 12 my @remove = ([$x, $y]); # Removal sequence
125 12 my $last; # We know that there are two or more pixels in the paritition
126 12 ▶ while(@remove)
Branch: 1 branch
BranchTrueFalseTotal
if (@remove) 144 12 156
127 144 {$last = shift @remove;
128 144 my ($x, $y) = @$last;
129 144 my $P = $i->partitions->{$partition};
130 144 delete $P->{$x}{$y}; # Remove the pixel currently being examined
131 144 push @remove, $i->searchArea($partition, $x, $y);
132 }
133 $last # Last point is the other end
134 12 }
135  
136 sub start($$) #P Find the starting point for a path in a partition.
137 6 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine start: called CC=3 SCAR=11.0
Pod documented
138 6 my $remove; # Removal sequence
139  
140 6 for my $x((sort{$a<=>$b} keys %{$i->partitions->{$partition} })[0]) # Find the first point in a partition
141 6 {for my $y((sort{$a<=>$b} keys %{$i->partitions->{$partition}{$x}})[0])
142 6 {$remove = [$x, $y];
143 }
144 }
145  
146 6 $i->partitionStart->{$partition} = # Record start point
147 $i->traverseToOtherEnd($partition, @$remove);
148 }
149  
150 sub end($$) #P Find the other end of a path in a partition.
151 6 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine end: called CC=1 SCAR=0.0
Pod documented
152 $i->partitionEnd->{$partition} = # Record start point
153 6 $i->traverseToOtherEnd($partition, @{$i->partitionStart->{$partition}});
154 }
155  
156 sub searchArea($$$$) #P Return the pixels to search from around a given pixel.
157 217 ▶ {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
158 217 my @s; # Pixels to search from
159 217 my $P = $i->partitions->{$partition};
160 217 my ($𝘅, $𝕩, $𝘆, $𝕪) = ($x+1, $x-1, $y+1, $y-1);
161 217 ▶ push @s, [$𝘅, $y] if exists $P->{$𝘅}{$y};
Branch: 1 branch
BranchTrueFalseTotal
if exists $P->{$𝘅}{$y} 74 143 217
162 217 ▶ push @s, [$x, $𝘆] if exists $P->{$x}{$𝘆};
Branch: 1 branch
BranchTrueFalseTotal
if exists $P->{$x}{$𝘆} 65 152 217
163 217 ▶ push @s, [$x, $𝕪] if exists $P->{$x}{$𝕪};
Branch: 1 branch
BranchTrueFalseTotal
if exists $P->{$x}{$𝕪} 65 152 217
164 217 ▶ push @s, [$𝕩, $y] if exists $P->{$𝕩}{$y};
Branch: 1 branch
BranchTrueFalseTotal
if exists $P->{$𝕩}{$y} 68 149 217
165 @s # Return all possible pixels
166 217 }
167  
168 sub shortestPathBetweenEndPoints($$) #P Find the shortest path between the start and the end points of a partition.
169 6 ▶ {my ($I, $partition) = @_; # Image, partition
Subroutine shortestPathBetweenEndPoints: called CC=9 SCAR=22.0
Pod documented
170 6 my $i = $I->clone;
171 6 my ($X, $Y) = @{$i->partitionEnd->{$partition}}; # The end point for this partition
172 6 my @path = ($i->partitionStart->{$partition}); # A possible path
173 6 my @shortestPath; # Shortest path so far
174 6 my @search = [$i->searchArea($partition, @{$path[0]})]; # Initial search area is the pixels around the start pixel
175 6 my %visited; # Pixels we have already visited along the possible path
176
177 6 ▶ while(@search) # Find the shortest path amongst all the possible paths
Branch: 1 branch
BranchTrueFalseTotal
if (@search) 140 6 146
178 140 ▶ {@path == @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
@path == @search or confess("Search and path depth mismatch") 0 140 140
179 140 my $search = $search[-1]; # Pixels to search for latest path element
180 140 ▶ if (!@$search) # Nothing left to search at this level
Branch: 1 branch
BranchTrueFalseTotal
if (not @$search) { } 66 74 140
181 66 {pop @search; # Remove search level
182 66 my ($x, $y) = @{pop @path}; # Pixel to remove from possible path
183 66 delete $visited{$x}{$y}; # Pixel no longer visited on this possible path
184 }
185 else
186 74 {my ($x, $y) = @{pop @$search}; # Next pixel to add to path
187 74 ▶ 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 74 74
188 74 ▶ if ($x == $X and $y == $Y)
Branch: 1 branch
BranchTrueFalseTotal
if ($x == $X and $y == $Y) { } 7 67 74
Condition100%3/3
$x == $X and $y == $Y
!ll&&!rl&&r
5987
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
189 7 ▶ {@shortestPath = @path if !@shortestPath or @path < @shortestPath;
Branch: 1 branch
BranchTrueFalseTotal
if not @shortestPath or @path < @shortestPath 6 1 7
Condition66%2/3
not @shortestPath or @path < @shortestPath
l!l&&r!l&&!r
601
MC/DC50%1/2
not @shortestPath or @path < @shortestPath
not @shortestPath @path < @shortestPath
Truth table66%2/3
not(A) or B
A= not @shortestPathB= @path < @shortestPath
ABresult
1X1
011
000
190 7 my ($x, $y) = @{pop @path}; # Pixel to remove from possible path
191 7 pop @search; # any other adjacent pixels will not produce a shorter path
192 7 delete $visited{$x}{$y}; # Pixel no longer visited on this possible path
193 }
194 else # Extend the search
195 67 {push @path, [$x, $y]; # Extend the path
196 67 $visited{$x}{$y}++;
197 push @search, # Extend the search area to pixels not already visited on this path
198 67 [grep {my ($x, $y) = @$_; !$visited{$x}{$y}}
199 $i->searchArea($partition, $x, $y)]
200 }
201 }
202 }
203  
204 6 push @shortestPath, $i->partitionEnd->{$partition}; # Add end point.
205 6 $I->partitionPath->{$partition} = [@shortestPath] # Return the shortest path
206 }
207  
208 sub path($$) # Path for a specified partition.
209 1 ▶ {my ($i, $partition) = @_; # Image, partition
Subroutine path: called CC=1 SCAR=0.0
Pod documented
210 1 $i->partitionPath->{$partition} # Return the shortest path
211 }
212
213 sub print($) # Print the image.
214 1 ▶ {my ($i) = @_; # Image
Subroutine print: called CC=3 SCAR=11.0
Pod documented
215 1 my $X = $i->x; my $Y = $i->y;
216 1 my $s = ' ' x $X;
217 1 my @s = ($s) x $Y;
218  
219 my $plot = sub
220 61 ▶ {my ($x, $y, $symbol) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
221 61 substr($s[$y], $x, 1) = $symbol;
222 1 };
223  
224 1 for my $partition(keys %{$i->partitionPath}) # Each path
225 6 {my ($start, @p) = @{$i->partitionPath->{$partition}}; # Draw path
226 6 my $end = pop @p;
227 6 $plot->(@$start, q(S));
228 6 $plot->(@$_, q(+)) for @p;
229 6 $plot->(@$end, q(E));
230 }
231  
232 1 join "\n", @s
233 }
234
235 #-------------------------------------------------------------------------------
236 # Export
237 #-------------------------------------------------------------------------------
238  
239 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 1 @ISA = qw(Exporter);
244 1 @EXPORT_OK = qw(
245 );
246 1 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
247  
248 # podDocumentation
249  
250 =pod
251  
252 =encoding utf-8
253  
254 =head1 Name
255  
256 Image::Find::Paths - Locate paths in an image.
257  
258 =head1 Synopsis
259  
260 =head1 Description
261  
262 Locate paths in an image.
263  
264 The following sections describe the methods in each functional area of this
265 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
266  
267  
268  
269 =head1 Attributes
270  
271 Attributes of an image
272  
273 =head2 count :lvalue
274  
275 Number of points in image
276  
277  
278 =head2 image :lvalue
279  
280 Image data points
281  
282  
283 =head2 partitions :lvalue
284  
285 Number of parts in the image
286  
287  
288 =head2 partitionEnd :lvalue
289  
290 End point for a partition
291  
292  
293 =head2 partitionStart :lvalue
294  
295 Start point for a partition
296  
297  
298 =head2 partitionPath :lvalue
299  
300 Start path for each partition
301  
302  
303 =head2 x :lvalue
304  
305 Image dimension in x
306  
307  
308 =head2 y :lvalue
309  
310 Image dimension in y
311  
312  
313 =head1 Methods
314  
315 Locate paths in an image
316  
317 =head2 new($)
318  
319 Find paths in an image represented as a string.
320  
321 Parameter Description
322 1 $string String of blanks; non blanks; new lines defining the image
323  
324 Example:
325  
326  
327 my $d = new(<<END);
328 11 1
329 11 1 1
330 1111 111 1
331 1 11 1 1
332 111 111 111 1
333 11 11 1 1 1
334 11 111 1 1
335 1 1 1 1 1
336 1111111111 1 111111 1
337 111 1 1 1
338 END
339
340 ok $d->x == 80;
341
342 ok $d->y == 10;
343
344 ok nws($d->print) eq nws(<<END);
345 E+ E
346 ++ +
347 ++++ E++ +
348 + + S +
349 +++ ++ E+ S
350 ++ S+ +
351 ++ +++ + E
352 + + + S +
353 +++++++++ + E++++ +
354 S S
355 END
356
357 ok $d->numberOfPaths == 6;
358
359 is_deeply $d->path(5), [[79, 4], [79, 3], [79, 2], [79, 1], [79, 0]];
360
361  
362 This is a static method and so should be invoked as:
363  
364 Image::Find::Paths::new
365  
366  
367 =head2 clone($)
368  
369 Clone an image.
370  
371 Parameter Description
372 1 $i Image
373  
374 Example:
375  
376  
377 is_deeply $d, $d->clone;
378
379  
380 =head2 numberOfPaths($)
381  
382 Number of paths in the image.
383  
384 Parameter Description
385 1 $i Image
386  
387 Example:
388  
389  
390 ok $d->numberOfPaths == 6;
391
392  
393 =head2 path($$)
394  
395 Path for a specified partition.
396  
397 Parameter Description
398 1 $i Image
399 2 $partition Partition
400  
401 Example:
402  
403  
404 is_deeply $d->path(5), [[79, 4], [79, 3], [79, 2], [79, 1], [79, 0]];
405
406  
407 =head2 print($)
408  
409 Print the image.
410  
411 Parameter Description
412 1 $i Image
413  
414 Example:
415  
416  
417 ok nws($d->print) eq nws(<<END);
418 E+ E
419 ++ +
420 ++++ E++ +
421 + + S +
422 +++ ++ E+ S
423 ++ S+ +
424 ++ +++ + E
425 + + + S +
426 +++++++++ + E++++ +
427 S S
428 END
429
430  
431  
432 =head1 Private Methods
433  
434 =head2 partition($)
435  
436 Partition == set of connected points.
437  
438 Parameter Description
439 1 $i Image
440  
441 =head2 mapPartition($$$)
442  
443 Locate the pixels in the image that are connected to a pixel with a specified value
444  
445 Parameter Description
446 1 $i Image
447 2 $x X coordinate of first point in partition
448 3 $y Y coordinate of first point in partition
449  
450 =head2 traverseToOtherEnd($$$$)
451  
452 Traverse to the other end of a partition.
453  
454 Parameter Description
455 1 $I Image
456 2 $partition Partition
457 3 $x Start x coordinate
458 4 $y Start y coordinate
459  
460 =head2 start($$)
461  
462 Find the starting point for a path in a partition.
463  
464 Parameter Description
465 1 $i Image
466 2 $partition Partition
467  
468 =head2 end($$)
469  
470 Find the other end of a path in a partition.
471  
472 Parameter Description
473 1 $i Image
474 2 $partition Partition
475  
476 =head2 searchArea($$$$)
477  
478 Return the pixels to search from around a given pixel.
479  
480 Parameter Description
481 1 $i Image
482 2 $partition Partition
483 3 $x X coordinate of center of search
484 4 $y Y coordinate of center of search.
485  
486 =head2 shortestPathBetweenEndPoints($$)
487  
488 Find the shortest path between the start and the end points of a partition.
489  
490 Parameter Description
491 1 $I Image
492 2 $partition Partition
493  
494  
495 =head1 Index
496  
497  
498 1 L<clone|/clone>
499  
500 2 L<count|/count>
501  
502 3 L<end|/end>
503  
504 4 L<image|/image>
505  
506 5 L<mapPartition|/mapPartition>
507  
508 6 L<new|/new>
509  
510 7 L<numberOfPaths|/numberOfPaths>
511  
512 8 L<partition|/partition>
513  
514 9 L<partitionEnd|/partitionEnd>
515  
516 10 L<partitionPath|/partitionPath>
517  
518 11 L<partitions|/partitions>
519  
520 12 L<partitionStart|/partitionStart>
521  
522 13 L<path|/path>
523  
524 14 L<print|/print>
525  
526 15 L<searchArea|/searchArea>
527  
528 16 L<shortestPathBetweenEndPoints|/shortestPathBetweenEndPoints>
529  
530 17 L<start|/start>
531  
532 18 L<traverseToOtherEnd|/traverseToOtherEnd>
533  
534 19 L<x|/x>
535  
536 20 L<y|/y>
537  
538 =head1 Installation
539  
540 This module is written in 100% Pure Perl and, thus, it is easy to read, use,
541 modify and install.
542  
543 Standard L<Module::Build> process for building and installing modules:
544  
545 perl Build.PL
546 ./Build
547 ./Build test
548 ./Build install
549  
550 =head1 Author
551  
552 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
553  
554 L<http://www.appaapps.com|http://www.appaapps.com>
555  
556 =head1 Copyright
557  
558 Copyright (c) 2016-2018 Philip R Brenan.
559  
560 This module is free software. It may be used, redistributed and/or modified
561 under the same terms as Perl itself.
562  
563 =cut
564  
565  
566  
567 # Tests and documentation
568  
569 sub test
570 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=5 SCAR=16.2
Pod undocumented
571 1 binmode($_, ":utf8") for *STDOUT, *STDERR;
572 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
573 1 my $s = eval "join('', <${p}::DATA>)";
574 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
575 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
576 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
577 }
578  
579 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
580  
581 1 1;
582 # podDocumentation
583 __DATA__
↑ Summary Build »