Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

linestring2bezier

crates.io version docs.rs docs

A Rust crate for converting a geo_types::LineString<T: CoordFloat> into a piecewise cubic Bezier spline, and vice versa. The conversion is controlled by a maximum allowed error, ensuring the Bezier approximation stays within a specified distance from the original line string.

Features

  • LineString to Bezier: Approximate a polyline as a sequence of cubic Bezier segments (BezierString).
  • Bezier to LineString: Approximate a Bezier spline as a polyline within a given error.
  • Error control: Specify the maximum deviation allowed between the original and the approximation.
  • Handles straight lines: Segments with no curvature are represented as straight lines.

Types

  • BezierSegment<T: CoordFloat>: Either a cubic Bezier or a straight line.
    • For Bezier: start, handle1, handle2, end (geo_types::Coord<T>)
    • For Line: start, end (geo_types::Coord<T>)
  • BezierString<T: CoordFloat>: A vector of BezierSegment<T>.

Example

use geo_types::{Coord, LineString};
use linestring2bezier::{BezierString};

let line = LineString(vec![
		Coord { x: 0.0, y: 0.0 },
		Coord { x: 1.0, y: 2.0 },
		Coord { x: 2.0, y: 0.0 },
]);

let bezier = BezierString::from_line_string(line, 0.1).unwrap();
let approx_line = bezier.to_line_string(0.1).unwrap();

to_line_string borrows the Bézier geometry, so it can be flattened repeatedly without cloning it. Use to_line_string_with_segment_ends when the flattened coordinates must remain associated with their original Bézier segments:

# use geo_types::{Coord, LineString};
# use linestring2bezier::BezierString;
# let line = LineString(vec![
#     Coord { x: 0.0, y: 0.0 },
#     Coord { x: 1.0, y: 2.0 },
#     Coord { x: 2.0, y: 0.0 },
# ]);
# let bezier = BezierString::from_line_string(line, 0.1).unwrap();
let (approx_line, segment_end_indices) =
    bezier.to_line_string_with_segment_ends(0.1).unwrap();

Each segment-end index identifies that segment's final coordinate in the returned LineString. Empty Bézier strings produce an empty LineString.

Error Handling

  • Returns errors if the error margin is too small, if a LineString has fewer than two coordinates when fitting a Bézier string, or if a Bézier string has disconnected segments when flattening.

Documentation

License

Licensed under the MIT license (LICENSE.md).

About

Convert a geo_types::LineString<T: CoordFloat> into a piecewise cubic bezier

Resources

Stars

Watchers

Forks

Used by

Contributors

Languages