pathfinder/content/src/dilation.rs

126 lines
4.2 KiB
Rust
Raw Normal View History

// pathfinder/content/src/dilation.rs
2019-02-05 23:10:20 -05:00
//
// 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.
2019-02-06 16:12:53 -05:00
use crate::orientation::Orientation;
2019-02-05 23:10:20 -05:00
use crate::outline::Contour;
use pathfinder_geometry::vector::{Vector2F, vec2f};
2019-02-05 23:10:20 -05:00
pub struct ContourDilator<'a> {
contour: &'a mut Contour,
amount: Vector2F,
2019-02-06 16:12:53 -05:00
orientation: Orientation,
2019-02-05 23:10:20 -05:00
}
impl<'a> ContourDilator<'a> {
2019-04-29 19:46:35 -04:00
pub fn new(
contour: &'a mut Contour,
amount: Vector2F,
2019-04-29 19:46:35 -04:00
orientation: Orientation,
) -> ContourDilator<'a> {
ContourDilator {
contour,
amount,
orientation,
}
2019-02-05 23:10:20 -05:00
}
pub fn dilate(&mut self) {
2019-02-06 16:48:50 -05:00
// Determine orientation.
2019-02-06 16:12:53 -05:00
let scale = self.amount.scale_xy(match self.orientation {
Orientation::Ccw => vec2f( 1.0, -1.0),
Orientation::Cw => vec2f(-1.0, 1.0),
2019-02-06 16:12:53 -05:00
});
2019-02-06 16:48:50 -05:00
// Find the starting and previous positions.
2019-02-06 16:46:19 -05:00
let first_position = self.contour.position_of(0);
2019-02-05 23:10:20 -05:00
let mut prev_point_index = 0;
let mut prev_position;
2019-02-06 16:12:53 -05:00
2019-02-05 23:10:20 -05:00
loop {
2019-02-06 16:46:19 -05:00
prev_point_index = self.contour.prev_point_index_of(prev_point_index);
prev_position = self.contour.position_of(prev_point_index);
2019-02-06 16:45:15 -05:00
if prev_point_index == 0 || prev_position != first_position {
2019-02-05 23:10:20 -05:00
break;
}
}
2019-02-06 16:48:50 -05:00
// Initialize our loop.
2019-02-06 16:46:19 -05:00
let first_point_index = self.contour.next_point_index_of(prev_point_index);
2019-02-06 16:45:15 -05:00
let mut current_point_index = first_point_index;
let mut position = first_position;
2019-02-06 16:12:53 -05:00
let mut prev_vector = (position - prev_position).normalize();
2019-02-05 23:10:20 -05:00
loop {
// Find the next non-degenerate position.
2019-02-06 16:12:53 -05:00
let mut next_point_index = current_point_index;
2019-02-05 23:10:20 -05:00
let mut next_position;
loop {
2019-02-06 16:46:19 -05:00
next_point_index = self.contour.next_point_index_of(next_point_index);
2019-02-06 16:45:15 -05:00
if next_point_index == first_point_index {
next_position = first_position;
break;
}
2019-02-06 16:46:19 -05:00
next_position = self.contour.position_of(next_point_index);
2019-02-06 16:12:53 -05:00
if next_point_index == current_point_index || next_position != position {
2019-02-05 23:10:20 -05:00
break;
}
}
2019-02-06 16:12:53 -05:00
let next_vector = (next_position - position).normalize();
2019-04-29 19:46:35 -04:00
debug!(
"prev={} cur={} next={}",
prev_point_index, current_point_index, next_point_index
);
2019-02-06 16:45:15 -05:00
// Calculate new position by moving the point by the bisector.
2019-02-06 16:12:53 -05:00
let bisector = prev_vector.yx() + next_vector.yx();
2019-02-05 23:10:20 -05:00
let bisector_length = bisector.length();
2019-02-06 16:12:53 -05:00
let scaled_bisector = if bisector_length == 0.0 {
Vector2F::zero()
2019-02-05 23:10:20 -05:00
} else {
2019-02-06 16:12:53 -05:00
bisector.scale_xy(scale).scale(1.0 / bisector_length)
2019-02-05 23:10:20 -05:00
};
2019-02-06 16:12:53 -05:00
let new_position = position - scaled_bisector;
2019-04-29 19:46:35 -04:00
debug!(
"dilate(): prev={}({:?}) cur={}({:?}) next={}({:?}) bisector={:?}({:?}, {:?})",
prev_point_index,
prev_position,
current_point_index,
position,
next_point_index,
next_position,
bisector,
bisector_length,
scaled_bisector
);
2019-02-05 23:10:20 -05:00
// Update all points.
2019-02-06 16:12:53 -05:00
let mut point_index = current_point_index;
while point_index != next_point_index {
2019-02-05 23:10:20 -05:00
self.contour.points[point_index as usize] = new_position;
debug!("... updating {:?}", point_index);
2019-02-06 16:46:19 -05:00
point_index = self.contour.next_point_index_of(point_index);
2019-02-05 23:10:20 -05:00
}
2019-02-06 16:45:15 -05:00
// Check to see if we're done.
if next_point_index == first_point_index {
2019-02-05 23:10:20 -05:00
break;
}
// Continue.
2019-02-06 16:12:53 -05:00
prev_vector = next_vector;
2019-02-05 23:10:20 -05:00
position = next_position;
2019-02-06 16:12:53 -05:00
current_point_index = next_point_index;
2019-02-05 23:10:20 -05:00
}
}
}