refactor(router/astar): Use getters instead of `pub` members

This commit is contained in:
Mikolaj Wielgus 2025-06-03 15:27:11 +02:00 committed by mikolaj
parent cfd20ed381
commit 0702b7eb8c
3 changed files with 22 additions and 15 deletions

View File

@ -251,7 +251,7 @@ impl Viewport {
if menu_bar.show_navmesh { if menu_bar.show_navmesh {
if let Some(activity) = workspace.interactor.maybe_activity() { if let Some(activity) = workspace.interactor.maybe_activity() {
if let Some(astar) = activity.maybe_astar() { if let Some(astar) = activity.maybe_astar() {
let navmesh = &astar.graph; let navmesh = astar.graph();
for edge in navmesh.edge_references() { for edge in navmesh.edge_references() {
let mut from = PrimitiveIndex::from( let mut from = PrimitiveIndex::from(
@ -354,12 +354,12 @@ impl Viewport {
}; };
//TODO "{astar.scores[index]} ({astar.estimate_scores[index]}) (...)" //TODO "{astar.scores[index]} ({astar.estimate_scores[index]}) (...)"
let score_text = &astar let score_text = astar
.scores .scores()
.get(&navnode) .get(&navnode)
.map_or_else(String::new, |s| format!("g={:.2}", s)); .map_or_else(String::new, |s| format!("g={:.2}", s));
let estimate_score_text = &astar let estimate_score_text = astar
.estimate_scores .estimate_scores()
.get(&navnode) .get(&navnode)
.map_or_else(String::new, |s| format!("(f={:.2})", s)); .map_or_else(String::new, |s| format!("(f={:.2})", s));
let debug_text = let debug_text =
@ -460,7 +460,7 @@ impl Viewport {
} }
if let Some(ref navmesh) = if let Some(ref navmesh) =
activity.maybe_astar().map(|astar| &astar.graph) activity.maybe_astar().map(|astar| astar.graph())
{ {
if menu_bar.show_origin_destination { if menu_bar.show_origin_destination {
let (origin, destination) = let (origin, destination) =

View File

@ -7,6 +7,7 @@ use std::collections::{btree_map::Entry, BTreeMap, BinaryHeap, VecDeque};
use std::ops::ControlFlow; use std::ops::ControlFlow;
use derive_getters::Getters;
use petgraph::algo::Measure; use petgraph::algo::Measure;
use petgraph::visit::{EdgeRef, GraphBase, IntoEdgeReferences, IntoEdges}; use petgraph::visit::{EdgeRef, GraphBase, IntoEdgeReferences, IntoEdges};
use thiserror::Error; use thiserror::Error;
@ -122,6 +123,7 @@ pub trait MakeEdgeRef: IntoEdgeReferences {
fn edge_ref(&self, edge_id: Self::EdgeId) -> Self::EdgeRef; fn edge_ref(&self, edge_id: Self::EdgeId) -> Self::EdgeRef;
} }
#[derive(Getters)]
pub struct AstarStepper<G, K> pub struct AstarStepper<G, K>
where where
G: GraphBase, G: GraphBase,
@ -129,18 +131,23 @@ where
for<'a> &'a G: IntoEdges<NodeId = G::NodeId, EdgeId = G::EdgeId> + MakeEdgeRef, for<'a> &'a G: IntoEdges<NodeId = G::NodeId, EdgeId = G::EdgeId> + MakeEdgeRef,
K: Measure + Copy, K: Measure + Copy,
{ {
pub graph: G, graph: G,
pub visit_next: BinaryHeap<MinScored<K, G::NodeId>>, #[getter(skip)]
visit_next: BinaryHeap<MinScored<K, G::NodeId>>,
/// Also known as the g-scores, or just g. /// Also known as the g-scores, or just g.
pub scores: BTreeMap<G::NodeId, K>, scores: BTreeMap<G::NodeId, K>,
/// Also known as the f-scores, or just f. /// Also known as the f-scores, or just f.
pub estimate_scores: BTreeMap<G::NodeId, K>, estimate_scores: BTreeMap<G::NodeId, K>,
pub path_tracker: PathTracker<G>, #[getter(skip)]
pub maybe_curr_node: Option<G::NodeId>, path_tracker: PathTracker<G>,
#[getter(skip)]
maybe_curr_node: Option<G::NodeId>,
// FIXME: To work around edge references borrowing from the graph we collect then reiterate over them. // FIXME: To work around edge references borrowing from the graph we collect then reiterate over them.
pub edge_ids: VecDeque<G::EdgeId>, #[getter(skip)]
edge_ids: VecDeque<G::EdgeId>,
// TODO: Rewrite this to be a well-designed state machine. Booleans are a code smell. // TODO: Rewrite this to be a well-designed state machine. Booleans are a code smell.
pub is_probing: bool, #[getter(skip)]
is_probing: bool,
} }
/// The status enum of the A* stepper returned when there is no failure or /// The status enum of the A* stepper returned when there is no failure or

View File

@ -77,7 +77,7 @@ impl<R: AccessRules> Step<Router<'_, R>, BandTermsegIndex> for RouteStepper {
router: &mut Router<R>, router: &mut Router<R>,
) -> Result<ControlFlow<BandTermsegIndex>, AstarError> { ) -> Result<ControlFlow<BandTermsegIndex>, AstarError> {
let layout = router.layout_mut(); let layout = router.layout_mut();
let target = self.astar.graph.destination(); let target = self.astar.graph().destination();
let mut strategy = RouterAstarStrategy::new(layout, &mut self.navcord, target); let mut strategy = RouterAstarStrategy::new(layout, &mut self.navcord, target);
let result = self.astar.step(&mut strategy); let result = self.astar.step(&mut strategy);
self.ghosts = strategy.probe_ghosts; self.ghosts = strategy.probe_ghosts;