JFLib

render2d/fill.rs#

Source: lib/render2d/fill.rs

fill -- a fill's rings on the CPU (decision 26; docs/RENDER2D.md "Flächen"): cleaned, nested (even-odd: a ring inside
an odd number of others is a hole), oriented (the inside on the left in drawing coordinates: outer rings
counter-clockwise, holes clockwise), and each outer ring with its holes cut into triangles in f64.

  prepare(rings) -> Prepared   the rings as they go up (their points), the triangles as indices into them

The triangulation is ear clipping after earcut (Mapbox, ISC; written anew here): holes bridged into their outer ring
left to right (Eberly's bridge), ears found through a z-order hash for larger rings, collinear and repeated points
filtered, and where no ear is left, small self-intersections cured locally and as a last resort the polygon split by
a valid diagonal.  It only takes the rings' points, never new ones, so every triangle's corner is a ring's point (the
shader finds a corner's two boundary edges through it).  Robust against collinear and repeated points and rings
that touch; self-intersecting rings come out approximately (decision 26).  The numbers are relative to the fill's
first point (small differences, exact orientation tests where the points allow).
  • struct Prepared the rings ready for the records: their points (relative to origin), oriented; triangles as (ring, point) pairs; per triangle and per edge the edges near it (near: numbered ring after ring, nearest first)
    • origin: [f64; 2]
    • rings: Vec<Vec<[f64; 2]>>
    • tris: Vec<[(u32, u32); 3]>
    • tri_near: Vec<Vec<u32>>
    • edge_near: Vec<Vec<u32>>
/// the most edges a triangle's record names (the fragment shader computes its pixels from them)
TRI_NEAR: usize = 20
/// the most edges besides its two neighbours an edge's record names
EDGE_NEAR: usize = 4

impl Prepared

  • fn near(&mut self, d: f64) the edges within d (drawing units) of each triangle and of each edge (other than itself and its neighbours), nearest first, at most TRI_NEAR and EDGE_NEAR (beyond: the nearest ones; the shader is then approximate there)

  • fn prepare(input: &[Vec<[f64; 2]>]) -> Prepared the rings (absolute f64 points, implicitly closed): cleaned, nested, oriented, triangulated

  • fn earcut(data: &[[f64; 2]], starts: &[usize], out: &mut Vec<usize>) triangles of the polygon data (the outer ring, then the holes from starts on) as indices into it

  • fn areas(p: &Prepared) -> (f64, f64) (tests) the triangles' area against the rings' even-odd area, both in units squared

Source: docs/API.md in the JFLib repository.