Create new `Band` struct to pass around whole bands

This commit is contained in:
Mikolaj Wielgus 2023-10-05 19:52:59 +00:00
parent 894fa654cf
commit 0e8a6d6e42
4 changed files with 111 additions and 3 deletions

102
src/band.rs Normal file
View File

@ -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<TaggedIndex>,
}
impl Band {
pub fn new(
from: DotIndex,
to: DotIndex,
graph: &StableDiGraph<TaggedWeight, Label, usize>,
) -> 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<TaggedIndex> for Band {
fn interior(&self) -> Vec<TaggedIndex> {
// FIXME: Unnecessary clone. There should be a better way to do it.
self.interior.clone()
}
}
impl Ends<DotIndex, DotIndex> 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<TaggedWeight, Label, usize>,
index: TaggedIndex,
}
impl<'a> Iterator for BandIterator<'a> {
type Item = TaggedIndex;
fn next(&mut self) -> Option<Self::Item> {
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<TaggedWeight, Label, usize>) -> Self::Iter {
BandIterator {
graph,
index: self.from.tag(),
}
}
}
impl Ends<DotIndex, DotIndex> for Band {
fn ends(&self) -> (DotIndex, DotIndex) {
(self.from, self.to)
}
}*/

View File

@ -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))]

View File

@ -10,6 +10,7 @@ macro_rules! dbg_dot {
#[macro_use]
mod graph;
mod astar;
mod band;
mod bow;
mod draw;
mod guide;

View File

@ -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 {