Trait kurbo::ParamCurveFit

source ·
pub trait ParamCurveFit {
    // Required methods
    fn sample_pt_tangent(&self, t: f64, sign: f64) -> CurveFitSample;
    fn sample_pt_deriv(&self, t: f64) -> (Point, Vec2);
    fn break_cusp(&self, range: Range<f64>) -> Option<f64>;

    // Provided method
    fn moment_integrals(&self, range: Range<f64>) -> (f64, f64, f64) { ... }
}
Expand description

The source curve for curve fitting.

This trait is a general representation of curves to be used as input to a curve fitting process. It can represent source curves with cusps and corners, though if the corners are known in advance, it may be better to run curve fitting on subcurves bounded by the corners.

The trait primarily works by sampling the source curve and computing the position and derivative at each sample. Those derivatives are then used for multiple sub-tasks, including ensuring G1 continuity at subdivision points, computing the area and moment of the curve for curve fitting, and casting rays for evaluation of a distance metric to test accuracy.

A major motivation is computation of offset curves, which often have cusps, but the presence and location of those cusps is not generally known. It is also intended for conversion between curve types (for example, piecewise Euler spiral or NURBS), and distortion effects such as perspective transform.

Note general similarities to ParamCurve but also important differences. Instead of separate eval and evaluation of derivative, have a single sample_pt_deriv method which can be more efficient and also handles cusps more robustly. Also there is no method for subsegment, as that is not needed and would be annoying to implement.

Required Methods§

source

fn sample_pt_tangent(&self, t: f64, sign: f64) -> CurveFitSample

Evaluate the curve and its tangent at parameter t.

For a regular curve (one not containing a cusp or corner), the derivative is a good choice for the tangent vector and the sign parameter can be ignored. Otherwise, the sign parameter selects which side of the discontinuity the tangent will be sampled from.

Generally t is in the range [0..1].

source

fn sample_pt_deriv(&self, t: f64) -> (Point, Vec2)

Evaluate the point and derivative at parameter t.

In curves with cusps, the derivative can go to zero.

source

fn break_cusp(&self, range: Range<f64>) -> Option<f64>

Find a cusp or corner within the given range.

If the range contains a corner or cusp, return it. If there is more than one such discontinuity, any can be reported, as the function will be called repeatedly after subdivision of the range.

Do not report cusps at the endpoints of the range, as this may cause potentially infinite subdivision. In particular, when a cusp is reported and this method is called on a subdivided range bounded by the reported cusp, then the subsequent call should not report a cusp there.

The definition of what exactly constitutes a cusp is somewhat loose. If a cusp is missed, then the curve fitting algorithm will attempt to fit the curve with a smooth curve, which is generally not a disaster will usually result in more subdivision. Conversely, it might be useful to report near-cusps, specifically points of curvature maxima where the curvature is large but still mathematically finite.

Provided Methods§

source

fn moment_integrals(&self, range: Range<f64>) -> (f64, f64, f64)

Compute moment integrals.

This method computes the integrals of y dx, x y dx, and y^2 dx over the length of this curve. From these integrals it is fairly straightforward to derive the moments needed for curve fitting.

A default implementation is proved which does quadrature integration with Green’s theorem, in terms of samples evaulated with sample_pt_deriv.

Implementors§