diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2018-08-02 01:48:34 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2018-08-02 01:48:34 +0800 |
commit | 0e1b689ac20b9f8cd93033f86e2664a29aa6016d (patch) | |
tree | 85ac6e60b44179b5e18f4864f8d1fb55502c20e4 | |
parent | 7c9450893c722b78ff7689c78c00150521fdab8d (diff) |
Alter the logic of getting the elements, Get the enhance_elements first and take note of the consumed locations, then run the normal get elements while avoiding the consumed locations
-rw-r--r-- | svgbob/src/enhance.rs | 18 | ||||
-rw-r--r-- | svgbob/src/focus_char.rs | 64 | ||||
-rw-r--r-- | svgbob/src/grid.rs | 41 | ||||
-rw-r--r-- | svgbob/src/loc.rs | 1 | ||||
-rw-r--r-- | svgbob/src/optimizer.rs | 19 |
5 files changed, 93 insertions, 50 deletions
diff --git a/svgbob/src/enhance.rs b/svgbob/src/enhance.rs index 396de00..027fc34 100644 --- a/svgbob/src/enhance.rs +++ b/svgbob/src/enhance.rs @@ -88,22 +88,26 @@ impl<'g> Enhance for FocusChar<'g> { if self.top_right().any(".,") { elm.push(dashed_line(c, &top_right().m())); consumed.push(top_right()); + consumed.push(this()); } // . // ' if self.top_left().any(".,") { elm.push(dashed_line(c, &top_left().m())); consumed.push(top_left()); + consumed.push(this()); } // .' if self.left().any(".,") { elm.push(dashed_line(c, &left().m())); consumed.push(left()); + consumed.push(this()); } // '. if self.right().any(".,") { elm.push(dashed_line(c, &right().m())); consumed.push(right()); + consumed.push(this()); } } else if self.any(".,") { // for circuitries @@ -114,6 +118,7 @@ impl<'g> Enhance for FocusChar<'g> { consumed.push(left()); if self.bottom_right().is('\\') { consumed.push(bottom_right()); + consumed.push(this()); } } // for circuitries @@ -124,6 +129,7 @@ impl<'g> Enhance for FocusChar<'g> { consumed.push(right()); if self.bottom_left().is('/') { consumed.push(bottom_left()); + consumed.push(this()); } } } @@ -133,43 +139,51 @@ impl<'g> Enhance for FocusChar<'g> { // < > if self.bottom().any("><") { elm.push(line(c, &bottom().m())); + consumed.push(this()); } // < > // | | if self.top().any("><") { elm.push(line(w, &top().m())); + consumed.push(this()); } // _ // | if self.top_right().is('_') { elm.extend(vec![line(c,w),line(c, e)]); + consumed.push(this()); } // _ // | if self.top_left().is('_') { elm.extend(vec![line(c,w),line(a,c)]); + consumed.push(this()); } } else if self.is('/') { // > // / if self.top_right().is('>') { elm.push(line(u, &top_right().m())); + consumed.push(this()); } // / // < if self.bottom_left().is('<') { elm.push(line(e, &bottom_left().m())); + consumed.push(this()); } } else if self.is('\\') { // \ // > if self.bottom_right().is('>') { elm.push(line(a, &bottom_right().m())); + consumed.push(this()); } // < // \ if self.top_left().is('<') { elm.push(line(y, &top_left().m())); + consumed.push(this()); } } // circuitries jump @@ -183,7 +197,7 @@ impl<'g> Enhance for FocusChar<'g> { && self.right().can_strongly_connect(&K) { elm.extend(vec![arc(c, w, 5), line(k, o)]); - //consumed.push(this()); + consumed.push(this()); } // circuitries jump // | @@ -196,7 +210,7 @@ impl<'g> Enhance for FocusChar<'g> { && self.right().can_strongly_connect(&K) { elm.extend(vec![arc(w, c, 5), line(k, o)]); - //consumed.push(this()); + consumed.push(this()); } (elm, consumed) diff --git a/svgbob/src/focus_char.rs b/svgbob/src/focus_char.rs index 1b5f4fb..46b1b25 100644 --- a/svgbob/src/focus_char.rs +++ b/svgbob/src/focus_char.rs @@ -206,8 +206,9 @@ impl<'g> FocusChar<'g> { } } - pub fn get_elements(&self) -> (Vec<Element>, Vec<Loc>) { - let (fragments, consumed_location) = self.get_fragments(); + /// return the ehance elements and the consumed locations + pub fn get_enhance_elements(&self) -> (Vec<Element>, Vec<Loc>) { + let (fragments, consumed_location) = self.get_enhance_fragments(); let mut elements: Vec<Element> = fragments .into_iter() .map(|frag| self.to_element(frag)) @@ -221,6 +222,18 @@ impl<'g> FocusChar<'g> { (elements, consumed_loc) } + /// returns the defaults elements + pub fn get_elements(&self) -> Vec<Element> { + let fragments = self.get_fragments(); + let mut elements: Vec<Element> = fragments + .into_iter() + .map(|frag| self.to_element(frag)) + .collect(); + elements.sort(); + elements.dedup(); + (elements) + } + fn is_satisfied(&self, can: &Can) -> bool { match *can { ConnectTo(ref cond_block, ref signal) => { @@ -265,7 +278,7 @@ impl<'g> FocusChar<'g> { false } - fn get_fragments(&self) -> (Vec<Fragment>, Vec<Location>) { + fn get_enhance_fragments(&self) -> (Vec<Fragment>, Vec<Location>) { let character: Option<Characteristic> = self.ch.get_characteristic(); let mut elm: Vec<Fragment> = vec![]; let mut consumed: Vec<Location> = vec![]; @@ -275,7 +288,6 @@ impl<'g> FocusChar<'g> { if let Some(character) = character { - let (enhanced, enhance_consumed) = self.enhance(); if !enhanced.is_empty() && !self.used_as_text() { elm.extend(enhanced); @@ -283,18 +295,29 @@ impl<'g> FocusChar<'g> { consumed.extend(enhance_consumed); matched_enhance = true; } - // intended behaviors when signals are strong - // after applying the intensifiers - // do only when enhancements is not matched - // TODO: if nothing is consumed in the enhance - // allow the rest to do the matching - if !matched_enhance { - for &(ref blocks, ref fragments) in &character.intended_behavior { - let meet = blocks.iter().all(|ref b| self.can_be_strong_block(&b)); - if meet && !self.used_as_text() { - elm.extend(fragments.clone()); - matched_intended = true; - } + } + elm.sort(); + elm.dedup(); + consumed.sort(); + consumed.dedup(); + (elm, consumed) + } + + /// get the fragements generated at this focus character and the + /// consumed locations + fn get_fragments(&self) -> Vec<Fragment>{ + let character: Option<Characteristic> = self.ch.get_characteristic(); + let mut elm: Vec<Fragment> = vec![]; + + let mut matched_intended = false; + + + if let Some(character) = character { + for &(ref blocks, ref fragments) in &character.intended_behavior { + let meet = blocks.iter().all(|ref b| self.can_be_strong_block(&b)); + if meet && !self.used_as_text() { + elm.extend(fragments.clone()); + matched_intended = true; } } @@ -302,7 +325,7 @@ impl<'g> FocusChar<'g> { // add only when signal is strong // or the signal has been intensified to strong let mut matched = false; - if !matched_enhance && !matched_intended { + if !matched_intended { for &(ref block, ref _signal, ref fragments) in &character.properties { // draw when a strong block and not used as text if self.is_strong_block(&block) && !self.used_as_text() { @@ -316,8 +339,7 @@ impl<'g> FocusChar<'g> { } } } - if !matched && !matched_intended && !matched_enhance - && !self.is_blank() { + if !matched && !matched_intended && !self.is_blank() { elm.push(Text(self.text())); } } else { @@ -328,9 +350,7 @@ impl<'g> FocusChar<'g> { } elm.sort(); elm.dedup(); - consumed.sort(); - consumed.dedup(); - (elm, consumed) + elm } fn get_settings(&self) -> Settings { diff --git a/svgbob/src/grid.rs b/svgbob/src/grid.rs index c90ec28..e1c97d1 100644 --- a/svgbob/src/grid.rs +++ b/svgbob/src/grid.rs @@ -113,29 +113,44 @@ impl Grid { FocusChar::new(&loc, self) } - /// vector of each elements arranged in rows x columns - /// returns all the elements and the consumed location - fn get_all_elements(&self) -> (Vec<Vec<Vec<Element>>>, Vec<Loc>) { + fn get_enhance_elements(&self) -> (Vec<Vec<Vec<Element>>>, Vec<Loc>){ let mut rows: Vec<Vec<Vec<Element>>> = Vec::with_capacity(self.index.len()); let mut all_consumed_loc: Vec<Loc> = vec![]; - let mut y = 0; - for line in &self.index { - let mut x = 0; + for (y,line) in self.index.iter().enumerate() { let mut row: Vec<Vec<Element>> = Vec::with_capacity(line.len()); - for _ in line { - let loc = Loc::new(x, y); + for (x,_cell) in line.iter().enumerate() { + let loc = Loc::new(x as i32, y as i32); let focus_char = self.get_focuschar(&loc); - let (cell_elements, consumed_loc) = focus_char.get_elements(); + let (cell_elements, consumed_loc) = focus_char.get_enhance_elements(); all_consumed_loc.extend(consumed_loc); row.push(cell_elements); - x += 1; } rows.push(row); - y += 1; } (rows, all_consumed_loc) } + /// vector of each elements arranged in rows x columns + /// returns all the elements and the consumed location + fn get_all_elements(&self) -> Vec<Vec<Vec<Element>>>{ + let (enhanced_rows, enhance_consumed_locs) = self.get_enhance_elements(); + let mut rows: Vec<Vec<Vec<Element>>> = Vec::with_capacity(self.index.len()); + rows.extend(enhanced_rows); + for (y, line) in self.index.iter().enumerate() { + let mut row: Vec<Vec<Element>> = Vec::with_capacity(line.len()); + for (x, _cell) in line.iter().enumerate() { + let loc = Loc::new(x as i32, y as i32); + if !enhance_consumed_locs.contains(&loc) { + let focus_char = self.get_focuschar(&loc); + let cell_elements = focus_char.get_elements(); + row.push(cell_elements); + } + } + rows.push(row); + } + rows + } + fn get_escaped_text_elements(&self) -> Vec<Element> { self.text_elm .iter() @@ -147,11 +162,11 @@ impl Grid { /// use this info for optimizing svg by checking closest neigbor fn get_svg_nodes(&self) -> Vec<SvgElement> { let mut nodes = vec![]; - let (mut elements, consumed_loc) = self.get_all_elements(); + let mut elements= self.get_all_elements(); let text_elm = self.get_escaped_text_elements(); elements.push(vec![text_elm]); let input = if self.settings.optimize { - let mut optimizer = Optimizer::new(elements, consumed_loc); + let mut optimizer = Optimizer::new(elements); let mut optimized_elements = optimizer.optimize(&self.settings); optimized_elements } else { diff --git a/svgbob/src/loc.rs b/svgbob/src/loc.rs index dbf0cf0..fa83d11 100644 --- a/svgbob/src/loc.rs +++ b/svgbob/src/loc.rs @@ -3,6 +3,7 @@ use location::Location; use location::Direction::{Top,Bottom,Left,Right,TopLeft,TopRight,BottomLeft,BottomRight}; /// Location of Block relative to the Grid +/// This the equivalent to the cell cation in the grid #[derive(Debug, Clone, PartialEq, PartialOrd, Eq)] pub struct Loc { pub x: i32, diff --git a/svgbob/src/optimizer.rs b/svgbob/src/optimizer.rs index 450f434..0f7609e 100644 --- a/svgbob/src/optimizer.rs +++ b/svgbob/src/optimizer.rs @@ -6,14 +6,12 @@ use element::Stroke; pub struct Optimizer { elements: Vec<Vec<Vec<Element>>>, - consumed_loc: Vec<Loc>, } impl Optimizer { - pub fn new(elements: Vec<Vec<Vec<Element>>>, consumed_loc: Vec<Loc>) -> Optimizer { + pub fn new(elements: Vec<Vec<Vec<Element>>>) -> Optimizer { Optimizer { elements: elements, - consumed_loc: consumed_loc, } } @@ -108,25 +106,20 @@ impl Optimizer { // TODO: order the elements in such a way that // the start -> end -> start chains nicely pub fn optimize(&self, settings: &Settings) -> Vec<Element> { - let mut completely_consumed_locs:Vec<Loc> = self.consumed_loc.clone(); let mut tracing_consumed_locs: Vec<(Loc,usize)> = vec![]; let mut optimized = vec![]; for (y, line) in self.elements.iter().enumerate() { for (x, cell) in line.iter().enumerate() { let loc = &Loc::new(x as i32, y as i32); - if !completely_consumed_locs.contains(loc) { - for (elm_index, elm) in cell.iter().enumerate() { - if !tracing_consumed_locs.contains(&(loc.clone(),elm_index)){ - let (traced, consumed) = self.trace_elements(elm, loc); - optimized.extend(traced); - tracing_consumed_locs.extend(consumed); - } + for (elm_index, elm) in cell.iter().enumerate() { + if !tracing_consumed_locs.contains(&(loc.clone(),elm_index)){ + let (traced, consumed) = self.trace_elements(elm, loc); + optimized.extend(traced); + tracing_consumed_locs.extend(consumed); } } } } - //let excess = self.get_excess_elements(&completely_consumed_locs, &tracing_consumed_locs); - //optimized.extend(excess); optimized.sort(); optimized.dedup(); if settings.compact_path { |