use anyhow::{Context, Result};
use pasture_core::containers::BorrowedBuffer;
use pasture_core::containers::MakeBufferFromLayout;
use pasture_core::containers::OwningBuffer;
mod reader;
use std::path::Path;
pub use self::reader::*;
mod writer;
pub use self::writer::*;
mod seek;
pub use self::seek::*;
mod io_factory;
pub use self::io_factory::*;
pub fn read_all<'a, B: MakeBufferFromLayout<'a> + OwningBuffer<'a> + 'a, P: AsRef<Path>>(
path: P,
) -> Result<B> {
let mut reader = GenericPointReader::open_file(path.as_ref()).context(format!(
"Could not create appropriate reader for point cloud file {}",
path.as_ref().display()
))?;
let num_points = reader.point_count().unwrap_or(usize::MAX);
reader.read::<B>(num_points)
}
pub fn read_all_into<'a, B: OwningBuffer<'a>, P: AsRef<Path>>(
buffer: &'a mut B,
path: P,
) -> Result<usize> {
let mut reader = GenericPointReader::open_file(path.as_ref()).context(format!(
"Could not create appropriate reader for point cloud file {}",
path.as_ref().display()
))?;
let num_points = reader.point_count().context(format!(
"Could not determine number of points in point cloud file {}",
path.as_ref().display()
))?;
reader.read_into(buffer, num_points)
}
pub fn write_all<'a, B: BorrowedBuffer<'a>, P: AsRef<Path>>(buffer: &'a B, path: P) -> Result<()> {
let mut writer =
GenericPointWriter::open_file(path.as_ref(), buffer.point_layout()).context(format!(
"Could not create appropriate writer for point cloud file {}",
path.as_ref().display()
))?;
writer.write(buffer).context(format!(
"Failed to write points to file {}",
path.as_ref().display()
))?;
writer.flush().context(format!(
"Failed to flush points to file {}",
path.as_ref().display()
))?;
Ok(())
}