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 std::sync::{Arc, Mutex};
use geo::Point; use geo::Point;
use petgraph::visit::{EdgeRef, IntoEdgeReferences}; use petgraph::{
graph::EdgeIndices,
visit::{EdgeRef, IntoEdgeReferences},
};
use spade::InsertionError; use spade::InsertionError;
use crate::{ use crate::{
@ -12,6 +15,57 @@ use crate::{
triangulation::GetVertexIndex, 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> { pub struct Autorouter<R: RulesTrait> {
ratsnest: Ratsnest, ratsnest: Ratsnest,
router: Router<R>, router: Router<R>,
@ -27,38 +81,15 @@ impl<R: RulesTrait> Autorouter<R> {
} }
pub fn autoroute(&mut self, observer: &mut impl RouterObserverTrait<R>) { pub fn autoroute(&mut self, observer: &mut impl RouterObserverTrait<R>) {
for ratline in self.ratsnest.graph().edge_references() { let mut it = self.autoroute_iter();
let from = self while let Some(()) = it.next(self, observer) {
.ratsnest //
.graph() }
.node_weight(ratline.source()) }
.unwrap()
.vertex_index();
let to = self
.ratsnest
.graph()
.node_weight(ratline.target())
.unwrap()
.vertex_index();
let (from_dot, to_dot) = { pub fn autoroute_iter(&mut self) -> Autoroute {
let layout = self.router.layout(); Autoroute {
let mut layout = layout.lock().unwrap(); edge_indices: self.ratsnest.graph().edge_indices(),
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);
} }
} }

View File

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