diff --git a/src/math.rs b/src/math.rs index cd25e16..1aea605 100644 --- a/src/math.rs +++ b/src/math.rs @@ -20,14 +20,29 @@ pub struct Circle { pub r: f64, } +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct PointWithRotation { + pub pos: Point, + pub rot: f64, +} + impl Sub for Circle { type Output = Self; fn sub(self, other: Self) -> Self { - return Self { + Self { pos: self.pos - other.pos, r: self.r, - }; + } + } +} + +impl Default for PointWithRotation { + fn default() -> Self { + Self { + pos: (0.0, 0.0).into(), + rot: 0.0, + } } } diff --git a/src/specctra/design.rs b/src/specctra/design.rs index e7fbec8..63ee2a8 100644 --- a/src/specctra/design.rs +++ b/src/specctra/design.rs @@ -14,7 +14,7 @@ use crate::{ }, geometry::{primitive::PrimitiveShape, GetWidth}, layout::{poly::SolidPolyWeight, Layout}, - math::Circle, + math::{Circle, PointWithRotation}, specctra::{ mesadata::SpecctraMesadata, read::{self, ListTokenizer}, @@ -253,10 +253,8 @@ impl SpecctraDesign { ); Self::add_circle( &mut board, - (place.x, place.y).into(), - place.rotation, - (pin.x, pin.y).into(), - pin.rotate.unwrap_or(0.0), + place.point_with_rotation(), + pin.point_with_rotation(), circle.diameter / 2.0, layer, *net, @@ -272,10 +270,8 @@ impl SpecctraDesign { ); Self::add_rect( &mut board, - (place.x, place.y).into(), - place.rotation, - (pin.x, pin.y).into(), - pin.rotate.unwrap_or(0.0), + place.point_with_rotation(), + pin.point_with_rotation(), rect.x1, rect.y1, rect.x2, @@ -294,10 +290,8 @@ impl SpecctraDesign { ); Self::add_path( &mut board, - (place.x, place.y).into(), - place.rotation, - (pin.x, pin.y).into(), - pin.rotate.unwrap_or(0.0), + place.point_with_rotation(), + pin.point_with_rotation(), &path.coords, path.width, layer, @@ -314,10 +308,8 @@ impl SpecctraDesign { ); Self::add_polygon( &mut board, - (place.x, place.y).into(), - place.rotation, - (pin.x, pin.y).into(), - pin.rotate.unwrap_or(0.0), + place.point_with_rotation(), + pin.point_with_rotation(), &polygon.coords, polygon.width, layer, @@ -359,10 +351,8 @@ impl SpecctraDesign { ); Self::add_circle( &mut board, - (0.0, 0.0).into(), - 0.0, - (0.0, 0.0).into(), - 0.0, + PointWithRotation::default(), + PointWithRotation::default(), circle.diameter / 2.0, layer, net, @@ -374,10 +364,8 @@ impl SpecctraDesign { Self::layer(&mut board, &self.pcb.structure.layers, &rect.layer, true); Self::add_rect( &mut board, - (0.0, 0.0).into(), - 0.0, - (0.0, 0.0).into(), - 0.0, + PointWithRotation::default(), + PointWithRotation::default(), rect.x1, rect.y1, rect.x2, @@ -392,10 +380,8 @@ impl SpecctraDesign { Self::layer(&mut board, &self.pcb.structure.layers, &path.layer, true); Self::add_path( &mut board, - (0.0, 0.0).into(), - 0.0, - (0.0, 0.0).into(), - 0.0, + PointWithRotation::default(), + PointWithRotation::default(), &path.coords, path.width, layer, @@ -412,10 +398,8 @@ impl SpecctraDesign { ); Self::add_polygon( &mut board, - (0.0, 0.0).into(), - 0.0, - (0.0, 0.0).into(), - 0.0, + PointWithRotation::default(), + PointWithRotation::default(), &polygon.coords, polygon.width, layer, @@ -443,10 +427,8 @@ impl SpecctraDesign { Self::add_path( &mut board, - (0.0, 0.0).into(), - 0.0, - (0.0, 0.0).into(), - 0.0, + PointWithRotation::default(), + PointWithRotation::default(), &wire.path.coords, wire.path.width, layer, @@ -500,17 +482,15 @@ impl SpecctraDesign { fn add_circle( board: &mut Board, - place_pos: Point, - place_rot: f64, - pin_pos: Point, - pin_rot: f64, + place: PointWithRotation, + pin: PointWithRotation, r: f64, layer: usize, net: usize, maybe_pin: Option, ) { let circle = Circle { - pos: Self::pos(place_pos, place_rot, pin_pos, pin_rot, 0.0, 0.0), + pos: Self::pos(place, pin, 0.0, 0.0), r, }; @@ -526,10 +506,8 @@ impl SpecctraDesign { fn add_rect( board: &mut Board, - place_pos: Point, - place_rot: f64, - pin_pos: Point, - pin_rot: f64, + place: PointWithRotation, + pin: PointWithRotation, x1: f64, y1: f64, x2: f64, @@ -551,7 +529,7 @@ impl SpecctraDesign { let dot_1_1 = board.add_poly_fixed_dot_infringably( FixedDotWeight { circle: Circle { - pos: Self::pos(place_pos, place_rot, pin_pos, pin_rot, x1, y1), + pos: Self::pos(place, pin, x1, y1), r: 0.5, }, layer, @@ -562,7 +540,7 @@ impl SpecctraDesign { let dot_2_1 = board.add_poly_fixed_dot_infringably( FixedDotWeight { circle: Circle { - pos: Self::pos(place_pos, place_rot, pin_pos, pin_rot, x2, y1), + pos: Self::pos(place, pin, x2, y1), r: 0.5, }, layer, @@ -573,7 +551,7 @@ impl SpecctraDesign { let dot_2_2 = board.add_poly_fixed_dot_infringably( FixedDotWeight { circle: Circle { - pos: Self::pos(place_pos, place_rot, pin_pos, pin_rot, x2, y2), + pos: Self::pos(place, pin, x2, y2), r: 0.5, }, layer, @@ -584,7 +562,7 @@ impl SpecctraDesign { let dot_1_2 = board.add_poly_fixed_dot_infringably( FixedDotWeight { circle: Circle { - pos: Self::pos(place_pos, place_rot, pin_pos, pin_rot, x1, y2), + pos: Self::pos(place, pin, x1, y2), r: 0.5, }, layer, @@ -637,10 +615,8 @@ impl SpecctraDesign { fn add_path( board: &mut Board, - place_pos: Point, - place_rot: f64, - pin_pos: Point, - pin_rot: f64, + place: PointWithRotation, + pin: PointWithRotation, coords: &Vec, width: f64, layer: usize, @@ -648,14 +624,7 @@ impl SpecctraDesign { maybe_pin: Option, ) { // add the first coordinate in the wire path as a dot and save its index - let mut prev_pos = Self::pos( - place_pos, - place_rot, - pin_pos, - pin_rot, - coords[0].x, - coords[0].y, - ); + let mut prev_pos = Self::pos(place, pin, coords[0].x, coords[0].y); let mut prev_index = board.add_fixed_dot_infringably( FixedDotWeight { circle: Circle { @@ -670,7 +639,7 @@ impl SpecctraDesign { // iterate through path coords starting from the second for coord in coords.iter().skip(1) { - let pos = Self::pos(place_pos, place_rot, pin_pos, pin_rot, coord.x, coord.y); + let pos = Self::pos(place, pin, coord.x, coord.y); if pos == prev_pos { continue; @@ -707,10 +676,8 @@ impl SpecctraDesign { fn add_polygon( board: &mut Board, - place_pos: Point, - place_rot: f64, - pin_pos: Point, - pin_rot: f64, + place: PointWithRotation, + pin: PointWithRotation, coords: &Vec, width: f64, layer: usize, @@ -730,14 +697,7 @@ impl SpecctraDesign { let mut prev_index = board.add_poly_fixed_dot_infringably( FixedDotWeight { circle: Circle { - pos: Self::pos( - place_pos, - place_rot, - pin_pos, - pin_rot, - coords[0].x, - coords[0].y, - ), + pos: Self::pos(place, pin, coords[0].x, coords[0].y), r: width / 2.0, }, layer, @@ -753,8 +713,7 @@ impl SpecctraDesign { let index = board.add_poly_fixed_dot_infringably( FixedDotWeight { circle: Circle { - pos: Self::pos(place_pos, place_rot, pin_pos, pin_rot, coord.x, coord.y) - .into(), + pos: Self::pos(place, pin, coord.x, coord.y).into(), r: width / 2.0, }, layer, @@ -782,14 +741,12 @@ impl SpecctraDesign { } fn pos( - place_pos: Point, - place_rot: f64, - pin_pos: Point, - pin_rot: f64, + place: PointWithRotation, + pin: PointWithRotation, x: f64, y: f64, ) -> Point { - let pos = (point! {x: x, y: y} + pin_pos).rotate_around_point(pin_rot, pin_pos); - (pos + place_pos).rotate_around_point(place_rot, place_pos) + let pos = (point! {x: x, y: y} + pin.pos).rotate_around_point(pin.rot, pin.pos); + (pos + place.pos).rotate_around_point(place.rot, place.pos) } } diff --git a/src/specctra/structure.rs b/src/specctra/structure.rs index d0a6c36..897ab2b 100644 --- a/src/specctra/structure.rs +++ b/src/specctra/structure.rs @@ -3,6 +3,7 @@ use super::read::ReadDsn; use super::read::{ListTokenizer, ParseError}; use super::write::ListWriter; use super::write::WriteSes; +use crate::math::PointWithRotation; use specctra_derive::ReadDsn; use specctra_derive::WriteSes; @@ -151,6 +152,15 @@ pub struct Place { pub PN: Option, } +impl Place { + pub fn point_with_rotation(&self) -> PointWithRotation { + PointWithRotation { + pos: (self.x, self.y).into(), + rot: self.rotation, + } + } +} + #[derive(ReadDsn, WriteSes, Debug)] pub struct Library { #[vec("image")] @@ -189,6 +199,15 @@ pub struct Pin { pub y: f64, } +impl Pin { + pub fn point_with_rotation(&self) -> PointWithRotation { + PointWithRotation { + pos: (self.x, self.y).into(), + rot: self.rotate.unwrap_or(0.0), + } + } +} + #[derive(ReadDsn, WriteSes, Debug)] pub struct Keepout { #[anon]