From 0e8a6d6e42f4e5983053450c4978ba5bf20cb04c Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Thu, 5 Oct 2023 19:52:59 +0000 Subject: [PATCH] Create new `Band` struct to pass around whole bands --- src/band.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/layout.rs | 5 +++ src/main.rs | 1 + src/rules.rs | 6 +-- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 src/band.rs diff --git a/src/band.rs b/src/band.rs new file mode 100644 index 0000000..500960e --- /dev/null +++ b/src/band.rs @@ -0,0 +1,102 @@ +use petgraph::stable_graph::StableDiGraph; + +use crate::{ + graph::{DotIndex, Ends, Interior, Label, Tag, TaggedIndex, TaggedWeight}, + primitive::Primitive, +}; + +pub struct Band { + from: DotIndex, + to: DotIndex, + interior: Vec, +} + +impl Band { + pub fn new( + from: DotIndex, + to: DotIndex, + graph: &StableDiGraph, + ) -> Self { + let mut this = Self { + from, + to, + interior: vec![], + }; + let mut prev_index = from.tag(); + + while let Some(index) = untag!(prev_index, Primitive::new(prev_index, graph).tagged_next()) + { + this.interior.push(index); + prev_index = index; + } + + this + } +} + +impl Interior for Band { + fn interior(&self) -> Vec { + // FIXME: Unnecessary clone. There should be a better way to do it. + self.interior.clone() + } +} + +impl Ends for Band { + fn ends(&self) -> (DotIndex, DotIndex) { + (self.from, self.to) + } +} +/*use petgraph::stable_graph::StableDiGraph; + +use crate::{ + graph::{DotIndex, Ends, Interior, Label, Tag, TaggedIndex, TaggedWeight}, + primitive::Primitive, +}; + +pub struct Band { + from: DotIndex, + to: DotIndex, +} + +impl Band { + pub fn new(from: DotIndex, to: DotIndex) -> Self { + Self { from, to } + } +} + +struct BandIterator<'a> { + graph: &'a StableDiGraph, + index: TaggedIndex, +} + +impl<'a> Iterator for BandIterator<'a> { + type Item = TaggedIndex; + + fn next(&mut self) -> Option { + let index = self.index; + let next_index = untag!(index, Primitive::new(index, self.graph).tagged_next()); + + if let Some(next_index) = next_index { + self.index = next_index; + } + + next_index + } +} + +impl<'a> Interior<'a, TaggedIndex> for Band { + type Iter = BandIterator<'a>; + + fn interior(&self, graph: &'a StableDiGraph) -> Self::Iter { + BandIterator { + graph, + index: self.from.tag(), + } + } +} + +impl Ends for Band { + fn ends(&self) -> (DotIndex, DotIndex) { + (self.from, self.to) + } +}*/ diff --git a/src/layout.rs b/src/layout.rs index 18ed68c..1582a78 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -7,6 +7,7 @@ use rstar::primitives::GeomWithData; use rstar::RTree; use spade::Triangulation; +use crate::band::Band; use crate::bow::Bow; use crate::graph::{ BendIndex, BendWeight, DotIndex, DotWeight, Index, Interior, Label, SegIndex, SegWeight, Tag, @@ -206,6 +207,10 @@ impl Layout { Segbend::from_dot_next(dot, &self.graph) } + pub fn band(&self, from: DotIndex, to: DotIndex) -> Band { + Band::new(from, to, &self.graph) + } + #[debug_ensures(ret.is_ok() -> self.graph.node_count() == old(self.graph.node_count()))] #[debug_ensures(ret.is_ok() -> self.graph.edge_count() == old(self.graph.edge_count()))] #[debug_ensures(ret.is_err() -> self.graph.node_count() == old(self.graph.node_count() - 1))] diff --git a/src/main.rs b/src/main.rs index 53338e8..2dc3dbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ macro_rules! dbg_dot { #[macro_use] mod graph; mod astar; +mod band; mod bow; mod draw; mod guide; diff --git a/src/rules.rs b/src/rules.rs index de05878..718a6b3 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -25,10 +25,10 @@ pub struct Rules { impl Rules { pub fn new() -> Self { - let mut me = Self { + let mut this = Self { rulesets: Default::default(), }; - me.rulesets[0] = Some(HashMap::from([( + this.rulesets[0] = Some(HashMap::from([( Conditions { lower_net: None, higher_net: None, @@ -37,7 +37,7 @@ impl Rules { }, Ruleset::new(), )])); - me + this } pub fn ruleset(&self, conditions: &Conditions) -> &Ruleset {