This commit is contained in:
Spencer Killen 2023-11-26 18:08:17 -07:00
parent 6f1c60fc70
commit cc5c3d6dca
Signed by: sjkillen
GPG Key ID: 3AF3117BA6FBB75B
5 changed files with 84 additions and 75 deletions

7
Cargo.lock generated
View File

@ -5,3 +5,10 @@ version = 3
[[package]] [[package]]
name = "pointcache" name = "pointcache"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "tools"
version = "0.1.0"
dependencies = [
"pointcache",
]

View File

@ -1,3 +1,4 @@
workspace = { members = ["tools"] }
[package] [package]
name = "pointcache" name = "pointcache"
version = "0.1.0" version = "0.1.0"

View File

@ -1,20 +1,21 @@
use std::fmt::Display; use std::fmt::Display;
use std::io::Write; use std::io::Write;
use std::mem::size_of; use std::mem::size_of;
use std::ops::Range;
use std::{ use std::{
error::Error, error::Error,
fs::File, fs::File,
io::{BufReader, Read, Seek, SeekFrom}, io::{Read, Seek, SeekFrom},
iter::repeat,
}; };
pub trait PointCache { pub trait PointCache {
fn map<F: FnMut(MDDFrame) -> Result<(), Box<dyn Error>>>( type Frame;
fn read(
infile: File, infile: File,
op: F, ) -> Result<
) -> Result<(), Box<dyn Error>>; Box<dyn Iterator<Item = Result<<Self as PointCache>::Frame, Box<dyn Error>>>>,
fn map_to<F: FnMut(MDDFrame) -> Result<MDDFrame, Box<dyn Error>>>( Box<dyn Error>,
>;
fn map_to<F: FnMut(<Self as PointCache>::Frame) -> Result<MDDFrame, Box<dyn Error>>>(
infile: File, infile: File,
outfile: File, outfile: File,
op: F, op: F,
@ -22,29 +23,24 @@ pub trait PointCache {
} }
pub struct MDDSeekableFile; pub struct MDDSeekableFile;
struct MDDSeekableFileReader {
total_frames: i32,
total_points: i32,
data: Box<dyn Iterator<Item = Result<MDDFrame, Box<dyn Error>>>>,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Point { pub struct Point {
x: f32, pub x: f32,
y: f32, pub y: f32,
z: f32, pub z: f32,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct MDDFrame { pub struct MDDFrame {
frame_idx: usize, pub frame_idx: usize,
point_idx: usize, pub point_idx: usize,
time: f32, pub time: f32,
point: Point, pub point: Point,
} }
impl MDDSeekableFile { impl MDDSeekableFile {
fn read_header(infile: &mut File) -> Result<(i32, i32), Box<dyn Error>> { fn read_header(infile: &mut File) -> Result<(i32, i32), Box<dyn Error>> {
infile.seek(SeekFrom::Start(0)); infile.seek(SeekFrom::Start(0))?;
let mut buff = [0u8; size_of::<i32>()]; let mut buff = [0u8; size_of::<i32>()];
infile.read_exact(&mut buff)?; infile.read_exact(&mut buff)?;
let total_frames = i32::from_be_bytes(buff); let total_frames = i32::from_be_bytes(buff);
@ -58,25 +54,28 @@ impl MDDSeekableFile {
total_frames: i32, total_frames: i32,
total_points: i32, total_points: i32,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
outfile.seek(SeekFrom::Start(0)); outfile.seek(SeekFrom::Start(0))?;
outfile.write_all(&total_frames.to_be_bytes())?; outfile.write_all(&total_frames.to_be_bytes())?;
outfile.write_all(&total_points.to_be_bytes())?; outfile.write_all(&total_points.to_be_bytes())?;
Ok(()) Ok(())
} }
fn read(mut infile: File) -> Result<MDDSeekableFileReader, Box<dyn Error>> { }
let (total_frames, total_points) = MDDSeekableFile::read_header(&mut infile)?;
let time_byte_offset = (total_frames as usize) * size_of::<f32>();
let mut buff = [0u8; size_of::<f32>()]; #[derive(Debug)]
let times = Box::new( struct ExaustedPointsUnexpectedly;
repeat(()) impl Display for ExaustedPointsUnexpectedly {
.map::<Result<_, Box<dyn Error>>, _>(move |()| { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//times.read_exact(&mut buff)?; write!(f, "ExaustedPointsUnexpectedly")
//Ok(f32::from_be_bytes(buff)) }
Ok(0.0) }
}) impl Error for ExaustedPointsUnexpectedly {}
.take(total_frames.try_into()?),
); impl PointCache for MDDSeekableFile {
type Frame = MDDFrame;
fn read(
mut infile: File,
) -> Result<Box<dyn Iterator<Item = Result<MDDFrame, Box<dyn Error>>>>, Box<dyn Error>> {
let (total_frames, total_points) = MDDSeekableFile::read_header(&mut infile)?;
let header_size: u64 = (size_of::<i32>() * 2).try_into()?; let header_size: u64 = (size_of::<i32>() * 2).try_into()?;
let f32_size: u64 = size_of::<f32>().try_into()?; let f32_size: u64 = size_of::<f32>().try_into()?;
@ -112,40 +111,7 @@ impl MDDSeekableFile {
}, },
), ),
); );
Ok(data)
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<F>(infile: File, mut op: F) -> Result<(), Box<dyn Error>>
where
F: FnMut(MDDFrame) -> Result<(), Box<dyn Error>>,
{
let MDDSeekableFileReader {
total_frames: _,
total_points: _,
data,
} = MDDSeekableFile::read(infile)?;
for frame in data {
op(frame?)?;
}
Ok(())
} }
fn map_to<F>(mut infile: File, mut outfile: File, mut op: F) -> Result<(), Box<dyn Error>> fn map_to<F>(mut infile: File, mut outfile: File, mut op: F) -> Result<(), Box<dyn Error>>
@ -159,8 +125,8 @@ impl PointCache for MDDSeekableFile {
let f32_size: u64 = size_of::<f32>().try_into()?; let f32_size: u64 = size_of::<f32>().try_into()?;
let mut count = 0; let mut count = 0;
Self::map(infile, |frame| { for frame in Self::read(infile)? {
let frame = op(frame)?; let frame = op(frame?)?;
let frame_idx: u64 = count / (total_points as u64); let frame_idx: u64 = count / (total_points as u64);
let time_offset: u64 = f32_size * frame_idx; let time_offset: u64 = f32_size * frame_idx;
let time_size: u64 = f32_size * (total_frames as u64); 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())?; outfile.write_all(&frame.point.z.to_be_bytes())?;
count += 1; count += 1;
Ok(()) }
})?;
Ok(()) Ok(())
} }
} }
@ -204,10 +170,9 @@ mod tests {
})?; })?;
println!("\nReading back output.mdd"); println!("\nReading back output.mdd");
let infile = File::open("output.mdd")?; let infile = File::open("output.mdd")?;
MDDSeekableFile::map(infile, |frame| { for frame in MDDSeekableFile::read(infile)? {
println!("{:?}", frame); println!("{:?}", frame);
Ok(()) }
})?;
Ok(()) Ok(())
} }
} }

7
tools/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "tools"
version = "0.1.0"
edition = "2021"
[dependencies]
pointcache = { path = ".." }

29
tools/src/main.rs Normal file
View File

@ -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();
}