From 13c8237da6694a6f4b533497d16adfd063d58d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Cicho=C5=84?= Date: Sun, 16 Jun 2024 22:30:10 +0200 Subject: [PATCH] specctra: remove casts, accept floating point precision used internally --- src/specctra/design.rs | 98 +++++++++++++++++++-------------------- src/specctra/read.rs | 12 +++++ src/specctra/structure.rs | 43 ++++++++--------- src/specctra/write.rs | 6 +++ 4 files changed, 86 insertions(+), 73 deletions(-) diff --git a/src/specctra/design.rs b/src/specctra/design.rs index 79391bc..1abc254 100644 --- a/src/specctra/design.rs +++ b/src/specctra/design.rs @@ -184,12 +184,12 @@ impl SpecctraDesign { ); Self::add_circle( &mut board, - (place.x as f64, place.y as f64).into(), - place.rotation as f64, - (pin.x as f64, pin.y as f64).into(), - pin.rotate.unwrap_or(0.0) as f64, - circle.diameter as f64 / 2.0, - layer as usize, + (place.x, place.y).into(), + place.rotation, + (pin.x, pin.y).into(), + pin.rotate.unwrap_or(0.0), + circle.diameter / 2.0, + layer, *net, Some(pinname.clone()), ) @@ -203,15 +203,15 @@ impl SpecctraDesign { ); Self::add_rect( &mut board, - (place.x as f64, place.y as f64).into(), - place.rotation as f64, - (pin.x as f64, pin.y as f64).into(), - pin.rotate.unwrap_or(0.0) as f64, - rect.x1 as f64, - rect.y1 as f64, - rect.x2 as f64, - rect.y2 as f64, - layer as usize, + (place.x, place.y).into(), + place.rotation, + (pin.x, pin.y).into(), + pin.rotate.unwrap_or(0.0), + rect.x1, + rect.y1, + rect.x2, + rect.y2, + layer, *net, Some(pinname.clone()), ) @@ -225,13 +225,13 @@ impl SpecctraDesign { ); Self::add_path( &mut board, - (place.x as f64, place.y as f64).into(), - place.rotation as f64, - (pin.x as f64, pin.y as f64).into(), - pin.rotate.unwrap_or(0.0) as f64, + (place.x, place.y).into(), + place.rotation, + (pin.x, pin.y).into(), + pin.rotate.unwrap_or(0.0), &path.coords, - path.width as f64, - layer as usize, + path.width, + layer, *net, Some(pinname.clone()), ) @@ -245,13 +245,13 @@ impl SpecctraDesign { ); Self::add_polygon( &mut board, - (place.x as f64, place.y as f64).into(), - place.rotation as f64, - (pin.x as f64, pin.y as f64).into(), - pin.rotate.unwrap_or(0.0) as f64, + (place.x, place.y).into(), + place.rotation, + (pin.x, pin.y).into(), + pin.rotate.unwrap_or(0.0), &polygon.coords, - polygon.width as f64, - layer as usize, + polygon.width, + layer, *net, Some(pinname.clone()), ) @@ -294,8 +294,8 @@ impl SpecctraDesign { 0.0, (0.0, 0.0).into(), 0.0, - circle.diameter as f64 / 2.0, - layer as usize, + circle.diameter / 2.0, + layer, net, None, ) @@ -309,11 +309,11 @@ impl SpecctraDesign { 0.0, (0.0, 0.0).into(), 0.0, - rect.x1 as f64, - rect.y1 as f64, - rect.x2 as f64, - rect.y2 as f64, - layer as usize, + rect.x1, + rect.y1, + rect.x2, + rect.y2, + layer, net, None, ) @@ -328,8 +328,8 @@ impl SpecctraDesign { (0.0, 0.0).into(), 0.0, &path.coords, - path.width as f64, - layer as usize, + path.width, + layer, net, None, ) @@ -348,8 +348,8 @@ impl SpecctraDesign { (0.0, 0.0).into(), 0.0, &polygon.coords, - polygon.width as f64, - layer as usize, + polygon.width, + layer, net, None, ) @@ -379,7 +379,7 @@ impl SpecctraDesign { (0.0, 0.0).into(), 0.0, &wire.path.coords, - wire.path.width as f64, + wire.path.width, layer, net, None, @@ -423,9 +423,9 @@ impl SpecctraDesign { .unwrap(); if front { - image_layer as usize + image_layer } else { - layers.len() - image_layer as usize - 1 + layers.len() - image_layer - 1 } } @@ -584,8 +584,8 @@ impl SpecctraDesign { place_rot, pin_pos, pin_rot, - coords[0].x as f64, - coords[0].y as f64, + coords[0].x, + coords[0].y, ); let mut prev_index = board.add_fixed_dot_infringably( FixedDotWeight { @@ -606,8 +606,8 @@ impl SpecctraDesign { place_rot, pin_pos, pin_rot, - coord.x as f64, - coord.y as f64, + coord.x, + coord.y, ); if pos == prev_pos { @@ -673,8 +673,8 @@ impl SpecctraDesign { place_rot, pin_pos, pin_rot, - coords[0].x as f64, - coords[0].y as f64, + coords[0].x, + coords[0].y, ), r: width / 2.0, }, @@ -696,8 +696,8 @@ impl SpecctraDesign { place_rot, pin_pos, pin_rot, - coord.x as f64, - coord.y as f64, + coord.x, + coord.y, ) .into(), r: width / 2.0, diff --git a/src/specctra/read.rs b/src/specctra/read.rs index 408e55c..6e18217 100644 --- a/src/specctra/read.rs +++ b/src/specctra/read.rs @@ -80,6 +80,7 @@ impl ReadDsn for u32 { .map_err(|_| ParseError::Expected("u32"))?) } } + impl ReadDsn for usize { fn read_dsn(tokenizer: &mut ListTokenizer) -> Result { Ok(tokenizer @@ -89,6 +90,7 @@ impl ReadDsn for usize { .map_err(|_| ParseError::Expected("usize"))?) } } + impl ReadDsn for f32 { fn read_dsn(tokenizer: &mut ListTokenizer) -> Result { Ok(tokenizer @@ -99,6 +101,16 @@ impl ReadDsn for f32 { } } +impl ReadDsn for f64 { + fn read_dsn(tokenizer: &mut ListTokenizer) -> Result { + Ok(tokenizer + .consume_token()? + .expect_leaf()? + .parse() + .map_err(|_| ParseError::Expected("f64"))?) + } +} + pub struct ListTokenizer { reader: R, peeked_char: Option, diff --git a/src/specctra/structure.rs b/src/specctra/structure.rs index 753057f..29dbeef 100644 --- a/src/specctra/structure.rs +++ b/src/specctra/structure.rs @@ -141,13 +141,13 @@ pub struct Place { #[anon] pub name: String, #[anon] - pub x: f32, + pub x: f64, #[anon] - pub y: f32, + pub y: f64, #[anon] pub side: String, #[anon] - pub rotation: f32, + pub rotation: f64, pub PN: Option, } @@ -180,18 +180,13 @@ pub struct Outline { pub struct Pin { #[anon] pub name: String, - pub rotate: Option, + pub rotate: Option, #[anon] pub id: String, #[anon] - pub x: f32, + pub x: f64, #[anon] - pub y: f32, -} - -#[derive(ReadDsn, WriteSes, Debug)] -pub struct Rotate { - pub angle: f32, + pub y: f64, } #[derive(ReadDsn, WriteSes, Debug)] @@ -251,7 +246,7 @@ pub struct Circle { #[anon] pub layer: String, #[anon] - pub diameter: u32, + pub diameter: f64, #[anon] pub offset: Option, } @@ -318,8 +313,8 @@ pub struct Wire { // (and enforce that such an array actually contains a whole number of points) #[derive(Debug)] pub struct Point { - pub x: f32, - pub y: f32, + pub x: f64, + pub y: f64, } // Custom impl for the case described above @@ -329,8 +324,8 @@ impl ReadDsn for Vec { loop { let token = tokenizer.consume_token()?; if let ListToken::Leaf { value: ref x } = token { - let x = x.parse::().unwrap(); - let y = tokenizer.read_value::()?; + let x = x.parse::().unwrap(); + let y = tokenizer.read_value::()?; array.push(Point { x, y }); } else { tokenizer.return_token(token); @@ -345,8 +340,8 @@ impl ReadDsn for Option { fn read_dsn(tokenizer: &mut ListTokenizer) -> Result { let token = tokenizer.consume_token()?; if let ListToken::Leaf { value: ref x } = token { - let x = x.parse::().unwrap(); - let y = tokenizer.read_value::()?; + let x = x.parse::().unwrap(); + let y = tokenizer.read_value::()?; Ok(Some(Point { x, y })) } else { tokenizer.return_token(token); @@ -380,7 +375,7 @@ pub struct Polygon { #[anon] pub layer: String, #[anon] - pub width: f32, + pub width: f64, #[anon] pub coords: Vec, } @@ -390,7 +385,7 @@ pub struct Path { #[anon] pub layer: String, #[anon] - pub width: f32, + pub width: f64, #[anon] pub coords: Vec, } @@ -400,13 +395,13 @@ pub struct Rect { #[anon] pub layer: String, #[anon] - pub x1: f32, + pub x1: f64, #[anon] - pub y1: f32, + pub y1: f64, #[anon] - pub x2: f32, + pub x2: f64, #[anon] - pub y2: f32, + pub y2: f64, } #[derive(ReadDsn, WriteSes, Debug)] diff --git a/src/specctra/write.rs b/src/specctra/write.rs index 538f627..fb5155e 100644 --- a/src/specctra/write.rs +++ b/src/specctra/write.rs @@ -61,6 +61,12 @@ impl WriteSes for f32 { } } +impl WriteSes for f64 { + fn write_dsn(&self, writer: &mut ListWriter) -> Result<(), io::Error> { + writer.write_leaf(self.to_string()) + } +} + pub struct ListWriter { writable: W, indent_level: usize,