Implement .next() and .prev() methods to traverse along traces

This commit is contained in:
Mikolaj Wielgus 2023-07-23 01:36:22 +02:00
parent 925448802a
commit 9c75151d3a
2 changed files with 18 additions and 4 deletions

View File

@ -35,7 +35,7 @@ impl Mesh {
pub fn add_seg(&mut self, from: DotIndex, to: DotIndex, weight: SegWeight) -> SegIndex { pub fn add_seg(&mut self, from: DotIndex, to: DotIndex, weight: SegWeight) -> SegIndex {
let seg = SegIndex::new(self.graph.add_node(TaggedWeight::Seg(weight))); let seg = SegIndex::new(self.graph.add_node(TaggedWeight::Seg(weight)));
self.graph.add_edge(seg.index, from.index, Label::End); self.graph.add_edge(from.index, seg.index, Label::End);
self.graph.add_edge(seg.index, to.index, Label::End); self.graph.add_edge(seg.index, to.index, Label::End);
self.rtree.insert(RTreeWrapper::new(self.primitive(seg).shape(), TaggedIndex::Seg(seg))); self.rtree.insert(RTreeWrapper::new(self.primitive(seg).shape(), TaggedIndex::Seg(seg)));
@ -59,7 +59,7 @@ impl Mesh {
pub fn add_core_bend(&mut self, from: DotIndex, to: DotIndex, core: DotIndex, weight: BendWeight) -> BendIndex { pub fn add_core_bend(&mut self, from: DotIndex, to: DotIndex, core: DotIndex, weight: BendWeight) -> BendIndex {
let bend = BendIndex::new(self.graph.add_node(TaggedWeight::Bend(weight))); let bend = BendIndex::new(self.graph.add_node(TaggedWeight::Bend(weight)));
self.graph.add_edge(bend.index, from.index, Label::End); self.graph.add_edge(from.index, bend.index, Label::End);
self.graph.add_edge(bend.index, to.index, Label::End); self.graph.add_edge(bend.index, to.index, Label::End);
self.graph.add_edge(bend.index, core.index, Label::Core); self.graph.add_edge(bend.index, core.index, Label::Core);

View File

@ -49,9 +49,23 @@ impl<'a, Weight> Primitive<'a, Weight> {
} }
} }
pub fn ends(&self) -> Vec<DotIndex> { pub fn next(&self) -> Option<TaggedIndex> {
self.graph.neighbors(self.index.index) self.graph.neighbors_directed(self.index.index, Outgoing)
.filter(|ni| self.graph.edge_weight(self.graph.find_edge(*ni, self.index.index).unwrap()).unwrap().is_end())
.map(|ni| Index::<Label>::new(ni).retag(*self.graph.node_weight(ni).unwrap()))
.next()
}
pub fn prev(&self) -> Option<TaggedIndex> {
self.graph.neighbors_directed(self.index.index, Incoming)
.filter(|ni| self.graph.edge_weight(self.graph.find_edge(self.index.index, *ni).unwrap()).unwrap().is_end()) .filter(|ni| self.graph.edge_weight(self.graph.find_edge(self.index.index, *ni).unwrap()).unwrap().is_end())
.map(|ni| Index::<Label>::new(ni).retag(*self.graph.node_weight(ni).unwrap()))
.next()
}
pub fn ends(&self) -> Vec<DotIndex> {
self.graph.neighbors_undirected(self.index.index)
.filter(|ni| self.graph.edge_weight(self.graph.find_edge_undirected(self.index.index, *ni).unwrap().0).unwrap().is_end())
.filter(|ni| self.graph.node_weight(*ni).unwrap().is_dot()) .filter(|ni| self.graph.node_weight(*ni).unwrap().is_dot())
.map(|ni| DotIndex::new(ni)) .map(|ni| DotIndex::new(ni))
.collect() .collect()