Expand description

simplify-polyline

crates.io total downloads docs.rs status

A nearly line-by-line Rust port of the Javascript simplify-js library.

Examples

use simplify_polyline::*;

fn main() {
    let points = &[
        Point { x: 0.0, y: 0.0 }, Point { x: 1.0, y: 1.0 },
        Point { x: 2.0, y: 2.0 }, Point { x: 3.0, y: 3.0 },
        Point { x: 4.0, y: 4.0 }
    ];
    // alternatively, use the point! macro
    let points = &[
        point!(f64, 0.0, 0.0), point!(f64, 1.0, 1.0), point!(f64, 2.0, 2.0),
        point!(f64, 3.0, 3.0), point!(f64, 4.0, 4.0)
    ];
    // alternatively, use the points! macro
    let points = points![f64, (0.0, 0.0), (1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (4.0, 4.0)];

    // low-quality simplification (fast)
    let new_points = simplify(points, 1.0, false);
    // low-quality simplification (slower)
    let new_points = simplify(points, 1.0, true);
}

Performance

Measurements taken with an AMD Ryzen 7 5800x, in Pop!_OS 22.04.

Test Casesimplify-polylinesimplify-js
1118 Points, Low Quality, Tolerance 116.584 μs52.907 μs
1118 Points, High Quality, Tolerance 130.910 μs85.653 μs
1118 Points, Low Quality, Tolerance 53.987 μs12.840 μs
1118 Points, High Quality, Tolerance 523.215 μs57.901 μs
73752 Points, Low Quality, Tolerance 182.251 μs273.075 μs
73752 Points, High Quality, Tolerance 11974.004 μs5376.344 μs
73752 Points, Low Quality, Tolerance 554.150 μs181.554 μs
73752 Points, High Quality, Tolerance 51460.742 μs3921.569 μs

Contributing

Running tests:

$ cargo test --features tests

Running benchmarks:

$ cargo +nightly bench --features tests

Macros

Creates a Point struct, used for input and output for simplify. A type should be specified as the first argument that implements the ExtendedNumOps trait.
Creates a &[Point] array, used for used for input and output for simplify. A type should be specified as the first argument that implements the ExtendedNumOps trait. Point values should be specified as length-2 tuples, where each value matches the input type, in (x, y) order.

Structs

A two-diemensional point, where the value of each coordinate must implement the ExtendedNumOps trait.

Traits

Required traits a number type must implement to be used with simplify.

Functions

Simplifies a polyline within a given tolerance.