Generalize rendering into a trait

This commit is contained in:
2020-09-04 16:12:33 -07:00
parent 99cb824a36
commit 6d7bb5becb
3 changed files with 42 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
use coremem::{consts, mat, SimState}; use coremem::{consts, mat, SimState};
use coremem::render; use coremem::render::{self, Renderer as _};
use std::{thread, time}; use std::{thread, time};
fn main() { fn main() {

View File

@@ -1,14 +1,26 @@
use coremem::{consts, mat, SimState}; use coremem::{consts, mat, SimState};
use coremem::render::ColorTermRenderer as Renderer; use coremem::render::{self, Renderer as _};
use std::{thread, time}; use std::{thread, time};
fn main() { fn main() {
let width = 201; let width = 201;
let mut state = SimState::new(width, 101, 1e-3 /* feature size */); let height = 101;
let mut state = SimState::new(width, height, 1e-3 /* feature size */);
for y in 0..100 { for y in 0..height {
for x in 50..60 { for x in 50..60 {
*state.get_mut(x, y).mat_mut() = mat::Conductor { conductivity: 10.0 }.into(); *state.get_mut(x, y).mat_mut() = mat::Conductor { conductivity: 1.0e1 }.into();
}
// for x in 30..40 {
// *state.get_mut(x, y).mat_mut() = mat::Conductor { conductivity: 1.0e8 }.into();
// }
// if (0..10).contains(&y) || (height-10..height).contains(&y) {
// for x in 40..50 {
// *state.get_mut(x, y).mat_mut() = mat::Conductor { conductivity: 1.0e8 }.into();
// }
// }
for x in 72..80 {
*state.get_mut(x, y).mat_mut() = mat::Conductor { conductivity: 1.0e1 }.into();
} }
} }
for y in 40..60 { for y in 40..60 {
@@ -33,11 +45,15 @@ fn main() {
} }
} }
let mut step = 0u64; let mut renderer = render::ColorTermRenderer;
loop { loop {
step += 1; //let imp = match state.step_no() {
let imp = if step < 50 { // 20..=60 => 1e6,
250000.0 * ((step as f64)*0.02*std::f64::consts::PI).sin() // 400..=440 => -1e6,
// _ => 0.0
//};
let imp = if state.step_no() < 50 {
250000.0 * ((state.step_no() as f64)*0.02*std::f64::consts::PI).sin()
} else { } else {
0.0 0.0
}; };
@@ -45,13 +61,15 @@ fn main() {
// state.impulse_ey(50, 50, imp); // state.impulse_ey(50, 50, imp);
// state.impulse_bz(20, 20, (imp / 3.0e8) as _); // state.impulse_bz(20, 20, (imp / 3.0e8) as _);
// state.impulse_bz(80, 20, (imp / 3.0e8) as _); // state.impulse_bz(80, 20, (imp / 3.0e8) as _);
for y in 0..100 { for y in 10..height-10 {
for x in 52..58 { for x in 52..58 {
state.impulse_ey(x, y, imp as _); state.impulse_ey(x, y, imp as _);
} }
} }
Renderer.render(&state); if state.step_no() % 10 == 0 {
renderer.render(&state);
thread::sleep(time::Duration::from_millis(33));
}
state.step(); state.step();
thread::sleep(time::Duration::from_millis(67));
} }
} }

View File

@@ -7,8 +7,12 @@ use y4m::{Colorspace, encode, Encoder, Frame, Ratio};
pub struct NumericTermRenderer; pub struct NumericTermRenderer;
impl NumericTermRenderer { pub trait Renderer {
pub fn render(&self, state: &SimState) { fn render(&mut self, state: &SimState);
}
impl Renderer for NumericTermRenderer {
fn render(&mut self, state: &SimState) {
for y in 0..state.height() { for y in 0..state.height() {
for x in 0..state.width() { for x in 0..state.width() {
let cell = state.get(x, y); let cell = state.get(x, y);
@@ -44,8 +48,8 @@ fn curl(x: f64, y: f64) -> f64 {
} }
} }
impl ColorTermRenderer { impl Renderer for ColorTermRenderer {
pub fn render(&self, state: &SimState) { fn render(&mut self, state: &SimState) {
let mut buf = String::new(); let mut buf = String::new();
let square = ""; let square = "";
for y in 0..state.height() { for y in 0..state.height() {
@@ -82,7 +86,10 @@ impl Y4MRenderer {
encoder: None, encoder: None,
} }
} }
pub fn render(&mut self, state: &SimState) { }
impl Renderer for Y4MRenderer {
fn render(&mut self, state: &SimState) {
if self.encoder.is_none() { if self.encoder.is_none() {
let writer = File::create(&self.out_path).unwrap(); let writer = File::create(&self.out_path).unwrap();
self.encoder = Some(encode(state.width(), state.height(), Ratio::new(30, 1)) self.encoder = Some(encode(state.width(), state.height(), Ratio::new(30, 1))