Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add derivative and curvature to PathSeg #197

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/bezpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use crate::mindist::min_dist_param;
use crate::MAX_EXTREMA;
use crate::{
Affine, CubicBez, Line, Nearest, ParamCurve, ParamCurveArclen, ParamCurveArea,
ParamCurveExtrema, ParamCurveNearest, Point, QuadBez, Rect, Shape, TranslateScale, Vec2,
ParamCurveCurvature, ParamCurveDeriv, ParamCurveExtrema, ParamCurveNearest, Point, QuadBez,
Rect, Shape, TranslateScale, Vec2,
};

/// A Bézier path.
Expand Down Expand Up @@ -689,6 +690,39 @@ impl ParamCurveArea for PathSeg {
}
}

impl ParamCurveCurvature for PathSeg {
fn curvature(&self, t: f64) -> f64 {
match *self {
PathSeg::Line(line) => line.curvature(t),
PathSeg::Quad(quad) => quad.curvature(t),
PathSeg::Cubic(cubic) => cubic.curvature(t),
}
}
}

impl ParamCurveDeriv for PathSeg {
type DerivResult = PathSeg;

fn deriv(&self) -> Self::DerivResult {
match *self {
PathSeg::Line(line) => {
let deriv = line.deriv();
PathSeg::Line(Line::new(deriv.start(), deriv.end()))
}
PathSeg::Quad(quad) => PathSeg::Line(quad.deriv()),
PathSeg::Cubic(cubic) => PathSeg::Quad(cubic.deriv()),
}
}

fn gauss_arclen(&self, coeffs: &[(f64, f64)]) -> f64 {
match *self {
PathSeg::Line(line) => line.gauss_arclen(coeffs),
PathSeg::Quad(quad) => quad.gauss_arclen(coeffs),
PathSeg::Cubic(cubic) => cubic.gauss_arclen(coeffs),
}
}
}

impl ParamCurveNearest for PathSeg {
fn nearest(&self, p: Point, accuracy: f64) -> Nearest {
match *self {
Expand Down Expand Up @@ -729,6 +763,15 @@ impl PathSeg {
}
}

/// Evaluates the derivative of this segment at parameter `t`.
pub fn deriv_at(&self, t: f64) -> Point {
match *self {
PathSeg::Line(line) => line.deriv().eval(t),
PathSeg::Quad(quad) => quad.deriv().eval(t),
PathSeg::Cubic(cubic) => cubic.deriv().eval(t),
}
}

// Assumes split at extrema.
fn winding_inner(&self, p: Point) -> i32 {
let start = self.start();
Expand Down