2019-02-05 23:10:20 -05:00
|
|
|
// pathfinder/geometry/src/dilation.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.
|
|
|
|
|
|
|
|
use crate::basic::point::Point2DF32;
|
2019-02-06 16:12:53 -05:00
|
|
|
use crate::orientation::Orientation;
|
2019-02-05 23:10:20 -05:00
|
|
|
use crate::outline::Contour;
|
|
|
|
|
|
|
|
pub struct ContourDilator<'a> {
|
|
|
|
contour: &'a mut Contour,
|
|
|
|
amount: Point2DF32,
|
2019-02-06 16:12:53 -05:00
|
|
|
orientation: Orientation,
|
2019-02-05 23:10:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ContourDilator<'a> {
|
2019-02-06 16:12:53 -05:00
|
|
|
pub fn new(contour: &'a mut Contour, amount: Point2DF32, 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:45:15 -05:00
|
|
|
println!("---");
|
2019-02-06 16:12:53 -05:00
|
|
|
let scale = self.amount.scale_xy(match self.orientation {
|
|
|
|
Orientation::Ccw => Point2DF32::new( 1.0, -1.0),
|
|
|
|
Orientation::Cw => Point2DF32::new(-1.0, 1.0),
|
|
|
|
});
|
|
|
|
|
|
|
|
let input = self.contour.clone();
|
|
|
|
|
2019-02-06 16:45:15 -05:00
|
|
|
let first_position = input.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:12:53 -05:00
|
|
|
prev_point_index = input.prev_point_index_of(prev_point_index);
|
|
|
|
prev_position = input.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:45:15 -05:00
|
|
|
// Find the starting position.
|
|
|
|
let first_point_index = input.next_point_index_of(prev_point_index);
|
|
|
|
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:12:53 -05:00
|
|
|
next_point_index = input.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:12:53 -05:00
|
|
|
next_position = input.position_of(next_point_index);
|
|
|
|
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-02-06 16:45:15 -05:00
|
|
|
/*
|
|
|
|
println!("prev={} cur={} next={}",
|
|
|
|
prev_point_index,
|
|
|
|
current_point_index,
|
|
|
|
next_point_index);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
Point2DF32::default()
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
println!("dilate(): prev={}({:?}) cur={}({:?}) next={}({:?}) bisector={:?}({:?}, {:?})",
|
|
|
|
prev_point_index,
|
|
|
|
prev_position,
|
|
|
|
current_point_index,
|
2019-02-05 23:10:20 -05:00
|
|
|
position,
|
2019-02-06 16:12:53 -05:00
|
|
|
next_point_index,
|
|
|
|
next_position,
|
2019-02-05 23:10:20 -05:00
|
|
|
bisector,
|
2019-02-06 16:12:53 -05:00
|
|
|
bisector_length,
|
|
|
|
scaled_bisector);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*if bisector_length == 0.0 {
|
|
|
|
println!("dilate({:?}): {:?} -> {:?} (bisector {:?}, length {:?})",
|
|
|
|
self.amount,
|
|
|
|
position,
|
|
|
|
new_position,
|
|
|
|
bisector,
|
|
|
|
bisector_length);
|
|
|
|
}*/
|
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;
|
2019-02-06 16:12:53 -05:00
|
|
|
//println!("... updating {:?}", point_index);
|
|
|
|
point_index = input.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_point_index = next_point_index - 1;
|
2019-02-05 23:10:20 -05:00
|
|
|
prev_position = position;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|