epoint_io/e57/
read.rs

1use crate::Error;
2use crate::Error::{InvalidFileExtension, NoFileExtension};
3use crate::e57::FILE_EXTENSION_E57_FORMAT;
4use crate::e57::read_impl::import_point_cloud_from_e57_file;
5use ecoord::FrameId;
6use epoint_core::PointCloud;
7use std::fmt::Debug;
8use std::fs::File;
9use std::io::{Read, Seek};
10use std::path::Path;
11
12/// `E57Reader` imports a point cloud from a E57 file.
13///
14#[derive(Debug, Clone)]
15pub struct E57Reader<R: Read + Seek> {
16    reader: R,
17    reference_frame_id: FrameId,
18    sensor_frame_id: FrameId,
19}
20
21impl<R: Read + Seek> E57Reader<R> {
22    pub fn new(reader: R) -> Self {
23        Self {
24            reader,
25            reference_frame_id: FrameId::global(),
26            sensor_frame_id: FrameId::sensor(),
27        }
28    }
29
30    pub fn finish(self) -> Result<PointCloud, Error> {
31        let point_cloud = import_point_cloud_from_e57_file(
32            self.reader,
33            self.reference_frame_id,
34            self.sensor_frame_id,
35        )?;
36
37        Ok(point_cloud)
38    }
39}
40
41impl E57Reader<File> {
42    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
43        let extension = path.as_ref().extension().ok_or(NoFileExtension())?;
44        if extension != FILE_EXTENSION_E57_FORMAT {
45            return Err(InvalidFileExtension(
46                extension.to_str().unwrap_or_default().to_string(),
47            ));
48        }
49
50        let file = File::open(path)?;
51        Ok(Self::new(file))
52    }
53}