Fold SampleableSim and MaterialSim into one

This commit is contained in:
2022-07-28 16:41:32 -07:00
parent a49d9cd7a4
commit 6206569f4a
8 changed files with 137 additions and 149 deletions

View File

@@ -1,7 +1,7 @@
use crate::geom::Meters;
use crate::real::ToFloat as _;
use crate::cross::vec::{Vec2, Vec3};
use crate::sim::{SampleableSim, Sample, StaticSim};
use crate::sim::{GenericSim, Sample, StaticSim};
use crate::meas::{self, AbstractMeasurement};
use crossterm::{cursor, QueueableCommand as _};
use crossterm::style::{style, Color, PrintStyledContent, Stylize as _};
@@ -55,7 +55,7 @@ fn scale_vector(x: Vec2<f32>, typical_mag: f32) -> Vec2<f32> {
x.with_mag(new_mag)
}
fn im_size<S: SampleableSim>(state: &S, max_w: u32, max_h: u32) -> (u32, u32) {
fn im_size<S: GenericSim>(state: &S, max_w: u32, max_h: u32) -> (u32, u32) {
let mut width = max_w;
let mut height = width * state.height() / state.width();
if height > max_h {
@@ -134,7 +134,7 @@ struct RenderSteps<'a, S> {
z: u32,
}
impl<'a, S: SampleableSim> RenderSteps<'a, S> {
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 {
Self::render_configured(state, measurements, z, (640, 480), RenderConfig::default())
@@ -352,12 +352,12 @@ pub trait Renderer<S>: Send + Sync {
}
}
fn default_render_z_slice<S: SampleableSim, R: Renderer<S>>(
fn default_render_z_slice<S: GenericSim, R: Renderer<S>>(
me: &R, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig,
) {
me.render_with_image(state, &RenderSteps::render(state, measurements, z), measurements, config);
}
fn default_render<S: SampleableSim, R: Renderer<S>>(
fn default_render<S: GenericSim, R: Renderer<S>>(
me: &R, state: &S, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig
) {
me.render_z_slice(state, state.depth() / 2, measurements, config);
@@ -386,7 +386,7 @@ fn default_render<S: SampleableSim, R: Renderer<S>>(
#[derive(Default)]
pub struct ColorTermRenderer;
impl<S: SampleableSim> Renderer<S> for ColorTermRenderer {
impl<S: GenericSim> Renderer<S> for ColorTermRenderer {
fn render(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
default_render(self, state, measurements, config)
}
@@ -448,7 +448,7 @@ impl Y4MRenderer {
}
}
impl<S: SampleableSim> Renderer<S> for Y4MRenderer {
impl<S: GenericSim> Renderer<S> for Y4MRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
@@ -540,7 +540,7 @@ impl<S> MultiRenderer<S> {
}
}
impl<S: SampleableSim> Renderer<S> for 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) {
default_render_z_slice(self, state, z, measurements, config)
}
@@ -567,10 +567,10 @@ pub struct SerializedFrame<S=StaticSim> {
pub measurements: Vec<Box<dyn AbstractMeasurement>>,
}
impl<S: SampleableSim> SerializedFrame<S> {
impl<S: GenericSim> SerializedFrame<S> {
pub fn to_static(self) -> SerializedFrame<StaticSim> {
SerializedFrame {
state: SampleableSim::to_static(&self.state),
state: GenericSim::to_static(&self.state),
measurements: self.measurements,
}
}
@@ -605,7 +605,7 @@ impl SerializerRenderer {
}
impl SerializerRenderer {
fn serialize<S: SampleableSim + Serialize>(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>]) {
fn serialize<S: GenericSim + Serialize>(&self, state: &S, measurements: &[Box<dyn AbstractMeasurement>]) {
let frame = SerializedFrame {
state,
measurements: measurements.iter().cloned().collect(),
@@ -616,13 +616,13 @@ impl SerializerRenderer {
bincode::serialize_into(out, &frame).unwrap();
}
pub fn try_load<S: SampleableSim + for <'a> Deserialize<'a>>(&self) -> Option<SerializedFrame<S>> {
pub fn try_load<S: GenericSim + for <'a> Deserialize<'a>>(&self) -> Option<SerializedFrame<S>> {
let mut reader = BufReader::new(File::open(&*self.fmt_str).ok()?);
bincode::deserialize_from(&mut reader).ok()
}
}
impl<S: SampleableSim + Serialize> Renderer<S> for SerializerRenderer {
impl<S: GenericSim + Serialize> Renderer<S> for SerializerRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}
@@ -665,7 +665,7 @@ impl CsvRenderer {
}
}
impl<S: SampleableSim> Renderer<S> for CsvRenderer {
impl<S: GenericSim> Renderer<S> for CsvRenderer {
fn render_z_slice(&self, state: &S, z: u32, measurements: &[Box<dyn AbstractMeasurement>], config: RenderConfig) {
default_render_z_slice(self, state, z, measurements, config)
}