de-virtualize GenericSim

this should let us fold the GenericSim and MaterialSim traits together.
This commit is contained in:
2022-07-28 22:22:07 -07:00
parent 2d1a15eabc
commit 71ab89c4c9
8 changed files with 185 additions and 179 deletions

View File

@@ -129,20 +129,22 @@ impl RenderConfig {
struct RenderSteps<'a, S> {
im: RgbImage,
sim: &'a S,
meas: &'a [&'a dyn AbstractMeasurement],
meas: &'a [&'a dyn AbstractMeasurement<S>],
/// Simulation z coordinate to sample
z: u32,
}
impl<'a, S: GenericSim> RenderSteps<'a, S> {
// TODO: this could probably be a single measurement, and we just let collections of
// measurements also behave as measurements
/// Render using default configuration constants
fn render(state: &'a S, measurements: &'a [&'a dyn AbstractMeasurement], z: u32) -> RgbImage {
fn render(state: &'a S, measurements: &'a [&'a dyn AbstractMeasurement<S>], z: u32) -> RgbImage {
Self::render_configured(state, measurements, z, (640, 480), RenderConfig::default())
}
/// Render, controlling things like the size.
fn render_configured(
state: &'a S,
measurements: &'a [&'a dyn AbstractMeasurement],
measurements: &'a [&'a dyn AbstractMeasurement<S>],
z: u32,
max_size: (u32, u32),
config: RenderConfig,
@@ -178,7 +180,7 @@ impl<'a, S: GenericSim> RenderSteps<'a, S> {
me.render_measurements();
me.im
}
fn new(sim: &'a S, meas: &'a [&'a dyn AbstractMeasurement], width: u32, height: u32, z: u32) -> Self {
fn new(sim: &'a S, meas: &'a [&'a dyn AbstractMeasurement<S>], width: u32, height: u32, z: u32) -> Self {
RenderSteps {
im: RgbImage::new(width, height),
sim,
@@ -340,25 +342,25 @@ impl ImageRenderExt for RgbImage {
}
pub trait Renderer<S>: Send + Sync {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig);
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig);
// {
// self.render_with_image(state, &RenderSteps::render(state, measurements, z), measurements);
// }
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig);
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig);
/// Not intended to be called directly by users; implement this if you want the image to be
/// computed using default settings and you just manage where to display/save it.
fn render_with_image(&self, state: &S, _im: &RgbImage, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render_with_image(&self, state: &S, _im: &RgbImage, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
self.render(state, measurements, config);
}
}
fn default_render_z_slice<S: GenericSim, R: Renderer<S>>(
me: &R, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig,
me: &R, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig,
) {
me.render_with_image(state, &RenderSteps::render(state, measurements, z), measurements, config);
}
fn default_render<S: GenericSim, R: Renderer<S>>(
me: &R, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig
me: &R, state: &S, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig
) {
me.render_z_slice(state, state.depth() / 2, measurements, config);
}
@@ -366,7 +368,7 @@ fn default_render<S: GenericSim, R: Renderer<S>>(
// pub struct NumericTermRenderer;
//
// impl Renderer for NumericTermRenderer {
// fn render(&mut self, state: &SimSnapshot, _measurements: &[&dyn AbstractMeasurement]) {
// fn render(&mut self, state: &SimSnapshot, _measurements: &[&dyn AbstractMeasurement<S>]) {
// for y in 0..state.height() {
// for x in 0..state.width() {
// let cell = state.get((x, y).into());
@@ -387,14 +389,14 @@ fn default_render<S: GenericSim, R: Renderer<S>>(
pub struct ColorTermRenderer;
impl<S: GenericSim> Renderer<S> for ColorTermRenderer {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
default_render(self, state, measurements, config)
}
fn render_z_slice(
&self,
state: &S,
z: u32,
measurements: &[&dyn AbstractMeasurement],
measurements: &[&dyn AbstractMeasurement<S>],
config: RenderConfig,
) {
let (max_w, mut max_h) = crossterm::terminal::size().unwrap();
@@ -449,13 +451,13 @@ impl Y4MRenderer {
}
impl<S: GenericSim> Renderer<S> for Y4MRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
default_render(self, state, measurements, config)
}
fn render_with_image(&self, _state: &S, im: &RgbImage, _meas: &[&dyn AbstractMeasurement], _config: RenderConfig) {
fn render_with_image(&self, _state: &S, im: &RgbImage, _meas: &[&dyn AbstractMeasurement<S>], _config: RenderConfig) {
{
let mut enc = self.encoder.lock().unwrap();
if enc.is_none() {
@@ -541,16 +543,16 @@ impl<S> MultiRenderer<S> {
}
impl<S: GenericSim> Renderer<S> for MultiRenderer<S> {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
if self.renderers.read().unwrap().len() != 0 {
self.render_with_image(state, &RenderSteps::render(state, measurements, state.depth() / 2), measurements, config);
}
}
fn render_with_image(&self, state: &S, im: &RgbImage, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render_with_image(&self, state: &S, im: &RgbImage, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
for r in &*self.renderers.read().unwrap() {
if r.work_this_frame(state.step_no()) {
r.renderer.render_with_image(state, im, measurements, config);
@@ -605,10 +607,10 @@ impl SerializerRenderer {
}
impl SerializerRenderer {
fn serialize<S: GenericSim + Serialize>(&self, state: &S, measurements: &[&dyn AbstractMeasurement]) {
fn serialize<S: GenericSim + Serialize>(&self, state: &S, measurements: Vec<meas::Evaluated>) {
let frame = SerializedFrame {
state,
measurements: meas::eval_to_vec(state, measurements),
measurements,
};
let name = self.fmt_str.replace("{step_no}", &*frame.state.step_no().to_string());
let out = BufWriter::new(File::create(name).unwrap());
@@ -623,14 +625,14 @@ impl SerializerRenderer {
}
impl<S: GenericSim + Serialize> Renderer<S> for SerializerRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], _config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], _config: RenderConfig) {
if self.prefer_static {
self.serialize(&state.to_static(), measurements);
self.serialize(&state.to_static(), meas::eval_to_vec(state, measurements));
} else {
self.serialize(state, measurements);
self.serialize(state, meas::eval_to_vec(state, measurements));
}
}
}
@@ -666,10 +668,10 @@ impl CsvRenderer {
}
impl<S: GenericSim> Renderer<S> for CsvRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement<S>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], _config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement<S>], _config: RenderConfig) {
let row = meas::eval_multiple_kv(state, measurements);
let step = state.step_no();
let mut lock = self.state.lock().unwrap();