JFLib

render2d/curve.rs#

Source: lib/render2d/curve.rs

curve -- curves on the CPU (decision 25; docs/RENDER2D.md "Kurven"): Bézier chains, ellipses and splines (B-splines up
to degree 3, rational ones included) as rational Bézier spans in f64, flattened into segments per zoom band within
FLAT_TOL px; where the whole curve would take too many segments, only what lies in view plus a margin is flattened
finely, the rest as the chords of whole sub-spans (they lie in their control hulls, which lie outside the window).

  Curve::bezier / ellipse / spline / polyline, ring    the spans (homogeneous control points relative to an origin)
  flatten(&curve, tol, window, max_leaf, budget)         the points, each with its length along the curve (for dashes)

A span is subdivided at t = 1/2 until its control polygon lies within the tolerance of its chord (the factor of the
bound: a quadratic's curve keeps half its control point's distance, a cubic's three quarters, a rational quadratic
w / (1 + w) in standard form, any other rational one the whole hull); the subdivision depends on the curve and the
tolerance only, so a window keeps the vertices a full flattening would have.  A vertex is evaluated from the span
itself at its dyadic parameter (repeated halving would add its roundings up).  The length at a vertex (for dash
patterns) is the length along the chords, as for a polyline; where a windowed curve's fine part begins, the curve's
arc length there (adaptive Gauss-Legendre on the spans), so that the pattern stays put when the window moves.
/// the flattening's tolerance at the finest scale of a zoom band (px): a chord lies at most this far inside its curve
/// (2/3 of it on average); within a band and its hysteresis at most HYST times it (docs/RENDER2D.md "Kurven")
FLAT_TOL: f64 = 1.0 / 32.0
/// a curve is flattened again when the scale leaves its band [2^b, 2^(b+1)) by more than this factor
HYST: f64 = 1.189_207_115_002_721
/// a curve whose whole flattening takes more segments than this is flattened in view plus a margin only
FULL_MAX: usize = 4096
/// the most segments of one curve (beyond: a coarser tolerance)
CURVE_MAX: usize = 16384
  • struct Span a rational Bézier span of degree 1 to 3: homogeneous control points (w x, w y, w) relative to the curve's origin

    • deg: u8
    • p: [[f64; 3]; 4]
  • struct Curve a curve: its origin (a point of it, so that the spans' numbers stay small), its spans end to end, closed (its end joins its start)

    • origin: [f64; 2]
    • spans: Vec<Span>
    • closed: bool
  • struct Flat the flattened curve: points (absolute), the arc length at each from the curve's start, and for a closed one the length all around (where the closing segment ends)

    • pts: Vec<[f64; 2]>
    • lens: Vec<f64>
    • total: f64
    • closed: bool

impl Span

  • fn line(a: [f64; 2], b: [f64; 2]) -> Span
  • fn eval(&self, t: f64) -> [f64; 2]

impl Curve

  • fn bezier(points: &[[f64; 2]], deg: usize, closed: bool) -> Curve a chain of Bézier pieces: the start, then per piece its control points and its end (degree 2 or 3)

  • fn polyline(points: &[[f64; 2]], closed: bool) -> Curve straight segments through the points (fills' rings; repeated points count once)

  • fn ellipse(center: [f64; 2], radii: [f64; 2], rotation: f64, start: f64, sweep: f64) -> Curve an elliptic arc: the point at the parameter t is centre + R(rotation) (rx cos t, ry sin t); a sweep of a full turn or more is closed. Rational quadratic spans of at most a quarter turn: exact

  • fn spline(deg: usize, points: &[[f64; 2]], weights: &[f64], knots: &[f64], closed: bool) -> Curve a B-spline of degree 1 to 3, rational with weights (empty: all 1), knots (empty: clamped uniform, or periodic uniform when closed), as Bézier spans by knot insertion (Boehm). closed: its end joins its start (a straight segment where they differ). The caller has checked the numbers (Scene::geom)

  • fn ring(mut self) -> Curve a fill's ring: closed, a straight segment from the end back to the start where they differ

  • fn bounds(&self) -> [f64; 4] the curve's box (its control hulls), absolute

  • fn append(&mut self, o: &Curve) appends another curve's spans (a ring of a fill made of pieces): moved to this origin, joined by a straight segment where the other does not start at this one's end

  • fn flatten(c: &Curve, tol: f64, window: Option<[f64; 4]>, max_leaf: f64, budget: usize) -> Option<Flat> the curve's points within tol (drawing units) of it; window (absolute, None: all of it): finely only where the spans meet it, max_leaf the longest chord there; None when it takes more than budget points

  • fn band_of(x: f64) -> i32 floor(log2(x)) for x > 0 (normal numbers), from its exponent

  • fn pow2(b: i32) -> f64 2^b

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