specctra: remove casts, accept floating point precision used internally

This commit is contained in:
Tomasz Cichoń 2024-06-16 22:30:10 +02:00
parent f37e7ce88d
commit 13c8237da6
4 changed files with 86 additions and 73 deletions

View File

@ -184,12 +184,12 @@ impl SpecctraDesign {
); );
Self::add_circle( Self::add_circle(
&mut board, &mut board,
(place.x as f64, place.y as f64).into(), (place.x, place.y).into(),
place.rotation as f64, place.rotation,
(pin.x as f64, pin.y as f64).into(), (pin.x, pin.y).into(),
pin.rotate.unwrap_or(0.0) as f64, pin.rotate.unwrap_or(0.0),
circle.diameter as f64 / 2.0, circle.diameter / 2.0,
layer as usize, layer,
*net, *net,
Some(pinname.clone()), Some(pinname.clone()),
) )
@ -203,15 +203,15 @@ impl SpecctraDesign {
); );
Self::add_rect( Self::add_rect(
&mut board, &mut board,
(place.x as f64, place.y as f64).into(), (place.x, place.y).into(),
place.rotation as f64, place.rotation,
(pin.x as f64, pin.y as f64).into(), (pin.x, pin.y).into(),
pin.rotate.unwrap_or(0.0) as f64, pin.rotate.unwrap_or(0.0),
rect.x1 as f64, rect.x1,
rect.y1 as f64, rect.y1,
rect.x2 as f64, rect.x2,
rect.y2 as f64, rect.y2,
layer as usize, layer,
*net, *net,
Some(pinname.clone()), Some(pinname.clone()),
) )
@ -225,13 +225,13 @@ impl SpecctraDesign {
); );
Self::add_path( Self::add_path(
&mut board, &mut board,
(place.x as f64, place.y as f64).into(), (place.x, place.y).into(),
place.rotation as f64, place.rotation,
(pin.x as f64, pin.y as f64).into(), (pin.x, pin.y).into(),
pin.rotate.unwrap_or(0.0) as f64, pin.rotate.unwrap_or(0.0),
&path.coords, &path.coords,
path.width as f64, path.width,
layer as usize, layer,
*net, *net,
Some(pinname.clone()), Some(pinname.clone()),
) )
@ -245,13 +245,13 @@ impl SpecctraDesign {
); );
Self::add_polygon( Self::add_polygon(
&mut board, &mut board,
(place.x as f64, place.y as f64).into(), (place.x, place.y).into(),
place.rotation as f64, place.rotation,
(pin.x as f64, pin.y as f64).into(), (pin.x, pin.y).into(),
pin.rotate.unwrap_or(0.0) as f64, pin.rotate.unwrap_or(0.0),
&polygon.coords, &polygon.coords,
polygon.width as f64, polygon.width,
layer as usize, layer,
*net, *net,
Some(pinname.clone()), Some(pinname.clone()),
) )
@ -294,8 +294,8 @@ impl SpecctraDesign {
0.0, 0.0,
(0.0, 0.0).into(), (0.0, 0.0).into(),
0.0, 0.0,
circle.diameter as f64 / 2.0, circle.diameter / 2.0,
layer as usize, layer,
net, net,
None, None,
) )
@ -309,11 +309,11 @@ impl SpecctraDesign {
0.0, 0.0,
(0.0, 0.0).into(), (0.0, 0.0).into(),
0.0, 0.0,
rect.x1 as f64, rect.x1,
rect.y1 as f64, rect.y1,
rect.x2 as f64, rect.x2,
rect.y2 as f64, rect.y2,
layer as usize, layer,
net, net,
None, None,
) )
@ -328,8 +328,8 @@ impl SpecctraDesign {
(0.0, 0.0).into(), (0.0, 0.0).into(),
0.0, 0.0,
&path.coords, &path.coords,
path.width as f64, path.width,
layer as usize, layer,
net, net,
None, None,
) )
@ -348,8 +348,8 @@ impl SpecctraDesign {
(0.0, 0.0).into(), (0.0, 0.0).into(),
0.0, 0.0,
&polygon.coords, &polygon.coords,
polygon.width as f64, polygon.width,
layer as usize, layer,
net, net,
None, None,
) )
@ -379,7 +379,7 @@ impl SpecctraDesign {
(0.0, 0.0).into(), (0.0, 0.0).into(),
0.0, 0.0,
&wire.path.coords, &wire.path.coords,
wire.path.width as f64, wire.path.width,
layer, layer,
net, net,
None, None,
@ -423,9 +423,9 @@ impl SpecctraDesign {
.unwrap(); .unwrap();
if front { if front {
image_layer as usize image_layer
} else { } else {
layers.len() - image_layer as usize - 1 layers.len() - image_layer - 1
} }
} }
@ -584,8 +584,8 @@ impl SpecctraDesign {
place_rot, place_rot,
pin_pos, pin_pos,
pin_rot, pin_rot,
coords[0].x as f64, coords[0].x,
coords[0].y as f64, coords[0].y,
); );
let mut prev_index = board.add_fixed_dot_infringably( let mut prev_index = board.add_fixed_dot_infringably(
FixedDotWeight { FixedDotWeight {
@ -606,8 +606,8 @@ impl SpecctraDesign {
place_rot, place_rot,
pin_pos, pin_pos,
pin_rot, pin_rot,
coord.x as f64, coord.x,
coord.y as f64, coord.y,
); );
if pos == prev_pos { if pos == prev_pos {
@ -673,8 +673,8 @@ impl SpecctraDesign {
place_rot, place_rot,
pin_pos, pin_pos,
pin_rot, pin_rot,
coords[0].x as f64, coords[0].x,
coords[0].y as f64, coords[0].y,
), ),
r: width / 2.0, r: width / 2.0,
}, },
@ -696,8 +696,8 @@ impl SpecctraDesign {
place_rot, place_rot,
pin_pos, pin_pos,
pin_rot, pin_rot,
coord.x as f64, coord.x,
coord.y as f64, coord.y,
) )
.into(), .into(),
r: width / 2.0, r: width / 2.0,

View File

@ -80,6 +80,7 @@ impl<R: std::io::BufRead> ReadDsn<R> for u32 {
.map_err(|_| ParseError::Expected("u32"))?) .map_err(|_| ParseError::Expected("u32"))?)
} }
} }
impl<R: std::io::BufRead> ReadDsn<R> for usize { impl<R: std::io::BufRead> ReadDsn<R> for usize {
fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> { fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> {
Ok(tokenizer Ok(tokenizer
@ -89,6 +90,7 @@ impl<R: std::io::BufRead> ReadDsn<R> for usize {
.map_err(|_| ParseError::Expected("usize"))?) .map_err(|_| ParseError::Expected("usize"))?)
} }
} }
impl<R: std::io::BufRead> ReadDsn<R> for f32 { impl<R: std::io::BufRead> ReadDsn<R> for f32 {
fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> { fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> {
Ok(tokenizer Ok(tokenizer
@ -99,6 +101,16 @@ impl<R: std::io::BufRead> ReadDsn<R> for f32 {
} }
} }
impl<R: std::io::BufRead> ReadDsn<R> for f64 {
fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> {
Ok(tokenizer
.consume_token()?
.expect_leaf()?
.parse()
.map_err(|_| ParseError::Expected("f64"))?)
}
}
pub struct ListTokenizer<R: std::io::BufRead> { pub struct ListTokenizer<R: std::io::BufRead> {
reader: R, reader: R,
peeked_char: Option<char>, peeked_char: Option<char>,

View File

@ -141,13 +141,13 @@ pub struct Place {
#[anon] #[anon]
pub name: String, pub name: String,
#[anon] #[anon]
pub x: f32, pub x: f64,
#[anon] #[anon]
pub y: f32, pub y: f64,
#[anon] #[anon]
pub side: String, pub side: String,
#[anon] #[anon]
pub rotation: f32, pub rotation: f64,
pub PN: Option<String>, pub PN: Option<String>,
} }
@ -180,18 +180,13 @@ pub struct Outline {
pub struct Pin { pub struct Pin {
#[anon] #[anon]
pub name: String, pub name: String,
pub rotate: Option<f32>, pub rotate: Option<f64>,
#[anon] #[anon]
pub id: String, pub id: String,
#[anon] #[anon]
pub x: f32, pub x: f64,
#[anon] #[anon]
pub y: f32, pub y: f64,
}
#[derive(ReadDsn, WriteSes, Debug)]
pub struct Rotate {
pub angle: f32,
} }
#[derive(ReadDsn, WriteSes, Debug)] #[derive(ReadDsn, WriteSes, Debug)]
@ -251,7 +246,7 @@ pub struct Circle {
#[anon] #[anon]
pub layer: String, pub layer: String,
#[anon] #[anon]
pub diameter: u32, pub diameter: f64,
#[anon] #[anon]
pub offset: Option<Point>, pub offset: Option<Point>,
} }
@ -318,8 +313,8 @@ pub struct Wire {
// (and enforce that such an array actually contains a whole number of points) // (and enforce that such an array actually contains a whole number of points)
#[derive(Debug)] #[derive(Debug)]
pub struct Point { pub struct Point {
pub x: f32, pub x: f64,
pub y: f32, pub y: f64,
} }
// Custom impl for the case described above // Custom impl for the case described above
@ -329,8 +324,8 @@ impl<R: std::io::BufRead> ReadDsn<R> for Vec<Point> {
loop { loop {
let token = tokenizer.consume_token()?; let token = tokenizer.consume_token()?;
if let ListToken::Leaf { value: ref x } = token { if let ListToken::Leaf { value: ref x } = token {
let x = x.parse::<f32>().unwrap(); let x = x.parse::<f64>().unwrap();
let y = tokenizer.read_value::<f32>()?; let y = tokenizer.read_value::<f64>()?;
array.push(Point { x, y }); array.push(Point { x, y });
} else { } else {
tokenizer.return_token(token); tokenizer.return_token(token);
@ -345,8 +340,8 @@ impl<R: std::io::BufRead> ReadDsn<R> for Option<Point> {
fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> { fn read_dsn(tokenizer: &mut ListTokenizer<R>) -> Result<Self, ParseError> {
let token = tokenizer.consume_token()?; let token = tokenizer.consume_token()?;
if let ListToken::Leaf { value: ref x } = token { if let ListToken::Leaf { value: ref x } = token {
let x = x.parse::<f32>().unwrap(); let x = x.parse::<f64>().unwrap();
let y = tokenizer.read_value::<f32>()?; let y = tokenizer.read_value::<f64>()?;
Ok(Some(Point { x, y })) Ok(Some(Point { x, y }))
} else { } else {
tokenizer.return_token(token); tokenizer.return_token(token);
@ -380,7 +375,7 @@ pub struct Polygon {
#[anon] #[anon]
pub layer: String, pub layer: String,
#[anon] #[anon]
pub width: f32, pub width: f64,
#[anon] #[anon]
pub coords: Vec<Point>, pub coords: Vec<Point>,
} }
@ -390,7 +385,7 @@ pub struct Path {
#[anon] #[anon]
pub layer: String, pub layer: String,
#[anon] #[anon]
pub width: f32, pub width: f64,
#[anon] #[anon]
pub coords: Vec<Point>, pub coords: Vec<Point>,
} }
@ -400,13 +395,13 @@ pub struct Rect {
#[anon] #[anon]
pub layer: String, pub layer: String,
#[anon] #[anon]
pub x1: f32, pub x1: f64,
#[anon] #[anon]
pub y1: f32, pub y1: f64,
#[anon] #[anon]
pub x2: f32, pub x2: f64,
#[anon] #[anon]
pub y2: f32, pub y2: f64,
} }
#[derive(ReadDsn, WriteSes, Debug)] #[derive(ReadDsn, WriteSes, Debug)]

View File

@ -61,6 +61,12 @@ impl<W: io::Write> WriteSes<W> for f32 {
} }
} }
impl<W: io::Write> WriteSes<W> for f64 {
fn write_dsn(&self, writer: &mut ListWriter<W>) -> Result<(), io::Error> {
writer.write_leaf(self.to_string())
}
}
pub struct ListWriter<W: io::Write> { pub struct ListWriter<W: io::Write> {
writable: W, writable: W,
indent_level: usize, indent_level: usize,