Move monotonic conversion to the geometry crate

This commit is contained in:
Patrick Walton 2019-01-14 11:22:45 -08:00
parent dcee161f6a
commit 3f28845157
3 changed files with 83 additions and 66 deletions

View File

@ -18,6 +18,7 @@ extern crate bitflags;
pub mod clip; pub mod clip;
pub mod cubic_to_quadratic; pub mod cubic_to_quadratic;
pub mod line_segment; pub mod line_segment;
pub mod monotonic;
pub mod normals; pub mod normals;
pub mod orientation; pub mod orientation;
pub mod outline; pub mod outline;

79
geometry/src/monotonic.rs Normal file
View File

@ -0,0 +1,79 @@
// pathfinder/geometry/src/monotonic.rs
//
// Copyright © 2019 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.
//! Converts paths to monotonically increasing/decreasing segments.
use crate::segment::{Segment, SegmentKind};
use arrayvec::ArrayVec;
// TODO(pcwalton): I think we only need to be monotonic in Y, maybe?
pub struct MonotonicConversionIter<I>
where
I: Iterator<Item = Segment>,
{
iter: I,
buffer: ArrayVec<[Segment; 2]>,
}
impl<I> Iterator for MonotonicConversionIter<I>
where
I: Iterator<Item = Segment>,
{
type Item = Segment;
#[inline]
fn next(&mut self) -> Option<Segment> {
if let Some(segment) = self.buffer.pop() {
return Some(segment);
}
let segment = self.iter.next()?;
match segment.kind {
SegmentKind::None => self.next(),
SegmentKind::Line => Some(segment),
SegmentKind::Cubic => self.handle_cubic(&segment),
SegmentKind::Quadratic => {
// TODO(pcwalton): Don't degree elevate!
self.handle_cubic(&segment.to_cubic())
}
}
}
}
impl<I> MonotonicConversionIter<I>
where
I: Iterator<Item = Segment>,
{
#[inline]
pub fn new(iter: I) -> MonotonicConversionIter<I> {
MonotonicConversionIter {
iter,
buffer: ArrayVec::new(),
}
}
pub fn handle_cubic(&mut self, segment: &Segment) -> Option<Segment> {
match segment.as_cubic_segment().y_extrema() {
(Some(t0), Some(t1)) => {
let (segments_01, segment_2) = segment.as_cubic_segment().split(t1);
self.buffer.push(segment_2);
let (segment_0, segment_1) = segments_01.as_cubic_segment().split(t0 / t1);
self.buffer.push(segment_1);
Some(segment_0)
}
(Some(t0), None) | (None, Some(t0)) => {
let (segment_0, segment_1) = segment.as_cubic_segment().split(t0);
self.buffer.push(segment_1);
Some(segment_0)
}
(None, None) => Some(*segment),
}
}
}

View File

@ -15,7 +15,6 @@ extern crate quickcheck;
#[cfg(test)] #[cfg(test)]
extern crate rand; extern crate rand;
use arrayvec::ArrayVec;
use byteorder::{LittleEndian, WriteBytesExt}; use byteorder::{LittleEndian, WriteBytesExt};
use clap::{App, Arg}; use clap::{App, Arg};
use euclid::{Point2D, Rect, Size2D}; use euclid::{Point2D, Rect, Size2D};
@ -24,10 +23,11 @@ use hashbrown::HashMap;
use jemallocator; use jemallocator;
use lyon_path::iterator::PathIter; use lyon_path::iterator::PathIter;
use pathfinder_geometry::line_segment::{LineSegmentF32, LineSegmentU4, LineSegmentU8}; use pathfinder_geometry::line_segment::{LineSegmentF32, LineSegmentU4, LineSegmentU8};
use pathfinder_geometry::monotonic::MonotonicConversionIter;
use pathfinder_geometry::outline::{Contour, Outline, PointIndex}; use pathfinder_geometry::outline::{Contour, Outline, PointIndex};
use pathfinder_geometry::point::Point2DF32; use pathfinder_geometry::point::Point2DF32;
use pathfinder_geometry::segment::{PathEventsToSegments, Segment, SegmentFlags}; use pathfinder_geometry::segment::{PathEventsToSegments, Segment};
use pathfinder_geometry::segment::{SegmentKind, SegmentsToPathEvents}; use pathfinder_geometry::segment::{SegmentFlags, SegmentsToPathEvents};
use pathfinder_geometry::simd::{F32x4, I32x4}; use pathfinder_geometry::simd::{F32x4, I32x4};
use pathfinder_geometry::stroke::{StrokeStyle, StrokeToFillIter}; use pathfinder_geometry::stroke::{StrokeStyle, StrokeToFillIter};
use pathfinder_geometry::transform::{Transform2DF32, Transform2DF32PathIter}; use pathfinder_geometry::transform::{Transform2DF32, Transform2DF32PathIter};
@ -1364,69 +1364,6 @@ where
// Monotonic conversion utilities // Monotonic conversion utilities
// TODO(pcwalton): I think we only need to be monotonic in Y, maybe?
struct MonotonicConversionIter<I>
where
I: Iterator<Item = Segment>,
{
iter: I,
buffer: ArrayVec<[Segment; 2]>,
}
impl<I> Iterator for MonotonicConversionIter<I>
where
I: Iterator<Item = Segment>,
{
type Item = Segment;
fn next(&mut self) -> Option<Segment> {
if let Some(segment) = self.buffer.pop() {
return Some(segment);
}
let segment = self.iter.next()?;
match segment.kind {
SegmentKind::None => self.next(),
SegmentKind::Line => Some(segment),
SegmentKind::Cubic => self.handle_cubic(&segment),
SegmentKind::Quadratic => {
// TODO(pcwalton): Don't degree elevate!
self.handle_cubic(&segment.to_cubic())
}
}
}
}
impl<I> MonotonicConversionIter<I>
where
I: Iterator<Item = Segment>,
{
fn new(iter: I) -> MonotonicConversionIter<I> {
MonotonicConversionIter {
iter,
buffer: ArrayVec::new(),
}
}
fn handle_cubic(&mut self, segment: &Segment) -> Option<Segment> {
match segment.as_cubic_segment().y_extrema() {
(Some(t0), Some(t1)) => {
let (segments_01, segment_2) = segment.as_cubic_segment().split(t1);
self.buffer.push(segment_2);
let (segment_0, segment_1) = segments_01.as_cubic_segment().split(t0 / t1);
self.buffer.push(segment_1);
Some(segment_0)
}
(Some(t0), None) | (None, Some(t0)) => {
let (segment_0, segment_1) = segment.as_cubic_segment().split(t0);
self.buffer.push(segment_1);
Some(segment_0)
}
(None, None) => Some(*segment),
}
}
}
// SortedVector // SortedVector
#[derive(Clone, Debug)] #[derive(Clone, Debug)]