autorouter: simplify MeasureLength(ExecutionStepper) (Stepper is a misnormer, fixed here)

This commit is contained in:
Alain Emilia Anna Zscheile 2024-10-05 00:24:28 +02:00
parent 2fdb8aee6c
commit efca1178f8
5 changed files with 48 additions and 75 deletions

View File

@ -14,7 +14,7 @@ use crate::{
use super::{ use super::{
autoroute::AutorouteExecutionStepper, autoroute::AutorouteExecutionStepper,
compare_detours::CompareDetoursExecutionStepper, compare_detours::CompareDetoursExecutionStepper,
measure_length::MeasureLengthExecutionStepper, measure_length::MeasureLength,
place_via::PlaceViaExecutionStepper, place_via::PlaceViaExecutionStepper,
ratsnest::{Ratsnest, RatvertexIndex}, ratsnest::{Ratsnest, RatvertexIndex},
remove_bands::RemoveBandsExecutionStepper, remove_bands::RemoveBandsExecutionStepper,
@ -138,11 +138,8 @@ impl<M: AccessMesadata> Autorouter<M> {
CompareDetoursExecutionStepper::new(self, ratline1, ratline2, options) CompareDetoursExecutionStepper::new(self, ratline1, ratline2, options)
} }
pub fn measure_length( pub fn measure_length(&mut self, selection: &BandSelection) -> MeasureLength {
&mut self, MeasureLength(selection.clone())
selection: &BandSelection,
) -> Result<MeasureLengthExecutionStepper, AutorouterError> {
MeasureLengthExecutionStepper::new(selection)
} }
pub fn ratline_endpoints( pub fn ratline_endpoints(

View File

@ -5,14 +5,14 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
board::mesadata::AccessMesadata, board::mesadata::AccessMesadata,
layout::via::ViaWeight, layout::via::ViaWeight,
stepper::{PollStep, Step, StepError}, stepper::{EvalImmut, PollStep, Step, StepError},
}; };
use super::{ use super::{
autoroute::{AutorouteExecutionStepper, AutorouteStatus}, autoroute::{AutorouteExecutionStepper, AutorouteStatus},
compare_detours::CompareDetoursExecutionStepper, compare_detours::CompareDetoursExecutionStepper,
invoker::{Invoker, InvokerError}, invoker::{Invoker, InvokerError},
measure_length::MeasureLengthExecutionStepper, measure_length::MeasureLength,
place_via::PlaceViaExecutionStepper, place_via::PlaceViaExecutionStepper,
remove_bands::RemoveBandsExecutionStepper, remove_bands::RemoveBandsExecutionStepper,
selection::{BandSelection, PinSelection}, selection::{BandSelection, PinSelection},
@ -36,7 +36,7 @@ pub enum ExecutionStepper {
PlaceVia(PlaceViaExecutionStepper), PlaceVia(PlaceViaExecutionStepper),
RemoveBands(RemoveBandsExecutionStepper), RemoveBands(RemoveBandsExecutionStepper),
CompareDetours(CompareDetoursExecutionStepper), CompareDetours(CompareDetoursExecutionStepper),
MeasureLength(MeasureLengthExecutionStepper), MeasureLength(MeasureLength),
} }
impl ExecutionStepper { impl ExecutionStepper {
@ -70,7 +70,7 @@ impl ExecutionStepper {
}); });
} }
ExecutionStepper::MeasureLength(measure_length) => { ExecutionStepper::MeasureLength(measure_length) => {
let length = measure_length.doit(autorouter)?; let length = measure_length.eval(autorouter)?;
Poll::Ready(format!("Total length of selected bands: {}", length)) Poll::Ready(format!("Total length of selected bands: {}", length))
} }
} }

View File

@ -18,7 +18,7 @@ use super::{
compare_detours::CompareDetoursExecutionStepper, compare_detours::CompareDetoursExecutionStepper,
execution::{Command, ExecutionStepper}, execution::{Command, ExecutionStepper},
history::{History, HistoryError}, history::{History, HistoryError},
measure_length::MeasureLengthExecutionStepper, measure_length::MeasureLength,
place_via::PlaceViaExecutionStepper, place_via::PlaceViaExecutionStepper,
remove_bands::RemoveBandsExecutionStepper, remove_bands::RemoveBandsExecutionStepper,
Autorouter, AutorouterError, Autorouter, AutorouterError,
@ -26,22 +26,30 @@ use super::{
#[enum_dispatch] #[enum_dispatch]
pub trait GetMaybeNavmesh { pub trait GetMaybeNavmesh {
fn maybe_navmesh(&self) -> Option<&Navmesh>; fn maybe_navmesh(&self) -> Option<&Navmesh> {
None
}
} }
#[enum_dispatch] #[enum_dispatch]
pub trait GetMaybeTrace { pub trait GetMaybeTrace {
fn maybe_trace(&self) -> Option<&TraceStepper>; fn maybe_trace(&self) -> Option<&TraceStepper> {
None
}
} }
#[enum_dispatch] #[enum_dispatch]
pub trait GetGhosts { pub trait GetGhosts {
fn ghosts(&self) -> &[PrimitiveShape]; fn ghosts(&self) -> &[PrimitiveShape] {
&[]
}
} }
#[enum_dispatch] #[enum_dispatch]
pub trait GetObstacles { pub trait GetObstacles {
fn obstacles(&self) -> &[PrimitiveIndex]; fn obstacles(&self) -> &[PrimitiveIndex] {
&[]
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -128,7 +136,7 @@ impl<M: AccessMesadata> Invoker<M> {
self.autorouter.compare_detours(selection, *options)?, self.autorouter.compare_detours(selection, *options)?,
), ),
Command::MeasureLength(selection) => { Command::MeasureLength(selection) => {
ExecutionStepper::MeasureLength(self.autorouter.measure_length(selection)?) ExecutionStepper::MeasureLength(self.autorouter.measure_length(selection))
} }
}) })
} }

View File

@ -3,11 +3,8 @@
//! the length of specified band selections. //! the length of specified band selections.
use crate::{ use crate::{
board::mesadata::AccessMesadata, board::mesadata::AccessMesadata, geometry::shape::MeasureLength as MeasureLengthTrait,
drawing::graph::PrimitiveIndex, graph::MakeRef, stepper::EvalImmut,
geometry::{primitive::PrimitiveShape, shape::MeasureLength as MeasureLengthTrait},
graph::MakeRef,
router::{navmesh::Navmesh, trace::TraceStepper},
}; };
use super::{ use super::{
@ -16,65 +13,28 @@ use super::{
Autorouter, AutorouterError, Autorouter, AutorouterError,
}; };
pub struct MeasureLengthExecutionStepper { pub struct MeasureLength(pub BandSelection);
selection: BandSelection,
maybe_length: Option<f64>,
}
impl MeasureLengthExecutionStepper { impl<M: AccessMesadata> EvalImmut<Autorouter<M>, f64> for MeasureLength {
pub fn new(selection: &BandSelection) -> Result<Self, AutorouterError> { type Error = AutorouterError;
Ok(Self {
selection: selection.clone(),
maybe_length: None,
})
}
pub fn doit( fn eval(&self, autorouter: &Autorouter<M>) -> Result<f64, AutorouterError> {
&mut self, let mut length = 0.0;
autorouter: &mut Autorouter<impl AccessMesadata>,
) -> Result<f64, AutorouterError> {
let length = if let Some(length) = self.maybe_length {
length
} else {
let mut length = 0.0;
for selector in self.selection.selectors() { for selector in self.0.selectors() {
let band = autorouter let band = autorouter
.board .board
.bandname_band(selector.band.clone()) .bandname_band(selector.band.clone())
.unwrap() .unwrap()
.0; .0;
length += band.ref_(autorouter.board.layout().drawing()).length(); length += band.ref_(autorouter.board.layout().drawing()).length();
} }
self.maybe_length = Some(length);
length
};
Ok(length) Ok(length)
} }
} }
impl GetMaybeNavmesh for MeasureLengthExecutionStepper { impl GetMaybeNavmesh for MeasureLength {}
fn maybe_navmesh(&self) -> Option<&Navmesh> { impl GetMaybeTrace for MeasureLength {}
None impl GetGhosts for MeasureLength {}
} impl GetObstacles for MeasureLength {}
}
impl GetMaybeTrace for MeasureLengthExecutionStepper {
fn maybe_trace(&self) -> Option<&TraceStepper> {
None
}
}
impl GetGhosts for MeasureLengthExecutionStepper {
fn ghosts(&self) -> &[PrimitiveShape] {
&[]
}
}
impl GetObstacles for MeasureLengthExecutionStepper {
fn obstacles(&self) -> &[PrimitiveIndex] {
&[]
}
}

View File

@ -47,3 +47,11 @@ pub trait StepBack<C, S>: StepError {
pub trait Abort<C> { pub trait Abort<C> {
fn abort(&mut self, context: &mut C); fn abort(&mut self, context: &mut C);
} }
/// Evaluations which don't modify the context
/// (basically calculating derived values)
pub trait EvalImmut<C, S> {
type Error;
fn eval(&self, context: &C) -> Result<S, Self::Error>;
}