mirror of https://codeberg.org/topola/topola.git
autorouter: reorder code to avoid peeking in the edges iterator
This commit is contained in:
parent
83f3245e55
commit
c1db44c25f
|
|
@ -23,27 +23,31 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Autoroute {
|
pub struct Autoroute {
|
||||||
edge_indices: Peekable<EdgeIndices<usize>>,
|
edge_indices: EdgeIndices<usize>,
|
||||||
|
cur_edge: EdgeIndex<usize>,
|
||||||
navmesh: Navmesh, // Useful for debugging.
|
navmesh: Navmesh, // Useful for debugging.
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Autoroute {
|
impl Autoroute {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
edge_indices: EdgeIndices<usize>,
|
mut edge_indices: EdgeIndices<usize>,
|
||||||
autorouter: &mut Autorouter<impl RulesTrait>,
|
autorouter: &mut Autorouter<impl RulesTrait>,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
let mut peekable_edge_indices = edge_indices.peekable();
|
let Some(cur_edge) = edge_indices.next() else {
|
||||||
let Some(ratline) = peekable_edge_indices.peek() else {
|
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut layout = autorouter.layout.lock().unwrap();
|
let (from, to) = Self::edge_from_to(autorouter, cur_edge);
|
||||||
let (from_dot, to_dot) = Self::terminating_dots(autorouter, &mut layout, ratline);
|
let layout = autorouter.layout.lock().unwrap();
|
||||||
let navmesh = Self::next_navmesh(&layout, from_dot, to_dot);
|
let navmesh = Navmesh::new(&layout, from, to).ok()?;
|
||||||
Some(Self {
|
|
||||||
edge_indices: peekable_edge_indices,
|
let this = Self {
|
||||||
|
edge_indices,
|
||||||
|
cur_edge,
|
||||||
navmesh,
|
navmesh,
|
||||||
})
|
};
|
||||||
|
|
||||||
|
Some(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next<R: RulesTrait>(
|
pub fn next<R: RulesTrait>(
|
||||||
|
|
@ -51,37 +55,43 @@ impl Autoroute {
|
||||||
autorouter: &mut Autorouter<R>,
|
autorouter: &mut Autorouter<R>,
|
||||||
observer: &mut impl RouterObserverTrait<R>,
|
observer: &mut impl RouterObserverTrait<R>,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
let Some(ratline) = self.edge_indices.next() else {
|
let (navmesh, from, to) = {
|
||||||
return None;
|
let (from, to) = self.from_to(autorouter);
|
||||||
};
|
let layout = autorouter.layout.lock().unwrap();
|
||||||
|
let navmesh = Navmesh::new(&layout, from, to).ok()?;
|
||||||
let (navmesh, from_dot, to_dot) = {
|
(navmesh, from, to)
|
||||||
let mut layout = autorouter.layout.lock().unwrap();
|
|
||||||
let (from_dot, to_dot) = Self::terminating_dots(autorouter, &mut layout, &ratline);
|
|
||||||
let navmesh = Self::next_navmesh(&layout, from_dot, to_dot);
|
|
||||||
(navmesh, from_dot, to_dot)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let router = Router::new_with_navmesh(
|
let router = Router::new_with_navmesh(
|
||||||
&mut autorouter.layout,
|
&mut autorouter.layout,
|
||||||
from_dot,
|
from,
|
||||||
to_dot,
|
to,
|
||||||
std::mem::replace(&mut self.navmesh, navmesh),
|
std::mem::replace(&mut self.navmesh, navmesh),
|
||||||
);
|
);
|
||||||
router.unwrap().route_band(to_dot, 100.0, observer);
|
router.unwrap().route_band(to, 100.0, observer);
|
||||||
|
|
||||||
|
if let Some(cur_edge) = self.edge_indices.next() {
|
||||||
|
self.cur_edge = cur_edge;
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn terminating_dots<R: RulesTrait>(
|
pub fn from_to<R: RulesTrait>(
|
||||||
|
&self,
|
||||||
autorouter: &Autorouter<R>,
|
autorouter: &Autorouter<R>,
|
||||||
layout: &mut Layout<R>,
|
|
||||||
ratline: &EdgeIndex<usize>,
|
|
||||||
) -> (FixedDotIndex, FixedDotIndex) {
|
) -> (FixedDotIndex, FixedDotIndex) {
|
||||||
let (from, to) = autorouter
|
Self::edge_from_to(autorouter, self.cur_edge)
|
||||||
.ratsnest
|
}
|
||||||
.graph()
|
|
||||||
.edge_endpoints(*ratline)
|
fn edge_from_to<R: RulesTrait>(
|
||||||
.unwrap();
|
autorouter: &Autorouter<R>,
|
||||||
|
edge: EdgeIndex<usize>,
|
||||||
|
) -> (FixedDotIndex, FixedDotIndex) {
|
||||||
|
let mut layout = autorouter.layout.lock().unwrap();
|
||||||
|
let (from, to) = autorouter.ratsnest.graph().edge_endpoints(edge).unwrap();
|
||||||
|
|
||||||
let from_dot = match autorouter
|
let from_dot = match autorouter
|
||||||
.ratsnest
|
.ratsnest
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue