基于 MoonBit 语言实现的纯算法、轻量级位图转矢量路径规划器。项目支持多颜色通道分层(Color Layering)、Moore-Neighbor 像素边界轮廓追踪(Moore-Neighbor Contour Tracing)、以及基于 Ramer-Douglas-Peucker (RDP) 算法的路径降噪与多边形几何简化,可广泛应用于图形矢量化、机器人路径规划和矢量打印设备等场景。
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
MoonBit Vector Trace Planner (moonbit-vector-trace-plan)
moonbit-vector-trace-planis a high-performance, pure MoonBit bitmap-to-vector path tracing and planning library. It processes pixel-based grid images, clusters colors into layers, traces boundary contours, simplifies curves, and produces structured vector path intermediate representations (IR) ready for SVG serialization or target machine path planning.This project is built for the MoonBit Hackathon 2026 to demonstrate robust algorithm design, standard-compliant package structure, and complete test coverage using MoonBit v0.10.3.
Project Structure
The project is split into several modular packages:
lib/core: Defines the foundational data models and serialization helpers.Point: Represents a 2D coordinate(x, y).Image: Represents the input bitmap grid with width, height, and color pixels.PathSegment: Vector path segment enum supportingMoveTo,LineTo,QuadTo(quadratic Bezier),CubicTo(cubic Bezier), andClosePath.Path: Encapsulates a series of segments.ColorLayer: Holds a list of vector paths mapped to a specific ARGB color value.VectorManifest: The final container holding canvas size and all color layers.d="...") or XML files.lib/trace: Implements image contour detection.image_extract_mask: Extracts binary masks for specific color layers.image_unique_colors: Detects all unique colors in an image.trace_contour: Moore-Neighbor boundary tracing algorithm implementation for pixel-perfect loop extraction.plan_trace: The primary orchestration function linking mask extraction, contour tracing, path simplification, and vector path generation.lib/simplify: Implements curve simplification.rdp: The Ramer-Douglas-Peucker (RDP) algorithm implementation for polygon vertex reduction using a configurable distance thresholdepsilon.cmd/main: Command-line demonstration showing the tracer in action with a mock multi-color bitmap grid, printing the resulting SVG path XML.Key Algorithms
1. Moore-Neighbor Tracing
The library extracts contours using the Moore-Neighbor tracing algorithm guided by Jacob’s stopping criterion. This allows the tracer to track exact boundaries of arbitrary shapes inside a binary grid. It supports complex region configurations, including thin borders and isolated single-pixel segments.
2. Ramer-Douglas-Peucker (RDP) Simplification
Contours traced directly from pixels are jagged and blocky. The RDP algorithm recursively reduces the number of points by calculating the perpendicular distance of vertices from line segments. It simplifies curves when the deviation is below a threshold
epsilon, turning pixelated steps into smooth vector lines.Getting Started
Prerequisites
Ensure you have the latest MoonBit toolchain installed (v0.10.3 or higher is recommended). Verify your toolchain version with:
Installation
Clone the repository to your local machine:
Type Checking & Formatting
To verify the code is type-safe and formatted correctly:
Running Tests
To run all tests inside the core, trace, and simplify packages:
Running the Demo
To compile and run the CLI demonstration:
The demo will print the structured color layers found in a mock 10x10 grid along with the generated SVG output:
License
This project is licensed under the MIT License - see the LICENSE file for details.