diff --git a/Cargo.lock b/Cargo.lock index 4185094..adc83f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,10 @@ version = 3 [[package]] name = "pointcache" version = "0.1.0" + +[[package]] +name = "tools" +version = "0.1.0" +dependencies = [ + "pointcache", +] diff --git a/Cargo.toml b/Cargo.toml index 9105f5a..1a1e942 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,4 @@ +workspace = { members = ["tools"] } [package] name = "pointcache" version = "0.1.0" diff --git a/src/lib.rs b/src/lib.rs index c450c6d..8f999c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,21 @@ use std::fmt::Display; use std::io::Write; use std::mem::size_of; -use std::ops::Range; use std::{ error::Error, fs::File, - io::{BufReader, Read, Seek, SeekFrom}, - iter::repeat, + io::{Read, Seek, SeekFrom}, }; pub trait PointCache { - fn map Result<(), Box>>( + type Frame; + fn read( infile: File, - op: F, - ) -> Result<(), Box>; - fn map_to Result>>( + ) -> Result< + Box::Frame, Box>>>, + Box, + >; + fn map_to::Frame) -> Result>>( infile: File, outfile: File, op: F, @@ -22,29 +23,24 @@ pub trait PointCache { } pub struct MDDSeekableFile; -struct MDDSeekableFileReader { - total_frames: i32, - total_points: i32, - data: Box>>>, -} #[derive(Debug)] pub struct Point { - x: f32, - y: f32, - z: f32, + pub x: f32, + pub y: f32, + pub z: f32, } #[derive(Debug)] pub struct MDDFrame { - frame_idx: usize, - point_idx: usize, - time: f32, - point: Point, + pub frame_idx: usize, + pub point_idx: usize, + pub time: f32, + pub point: Point, } impl MDDSeekableFile { fn read_header(infile: &mut File) -> Result<(i32, i32), Box> { - infile.seek(SeekFrom::Start(0)); + infile.seek(SeekFrom::Start(0))?; let mut buff = [0u8; size_of::()]; infile.read_exact(&mut buff)?; let total_frames = i32::from_be_bytes(buff); @@ -58,25 +54,28 @@ impl MDDSeekableFile { total_frames: i32, total_points: i32, ) -> Result<(), Box> { - outfile.seek(SeekFrom::Start(0)); + outfile.seek(SeekFrom::Start(0))?; outfile.write_all(&total_frames.to_be_bytes())?; outfile.write_all(&total_points.to_be_bytes())?; Ok(()) } - fn read(mut infile: File) -> Result> { - let (total_frames, total_points) = MDDSeekableFile::read_header(&mut infile)?; - let time_byte_offset = (total_frames as usize) * size_of::(); +} - let mut buff = [0u8; size_of::()]; - let times = Box::new( - repeat(()) - .map::>, _>(move |()| { - //times.read_exact(&mut buff)?; - //Ok(f32::from_be_bytes(buff)) - Ok(0.0) - }) - .take(total_frames.try_into()?), - ); +#[derive(Debug)] +struct ExaustedPointsUnexpectedly; +impl Display for ExaustedPointsUnexpectedly { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "ExaustedPointsUnexpectedly") + } +} +impl Error for ExaustedPointsUnexpectedly {} + +impl PointCache for MDDSeekableFile { + type Frame = MDDFrame; + fn read( + mut infile: File, + ) -> Result>>>, Box> { + let (total_frames, total_points) = MDDSeekableFile::read_header(&mut infile)?; let header_size: u64 = (size_of::() * 2).try_into()?; let f32_size: u64 = size_of::().try_into()?; @@ -112,40 +111,7 @@ impl MDDSeekableFile { }, ), ); - - Ok(MDDSeekableFileReader { - total_frames, - total_points, - data, - }) - } -} - -#[derive(Debug)] -struct ExaustedPointsUnexpectedly; -impl Display for ExaustedPointsUnexpectedly { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "ExaustedPointsUnexpectedly") - } -} -impl Error for ExaustedPointsUnexpectedly {} - -impl PointCache for MDDSeekableFile { - fn map(infile: File, mut op: F) -> Result<(), Box> - where - F: FnMut(MDDFrame) -> Result<(), Box>, - { - let MDDSeekableFileReader { - total_frames: _, - total_points: _, - data, - } = MDDSeekableFile::read(infile)?; - - for frame in data { - op(frame?)?; - } - - Ok(()) + Ok(data) } fn map_to(mut infile: File, mut outfile: File, mut op: F) -> Result<(), Box> @@ -159,8 +125,8 @@ impl PointCache for MDDSeekableFile { let f32_size: u64 = size_of::().try_into()?; let mut count = 0; - Self::map(infile, |frame| { - let frame = op(frame)?; + for frame in Self::read(infile)? { + let frame = op(frame?)?; let frame_idx: u64 = count / (total_points as u64); let time_offset: u64 = f32_size * frame_idx; let time_size: u64 = f32_size * (total_frames as u64); @@ -175,8 +141,8 @@ impl PointCache for MDDSeekableFile { outfile.write_all(&frame.point.z.to_be_bytes())?; count += 1; - Ok(()) - })?; + } + Ok(()) } } @@ -204,10 +170,9 @@ mod tests { })?; println!("\nReading back output.mdd"); let infile = File::open("output.mdd")?; - MDDSeekableFile::map(infile, |frame| { + for frame in MDDSeekableFile::read(infile)? { println!("{:?}", frame); - Ok(()) - })?; + } Ok(()) } } diff --git a/tools/Cargo.toml b/tools/Cargo.toml new file mode 100644 index 0000000..ac95a68 --- /dev/null +++ b/tools/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tools" +version = "0.1.0" +edition = "2021" + +[dependencies] +pointcache = { path = ".." } diff --git a/tools/src/main.rs b/tools/src/main.rs new file mode 100644 index 0000000..28335d7 --- /dev/null +++ b/tools/src/main.rs @@ -0,0 +1,29 @@ +use std::fs::File; + +use pointcache::{MDDSeekableFile, PointCache, MDDFrame, Point}; + + +fn main() { + let file_a = std::env::args().nth(1).expect("Need three files"); + let file_b = std::env::args().nth(1).expect("Need three files"); + let file_c = std::env::args().nth(1).expect("Need three files"); + + let file_a = File::open(file_a).unwrap(); + let file_b = File::open(file_b).unwrap(); + let file_c = File::create(file_c).unwrap(); + + let mut file_b = MDDSeekableFile::read(file_b).unwrap(); + + MDDSeekableFile::map_to(file_a, file_c, |frame| { + let frame_b = file_b.next().unwrap()?; + Ok(MDDFrame { + point: Point { + x: frame_b.point.x - frame.point.x, + y: frame_b.point.y - frame.point.y, + z: frame_b.point.z - frame.point.z, + }, + ..frame + }) + }).unwrap(); + +}