Route with A* step by step instead of only after A*

This commit is contained in:
Mikolaj Wielgus 2023-09-01 01:35:48 +02:00
parent 1ac8dd2828
commit b5f9a5957a
4 changed files with 33 additions and 35 deletions

View File

@ -171,6 +171,7 @@ impl<'a> Draw<'a> {
.prev()
.map(|prev_dot| {
self.layout.remove_interior(&segbend);
self.layout.remove(head.dot);
Head {
dot: prev_dot,

View File

@ -307,14 +307,14 @@ fn render_times(
}
}
}
let envelope = shape.envelope();
/*let envelope = shape.envelope();
let _ = canvas.rectangle(
envelope.lower()[0] as i16,
envelope.lower()[1] as i16,
envelope.upper()[0] as i16,
envelope.upper()[1] as i16,
Color::RGB(100, 100, 100),
);
);*/
}
/*for edge in router.routeedges() {

View File

@ -36,17 +36,17 @@ impl<'a> Route<'a> {
}
}
pub fn finish(&mut self, trace: Trace, into: VertexIndex, width: f64) -> Result<(), ()> {
pub fn finish(&mut self, trace: &mut Trace, into: VertexIndex, width: f64) -> Result<(), ()> {
let into_dot = self.mesh.dot(into);
self.draw().finish(trace.head, into_dot, width)
}
pub fn rework_path(
&mut self,
mut trace: Trace,
trace: &mut Trace,
path: &[VertexIndex],
width: f64,
) -> Result<Trace, ()> {
) -> Result<(), ()> {
let prefix_length = trace
.path
.iter()
@ -55,41 +55,37 @@ impl<'a> Route<'a> {
.count();
let length = trace.path.len();
trace = self.undo_path(trace, length - prefix_length)?;
self.undo_path(trace, length - prefix_length)?;
self.path(trace, &path[prefix_length..], width)
}
pub fn path(
&mut self,
mut trace: Trace,
path: &[VertexIndex],
width: f64,
) -> Result<Trace, ()> {
pub fn path(&mut self, trace: &mut Trace, path: &[VertexIndex], width: f64) -> Result<(), ()> {
for vertex in path {
trace = self.step(trace, *vertex, width)?;
self.step(trace, *vertex, width)?;
}
Ok(trace)
Ok(())
}
pub fn undo_path(&mut self, mut trace: Trace, step_count: usize) -> Result<Trace, ()> {
pub fn undo_path(&mut self, trace: &mut Trace, step_count: usize) -> Result<(), ()> {
for _ in 0..step_count {
trace = self.undo_step(trace)?;
self.undo_step(trace)?;
}
Ok(trace)
Ok(())
}
pub fn step(&mut self, mut trace: Trace, to: VertexIndex, width: f64) -> Result<Trace, ()> {
pub fn step(&mut self, trace: &mut Trace, to: VertexIndex, width: f64) -> Result<(), ()> {
let to_dot = self.mesh.dot(to);
trace.head = self
.draw()
.segbend_around_dot(trace.head, to_dot, true, width)?;
Ok(trace)
trace.path.push(to);
Ok(())
}
pub fn undo_step(&mut self, mut trace: Trace) -> Result<Trace, ()> {
pub fn undo_step(&mut self, trace: &mut Trace) -> Result<(), ()> {
trace.head = self.draw().undo_segbend(trace.head).unwrap();
trace.path.pop();
Ok(trace)
Ok(())
}
fn draw(&mut self) -> Draw {

View File

@ -36,31 +36,32 @@ impl Router {
let mut mesh = Mesh::new();
mesh.triangulate(&self.layout)?;
let mut route = self.route(&mesh);
let mut trace = route.start(mesh.vertex(from));
let (_cost, path) = astar(
&mesh,
mesh.vertex(from),
|node, tracker| {
let new_path = tracker.reconstruct_path_to(node);
(node != mesh.vertex(to)).then_some(0)
if node == mesh.vertex(to) {
route
.rework_path(&mut trace, &new_path[..new_path.len() - 1], 5.0)
.ok();
route
.finish(&mut trace, new_path[new_path.len() - 1], 5.0)
.ok();
None
} else {
route.rework_path(&mut trace, &new_path, 5.0).ok();
Some(0)
}
},
|_edge| 1,
|_| 0,
)
.unwrap(); // TODO.
/*let path: Vec<DotIndex> = mesh_path
.iter()
.map(|vertex| self.mesh.dot(*vertex))
.collect();*/
let mut trace = self.route(&mesh).start(path[0]);
trace = self
.route(&mesh)
.path(trace, &path[1..(path.len() - 1)], 5.0)
.unwrap(); // TODO.
let _ = self.route(&mesh).finish(trace, path[path.len() - 1], 5.0);
Ok(())
}