diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2018-08-13 13:23:59 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2018-08-13 13:23:59 +0800 |
commit | 2331d94de41b7e29be32e79545c1f6be54ff5f86 (patch) | |
tree | a0b85a3f470b1c3101c6712d991d7b9d74cbdcee | |
parent | 90f428599c9b68313105410fcda142f712e3f57b (diff) | |
parent | 6efefb3c1786051daea31b994dd430e2936ce352 (diff) |
Merge branch 'optimization-overhaul' of https://gitlab.com/ivanceras/svgbob into optimization-overhauloptimization-overhaul
-rw-r--r-- | svgbob/src/element.rs | 37 | ||||
-rw-r--r-- | svgbob/src/optimizer.rs | 10 | ||||
-rw-r--r-- | svgbob/src/point.rs | 2 |
3 files changed, 48 insertions, 1 deletions
diff --git a/svgbob/src/element.rs b/svgbob/src/element.rs index 4dfe1a5..17b3b08 100644 --- a/svgbob/src/element.rs +++ b/svgbob/src/element.rs @@ -19,6 +19,7 @@ use element::{ Feature::{Arrow,ArrowStart,Circle,Square,OpenCircle,BigOpenCircle,Nothing}, }; use unicode_width::UnicodeWidthStr; +use point; @@ -32,6 +33,28 @@ pub enum Element { Text(Loc, String), } +impl Element{ + + fn longer_line<'a>(&'a self, other: &'a Element) -> &'a Element { + match self{ + Element::Line(s1, e1, _, _, _) => match other{ + Element::Line(s2, e2, _, _,_) => { + let d1 = point::distance(s1, e1); + let d2 = point::distance(s2, e2); + if d1 > d2 { + self + } + else{ + other + } + } + _ => panic!("only for lines"), + } + _ => panic!("only for lines"), + } + } +} + #[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq)] pub enum Stroke { Solid, @@ -165,6 +188,7 @@ impl Element { return Some(other.clone()) } + // extend 1 with 2 // line1 line2 // s-----e s2-----e2 // s----------------e2 @@ -183,6 +207,7 @@ impl Element { )); } } + // extend 1 with flip 2 // line1 line2 // s------e e2-------s2 // s-------------------s2 @@ -199,6 +224,7 @@ impl Element { )); } } + // flip1 extend 2 // line1 line2 // e------s s2------e2 // s------------------e2 @@ -215,7 +241,17 @@ impl Element { end_feature2.clone(), )); } + // TODO: need fixing + // same starting point, take longer line + // longer line + // s-------e + // s2-------------->e2 + else { + //println!("longer line"); + return Some(self.longer_line(other).clone()) + } } + // extend 2 with 1 // line1 line2 // e------s e2------s2 // e---------------------s2 @@ -235,6 +271,7 @@ impl Element { )); } } + } return None; } diff --git a/svgbob/src/optimizer.rs b/svgbob/src/optimizer.rs index 5526121..204cc7c 100644 --- a/svgbob/src/optimizer.rs +++ b/svgbob/src/optimizer.rs @@ -139,6 +139,16 @@ impl Optimizer { all_reduced.push(elm.clone()); } } + if consumed.len() > 0 { + //println!("original elements: {}", elements.len()); + //println!("total consumed: {}", consumed.len()); + //println!("all_reduced: {}", all_reduced.len()); + } + all_reduced.sort(); + all_reduced.dedup(); + if elements.len() != all_reduced.len(){ + //println!("reduced from {} to {}", elements.len(), all_reduced.len()); + } all_reduced } diff --git a/svgbob/src/point.rs b/svgbob/src/point.rs index 106b251..b26ac38 100644 --- a/svgbob/src/point.rs +++ b/svgbob/src/point.rs @@ -32,6 +32,6 @@ pub fn collinear(a: &Point, b: &Point, c: &Point) -> bool { a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) == 0.0 } -pub fn _distance(a: &Point, b: &Point) -> f32{ +pub fn distance(a: &Point, b: &Point) -> f32{ ((b.x - a.x ).powi(2) + (b.y - a.y).powi(2)).sqrt() } |