Skip to content

Commit

Permalink
Make the slicer generic allowing slicing into different formats
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Jul 3, 2024
1 parent 5fdf616 commit 3fae064
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 57 deletions.
6 changes: 3 additions & 3 deletions common/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl Image {
let half_kernel_size = kernel_size / 2;

let mut kernel = vec![0.0; kernel_size];
for x in 0..kernel_size {
kernel[x] = gaussian((x - half_kernel_size) as f32, sigma);
for (i, e) in kernel.iter_mut().enumerate().take(kernel_size) {
*e = gaussian((i - half_kernel_size) as f32, sigma);
}

// Blur image horizontally
Expand Down Expand Up @@ -104,6 +104,6 @@ impl Image {
}

fn gaussian(x: f32, sigma: f32) -> f32 {
const ROOT_TWO_PI: f32 = 2.50662827463100050242;
const ROOT_TWO_PI: f32 = 2.506_628_3;
(x.powi(2) / (2.0 * sigma.powi(2))).exp() / (sigma * ROOT_TWO_PI)
}
14 changes: 11 additions & 3 deletions common/src/misc.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use crate::{config::SliceConfig, image::Image};
use crate::config::SliceConfig;

pub struct SliceResult<'a> {
pub layers: Vec<Image>,
pub struct SliceResult<'a, Layer> {
pub layers: Vec<Layer>,
pub slice_config: &'a SliceConfig,
}

pub struct Run {
pub length: u64,
pub value: u8,
}

pub trait EncodableLayer {
type Output: Send;

fn new() -> Self;
fn add_run(&mut self, length: u64, value: u8);
fn finish(self, layer: usize, config: &SliceConfig) -> Self::Output;
}
42 changes: 40 additions & 2 deletions goo_format/src/encoded_layer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use common::misc::Run;
use common::{
config::SliceConfig,
misc::{EncodableLayer, Run},
};

use crate::layer_content::calculate_checksum;
use crate::{layer_content::calculate_checksum, LayerContent};

pub struct LayerEncoder {
data: Vec<u8>,
Expand Down Expand Up @@ -102,6 +105,41 @@ impl LayerEncoder {
}
}

impl EncodableLayer for LayerEncoder {
type Output = LayerContent;

fn new() -> Self {
Self::new()
}

fn add_run(&mut self, length: u64, value: u8) {
self.add_run(length, value);
}

fn finish(self, layer: usize, slice_config: &SliceConfig) -> Self::Output {
let (data, checksum) = self.finish();
let layer_exposure = if (layer as u32) < slice_config.first_layers {
&slice_config.first_exposure_config
} else {
&slice_config.exposure_config
};

LayerContent {
data,
checksum,
layer_position_z: slice_config.slice_height * (layer + 1) as f32,

layer_exposure_time: layer_exposure.exposure_time,
lift_distance: layer_exposure.lift_distance,
lift_speed: layer_exposure.lift_speed,
retract_distance: layer_exposure.retract_distance,
retract_speed: layer_exposure.retract_speed,
pause_position_z: slice_config.platform_size.z,
..Default::default()
}
}
}

impl<'a> LayerDecoder<'a> {
pub fn new(data: &'a [u8]) -> Self {
Self {
Expand Down
42 changes: 6 additions & 36 deletions goo_format/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use common::{
serde::{Deserializer, Serializer},
};

use crate::{HeaderInfo, LayerContent, LayerEncoder, ENDING_STRING};
use crate::{HeaderInfo, LayerContent, ENDING_STRING};

pub struct File {
pub header: HeaderInfo,
Expand All @@ -17,41 +17,11 @@ impl File {
Self { header, layers }
}

pub fn from_slice_result(result: SliceResult) -> Self {
let slice_config = result.slice_config;
let layers = result
.layers
.into_iter()
.enumerate()
.map(|(idx, layer)| {
let mut encoder = LayerEncoder::new();

for run in layer.runs() {
encoder.add_run(run.length, run.value);
}

let (data, checksum) = encoder.finish();
let layer_exposure = if (idx as u32) < slice_config.first_layers {
&slice_config.first_exposure_config
} else {
&slice_config.exposure_config
};

LayerContent {
data,
checksum,
layer_position_z: slice_config.slice_height * (idx + 1) as f32,

layer_exposure_time: layer_exposure.exposure_time,
lift_distance: layer_exposure.lift_distance,
lift_speed: layer_exposure.lift_speed,
retract_distance: layer_exposure.retract_distance,
retract_speed: layer_exposure.retract_speed,
pause_position_z: slice_config.platform_size.z,
..Default::default()
}
})
.collect::<Vec<_>>();
pub fn from_slice_result(result: SliceResult<LayerContent>) -> Self {
let SliceResult {
layers,
slice_config,
} = result;

let layer_time = slice_config.exposure_config.exposure_time
+ slice_config.exposure_config.lift_distance / slice_config.exposure_config.lift_speed;
Expand Down
4 changes: 2 additions & 2 deletions mslicer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
windows::{self, Windows},
};
use common::config::{ExposureConfig, SliceConfig};
use goo_format::File as GooFile;
use goo_format::{File as GooFile, LayerEncoder};

pub struct App {
pub camera: Camera,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl App {
self.slice_progress = Some(slicer.progress());

thread::spawn(clone!([{ self.slice_result } as slice_result], move || {
let goo = GooFile::from_slice_result(slicer.slice());
let goo = GooFile::from_slice_result(slicer.slice::<LayerEncoder>());
slice_result.lock().unwrap().replace(SliceResult {
goo,
slice_preview_layer: 0,
Expand Down
4 changes: 2 additions & 2 deletions slicer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use common::{
config::{ExposureConfig, SliceConfig},
serde::DynamicSerializer,
};
use goo_format::File as GooFile;
use goo_format::{File as GooFile, LayerEncoder};
use slicer::{mesh::load_mesh, slicer::Slicer, Pos};

fn main() -> Result<()> {
Expand Down Expand Up @@ -66,7 +66,7 @@ fn main() -> Result<()> {
let slicer = Slicer::new(slice_config.clone(), mesh);
let progress = slicer.progress();

let goo = thread::spawn(move || GooFile::from_slice_result(slicer.slice()));
let goo = thread::spawn(move || GooFile::from_slice_result(slicer.slice::<LayerEncoder>()));

let mut completed = 0;
while completed < progress.total() {
Expand Down
23 changes: 14 additions & 9 deletions slicer/src/slicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{
},
};

use common::{config::SliceConfig, image::Image, misc::SliceResult};
use common::{
config::SliceConfig,
misc::{EncodableLayer, SliceResult},
};
use ordered_float::OrderedFloat;
use rayon::iter::{IntoParallelIterator, ParallelIterator};

Expand Down Expand Up @@ -58,7 +61,7 @@ impl Slicer {
self.progress.clone()
}

pub fn slice(&self) -> SliceResult {
pub fn slice<Layer: EncodableLayer>(&self) -> SliceResult<Layer::Output> {
let (slice_config, model) = (&self.slice_config, &self.model);
let layers = (0..self.progress.total)
.into_par_iter()
Expand Down Expand Up @@ -98,23 +101,25 @@ impl Slicer {
}
}

let mut image = Image::blank(
self.slice_config.platform_resolution.x as usize,
self.slice_config.platform_resolution.y as usize,
);
// let mut image = Image::blank(
// self.slice_config.platform_resolution.x as usize,
// self.slice_config.platform_resolution.y as usize,
// );

let mut encoder = Layer::new();

let mut last = 0;
for (start, end) in out {
if start > last {
image.add_run((start - last) as usize, 0);
encoder.add_run(start - last, 0);
}

assert!(end >= start, "End precedes start in layer {layer}");
image.add_run((end - start) as usize, 255);
encoder.add_run(end - start, 255);
last = end;
}

image
encoder.finish(layer as usize, slice_config)
})
.collect::<Vec<_>>();

Expand Down

0 comments on commit 3fae064

Please sign in to comment.