From c0f4319a13a6f674774901fd6bde98180e21e2d0 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 31 Oct 2025 02:08:42 +0100 Subject: [PATCH] refactor(stepper): Join progress estimation into one method --- crates/topola-egui/src/status_bar.rs | 7 +++-- src/autorouter/execution.rs | 31 ++++++--------------- src/autorouter/measure_length.rs | 4 +-- src/autorouter/multilayer_autoroute.rs | 14 ++++------ src/autorouter/multilayer_reconfigurator.rs | 16 ++++------- src/autorouter/place_via.rs | 4 +-- src/autorouter/planar_autoroute.rs | 22 +++++++-------- src/autorouter/planar_reconfigurator.rs | 16 ++++------- src/autorouter/remove_bands.rs | 4 +-- src/interactor/activity.rs | 27 ++++++------------ src/router/ng/router.rs | 4 +-- src/router/route.rs | 12 +++----- src/router/thetastar.rs | 12 +++----- src/stepper.rs | 28 +++++++++++++------ 14 files changed, 85 insertions(+), 116 deletions(-) diff --git a/crates/topola-egui/src/status_bar.rs b/crates/topola-egui/src/status_bar.rs index c70c8fa..402dc27 100644 --- a/crates/topola-egui/src/status_bar.rs +++ b/crates/topola-egui/src/status_bar.rs @@ -4,7 +4,7 @@ use std::ops::ControlFlow; -use topola::{interactor::activity::ActivityStepperWithStatus, stepper::EstimateProgress}; +use topola::{interactor::activity::ActivityStepperWithStatus, stepper::EstimateLinearProgress}; use crate::{translator::Translator, viewport::Viewport}; @@ -40,8 +40,9 @@ impl StatusBar { )); if let Some(activity) = maybe_activity { - let value = activity.estimate_progress_value(); - let maximum = activity.estimate_progress_maximum(); + let linear_progress = activity.estimate_linear_progress(); + let value = linear_progress.value(); + let maximum = linear_progress.maximum(); ui.add( egui::ProgressBar::new((value / maximum) as f32).text(format!( diff --git a/src/autorouter/execution.rs b/src/autorouter/execution.rs index 2e1234c..c3cc522 100644 --- a/src/autorouter/execution.rs +++ b/src/autorouter/execution.rs @@ -16,7 +16,7 @@ use crate::{ board::{edit::BoardEdit, AccessMesadata}, layout::via::ViaWeight, router::ng, - stepper::{Abort, EstimateProgress, Step}, + stepper::{Abort, EstimateLinearProgress, LinearProgress, Step}, }; use super::{ @@ -173,33 +173,20 @@ impl Abort> for ExecutionStepper { // Since enum_dispatch does not really support generics, we implement this the // long way. -impl EstimateProgress for ExecutionStepper { +impl EstimateLinearProgress for ExecutionStepper { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - match self { - ExecutionStepper::MultilayerAutoroute(autoroute) => autoroute.estimate_progress_value(), - ExecutionStepper::PlanarAutoroute(autoroute) => autoroute.estimate_progress_value(), - ExecutionStepper::TopoAutoroute(toporoute) => toporoute.estimate_progress_value(), - ExecutionStepper::PlaceVia(place_via) => place_via.estimate_progress_value(), - ExecutionStepper::RemoveBands(remove_bands) => remove_bands.estimate_progress_value(), - ExecutionStepper::MeasureLength(measure_length) => { - measure_length.estimate_progress_value() - } - } - } - - fn estimate_progress_maximum(&self) -> f64 { + fn estimate_linear_progress(&self) -> LinearProgress { match self { ExecutionStepper::MultilayerAutoroute(autoroute) => { - autoroute.estimate_progress_maximum() + autoroute.estimate_linear_progress() } - ExecutionStepper::PlanarAutoroute(autoroute) => autoroute.estimate_progress_maximum(), - ExecutionStepper::TopoAutoroute(toporoute) => toporoute.estimate_progress_maximum(), - ExecutionStepper::PlaceVia(place_via) => place_via.estimate_progress_maximum(), - ExecutionStepper::RemoveBands(remove_bands) => remove_bands.estimate_progress_maximum(), + ExecutionStepper::PlanarAutoroute(autoroute) => autoroute.estimate_linear_progress(), + ExecutionStepper::TopoAutoroute(toporoute) => toporoute.estimate_linear_progress(), + ExecutionStepper::PlaceVia(place_via) => place_via.estimate_linear_progress(), + ExecutionStepper::RemoveBands(remove_bands) => remove_bands.estimate_linear_progress(), ExecutionStepper::MeasureLength(measure_length) => { - measure_length.estimate_progress_maximum() + measure_length.estimate_linear_progress() } } } diff --git a/src/autorouter/measure_length.rs b/src/autorouter/measure_length.rs index 52b9fcc..9884e0e 100644 --- a/src/autorouter/measure_length.rs +++ b/src/autorouter/measure_length.rs @@ -8,7 +8,7 @@ use crate::{ board::AccessMesadata, geometry::shape::MeasureLength as MeasureLengthTrait, graph::MakeRef, - stepper::EstimateProgress, + stepper::EstimateLinearProgress, }; use super::{invoker::GetDebugOverlayData, selection::BandSelection, Autorouter, AutorouterError}; @@ -48,7 +48,7 @@ impl MeasureLengthExecutionStepper { } } -impl EstimateProgress for MeasureLengthExecutionStepper { +impl EstimateLinearProgress for MeasureLengthExecutionStepper { type Value = f64; } impl GetDebugOverlayData for MeasureLengthExecutionStepper {} diff --git a/src/autorouter/multilayer_autoroute.rs b/src/autorouter/multilayer_autoroute.rs index 7d57bde..320a6c5 100644 --- a/src/autorouter/multilayer_autoroute.rs +++ b/src/autorouter/multilayer_autoroute.rs @@ -21,7 +21,9 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::{edit::Edit, primitive::PrimitiveShape}, router::{navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper}, - stepper::{Abort, EstimateProgress, ReconfiguratorStatus, Reconfigure, Step}, + stepper::{ + Abort, EstimateLinearProgress, LinearProgress, ReconfiguratorStatus, Reconfigure, Step, + }, }; #[derive(Clone, Debug)] @@ -117,15 +119,11 @@ impl Reconfigure> for MultilayerAutorouteExecut } } -impl EstimateProgress for MultilayerAutorouteExecutionStepper { +impl EstimateLinearProgress for MultilayerAutorouteExecutionStepper { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - self.planar.estimate_progress_value() - } - - fn estimate_progress_maximum(&self) -> f64 { - self.planar.estimate_progress_maximum() + fn estimate_linear_progress(&self) -> LinearProgress { + self.planar.estimate_linear_progress() } } diff --git a/src/autorouter/multilayer_reconfigurator.rs b/src/autorouter/multilayer_reconfigurator.rs index f0070b2..bceac00 100644 --- a/src/autorouter/multilayer_reconfigurator.rs +++ b/src/autorouter/multilayer_reconfigurator.rs @@ -29,7 +29,9 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::primitive::PrimitiveShape, router::{navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper}, - stepper::{Abort, EstimateProgress, ReconfiguratorStatus, Reconfigure, Step}, + stepper::{ + Abort, EstimateLinearProgress, LinearProgress, ReconfiguratorStatus, Reconfigure, Step, + }, }; pub type MultilayerReconfiguratorStatus = @@ -139,17 +141,11 @@ impl Abort> for MultilayerAutorouteReconfigurat } } -impl EstimateProgress for MultilayerAutorouteReconfigurator { +impl EstimateLinearProgress for MultilayerAutorouteReconfigurator { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - // TODO. - self.stepper.estimate_progress_value() - } - - fn estimate_progress_maximum(&self) -> f64 { - // TODO. - self.stepper.estimate_progress_maximum() + fn estimate_linear_progress(&self) -> LinearProgress { + self.stepper.estimate_linear_progress() } } diff --git a/src/autorouter/place_via.rs b/src/autorouter/place_via.rs index cecb120..2092e19 100644 --- a/src/autorouter/place_via.rs +++ b/src/autorouter/place_via.rs @@ -12,7 +12,7 @@ use crate::{ AccessMesadata, }, layout::{via::ViaWeight, LayoutEdit}, - stepper::EstimateProgress, + stepper::EstimateLinearProgress, }; use super::{invoker::GetDebugOverlayData, Autorouter, AutorouterError}; @@ -53,7 +53,7 @@ impl PlaceViaExecutionStepper { } } -impl EstimateProgress for PlaceViaExecutionStepper { +impl EstimateLinearProgress for PlaceViaExecutionStepper { type Value = f64; } impl GetDebugOverlayData for PlaceViaExecutionStepper {} diff --git a/src/autorouter/planar_autoroute.rs b/src/autorouter/planar_autoroute.rs index e56bc4f..27e6d5e 100644 --- a/src/autorouter/planar_autoroute.rs +++ b/src/autorouter/planar_autoroute.rs @@ -21,7 +21,7 @@ use crate::{ router::{ navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper, RouteStepper, Router, }, - stepper::{Abort, EstimateProgress, Reconfigure, Step}, + stepper::{Abort, EstimateLinearProgress, LinearProgress, Reconfigure, Step}, }; use super::{ @@ -303,18 +303,18 @@ impl Reconfigure> for PlanarAutorouteExecutionS } } -impl EstimateProgress for PlanarAutorouteExecutionStepper { +impl EstimateLinearProgress for PlanarAutorouteExecutionStepper { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - self.curr_ratline_index as f64 - + self.route.as_ref().map_or(0.0, |route| { - route.estimate_progress_value() / route.estimate_progress_maximum() - }) - } - - fn estimate_progress_maximum(&self) -> f64 { - self.configuration().ratlines.len() as f64 + fn estimate_linear_progress(&self) -> LinearProgress { + LinearProgress::new( + self.curr_ratline_index as f64 + + self.route.as_ref().map_or(0.0, |route| { + route.estimate_linear_progress().value() + / route.estimate_linear_progress().maximum() + }), + self.configuration().ratlines.len() as f64, + ) } } diff --git a/src/autorouter/planar_reconfigurator.rs b/src/autorouter/planar_reconfigurator.rs index 8423430..856c853 100644 --- a/src/autorouter/planar_reconfigurator.rs +++ b/src/autorouter/planar_reconfigurator.rs @@ -24,7 +24,9 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::primitive::PrimitiveShape, router::{navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper}, - stepper::{Abort, EstimateProgress, ReconfiguratorStatus, Reconfigure, Step}, + stepper::{ + Abort, EstimateLinearProgress, LinearProgress, ReconfiguratorStatus, Reconfigure, Step, + }, }; pub type PlanarAutorouteReconfiguratorStatus = @@ -118,17 +120,11 @@ impl Abort> for PlanarAutorouteReconfigurator { } } -impl EstimateProgress for PlanarAutorouteReconfigurator { +impl EstimateLinearProgress for PlanarAutorouteReconfigurator { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - // TODO. - self.stepper.estimate_progress_value() - } - - fn estimate_progress_maximum(&self) -> f64 { - // TODO. - self.stepper.estimate_progress_maximum() + fn estimate_linear_progress(&self) -> LinearProgress { + self.stepper.estimate_linear_progress() } } diff --git a/src/autorouter/remove_bands.rs b/src/autorouter/remove_bands.rs index d9ecfd9..8ca81ae 100644 --- a/src/autorouter/remove_bands.rs +++ b/src/autorouter/remove_bands.rs @@ -6,7 +6,7 @@ use crate::{ board::{edit::BoardEdit, AccessMesadata}, - stepper::EstimateProgress, + stepper::EstimateLinearProgress, }; use super::{invoker::GetDebugOverlayData, selection::BandSelection, Autorouter, AutorouterError}; @@ -48,7 +48,7 @@ impl RemoveBandsExecutionStepper { } } -impl EstimateProgress for RemoveBandsExecutionStepper { +impl EstimateLinearProgress for RemoveBandsExecutionStepper { type Value = f64; } impl GetDebugOverlayData for RemoveBandsExecutionStepper {} diff --git a/src/interactor/activity.rs b/src/interactor/activity.rs index 89f53b2..787309c 100644 --- a/src/interactor/activity.rs +++ b/src/interactor/activity.rs @@ -25,7 +25,7 @@ use crate::{ ng, thetastar::ThetastarStepper, }, - stepper::{Abort, EstimateProgress, OnEvent, Step}, + stepper::{Abort, EstimateLinearProgress, LinearProgress, OnEvent, Step}, }; /// Stores the interactive input data from the user. @@ -100,20 +100,13 @@ impl Abort> for ActivityStepper { // Since enum_dispatch does not really support generics, we implement this the // long way. -impl EstimateProgress for ActivityStepper { +impl EstimateLinearProgress for ActivityStepper { type Value = f64; - fn estimate_progress_value(&self) -> f64 { + fn estimate_linear_progress(&self) -> LinearProgress { match self { - ActivityStepper::Interaction(..) => 0.0, - ActivityStepper::Execution(execution) => execution.estimate_progress_value(), - } - } - - fn estimate_progress_maximum(&self) -> f64 { - match self { - ActivityStepper::Interaction(..) => 0.0, - ActivityStepper::Execution(execution) => execution.estimate_progress_maximum(), + ActivityStepper::Interaction(..) => LinearProgress::new(0.0, 0.0), + ActivityStepper::Execution(execution) => execution.estimate_linear_progress(), } } } @@ -199,15 +192,11 @@ impl OnEvent, InteractiveEvent } } -impl EstimateProgress for ActivityStepperWithStatus { +impl EstimateLinearProgress for ActivityStepperWithStatus { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - self.activity.estimate_progress_value() - } - - fn estimate_progress_maximum(&self) -> f64 { - self.activity.estimate_progress_maximum() + fn estimate_linear_progress(&self) -> LinearProgress { + self.activity.estimate_linear_progress() } } diff --git a/src/router/ng/router.rs b/src/router/ng/router.rs index 5d8d192..5e328a7 100644 --- a/src/router/ng/router.rs +++ b/src/router/ng/router.rs @@ -16,7 +16,7 @@ use crate::{ geometry::primitive::PrimitiveShape, graph::GenericIndex, layout::{poly::PolyWeight, Layout}, - stepper::{Abort, EstimateProgress}, + stepper::{Abort, EstimateLinearProgress}, }; use super::{ @@ -233,7 +233,7 @@ impl AutorouteExecutionStepp } } -impl EstimateProgress for AutorouteExecutionStepper { +impl EstimateLinearProgress for AutorouteExecutionStepper { type Value = f64; } diff --git a/src/router/route.rs b/src/router/route.rs index 443a131..18d5d4c 100644 --- a/src/router/route.rs +++ b/src/router/route.rs @@ -19,7 +19,7 @@ use crate::{ thetastar::{ThetastarError, ThetastarStepper}, Router, RouterThetastarStrategy, }, - stepper::{EstimateProgress, Step}, + stepper::{EstimateLinearProgress, LinearProgress, Step}, }; #[derive(Getters, Dissolve)] @@ -99,14 +99,10 @@ impl Step, BandTermsegIndex> for RouteStepper { } } -impl EstimateProgress for RouteStepper { +impl EstimateLinearProgress for RouteStepper { type Value = f64; - fn estimate_progress_value(&self) -> f64 { - self.thetastar.estimate_progress_value() - } - - fn estimate_progress_maximum(&self) -> f64 { - self.thetastar.estimate_progress_maximum() + fn estimate_linear_progress(&self) -> LinearProgress { + self.thetastar.estimate_linear_progress() } } diff --git a/src/router/thetastar.rs b/src/router/thetastar.rs index 6d4e6cf..2cbe91e 100644 --- a/src/router/thetastar.rs +++ b/src/router/thetastar.rs @@ -20,7 +20,7 @@ use thiserror::Error; use std::cmp::Ordering; -use crate::stepper::{EstimateProgress, Step}; +use crate::stepper::{EstimateLinearProgress, LinearProgress, Step}; #[derive(Copy, Clone, Debug)] pub struct MinScored(pub K, pub T); @@ -443,7 +443,7 @@ where } } -impl EstimateProgress for ThetastarStepper +impl EstimateLinearProgress for ThetastarStepper where G: GraphBase, G::NodeId: Eq + Ord, @@ -452,11 +452,7 @@ where { type Value = K; - fn estimate_progress_value(&self) -> K { - self.progress_estimate_value - } - - fn estimate_progress_maximum(&self) -> K { - self.progress_estimate_maximum + fn estimate_linear_progress(&self) -> LinearProgress { + LinearProgress::new(self.progress_estimate_value, self.progress_estimate_maximum) } } diff --git a/src/stepper.rs b/src/stepper.rs index c2e80cd..24919dc 100644 --- a/src/stepper.rs +++ b/src/stepper.rs @@ -4,6 +4,8 @@ use core::ops::ControlFlow; +use derive_getters::Getters; + /// This trait represents a linearly advanceable state whose advancement may /// break or fail with many different return values, and to which part of /// the information, called the context, has to be supplied on each call as a @@ -81,15 +83,23 @@ pub trait OnEvent { fn on_event(&mut self, context: &mut Ctx, event: Event) -> Self::Output; } -/// Some steppers report estimates of how far they are from completion. -pub trait EstimateProgress { - type Value: Default; +#[derive(Clone, Copy, Debug, Getters)] +pub struct LinearProgress { + value: V, + maximum: V, +} - fn estimate_progress_value(&self) -> Self::Value { - Self::Value::default() - } - - fn estimate_progress_maximum(&self) -> Self::Value { - Self::Value::default() +impl LinearProgress { + pub fn new(value: V, maximum: V) -> Self { + Self { value, maximum } + } +} + +/// Some steppers report estimates of how far they are from completion. +pub trait EstimateLinearProgress { + type Value: Default; + + fn estimate_linear_progress(&self) -> LinearProgress { + LinearProgress::new(Self::Value::default(), Self::Value::default()) } }