mirror of https://codeberg.org/topola/topola.git
geometry,primitive: move code for finding segs and bends to `Geometry`
This commit is contained in:
parent
684d0be641
commit
67f985c980
|
|
@ -37,7 +37,7 @@ impl TryFrom<GeometryIndex> for BendIndex {
|
|||
match index {
|
||||
GeometryIndex::FixedBend(index) => Ok(BendIndex::Fixed(index)),
|
||||
GeometryIndex::LooseBend(index) => Ok(BendIndex::Loose(index)),
|
||||
_ => unreachable!(),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@ impl TryFrom<GeometryWeight> for BendWeight {
|
|||
match weight {
|
||||
GeometryWeight::FixedBend(weight) => Ok(BendWeight::Fixed(weight)),
|
||||
GeometryWeight::LooseBend(weight) => Ok(BendWeight::Loose(weight)),
|
||||
_ => unreachable!(),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ impl TryFrom<GeometryIndex> for DotIndex {
|
|||
match index {
|
||||
GeometryIndex::FixedDot(index) => Ok(DotIndex::Fixed(index)),
|
||||
GeometryIndex::LooseDot(index) => Ok(DotIndex::Loose(index)),
|
||||
_ => unreachable!(),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -66,7 +66,7 @@ impl TryFrom<GeometryWeight> for DotWeight {
|
|||
match weight {
|
||||
GeometryWeight::FixedDot(weight) => Ok(DotWeight::Fixed(weight)),
|
||||
GeometryWeight::LooseDot(weight) => Ok(DotWeight::Loose(weight)),
|
||||
_ => unreachable!(),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -332,46 +332,6 @@ impl<
|
|||
.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> {
|
||||
self.graph
|
||||
.neighbors_directed(node, Incoming)
|
||||
|
|
@ -453,6 +413,56 @@ impl<
|
|||
.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> {
|
||||
&self.graph
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ impl TryFrom<GeometryIndex> for SegIndex {
|
|||
GeometryIndex::FixedSeg(index) => Ok(SegIndex::Fixed(index)),
|
||||
GeometryIndex::LoneLooseSeg(index) => Ok(SegIndex::LoneLoose(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::LoneLooseSeg(weight) => Ok(SegWeight::LoneLoose(weight)),
|
||||
GeometryWeight::SeqLooseSeg(weight) => Ok(SegWeight::SeqLoose(weight)),
|
||||
_ => unreachable!(),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -248,7 +248,7 @@ impl<'a> FixedDot<'a> {
|
|||
pub fn first_loose(&self, _band: BandIndex) -> Option<LooseIndex> {
|
||||
self.layout
|
||||
.geometry()
|
||||
.connections(self.index.into())
|
||||
.connecteds(self.index.into())
|
||||
.into_iter()
|
||||
.find_map(|ni| {
|
||||
let weight = self
|
||||
|
|
@ -278,47 +278,14 @@ impl<'a> GetLimbs for FixedDot<'a> {
|
|||
fn segs(&self) -> Vec<SegIndex> {
|
||||
self.layout
|
||||
.geometry()
|
||||
.connections(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,
|
||||
}
|
||||
})
|
||||
.connected_segs(self.index.into())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn bends(&self) -> Vec<BendIndex> {
|
||||
self.layout
|
||||
.geometry()
|
||||
.connections(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())
|
||||
.connected_bends(self.index.into())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
|
@ -332,18 +299,7 @@ impl<'a> LooseDot<'a> {
|
|||
pub fn seg(&self) -> Option<SeqLooseSegIndex> {
|
||||
self.layout
|
||||
.geometry()
|
||||
.connections(self.index.into())
|
||||
.into_iter()
|
||||
.filter(|ni| {
|
||||
matches!(
|
||||
self.layout
|
||||
.geometry()
|
||||
.graph()
|
||||
.node_weight(ni.node_index())
|
||||
.unwrap(),
|
||||
GeometryWeight::SeqLooseSeg(..)
|
||||
)
|
||||
})
|
||||
.connected_segs(self.index.into())
|
||||
.map(|ni| SeqLooseSegIndex::new(ni.node_index()))
|
||||
.next()
|
||||
}
|
||||
|
|
@ -351,18 +307,7 @@ impl<'a> LooseDot<'a> {
|
|||
pub fn bend(&self) -> LooseBendIndex {
|
||||
self.layout
|
||||
.geometry()
|
||||
.connections(self.index.into())
|
||||
.into_iter()
|
||||
.filter(|ni| {
|
||||
matches!(
|
||||
self.layout
|
||||
.geometry()
|
||||
.graph()
|
||||
.node_weight(ni.node_index())
|
||||
.unwrap(),
|
||||
GeometryWeight::LooseBend(..)
|
||||
)
|
||||
})
|
||||
.connected_bends(self.index.into())
|
||||
.map(|ni| LooseBendIndex::new(ni.node_index()))
|
||||
.next()
|
||||
.unwrap()
|
||||
|
|
|
|||
Loading…
Reference in New Issue