Skip to content

Plot traced images: fill outlines, minimum shape size, simplify - #167

Draft
HaikuZen wants to merge 5 commits into
sameer:mainfrom
HaikuZen:traced-image-cleanup
Draft

HaikuZen wants to merge 5 commits into
sameer:mainfrom
HaikuZen:traced-image-cleanup

Conversation

@HaikuZen

Copy link
Copy Markdown

Depends on #166. This branch is built on top of it, so its first commit ("Add fit-to-area scaling and machine bed-size check") appears here too. Only the last four commits are new. Once #166 is merged, this PR will show only those four.

SVGs traced from images (bitmap-to-vector output) are usually made only of filled shapes, often with dithering and far more detail than a pen can draw. svg2gcode currently produces empty g-code and an empty preview for them, because fills without a stroke are skipped (fill() is a TODO in both GCodeTurtle and SvgPreviewTurtle). This adds options to plot them well. All are off by default, so existing conversions are unchanged.

Changes

  1. Draw outlines of filled shapes: fill_as_outline, --fill-as-outline true
    • Filled shapes without a stroke are drawn along their outline. Shapes that already have a stroke are drawn once, as before.
    • It's done in the visitor, so the preview, path optimization, fit to area and the bed check all include the outlines.
  2. Minimum shape size: min_shape_size, --min-shape-size MM
    • Skips paths whose width and height are both below the size. This removes dithering specks.
  3. Simplify: simplify, --simplify MM
    • Flattens each path and simplifies it with Douglas–Peucker, so it stays within MM of the original. This removes tracing wobble.
    • Closed paths are split at their farthest point so they stay closed. Simplified paths are straight lines only.
  4. Selector filter in the web settings
    • Adds a field for the existing selector_filter, validated as you type with a new public validate_selector_filter. An invalid selector disables Save instead of panicking during conversion.
    • Also fixes the web settings always saving selector_filter as None, which dropped a selector imported from a JSON file.
  5. Help: collapsible "How to use" sections in the web settings for the selector filter and the cleanup options. They include an examples table, which I checked against the real selector implementation.

Minimum shape size and simplify are applied by a new CleanupTurtle that wraps the backend turtle, after DpiConvertingTurtle. So both are measured in final millimeters after scaling, and both apply to strokes arriving through the TSP path-optimization route too.

Results on a traced poster

The poster is 241 × 361 mm with 864 filled paths. Its page-sized background was skipped with --selector-filter.

Settings Paths Pen moves G-code
Before – – empty
--fill-as-outline true 1,151 156,331 4.0 MB
+ --min-shape-size 2 --simplify 0.3 1,053 11,322 326 KB

Simplify removes 93% of the moves, while the drawn length barely changes (38.4 → 37.8 m).

Known limitations

  • Fit to area (Add fit-to-area scaling and a machine bed-size check #166) computes its scale before minimum shape size removes paths, so a dropped speck at the edge can leave the result slightly smaller than the area.
  • Shapes are simplified separately, so an edge shared by two traced regions can come out as two lines a fraction of a mm apart. A smaller value avoids this.
  • Simplified paths don't use G2/G3.

Testing

  • New tests:
    • fill as outline: the output equals the same SVG with explicit strokes, and is unchanged when the option is off
    • selector validation
    • simplification: wobble removed with corners kept, closed paths stay closed, curves stay within epsilon
    • minimum size: by width or height
  • cargo test --workspace passes, nightly cargo fmt --check passes, and nightly clippy passes with -D warnings for every feature combination, plus the CLI and web crates.
  • The web app builds for wasm32-unknown-unknown and was tested deployed.

🤖 Generated with Claude Code

HaikuZen and others added 5 commits November 29, 2026 21:18
Fit to area (`fit_to_area`, `--fit-to-area W,H`):
- Uniformly scales the drawing so its plotted strokes fit within W x H mm,
  preserving aspect ratio, and places its corner at `origin`.
- Either axis may be left empty to leave it unconstrained.
- Measures strokes only, since fills and images are not plotted.
- `fit_shrink_only` / `--fit-shrink-only true` never enlarges drawings.

Bed-size check (`machine.bed_size`, `--bed-size W,H`):
- After conversion, checks every stroke against a bed from (0,0) to W x H mm.
- `bed_check` is `error` by default (no g-code is written, exit code 1);
  `warn` / `--bed-check warn` writes g-code and prints a warning.
- Error messages report how far the drawing overshoots each edge.
- New `svg_to_gcode_with_bounds` and `check_bed_bounds` in the library;
  `svg_to_gcode` is unchanged.

Web UI: new settings fields for fit area, shrink-only, bed size and
warn-only; out-of-bed drawings show an alert and are skipped unless
warn-only is enabled.

Settings without the new fields load unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Filled shapes without a stroke were silently ignored, so SVGs traced
from images (often made only of filled shapes) produced empty g-code
and an empty preview.

`fill_as_outline` (`--fill-as-outline true`, or the "Draw outlines of
filled shapes" checkbox in the web settings) traces the outline of such
shapes instead. Shapes that already have a stroke are drawn once, as
before. Since the outlines are created while reading the SVG, path
optimization, fit to area and the bed check all include them.

Off by default, so existing conversions are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The web settings had no way to set `selector_filter`, and always saved
it as empty, so a selector imported from a JSON settings file was
silently dropped on the next save.

Adds a "Selector filter" field that loads and saves the setting. It is
validated as you type using the new `validate_selector_filter`, so an
invalid selector disables Save instead of panicking during conversion.
An empty field disables filtering.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SVGs traced from images often contain dithering specks and detail far
finer than a pen can draw, which turn into meaningless short lines and
huge g-code files.

- `min_shape_size` (`--min-shape-size MM`): skips paths whose width and
  height are both below the given size.
- `simplify` (`--simplify MM`): flattens each path and simplifies it
  with Douglas-Peucker so it stays within the given distance of the
  original. Closed paths are split at their farthest point so they stay
  closed. Simplified paths are straight lines only.

Both are applied by a new CleanupTurtle just before the output backend,
so they are measured in final millimeters after scaling, and apply to
the g-code, the web preview and the bed check, with or without path
optimization. Both are off by default; the web settings get a field for
each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds collapsible "How to use" sections to the web settings, closed by
default:

- Selector filter: an examples table (skip by id, one layer only, skip
  a group, skip white shapes, skip by class, skip a page background),
  how to find ids and attributes in Inkscape or a browser, escaping ids
  with special characters, and which selector features are supported.
- Minimum shape size and simplify: what each does, suggested values,
  side effects, and a starting point for traced images.

Adds a reusable `Help` component and its styling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant