refactor(layout::poly): use `MakeRef<...>` to obtain poly's ref-struct

This commit is contained in:
Mikolaj Wielgus 2025-04-22 21:59:45 +02:00
parent 4bf3611bb7
commit b736aa2d7d
7 changed files with 37 additions and 31 deletions

View File

@ -19,6 +19,7 @@ use topola::{
primitive::MakePrimitiveShape, primitive::MakePrimitiveShape,
}, },
geometry::{shape::AccessShape, GenericNode}, geometry::{shape::AccessShape, GenericNode},
graph::MakeRef,
layout::{poly::MakePolygon, via::ViaWeight}, layout::{poly::MakePolygon, via::ViaWeight},
math::Circle, math::Circle,
}; };
@ -176,7 +177,7 @@ impl Viewport {
.normal .normal
}; };
painter.paint_polygon(&board.layout().poly(poly).shape(), color) painter.paint_polygon(&poly.ref_(board.layout()).shape(), color)
} }
} }
} }

View File

@ -12,6 +12,7 @@ use thiserror::Error;
use crate::{ use crate::{
board::{AccessMesadata, Board}, board::{AccessMesadata, Board},
drawing::{band::BandTermsegIndex, dot::FixedDotIndex, Infringement}, drawing::{band::BandTermsegIndex, dot::FixedDotIndex, Infringement},
graph::MakeRef,
layout::{via::ViaWeight, LayoutEdit}, layout::{via::ViaWeight, LayoutEdit},
router::{astar::AstarError, navmesh::NavmeshError, RouterOptions}, router::{astar::AstarError, navmesh::NavmeshError, RouterOptions},
triangulation::GetTrianvertexNodeIndex, triangulation::GetTrianvertexNodeIndex,
@ -77,7 +78,7 @@ impl<M: AccessMesadata> Autorouter<M> {
.node_index() .node_index()
{ {
RatvertexIndex::FixedDot(dot) => dot, RatvertexIndex::FixedDot(dot) => dot,
RatvertexIndex::Poly(poly) => self.board.layout().poly(poly).apex(), RatvertexIndex::Poly(poly) => poly.ref_(self.board.layout()).apex(),
}; };
PointrouteExecutionStepper::new(self, origin_dot, point, options) PointrouteExecutionStepper::new(self, origin_dot, point, options)
@ -195,7 +196,7 @@ impl<M: AccessMesadata> Autorouter<M> {
.node_index() .node_index()
{ {
RatvertexIndex::FixedDot(dot) => dot, RatvertexIndex::FixedDot(dot) => dot,
RatvertexIndex::Poly(poly) => self.board.layout().poly(poly).apex(), RatvertexIndex::Poly(poly) => poly.ref_(self.board.layout()).apex(),
}; };
let target_dot = match self let target_dot = match self
@ -206,7 +207,7 @@ impl<M: AccessMesadata> Autorouter<M> {
.node_index() .node_index()
{ {
RatvertexIndex::FixedDot(dot) => dot, RatvertexIndex::FixedDot(dot) => dot,
RatvertexIndex::Poly(poly) => self.board.layout().poly(poly).apex(), RatvertexIndex::Poly(poly) => poly.ref_(self.board.layout()).apex(),
}; };
(source_dot, target_dot) (source_dot, target_dot)

View File

@ -28,7 +28,7 @@ use crate::{
rules::AccessRules, rules::AccessRules,
}, },
geometry::shape::AccessShape, geometry::shape::AccessShape,
graph::{GenericIndex, GetPetgraphIndex}, graph::{GenericIndex, GetPetgraphIndex, MakeRef},
layout::{ layout::{
poly::{MakePolygon, PolyWeight}, poly::{MakePolygon, PolyWeight},
Layout, Layout,
@ -122,7 +122,7 @@ impl Ratsnest {
handle_rvw( handle_rvw(
layout.drawing().compound_weight(poly.into()).maybe_net(), layout.drawing().compound_weight(poly.into()).maybe_net(),
RatvertexIndex::Poly(poly), RatvertexIndex::Poly(poly),
layout.poly(poly).shape().center(), poly.ref_(layout).shape().center(),
)?; )?;
} }
} }

View File

@ -17,7 +17,7 @@ use crate::{
shape::{AccessShape, Shape}, shape::{AccessShape, Shape},
GenericNode, GetLayer, GenericNode, GetLayer,
}, },
graph::{GenericIndex, GetPetgraphIndex}, graph::{GenericIndex, GetPetgraphIndex, MakeRef},
layout::{poly::PolyWeight, CompoundWeight, NodeIndex}, layout::{poly::PolyWeight, CompoundWeight, NodeIndex},
}; };
@ -39,9 +39,8 @@ impl PinSelector {
NodeIndex::Compound(compound) => { NodeIndex::Compound(compound) => {
if let CompoundWeight::Poly(..) = board.layout().drawing().compound_weight(compound) if let CompoundWeight::Poly(..) = board.layout().drawing().compound_weight(compound)
{ {
board GenericIndex::<PolyWeight>::new(compound.petgraph_index())
.layout() .ref_(board.layout())
.poly(GenericIndex::<PolyWeight>::new(compound.petgraph_index()))
.layer() .layer()
} else { } else {
unreachable!() unreachable!()

View File

@ -22,7 +22,7 @@ use crate::{
Collect, Collect,
}, },
geometry::{edit::ApplyGeometryEdit, GenericNode, GetLayer}, geometry::{edit::ApplyGeometryEdit, GenericNode, GetLayer},
graph::GenericIndex, graph::{GenericIndex, MakeRef},
layout::{poly::PolyWeight, CompoundWeight, Layout, LayoutEdit, NodeIndex}, layout::{poly::PolyWeight, CompoundWeight, Layout, LayoutEdit, NodeIndex},
}; };
@ -47,9 +47,8 @@ impl<'a> ResolvedSelector<'a> {
NodeIndex::Compound(compound) => { NodeIndex::Compound(compound) => {
match board.layout().drawing().compound_weight(compound) { match board.layout().drawing().compound_weight(compound) {
CompoundWeight::Poly(..) => ( CompoundWeight::Poly(..) => (
board GenericIndex::<PolyWeight>::new(compound.petgraph_index())
.layout() .ref_(board.layout())
.poly(GenericIndex::<PolyWeight>::new(compound.petgraph_index()))
.layer(), .layer(),
None, None,
), ),

View File

@ -34,9 +34,9 @@ use crate::{
shape::{AccessShape, Shape}, shape::{AccessShape, Shape},
GenericNode, GetLayer, GetSetPos, GenericNode, GetLayer, GetSetPos,
}, },
graph::{GenericIndex, GetPetgraphIndex}, graph::{GenericIndex, GetPetgraphIndex, MakeRef},
layout::{ layout::{
poly::{is_apex, MakePolygon, Poly, PolyWeight}, poly::{is_apex, MakePolygon, PolyWeight},
via::{Via, ViaWeight}, via::{Via, ViaWeight},
}, },
math::{Circle, LineIntersection, NormalLine}, math::{Circle, LineIntersection, NormalLine},
@ -292,7 +292,7 @@ impl<R: AccessRules> Layout<R> {
); );
} }
let shape = self.poly(poly).shape(); let shape = poly.ref_(self).shape();
let apex = self.add_fixed_dot_infringably( let apex = self.add_fixed_dot_infringably(
recorder, recorder,
FixedDotWeight(GeneralDotWeight { FixedDotWeight(GeneralDotWeight {
@ -374,10 +374,12 @@ impl<R: AccessRules> Layout<R> {
match index { match index {
NodeIndex::Primitive(primitive) => primitive.primitive(&self.drawing).shape().into(), NodeIndex::Primitive(primitive) => primitive.primitive(&self.drawing).shape().into(),
NodeIndex::Compound(compound) => match self.drawing.compound_weight(compound) { NodeIndex::Compound(compound) => match self.drawing.compound_weight(compound) {
CompoundWeight::Poly(_) => self CompoundWeight::Poly(_) => {
.poly(GenericIndex::<PolyWeight>::new(compound.petgraph_index())) GenericIndex::<PolyWeight>::new(compound.petgraph_index())
.shape() .ref_(self)
.into(), .shape()
.into()
}
CompoundWeight::Via(_) => self CompoundWeight::Via(_) => self
.via(GenericIndex::<ViaWeight>::new(compound.petgraph_index())) .via(GenericIndex::<ViaWeight>::new(compound.petgraph_index()))
.shape() .shape()
@ -507,10 +509,6 @@ impl<R: AccessRules> Layout<R> {
self.drawing.rules_mut() self.drawing.rules_mut()
} }
pub fn poly(&self, index: GenericIndex<PolyWeight>) -> Poly<R> {
Poly::new(index, self.drawing())
}
pub fn via(&self, index: GenericIndex<ViaWeight>) -> Via<R> { pub fn via(&self, index: GenericIndex<ViaWeight>) -> Via<R> {
Via::new(index, self.drawing()) Via::new(index, self.drawing())
} }

View File

@ -18,21 +18,29 @@ use crate::{
Drawing, Drawing,
}, },
geometry::{GetLayer, GetSetPos}, geometry::{GetLayer, GetSetPos},
graph::{GenericIndex, GetPetgraphIndex}, graph::{GenericIndex, GetPetgraphIndex, MakeRef},
layout::CompoundWeight, layout::CompoundWeight,
}; };
use super::Layout;
#[enum_dispatch] #[enum_dispatch]
pub trait MakePolygon { pub trait MakePolygon {
fn shape(&self) -> Polygon; fn shape(&self) -> Polygon;
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Poly<'a, R> { pub struct PolyRef<'a, R> {
pub index: GenericIndex<PolyWeight>, pub index: GenericIndex<PolyWeight>,
drawing: &'a Drawing<CompoundWeight, R>, drawing: &'a Drawing<CompoundWeight, R>,
} }
impl<'a, R: AccessRules> MakeRef<'a, PolyRef<'a, R>, Layout<R>> for GenericIndex<PolyWeight> {
fn ref_(&self, layout: &'a Layout<R>) -> PolyRef<'a, R> {
PolyRef::new(*self, layout.drawing())
}
}
pub(super) fn is_apex<'a, R: AccessRules>( pub(super) fn is_apex<'a, R: AccessRules>(
drawing: &'a Drawing<CompoundWeight, R>, drawing: &'a Drawing<CompoundWeight, R>,
dot: FixedDotIndex, dot: FixedDotIndex,
@ -45,7 +53,7 @@ pub(super) fn is_apex<'a, R: AccessRules>(
&& drawing.primitive(dot).bends().is_empty() && drawing.primitive(dot).bends().is_empty()
} }
impl<'a, R: AccessRules> Poly<'a, R> { impl<'a, R: AccessRules> PolyRef<'a, R> {
pub fn new(index: GenericIndex<PolyWeight>, drawing: &'a Drawing<CompoundWeight, R>) -> Self { pub fn new(index: GenericIndex<PolyWeight>, drawing: &'a Drawing<CompoundWeight, R>) -> Self {
Self { index, drawing } Self { index, drawing }
} }
@ -71,7 +79,7 @@ impl<'a, R: AccessRules> Poly<'a, R> {
} }
} }
impl<R: AccessRules> GetLayer for Poly<'_, R> { impl<R: AccessRules> GetLayer for PolyRef<'_, R> {
fn layer(&self) -> usize { fn layer(&self) -> usize {
if let CompoundWeight::Poly(weight) = self.drawing.compound_weight(self.index.into()) { if let CompoundWeight::Poly(weight) = self.drawing.compound_weight(self.index.into()) {
weight.layer() weight.layer()
@ -81,13 +89,13 @@ impl<R: AccessRules> GetLayer for Poly<'_, R> {
} }
} }
impl<R: AccessRules> GetMaybeNet for Poly<'_, R> { impl<R: AccessRules> GetMaybeNet for PolyRef<'_, R> {
fn maybe_net(&self) -> Option<usize> { fn maybe_net(&self) -> Option<usize> {
self.drawing.compound_weight(self.index.into()).maybe_net() self.drawing.compound_weight(self.index.into()).maybe_net()
} }
} }
impl<R: AccessRules> MakePolygon for Poly<'_, R> { impl<R: AccessRules> MakePolygon for PolyRef<'_, R> {
fn shape(&self) -> Polygon { fn shape(&self) -> Polygon {
Polygon::new( Polygon::new(
LineString::from( LineString::from(