From 97b1315eee509adb58d111f7760ebab6d281e821 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 13 May 2025 12:34:28 +0200 Subject: [PATCH] refactor(autorouter/invoker): Expose getter for whole A* stepper, not just navmesh Besides owning the navmesh, the A* stepper stores g-scores and f-scores, displaying which will be helpful for debugging, so we want to be able to access the whole A* stepper instead of only the navmesh. --- crates/topola-egui/src/viewport.rs | 10 +++++++--- src/autorouter/autoroute.rs | 11 +++++++---- src/autorouter/compare_detours.rs | 11 +++++++---- src/autorouter/execution.rs | 2 +- src/autorouter/invoker.rs | 14 ++++++++------ src/autorouter/measure_length.rs | 9 ++++++--- src/autorouter/place_via.rs | 9 ++++++--- src/autorouter/remove_bands.rs | 9 ++++++--- src/interactor/activity.rs | 11 ++++++----- src/interactor/interaction.rs | 7 ++++--- src/router/route.rs | 5 ----- 11 files changed, 58 insertions(+), 40 deletions(-) diff --git a/crates/topola-egui/src/viewport.rs b/crates/topola-egui/src/viewport.rs index 763232d..04582fa 100644 --- a/crates/topola-egui/src/viewport.rs +++ b/crates/topola-egui/src/viewport.rs @@ -12,7 +12,7 @@ use topola::{ autorouter::{ execution::Command, invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles, + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, }, }, board::AccessMesadata, @@ -252,7 +252,9 @@ impl Viewport { if menu_bar.show_navmesh { if let Some(activity) = workspace.interactor.maybe_activity() { - if let Some(navmesh) = activity.maybe_navmesh() { + if let Some(ref navmesh) = + activity.maybe_astar().map(|astar| &astar.graph) + { for edge in navmesh.edge_references() { let mut from = PrimitiveIndex::from( navmesh.node_weight(edge.source()).unwrap().node, @@ -448,7 +450,9 @@ impl Viewport { .paint_primitive(ghost, egui::Color32::from_rgb(75, 75, 150)); } - if let Some(navmesh) = activity.maybe_navmesh() { + if let Some(ref navmesh) = + activity.maybe_astar().map(|astar| &astar.graph) + { if menu_bar.show_origin_destination { let (origin, destination) = (navmesh.origin(), navmesh.destination()); diff --git a/src/autorouter/autoroute.rs b/src/autorouter/autoroute.rs index 8ddcddc..bf7d91e 100644 --- a/src/autorouter/autoroute.rs +++ b/src/autorouter/autoroute.rs @@ -15,6 +15,7 @@ use crate::{ geometry::primitive::PrimitiveShape, layout::LayoutEdit, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, RouteStepper, Router, @@ -23,7 +24,9 @@ use crate::{ }; use super::{ - invoker::{GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles}, + invoker::{ + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, + }, Autorouter, AutorouterError, AutorouterOptions, }; @@ -162,9 +165,9 @@ impl Step, Option, AutorouteContinu } } -impl GetMaybeNavmesh for AutorouteExecutionStepper { - fn maybe_navmesh(&self) -> Option<&Navmesh> { - self.route.as_ref().map(|route| route.navmesh()) +impl GetMaybeAstarStepper for AutorouteExecutionStepper { + fn maybe_astar(&self) -> Option<&AstarStepper> { + self.route.as_ref().map(|route| route.astar()) } } diff --git a/src/autorouter/compare_detours.rs b/src/autorouter/compare_detours.rs index c451c02..11e118a 100644 --- a/src/autorouter/compare_detours.rs +++ b/src/autorouter/compare_detours.rs @@ -15,6 +15,7 @@ use crate::{ geometry::{primitive::PrimitiveShape, shape::MeasureLength}, graph::MakeRef, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, @@ -23,7 +24,9 @@ use crate::{ use super::{ autoroute::{AutorouteContinueStatus, AutorouteExecutionStepper}, - invoker::{GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles}, + invoker::{ + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, + }, Autorouter, AutorouterError, AutorouterOptions, }; @@ -103,9 +106,9 @@ impl Step, (f64, f64)> for CompareDetoursExecut } } -impl GetMaybeNavmesh for CompareDetoursExecutionStepper { - fn maybe_navmesh(&self) -> Option<&Navmesh> { - self.autoroute.maybe_navmesh() +impl GetMaybeAstarStepper for CompareDetoursExecutionStepper { + fn maybe_astar(&self) -> Option<&AstarStepper> { + self.autoroute.maybe_astar() } } diff --git a/src/autorouter/execution.rs b/src/autorouter/execution.rs index e59297b..b0763d7 100644 --- a/src/autorouter/execution.rs +++ b/src/autorouter/execution.rs @@ -36,7 +36,7 @@ pub enum Command { } #[enum_dispatch( - GetMaybeNavmesh, + GetMaybeAstarStepper, GetMaybeNavcord, GetGhosts, GetObstacles, diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index ba6397a..f3c5f69 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -16,6 +16,7 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::{edit::ApplyGeometryEdit, primitive::PrimitiveShape}, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, @@ -33,31 +34,32 @@ use super::{ Autorouter, AutorouterError, }; +/// Trait for getting the A* stepper to display its data on the debug overlay, +/// most importantly the navmesh which is owned by the A* stepper. #[enum_dispatch] -/// Trait for getting the navmesh to display it on the debug overlay. -pub trait GetMaybeNavmesh { - fn maybe_navmesh(&self) -> Option<&Navmesh>; +pub trait GetMaybeAstarStepper { + fn maybe_astar(&self) -> Option<&AstarStepper>; } -#[enum_dispatch] /// Trait for getting the navcord to display it on the debug overlay. +#[enum_dispatch] pub trait GetMaybeNavcord { fn maybe_navcord(&self) -> Option<&Navcord>; } -#[enum_dispatch] /// Trait for getting ghosts to display on the debug overlay. Ghosts are the /// shapes that Topola attempted to create but failed due to them infringing on /// other shapes. +#[enum_dispatch] pub trait GetGhosts { fn ghosts(&self) -> &[PrimitiveShape]; } -#[enum_dispatch] /// Trait for getting the obstacles that prevented Topola from creating /// new objects (the shapes of these objects can be obtained with the above /// `GetGhosts` trait), for the purpose of displaying these obstacles on the /// debug overlay. +#[enum_dispatch] pub trait GetObstacles { fn obstacles(&self) -> &[PrimitiveIndex]; } diff --git a/src/autorouter/measure_length.rs b/src/autorouter/measure_length.rs index 8e9844b..fb0a433 100644 --- a/src/autorouter/measure_length.rs +++ b/src/autorouter/measure_length.rs @@ -12,13 +12,16 @@ use crate::{ geometry::{primitive::PrimitiveShape, shape::MeasureLength as MeasureLengthTrait}, graph::MakeRef, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, }; use super::{ - invoker::{GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles}, + invoker::{ + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, + }, selection::BandSelection, Autorouter, AutorouterError, }; @@ -58,8 +61,8 @@ impl MeasureLengthExecutionStepper { } } -impl GetMaybeNavmesh for MeasureLengthExecutionStepper { - fn maybe_navmesh(&self) -> Option<&Navmesh> { +impl GetMaybeAstarStepper for MeasureLengthExecutionStepper { + fn maybe_astar(&self) -> Option<&AstarStepper> { None } } diff --git a/src/autorouter/place_via.rs b/src/autorouter/place_via.rs index f57aadc..c828fb2 100644 --- a/src/autorouter/place_via.rs +++ b/src/autorouter/place_via.rs @@ -12,13 +12,16 @@ use crate::{ geometry::primitive::PrimitiveShape, layout::{via::ViaWeight, LayoutEdit}, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, }; use super::{ - invoker::{GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles}, + invoker::{ + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, + }, Autorouter, AutorouterError, }; @@ -55,8 +58,8 @@ impl PlaceViaExecutionStepper { } } -impl GetMaybeNavmesh for PlaceViaExecutionStepper { - fn maybe_navmesh(&self) -> Option<&Navmesh> { +impl GetMaybeAstarStepper for PlaceViaExecutionStepper { + fn maybe_astar(&self) -> Option<&AstarStepper> { None } } diff --git a/src/autorouter/remove_bands.rs b/src/autorouter/remove_bands.rs index 71326f8..47dce6a 100644 --- a/src/autorouter/remove_bands.rs +++ b/src/autorouter/remove_bands.rs @@ -10,13 +10,16 @@ use crate::{ geometry::primitive::PrimitiveShape, layout::LayoutEdit, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, }; use super::{ - invoker::{GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles}, + invoker::{ + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, + }, selection::BandSelection, Autorouter, AutorouterError, }; @@ -54,8 +57,8 @@ impl RemoveBandsExecutionStepper { } } -impl GetMaybeNavmesh for RemoveBandsExecutionStepper { - fn maybe_navmesh(&self) -> Option<&Navmesh> { +impl GetMaybeAstarStepper for RemoveBandsExecutionStepper { + fn maybe_astar(&self) -> Option<&AstarStepper> { None } } diff --git a/src/interactor/activity.rs b/src/interactor/activity.rs index 1134dbb..af9254a 100644 --- a/src/interactor/activity.rs +++ b/src/interactor/activity.rs @@ -12,7 +12,7 @@ use crate::{ autorouter::{ execution::ExecutionStepper, invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles, + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, Invoker, InvokerError, }, }, @@ -21,6 +21,7 @@ use crate::{ geometry::primitive::PrimitiveShape, interactor::interaction::{InteractionError, InteractionStepper}, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, @@ -49,7 +50,7 @@ pub enum ActivityError { /// An activity is either an interaction or an execution #[enum_dispatch( - GetMaybeNavmesh, + GetMaybeAstarStepper, GetMaybeNavcord, GetGhosts, GetObstacles, @@ -124,9 +125,9 @@ impl Abort> for ActivityStepperWithSta } } -impl GetMaybeNavmesh for ActivityStepperWithStatus { - fn maybe_navmesh(&self) -> Option<&Navmesh> { - self.activity.maybe_navmesh() +impl GetMaybeAstarStepper for ActivityStepperWithStatus { + fn maybe_astar(&self) -> Option<&AstarStepper> { + self.activity.maybe_astar() } } diff --git a/src/interactor/interaction.rs b/src/interactor/interaction.rs index d6e80d7..e938e18 100644 --- a/src/interactor/interaction.rs +++ b/src/interactor/interaction.rs @@ -8,12 +8,13 @@ use thiserror::Error; use crate::{ autorouter::invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetNavmeshDebugTexts, GetObstacles, + GetGhosts, GetMaybeAstarStepper, GetMaybeNavcord, GetNavmeshDebugTexts, GetObstacles, }, board::AccessMesadata, drawing::graph::PrimitiveIndex, geometry::primitive::PrimitiveShape, router::{ + astar::AstarStepper, navcord::Navcord, navmesh::{Navmesh, NavvertexIndex}, }, @@ -52,8 +53,8 @@ impl Abort> for InteractionStepper { } } -impl GetMaybeNavmesh for InteractionStepper { - fn maybe_navmesh(&self) -> Option<&Navmesh> { +impl GetMaybeAstarStepper for InteractionStepper { + fn maybe_astar(&self) -> Option<&AstarStepper> { todo!() } } diff --git a/src/router/route.rs b/src/router/route.rs index d305888..1c4f38a 100644 --- a/src/router/route.rs +++ b/src/router/route.rs @@ -24,7 +24,6 @@ use crate::{ #[derive(Getters, Dissolve)] pub struct RouteStepper { - #[getter(skip)] astar: AstarStepper, navcord: Navcord, ghosts: Vec, @@ -68,10 +67,6 @@ impl RouteStepper { obstacles, } } - - pub fn navmesh(&self) -> &Navmesh { - &self.astar.graph - } } impl Step, BandTermsegIndex> for RouteStepper {