A Rust library for parsing SVG path commands and generating discrete points along the path. This tool transforms SVG path commands into a series of points that can be used for visualization or further processing.
- Parses standard SVG path commands (M, L, H, V, Q, C and their lowercase variants)
- Transforms all commands into cubic or quadratic Bézier curves
- Converts SVG coordinates to Cartesian coordinate system
- Calculates path lengths using Gaussian quadrature
- Generates evenly distributed points along the path
- Removes duplicate points for cleaner output
- Exports points to CSV format
- Includes Python integration for visualization
- Ensure you have Rust installed on your system. If not, install it from rustup.rs
- Clone this repository:
git clone [repository-url]
cd [repository-name]
The project uses the following Rust standard library components:
std::collections::HashSet
std::fs::File
std::hash::{Hash, Hasher}
std::io::Write
std::process::Command
fn main() {
// Create an SVG path string
let path = "M 20.5 50.0 L 100.0 50.0 Q 300.0 200.0 350.0 150.0";
// Initialize the path processor
let mut path_processor = Path::init(path);
// Generate points along the path
path_processor.get_points();
// Get the resulting points
let points = path_processor.points;
// Save points to CSV
let _error = save_points_to_file(points, "points.csv");
// Run visualization script
let _error = run_python_script();
}
The library supports the following SVG path commands:
M/m
: Move toL/l
: Line toH/h
: Horizontal lineV/v
: Vertical lineQ/q
: Quadratic Bézier curveC/c
: Cubic Bézier curve
Points are generated along the path using the following process:
- Commands are synthesized into cubic or quadratic Bézier curves
- Coordinates are transformed to the Cartesian system
- Arc lengths are calculated using Gaussian quadrature
- Points are distributed proportionally to segment lengths
- Duplicate points are removed
The library uses 7-point Gaussian quadrature for accurate path length calculations:
const GAUSS_POINTS: [(f64, f64); 7] = [
(-0.949107912342759, 0.129484966168870),
(-0.741531185599394, 0.279705391489277),
(-0.405845151377397, 0.381830050505119),
(0.000000000000000, 0.417959183673469),
(0.405845151377397, 0.381830050505119),
(0.741531185599394, 0.279705391489277),
(0.949107912342759, 0.129484966168870),
];
Points are implemented with floating-point tolerance for comparison and hashing:
#[derive(Debug, Clone, Copy)]
struct Point {
x: f64,
y: f64,
}
The program generates two types of output:
- A CSV file containing the generated points
- A visualization (requires the accompanying Python script)
Contributions are welcome! Some areas for potential improvement:
- Additional SVG path commands support
- More sophisticated point distribution algorithms
- Better error handling
- Additional output formats
- Enhanced visualization options