refactor(autorouter/autorouter): Move ratline sorting to `permutator.rs`

This commit is contained in:
Mikolaj Wielgus 2025-09-05 12:58:30 +02:00
parent f99f31e356
commit 19c6ede09a
2 changed files with 36 additions and 35 deletions

View File

@ -112,37 +112,7 @@ impl<M: AccessMesadata> Autorouter<M> {
selection: &PinSelection, selection: &PinSelection,
options: AutorouterOptions, options: AutorouterOptions,
) -> Result<AutorouteExecutionPermutator, AutorouterError> { ) -> Result<AutorouteExecutionPermutator, AutorouterError> {
let mut ratlines = self.selected_ratlines(selection); AutorouteExecutionPermutator::new(self, self.selected_ratlines(selection), options)
match options.presort_by {
PresortBy::RatlineIntersectionCountAndLength => ratlines.sort_unstable_by(|a, b| {
let a_intersector_count = a.ref_(self).interior_obstacle_ratlines().count();
let b_intersector_count = b.ref_(self).interior_obstacle_ratlines().count();
let primary_ordering = a_intersector_count.cmp(&b_intersector_count);
if primary_ordering != Ordering::Equal {
primary_ordering
} else {
let a_length = a.ref_(self).length();
let b_length = b.ref_(self).length();
let secondary_ordering = a_length.total_cmp(&b_length);
secondary_ordering
}
}),
PresortBy::PairwiseDetours => ratlines.sort_unstable_by(|a, b| {
let mut compare_detours = self.compare_detours_ratlines(*a, *b, options).unwrap();
if let Ok((al, bl)) = compare_detours.finish(self) {
PartialOrd::partial_cmp(&al, &bl).unwrap()
} else {
Ordering::Equal
}
}),
}
AutorouteExecutionPermutator::new(self, ratlines, options)
} }
pub(super) fn autoroute_ratlines( pub(super) fn autoroute_ratlines(

View File

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use std::{iter::Skip, ops::ControlFlow}; use std::{cmp::Ordering, iter::Skip, ops::ControlFlow};
use itertools::{Itertools, Permutations}; use itertools::{Itertools, Permutations};
use specctra_core::mesadata::AccessMesadata; use specctra_core::mesadata::AccessMesadata;
@ -12,11 +12,12 @@ use crate::{
autoroute::{AutorouteContinueStatus, AutorouteExecutionStepper}, autoroute::{AutorouteContinueStatus, AutorouteExecutionStepper},
invoker::GetDebugOverlayData, invoker::GetDebugOverlayData,
ratline::RatlineIndex, ratline::RatlineIndex,
Autorouter, AutorouterError, AutorouterOptions, Autorouter, AutorouterError, AutorouterOptions, PresortBy,
}, },
board::edit::BoardEdit, board::edit::BoardEdit,
drawing::graph::PrimitiveIndex, drawing::graph::PrimitiveIndex,
geometry::primitive::PrimitiveShape, geometry::{primitive::PrimitiveShape, shape::MeasureLength},
graph::MakeRef,
router::{navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper}, router::{navcord::Navcord, navmesh::Navmesh, thetastar::ThetastarStepper},
stepper::{Abort, EstimateProgress, Permutate, Step}, stepper::{Abort, EstimateProgress, Permutate, Step},
}; };
@ -30,11 +31,41 @@ pub struct AutorouteExecutionPermutator {
impl AutorouteExecutionPermutator { impl AutorouteExecutionPermutator {
pub fn new( pub fn new(
autorouter: &mut Autorouter<impl AccessMesadata>, autorouter: &mut Autorouter<impl AccessMesadata>,
ratlines: Vec<RatlineIndex>, mut ratlines: Vec<RatlineIndex>,
options: AutorouterOptions, options: AutorouterOptions,
) -> Result<Self, AutorouterError> { ) -> Result<Self, AutorouterError> {
let ratlines_len = ratlines.len(); let ratlines_len = ratlines.len();
match options.presort_by {
PresortBy::RatlineIntersectionCountAndLength => ratlines.sort_unstable_by(|a, b| {
let a_intersector_count = a.ref_(autorouter).interior_obstacle_ratlines().count();
let b_intersector_count = b.ref_(autorouter).interior_obstacle_ratlines().count();
let primary_ordering = a_intersector_count.cmp(&b_intersector_count);
if primary_ordering != Ordering::Equal {
primary_ordering
} else {
let a_length = a.ref_(autorouter).length();
let b_length = b.ref_(autorouter).length();
let secondary_ordering = a_length.total_cmp(&b_length);
secondary_ordering
}
}),
PresortBy::PairwiseDetours => ratlines.sort_unstable_by(|a, b| {
let mut compare_detours = autorouter
.compare_detours_ratlines(*a, *b, options)
.unwrap();
if let Ok((al, bl)) = compare_detours.finish(autorouter) {
PartialOrd::partial_cmp(&al, &bl).unwrap()
} else {
Ordering::Equal
}
}),
}
Ok(Self { Ok(Self {
stepper: AutorouteExecutionStepper::new(autorouter, ratlines.clone(), options)?, stepper: AutorouteExecutionStepper::new(autorouter, ratlines.clone(), options)?,
// Note: I assume here that the first permutation is the same as the original order. // Note: I assume here that the first permutation is the same as the original order.