autorouter: make it possible to delegate the loop outside `.autoroute()`

This commit is contained in:
Mikolaj Wielgus 2024-05-05 17:55:47 +02:00
parent b82f5417c3
commit 946f2dbd6c
2 changed files with 68 additions and 37 deletions

View File

@ -1,7 +1,10 @@
use std::sync::{Arc, Mutex};
use geo::Point;
use petgraph::visit::{EdgeRef, IntoEdgeReferences};
use petgraph::{
graph::EdgeIndices,
visit::{EdgeRef, IntoEdgeReferences},
};
use spade::InsertionError;
use crate::{
@ -12,6 +15,57 @@ use crate::{
triangulation::GetVertexIndex,
};
pub struct Autoroute {
edge_indices: EdgeIndices<usize>,
}
impl Autoroute {
pub fn next<R: RulesTrait>(
&mut self,
autorouter: &mut Autorouter<R>,
observer: &mut impl RouterObserverTrait<R>,
) -> Option<()> {
let Some(ratline) = self.edge_indices.next() else {
return None;
};
let (from, to) = autorouter.ratsnest.graph().edge_endpoints(ratline).unwrap();
let (from_dot, to_dot) = {
let layout = autorouter.router.layout();
let mut layout = layout.lock().unwrap();
let from_dot = match autorouter
.ratsnest
.graph()
.node_weight(from)
.unwrap()
.vertex_index()
{
RatsnestVertexIndex::FixedDot(dot) => dot,
RatsnestVertexIndex::Zone(zone) => layout.zone_apex(zone),
};
let to_dot = match autorouter
.ratsnest
.graph()
.node_weight(to)
.unwrap()
.vertex_index()
{
RatsnestVertexIndex::FixedDot(dot) => dot,
RatsnestVertexIndex::Zone(zone) => layout.zone_apex(zone),
};
(from_dot, to_dot)
};
autorouter
.router
.route_band(from_dot, to_dot, 100.0, observer);
Some(())
}
}
pub struct Autorouter<R: RulesTrait> {
ratsnest: Ratsnest,
router: Router<R>,
@ -27,38 +81,15 @@ impl<R: RulesTrait> Autorouter<R> {
}
pub fn autoroute(&mut self, observer: &mut impl RouterObserverTrait<R>) {
for ratline in self.ratsnest.graph().edge_references() {
let from = self
.ratsnest
.graph()
.node_weight(ratline.source())
.unwrap()
.vertex_index();
let to = self
.ratsnest
.graph()
.node_weight(ratline.target())
.unwrap()
.vertex_index();
let mut it = self.autoroute_iter();
while let Some(()) = it.next(self, observer) {
//
}
}
let (from_dot, to_dot) = {
let layout = self.router.layout();
let mut layout = layout.lock().unwrap();
let from_dot = match from {
RatsnestVertexIndex::FixedDot(dot) => dot,
RatsnestVertexIndex::Zone(zone) => layout.zone_apex(zone),
};
let to_dot = match to {
RatsnestVertexIndex::FixedDot(dot) => dot,
RatsnestVertexIndex::Zone(zone) => layout.zone_apex(zone),
};
(from_dot, to_dot)
};
self.router.route_band(from_dot, to_dot, 100.0, observer);
pub fn autoroute_iter(&mut self) -> Autoroute {
Autoroute {
edge_indices: self.ratsnest.graph().edge_indices(),
}
}

View File

@ -4,9 +4,9 @@ use enum_dispatch::enum_dispatch;
use geo::Point;
use petgraph::{
data::{Element, FromElements},
stable_graph::{NodeIndex, StableGraph, StableUnGraph},
graph::{NodeIndex, UnGraph},
unionfind::UnionFind,
visit::{self, EdgeRef, IntoEdgeReferences, NodeIndexable},
visit::{EdgeRef, IntoEdgeReferences, NodeIndexable},
};
use spade::{HasPosition, InsertionError, Point2};
@ -53,7 +53,7 @@ impl HasPosition for VertexWeight {
}
pub struct Ratsnest {
graph: StableUnGraph<VertexWeight, TriangulationEdgeWeight, usize>,
graph: UnGraph<VertexWeight, TriangulationEdgeWeight, usize>,
}
impl Ratsnest {
@ -65,7 +65,7 @@ impl Ratsnest {
}
let mut this = Self {
graph: StableUnGraph::default(),
graph: UnGraph::default(),
};
let mut triangulations = HashMap::new();
@ -149,7 +149,7 @@ impl Ratsnest {
Ok(this)
}
pub fn graph(&self) -> &StableUnGraph<VertexWeight, TriangulationEdgeWeight, usize> {
pub fn graph(&self) -> &UnGraph<VertexWeight, TriangulationEdgeWeight, usize> {
&self.graph
}
}