From b25ad863babda06c1eeba873d3c5086370a9bf22 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 4 Jul 2025 14:57:47 +0200 Subject: [PATCH] refactor(autorouter/invoker): Merge debug overlay data traits into one trait The name of this new trait shall be `GetDebugOverlayData`. --- crates/topola-egui/src/viewport.rs | 8 +-- src/autorouter/autoroute.rs | 20 +------ src/autorouter/compare_detours.rs | 17 +----- src/autorouter/execution.rs | 32 +---------- src/autorouter/invoker.rs | 88 ++++++++++++------------------ src/autorouter/measure_length.rs | 16 +----- src/autorouter/place_via.rs | 15 +---- src/autorouter/remove_bands.rs | 16 +----- src/interactor/activity.rs | 61 ++++++--------------- src/interactor/interaction.rs | 14 +---- src/router/ng/router.rs | 65 +++++++--------------- 11 files changed, 88 insertions(+), 264 deletions(-) diff --git a/crates/topola-egui/src/viewport.rs b/crates/topola-egui/src/viewport.rs index da4ed75..d6cb8be 100644 --- a/crates/topola-egui/src/viewport.rs +++ b/crates/topola-egui/src/viewport.rs @@ -9,10 +9,7 @@ use petgraph::{ }; use rstar::{Envelope, AABB}; use topola::{ - autorouter::invoker::{ - GetActivePolygons, GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, - GetMaybeTopoNavmesh, GetNavmeshDebugTexts, GetObstacles, GetPolygonalBlockers, - }, + autorouter::invoker::GetDebugOverlayData, board::AccessMesadata, drawing::{ graph::{MakePrimitive, PrimitiveIndex}, @@ -26,8 +23,7 @@ use topola::{ }, layout::poly::MakePolygon, math::{Circle, RotationSense}, - router::navmesh::NavnodeIndex, - router::ng::pie, + router::{navmesh::NavnodeIndex, ng::pie}, }; use crate::{ diff --git a/src/autorouter/autoroute.rs b/src/autorouter/autoroute.rs index 96c40ad..d165117 100644 --- a/src/autorouter/autoroute.rs +++ b/src/autorouter/autoroute.rs @@ -20,13 +20,7 @@ use crate::{ stepper::Step, }; -use super::{ - invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts, GetObstacles, - GetPolygonalBlockers, - }, - Autorouter, AutorouterError, AutorouterOptions, -}; +use super::{invoker::GetDebugOverlayData, Autorouter, AutorouterError, AutorouterOptions}; /// Represents the current status of the autoroute operation. pub enum AutorouteContinueStatus { @@ -170,30 +164,20 @@ impl Step, Option, AutorouteContinu } } -impl GetMaybeThetastarStepper for AutorouteExecutionStepper { +impl GetDebugOverlayData for AutorouteExecutionStepper { fn maybe_thetastar(&self) -> Option<&ThetastarStepper> { self.route.as_ref().map(|route| route.thetastar()) } -} -impl GetMaybeNavcord for AutorouteExecutionStepper { fn maybe_navcord(&self) -> Option<&Navcord> { self.route.as_ref().map(|route| route.navcord()) } -} -impl GetGhosts for AutorouteExecutionStepper { fn ghosts(&self) -> &[PrimitiveShape] { self.route.as_ref().map_or(&[], |route| route.ghosts()) } -} -impl GetPolygonalBlockers for AutorouteExecutionStepper {} - -impl GetObstacles for AutorouteExecutionStepper { fn obstacles(&self) -> &[PrimitiveIndex] { self.route.as_ref().map_or(&[], |route| route.obstacles()) } } - -impl GetNavmeshDebugTexts for AutorouteExecutionStepper {} diff --git a/src/autorouter/compare_detours.rs b/src/autorouter/compare_detours.rs index bb1609c..cbb69d7 100644 --- a/src/autorouter/compare_detours.rs +++ b/src/autorouter/compare_detours.rs @@ -20,10 +20,7 @@ use crate::{ use super::{ autoroute::{AutorouteContinueStatus, AutorouteExecutionStepper}, - invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts, GetObstacles, - GetPolygonalBlockers, - }, + invoker::GetDebugOverlayData, Autorouter, AutorouterError, AutorouterOptions, }; @@ -103,30 +100,20 @@ impl Step, (f64, f64)> for CompareDetoursExecut } } -impl GetMaybeThetastarStepper for CompareDetoursExecutionStepper { +impl GetDebugOverlayData for CompareDetoursExecutionStepper { fn maybe_thetastar(&self) -> Option<&ThetastarStepper> { self.autoroute.maybe_thetastar() } -} -impl GetMaybeNavcord for CompareDetoursExecutionStepper { fn maybe_navcord(&self) -> Option<&Navcord> { self.autoroute.maybe_navcord() } -} -impl GetGhosts for CompareDetoursExecutionStepper { fn ghosts(&self) -> &[PrimitiveShape] { self.autoroute.ghosts() } -} -impl GetPolygonalBlockers for CompareDetoursExecutionStepper {} - -impl GetObstacles for CompareDetoursExecutionStepper { fn obstacles(&self) -> &[PrimitiveIndex] { self.autoroute.obstacles() } } - -impl GetNavmeshDebugTexts for CompareDetoursExecutionStepper {} diff --git a/src/autorouter/execution.rs b/src/autorouter/execution.rs index 22a601d..840a30d 100644 --- a/src/autorouter/execution.rs +++ b/src/autorouter/execution.rs @@ -9,8 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::{ board::AccessMesadata, - graph::GenericIndex, - layout::{poly::PolyWeight, via::ViaWeight, LayoutEdit}, + layout::{via::ViaWeight, LayoutEdit}, router::ng, stepper::{Abort, Step}, }; @@ -18,7 +17,7 @@ use crate::{ use super::{ autoroute::AutorouteExecutionStepper, compare_detours::CompareDetoursExecutionStepper, - invoker::{GetActivePolygons, GetMaybeTopoNavmesh, Invoker, InvokerError}, + invoker::{GetDebugOverlayData, Invoker, InvokerError}, measure_length::MeasureLengthExecutionStepper, place_via::PlaceViaExecutionStepper, remove_bands::RemoveBandsExecutionStepper, @@ -44,14 +43,7 @@ pub enum Command { MeasureLength(BandSelection), } -#[enum_dispatch( - GetMaybeThetastarStepper, - GetMaybeNavcord, - GetGhosts, - GetPolygonalBlockers, - GetObstacles, - GetNavmeshDebugTexts -)] +#[enum_dispatch(GetDebugOverlayData)] pub enum ExecutionStepper { Autoroute(AutorouteExecutionStepper), TopoAutoroute(ng::AutorouteExecutionStepper), @@ -173,21 +165,3 @@ impl Abort> for ExecutionStepper { } } } - -impl GetActivePolygons for ExecutionStepper { - fn active_polygons(&self) -> &[GenericIndex] { - match self { - ExecutionStepper::TopoAutoroute(autoroute) => autoroute.active_polygons(), - _ => &[], - } - } -} - -impl GetMaybeTopoNavmesh for ExecutionStepper { - fn maybe_topo_navmesh(&self) -> Option> { - match self { - ExecutionStepper::TopoAutoroute(autoroute) => autoroute.maybe_topo_navmesh(), - _ => None, - } - } -} diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index d8889d3..4112081 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -38,76 +38,58 @@ 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. +/// Trait for getting the information to display on the debug overlay, #[enum_dispatch] -pub trait GetMaybeThetastarStepper { +pub trait GetDebugOverlayData { + /// Get the Theta* stepper. Most importantly, this gives us the access to + /// the navmesh. fn maybe_thetastar(&self) -> Option<&ThetastarStepper> { None } -} -/// Trait for getting the navcord to display it on the debug overlay. -#[enum_dispatch] -pub trait GetMaybeNavcord { - fn maybe_navcord(&self) -> Option<&Navcord> { - None - } -} - -/// 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] { - &[] - } -} - -/// Getter for the polygonal blockers (polygonal regions which block routing) -#[enum_dispatch] -pub trait GetPolygonalBlockers { - fn polygonal_blockers(&self) -> &[LineString] { - &[] - } -} - -/// Getter for the polygons around which some routing happens -#[enum_dispatch] -pub trait GetActivePolygons { - fn active_polygons(&self) -> &[GenericIndex] { - &[] - } -} - -/// Getter trait to obtain Topological/Planar Navigation Mesh -#[enum_dispatch] -pub trait GetMaybeTopoNavmesh { + /// Obtain Topological/Planar Navigation Mesh, if present. fn maybe_topo_navmesh(&self) -> Option> { None } -} -/// 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 { + /// Get the navcord. This is useful for coloring the currently visited path + /// on the navmesh. + fn maybe_navcord(&self) -> Option<&Navcord> { + None + } + + fn active_polygons(&self) -> &[GenericIndex] { + &[] + } + + /// Get ghosts. Ghosts are the shapes that Topola attempted to create but + /// failed due to them infringing on other shapes. + fn ghosts(&self) -> &[PrimitiveShape] { + &[] + } + + /// Get the obstacles that prevented Topola from creating new objects (the + /// shapes of these objects can be obtained from the above `.ghosts(...)`) + /// method. This allows to highlight what prevented Topola's algorithm from + /// going some way. fn obstacles(&self) -> &[PrimitiveIndex] { &[] } -} -/// Trait for getting text strings with debug information attached to navmesh -/// edges and vertices. -#[enum_dispatch] -pub trait GetNavmeshDebugTexts { + fn polygonal_blockers(&self) -> &[LineString] { + &[] + } + + /// Get a text string to show on the debug overlay near to a navnode. + /// Usually returns None, this method exists only for quick and dirty + /// debugging. fn navnode_debug_text(&self, _navnode: NavnodeIndex) -> Option<&str> { None } + /// Get a text string to show on the debug overlay near to a navedge. + /// Usually returns None, this method exists only for quick and dirty + /// debugging. fn navedge_debug_text(&self, _navedge: (NavnodeIndex, NavnodeIndex)) -> Option<&str> { None } diff --git a/src/autorouter/measure_length.rs b/src/autorouter/measure_length.rs index 7aaa588..1ccc269 100644 --- a/src/autorouter/measure_length.rs +++ b/src/autorouter/measure_length.rs @@ -10,14 +10,7 @@ use crate::{ board::AccessMesadata, geometry::shape::MeasureLength as MeasureLengthTrait, graph::MakeRef, }; -use super::{ - invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts, GetObstacles, - GetPolygonalBlockers, - }, - selection::BandSelection, - Autorouter, AutorouterError, -}; +use super::{invoker::GetDebugOverlayData, selection::BandSelection, Autorouter, AutorouterError}; pub struct MeasureLengthExecutionStepper { selection: BandSelection, @@ -54,9 +47,4 @@ impl MeasureLengthExecutionStepper { } } -impl GetGhosts for MeasureLengthExecutionStepper {} -impl GetMaybeNavcord for MeasureLengthExecutionStepper {} -impl GetMaybeThetastarStepper for MeasureLengthExecutionStepper {} -impl GetNavmeshDebugTexts for MeasureLengthExecutionStepper {} -impl GetObstacles for MeasureLengthExecutionStepper {} -impl GetPolygonalBlockers for MeasureLengthExecutionStepper {} +impl GetDebugOverlayData for MeasureLengthExecutionStepper {} diff --git a/src/autorouter/place_via.rs b/src/autorouter/place_via.rs index a297057..01a0d44 100644 --- a/src/autorouter/place_via.rs +++ b/src/autorouter/place_via.rs @@ -11,13 +11,7 @@ use crate::{ layout::{via::ViaWeight, LayoutEdit}, }; -use super::{ - invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts, GetObstacles, - GetPolygonalBlockers, - }, - Autorouter, AutorouterError, -}; +use super::{invoker::GetDebugOverlayData, Autorouter, AutorouterError}; #[derive(Debug)] pub struct PlaceViaExecutionStepper { @@ -52,9 +46,4 @@ impl PlaceViaExecutionStepper { } } -impl GetGhosts for PlaceViaExecutionStepper {} -impl GetMaybeNavcord for PlaceViaExecutionStepper {} -impl GetMaybeThetastarStepper for PlaceViaExecutionStepper {} -impl GetNavmeshDebugTexts for PlaceViaExecutionStepper {} -impl GetObstacles for PlaceViaExecutionStepper {} -impl GetPolygonalBlockers for PlaceViaExecutionStepper {} +impl GetDebugOverlayData for PlaceViaExecutionStepper {} diff --git a/src/autorouter/remove_bands.rs b/src/autorouter/remove_bands.rs index 9736ca0..0b42860 100644 --- a/src/autorouter/remove_bands.rs +++ b/src/autorouter/remove_bands.rs @@ -6,14 +6,7 @@ use crate::{board::AccessMesadata, layout::LayoutEdit}; -use super::{ - invoker::{ - GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, GetNavmeshDebugTexts, GetObstacles, - GetPolygonalBlockers, - }, - selection::BandSelection, - Autorouter, AutorouterError, -}; +use super::{invoker::GetDebugOverlayData, selection::BandSelection, Autorouter, AutorouterError}; #[derive(Debug)] pub struct RemoveBandsExecutionStepper { @@ -51,9 +44,4 @@ impl RemoveBandsExecutionStepper { } } -impl GetGhosts for RemoveBandsExecutionStepper {} -impl GetMaybeNavcord for RemoveBandsExecutionStepper {} -impl GetMaybeThetastarStepper for RemoveBandsExecutionStepper {} -impl GetNavmeshDebugTexts for RemoveBandsExecutionStepper {} -impl GetObstacles for RemoveBandsExecutionStepper {} -impl GetPolygonalBlockers for RemoveBandsExecutionStepper {} +impl GetDebugOverlayData for RemoveBandsExecutionStepper {} diff --git a/src/interactor/activity.rs b/src/interactor/activity.rs index 1debe59..560da9e 100644 --- a/src/interactor/activity.rs +++ b/src/interactor/activity.rs @@ -11,11 +11,7 @@ use thiserror::Error; use crate::{ autorouter::{ execution::ExecutionStepper, - invoker::{ - GetActivePolygons, GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, - GetMaybeTopoNavmesh, GetNavmeshDebugTexts, GetObstacles, GetPolygonalBlockers, Invoker, - InvokerError, - }, + invoker::{GetDebugOverlayData, Invoker, InvokerError}, }, board::AccessMesadata, drawing::graph::PrimitiveIndex, @@ -32,7 +28,7 @@ use crate::{ stepper::{Abort, OnEvent, Step}, }; -/// Stores the interactive input data from the user +/// Stores the interactive input data from the user. pub struct InteractiveInput { pub active_layer: Option, pub pointer_pos: Point, @@ -47,18 +43,18 @@ pub enum InteractiveEventKind { PointerSecondaryButtonClicked, } -/// An event received from the user +/// An event received from the user. pub struct InteractiveEvent { pub kind: InteractiveEventKind, - /// `true` if the `Ctrl` key pressed during the event + /// `true` if the `Ctrl` key is pressed during the event pub ctrl: bool, - /// `true` if the `Shift` key pressed during the event + /// `true` if the `Shift` key is pressed during the event pub shift: bool, } -/// This is the execution context passed to the stepper on each step +/// This is the execution context passed to the stepper on each step. pub struct ActivityContext<'a, M> { pub interactive_input: &'a InteractiveInput, pub invoker: &'a mut Invoker, @@ -72,17 +68,8 @@ pub enum ActivityError { Invoker(#[from] InvokerError), } -/// An activity is either an interaction or an execution -#[enum_dispatch( - GetActivePolygons, - GetGhosts, - GetMaybeNavcord, - GetMaybeThetastarStepper, - GetMaybeTopoNavmesh, - GetNavmeshDebugTexts, - GetObstacles, - GetPolygonalBlockers -)] +/// An activity is either an interaction or an execution. +#[enum_dispatch(GetDebugOverlayData)] pub enum ActivityStepper { Interaction(InteractionStepper), Execution(ExecutionStepper), @@ -192,49 +179,35 @@ impl OnEvent, InteractiveEvent } } -impl GetActivePolygons for ActivityStepperWithStatus { - fn active_polygons(&self) -> &[GenericIndex] { - self.activity.active_polygons() - } -} - -impl GetMaybeThetastarStepper for ActivityStepperWithStatus { +impl GetDebugOverlayData for ActivityStepperWithStatus { fn maybe_thetastar(&self) -> Option<&ThetastarStepper> { self.activity.maybe_thetastar() } -} -impl GetMaybeTopoNavmesh for ActivityStepperWithStatus { fn maybe_topo_navmesh(&self) -> Option> { self.activity.maybe_topo_navmesh() } -} -impl GetMaybeNavcord for ActivityStepperWithStatus { fn maybe_navcord(&self) -> Option<&Navcord> { self.activity.maybe_navcord() } -} -impl GetGhosts for ActivityStepperWithStatus { + fn active_polygons(&self) -> &[GenericIndex] { + self.activity.active_polygons() + } + fn ghosts(&self) -> &[PrimitiveShape] { self.activity.ghosts() } -} -impl GetPolygonalBlockers for ActivityStepperWithStatus { - fn polygonal_blockers(&self) -> &[LineString] { - self.activity.polygonal_blockers() - } -} - -impl GetObstacles for ActivityStepperWithStatus { fn obstacles(&self) -> &[PrimitiveIndex] { self.activity.obstacles() } -} -impl GetNavmeshDebugTexts for ActivityStepperWithStatus { + fn polygonal_blockers(&self) -> &[LineString] { + self.activity.polygonal_blockers() + } + fn navnode_debug_text(&self, navnode: NavnodeIndex) -> Option<&str> { self.activity.navnode_debug_text(navnode) } diff --git a/src/interactor/interaction.rs b/src/interactor/interaction.rs index 1a65c1c..6e6e98d 100644 --- a/src/interactor/interaction.rs +++ b/src/interactor/interaction.rs @@ -6,10 +6,7 @@ use core::ops::ControlFlow; use thiserror::Error; use crate::{ - autorouter::invoker::{ - GetActivePolygons, GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, - GetMaybeTopoNavmesh, GetNavmeshDebugTexts, GetObstacles, GetPolygonalBlockers, Invoker, - }, + autorouter::invoker::{GetDebugOverlayData, Invoker}, board::AccessMesadata, stepper::{Abort, OnEvent, Step}, }; @@ -67,11 +64,4 @@ impl OnEvent, InteractiveEvent> for In } } -impl GetActivePolygons for InteractionStepper {} -impl GetGhosts for InteractionStepper {} -impl GetMaybeNavcord for InteractionStepper {} -impl GetMaybeThetastarStepper for InteractionStepper {} -impl GetMaybeTopoNavmesh for InteractionStepper {} -impl GetNavmeshDebugTexts for InteractionStepper {} -impl GetObstacles for InteractionStepper {} -impl GetPolygonalBlockers for InteractionStepper {} +impl GetDebugOverlayData for InteractionStepper {} diff --git a/src/router/ng/router.rs b/src/router/ng/router.rs index c6cc04e..3bb0e2d 100644 --- a/src/router/ng/router.rs +++ b/src/router/ng/router.rs @@ -10,10 +10,7 @@ use std::{ }; use crate::{ - autorouter::invoker::{ - GetActivePolygons, GetGhosts, GetMaybeNavcord, GetMaybeThetastarStepper, - GetMaybeTopoNavmesh, GetNavmeshDebugTexts, GetObstacles, GetPolygonalBlockers, - }, + autorouter::invoker::GetDebugOverlayData, drawing::{band::BandUid, dot::FixedDotIndex, graph::PrimitiveIndex, rules::AccessRules}, geometry::primitive::PrimitiveShape, graph::GenericIndex, @@ -235,23 +232,7 @@ impl AutorouteExecutionStepp } } -impl GetActivePolygons for AutorouteExecutionStepper { - fn active_polygons(&self) -> &[GenericIndex] { - &self.active_polygons[..] - } -} - -impl GetGhosts for AutorouteExecutionStepper { - fn ghosts(&self) -> &[PrimitiveShape] { - &self.ghosts[..] - } -} - -impl GetMaybeThetastarStepper for AutorouteExecutionStepper {} -impl GetMaybeNavcord for AutorouteExecutionStepper {} -impl GetNavmeshDebugTexts for AutorouteExecutionStepper {} - -impl GetMaybeTopoNavmesh for AutorouteExecutionStepper { +impl GetDebugOverlayData for AutorouteExecutionStepper { fn maybe_topo_navmesh(&self) -> Option> { Some(pie::navmesh::NavmeshRef { nodes: &self.pmg_astar.nodes, @@ -259,15 +240,19 @@ impl GetMaybeTopoNavmesh for AutorouteExecutionStepper { edge_paths: &self.last_edge_paths, }) } -} -impl GetObstacles for AutorouteExecutionStepper { + fn active_polygons(&self) -> &[GenericIndex] { + &self.active_polygons[..] + } + + fn ghosts(&self) -> &[PrimitiveShape] { + &self.ghosts[..] + } + fn obstacles(&self) -> &[PrimitiveIndex] { &self.obstacles[..] } -} -impl GetPolygonalBlockers for AutorouteExecutionStepper { fn polygonal_blockers(&self) -> &[LineString] { &self.polygonal_blockers[..] } @@ -534,23 +519,7 @@ impl ManualrouteExecutionSte } } -impl GetActivePolygons for ManualrouteExecutionStepper { - fn active_polygons(&self) -> &[GenericIndex] { - &self.active_polygons[..] - } -} - -impl GetGhosts for ManualrouteExecutionStepper { - fn ghosts(&self) -> &[PrimitiveShape] { - &self.ghosts[..] - } -} - -impl GetMaybeThetastarStepper for ManualrouteExecutionStepper {} -impl GetMaybeNavcord for ManualrouteExecutionStepper {} -impl GetNavmeshDebugTexts for ManualrouteExecutionStepper {} - -impl GetMaybeTopoNavmesh for ManualrouteExecutionStepper { +impl GetDebugOverlayData for ManualrouteExecutionStepper { fn maybe_topo_navmesh(&self) -> Option> { Some(pie::navmesh::NavmeshRef { nodes: &self.nodes, @@ -558,15 +527,19 @@ impl GetMaybeTopoNavmesh for ManualrouteExecutionStepper { edge_paths: &self.last_edge_paths(), }) } -} -impl GetObstacles for ManualrouteExecutionStepper { + fn active_polygons(&self) -> &[GenericIndex] { + &self.active_polygons[..] + } + + fn ghosts(&self) -> &[PrimitiveShape] { + &self.ghosts[..] + } + fn obstacles(&self) -> &[PrimitiveIndex] { &self.obstacles[..] } -} -impl GetPolygonalBlockers for ManualrouteExecutionStepper { fn polygonal_blockers(&self) -> &[LineString] { &self.polygonal_blockers[..] }