From 2087e76dfe5af89ac38e96c51484228a513b5055 Mon Sep 17 00:00:00 2001 From: Ellen Emilia Anna Zscheile Date: Sat, 1 Feb 2025 23:54:45 +0100 Subject: [PATCH] refactor(drawing::guide): turn Guide into a trait --- src/drawing/drawing.rs | 21 +++--- src/drawing/guide.rs | 150 +++++++++++++++++++++++++++-------------- src/router/draw.rs | 22 +++--- src/router/router.rs | 2 +- 4 files changed, 118 insertions(+), 77 deletions(-) diff --git a/src/drawing/drawing.rs b/src/drawing/drawing.rs index 031308c..7cf45ab 100644 --- a/src/drawing/drawing.rs +++ b/src/drawing/drawing.rs @@ -540,12 +540,11 @@ impl Drawing { let rail_primitive = self.primitive(rail); let joints = rail_primitive.joints(); - let guide = Guide::new(self); - let from_head = guide.rear_head(joints.1); - let to_head = guide.rear_head(joints.0); + let from_head = self.rear_head(joints.1); + let to_head = self.rear_head(joints.0); if let Some(inner) = rail_primitive.inner() { - let from = guide + let from = self .head_around_bend_segment( &from_head, inner.into(), @@ -553,7 +552,7 @@ impl Drawing { self.primitive(rail).width(), )? .end_point(); - let to = guide + let to = self .head_around_bend_segment( &to_head, inner.into(), @@ -561,7 +560,7 @@ impl Drawing { self.primitive(rail).width(), )? .end_point(); - let offset = guide.head_around_bend_offset( + let offset = self.head_around_bend_offset( &from_head, inner.into(), self.primitive(rail).width(), @@ -590,7 +589,7 @@ impl Drawing { // Update offsets in case the rule conditions changed. } else { let core = rail_primitive.core(); - let from = guide + let from = self .head_around_dot_segment( &from_head, core.into(), @@ -598,7 +597,7 @@ impl Drawing { self.primitive(rail).width(), )? .end_point(); - let to = guide + let to = self .head_around_dot_segment( &to_head, core.into(), @@ -606,7 +605,7 @@ impl Drawing { self.primitive(rail).width(), )? .end_point(); - let offset = guide.head_around_dot_offset( + let offset = self.head_around_dot_offset( &from_head, core.into(), self.primitive(rail).width(), @@ -1105,10 +1104,6 @@ impl Drawing { &mut self.rules } - pub fn guide(&self) -> Guide { - Guide::new(self) - } - pub fn primitive(&self, index: GenericIndex) -> GenericPrimitive { GenericPrimitive::new(index, self) } diff --git a/src/drawing/guide.rs b/src/drawing/guide.rs index cff3ba4..51dde74 100644 --- a/src/drawing/guide.rs +++ b/src/drawing/guide.rs @@ -22,18 +22,59 @@ use super::{ Drawing, }; -pub struct Guide<'a, CW, R> { - drawing: &'a Drawing, +pub trait Guide { + fn head_into_dot_segment( + &self, + head: &Head, + into: FixedDotIndex, + width: f64, + ) -> Result; + + fn head_around_dot_segments( + &self, + head: &Head, + around: DotIndex, + width: f64, + ) -> Result<(Line, Line), NoTangents>; + + fn head_around_dot_segment( + &self, + head: &Head, + around: DotIndex, + cw: bool, + width: f64, + ) -> Result; + + fn head_around_dot_offset(&self, head: &Head, around: DotIndex, _width: f64) -> f64; + + fn head_around_bend_segments( + &self, + head: &Head, + around: BendIndex, + width: f64, + ) -> Result<(Line, Line), NoTangents>; + + fn head_around_bend_segment( + &self, + head: &Head, + around: BendIndex, + cw: bool, + width: f64, + ) -> Result; + + fn head_around_bend_offset(&self, head: &Head, around: BendIndex, _width: f64) -> f64; + + fn head_cw(&self, head: &Head) -> Option; + + fn cane_head(&self, face: LooseDotIndex) -> CaneHead; + + fn rear_head(&self, face: LooseDotIndex) -> Head; + + fn head(&self, face: DotIndex) -> Head; } -impl<'a, CW, R> Guide<'a, CW, R> { - pub fn new(drawing: &'a Drawing) -> Self { - Self { drawing } - } -} - -impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { - pub fn head_into_dot_segment( +impl Guide for Drawing { + fn head_into_dot_segment( &self, head: &Head, into: FixedDotIndex, @@ -41,7 +82,7 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { ) -> Result { let from_circle = self.head_circle(head, width); let to_circle = Circle { - pos: self.drawing.primitive(into).weight().circle.pos, + pos: self.primitive(into).weight().circle.pos, r: 0.0, }; @@ -49,7 +90,7 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { math::tangent_segment(from_circle, from_cw, to_circle, None) } - pub fn head_around_dot_segments( + fn head_around_dot_segments( &self, head: &Head, around: DotIndex, @@ -64,7 +105,7 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { Ok((tangents[0], tangents[1])) } - pub fn head_around_dot_segment( + fn head_around_dot_segment( &self, head: &Head, around: DotIndex, @@ -78,14 +119,14 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { math::tangent_segment(from_circle, from_cw, to_circle, Some(cw)) } - pub fn head_around_dot_offset(&self, head: &Head, around: DotIndex, _width: f64) -> f64 { - self.drawing.rules().clearance( + fn head_around_dot_offset(&self, head: &Head, around: DotIndex, _width: f64) -> f64 { + self.rules().clearance( &self.conditions(around.into()), &self.conditions(head.face().into()), ) } - pub fn head_around_bend_segments( + fn head_around_bend_segments( &self, head: &Head, around: BendIndex, @@ -100,7 +141,7 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { Ok((tangents[0], tangents[1])) } - pub fn head_around_bend_segment( + fn head_around_bend_segment( &self, head: &Head, around: BendIndex, @@ -114,16 +155,16 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { math::tangent_segment(from_circle, from_cw, to_circle, Some(cw)) } - pub fn head_around_bend_offset(&self, head: &Head, around: BendIndex, _width: f64) -> f64 { - self.drawing.rules().clearance( + fn head_around_bend_offset(&self, head: &Head, around: BendIndex, _width: f64) -> f64 { + self.rules().clearance( &self.conditions(head.face().into()), &self.conditions(around.into()), ) } - pub fn head_cw(&self, head: &Head) -> Option { + fn head_cw(&self, head: &Head) -> Option { if let Head::Cane(head) = head { - let joints = self.drawing.primitive(head.cane.bend).joints(); + let joints = self.primitive(head.cane.bend).joints(); if head.face() == joints.0.into() { Some(false) @@ -135,18 +176,50 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { } } + fn cane_head(&self, face: LooseDotIndex) -> CaneHead { + CaneHead { + face, + cane: self.cane(face), + } + } + + fn rear_head(&self, face: LooseDotIndex) -> Head { + self.head(self.rear(self.cane_head(face))) + } + + fn head(&self, face: DotIndex) -> Head { + match face { + DotIndex::Fixed(dot) => BareHead { face: dot }.into(), + DotIndex::Loose(dot) => self.cane_head(dot).into(), + } + } +} + +trait GuidePrivate { + fn head_circle(&self, head: &Head, width: f64) -> Circle; + + fn bend_circle(&self, bend: BendIndex, width: f64, guide_conditions: &Conditions) -> Circle; + + fn dot_circle(&self, dot: DotIndex, width: f64, guide_conditions: &Conditions) -> Circle; + + fn rear(&self, head: CaneHead) -> DotIndex; + + fn conditions(&self, node: PrimitiveIndex) -> Conditions; +} + +impl GuidePrivate for Drawing { fn head_circle(&self, head: &Head, width: f64) -> Circle { match *head { Head::Bare(head) => Circle { - pos: head.face().primitive(self.drawing).shape().center(), // TODO. + pos: head.face().primitive(self).shape().center(), // TODO. r: 0.0, }, Head::Cane(head) => { - if let Some(inner) = self.drawing.primitive(head.cane.bend).inner() { + if let Some(inner) = self.primitive(head.cane.bend).inner() { self.bend_circle(inner.into(), width, &self.conditions(head.face().into())) } else { self.dot_circle( - self.drawing.primitive(head.cane.bend).core().into(), + self.primitive(head.cane.bend).core().into(), width, &self.conditions(head.face().into()), ) @@ -156,7 +229,7 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { } fn bend_circle(&self, bend: BendIndex, width: f64, guide_conditions: &Conditions) -> Circle { - let outer_circle = match bend.primitive(self.drawing).shape() { + let outer_circle = match bend.primitive(self).shape() { PrimitiveShape::Bend(shape) => shape.outer_circle(), _ => unreachable!(), }; @@ -166,50 +239,29 @@ impl<'a, CW: Copy, R: AccessRules> Guide<'a, CW, R> { r: outer_circle.r + width / 2.0 + self - .drawing .rules() .clearance(&self.conditions(bend.into()), guide_conditions), } } fn dot_circle(&self, dot: DotIndex, width: f64, guide_conditions: &Conditions) -> Circle { - let shape = dot.primitive(self.drawing).shape(); + let shape = dot.primitive(self).shape(); Circle { pos: shape.center(), r: shape.width() / 2.0 + width / 2.0 + self - .drawing .rules() .clearance(&self.conditions(dot.into()), guide_conditions), } } - pub fn cane_head(&self, face: LooseDotIndex) -> CaneHead { - CaneHead { - face, - cane: self.drawing.cane(face), - } - } - - pub fn rear_head(&self, face: LooseDotIndex) -> Head { - self.head(self.rear(self.cane_head(face))) - } - - pub fn head(&self, face: DotIndex) -> Head { - match face { - DotIndex::Fixed(dot) => BareHead { face: dot }.into(), - DotIndex::Loose(dot) => self.cane_head(dot).into(), - } - } - fn rear(&self, head: CaneHead) -> DotIndex { - self.drawing - .primitive(head.cane.seg) + self.primitive(head.cane.seg) .other_joint(head.cane.dot.into()) } fn conditions(&self, node: PrimitiveIndex) -> Conditions { - node.primitive(self.drawing).conditions() + node.primitive(self).conditions() } } diff --git a/src/router/draw.rs b/src/router/draw.rs index 53431fc..7ec6a70 100644 --- a/src/router/draw.rs +++ b/src/router/draw.rs @@ -22,7 +22,7 @@ use crate::{ seg::{LoneLooseSegWeight, SeqLooseSegWeight}, DrawingException, Infringement, }, - layout::{CompoundWeight, Layout, LayoutEdit}, + layout::{Layout, LayoutEdit}, math::{Circle, NoTangents}, }; @@ -71,7 +71,7 @@ pub trait Draw { impl Draw for Layout { fn start(&mut self, from: LooseDotIndex) -> Head { - self.guide().cane_head(from).into() + self.drawing().cane_head(from).into() } #[debug_ensures(ret.is_ok() -> self.drawing().node_count() == old(self.drawing().node_count() + 1))] @@ -84,7 +84,7 @@ impl Draw for Layout { width: f64, ) -> Result { let tangent = self - .guide() + .drawing() .head_into_dot_segment(&head, into, width) .map_err(Into::::into)?; let head = self @@ -134,10 +134,10 @@ impl Draw for Layout { width: f64, ) -> Result { let tangent = self - .guide() + .drawing() .head_around_dot_segment(&head, around.into(), cw, width)?; let offset = self - .guide() + .drawing() .head_around_dot_offset(&head, around.into(), width); self.cane_around( recorder, @@ -163,9 +163,9 @@ impl Draw for Layout { width: f64, ) -> Result { let tangent = self - .guide() + .drawing() .head_around_bend_segment(&head, around, cw, width)?; - let offset = self.guide().head_around_bend_offset(&head, around, width); + let offset = self.drawing().head_around_bend_offset(&head, around, width); self.cane_around( recorder, @@ -189,7 +189,7 @@ impl Draw for Layout { .other_joint(head.cane.dot.into()); self.remove_cane(recorder, &head.cane, head.face); - Some(self.guide().head(prev_dot)) + Some(self.drawing().head(prev_dot)) } } @@ -225,8 +225,6 @@ trait DrawPrivate { width: f64, offset: f64, ) -> Result; - - fn guide(&self) -> Guide; } impl DrawPrivate for Layout { @@ -308,8 +306,4 @@ impl DrawPrivate for Layout { cane, }) } - - fn guide(&self) -> Guide { - Guide::new(self.drawing()) - } } diff --git a/src/router/router.rs b/src/router/router.rs index 1d09df0..b9c5784 100644 --- a/src/router/router.rs +++ b/src/router/router.rs @@ -12,6 +12,7 @@ use crate::{ band::BandTermsegIndex, dot::{DotIndex, FixedDotIndex}, graph::{MakePrimitive, PrimitiveIndex}, + guide::Guide, head::GetFace, primitive::MakePrimitiveShape, rules::AccessRules, @@ -74,7 +75,6 @@ impl<'a, R: AccessRules> RouterAstarStrategy<'a, R> { DotIndex::Loose(face) => self .layout .drawing() - .guide() .rear_head(face) .ref_(self.layout.drawing()) .length(),