Expand description
§polystrata
High-performance, topologically robust 2D polygon clipping, Boolean operations, and self-intersection healing kernel based on a 2D Stratified Tolerance Complex.
- Zero dependencies, native
f64coordinates (no integer grid quantization). - 4-Stage Snapped Consolidation Pipeline:
- Scale-aware vertex tolerance disks (
r₀ = 1e-9 × L) & union-find merging. - Iterative vertex-on-edge T-junction splitting, interior crossing splits, and coincident edge consolidation.
- Planar half-edge spoke sorting (
CCW) and closed face-cycle extraction. - Left-normal winding evaluation +
O(C + E)dual-graph BFS classification and pinch-vertex decomposition.
- Scale-aware vertex tolerance disks (
§Quick Start
use polystrata::{union, intersect, difference, xor, heal, KernelOptions};
let square_a = [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0]];
let square_b = [[5.0, 5.0], [15.0, 5.0], [15.0, 15.0], [5.0, 15.0]];
let opts = KernelOptions::default();
let u = union(&square_a, &square_b, opts);
assert!((u.total_area - 175.0).abs() < 1e-6);
let i = intersect(&square_a, &square_b, opts);
assert!((i.total_area - 25.0).abs() < 1e-6);
let d = difference(&square_a, &square_b, opts);
assert!((d.total_area - 75.0).abs() < 1e-6);
let x = xor(&square_a, &square_b, opts);
assert!((x.total_area - 150.0).abs() < 1e-6);Modules§
Structs§
- Kernel
Options - Configuration options for the 2D Stratified Tolerance Complex kernel.
- Kernel
Result - Output produced by a 2D Boolean or healing operation.
- Point2D
- A 2D point in double-precision (
f64) world coordinates. - Polygon
With Holes2D - A 2D polygon consisting of one counter-clockwise (
CCW) outer shell and zero or more clockwise (CW) interior holes. - Polystrata
Kernel - Reusable 2D Boolean & topological healing kernel owning its own pre-allocated
KernelArena. - Segment2D
- A directed 2D line segment tagged with an optional
body_index.
Enums§
- Boolean
Op - Standard 2-body Boolean operation selector.
- Winding
Rule - Winding rule used to classify the interior of each input body from its signed winding number
w:
Traits§
- Shape
Input - Trait implemented by all supported 2D shape inputs (
[Point2D],[[f64; 2]],[(f64, f64)],PolygonWithHoles2D, slices/vectors of loops or polygons, and[Segment2D]).
Functions§
- boolean_
multi - Execute a general N-body Boolean operation defined by a custom predicate
predicate(&[w₀, w₁, …]) -> boolover the per-body winding vector of each planar face cycle. - clip
- Execute a 2-body Boolean operation and return GeoJSON-style
MultiPolygoncoordinate rings (Vec<Vec<Vec<[f64; 2]>>>), where each ring is explicitly closed (first == last). - difference
- Compute the Boolean difference (
A \ B) of two 2D shapes. - heal
- Topological self-intersection healing & polygonization of a single 2D shape or segment soup.
- intersect
- Compute the Boolean intersection (
A ∩ B) of two 2D shapes. - intersect_
all - Single-pass N-body Boolean intersection (
B₀ ∩ B₁ ∩ … ∩ Bₙ₋₁) across an arbitrary slice of shapes. - signed_
loop_ area - Compute the signed area of a closed 2D loop via the Shoelace formula.
- union
- Compute the Boolean union (
A ∪ B) of two 2D shapes. - union_
all - Single-pass N-body Boolean union (
B₀ ∪ B₁ ∪ … ∪ Bₙ₋₁) across an arbitrary slice of shapes. - wasm_
alloc_ raw_ segments - Allocate / resize the raw input segment buffer (
seg_count * 5f64s:[x0, y0, x1, y1, bodyIndex]) and return a pointer in WASM linear memory where JS can write the segments directly. - wasm_
compute_ cycles_ and_ windings - Run Stages 0–3 plus Stage 4a winding evaluation for custom N-body predicates (
booleanMulti) and interactive pipeline inspection (inspectPipeline). Returnscycle_count. - wasm_
execute_ prepared - Execute Stages 1–4 on the already-prepared normalized segments in
arena. Returns the number of output boundary loops written intoout_loop_metaandout_coords. - wasm_
execute_ raw - Full end-to-end execution (Stages 0–4) from
raw_segments. Returns the number of output boundary loops. - wasm_
extract_ from_ cycle_ keep - Run Stage 4b loop extraction from
arena.cycle_keep[0..cycle_count](populated by JS). Returns the number of output boundary loops. - wasm_
get_ cycle_ count - wasm_
get_ cycle_ keep_ ptr - wasm_
get_ cycle_ sample_ x_ ptr - wasm_
get_ cycle_ sample_ y_ ptr - wasm_
get_ cycle_ windings_ f64_ ptr - wasm_
get_ e_ alive_ ptr - wasm_
get_ e_ delta_ w_ ptr - wasm_
get_ edge_ count - wasm_
get_ ev0_ ptr - wasm_
get_ ev1_ ptr - wasm_
get_ out_ coords_ len - wasm_
get_ out_ coords_ ptr - wasm_
get_ out_ loop_ meta_ ptr - wasm_
get_ out_ total_ area - wasm_
get_ prep_ body_ count - wasm_
get_ prep_ cx - wasm_
get_ prep_ cy - wasm_
get_ prep_ diagonal - wasm_
get_ prep_ r0 - wasm_
get_ prep_ rmax - wasm_
get_ v_ alive_ ptr - wasm_
get_ vertex_ count - wasm_
get_ vr_ ptr - wasm_
get_ vx_ ptr - wasm_
get_ vy_ ptr - wasm_
prepare_ segments - Stage 0 preparation: normalize the
seg_countsegments already written intoraw_segments. Used by benchmark harnesses to pre-normalize segments before timed execution. - xor
- Compute the Boolean symmetric difference (
A ⊕ B) of two 2D shapes.
Type Aliases§
- Loop2D
- A closed 2D polygonal ring represented as an ordered list of vertices
(the closing edge from
lasttofirstis implicit).