epoint_io/auto/
read.rs

1use crate::Error::{FormatNotSupported, InvalidFileExtension};
2use crate::format::PointCloudFormat;
3use crate::{E57Reader, EpointReader, Error, LasReader, XyzReader};
4use epoint_core::PointCloud;
5use std::path::{Path, PathBuf};
6
7/// `AutoReader` sets up a reader that automatically determines format and reads the point cloud
8/// file.
9///
10#[derive(Debug, Clone)]
11pub struct AutoReader {
12    path: PathBuf,
13    format: PointCloudFormat,
14}
15
16impl AutoReader {
17    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
18        let file_path = path.as_ref().to_path_buf();
19
20        let format = PointCloudFormat::from_path(&file_path).ok_or(InvalidFileExtension(
21            file_path
22                .extension()
23                .unwrap_or_default()
24                .to_string_lossy()
25                .to_string(),
26        ))?;
27
28        Ok(Self {
29            path: file_path,
30            format,
31        })
32    }
33
34    pub fn finish(&self) -> Result<PointCloud, Error> {
35        match self.format {
36            PointCloudFormat::Epoint => EpointReader::from_path(&self.path)?.finish(),
37            PointCloudFormat::EpointTar => EpointReader::from_path(&self.path)?.finish(),
38            PointCloudFormat::E57 => E57Reader::from_path(&self.path)?.finish(),
39            PointCloudFormat::Las => Ok(LasReader::from_path(&self.path)?.finish()?.0),
40            PointCloudFormat::Laz => Ok(LasReader::from_path(&self.path)?.finish()?.0),
41            PointCloudFormat::Xyz => XyzReader::from_path(&self.path)?.finish(),
42            PointCloudFormat::XyzZst => Err(FormatNotSupported(
43                "XyzZst not supported for writing".to_string(),
44            )),
45        }
46    }
47}