clipper2-sys
Rust bindings for Clipper2 (C++) using cxx.
Provides integer Path64 / Paths64 with Clipper64, floating-point PathD / PathsD with ClipperD, ClipperOffset, boolean clipping, simplification, Minkowski operations, and lazy iteration over Clipper2 path blobs. Polygon hierarchy from execute_tree is consumed through PolyCxxPreorderIter preorder iterators without materializing a full Rust tree.
中文简介
本 crate 将 Clipper2 以 C++ 库形式接入 Rust:Clipper64 使用整数坐标,ClipperD 使用双精度坐标,并提供 ClipperOffset 做路径偏移。与 C++ 侧交换几何时使用扁平的 PathsBlob 缓冲;树形裁剪结果可通过 PolyCxxPreorderIter 前序遍历,避免在 Rust 中整棵拷贝多边形树。
Features
- Boolean clip on
Clipper64/ClipperD(Union,Intersection,Difference,Xor, fill rules). - Offset / inflate helpers (
ClipperOffset,Paths64::inflate). Path64/PathDhelpers: area, point-in-polygon, simplify, translate,PathD→Path64conversion.- Optional
execute_tree+ preorder iterator over the nativePolyPathtree.
Requirements
- Rust (edition 2021, as specified in
Cargo.toml). - C++17 toolchain (
clang++,g++, or MSVC) forcxxand the bundled Clipper2 sources. - Linux: typically
libstdc++; macOS:libc++.
Building
Clipper2 sources are built from build.rs together with cpp/clipper2_sys_bridge.cpp.
Examples
Clipper64 — union and lazy closed solution
use ;
let rect = ;
let mut clip = new;
clip.add_subject;
clip.add_clip;
let sol = clip.execute;
let = sol.into_lazy;
assert!;
Clipper64 — collect closed and open paths
use ;
let rect = ;
let mut clip = new;
clip.add_subject;
clip.add_clip;
let sol = clip.execute;
let all: Paths64 = sol.iter_closed.chain.collect;
assert!;
Clipper64 — execute_tree and PolyPath preorder
use ;
let rect = ;
let mut clip = new;
clip.add_subject;
clip.add_clip;
let sol = clip.execute_tree;
let = sol.into_open_and_poly_preorder;
let n = preorder.count;
assert!;
ClipperD — union
use ;
let rect = ;
let mut clip = new;
clip.add_subject;
clip.add_clip;
let sol = clip.execute;
let = sol.into_lazy;
assert!;
ClipperOffset — inflate a square
use ;
let path = new;
let mut co = new;
co.add_path;
let out = co.execute;
assert!;
Path64 — area, point-in-polygon, translate, simplify
use ;
let p = new;
assert!;
assert!;
let t = p.translate;
assert_eq!;
let collinear = new;
let simp = collinear.simplify;
assert!;
Paths64 — inflate helper
use ;
let paths = new;
let grown = paths.inflate;
assert!;
Path64 and Paths64 — Minkowski sum
use ;
let rect = ;
let a = rect;
let b = rect;
let ms = a.minkowski_sum;
assert!;
let many = new;
let ms2 = many.minkowski_sum;
assert!;
PathD — simplify and convert to Path64
use ;
let p = new;
let simplified = p.simplify;
let _: LazyPaths64 = p.to_path64;
assert!;
ClipSolution64 — materialize closed paths
use ;
let rect = ;
let mut clip = new;
clip.add_subject;
clip.add_clip;
let sol = clip.execute;
let closed: Paths64 = sol.to_closed;
assert!;
ClipTreeSolution64 — open paths only (drop polygon tree)
use ;
let rect = ;
let mut clip = new;
clip.add_subject;
clip.add_clip;
let sol = clip.execute_tree;
let _open = sol.into_open_lazy;
Module overview
| Area | Contents |
|---|---|
clipper64 (src/clipper64/) |
Point64, Path64, Paths64, Clipper64 (re-exported at crate root). |
clipperd (src/clipperd/) |
PointD, PathD, PathsD, ClipperD. |
| offset | ClipperOffset for path offset on Path64. |
| poly_path | PolyCxxPreorderIter64 / PolyCxxPreorderIterD for native PolyPath preorder. |
| paths_blob | Conversions between PathsBlob* and Rust path types. |
| cxx_bridge | cxx::bridge definitions and FFI to cpp/. |
Repository layout
| Path | Role |
|---|---|
src/lib.rs |
Crate root: shared enums, re-exports, documentation. |
src/cxx_bridge.rs |
cxx::bridge types and extern "C++" API. |
src/paths_blob.rs |
PathsBlob64 / PathsBlobD ↔ Path64 / PathD. |
src/clipper64/ |
Integer coordinate pipeline. |
src/clipperd/ |
Double-precision pipeline. |
src/offset.rs |
ClipperOffset. |
src/poly_path.rs |
Preorder iterators for C++ PolyPath. |
src/macros.rs |
Shared macro_rules! for paths and blobs. |
cpp/ |
C++ bridge (clipper2_sys_bridge). |
Related projects
- Clipper2 (upstream C++).
- clipper-sys (Clipper1 bindings).
- clipper2c (C layer for Clipper2).
License
This crate is licensed under the MIT License (see LICENSE in this repository).
The bundled Clipper2 C++ library has its own license; refer to the Clipper2 repository for upstream terms.
本仓库中的 Rust 绑定以 MIT 授权;Clipper2 上游 C++ 库的许可请以官方仓库为准。