use crate::geom3::Aabb3;
use crate::{MeshData3, Point3, Result};
pub fn u_mesh_data_to_bytes(mesh: &MeshData3, allow_attribute_loss: bool) -> Result<Vec<u8>> {
mesh.check_attribute_loss("the micro mesh format", allow_attribute_loss)?;
u_buffers_to_bytes(mesh.points(), mesh.faces())
}
pub fn u_bytes_to_mesh_data(bytes: &[u8]) -> Result<MeshData3> {
let (points, faces) = u_bytes_to_buffers(bytes)?;
MeshData3::new(points, faces)
}
pub fn u_bytes_to_buffers(bytes: &[u8]) -> Result<(Vec<Point3>, Vec<[u32; 3]>)> {
let mut reader = ByteRead::new(bytes);
let min = Point3::new(reader.read_f64(), reader.read_f64(), reader.read_f64());
let max = Point3::new(reader.read_f64(), reader.read_f64(), reader.read_f64());
let vertex_count = reader.read_u16() as usize;
let mut vertices = Vec::with_capacity(vertex_count);
for _ in 0..vertex_count {
let x = min.x + (f64::from(reader.read_u16()) / u16::MAX as f64) * (max.x - min.x);
let y = min.y + (f64::from(reader.read_u16()) / u16::MAX as f64) * (max.y - min.y);
let z = min.z + (f64::from(reader.read_u16()) / u16::MAX as f64) * (max.z - min.z);
vertices.push(Point3::new(x, y, z));
}
let triangle_count = reader.read_u32() as usize;
let mut triangles = Vec::with_capacity(triangle_count);
for _ in 0..triangle_count {
triangles.push([
reader.read_u16() as u32,
reader.read_u16() as u32,
reader.read_u16() as u32,
]);
}
Ok((vertices, triangles))
}
pub fn u_buffers_to_bytes(vertices: &[Point3], triangles: &[[u32; 3]]) -> Result<Vec<u8>> {
if vertices.len() > u16::MAX as usize {
return Err("Mesh has too many vertices for the small format".into());
}
let mut output = Vec::new();
let bounds = Aabb3::from_points_ref(vertices);
output.extend_from_slice(&bounds.mins.x.to_le_bytes());
output.extend_from_slice(&bounds.mins.y.to_le_bytes());
output.extend_from_slice(&bounds.mins.z.to_le_bytes());
output.extend_from_slice(&bounds.maxs.x.to_le_bytes());
output.extend_from_slice(&bounds.maxs.y.to_le_bytes());
output.extend_from_slice(&bounds.maxs.z.to_le_bytes());
output.extend_from_slice(&(vertices.len() as u16).to_le_bytes());
for p in vertices {
output.extend_from_slice(&to_u16(p.x, bounds.mins.x, bounds.maxs.x).to_le_bytes());
output.extend_from_slice(&to_u16(p.y, bounds.mins.y, bounds.maxs.y).to_le_bytes());
output.extend_from_slice(&to_u16(p.z, bounds.mins.z, bounds.maxs.z).to_le_bytes());
}
output.extend_from_slice(&(triangles.len() as u32).to_le_bytes());
for triangle in triangles {
output.extend_from_slice(&(triangle[0] as u16).to_le_bytes());
output.extend_from_slice(&(triangle[1] as u16).to_le_bytes());
output.extend_from_slice(&(triangle[2] as u16).to_le_bytes());
}
Ok(output)
}
fn to_u16(value: f64, min: f64, max: f64) -> u16 {
let range = max - min;
let scale = u16::MAX as f64 / range;
((value - min) * scale).round() as u16
}
struct ByteRead<'a> {
bytes: &'a [u8],
offset: usize,
}
impl<'a> ByteRead<'a> {
fn new(bytes: &'a [u8]) -> Self {
ByteRead { bytes, offset: 0 }
}
fn read_f64(&mut self) -> f64 {
let value =
f64::from_le_bytes(self.bytes[self.offset..self.offset + 8].try_into().unwrap());
self.offset += 8;
value
}
fn read_u16(&mut self) -> u16 {
let value =
u16::from_le_bytes(self.bytes[self.offset..self.offset + 2].try_into().unwrap());
self.offset += 2;
value
}
fn read_u32(&mut self) -> u32 {
let value =
u32::from_le_bytes(self.bytes[self.offset..self.offset + 4].try_into().unwrap());
self.offset += 4;
value
}
}