geometry,primitive: move code for finding segs and bends to `Geometry`

This commit is contained in:
Mikolaj Wielgus 2024-01-29 02:25:32 +00:00
parent 684d0be641
commit 67f985c980
5 changed files with 61 additions and 106 deletions

View File

@ -37,7 +37,7 @@ impl TryFrom<GeometryIndex> for BendIndex {
match index { match index {
GeometryIndex::FixedBend(index) => Ok(BendIndex::Fixed(index)), GeometryIndex::FixedBend(index) => Ok(BendIndex::Fixed(index)),
GeometryIndex::LooseBend(index) => Ok(BendIndex::Loose(index)), GeometryIndex::LooseBend(index) => Ok(BendIndex::Loose(index)),
_ => unreachable!(), _ => Err(()),
} }
} }
} }
@ -65,7 +65,7 @@ impl TryFrom<GeometryWeight> for BendWeight {
match weight { match weight {
GeometryWeight::FixedBend(weight) => Ok(BendWeight::Fixed(weight)), GeometryWeight::FixedBend(weight) => Ok(BendWeight::Fixed(weight)),
GeometryWeight::LooseBend(weight) => Ok(BendWeight::Loose(weight)), GeometryWeight::LooseBend(weight) => Ok(BendWeight::Loose(weight)),
_ => unreachable!(), _ => Err(()),
} }
} }
} }

View File

@ -38,7 +38,7 @@ impl TryFrom<GeometryIndex> for DotIndex {
match index { match index {
GeometryIndex::FixedDot(index) => Ok(DotIndex::Fixed(index)), GeometryIndex::FixedDot(index) => Ok(DotIndex::Fixed(index)),
GeometryIndex::LooseDot(index) => Ok(DotIndex::Loose(index)), GeometryIndex::LooseDot(index) => Ok(DotIndex::Loose(index)),
_ => unreachable!(), _ => Err(()),
} }
} }
} }
@ -66,7 +66,7 @@ impl TryFrom<GeometryWeight> for DotWeight {
match weight { match weight {
GeometryWeight::FixedDot(weight) => Ok(DotWeight::Fixed(weight)), GeometryWeight::FixedDot(weight) => Ok(DotWeight::Fixed(weight)),
GeometryWeight::LooseDot(weight) => Ok(DotWeight::Loose(weight)), GeometryWeight::LooseDot(weight) => Ok(DotWeight::Loose(weight)),
_ => unreachable!(), _ => Err(()),
} }
} }
} }

View File

@ -332,46 +332,6 @@ impl<
.unwrap() .unwrap()
} }
pub fn seg_joints(&self, seg: SI) -> (DI, DI) {
let v: Vec<_> = self.connections(seg.into()).collect();
(
v[0].try_into().unwrap_or_else(|_| unreachable!()),
v[1].try_into().unwrap_or_else(|_| unreachable!()),
)
}
pub fn bend_joints(&self, bend: BI) -> (DI, DI) {
let v: Vec<_> = self.connections(bend.into()).collect();
(
v[0].try_into().unwrap_or_else(|_| unreachable!()),
v[1].try_into().unwrap_or_else(|_| unreachable!()),
)
}
pub fn connections(&self, node: GI) -> impl Iterator<Item = GI> + '_ {
self.graph
.neighbors_undirected(node.node_index())
.filter(move |ni| {
matches!(
self.graph
.edge_weight(
self.graph
.find_edge_undirected(node.node_index(), *ni)
.unwrap()
.0,
)
.unwrap(),
GeometryLabel::Connection
)
})
.map(|ni| {
self.weight(ni)
.retag(ni)
.try_into()
.unwrap_or_else(|_| unreachable!())
})
}
pub fn first_rail(&self, node: NodeIndex<usize>) -> Option<BI> { pub fn first_rail(&self, node: NodeIndex<usize>) -> Option<BI> {
self.graph self.graph
.neighbors_directed(node, Incoming) .neighbors_directed(node, Incoming)
@ -453,6 +413,56 @@ impl<
.next() .next()
} }
pub fn connecteds(&self, node: GI) -> impl Iterator<Item = GI> + '_ {
self.graph
.neighbors_undirected(node.node_index())
.filter(move |ni| {
matches!(
self.graph
.edge_weight(
self.graph
.find_edge_undirected(node.node_index(), *ni)
.unwrap()
.0,
)
.unwrap(),
GeometryLabel::Connection
)
})
.map(|ni| {
self.weight(ni)
.retag(ni)
.try_into()
.unwrap_or_else(|_| unreachable!())
})
}
pub fn seg_joints(&self, seg: SI) -> (DI, DI) {
let v: Vec<_> = self.connecteds(seg.into()).collect();
(
v[0].try_into().unwrap_or_else(|_| unreachable!()),
v[1].try_into().unwrap_or_else(|_| unreachable!()),
)
}
pub fn bend_joints(&self, bend: BI) -> (DI, DI) {
let v: Vec<_> = self.connecteds(bend.into()).collect();
(
v[0].try_into().unwrap_or_else(|_| unreachable!()),
v[1].try_into().unwrap_or_else(|_| unreachable!()),
)
}
pub fn connected_segs(&self, dot: DI) -> impl Iterator<Item = SI> + '_ {
self.connecteds(dot.into())
.filter_map(|ni| ni.try_into().ok())
}
pub fn connected_bends(&self, dot: DI) -> impl Iterator<Item = BI> + '_ {
self.connecteds(dot.into())
.filter_map(|ni| ni.try_into().ok())
}
pub fn graph(&self) -> &StableDiGraph<GW, GeometryLabel, usize> { pub fn graph(&self) -> &StableDiGraph<GW, GeometryLabel, usize> {
&self.graph &self.graph
} }

View File

@ -39,7 +39,7 @@ impl TryFrom<GeometryIndex> for SegIndex {
GeometryIndex::FixedSeg(index) => Ok(SegIndex::Fixed(index)), GeometryIndex::FixedSeg(index) => Ok(SegIndex::Fixed(index)),
GeometryIndex::LoneLooseSeg(index) => Ok(SegIndex::LoneLoose(index)), GeometryIndex::LoneLooseSeg(index) => Ok(SegIndex::LoneLoose(index)),
GeometryIndex::SeqLooseSeg(index) => Ok(SegIndex::SeqLoose(index)), GeometryIndex::SeqLooseSeg(index) => Ok(SegIndex::SeqLoose(index)),
_ => unreachable!(), _ => Err(()),
} }
} }
} }
@ -70,7 +70,7 @@ impl TryFrom<GeometryWeight> for SegWeight {
GeometryWeight::FixedSeg(weight) => Ok(SegWeight::Fixed(weight)), GeometryWeight::FixedSeg(weight) => Ok(SegWeight::Fixed(weight)),
GeometryWeight::LoneLooseSeg(weight) => Ok(SegWeight::LoneLoose(weight)), GeometryWeight::LoneLooseSeg(weight) => Ok(SegWeight::LoneLoose(weight)),
GeometryWeight::SeqLooseSeg(weight) => Ok(SegWeight::SeqLoose(weight)), GeometryWeight::SeqLooseSeg(weight) => Ok(SegWeight::SeqLoose(weight)),
_ => unreachable!(), _ => Err(()),
} }
} }
} }

View File

@ -248,7 +248,7 @@ impl<'a> FixedDot<'a> {
pub fn first_loose(&self, _band: BandIndex) -> Option<LooseIndex> { pub fn first_loose(&self, _band: BandIndex) -> Option<LooseIndex> {
self.layout self.layout
.geometry() .geometry()
.connections(self.index.into()) .connecteds(self.index.into())
.into_iter() .into_iter()
.find_map(|ni| { .find_map(|ni| {
let weight = self let weight = self
@ -278,47 +278,14 @@ impl<'a> GetLimbs for FixedDot<'a> {
fn segs(&self) -> Vec<SegIndex> { fn segs(&self) -> Vec<SegIndex> {
self.layout self.layout
.geometry() .geometry()
.connections(self.index.into()) .connected_segs(self.index.into())
.into_iter()
.filter_map(|ni| {
match self
.layout
.geometry()
.graph()
.node_weight(ni.node_index())
.unwrap()
{
GeometryWeight::FixedSeg(..) => {
Some(SegIndex::Fixed(FixedSegIndex::new(ni.node_index())))
}
GeometryWeight::LoneLooseSeg(..) => Some(SegIndex::LoneLoose(
LoneLooseSegIndex::new(ni.node_index()).into(),
)),
GeometryWeight::SeqLooseSeg(..) => Some(SegIndex::SeqLoose(
SeqLooseSegIndex::new(ni.node_index()).into(),
)),
_ => None,
}
})
.collect() .collect()
} }
fn bends(&self) -> Vec<BendIndex> { fn bends(&self) -> Vec<BendIndex> {
self.layout self.layout
.geometry() .geometry()
.connections(self.index.into()) .connected_bends(self.index.into())
.into_iter()
.filter(|ni| {
matches!(
self.layout
.geometry()
.graph()
.node_weight(ni.node_index())
.unwrap(),
GeometryWeight::FixedBend(..)
)
})
.map(|ni| FixedBendIndex::new(ni.node_index()).into())
.collect() .collect()
} }
} }
@ -332,18 +299,7 @@ impl<'a> LooseDot<'a> {
pub fn seg(&self) -> Option<SeqLooseSegIndex> { pub fn seg(&self) -> Option<SeqLooseSegIndex> {
self.layout self.layout
.geometry() .geometry()
.connections(self.index.into()) .connected_segs(self.index.into())
.into_iter()
.filter(|ni| {
matches!(
self.layout
.geometry()
.graph()
.node_weight(ni.node_index())
.unwrap(),
GeometryWeight::SeqLooseSeg(..)
)
})
.map(|ni| SeqLooseSegIndex::new(ni.node_index())) .map(|ni| SeqLooseSegIndex::new(ni.node_index()))
.next() .next()
} }
@ -351,18 +307,7 @@ impl<'a> LooseDot<'a> {
pub fn bend(&self) -> LooseBendIndex { pub fn bend(&self) -> LooseBendIndex {
self.layout self.layout
.geometry() .geometry()
.connections(self.index.into()) .connected_bends(self.index.into())
.into_iter()
.filter(|ni| {
matches!(
self.layout
.geometry()
.graph()
.node_weight(ni.node_index())
.unwrap(),
GeometryWeight::LooseBend(..)
)
})
.map(|ni| LooseBendIndex::new(ni.node_index())) .map(|ni| LooseBendIndex::new(ni.node_index()))
.next() .next()
.unwrap() .unwrap()