1use crate::Error::{FormatNotSupported, InvalidFileExtension};
2use crate::format::PointCloudFormat;
3use crate::{EpointWriter, Error, LasWriter, XyzWriter};
4use epoint_core::PointCloud;
5use std::path::{Path, PathBuf};
6
7#[derive(Debug, Clone)]
11pub struct AutoWriter {
12 path: PathBuf,
13 format: PointCloudFormat,
14}
15
16impl AutoWriter {
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 from_base_path_with_format(
35 base_path: impl AsRef<Path>,
36 format: PointCloudFormat,
37 ) -> Result<Self, Error> {
38 let base_path = base_path.as_ref();
39 let extension = format.extension();
40
41 let mut path_string = base_path.to_string_lossy().into_owned();
43 path_string.push('.');
44 path_string.push_str(extension);
45
46 Ok(Self {
47 path: PathBuf::from(path_string),
48 format,
49 })
50 }
51
52 pub fn finish(self, point_cloud: PointCloud) -> Result<(), Error> {
53 match self.format {
54 PointCloudFormat::Epoint => EpointWriter::from_path(self.path)?.finish(point_cloud),
55 PointCloudFormat::EpointTar => EpointWriter::from_path(self.path)?
56 .with_compressed(false)
57 .finish(point_cloud),
58 PointCloudFormat::E57 => Err(FormatNotSupported(
59 "E57 not supported for reading".to_string(),
60 )),
61 PointCloudFormat::Las => LasWriter::from_path(self.path)?.finish(&point_cloud),
62 PointCloudFormat::Laz => LasWriter::from_path(self.path)?.finish(&point_cloud),
63 PointCloudFormat::Xyz => XyzWriter::from_path(self.path)?
64 .with_compressed(false)
65 .finish(&point_cloud),
66 PointCloudFormat::XyzZst => XyzWriter::from_path(self.path)?.finish(&point_cloud),
67 }
68 }
69}