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,
},
geometry::{shape::AccessShape, GenericNode},
graph::MakeRef,
layout::{poly::MakePolygon, via::ViaWeight},
math::Circle,
};
@ -176,7 +177,7 @@ impl Viewport {
.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::{
board::{AccessMesadata, Board},
drawing::{band::BandTermsegIndex, dot::FixedDotIndex, Infringement},
graph::MakeRef,
layout::{via::ViaWeight, LayoutEdit},
router::{astar::AstarError, navmesh::NavmeshError, RouterOptions},
triangulation::GetTrianvertexNodeIndex,
@ -77,7 +78,7 @@ impl<M: AccessMesadata> Autorouter<M> {
.node_index()
{
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)
@ -195,7 +196,7 @@ impl<M: AccessMesadata> Autorouter<M> {
.node_index()
{
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
@ -206,7 +207,7 @@ impl<M: AccessMesadata> Autorouter<M> {
.node_index()
{
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)

View File

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

View File

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

View File

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

View File

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

View File

@ -18,21 +18,29 @@ use crate::{
Drawing,
},
geometry::{GetLayer, GetSetPos},
graph::{GenericIndex, GetPetgraphIndex},
graph::{GenericIndex, GetPetgraphIndex, MakeRef},
layout::CompoundWeight,
};
use super::Layout;
#[enum_dispatch]
pub trait MakePolygon {
fn shape(&self) -> Polygon;
}
#[derive(Debug)]
pub struct Poly<'a, R> {
pub struct PolyRef<'a, R> {
pub index: GenericIndex<PolyWeight>,
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>(
drawing: &'a Drawing<CompoundWeight, R>,
dot: FixedDotIndex,
@ -45,7 +53,7 @@ pub(super) fn is_apex<'a, R: AccessRules>(
&& 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 {
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 {
if let CompoundWeight::Poly(weight) = self.drawing.compound_weight(self.index.into()) {
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> {
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 {
Polygon::new(
LineString::from(