Skip to main content

Crate polystrata

Crate polystrata 

Source
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 f64 coordinates (no integer grid quantization).
  • 4-Stage Snapped Consolidation Pipeline:
    1. Scale-aware vertex tolerance disks (r₀ = 1e-9 × L) & union-find merging.
    2. Iterative vertex-on-edge T-junction splitting, interior crossing splits, and coincident edge consolidation.
    3. Planar half-edge spoke sorting (CCW) and closed face-cycle extraction.
    4. Left-normal winding evaluation + O(C + E) dual-graph BFS classification and pinch-vertex decomposition.

§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§

arena
consolidate
grid
topology

Structs§

KernelOptions
Configuration options for the 2D Stratified Tolerance Complex kernel.
KernelResult
Output produced by a 2D Boolean or healing operation.
Point2D
A 2D point in double-precision (f64) world coordinates.
PolygonWithHoles2D
A 2D polygon consisting of one counter-clockwise (CCW) outer shell and zero or more clockwise (CW) interior holes.
PolystrataKernel
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§

BooleanOp
Standard 2-body Boolean operation selector.
WindingRule
Winding rule used to classify the interior of each input body from its signed winding number w:

Traits§

ShapeInput
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₁, …]) -> bool over the per-body winding vector of each planar face cycle.
clip
Execute a 2-body Boolean operation and return GeoJSON-style MultiPolygon coordinate 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 * 5 f64s: [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). Returns cycle_count.
wasm_execute_prepared
Execute Stages 1–4 on the already-prepared normalized segments in arena. Returns the number of output boundary loops written into out_loop_meta and out_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_count segments already written into raw_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 last to first is implicit).