PlanePath/HilbertCurve.pm

filter: Statementstmt 86.9% 113 / 130 Branchbran 81.6% 49 / 60 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 86.6% 13 / 15 Podpod 100% 5 / 5 totaltotal 83.1% 188 / 226 CC 37 SCAR 25.2
CC
37
Cov
84%
rect_to_n_range
21.2
n_to_level
12.0
xy_to_n
10.1
CRAP 12.4
« PlanePath/KochSnowflakes.pm ↑ Summary PlanePath/CellularRule.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 # http://www.woollythoughts.com/afghans/peano.html
20 # Knitting
21 #
22 # http://www.geom.uiuc.edu/docs/reference/CRC-formulas/node36.html
23 # Closed path, curved parts
24 #
25 # http://www.wolframalpha.com/entities/calculators/Peano_curve/jh/4o/im/
26 # Curved corners tilted to a diamond, or is it an 8-step pattern?
27 #
28 # http://www.davidsalomon.name/DC2advertis/AppendC.pdf
29 #
30 # http://www.cut-the-knot.org/do_you_know/hilbert.shtml
31 # Java applet
32 #
33  
34 package Math::PlanePath::HilbertCurve;
35 5 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 #use List::Util 'max','min';
38 5 *max = \&Math::PlanePath::_max;
39  
40 5 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 $VERSION = 128;
42 5 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use Math::PlanePath::Base::NSEW;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 @ISA = ('Math::PlanePath::Base::NSEW',
45 'Math::PlanePath');
46  
47 use Math::PlanePath::Base::Generic
48 5 'is_infinite',
49 5 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 use Math::PlanePath::Base::Digits
51 5 'round_down_pow',
52 'round_up_pow',
53 'bit_split_lowtohigh',
54 'digit_split_lowtohigh',
55 5 ▶ 'digit_join_lowtohigh';
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 # uncomment this to run the ### lines
58 #use Smart::Comments;
59  
60  
61 5 ▶use constant n_start => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 5 ▶use constant class_x_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 5 ▶use constant class_y_negative => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 5 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
65  
66  
67 #------------------------------------------------------------------------------
68  
69 # state=0 3--2 plain
70 # |
71 # 0--1
72 #
73 # state=4 1--2 transpose
74 # | |
75 # 0 3
76 #
77 # state=8
78 #
79 # state=12 3 0 rot180 + transpose
80 # | |
81 # 2--1
82 #
83 # generated by tools/hilbert-curve-table.pl
84 5 my @next_state = (4,0,0,12, 0,4,4,8, 12,8,8,4, 8,12,12,0);
85 5 my @digit_to_x = (0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0);
86 5 my @digit_to_y = (0,0,1,1, 0,1,1,0, 1,1,0,0, 1,0,0,1);
87 5 my @yx_to_digit = (0,1,3,2, 0,3,1,2, 2,3,1,0, 2,1,3,0);
88 5 my @min_digit = (0,0,1,0, 0,1,3,2, 2,undef,undef,undef,
89 0,0,3,0, 0,2,1,1, 2,undef,undef,undef,
90 2,2,3,1, 0,0,1,0, 0,undef,undef,undef,
91 2,1,1,2, 0,0,3,0, 0);
92 5 my @max_digit = (0,1,1,3, 3,2,3,3, 2,undef,undef,undef,
93 0,3,3,1, 3,3,1,2, 2,undef,undef,undef,
94 2,3,3,2, 3,3,1,1, 0,undef,undef,undef,
95 2,2,1,3, 3,1,3,3, 0);
96  
97 sub n_to_xy {
98 128 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=7 SCAR=19.6
Pod documented
99 ### HilbertCurve n_to_xy(): $n
100 ### hex: sprintf "%#X", $n
101  
102 128 ▶ if ($n < 0) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 128 128
103 128 ▶ if (is_infinite($n)) { return ($n,$n); }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 128 128
104  
105 128 my $int = int($n);
106 128 $n -= $int; # fraction part
107  
108 128 my @ndigits = digit_split_lowtohigh($int,4);
109 128 ▶ my $state = ($#ndigits & 1 ? 4 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$#ndigits & 1 ? : 66 62 128
110 128 ▶ my $dirstate = ($#ndigits & 1 ? 0 : 4); # default if all $ndigit==3
Branch: 1 branch
BranchTrueFalseTotal
$#ndigits & 1 ? : 66 62 128
111  
112 128 my (@xbits, @ybits);
113 128 foreach my $i (reverse 0 .. $#ndigits) { # digits high to low
114 580 my $ndigit = $ndigits[$i];
115 580 $state += $ndigit;
116 580 ▶ if ($ndigit != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigit != 3) 414 166 580
117 414 $dirstate = $state; # lowest non-3 digit
118 }
119  
120 580 $xbits[$i] = $digit_to_x[$state];
121 580 $ybits[$i] = $digit_to_y[$state];
122 580 $state = $next_state[$state];
123 }
124  
125 128 my $zero = ($int * 0); # inherit bigint 0
126 128 return ($n * ($digit_to_x[$dirstate+1] - $digit_to_x[$dirstate]) # frac
127 + digit_join_lowtohigh (\@xbits, 2, $zero),
128  
129 $n * ($digit_to_y[$dirstate+1] - $digit_to_y[$dirstate]) # frac
130 + digit_join_lowtohigh (\@ybits, 2, $zero));
131 }
132  
133 sub xy_to_n {
134 232 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=9 SCAR=23.1
Pod documented
135 ### HilbertCurve xy_to_n(): "$x, $y"
136  
137 232 $x = round_nearest ($x);
138 232 ▶ if (is_infinite($x)) { return $x; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($x)) 0 232 232
139 232 $y = round_nearest ($y);
140 232 ▶ if (is_infinite($y)) { return $y; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($y)) 0 232 232
141  
142 232 ▶ if ($x < 0 || $y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 0 or $y < 0) 0 232 232
Condition33%1/3
$x < 0 or $y < 0
l!l&&r!l&&!r
00232
MC/DC0%0/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table33%1/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
143 0 return undef;
144 }
145  
146 232 my @xbits = bit_split_lowtohigh($x);
147 232 my @ybits = bit_split_lowtohigh($y);
148 232 my $numbits = max($#xbits,$#ybits);
149  
150 232 my @ndigits;
151 232 ▶ my $state = ($numbits & 1 ? 4 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$numbits & 1 ? : 145 87 232
152 232 foreach my $i (reverse 0 .. $numbits) { # high to low
153 ### at: "state=$state xbit=".($xbits[$i]||0)." ybit=".($ybits[$i]||0)
154 1385 ▶ my $ndigit = $yx_to_digit[$state + 2*($ybits[$i]||0) + ($xbits[$i]||0)];
Condition100%4/4
$ybits[$i] || 0
l!l
703682
$xbits[$i] || 0
l!l
729656
MC/DC100%1/1
$ybits[$i] || 0
$ybits[$i]
MC/DC100%1/1
$xbits[$i] || 0
$xbits[$i]
Truth table100%2/2
A
A= $ybits[$i]
Aresult
11
00
Truth table100%2/2
A
A= $xbits[$i]
Aresult
11
00
155 1385 $ndigits[$i] = $ndigit;
156 1385 $state = $next_state[$state+$ndigit];
157 }
158 ### @ndigits
159 232 return digit_join_lowtohigh(\@ndigits, 4,
160 $x * 0 * $y); # inherit bignum 0
161 }
162  
163  
164 # rect_to_n_range() finds the exact minimum/maximum N in the given rectangle.
165 #
166 # The strategy is similar to xy_to_n(), except that at each bit position
167 # instead of taking a bit of x,y from the input instead those bits are
168 # chosen from among the 4 sub-parts according to which has the maximum N and
169 # is within the given target rectangle. The final result is both an $n_max
170 # and a $x_max,$y_max which is its position, but only the $n_max is
171 # returned.
172 #
173 # At a given sub-part the comparisons ask whether x1 is above or below the
174 # midpoint, and likewise x2,y1,y2. Since x2>=x1 and y2>=y1 there's only 3
175 # combinations of x1>=cmp,x2>=cmp, not 4.
176  
177 # exact
178 sub rect_to_n_range {
179 78 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=21 SCAR=30.5
Pod documented
180 ### HilbertCurve rect_to_n_range(): "$x1,$y1, $x2,$y2"
181  
182 78 $x1 = round_nearest ($x1);
183 78 $y1 = round_nearest ($y1);
184 78 $x2 = round_nearest ($x2);
185 78 $y2 = round_nearest ($y2);
186 78 ▶ ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $x2 34 44 78
187 78 ▶ ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 > $y2 30 48 78
188  
189 78 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 78 78
Condition33%1/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
0078
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table33%1/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
190 0 return (1, 0); # rectangle outside first quadrant
191 }
192  
193 78 my $n_min = my $n_max
194 = my $x_min = my $y_min
195 = my $x_max = my $y_max
196 = ($x1 * 0 * $x2 * $y1 * $y2); # inherit bignum 0
197  
198 78 ▶ my ($len, $level) = round_down_pow (($x2 > $y2 ? $x2 : $y2),
Branch: 1 branch
BranchTrueFalseTotal
$x2 > $y2 ? : 35 43 78
199 2);
200 ### $len
201 ### $level
202 78 ▶ if (is_infinite($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($level)) 0 78 78
203 0 return (0, $level);
204 }
205 78 ▶ my $min_state = my $max_state = ($level & 1 ? 4 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$level & 1 ? : 46 32 78
206  
207 78 ▶ while ($level >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level >= 0) 356 78 434
208 {
209 356 my $x_cmp = $x_min + $len;
210 356 my $y_cmp = $y_min + $len;
211 356 ▶ my $digit = $min_digit[3*$min_state
Branch: 4 branches
BranchTrueFalseTotal
$x1 >= $x_cmp ? : 144 212 356
$x2 >= $x_cmp ? : 79 133 212
$y1 >= $y_cmp ? : 130 226 356
$y2 >= $y_cmp ? : 76 150 226
212 + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
213 + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)];
214  
215 356 $n_min = 4*$n_min + $digit;
216 356 $min_state += $digit;
217 356 ▶ if ($digit_to_x[$min_state]) { $x_min += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_x[$min_state]) 161 195 356
218 356 ▶ if ($digit_to_y[$min_state]) { $y_min += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_y[$min_state]) 143 213 356
219 356 $min_state = $next_state[$min_state];
220 }
221 {
222 356 my $x_cmp = $x_max + $len;
223 356 my $y_cmp = $y_max + $len;
224 356 ▶ my $digit = $max_digit[3*$max_state
Branch: 4 branches
BranchTrueFalseTotal
$x1 >= $x_cmp ? : 131 225 356
$x2 >= $x_cmp ? : 72 153 225
$y1 >= $y_cmp ? : 120 236 356
$y2 >= $y_cmp ? : 68 168 236
225 + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0)
226 + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)];
227  
228 356 $n_max = 4*$n_max + $digit;
229 356 $max_state += $digit;
230 356 ▶ if ($digit_to_x[$max_state]) { $x_max += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_x[$max_state]) 181 175 356
231 356 ▶ if ($digit_to_y[$max_state]) { $y_max += $len; }
Branch: 1 branch
BranchTrueFalseTotal
if ($digit_to_y[$max_state]) 162 194 356
232 356 $max_state = $next_state[$max_state];
233 }
234  
235 356 $len = int($len/2);
236 356 $level--;
237 }
238  
239 78 return ($n_min, $n_max);
240 }
241  
242 #------------------------------------------------------------------------------
243  
244 # shared by Math::PlanePath::AR2W2Curve and others
245 sub level_to_n_range {
246 0 ▶ my ($self, $level) = @_;
Subroutine level_to_n_range: not called CC=1 SCAR=6.9
Pod documented
247 0 return (0, 4**$level - 1);
248 }
249 sub n_to_level {
250 0 ▶ my ($self, $n) = @_;
Subroutine n_to_level: not called CC=3 SCAR=24.8
Pod documented
251 0 ▶ if ($n < 0) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) 0 0 0
252 0 ▶ if (is_infinite($n)) { return $n; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_infinite($n)) 0 0 0
253 0 $n = round_nearest($n);
254 0 my ($pow, $exp) = round_up_pow ($n+1, 4);
255 0 return $exp;
256 }
257  
258 #------------------------------------------------------------------------------
259 5 1;
260 __END__
« PlanePath/KochSnowflakes.pm ↑ Summary PlanePath/CellularRule.pm »