refactor(stepper): Join progress estimation into one method

This commit is contained in:
Mikolaj Wielgus 2025-10-31 02:08:42 +01:00
parent 7b66db1672
commit c0f4319a13
14 changed files with 85 additions and 116 deletions

View File

@ -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!(

View File

@ -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<M: AccessMesadata + Clone> Abort<Invoker<M>> for ExecutionStepper<M> {
// Since enum_dispatch does not really support generics, we implement this the
// long way.
impl<M> EstimateProgress for ExecutionStepper<M> {
impl<M> EstimateLinearProgress for ExecutionStepper<M> {
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<f64> {
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()
}
}
}

View File

@ -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 {}

View File

@ -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<M: AccessMesadata> Reconfigure<Autorouter<M>> 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<f64> {
self.planar.estimate_linear_progress()
}
}

View File

@ -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<M: AccessMesadata> Abort<Autorouter<M>> 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<f64> {
self.stepper.estimate_linear_progress()
}
}

View File

@ -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 {}

View File

@ -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<M: AccessMesadata> Reconfigure<Autorouter<M>> 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<f64> {
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,
)
}
}

View File

@ -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<M: AccessMesadata> Abort<Autorouter<M>> 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<f64> {
self.stepper.estimate_linear_progress()
}
}

View File

@ -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 {}

View File

@ -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<M: AccessMesadata + Clone> Abort<Invoker<M>> for ActivityStepper<M> {
// Since enum_dispatch does not really support generics, we implement this the
// long way.
impl<M> EstimateProgress for ActivityStepper<M> {
impl<M> EstimateLinearProgress for ActivityStepper<M> {
type Value = f64;
fn estimate_progress_value(&self) -> f64 {
fn estimate_linear_progress(&self) -> LinearProgress<f64> {
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<M: AccessMesadata + Clone> OnEvent<ActivityContext<'_, M>, InteractiveEvent
}
}
impl<M> EstimateProgress for ActivityStepperWithStatus<M> {
impl<M> EstimateLinearProgress for ActivityStepperWithStatus<M> {
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<f64> {
self.activity.estimate_linear_progress()
}
}

View File

@ -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<R: AccessRules + Clone + std::panic::RefUnwindSafe> AutorouteExecutionStepp
}
}
impl<M> EstimateProgress for AutorouteExecutionStepper<M> {
impl<M> EstimateLinearProgress for AutorouteExecutionStepper<M> {
type Value = f64;
}

View File

@ -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<R: AccessRules> Step<Router<'_, R>, 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<f64> {
self.thetastar.estimate_linear_progress()
}
}

View File

@ -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<K, T>(pub K, pub T);
@ -443,7 +443,7 @@ where
}
}
impl<G, K> EstimateProgress for ThetastarStepper<G, K>
impl<G, K> EstimateLinearProgress for ThetastarStepper<G, K>
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<Self::Value> {
LinearProgress::new(self.progress_estimate_value, self.progress_estimate_maximum)
}
}

View File

@ -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<Ctx, Event> {
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<V> {
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<V> LinearProgress<V> {
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<Self::Value> {
LinearProgress::new(Self::Value::default(), Self::Value::default())
}
}