From 47649dfff56ebf82017daf962a75707913a004f0 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 2 Feb 2024 14:18:58 -0500 Subject: [PATCH] Support half2 reading/writing --- src/model_file_operations.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/model_file_operations.rs b/src/model_file_operations.rs index 182c595..e2b57e0 100644 --- a/src/model_file_operations.rs +++ b/src/model_file_operations.rs @@ -62,6 +62,19 @@ impl MDL { f16::from_f32(vec[3]).to_bits()]) } + pub(crate) fn read_half2(cursor: &mut Cursor) -> Option<[f32; 2]> { + Some([ + f16::from_bits(cursor.read_le::().ok()?).to_f32(), + f16::from_bits(cursor.read_le::().ok()?).to_f32() + ]) + } + + pub(crate) fn write_half2(cursor: &mut T, vec: &[f32; 2]) -> BinResult<()> { + cursor.write_le::<[u16; 2]>(&[ + f16::from_f32(vec[0]).to_bits(), + f16::from_f32(vec[1]).to_bits()]) + } + pub(crate) fn read_uint(cursor: &mut Cursor) -> BinResult<[u8; 4]> { cursor.read_le::<[u8; 4]>() } @@ -134,6 +147,19 @@ mod tests { assert_eq!(MDL::read_half4(&mut read_cursor).unwrap(), a); } + #[test] + fn half2() { + let a = [0.0, 1.0]; + + let mut v = vec![]; + let mut cursor = Cursor::new(&mut v); + + MDL::write_half2(&mut cursor, &a).unwrap(); + + let mut read_cursor = Cursor::new(v.as_slice()); + assert_eq!(MDL::read_half2(&mut read_cursor).unwrap(), a); + } + #[test] fn uint() { let a = [5u8, 0u8, 3u8, 15u8]; @@ -187,7 +213,6 @@ mod tests { assert_delta!(tangent, a, 0.001); } - #[test] fn pad_slice() { let a = [3.0, 0.0, -1.0];