measurements: store to disk *after* evaluating them

i'm hoping to simplify a lot of serialization code with this
This commit is contained in:
2022-07-28 21:43:48 -07:00
parent 32d1a70d15
commit 5c4b8d86f2
5 changed files with 53 additions and 39 deletions

View File

@@ -129,20 +129,20 @@ impl RenderConfig {
struct RenderSteps<'a, S> {
im: RgbImage,
sim: &'a S,
meas: &'a [Box<dyn AbstractMeasurement>],
meas: &'a [&'a dyn AbstractMeasurement],
/// Simulation z coordinate to sample
z: u32,
}
impl<'a, S: GenericSim> RenderSteps<'a, S> {
/// Render using default configuration constants
fn render(state: &'a S, measurements: &'a [Box<dyn AbstractMeasurement>], z: u32) -> RgbImage {
fn render(state: &'a S, measurements: &'a [&'a dyn AbstractMeasurement], 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 [Box<dyn AbstractMeasurement>],
measurements: &'a [&'a dyn AbstractMeasurement],
z: u32,
max_size: (u32, u32),
config: RenderConfig,
@@ -178,7 +178,7 @@ impl<'a, S: GenericSim> RenderSteps<'a, S> {
me.render_measurements();
me.im
}
fn new(sim: &'a S, meas: &'a [Box<dyn AbstractMeasurement>], width: u32, height: u32, z: u32) -> Self {
fn new(sim: &'a S, meas: &'a [&'a dyn AbstractMeasurement], width: u32, height: u32, z: u32) -> Self {
RenderSteps {
im: RgbImage::new(width, height),
sim,
@@ -340,25 +340,25 @@ impl ImageRenderExt for RgbImage {
}
pub trait Renderer<S>: Send + Sync {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig);
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig);
// {
// self.render_with_image(state, &RenderSteps::render(state, measurements, z), measurements);
// }
fn render(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig);
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], 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: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render_with_image(&self, state: &S, _im: &RgbImage, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
self.render(state, measurements, config);
}
}
fn default_render_z_slice<S: GenericSim, R: Renderer<S>>(
me: &R, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig,
me: &R, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], 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: &[Box<dyn AbstractMeasurement>], config: RenderConfig
me: &R, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig
) {
me.render_z_slice(state, state.depth() / 2, measurements, config);
}
@@ -366,7 +366,7 @@ fn default_render<S: GenericSim, R: Renderer<S>>(
// pub struct NumericTermRenderer;
//
// impl Renderer for NumericTermRenderer {
// fn render(&mut self, state: &SimSnapshot, _measurements: &[Box<dyn AbstractMeasurement>]) {
// fn render(&mut self, state: &SimSnapshot, _measurements: &[&dyn AbstractMeasurement]) {
// for y in 0..state.height() {
// for x in 0..state.width() {
// let cell = state.get((x, y).into());
@@ -387,14 +387,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: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
default_render(self, state, measurements, config)
}
fn render_z_slice(
&self,
state: &S,
z: u32,
measurements: &[Box<dyn AbstractMeasurement>],
measurements: &[&dyn AbstractMeasurement],
config: RenderConfig,
) {
let (max_w, mut max_h) = crossterm::terminal::size().unwrap();
@@ -449,13 +449,13 @@ impl Y4MRenderer {
}
impl<S: GenericSim> Renderer<S> for Y4MRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
default_render(self, state, measurements, config)
}
fn render_with_image(&self, _state: &S, im: &RgbImage, _meas: &[Box<dyn AbstractMeasurement>], _config: RenderConfig) {
fn render_with_image(&self, _state: &S, im: &RgbImage, _meas: &[&dyn AbstractMeasurement], _config: RenderConfig) {
{
let mut enc = self.encoder.lock().unwrap();
if enc.is_none() {
@@ -541,16 +541,16 @@ impl<S> MultiRenderer<S> {
}
impl<S: GenericSim> Renderer<S> for MultiRenderer<S> {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], 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: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render_with_image(&self, state: &S, im: &RgbImage, measurements: &[&dyn AbstractMeasurement], 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);
@@ -564,7 +564,7 @@ pub struct SerializedFrame<S=StaticSim> {
pub state: S,
/// although not generally necessary to load the sim, saving the measurements is beneficial for
/// post-processing.
pub measurements: Vec<Box<dyn AbstractMeasurement>>,
pub measurements: Vec<meas::Evaluated>,
}
impl<S: GenericSim> SerializedFrame<S> {
@@ -605,10 +605,10 @@ impl SerializerRenderer {
}
impl SerializerRenderer {
fn serialize<S: GenericSim + Serialize>(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>]) {
fn serialize<S: GenericSim + Serialize>(&self, state: &S, measurements: &[&dyn AbstractMeasurement]) {
let frame = SerializedFrame {
state,
measurements: measurements.iter().cloned().collect(),
measurements: meas::eval_to_vec(state, measurements),
};
let name = self.fmt_str.replace("{step_no}", &*frame.state.step_no().to_string());
let out = BufWriter::new(File::create(name).unwrap());
@@ -623,10 +623,10 @@ impl SerializerRenderer {
}
impl<S: GenericSim + Serialize> Renderer<S> for SerializerRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>], _config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], _config: RenderConfig) {
if self.prefer_static {
self.serialize(&state.to_static(), measurements);
} else {
@@ -666,10 +666,10 @@ impl CsvRenderer {
}
impl<S: GenericSim> Renderer<S> for CsvRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[&dyn AbstractMeasurement], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
fn render(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>], _config: RenderConfig) {
fn render(&self, state: &S, measurements: &[&dyn AbstractMeasurement], _config: RenderConfig) {
let row = meas::eval_multiple_kv(state, measurements);
let step = state.step_no();
let mut lock = self.state.lock().unwrap();