diff --git a/examples/wrapped_torus.rs b/examples/wrapped_torus.rs index 99e147e..a65571f 100644 --- a/examples/wrapped_torus.rs +++ b/examples/wrapped_torus.rs @@ -18,8 +18,8 @@ fn main() { let current_break = 0.5e-9; // time between 'set' pulse and 'clear' pulse let conductivity = 1.0e9; - let from_m = |m| (m/feat_size) as u32; - let m_to_um = |px| (px * 1e6) as u32; + let from_m = |m: Flt| (m/feat_size).round() as u32; + let m_to_um = |m: Flt| (m * 1e6).round() as u32; let half_width = width * 0.5; let half_depth = depth * 0.5; @@ -76,8 +76,8 @@ fn main() { std::mem::size_of::() * 8, *size_px, m_to_um(feat_size), - (peak_current * 1e3) as i64, - (current_duration * 1e12) as i64, + (peak_current * 1e3).round() as i64, + (current_duration * 1e12).round() as i64, m_to_um(ferro_major), m_to_um(ferro_minor), m_to_um(wire_major), diff --git a/src/geom/units.rs b/src/geom/units.rs index 90489e5..f554993 100644 --- a/src/geom/units.rs +++ b/src/geom/units.rs @@ -18,7 +18,7 @@ impl Coord for Meters { *self } fn to_index(&self, feature_size: Flt) -> Index { - Index((self.0 / feature_size).into()) + Index((self.0 / feature_size).round().into()) } fn from_meters(other: Meters, _feature_size: Flt) -> Self { other diff --git a/src/geom/vec.rs b/src/geom/vec.rs index 9a3a8d7..cf3348c 100644 --- a/src/geom/vec.rs +++ b/src/geom/vec.rs @@ -208,6 +208,9 @@ impl Vec3 { self.with_mag(1.0) } } + pub fn round(&self) -> Vec3 { + Self::new(self.x().round(), self.y().round(), self.z().round()) + } } impl Into<(Flt, Flt, Flt)> for Vec3 { diff --git a/src/meas.rs b/src/meas.rs index eb92a54..e1186fd 100644 --- a/src/meas.rs +++ b/src/meas.rs @@ -300,7 +300,7 @@ impl AbstractMeasurement for Magnetization { fn loc(v: Meters) -> String { let (x, y, z): (Flt, Flt, Flt) = (*v * 1e6).into(); - format!("({}, {}, {}) um", x as i32, y as i32, z as i32) + format!("({}, {}, {}) um", x.round() as i32, y.round() as i32, z.round() as i32) } /// M diff --git a/src/render.rs b/src/render.rs index f23e9ac..65e7a5f 100644 --- a/src/render.rs +++ b/src/render.rs @@ -64,7 +64,7 @@ fn im_size(state: &dyn GenericSim, max_w: u32, max_h: u32) -> (u32, u32) { let mut height = width * state.height() / state.width(); if height > max_h { let stretch = max_h as f32 / height as f32; - width = (width as f32 * stretch) as _; + width = (width as f32 * stretch).round() as _; height = max_h; } (width, height) @@ -249,8 +249,8 @@ impl ImageRenderExt for RgbImage { fn draw_field_arrow(&mut self, center: Vec2, rel: Vec2, color: Rgb, alpha: f32) { let start = (center - rel * 0.5).round(); let end = (center + rel * 0.5).round(); - let i_start = (start.x() as _, start.y() as _); - let i_end = (end.x() as _, end.y() as _); + let i_start = (start.x().round() as _, start.y().round() as _); + let i_end = (end.x().round() as _, end.y().round() as _); let interpolate_with_alpha = |left, right, left_weight| { pixelops::interpolate(left, right, left_weight*alpha) };