Crate clipper2

Source
Expand description

clipper2 is a path/polygon clipping and offsetting library that supports operations like difference, inflate, intersect, point-in-polygon, union, xor, simplify.

The create uses the clipper2c-sys crate that in turn is a Rust wrapper around the C++ version of Clipper2.

The crate exposes the Clipper API in two alternative ways until the best version has been figured out.

  1. Through the Path/Paths struct methods:
  2. Via the plain functions:

The Path/Paths structs also thas some transformation methods such as:

§Examples

use clipper2::*;

let path_a: Paths = vec![(0.2, 0.2), (6.0, 0.2), (6.0, 6.0), (0.2, 6.0)].into();
let path_b: Paths = vec![(5.0, 5.0), (8.0, 5.0), (8.0, 8.0), (5.0, 8.0)].into();

let output: Vec<Vec<(f64, f64)>> = path_a
    .to_clipper_subject()
    .add_clip(path_b)
    .difference(FillRule::default())
    .expect("Failed difference operation")
    .into();

dbg!(output);

Image displaying the result of the difference example

More examples can be found in the examples directory.

Structs§

Bounds
Represents an area from one min and one max Point.
Centi
Scale by 100. This is the default.
Clipper
The Clipper struct used as a builder for applying boolean operations to paths.
Deci
Scale by 10.
Milli
Scale by 1000.
NoSubjects
A state indicating no subjects and no clips.
One
No scaling.
Path
A collection of points.
Paths
A collection of paths.
Point
XY Point with custom scaler.
WithClips
A state indicating one or more subjects and one or more clips.
WithSubjects
A state indicating one or more subjects and no clips.

Enums§

ClipperError
Errors that can occur during clipper operations.
EndType
The EndType enumerator is only needed when offsetting (inflating/shrinking). It isn’t needed for polygon clipping.
FillRule
The Clipper Library supports 4 filling rules: Even-Odd, Non-Zero, Positive and Negative. These rules are base on the winding numbers (see below) of each polygon sub-region, which in turn are based on the orientation of each path. Orientation is determined by the order in which vertices are declared during path construction, and whether these vertices progress roughly clockwise or counter-clockwise.
JoinType
The JoinType enumeration is only needed when offsetting (inflating/shrinking). It isn’t needed for polygon clipping. It specifies how to manage offsetting at convex angled joins. Concave joins will always be offset with a mitered join.
PathError
Path related errors
PointInPolygonResult
The result indicates whether the point is inside, or outside, or on one of the specified polygon’s edges.

Traits§

ClipperState
The state of the Clipper struct.
PointScaler
The point scaling trait allows to choose a multiplier for the point values at compile time.

Functions§

difference
This function differences closed subject paths from clip paths.
inflate
This function performs both closed path and open path offsetting.
intersect
This function intersects closed subject paths with clip paths.
point_in_polygon
The function result indicates whether the point is inside, or outside, or on one of the specified polygon’s edges.
simplify
This function removes points that are less than the specified epsilon distance from an imaginary line that passes through its 2 adjacent points. Logically, smaller epsilon values will be less aggressive in removing points than larger epsilon values.
union
This function joins a set of closed subject paths, with and without clip paths.
xor
This function ‘XORs’ closed subject paths and clip paths.