Actions Status

NAME

GcodeXY - Produce gcode files for pen plotters from Perl

SYNOPSIS

use Graphics::Penplotters::GcodeXY;
# create a new GcodeXY object
$g = new Graphics::Penplotters::GcodeXY( papersize => "A4", units => "in");
# draw some lines and other shapes
$g->line(1,1, 1,4);
$g->box(1.5,1, 2,3.5);
$g->polygon(1,1, 1,2, 2,2, 2,1, 1,1);
# write the output to a file
$g->output("file.gcode");

DESCRIPTION

GcodeXY provides a method for generating gcode for pen plotters (hence the XY) from Perl. It has graphics primitives that allow arcs, lines, polygons, and rectangles to be drawn as line segments. Units used can be specified ("mm" or "in" or "pt"). The default unit is an inch, which is used internally. Other units are scaled accordingly. The only gcode commands generated are G00 and G01. Fonts are supported, SVG input is possible, and Postscript output can be generated as well. Three dimensional mappings are available too.

DEPENDENCIES

This module requires Math::Bezier, and Math::Trig. For SVG import you will need Image::SVG::Transform and XML::Parser and Image::SVG::Path and POSIX and List::Util and Font::FreeType.

CONSTRUCTOR

OBJECT METHODS

Unless otherwise specified, object methods return 1 for success or 0 in some error condition (e.g. insufficient arguments).

3D METHODS

SYNOPSIS

$g->gsave();                              # saves both 2-D and 3-D state
$g->initmatrix3();                        # reset 3-D CTM
$g->translate3(50, 50, 0);                # move 3-D origin
$g->rotate3(axis => [0,0,1], deg => 45);  # spin around Z
$g->scale3(10);                           # uniform scale

my $m = $g->sphere(0, 0, 0, 1, 12, 24);   # UV sphere mesh
my $s = $g->flatten_to_2d($m);            # project to 2-D edge list
$g->draw_polylines($s);                   # draw via host pen hooks

$g->grestore();
$g->output('myplot.gcode');

Camera-aware rendering:

$g->set_camera(
    eye    => [5, 5, 10],   # camera position in world space
    center => [0, 0,  0],   # point to look at
    up     => [0, 1,  0],   # world up hint
);
$g->camera_to_ctm();        # bake view matrix into the 3-D CTM

my $m   = $g->sphere(0, 0, 0, 1);
my $vis = $g->backface_cull($m);        # uses stored fwd automatically
$g->draw_polylines($g->flatten_to_2d(
    { verts => $m->{verts},
      faces => [ @{$m->{faces}}[@$vis] ] }
));

CTM and transforms

3-D current point

Point transformation

3-D drawing primitives

Wireframe solid drawing (draw directly, no mesh returned)

Mesh-returning solid primitives

All of the following return a mesh structure { verts => \@v, faces => \@f } which can be passed to flatten_to_2d, hidden_line_remove, mesh_to_obj, etc.

Quaternions

Mesh utilities

Visibility

2-D output

Mesh I/O

Camera

The camera and perspective methods together provide a gluLookAt- and gluPerspective-style workflow for positioning the viewer and projecting the scene in 3-D space. Camera and projection state is saved and restored by gsave() / grestore() alongside the 3-D CTM and current point.

Typical full workflow:

$g->initmatrix3();
$g->set_camera(eye => [5,5,10], center => [0,0,0]);
$g->camera_to_ctm();
$g->set_perspective(fov => 45, aspect => 1.0, near => 0.1, far => 100);
$g->perspective_to_ctm();
# All drawing calls now produce perspective-foreshortened output.

Numeric configuration

Mesh representation

All solid primitives that return a mesh use the structure:

{ verts => \@v, faces => \@f }

where @v is an array of [$x,$y,$z] position arrayrefs and @f is an array of [$i0,$i1,$i2] triangle index arrayrefs. Winding order is counter-clockwise when viewed from the outside (right-hand normal pointing outward).

ANAMORPHIC METHODS

An anamorphic image is a distorted drawing which, when viewed from a specific vantage point via a curved mirror, appears undistorted. The methods in this section implement the cylindrical convex mirror variant. The caller first builds a segment path using any drawing primitives (including importsvg), then calls anamorphic to replace that path with its distorted counterpart such that an observer at the configured viewpoint sees the original image when looking at the mirror.

See Graphics::Penplotter::GcodeXY::Anamorphic for the full description of the physical model and image coordinate convention.

SWIRL METHODS

A swirl (also called pursuit-curve polygon) is produced by iteratively constructing a series of nested polygons where each new vertex lies a fixed fractional distance along an edge of the enclosing polygon. The corners of successive polygons trace discrete approximations to logarithmic spirals.

The role is composed automatically when Graphics::Penplotter::GcodeXY is loaded; no extra use statement is required in user code.

See Graphics::Penplotter::GcodeXY::Swirl for the construction algorithm and termination conditions.

Swirl package variables

OPTIMIZE

A peephole optimiser applied automatically to the internal segment queue before gcode generation. No user-facing API change is needed; the optimiser runs transparently via _flushPsegments.

The optimiser makes a single pass over the psegments array, matching named patterns (tested longest-first) and rewriting or deleting redundant instructions. After each match the window is retracted so that newly formed optimisable sequences are not missed (Tanenbaum-style peephole).

The following object attributes configure its behaviour:

See Graphics::Penplotter::GcodeXY::Optimize for a full description of all recognised patterns.

BUGS AND LIMITATIONS

As noted above, the SVG specification (900 pages) is only partially implemented, and just one layer can be used. I suspect that diagnostics about pen travel distance may not always be correct. Layering is not supported officially, but can be simulated.

SEE ALSO

Graphics::Penplotter::GcodeXY::Geometry2D, Graphics::Penplotter::GcodeXY::Geometry3D, Graphics::Penplotter::GcodeXY::Postscript, Graphics::Penplotter::GcodeXY::SVG, Graphics::Penplotter::GcodeXY::Split, Graphics::Penplotter::GcodeXY::Hatch, Graphics::Penplotter::GcodeXY::Font, Graphics::Penplotter::GcodeXY::Vpype, Graphics::Penplotter::GcodeXY::Optimize, Graphics::Penplotter::GcodeXY::Anamorphic, Graphics::Penplotter::GcodeXY::Swirl

AUTHOR

Albert Koelmans (albert.koelmans@googlemail.com).

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.