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

View File

@ -5,14 +5,14 @@ use serde::{Deserialize, Serialize};
use crate::{
board::mesadata::AccessMesadata,
layout::via::ViaWeight,
stepper::{PollStep, Step, StepError},
stepper::{EvalImmut, PollStep, Step, StepError},
};
use super::{
autoroute::{AutorouteExecutionStepper, AutorouteStatus},
compare_detours::CompareDetoursExecutionStepper,
invoker::{Invoker, InvokerError},
measure_length::MeasureLengthExecutionStepper,
measure_length::MeasureLength,
place_via::PlaceViaExecutionStepper,
remove_bands::RemoveBandsExecutionStepper,
selection::{BandSelection, PinSelection},
@ -36,7 +36,7 @@ pub enum ExecutionStepper {
PlaceVia(PlaceViaExecutionStepper),
RemoveBands(RemoveBandsExecutionStepper),
CompareDetours(CompareDetoursExecutionStepper),
MeasureLength(MeasureLengthExecutionStepper),
MeasureLength(MeasureLength),
}
impl ExecutionStepper {
@ -70,7 +70,7 @@ impl ExecutionStepper {
});
}
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))
}
}

View File

@ -18,7 +18,7 @@ use super::{
compare_detours::CompareDetoursExecutionStepper,
execution::{Command, ExecutionStepper},
history::{History, HistoryError},
measure_length::MeasureLengthExecutionStepper,
measure_length::MeasureLength,
place_via::PlaceViaExecutionStepper,
remove_bands::RemoveBandsExecutionStepper,
Autorouter, AutorouterError,
@ -26,22 +26,30 @@ use super::{
#[enum_dispatch]
pub trait GetMaybeNavmesh {
fn maybe_navmesh(&self) -> Option<&Navmesh>;
fn maybe_navmesh(&self) -> Option<&Navmesh> {
None
}
}
#[enum_dispatch]
pub trait GetMaybeTrace {
fn maybe_trace(&self) -> Option<&TraceStepper>;
fn maybe_trace(&self) -> Option<&TraceStepper> {
None
}
}
#[enum_dispatch]
pub trait GetGhosts {
fn ghosts(&self) -> &[PrimitiveShape];
fn ghosts(&self) -> &[PrimitiveShape] {
&[]
}
}
#[enum_dispatch]
pub trait GetObstacles {
fn obstacles(&self) -> &[PrimitiveIndex];
fn obstacles(&self) -> &[PrimitiveIndex] {
&[]
}
}
#[derive(Debug, Clone)]
@ -128,7 +136,7 @@ impl<M: AccessMesadata> Invoker<M> {
self.autorouter.compare_detours(selection, *options)?,
),
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.
use crate::{
board::mesadata::AccessMesadata,
drawing::graph::PrimitiveIndex,
geometry::{primitive::PrimitiveShape, shape::MeasureLength as MeasureLengthTrait},
graph::MakeRef,
router::{navmesh::Navmesh, trace::TraceStepper},
board::mesadata::AccessMesadata, geometry::shape::MeasureLength as MeasureLengthTrait,
graph::MakeRef, stepper::EvalImmut,
};
use super::{
@ -16,65 +13,28 @@ use super::{
Autorouter, AutorouterError,
};
pub struct MeasureLengthExecutionStepper {
selection: BandSelection,
maybe_length: Option<f64>,
}
pub struct MeasureLength(pub BandSelection);
impl MeasureLengthExecutionStepper {
pub fn new(selection: &BandSelection) -> Result<Self, AutorouterError> {
Ok(Self {
selection: selection.clone(),
maybe_length: None,
})
}
impl<M: AccessMesadata> EvalImmut<Autorouter<M>, f64> for MeasureLength {
type Error = AutorouterError;
pub fn doit(
&mut self,
autorouter: &mut Autorouter<impl AccessMesadata>,
) -> Result<f64, AutorouterError> {
let length = if let Some(length) = self.maybe_length {
length
} else {
let mut length = 0.0;
fn eval(&self, autorouter: &Autorouter<M>) -> Result<f64, AutorouterError> {
let mut length = 0.0;
for selector in self.selection.selectors() {
let band = autorouter
.board
.bandname_band(selector.band.clone())
.unwrap()
.0;
length += band.ref_(autorouter.board.layout().drawing()).length();
}
self.maybe_length = Some(length);
length
};
for selector in self.0.selectors() {
let band = autorouter
.board
.bandname_band(selector.band.clone())
.unwrap()
.0;
length += band.ref_(autorouter.board.layout().drawing()).length();
}
Ok(length)
}
}
impl GetMaybeNavmesh for MeasureLengthExecutionStepper {
fn maybe_navmesh(&self) -> Option<&Navmesh> {
None
}
}
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] {
&[]
}
}
impl GetMaybeNavmesh for MeasureLength {}
impl GetMaybeTrace for MeasureLength {}
impl GetGhosts for MeasureLength {}
impl GetObstacles for MeasureLength {}

View File

@ -47,3 +47,11 @@ pub trait StepBack<C, S>: StepError {
pub trait Abort<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>;
}