refactor(drawing): factor out common {Bend,Dot,Seg}Weight fields

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-01-19 20:52:15 +01:00
parent 2e4b3b52ab
commit 1e690ace11
12 changed files with 161 additions and 147 deletions

View File

@ -10,7 +10,7 @@ use crate::{
board::AccessMesadata,
drawing::{
band::BandTermsegIndex,
dot::{FixedDotIndex, FixedDotWeight},
dot::{FixedDotIndex, FixedDotWeight, GeneralDotWeight},
},
layout::LayoutEdit,
math::Circle,
@ -34,14 +34,14 @@ impl PointrouteExecutionStepper {
) -> Result<Self, AutorouterError> {
let destination = autorouter.board.add_fixed_dot_infringably(
&mut LayoutEdit::new(),
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: point,
r: options.router_options.routed_band_width / 2.0,
},
layer: 0,
maybe_net: None,
},
}),
None,
);

View File

@ -18,7 +18,7 @@ use crate::{
drawing::{
band::BandUid,
bend::{BendIndex, BendWeight},
dot::{DotIndex, DotWeight, FixedDotIndex, FixedDotWeight},
dot::{DotIndex, DotWeight, FixedDotIndex, FixedDotWeight, GeneralDotWeight},
graph::{GetLayer, GetMaybeNet, PrimitiveIndex, PrimitiveWeight},
seg::{FixedSegIndex, FixedSegWeight, SegIndex, SegWeight},
Collect,
@ -246,14 +246,14 @@ impl<M: AccessMesadata> Board<M> {
} else {
self.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: resolved_poly.shape().center(),
r: 100.0,
},
layer: resolved_poly.layer(),
maybe_net: resolved_poly.maybe_net(),
},
}),
poly,
)
}

View File

@ -74,57 +74,71 @@ impl TryFrom<PrimitiveWeight> for BendWeight {
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FixedBendWeight {
pub width: f64,
pub offset: f64,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl_fixed_weight!(FixedBendWeight, FixedBend, FixedBendIndex);
pub struct FixedBendWeight(pub GeneralBendWeight);
impl_weight_forward!(FixedBendWeight, FixedBend, FixedBendIndex);
impl GetOffset for FixedBendWeight {
fn offset(&self) -> f64 {
self.offset
self.0.offset()
}
}
impl SetOffset for FixedBendWeight {
fn set_offset(&mut self, offset: f64) {
self.offset = offset
}
}
impl GetWidth for FixedBendWeight {
fn width(&self) -> f64 {
self.width
self.0.set_offset(offset);
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LooseBendWeight {
pub struct LooseBendWeight(pub GeneralBendWeight);
impl_weight_forward!(LooseBendWeight, LooseBend, LooseBendIndex);
impl GetOffset for LooseBendWeight {
fn offset(&self) -> f64 {
self.0.offset()
}
}
impl SetOffset for LooseBendWeight {
fn set_offset(&mut self, offset: f64) {
self.0.set_offset(offset);
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GeneralBendWeight {
pub width: f64,
pub offset: f64,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl GetOffset for LooseBendWeight {
impl GetLayer for GeneralBendWeight {
fn layer(&self) -> usize {
self.layer
}
}
impl GetMaybeNet for GeneralBendWeight {
fn maybe_net(&self) -> Option<usize> {
self.maybe_net
}
}
impl GetOffset for GeneralBendWeight {
fn offset(&self) -> f64 {
self.offset
}
}
impl SetOffset for LooseBendWeight {
impl SetOffset for GeneralBendWeight {
fn set_offset(&mut self, offset: f64) {
self.offset = offset
}
}
impl GetWidth for LooseBendWeight {
impl GetWidth for GeneralBendWeight {
fn width(&self) -> f64 {
self.width
}
}
impl_loose_weight!(LooseBendWeight, LooseBend, LooseBendIndex);

View File

@ -76,50 +76,61 @@ impl TryFrom<PrimitiveWeight> for DotWeight {
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FixedDotWeight {
pub circle: Circle,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl_fixed_weight!(FixedDotWeight, FixedDot, FixedDotIndex);
pub struct FixedDotWeight(pub GeneralDotWeight);
impl_weight_forward!(FixedDotWeight, FixedDot, FixedDotIndex);
impl GetSetPos for FixedDotWeight {
fn pos(&self) -> Point {
self.circle.pos
self.0.pos()
}
fn set_pos(&mut self, pos: Point) {
self.circle.pos = pos
}
}
impl GetWidth for FixedDotWeight {
fn width(&self) -> f64 {
self.circle.r * 2.0
self.0.set_pos(pos);
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LooseDotWeight {
pub struct LooseDotWeight(pub GeneralDotWeight);
impl_weight_forward!(LooseDotWeight, LooseDot, LooseDotIndex);
impl GetSetPos for LooseDotWeight {
fn pos(&self) -> Point {
self.0.pos()
}
fn set_pos(&mut self, pos: Point) {
self.0.set_pos(pos);
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GeneralDotWeight {
pub circle: Circle,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl_loose_weight!(LooseDotWeight, LooseDot, LooseDotIndex);
impl GetLayer for GeneralDotWeight {
fn layer(&self) -> usize {
self.layer
}
}
impl GetSetPos for LooseDotWeight {
impl GetMaybeNet for GeneralDotWeight {
fn maybe_net(&self) -> Option<usize> {
self.maybe_net
}
}
impl GetSetPos for GeneralDotWeight {
fn pos(&self) -> Point {
self.circle.pos
}
fn set_pos(&mut self, pos: Point) {
self.circle.pos = pos
self.circle.pos = pos;
}
}
impl GetWidth for LooseDotWeight {
impl GetWidth for GeneralDotWeight {
fn width(&self) -> f64 {
self.circle.r * 2.0
}

View File

@ -329,7 +329,7 @@ impl<CW: Copy, R: AccessRules> Drawing<CW, R> {
) -> Result<LooseBendIndex, DrawingException> {
// It makes no sense to wrap something around or under one of its connectables.
//
if let Some(net) = weight.maybe_net {
if let Some(net) = weight.maybe_net() {
if let Some(around_net) = around.primitive(self).maybe_net() {
if net == around_net {
return Err(AlreadyConnected(net, around.into()).into());

View File

@ -59,23 +59,29 @@ pub trait MakePrimitive {
) -> Primitive<'a, CW, R>;
}
macro_rules! impl_weight {
($weight_struct:ident, $weight_variant:ident, $index_struct:ident) => {
macro_rules! impl_weight_forward {
($weight_struct:ty, $weight_variant:ident, $index_struct:ident) => {
impl Retag<PrimitiveIndex> for $weight_struct {
fn retag(&self, index: NodeIndex<usize>) -> PrimitiveIndex {
PrimitiveIndex::$weight_variant($index_struct::new(index))
}
}
impl<'a> GetLayer for $weight_struct {
impl GetLayer for $weight_struct {
fn layer(&self) -> usize {
self.layer
self.0.layer()
}
}
impl<'a> GetMaybeNet for $weight_struct {
impl GetMaybeNet for $weight_struct {
fn maybe_net(&self) -> Option<usize> {
self.maybe_net
self.0.maybe_net()
}
}
impl GetWidth for $weight_struct {
fn width(&self) -> f64 {
self.0.width()
}
}
@ -92,18 +98,6 @@ macro_rules! impl_weight {
};
}
macro_rules! impl_fixed_weight {
($weight_struct:ident, $weight_variant:ident, $index_struct:ident) => {
impl_weight!($weight_struct, $weight_variant, $index_struct);
};
}
macro_rules! impl_loose_weight {
($weight_struct:ident, $weight_variant:ident, $index_struct:ident) => {
impl_weight!($weight_struct, $weight_variant, $index_struct);
};
}
// TODO: This enum shouldn't exist: we shouldn't be carrying the tag around like this. Instead we
// should be getting it from the graph when it's needed.
#[enum_dispatch(GetPetgraphIndex, MakePrimitive)]

View File

@ -82,7 +82,7 @@ impl<CW: Copy, R: AccessRules> Guide for Drawing<CW, R> {
) -> Result<Line, NoTangents> {
let from_circle = self.head_circle(head, width);
let to_circle = Circle {
pos: self.primitive(into).weight().circle.pos,
pos: self.primitive(into).weight().0.circle.pos,
r: 0.0,
};

View File

@ -476,7 +476,7 @@ impl<'a, CW: Copy, R: AccessRules> GetLimbs for LooseBend<'a, CW, R> {}
impl<'a, CW: Copy, R: AccessRules> GetOffset for LooseBend<'a, CW, R> {
fn offset(&self) -> f64 {
self.weight().offset
self.weight().offset()
}
}

View File

@ -80,45 +80,37 @@ impl TryFrom<PrimitiveWeight> for SegWeight {
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FixedSegWeight {
pub width: f64,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl_fixed_weight!(FixedSegWeight, FixedSeg, FixedSegIndex);
impl GetWidth for FixedSegWeight {
fn width(&self) -> f64 {
self.width
}
}
pub struct FixedSegWeight(pub GeneralSegWeight);
impl_weight_forward!(FixedSegWeight, FixedSeg, FixedSegIndex);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LoneLooseSegWeight {
pub width: f64,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl_loose_weight!(LoneLooseSegWeight, LoneLooseSeg, LoneLooseSegIndex);
impl GetWidth for LoneLooseSegWeight {
fn width(&self) -> f64 {
self.width
}
}
pub struct LoneLooseSegWeight(pub GeneralSegWeight);
impl_weight_forward!(LoneLooseSegWeight, LoneLooseSeg, LoneLooseSegIndex);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SeqLooseSegWeight {
pub struct SeqLooseSegWeight(pub GeneralSegWeight);
impl_weight_forward!(SeqLooseSegWeight, SeqLooseSeg, SeqLooseSegIndex);
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GeneralSegWeight {
pub width: f64,
pub layer: usize,
pub maybe_net: Option<usize>,
}
impl_loose_weight!(SeqLooseSegWeight, SeqLooseSeg, SeqLooseSegIndex);
impl GetLayer for GeneralSegWeight {
fn layer(&self) -> usize {
self.layer
}
}
impl GetWidth for SeqLooseSegWeight {
impl GetMaybeNet for GeneralSegWeight {
fn maybe_net(&self) -> Option<usize> {
self.maybe_net
}
}
impl GetWidth for GeneralSegWeight {
fn width(&self) -> f64 {
self.width
}

View File

@ -12,7 +12,10 @@ use crate::{
drawing::{
band::BandTermsegIndex,
bend::{BendIndex, BendWeight, LooseBendWeight},
dot::{DotIndex, DotWeight, FixedDotIndex, FixedDotWeight, LooseDotIndex, LooseDotWeight},
dot::{
DotIndex, DotWeight, FixedDotIndex, FixedDotWeight, GeneralDotWeight, LooseDotIndex,
LooseDotWeight,
},
gear::GearIndex,
graph::{GetMaybeNet, IsInLayer, MakePrimitive, PrimitiveIndex, PrimitiveWeight},
primitive::MakePrimitiveShape,
@ -99,11 +102,11 @@ impl<R: AccessRules> Layout<R> {
for layer in weight.from_layer..=weight.to_layer {
match self.drawing.add_fixed_dot(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: weight.circle,
layer,
maybe_net: weight.maybe_net,
},
}),
) {
Ok(dot) => {
self.drawing.add_to_compound(recorder, dot, compound);

View File

@ -11,14 +11,14 @@ use thiserror::Error;
use crate::{
drawing::{
band::BandTermsegIndex,
bend::{BendIndex, LooseBendWeight},
dot::{DotIndex, FixedDotIndex, LooseDotIndex, LooseDotWeight},
bend::{BendIndex, GeneralBendWeight, LooseBendWeight},
dot::{DotIndex, FixedDotIndex, GeneralDotWeight, LooseDotIndex, LooseDotWeight},
gear::GearIndex,
graph::{GetLayer, GetMaybeNet, MakePrimitive},
head::{CaneHead, GetFace, Head},
primitive::GetOtherJoint,
rules::AccessRules,
seg::{LoneLooseSegWeight, SeqLooseSegWeight},
seg::{GeneralSegWeight, LoneLooseSegWeight, SeqLooseSegWeight},
DrawingException, Guide, Infringement,
},
layout::{Layout, LayoutEdit},
@ -98,11 +98,11 @@ impl<R: AccessRules> Draw for Layout<R> {
recorder,
dot,
into,
LoneLooseSegWeight {
LoneLooseSegWeight(GeneralSegWeight {
width,
layer,
maybe_net,
},
}),
)
.map_err(|err| DrawException::CannotFinishIn(into, err.into()))?,
),
@ -111,11 +111,11 @@ impl<R: AccessRules> Draw for Layout<R> {
recorder,
into.into(),
dot,
SeqLooseSegWeight {
SeqLooseSegWeight(GeneralSegWeight {
width,
layer,
maybe_net,
},
}),
)
.map_err(|err| DrawException::CannotFinishIn(into, err.into()))?,
),
@ -279,25 +279,25 @@ impl<R: AccessRules> DrawPrivate for Layout<R> {
recorder,
head.face(),
around,
LooseDotWeight {
LooseDotWeight(GeneralDotWeight {
circle: Circle {
pos: to,
r: width / 2.0,
},
layer,
maybe_net,
},
SeqLooseSegWeight {
}),
SeqLooseSegWeight(GeneralSegWeight {
width,
layer,
maybe_net,
},
LooseBendWeight {
}),
LooseBendWeight(GeneralBendWeight {
width,
offset,
layer,
maybe_net,
},
}),
cw,
)?;
Ok(CaneHead {

View File

@ -13,10 +13,10 @@ use geo::{point, Point, Rotate};
use crate::{
board::{AccessMesadata, Board},
drawing::{
dot::FixedDotWeight,
dot::{FixedDotWeight, GeneralDotWeight},
graph::{GetLayer, GetMaybeNet, MakePrimitive},
primitive::MakePrimitiveShape,
seg::FixedSegWeight,
seg::{FixedSegWeight, GeneralSegWeight},
Drawing,
},
geometry::{primitive::PrimitiveShape, GetWidth},
@ -434,11 +434,11 @@ impl SpecctraDesign {
board.add_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle,
layer,
maybe_net,
},
}),
maybe_pin,
);
}
@ -465,50 +465,50 @@ impl SpecctraDesign {
// Corners.
let dot_1_1 = board.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: Self::pos(place, pin, x1, y1),
r: 0.5,
},
layer,
maybe_net,
},
}),
poly,
);
let dot_2_1 = board.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: Self::pos(place, pin, x2, y1),
r: 0.5,
},
layer,
maybe_net,
},
}),
poly,
);
let dot_2_2 = board.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: Self::pos(place, pin, x2, y2),
r: 0.5,
},
layer,
maybe_net,
},
}),
poly,
);
let dot_1_2 = board.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: Self::pos(place, pin, x1, y2),
r: 0.5,
},
layer,
maybe_net,
},
}),
poly,
);
// Sides.
@ -516,44 +516,44 @@ impl SpecctraDesign {
recorder,
dot_1_1,
dot_2_1,
FixedSegWeight {
FixedSegWeight(GeneralSegWeight {
width: 1.0,
layer,
maybe_net,
},
}),
poly,
);
board.add_poly_fixed_seg_infringably(
recorder,
dot_2_1,
dot_2_2,
FixedSegWeight {
FixedSegWeight(GeneralSegWeight {
width: 1.0,
layer,
maybe_net,
},
}),
poly,
);
board.add_poly_fixed_seg_infringably(
recorder,
dot_2_2,
dot_1_2,
FixedSegWeight {
FixedSegWeight(GeneralSegWeight {
width: 1.0,
layer,
maybe_net,
},
}),
poly,
);
board.add_poly_fixed_seg_infringably(
recorder,
dot_1_2,
dot_1_1,
FixedSegWeight {
FixedSegWeight(GeneralSegWeight {
width: 1.0,
layer,
maybe_net,
},
}),
poly,
);
}
@ -573,14 +573,14 @@ impl SpecctraDesign {
let mut prev_pos = Self::pos(place, pin, coords[0].x, coords[0].y);
let mut prev_index = board.add_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: prev_pos,
r: width / 2.0,
},
layer,
maybe_net,
},
}),
maybe_pin.clone(),
);
@ -594,14 +594,14 @@ impl SpecctraDesign {
let index = board.add_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos,
r: width / 2.0,
},
layer,
maybe_net,
},
}),
maybe_pin.clone(),
);
@ -610,11 +610,11 @@ impl SpecctraDesign {
recorder,
prev_index,
index,
FixedSegWeight {
FixedSegWeight(GeneralSegWeight {
width,
layer,
maybe_net,
},
}),
maybe_pin.clone(),
);
@ -643,14 +643,14 @@ impl SpecctraDesign {
// add the first coordinate in the wire path as a dot and save its index
let mut prev_index = board.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: Self::pos(place, pin, coords[0].x, coords[0].y),
r: width / 2.0,
},
layer,
maybe_net,
},
}),
// TODO: This manual retagging shouldn't be necessary, `.into()` should suffice.
//GenericIndex::new(poly.petgraph_index()).into(),
poly,
@ -660,14 +660,14 @@ impl SpecctraDesign {
for coord in coords.iter().skip(1) {
let index = board.add_poly_fixed_dot_infringably(
recorder,
FixedDotWeight {
FixedDotWeight(GeneralDotWeight {
circle: Circle {
pos: Self::pos(place, pin, coord.x, coord.y),
r: width / 2.0,
},
layer,
maybe_net,
},
}),
// TODO: This manual retagging shouldn't be necessary, `.into()` should suffice.
poly,
);
@ -677,11 +677,11 @@ impl SpecctraDesign {
recorder,
prev_index,
index,
FixedSegWeight {
FixedSegWeight(GeneralSegWeight {
width,
layer,
maybe_net,
},
}),
// TODO: This manual retagging shouldn't be necessary, `.into()` should suffice.
poly,
);