diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2020-11-25 18:57:47 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2020-11-25 18:57:47 +0800 |
commit | 85fb99fb7789972f95f3fe4f902e9f53159773af (patch) | |
tree | 1a61b72aa05f577c5444b1c47e644827e4ed6283 /svgbob | |
parent | e42ecb4fed9495daad3bc819268103305b26ea0b (diff) |
Remove can_merge function for line and polygon merging
Diffstat (limited to 'svgbob')
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment.rs | 35 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment/line.rs | 69 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs | 37 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment/polygon.rs | 16 | ||||
-rw-r--r-- | svgbob/test_data/merge.bob | 6 |
5 files changed, 81 insertions, 82 deletions
diff --git a/svgbob/src/buffer/fragment_buffer/fragment.rs b/svgbob/src/buffer/fragment_buffer/fragment.rs index ae3378a..6795558 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment.rs @@ -97,7 +97,8 @@ impl Fragment { } } - /// FIXME: This is only merging lines and text right now + /// merge this fragment to the other fragment if it is possible + /// returns None if the fragment can not be merge pub fn merge(&self, other: &Self) -> Option<Self> { match (self, other) { // line and line @@ -118,9 +119,12 @@ impl Fragment { /* // line and marker_line (Fragment::Line(line), Fragment::MarkerLine(mline)) => line.merge_marker_line(mline), - + */ + /* // marker_line and line (Fragment::MarkerLine(mline), Fragment::Line(line)) => line.merge_marker_line(mline), + */ + /* (Fragment::MarkerLine(mline), Fragment::Polygon(polygon)) => { mline.merge_polygon(polygon) } @@ -659,6 +663,33 @@ mod tests { } #[test] + fn merge_line_and_circle() { + let a = CellGrid::a(); + let m = CellGrid::m(); + let y = CellGrid::y(); + + let circle = circle(m, Cell::unit(2), false); + + let circle = circle.absolute_position(Cell::new(0, 0)); + let diagonal: Fragment = Line::new_noswap(a, y, false).into(); + let diagonal = diagonal.absolute_position(Cell::new(1, 1)); + + println!("circle: {:#?}", circle); + println!("diagonal: {:#?}", diagonal); + + let merged = circle.merge(&diagonal); + + let expected = marker_line( + Point::new(2.0, 4.0), + Point::new(0.5, 1.0), + false, + None, + Some(Marker::BigOpenCircle), + ); + assert_eq!(Some(expected), merged); + } + + #[test] fn line_overlaps() { let line = Line::new(CellGrid::a(), CellGrid::b(), false); println!("line: {}", line); diff --git a/svgbob/src/buffer/fragment_buffer/fragment/line.rs b/svgbob/src/buffer/fragment_buffer/fragment/line.rs index 2779b3b..6de5bd3 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment/line.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment/line.rs @@ -306,13 +306,15 @@ impl Line { let distance_end_center = self.end.distance(&poly_center); let distance_start_center = self.start.distance(&poly_center); - let threshold_length = self.heading().threshold_length(); + let line_heading = self.heading(); + + let threshold_length = line_heading.threshold_length(); let is_close_start_point = distance_start_center < threshold_length; let is_close_end_point = distance_end_center < threshold_length; - let is_same_direction = dbg!(polygon.matched_direction(self.heading())); + let is_same_direction = polygon.matched_direction(line_heading); - let is_opposite_direction = dbg!(polygon.matched_direction(self.heading().opposite())); + let is_opposite_direction = polygon.matched_direction(line_heading.opposite()); let can_merge = (is_same_direction || is_opposite_direction) && (is_close_start_point || is_close_end_point); @@ -340,48 +342,45 @@ impl Line { } } - pub(crate) fn can_merge_circle(&self, circle: &Circle) -> bool { + pub(crate) fn merge_circle(&self, circle: &Circle) -> Option<Fragment> { let distance_end_center = self.end.distance(&circle.center); let distance_start_center = self.start.distance(&circle.center); let threshold_length = self.heading().threshold_length(); let is_close_start_point = distance_start_center <= threshold_length * 0.75; let is_close_end_point = distance_end_center <= threshold_length * 0.75; - circle.radius <= Cell::unit(3) && (is_close_start_point || is_close_end_point) - } - pub(crate) fn merge_circle(&self, circle: &Circle) -> Option<Fragment> { - let distance_end_center = self.end.distance(&circle.center); - let distance_start_center = self.start.distance(&circle.center); + let can_merge = + circle.radius <= Cell::unit(3) && (is_close_start_point || is_close_end_point); - let threshold_length = self.heading().threshold_length(); - let is_close_start_point = distance_start_center <= threshold_length * 0.75; - let is_close_end_point = distance_end_center <= threshold_length * 0.75; + if can_merge { + let marker = if circle.is_filled { + Some(Marker::Circle) + } else if circle.radius >= Cell::unit(2) { + Some(Marker::BigOpenCircle) + } else { + Some(Marker::OpenCircle) + }; + let new_line = if is_close_end_point { + Line::new_noswap(self.start, circle.center, self.is_broken) + } else if is_close_start_point { + // if close to the start, swap the end points of the line + Line::new_noswap(self.end, circle.center, self.is_broken) + } else { + panic!("There is no endpoint of the line is that close to the arrow"); + }; - let marker = if circle.is_filled { - Some(Marker::Circle) - } else if circle.radius >= Cell::unit(2) { - Some(Marker::BigOpenCircle) - } else { - Some(Marker::OpenCircle) - }; - let new_line = if is_close_end_point { - Line::new_noswap(self.start, circle.center, self.is_broken) - } else if is_close_start_point { - // if close to the start, swap the end points of the line - Line::new_noswap(self.end, circle.center, self.is_broken) + let marker_line = marker_line( + new_line.start, + new_line.end, + new_line.is_broken, + None, + marker, + ); + Some(marker_line) } else { - panic!("There is no endpoint of the line is that close to the arrow"); - }; - - let marker_line = marker_line( - new_line.start, - new_line.end, - new_line.is_broken, - None, - marker, - ); - Some(marker_line) + None + } } /// check to see if any of the line endpoints is touching. diff --git a/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs b/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs index 60ff582..98e6e62 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment/marker_line.rs @@ -80,41 +80,27 @@ impl MarkerLine { } } - /* - pub(crate) fn can_merge_polygon(&self, polygon: &Polygon) -> bool { + /// merge this marker line to the polygon + pub(crate) fn merge_polygon(&self, polygon: &Polygon) -> Option<Fragment> { let poly_center = polygon.center(); let distance_end_center = self.line.end.distance(&poly_center); let distance_start_center = self.line.start.distance(&poly_center); - let threshold_length = self.line.heading().threshold_length(); + let line_heading = self.line.heading(); + + let threshold_length = line_heading.threshold_length(); let is_close_start_point = distance_start_center < threshold_length; let is_close_end_point = distance_end_center < threshold_length; - let can_connect_start = is_close_start_point && self.start_marker.is_none(); - let can_connect_end = is_close_end_point && self.end_marker.is_none(); + let is_same_direction = polygon.matched_direction(line_heading); - let is_same_direction = polygon - .tags - .iter() - .any(|tag| tag.matched_direction(self.line.heading())); + let is_opposite_direction = polygon.matched_direction(line_heading.opposite()); - is_same_direction && (can_connect_start || can_connect_end) - } - */ - - /* - /// merge this marker line to the polygon - pub(crate) fn merge_polygon(&self, polygon: &Polygon) -> Option<Fragment> { - if self.can_merge_polygon(polygon) { - let marker = polygon.tags.get(0).map(|tag| tag.get_marker()); - let direction = polygon.tags.get(0).map(|tag| tag.direction()); - let poly_center = polygon.center(); - let distance_end_center = self.line.end.distance(&poly_center); - let distance_start_center = self.line.start.distance(&poly_center); + let can_merge = (is_same_direction || is_opposite_direction) + && (is_close_start_point || is_close_end_point); - let threshold_length = self.line.heading().threshold_length(); - let is_close_start_point = distance_start_center < threshold_length; - let is_close_end_point = distance_end_center < threshold_length; + if can_merge { + let marker = polygon.get_marker(); let start_marker = if is_close_start_point && self.start_marker.is_none() { marker.clone() @@ -146,7 +132,6 @@ impl MarkerLine { None } } - */ } impl Bounds for MarkerLine { diff --git a/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs b/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs index 7143584..91df71e 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs @@ -91,22 +91,6 @@ impl PolygonTag { true } } - - /* - /// calculate the threshold length which is the basis - /// if the arrow and the line is connected - pub(crate) fn threshold_length(&self) -> f32 { - match self { - PolygonTag::ArrowTopRight - | PolygonTag::ArrowBottomRight - | PolygonTag::ArrowBottomLeft - | PolygonTag::ArrowTopLeft => CellGrid::diagonal_length(), - PolygonTag::ArrowBottom | PolygonTag::ArrowTop => CellGrid::height(), - PolygonTag::ArrowLeft | PolygonTag::ArrowRight => CellGrid::width(), - PolygonTag::DiamondBullet => CellGrid::diagonal_length(), - } - } - */ } impl Polygon { diff --git a/svgbob/test_data/merge.bob b/svgbob/test_data/merge.bob index 8770313..87cc671 100644 --- a/svgbob/test_data/merge.bob +++ b/svgbob/test_data/merge.bob @@ -64,7 +64,7 @@ -------▸ - ◂------ + ◂------ / @@ -97,8 +97,8 @@ ------o O O \ / O O O------ O O - \ / - ------O \ / + \ / O + ------O \ / \ \ / # |