NAME

Graphics::Penplotter::GcodeXY::Optimize - Segment-queue peephole optimiser for GcodeXY

SYNOPSIS

# The optimiser is called automatically by _flushPsegments; no
# user-facing API change.

DESCRIPTION

A Role::Tiny role providing the peephole optimiser for the internal segment queue of Graphics::Penplotter::GcodeXY.

The optimiser is applied automatically by _flushPsegments immediately before gcode generation. It makes a single pass over the psegments array, matching a set of named patterns and rewriting or deleting redundant instructions. After each match the window is retracted by $LONGEST positions so that newly formed optimisable sequences are not missed (Tanenbaum-style peephole).

The following patterns are recognised (tested longest-first):

Pattern 10

A line immediately followed by a zero-length line and its reverse, then a pen-up and a move. Removes the redundant lines and fixes up the following move's source.

Pattern 1

A move + pendown followed by a line and its reverse + penup (a line drawn twice, common in single-stroke fonts). Replaced by a single line.

Pattern 3

The canonical result of pattern 1: a line followed by PU/move(same)/PD. The PU/move/PD triple is deleted.

Pattern 2

A line followed by PU/move/PD/line(back)/PU, a pen lifted unnecessarily. Merged into two consecutive lines.

Patterns 4 and 11

Two consecutive fast-move triples (PU/move/PD), pattern 4 deletes the second when both land at the same point; pattern 11 merges them when they differ.

Patterns 5 and 8

Adjacent PU/PD or PD/PU pairs, both deleted.

Patterns 6 and 7

Adjacent PU/PU or PD/PD pairs, one deleted.

Pattern 9

A move or line whose source and destination are identical, deleted.

CONFIGURATION

The optimiser respects the following object attributes:

optimize

Set to 0 to disable the optimiser entirely. Default is 1.

check

When set, prints the number of instructions removed to STDOUT.

opt_debug

When set, prints a per-instruction trace to STDOUT.

AUTHOR

Albert Koelmans (albert.koelmans@googlemail.com)

LICENSE

Same terms as Perl itself.