Add a bunch of .round()s before conversions

Previously this was leading to inconsistencies when using f32 v.s. f64
This commit is contained in:
2020-12-17 15:09:53 -08:00
parent 75fde43aa7
commit d8ae766099
5 changed files with 12 additions and 9 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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

View File

@@ -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<u8>, 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)
};