Remove usize casts

This commit is contained in:
2020-09-07 17:54:50 -07:00
parent 5d61765a84
commit fbca8622df
4 changed files with 34 additions and 35 deletions

View File

@@ -4,7 +4,7 @@ fn main() {
let width = 201; let width = 201;
let mut driver = Driver::new(width, 101, 1e-3 /* feature size */); let mut driver = Driver::new(width, 101, 1e-3 /* feature size */);
driver.add_y4m_renderer("em_reflection.y4m"); driver.add_y4m_renderer("em_reflection.y4m");
driver.add_term_renderer();; driver.add_term_renderer();
for inset in 0..20 { for inset in 0..20 {
for x in 0..width { for x in 0..width {

View File

@@ -14,7 +14,7 @@ pub struct Driver {
} }
impl Driver { impl Driver {
pub fn new(width: usize, height: usize, feature_size: f64) -> Self { pub fn new(width: u32, height: u32, feature_size: f64) -> Self {
Driver { Driver {
state: SimState::new(width, height, feature_size), state: SimState::new(width, height, feature_size),
steps_per_frame: 1, steps_per_frame: 1,
@@ -43,17 +43,17 @@ impl Driver {
let depth = thickness - inset; let depth = thickness - inset;
// TODO: tune a scalar multiplier on this value // TODO: tune a scalar multiplier on this value
let conductivity = (depth*depth) as f64; let conductivity = (depth*depth) as f64;
for x in inset as usize..self.state.width() - inset as usize { for x in inset..self.state.width() - inset {
// left // left
*self.state.get_mut(x, inset as _).mat_mut() = mat::Static::conductor(conductivity).into(); *self.state.get_mut(x, inset).mat_mut() = mat::Static::conductor(conductivity).into();
// right // right
*self.state.get_mut(x, self.state.height() - 1 - inset as usize).mat_mut() = mat::Static::conductor(conductivity).into(); *self.state.get_mut(x, self.state.height() - 1 - inset).mat_mut() = mat::Static::conductor(conductivity).into();
} }
for y in inset as usize..self.state.height() - inset as usize { for y in inset..self.state.height() - inset {
// top // top
*self.state.get_mut(inset as _, y).mat_mut() = mat::Static::conductor(conductivity).into(); *self.state.get_mut(inset, y).mat_mut() = mat::Static::conductor(conductivity).into();
// bottom // bottom
*self.state.get_mut(self.state.width() - 1 - inset as usize, y).mat_mut() = mat::Static::conductor(conductivity).into(); *self.state.get_mut(self.state.width() - 1 - inset, y).mat_mut() = mat::Static::conductor(conductivity).into();
} }
} }
} }

View File

@@ -4,7 +4,6 @@ use crate::{Material as _, SimSnapshot, SimState};
use decorum::R64; use decorum::R64;
use image::{RgbImage, Rgb}; use image::{RgbImage, Rgb};
use imageproc::{pixelops, drawing}; use imageproc::{pixelops, drawing};
use std::convert::TryInto as _;
use std::fs::File; use std::fs::File;
use std::path::PathBuf; use std::path::PathBuf;
use y4m; use y4m;
@@ -56,13 +55,13 @@ trait SimSnapshotRenderExt {
impl SimSnapshotRenderExt for SimSnapshot { impl SimSnapshotRenderExt for SimSnapshot {
fn to_image(&self) -> RgbImage { fn to_image(&self) -> RgbImage {
let w = self.width().try_into().unwrap(); let w = self.width();
let h = self.height().try_into().unwrap(); let h = self.height();
let evec_spacing = 10; let evec_spacing = 10;
let mut image = RgbImage::new(w, h); let mut image = RgbImage::new(w, h);
for y in 0..h { for y in 0..h {
for x in 0..w { for x in 0..w {
let cell = self.get(x as usize, y as usize); let cell = self.get(x, y);
let r = scale_signed_to_u8(cell.mat().mz(), 100.0); let r = scale_signed_to_u8(cell.mat().mz(), 100.0);
let b = scale_unsigned_to_u8(cell.mat().conductivity(), 10.0); let b = scale_unsigned_to_u8(cell.mat().conductivity(), 10.0);
let g = scale_signed_to_u8(cell.bz(), 1.0e-4); let g = scale_signed_to_u8(cell.bz(), 1.0e-4);
@@ -87,15 +86,15 @@ impl SimSnapshotRenderExt for SimSnapshot {
fn e_vector(&self, xidx: u32, yidx: u32, size: u32) -> Point { fn e_vector(&self, xidx: u32, yidx: u32, size: u32) -> Point {
let mut e = Point::default(); let mut e = Point::default();
let w = self.width().try_into().unwrap(); let w = self.width();
let h = self.height().try_into().unwrap(); let h = self.height();
let xstart = xidx.min(w); let xstart = xidx.min(w);
let ystart = yidx.min(h); let ystart = yidx.min(h);
let xend = (xstart + size).min(w); let xend = (xstart + size).min(w);
let yend = (ystart + size).min(h); let yend = (ystart + size).min(h);
for y in ystart..yend { for y in ystart..yend {
for x in xstart..xend { for x in xstart..xend {
e += self.get(x as usize, y as usize).e(); e += self.get(x, y).e();
} }
} }
let xw = xend - xstart; let xw = xend - xstart;
@@ -240,7 +239,7 @@ impl MultiRenderer {
pub fn render(&mut self, state: &SimState) { pub fn render(&mut self, state: &SimState) {
//let max_width = 1980; //< TODO: make configurable //let max_width = 1980; //< TODO: make configurable
let max_width = 200; let max_width = 200;
let dec = (state.width() as u32 + max_width - 1) / max_width; let dec = (state.width() + max_width - 1) / max_width;
let snap = state.snapshot(dec); let snap = state.snapshot(dec);
Renderer::render(self, &snap); Renderer::render(self, &snap);
} }

View File

@@ -16,9 +16,9 @@ pub struct SimState<M=GenericMaterial> {
} }
impl<M: Material + Default> SimState<M> { impl<M: Material + Default> SimState<M> {
pub fn new(width: usize, height: usize, feature_size: f64) -> Self { pub fn new(width: u32, height: u32, feature_size: f64) -> Self {
Self { Self {
cells: Array2::default((height, width)), cells: Array2::default((height as _, width as _)),
feature_size: feature_size.into(), feature_size: feature_size.into(),
..Default::default() ..Default::default()
} }
@@ -75,8 +75,8 @@ impl<M: Material + Clone + Default + Send + Sync> SimState<M> {
/// dec = decimation, e.g. set to 3 to sample 3 x 3 grids /// dec = decimation, e.g. set to 3 to sample 3 x 3 grids
pub fn snapshot(&self, dec: u32) -> SimSnapshot { pub fn snapshot(&self, dec: u32) -> SimSnapshot {
let rows = self.height() as u32 / dec; let rows = self.height() / dec;
let cols = self.width() as u32 / dec; let cols = self.width() / dec;
let sample_area = (dec * dec) as f64; let sample_area = (dec * dec) as f64;
let immute_self = &self; let immute_self = &self;
@@ -87,7 +87,7 @@ impl<M: Material + Clone + Default + Send + Sync> SimState<M> {
let mut cell: Cell<mat::Static> = Default::default(); let mut cell: Cell<mat::Static> = Default::default();
for y in new_y*dec..new_y*dec + dec { for y in new_y*dec..new_y*dec + dec {
for x in new_x*dec..new_x*dec + dec { for x in new_x*dec..new_x*dec + dec {
let sample = immute_self.get(x as _, y as _); let sample = immute_self.get(x, y);
cell.state.ex += sample.ex(); cell.state.ex += sample.ex();
cell.state.ey += sample.ey(); cell.state.ey += sample.ey();
cell.state.hz += sample.hz(); cell.state.hz += sample.hz();
@@ -118,8 +118,8 @@ impl<M: Material + Clone + Default + Send + Sync> SimState<M> {
} }
impl<M: Material> SimState<M> { impl<M: Material> SimState<M> {
pub fn impulse_bz(&mut self, x: usize, y: usize, bz: f64) { pub fn impulse_bz(&mut self, x: u32, y: u32, bz: f64) {
self.cells[[y, x]].impulse_bz(bz); self.get_mut(x, y).impulse_bz(bz);
} }
} }
@@ -136,24 +136,24 @@ impl<M> SimState<M> {
self.feature_size.into() self.feature_size.into()
} }
pub fn impulse_ex(&mut self, x: usize, y: usize, ex: f64) { pub fn impulse_ex(&mut self, x: u32, y: u32, ex: f64) {
self.cells[[y, x]].state.ex += ex; self.get_mut(x, y).state.ex += ex;
} }
pub fn impulse_ey(&mut self, x: usize, y: usize, ey: f64) { pub fn impulse_ey(&mut self, x: u32, y: u32, ey: f64) {
self.cells[[y, x]].state.ey += ey; self.get_mut(x, y).state.ey += ey;
} }
pub fn width(&self) -> usize { pub fn width(&self) -> u32 {
self.cells.shape()[1] self.cells.shape()[1] as _
} }
pub fn height(&self) -> usize { pub fn height(&self) -> u32 {
self.cells.shape()[0] self.cells.shape()[0] as _
} }
pub fn get(&self, x: usize, y: usize) -> &Cell<M> { pub fn get(&self, x: u32, y: u32) -> &Cell<M> {
&self.cells[[y, x]] &self.cells[[y as _, x as _]]
} }
pub fn get_mut(&mut self, x: usize, y: usize) -> &mut Cell<M> { pub fn get_mut(&mut self, x: u32, y: u32) -> &mut Cell<M> {
&mut self.cells[[y, x]] &mut self.cells[[y as _, x as _]]
} }
fn timestep(&self) -> R64 { fn timestep(&self) -> R64 {