Pass `width` as an argument instead of holding it in `Route`

This commit is contained in:
Mikolaj Wielgus 2023-08-31 21:17:25 +02:00
parent e5ba7401c1
commit cd5365c26f
2 changed files with 24 additions and 18 deletions

View File

@ -15,16 +15,14 @@ pub struct Route<'a> {
layout: &'a mut Layout, layout: &'a mut Layout,
rules: &'a Rules, rules: &'a Rules,
mesh: &'a Mesh, mesh: &'a Mesh,
width: f64,
} }
impl<'a> Route<'a> { impl<'a> Route<'a> {
pub fn new(layout: &'a mut Layout, rules: &'a Rules, mesh: &'a Mesh, width: f64) -> Self { pub fn new(layout: &'a mut Layout, rules: &'a Rules, mesh: &'a Mesh) -> Self {
Route { Route {
layout, layout,
rules, rules,
mesh, mesh,
width,
} }
} }
@ -38,13 +36,17 @@ impl<'a> Route<'a> {
} }
} }
pub fn finish(&mut self, trace: Trace, into: VertexIndex) { pub fn finish(&mut self, trace: Trace, into: VertexIndex, width: f64) -> Result<(), ()> {
let into_dot = self.mesh.dot(into); let into_dot = self.mesh.dot(into);
let width = self.width; self.draw().finish(trace.head, into_dot, width)
self.draw().finish(trace.head, into_dot, width);
} }
pub fn rework_path(&mut self, mut trace: Trace, path: &[VertexIndex]) -> Result<Trace, ()> { pub fn rework_path(
&mut self,
mut trace: Trace,
path: &[VertexIndex],
width: f64,
) -> Result<Trace, ()> {
let prefix_length = trace let prefix_length = trace
.path .path
.iter() .iter()
@ -54,12 +56,17 @@ impl<'a> Route<'a> {
let length = trace.path.len(); let length = trace.path.len();
trace = self.undo_path(trace, length - prefix_length)?; trace = self.undo_path(trace, length - prefix_length)?;
self.path(trace, &path[prefix_length..]) self.path(trace, &path[prefix_length..], width)
} }
pub fn path(&mut self, mut trace: Trace, path: &[VertexIndex]) -> Result<Trace, ()> { pub fn path(
&mut self,
mut trace: Trace,
path: &[VertexIndex],
width: f64,
) -> Result<Trace, ()> {
for vertex in path { for vertex in path {
trace = self.step(trace, *vertex)?; trace = self.step(trace, *vertex, width)?;
} }
Ok(trace) Ok(trace)
} }
@ -71,9 +78,8 @@ impl<'a> Route<'a> {
Ok(trace) Ok(trace)
} }
pub fn step(&mut self, mut trace: Trace, to: VertexIndex) -> Result<Trace, ()> { pub fn step(&mut self, mut trace: Trace, to: VertexIndex, width: f64) -> Result<Trace, ()> {
let to_dot = self.mesh.dot(to); let to_dot = self.mesh.dot(to);
let width = self.width;
trace.head = self trace.head = self
.draw() .draw()
.segbend_around_dot(trace.head, to_dot, true, width)?; .segbend_around_dot(trace.head, to_dot, true, width)?;

View File

@ -54,12 +54,12 @@ impl Router {
.map(|vertex| self.mesh.dot(*vertex)) .map(|vertex| self.mesh.dot(*vertex))
.collect();*/ .collect();*/
let mut trace = self.route(&mesh, 5.0).start(path[0]); let mut trace = self.route(&mesh).start(path[0]);
trace = self trace = self
.route(&mesh, 5.0) .route(&mesh)
.path(trace, &path[1..(path.len() - 1)]) .path(trace, &path[1..(path.len() - 1)], 5.0)
.unwrap(); // TODO. .unwrap(); // TODO.
let _ = self.route(&mesh, 5.0).finish(trace, path[path.len() - 1]); let _ = self.route(&mesh).finish(trace, path[path.len() - 1], 5.0);
Ok(()) Ok(())
} }
@ -176,8 +176,8 @@ impl Router {
Ok(()) Ok(())
}*/ }*/
pub fn route<'a>(&'a mut self, mesh: &'a Mesh, width: f64) -> Route { pub fn route<'a>(&'a mut self, mesh: &'a Mesh) -> Route {
Route::new(&mut self.layout, &self.rules, mesh, width) Route::new(&mut self.layout, &self.rules, mesh)
} }
/*pub fn routeedges(&self) -> impl Iterator<Item = (Point, Point)> + '_ { /*pub fn routeedges(&self) -> impl Iterator<Item = (Point, Point)> + '_ {