use std::io::{BufRead, Read};
use std::path::Path;
use super::{properties::PlyType, PlyError, PlyPropertyTrait};
use crate::pointcloud::PointCloud;
pub fn read_ply_binary(path: impl AsRef<Path>, property: PlyType) -> Result<PointCloud, PlyError> {
let file = std::fs::File::open(path)?;
let mut reader = std::io::BufReader::new(file);
let mut header = String::new();
loop {
let mut line = String::new();
reader.read_line(&mut line)?;
if line.starts_with("end_header") {
header.push_str(&line);
break;
}
header.push_str(&line);
}
let mut buffer = vec![0u8; property.size_of()];
let mut points = Vec::new();
let mut colors = Vec::new();
let mut normals = Vec::new();
while reader.read_exact(&mut buffer).is_ok() {
let property_entry = property.deserialize(&buffer)?;
points.push(property_entry.to_point());
colors.push(property_entry.to_color());
normals.push(property_entry.to_normal());
}
Ok(PointCloud::new(points, Some(colors), Some(normals)))
}