From e3cca6a676ab165c1ebbcb3197f06624ccf35f1e Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 12 Feb 2019 19:51:48 -0800 Subject: [PATCH] Remove the `cubic_to_quadratic` module, as it's unneeded now --- geometry/src/cubic_to_quadratic.rs | 137 ----------------------------- geometry/src/lib.rs | 1 - 2 files changed, 138 deletions(-) delete mode 100644 geometry/src/cubic_to_quadratic.rs diff --git a/geometry/src/cubic_to_quadratic.rs b/geometry/src/cubic_to_quadratic.rs deleted file mode 100644 index 924f91fe..00000000 --- a/geometry/src/cubic_to_quadratic.rs +++ /dev/null @@ -1,137 +0,0 @@ -// pathfinder/geometry/src/cubic_to_quadratic.rs -// -// Copyright © 2019 The Pathfinder Project Developers. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A version of Lyon's `cubic_to_quadratic` that is less sensitive to floating point error. - -use euclid::Point2D; -use lyon_geom::{CubicBezierSegment, QuadraticBezierSegment}; -use lyon_path::PathEvent; - -const MAX_APPROXIMATION_ITERATIONS: u8 = 32; - -/// Approximates a single cubic Bézier curve with a series of quadratic Bézier curves. -pub struct CubicToQuadraticSegmentIter { - cubic_curves: Vec>, - error_bound: f32, - iteration: u8, -} - -impl CubicToQuadraticSegmentIter { - pub fn new(cubic: &CubicBezierSegment, error_bound: f32) -> CubicToQuadraticSegmentIter { - let (curve_a, curve_b) = cubic.split(0.5); - CubicToQuadraticSegmentIter { - cubic_curves: vec![curve_b, curve_a], - error_bound: error_bound, - iteration: 0, - } - } -} - -impl Iterator for CubicToQuadraticSegmentIter { - type Item = QuadraticBezierSegment; - - fn next(&mut self) -> Option> { - let mut cubic = match self.cubic_curves.pop() { - Some(cubic) => cubic, - None => return None, - }; - - while self.iteration < MAX_APPROXIMATION_ITERATIONS { - self.iteration += 1; - - // See Sederberg § 2.6, "Distance Between Two Bézier Curves". - let delta_ctrl_0 = (cubic.from - cubic.ctrl1 * 3.0) + (cubic.ctrl2 * 3.0 - cubic.to); - let delta_ctrl_1 = (cubic.ctrl1 * 3.0 - cubic.from) + (cubic.to - cubic.ctrl2 * 3.0); - let max_error = f32::max(delta_ctrl_1.length(), delta_ctrl_0.length()) / 6.0; - if max_error < self.error_bound { - break - } - - let (cubic_a, cubic_b) = cubic.split(0.5); - self.cubic_curves.push(cubic_b); - cubic = cubic_a - } - - let approx_ctrl_0 = (cubic.ctrl1 * 3.0 - cubic.from) * 0.5; - let approx_ctrl_1 = (cubic.ctrl2 * 3.0 - cubic.to) * 0.5; - - Some(QuadraticBezierSegment { - from: cubic.from, - ctrl: approx_ctrl_0.lerp(approx_ctrl_1, 0.5).to_point(), - to: cubic.to, - }) - } -} - -pub struct CubicToQuadraticTransformer where I: Iterator { - inner: I, - segment_iter: Option, - last_point: Point2D, - error_bound: f32, -} - -impl CubicToQuadraticTransformer where I: Iterator { - #[inline] - pub fn new(inner: I, error_bound: f32) -> CubicToQuadraticTransformer { - CubicToQuadraticTransformer { - inner: inner, - segment_iter: None, - last_point: Point2D::zero(), - error_bound: error_bound, - } - } -} - -impl Iterator for CubicToQuadraticTransformer where I: Iterator { - type Item = PathEvent; - - fn next(&mut self) -> Option { - if let Some(ref mut segment_iter) = self.segment_iter { - if let Some(quadratic) = segment_iter.next() { - return Some(PathEvent::QuadraticTo(quadratic.ctrl, quadratic.to)) - } - } - - self.segment_iter = None; - - match self.inner.next() { - None => None, - Some(PathEvent::CubicTo(ctrl1, ctrl2, to)) => { - let cubic = CubicBezierSegment { - from: self.last_point, - ctrl1: ctrl1, - ctrl2: ctrl2, - to: to, - }; - self.last_point = to; - self.segment_iter = Some(CubicToQuadraticSegmentIter::new(&cubic, - self.error_bound)); - self.next() - } - Some(PathEvent::MoveTo(to)) => { - self.last_point = to; - Some(PathEvent::MoveTo(to)) - } - Some(PathEvent::LineTo(to)) => { - self.last_point = to; - Some(PathEvent::LineTo(to)) - } - Some(PathEvent::QuadraticTo(ctrl, to)) => { - self.last_point = to; - Some(PathEvent::QuadraticTo(ctrl, to)) - } - Some(PathEvent::Close) => Some(PathEvent::Close), - Some(PathEvent::Arc(to, vector, angle_from, angle_to)) => { - self.last_point = to; - Some(PathEvent::Arc(to, vector, angle_from, angle_to)) - } - } - } -} diff --git a/geometry/src/lib.rs b/geometry/src/lib.rs index 8c824da7..a4dbdec1 100644 --- a/geometry/src/lib.rs +++ b/geometry/src/lib.rs @@ -17,7 +17,6 @@ extern crate bitflags; pub mod basic; pub mod clip; -pub mod cubic_to_quadratic; pub mod monotonic; pub mod normals; pub mod orientation;