pub mod ascii;
pub mod geotiff;
pub mod las;
pub mod mesh;
pub mod ply;
pub mod rdb;
pub mod mta;
pub mod mta_model;
pub mod riegl;
pub mod rxp_mta;
pub mod shots;
use std::path::Path;
use crate::error::{Error, Result};
use crate::PointCloud;
fn ext(path: &Path) -> String {
path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase()
}
pub fn read(path: impl AsRef<Path>) -> Result<PointCloud> {
let path = path.as_ref();
match ext(path).as_str() {
"las" | "laz" => las::read_las(path),
"ply" => ply::read_ply(path),
"xyz" | "txt" | "csv" | "asc" | "pts" => ascii::read_ascii(path, None),
"rxp" => riegl::read_rxp(path, &riegl::RxpOptions::default()),
"rdbx" => rdb::read_rdbx(path, &rdb::RdbxOptions::default()),
other => Err(Error::UnsupportedFormat(other.to_string())),
}
}
pub fn read_attrs(path: impl AsRef<Path>, keep: Option<&[&str]>) -> Result<PointCloud> {
let path = path.as_ref();
if matches!(ext(path).as_str(), "las" | "laz") {
return las::read_las_attrs(path, keep);
}
let mut cloud = read(path)?;
if let Some(keep) = keep {
cloud.attrs.retain(|k, _| keep.contains(&k.as_str()));
}
Ok(cloud)
}
pub fn write_annotated(src: impl AsRef<Path>, path: impl AsRef<Path>, opts: &WriteOptions, extra: &std::collections::BTreeMap<String, crate::pointcloud::Attr>, lo: Option<crate::Point>) -> Result<()> {
let (src, path) = (src.as_ref(), path.as_ref());
match (ext(src).as_str(), ext(path).as_str()) {
("las" | "laz", "las" | "laz") => las::write_las_annotated(src, path, &las::LasWriteOptions { point_format: opts.point_format, scale: opts.scale, crs_wkt: opts.crs_wkt.clone() }, extra, lo),
("las" | "laz", other) | (other, _) => Err(Error::UnsupportedFormat(other.to_string())),
}
}
#[derive(Debug, Clone)]
pub struct WriteOptions {
pub point_format: u8,
pub scale: f64,
pub binary: bool,
pub crs_wkt: Option<String>,
}
impl Default for WriteOptions {
fn default() -> Self {
let las = las::LasWriteOptions::default();
WriteOptions { point_format: las.point_format, scale: las.scale, binary: true, crs_wkt: None }
}
}
pub fn text_decimals(scale: f64) -> usize {
if !(scale > 0.0) || !scale.is_finite() {
return 4;
}
((-scale.log10() - 1e-9).ceil() + 1.0).clamp(0.0, 17.0) as usize
}
pub fn write(cloud: &PointCloud, path: impl AsRef<Path>) -> Result<()> {
write_with(cloud, path, &WriteOptions::default())
}
pub fn write_with(cloud: &PointCloud, path: impl AsRef<Path>, opts: &WriteOptions) -> Result<()> {
let path = path.as_ref();
match ext(path).as_str() {
"las" | "laz" => las::write_las(cloud, path, &las::LasWriteOptions { point_format: opts.point_format, scale: opts.scale, crs_wkt: opts.crs_wkt.clone() }),
"ply" => ply::write_ply(cloud, path, opts.binary),
"xyz" | "txt" | "asc" | "pts" => ascii::write_ascii(cloud, path, " ", true, text_decimals(opts.scale)),
"csv" => ascii::write_ascii(cloud, path, ",", true, text_decimals(opts.scale)),
other => Err(Error::UnsupportedFormat(other.to_string())),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_coordinates_follow_the_scale() {
assert_eq!([0.001, 0.0001, 1e-7, 0.01, 1.0, 0.005].map(text_decimals), [4, 5, 8, 3, 1, 4]);
assert_eq!([0.0, -1.0, f64::NAN, 1e-30].map(text_decimals), [4, 4, 4, 17]);
let cloud = PointCloud::new(vec![[153.0123456789, -27.4987654321, 12.0]]);
let path = std::env::temp_dir().join(format!("sylva-io-text-{}.csv", std::process::id()));
write_with(&cloud, &path, &WriteOptions { scale: 1e-7, ..Default::default() }).unwrap();
let back = read(&path).unwrap();
assert!((back.xyz[0][0] - cloud.xyz[0][0]).abs() < 1e-8 && (back.xyz[0][1] - cloud.xyz[0][1]).abs() < 1e-8);
write(&cloud, &path).unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap().lines().nth(1), Some("153.0123,-27.4988,12.0000"), "metres stay at 0.1 mm");
let _ = std::fs::remove_file(&path);
}
}