epoint_io/las/
read.rs

1use crate::las::read_impl::import_point_cloud_from_las_reader;
2use crate::{Error, FILE_EXTENSION_LAS_FORMAT, FILE_EXTENSION_LAZ_FORMAT};
3
4use crate::las::LasVersion;
5use epoint_core::PointCloud;
6
7use crate::Error::{InvalidFileExtension, NoFileExtension};
8use std::fmt::Debug;
9use std::fs::File;
10use std::io::{Read, Seek};
11use std::path::Path;
12
13/// `LasReader` imports a point cloud from a LAS or LAZ file.
14///
15#[derive(Debug, Clone)]
16pub struct LasReader<R: Read + Seek + Send + Debug> {
17    reader: R,
18    normalize_colors: bool,
19}
20
21impl<R: Read + Seek + Send + 'static + Debug> LasReader<R> {
22    /// Create a new [`LasReader`] from an existing `Reader`.
23    pub fn new(reader: R) -> Self {
24        Self {
25            reader,
26            normalize_colors: false,
27        }
28    }
29
30    pub fn normalize_colors(mut self, normalize_colors: bool) -> Self {
31        self.normalize_colors = normalize_colors;
32        self
33    }
34
35    pub fn finish(self) -> Result<(PointCloud, LasReadInfo), Error> {
36        let point_cloud = import_point_cloud_from_las_reader(self.reader, self.normalize_colors)?;
37        Ok(point_cloud)
38    }
39}
40
41impl LasReader<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_LAS_FORMAT && extension != FILE_EXTENSION_LAZ_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}
54
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct LasReadInfo {
57    pub version: LasVersion,
58}