refactor: simplify interface of NavcordStepper

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-03-18 03:44:48 +01:00 committed by mikolaj
parent 112cc0686d
commit a836189086
3 changed files with 17 additions and 52 deletions

View File

@ -7,7 +7,6 @@ use petgraph::data::DataMap;
use crate::{ use crate::{
drawing::{ drawing::{
bend::LooseBendIndex,
dot::FixedDotIndex, dot::FixedDotIndex,
head::{BareHead, CaneHead, Head}, head::{BareHead, CaneHead, Head},
rules::AccessRules, rules::AccessRules,
@ -67,54 +66,29 @@ impl NavcordStepper {
match around_node_weight.node { match around_node_weight.node {
BinavvertexNodeIndex::FixedDot(dot) => { BinavvertexNodeIndex::FixedDot(dot) => {
self.wrap_around_fixed_dot(layout, head, dot, cw) layout.cane_around_dot(&mut self.recorder, head, dot, cw, self.width)
}
BinavvertexNodeIndex::FixedBend(fixed_bend) => {
layout.cane_around_bend(&mut self.recorder, head, fixed_bend.into(), cw, self.width)
} }
BinavvertexNodeIndex::FixedBend(_fixed_bend) => todo!(),
BinavvertexNodeIndex::LooseBend(loose_bend) => { BinavvertexNodeIndex::LooseBend(loose_bend) => {
self.wrap_around_loose_bend(layout, head, loose_bend, cw) layout.cane_around_bend(&mut self.recorder, head, loose_bend.into(), cw, self.width)
} }
} }
.map_err(NavcorderException::CannotDraw)
} }
fn wrap_around_fixed_dot(
&mut self,
layout: &mut Layout<impl AccessRules>,
head: Head,
around: FixedDotIndex,
cw: bool,
) -> Result<CaneHead, NavcorderException> {
Ok(layout.cane_around_dot(&mut self.recorder, head, around, cw, self.width)?)
}
fn wrap_around_loose_bend(
&mut self,
layout: &mut Layout<impl AccessRules>,
head: Head,
around: LooseBendIndex,
cw: bool,
) -> Result<CaneHead, NavcorderException> {
Ok(layout.cane_around_bend(&mut self.recorder, head, around.into(), cw, self.width)?)
}
}
pub struct NavcordStepContext<'a, R> {
pub layout: &'a mut Layout<R>,
pub navmesh: &'a Navmesh,
pub to: NavvertexIndex,
}
impl NavcordStepper {
#[debug_ensures(ret.is_ok() -> matches!(self.head, Head::Cane(..)))] #[debug_ensures(ret.is_ok() -> matches!(self.head, Head::Cane(..)))]
#[debug_ensures(ret.is_ok() -> self.path.len() == old(self.path.len() + 1))] #[debug_ensures(ret.is_ok() -> self.path.len() == old(self.path.len() + 1))]
#[debug_ensures(ret.is_err() -> self.path.len() == old(self.path.len()))] #[debug_ensures(ret.is_err() -> self.path.len() == old(self.path.len()))]
pub fn step<R: AccessRules>( pub fn step<R: AccessRules>(
&mut self, &mut self,
input: &mut NavcordStepContext<'_, R>, layout: &mut Layout<R>,
navmesh: &Navmesh,
to: NavvertexIndex,
) -> Result<(), NavcorderException> { ) -> Result<(), NavcorderException> {
self.head = self self.head = self.wrap(layout, navmesh, self.head, to)?.into();
.wrap(input.layout, input.navmesh, self.head, input.to)? self.path.push(to);
.into();
self.path.push(input.to);
Ok(()) Ok(())
} }
@ -127,7 +101,8 @@ impl NavcordStepper {
if let Head::Cane(head) = self.head { if let Head::Cane(head) = self.head {
self.head = layout.undo_cane(&mut self.recorder, head).unwrap(); self.head = layout.undo_cane(&mut self.recorder, head).unwrap();
} else { } else {
panic!(); // "can't unwrap"
return Err(NavcorderException::CannotWrap);
} }
self.path.pop(); self.path.pop();

View File

@ -12,7 +12,7 @@ use crate::{
use super::{ use super::{
draw::{Draw, DrawException}, draw::{Draw, DrawException},
navcord::{NavcordStepContext, NavcordStepper}, navcord::NavcordStepper,
navmesh::{Navmesh, NavvertexIndex}, navmesh::{Navmesh, NavvertexIndex},
}; };
@ -105,11 +105,7 @@ impl<R: AccessRules> Navcorder for Layout<R> {
path: &[NavvertexIndex], path: &[NavvertexIndex],
) -> Result<(), NavcorderException> { ) -> Result<(), NavcorderException> {
for (i, vertex) in path.iter().enumerate() { for (i, vertex) in path.iter().enumerate() {
if let Err(err) = navcord.step(&mut NavcordStepContext { if let Err(err) = navcord.step(self, navmesh, *vertex) {
layout: self,
navmesh,
to: *vertex,
}) {
self.undo_path(navcord, i); self.undo_path(navcord, i);
return Err(err); return Err(err);
} }

View File

@ -28,7 +28,7 @@ use crate::{
use super::{ use super::{
astar::{AstarStrategy, PathTracker}, astar::{AstarStrategy, PathTracker},
draw::DrawException, draw::DrawException,
navcord::{NavcordStepContext, NavcordStepper}, navcord::NavcordStepper,
navcorder::{Navcorder, NavcorderException}, navcorder::{Navcorder, NavcorderException},
navmesh::{Navmesh, NavmeshEdgeReference, NavmeshError, NavvertexIndex}, navmesh::{Navmesh, NavmeshEdgeReference, NavmeshError, NavvertexIndex},
route::RouteStepper, route::RouteStepper,
@ -105,13 +105,7 @@ impl<'a, R: AccessRules> AstarStrategy<Navmesh, f64, BandTermsegIndex>
} }
let prev_bihead_length = self.bihead_length(); let prev_bihead_length = self.bihead_length();
let result = self.navcord.step(self.layout, navmesh, edge.target());
let result = self.navcord.step(&mut NavcordStepContext {
layout: self.layout,
navmesh,
to: edge.target(),
});
let probe_length = self.bihead_length() - prev_bihead_length; let probe_length = self.bihead_length() - prev_bihead_length;
match result { match result {