Add a streaming API for monotonic path conversion, untested as of yet
This commit is contained in:
parent
19e8305eaa
commit
c3e02d6faa
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
|||
authors = ["Patrick Walton <pcwalton@mimiga.net>"]
|
||||
|
||||
[dependencies]
|
||||
arrayvec = "0.4"
|
||||
euclid = "0.15"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
// pathfinder/path-utils/src/curve.rs
|
||||
//
|
||||
// Copyright © 2017 The Pathfinder Project Developers.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Geometry utilities for Bézier curves.
|
||||
|
||||
use euclid::Point2D;
|
||||
use euclid::approxeq::ApproxEq;
|
||||
use std::f32;
|
||||
|
||||
use PathSegment;
|
||||
|
||||
pub struct Curve {
|
||||
pub endpoints: [Point2D<f32>; 2],
|
||||
pub control_point: Point2D<f32>,
|
||||
}
|
||||
|
||||
impl Curve {
|
||||
#[inline]
|
||||
pub fn new(endpoint_0: &Point2D<f32>, control_point: &Point2D<f32>, endpoint_1: &Point2D<f32>)
|
||||
-> Curve {
|
||||
Curve {
|
||||
endpoints: [*endpoint_0, *endpoint_1],
|
||||
control_point: *control_point,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn sample(&self, t: f32) -> Point2D<f32> {
|
||||
let (p0, p1, p2) = (&self.endpoints[0], &self.control_point, &self.endpoints[1]);
|
||||
Point2D::lerp(&p0.lerp(*p1, t), p1.lerp(*p2, t), t)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn subdivide(&self, t: f32) -> (Curve, Curve) {
|
||||
let (p0, p1, p2) = (&self.endpoints[0], &self.control_point, &self.endpoints[1]);
|
||||
let (ap1, bp1) = (p0.lerp(*p1, t), p1.lerp(*p2, t));
|
||||
let ap2bp0 = ap1.lerp(bp1, t);
|
||||
(Curve::new(p0, &ap1, &ap2bp0), Curve::new(&ap2bp0, &bp1, p2))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn to_path_segment(&self) -> PathSegment {
|
||||
PathSegment::CurveTo(self.control_point, self.endpoints[1])
|
||||
}
|
||||
|
||||
pub fn inflection_points(&self) -> (Option<f32>, Option<f32>) {
|
||||
let inflection_point_x = Curve::inflection_point_x(self.endpoints[0].x,
|
||||
self.endpoints[1].x,
|
||||
self.control_point.x);
|
||||
let inflection_point_y = Curve::inflection_point_x(self.endpoints[0].y,
|
||||
self.endpoints[1].y,
|
||||
self.control_point.y);
|
||||
(inflection_point_x, inflection_point_y)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inflection_point_x(endpoint_x_0: f32, endpoint_x_1: f32, control_point_x: f32)
|
||||
-> Option<f32> {
|
||||
let num = endpoint_x_0 - control_point_x;
|
||||
let denom = endpoint_x_0 - 2.0 * control_point_x + endpoint_x_1;
|
||||
let t = num / denom;
|
||||
if t > f32::approx_epsilon() && t < 1.0 - f32::approx_epsilon() {
|
||||
Some(t)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
extern crate arrayvec;
|
||||
extern crate euclid;
|
||||
extern crate freetype_sys;
|
||||
#[macro_use]
|
||||
|
@ -16,7 +17,9 @@ extern crate serde_derive;
|
|||
use euclid::{Point2D, Transform2D};
|
||||
use std::u32;
|
||||
|
||||
pub mod curve;
|
||||
pub mod freetype;
|
||||
pub mod monotonic;
|
||||
pub mod stroke;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
// pathfinder/path-utils/src/monotonic.rs
|
||||
//
|
||||
// Copyright © 2017 The Pathfinder Project Developers.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use euclid::Point2D;
|
||||
use std::mem;
|
||||
|
||||
use PathSegment;
|
||||
use curve::Curve;
|
||||
|
||||
pub struct MonotonicPathSegmentStream<I> {
|
||||
inner: I,
|
||||
queue: ArrayVec<[PathSegment; 2]>,
|
||||
prev_point: Point2D<f32>,
|
||||
}
|
||||
|
||||
impl<I> MonotonicPathSegmentStream<I> where I: Iterator<Item = PathSegment> {
|
||||
pub fn new(inner: I) -> MonotonicPathSegmentStream<I> {
|
||||
MonotonicPathSegmentStream {
|
||||
inner: inner,
|
||||
queue: ArrayVec::new(),
|
||||
prev_point: Point2D::zero(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for MonotonicPathSegmentStream<I> where I: Iterator<Item = PathSegment> {
|
||||
type Item = PathSegment;
|
||||
|
||||
fn next(&mut self) -> Option<PathSegment> {
|
||||
if !self.queue.is_empty() {
|
||||
return Some(self.queue.remove(0))
|
||||
}
|
||||
|
||||
match self.inner.next() {
|
||||
None => None,
|
||||
Some(PathSegment::ClosePath) => Some(PathSegment::ClosePath),
|
||||
Some(PathSegment::MoveTo(point)) => {
|
||||
self.prev_point = point;
|
||||
Some(PathSegment::MoveTo(point))
|
||||
}
|
||||
Some(PathSegment::LineTo(point)) => {
|
||||
self.prev_point = point;
|
||||
Some(PathSegment::LineTo(point))
|
||||
}
|
||||
Some(PathSegment::CurveTo(control_point, endpoint)) => {
|
||||
let curve = Curve::new(&self.prev_point, &control_point, &endpoint);
|
||||
match curve.inflection_points() {
|
||||
(None, None) => {
|
||||
self.prev_point = endpoint;
|
||||
Some(PathSegment::CurveTo(control_point, endpoint))
|
||||
}
|
||||
(Some(t), None) | (None, Some(t)) => {
|
||||
let (prev_curve, next_curve) = curve.subdivide(t);
|
||||
self.queue.push(next_curve.to_path_segment());
|
||||
self.prev_point = prev_curve.endpoints[1];
|
||||
Some(prev_curve.to_path_segment())
|
||||
}
|
||||
(Some(mut t0), Some(mut t1)) => {
|
||||
if t1 < t0 {
|
||||
mem::swap(&mut t0, &mut t1)
|
||||
}
|
||||
|
||||
let (curve_0, curve_12) = curve.subdivide(t0);
|
||||
let (curve_1, curve_2) = curve_12.subdivide(t1);
|
||||
self.queue.push(curve_1.to_path_segment());
|
||||
self.queue.push(curve_2.to_path_segment());
|
||||
|
||||
self.prev_point = curve_0.endpoints[1];
|
||||
Some(curve_0.to_path_segment())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue