| 1 | package Graphics::Penplotter::GcodeXY::Swirl v0.9.4; | ||
| 2 | |||
| 3 | use v5.38.2; | ||
| 4 | use feature qw( signatures ); | ||
| 5 | no warnings qw( experimental::signatures ); | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Role::Tiny; | ||
| 9 | |||
| 10 | # --------------------------------------------------------------------------- | ||
| 11 | # Graphics::Penplotter::GcodeXY::Swirl | ||
| 12 | # | ||
| 13 | # Role providing whirl (pursuit-curve polygon) generation for GcodeXY. | ||
| 14 | # Based on: Kerry Mitchell, "Fun with Whirls", Bridges 2015, pp. 175-182. | ||
| 15 | # --------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | requires qw( _croak line polygon ); | ||
| 18 | |||
| 19 | # --------------------------------------------------------------------------- | ||
| 20 | # Direction constants (also exposed as exportable package scalars) | ||
| 21 | # --------------------------------------------------------------------------- | ||
| 22 | |||
| 23 | our $SWIRL_CW = 0; # clockwise (default) | ||
| 24 | our $SWIRL_CCW = 1; # counter-clockwise | ||
| 25 | |||
| 26 | # =========================================================================== | ||
| 27 | # PUBLIC API | ||
| 28 | # =========================================================================== | ||
| 29 | |||
| 30 | =for comment | ||
| 31 | swirl( points => \@pts, # flat [x0,y0, x1,y1, ...] array (compulsory) | ||
| 32 | d => \@d, # per-edge advance percentages (compulsory) | ||
| 33 | direction => 0|1, # 0=CW (default), 1=CCW | ||
| 34 | draw => \@bool, # per-edge draw flags (default: all 1) | ||
| 35 | iterations => $n, # stop after $n nested polygons (highest precedence) | ||
| 36 | min_size => $pct, # stop when first edge shrinks to $pct% of original | ||
| 37 | ) | ||
| 38 | =cut | ||
| 39 | |||
| 40 | sub swirl ($self, %args) { | ||
| 41 | |||
| 42 | # ----------------------------------------------------------------------- | ||
| 43 | # 1. Required parameters | ||
| 44 | # ----------------------------------------------------------------------- | ||
| 45 | my $pts_ref = $args{points} | ||
| 46 | or $self->_croak('swirl: "points" is required'); | ||
| 47 | my $d_ref = $args{d} | ||
| 48 | or $self->_croak('swirl: "d" (per-edge percentages) is required'); | ||
| 49 | |||
| 50 | # ----------------------------------------------------------------------- | ||
| 51 | # 2. Optional parameters with defaults | ||
| 52 | # ----------------------------------------------------------------------- | ||
| 53 | my $direction = $args{direction} // $SWIRL_CW; | ||
| 54 | my $draw_ref = $args{draw}; | ||
| 55 | my $iterations = $args{iterations}; # undef → use size-based termination | ||
| 56 | my $min_size = $args{min_size} // 1.0; # percent of original first-edge length | ||
| 57 | |||
| 58 | # ----------------------------------------------------------------------- | ||
| 59 | # 3. Validate inputs | ||
| 60 | # ----------------------------------------------------------------------- | ||
| 61 | my @pts = @{$pts_ref}; | ||
| 62 | my @d = @{$d_ref}; | ||
| 63 | |||
| 64 | $self->_croak('swirl: "points" must contain an even number of elements (x,y pairs)') | ||
| 65 | if @pts % 2; | ||
| 66 | |||
| 67 | my $n = @pts / 2; # number of vertices | ||
| 68 | $self->_croak('swirl: at least 3 vertices are required') | ||
| 69 | if $n < 3; | ||
| 70 | |||
| 71 | $self->_croak('swirl: "d" must have exactly one entry per edge (same count as vertices)') | ||
| 72 | if @d != $n; | ||
| 73 | |||
| 74 | # Per-edge draw flags — default all true | ||
| 75 | my @draw = defined $draw_ref ? @{$draw_ref} : ( (1) x $n ); | ||
| 76 | $self->_croak('swirl: "draw" must have exactly one entry per edge (same count as vertices)') | ||
| 77 | if @draw != $n; | ||
| 78 | |||
| 79 | $self->_croak('swirl: "direction" must be 0 (clockwise) or 1 (counter-clockwise)') | ||
| 80 | if $direction != $SWIRL_CW && $direction != $SWIRL_CCW; | ||
| 81 | |||
| 82 | # ----------------------------------------------------------------------- | ||
| 83 | # 4. Convert percentage d-values to fractions in [0,1] | ||
| 84 | # ----------------------------------------------------------------------- | ||
| 85 | my @df = map { $_ / 100.0 } @d; | ||
| 86 | |||
| 87 | # ----------------------------------------------------------------------- | ||
| 88 | # 5. Build vertex list as [[x,y], ...] pairs | ||
| 89 | # ----------------------------------------------------------------------- | ||
| 90 | my @verts; | ||
| 91 | for my $i ( 0 .. $n - 1 ) { | ||
| 92 | push @verts, [ $pts[ 2 * $i ], $pts[ 2 * $i + 1 ] ]; | ||
| 93 | } | ||
| 94 | |||
| 95 | # ----------------------------------------------------------------------- | ||
| 96 | # 6. Termination threshold (size-based) | ||
| 97 | # ----------------------------------------------------------------------- | ||
| 98 | my $orig_edge_len = _edge_len( $verts[0], $verts[1] ); | ||
| 99 | # Avoid division-by-zero for degenerate polygons | ||
| 100 | my $threshold = $orig_edge_len > 0 | ||
| 101 | ? $orig_edge_len * $min_size / 100.0 | ||
| 102 | : 0; | ||
| 103 | # Guard against a zero (or near-zero) threshold that would cause a | ||
| 104 | # near-infinite loop when min_size => 0. Clamp to a relative epsilon | ||
| 105 | # of 1e-9 so the loop always terminates within a reasonable number of | ||
| 106 | # iterations (the polygon simply converges to numerical noise). | ||
| 107 | if ( $orig_edge_len > 0 && $threshold < $orig_edge_len * 1e-9 ) { | ||
| 108 | $threshold = $orig_edge_len * 1e-9; | ||
| 109 | } | ||
| 110 | |||
| 111 | # ----------------------------------------------------------------------- | ||
| 112 | # 7. Draw the base (outermost) polygon | ||
| 113 | # ----------------------------------------------------------------------- | ||
| 114 | $self->_swirl_draw_poly( \@verts, \@draw ); | ||
| 115 | |||
| 116 | # ----------------------------------------------------------------------- | ||
| 117 | # 8. Iterate inward | ||
| 118 | # ----------------------------------------------------------------------- | ||
| 119 | my $iter = 0; | ||
| 120 | ITERATE: while (1) { | ||
| 121 | |||
| 122 | # Fixed-iteration termination (highest precedence) | ||
| 123 | last ITERATE if defined $iterations && $iter >= $iterations; | ||
| 124 | |||
| 125 | # Compute next ring of vertices | ||
| 126 | my @new_verts; | ||
| 127 | for my $i ( 0 .. $n - 1 ) { | ||
| 128 | my $j = ( $i + 1 ) % $n; | ||
| 129 | if ( $direction == $SWIRL_CW ) { | ||
| 130 | # CW: new vertex i moves fraction df[i] from verts[i] toward verts[j] | ||
| 131 | push @new_verts, [ | ||
| 132 | $verts[$i][0] + $df[$i] * ( $verts[$j][0] - $verts[$i][0] ), | ||
| 133 | $verts[$i][1] + $df[$i] * ( $verts[$j][1] - $verts[$i][1] ), | ||
| 134 | ]; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | # CCW: new vertex i moves fraction df[i] from verts[j] toward verts[i] | ||
| 138 | push @new_verts, [ | ||
| 139 | $verts[$j][0] + $df[$i] * ( $verts[$i][0] - $verts[$j][0] ), | ||
| 140 | $verts[$j][1] + $df[$i] * ( $verts[$i][1] - $verts[$j][1] ), | ||
| 141 | ]; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | $iter++; | ||
| 146 | @verts = @new_verts; | ||
| 147 | |||
| 148 | # Size-based termination (only when iterations not fixed) | ||
| 149 | unless ( defined $iterations ) { | ||
| 150 | last ITERATE if $orig_edge_len == 0; | ||
| 151 | my $cur_edge_len = _edge_len( $verts[0], $verts[1] ); | ||
| 152 | last ITERATE if $cur_edge_len <= $threshold; | ||
| 153 | } | ||
| 154 | |||
| 155 | # Draw this ring | ||
| 156 | $self->_swirl_draw_poly( \@verts, \@draw ); | ||
| 157 | } | ||
| 158 | |||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | # =========================================================================== | ||
| 163 | # PRIVATE HELPERS | ||
| 164 | # =========================================================================== | ||
| 165 | |||
| 166 | # Draw one ring of the whirl. | ||
| 167 | # Uses polygon() when all edges are active (efficient), otherwise line(). | ||
| 168 | sub _swirl_draw_poly ($self, $verts, $draw) { | ||
| 169 | my $n = scalar @{$verts}; | ||
| 170 | |||
| 171 | # Check whether all edges should be drawn | ||
| 172 | my $all_draw = 1; | ||
| 173 | for my $flag ( @{$draw} ) { | ||
| 174 | unless ($flag) { $all_draw = 0; last; } | ||
| 175 | } | ||
| 176 | |||
| 177 | if ($all_draw) { | ||
| 178 | # polygon() is a moveto + sequence of line()s; include first vertex | ||
| 179 | # at the end to close the ring. | ||
| 180 | my @flat; | ||
| 181 | for my $v ( @{$verts} ) { push @flat, $v->[0], $v->[1]; } | ||
| 182 | push @flat, $verts->[0][0], $verts->[0][1]; # closing point | ||
| 183 | $self->polygon(@flat); | ||
| 184 | $self->stroke(); # flush psegments to currentpage | ||
| 185 | } | ||
| 186 | else { | ||
| 187 | # Draw only the enabled edges individually. | ||
| 188 | for my $i ( 0 .. $n - 1 ) { | ||
| 189 | next unless $draw->[$i]; | ||
| 190 | my $j = ( $i + 1 ) % $n; | ||
| 191 | $self->line( | ||
| 192 | $verts->[$i][0], $verts->[$i][1], | ||
| 193 | $verts->[$j][0], $verts->[$j][1], | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | $self->stroke(); # flush psegments to currentpage | ||
| 197 | } | ||
| 198 | return 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | # Euclidean distance between two [x,y] vertex refs. | ||
| 202 | sub _edge_len ($v1, $v2) { | ||
| 203 | return sqrt( | ||
| 204 | ( $v2->[0] - $v1->[0] ) ** 2 + | ||
| 205 | ( $v2->[1] - $v1->[1] ) ** 2 | ||
| 206 | ); | ||
| 207 | } | ||
| 208 | |||
| 209 | 1; | ||
| 210 | |||
| 211 | __END__ |