use std::io::Write;
use std::path::Path;
use crate::error::{Error, Result};
use crate::mesh::Mesh;
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct PlyOptions {
pub comments: Vec<String>,
pub normals: bool,
pub vertex_colors: Vec<[f32; 3]>,
}
impl PlyOptions {
pub fn new() -> Self {
Self {
normals: true,
..Self::default()
}
}
}
pub fn write_ply<W: Write>(w: W, mesh: &Mesh, options: &PlyOptions) -> Result<()> {
write_impl(w, mesh, options).map_err(|e| match e {
Failure::Io(source) => Error::Io { path: None, source },
Failure::Lib(e) => e,
})
}
pub fn write_ply_path(path: impl AsRef<Path>, mesh: &Mesh, options: &PlyOptions) -> Result<()> {
let path = path.as_ref();
let io = |source| Error::Io {
path: Some(path.to_path_buf()),
source,
};
let mut w = super::Sink::create(path)?;
write_impl(&mut w, mesh, options)
.and_then(|()| w.finish().map_err(Failure::Io))
.map_err(|e| match e {
Failure::Io(source) => io(source),
Failure::Lib(e) => e,
})
}
enum Failure {
Io(std::io::Error),
Lib(Error),
}
impl From<std::io::Error> for Failure {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
fn write_impl<W: Write>(mut w: W, m: &Mesh, o: &PlyOptions) -> Result<(), Failure> {
let n = m.positions.len();
let normals = o.normals && !m.normals.is_empty();
let colors = !o.vertex_colors.is_empty();
let components = !m.vertex_component.is_empty();
let lengths = [
(normals, m.normals.len(), "normals"),
(colors, o.vertex_colors.len(), "vertex colours"),
(true, m.vertex_atom.len(), "vertex atoms"),
(true, m.vertex_patch.len(), "vertex patches"),
(components, m.vertex_component.len(), "vertex components"),
];
for (used, len, what) in lengths {
if used && len != n {
return Err(Failure::Lib(Error::InvalidInput(format!(
"{len} {what} for {n} vertices"
))));
}
}
let clean = |s: &str| s.replace(['\n', '\r'], " ");
let mut h = String::from("ply\nformat binary_little_endian 1.0\n");
h += &format!("comment sesmesh {}\n", crate::VERSION);
for c in &o.comments {
h += &format!("comment {}\n", clean(c));
}
h += &format!("element vertex {n}\n");
for p in ["x", "y", "z"] {
h += &format!("property float {p}\n");
}
if normals {
for p in ["nx", "ny", "nz"] {
h += &format!("property float {p}\n");
}
}
if colors {
for p in ["red", "green", "blue"] {
h += &format!("property uchar {p}\n");
}
}
h += "property uint atom\nproperty uchar patch\n";
if components {
h += "property uint component\n";
}
h += &format!(
"element face {}\nproperty list uchar uint vertex_indices\nend_header\n",
m.triangles.len()
);
w.write_all(h.as_bytes())?;
let mut buf = Vec::with_capacity(1 << 16);
for i in 0..n {
for c in m.positions[i] {
buf.extend_from_slice(&c.to_le_bytes());
}
if normals {
for c in m.normals[i] {
buf.extend_from_slice(&c.to_le_bytes());
}
}
if colors {
for c in o.vertex_colors[i] {
buf.push((c.clamp(0.0, 1.0) * 255.0).round() as u8);
}
}
buf.extend_from_slice(&m.vertex_atom[i].to_le_bytes());
buf.push(m.vertex_patch[i] as u8);
if components {
buf.extend_from_slice(&m.vertex_component[i].to_le_bytes());
}
if buf.len() >= 1 << 16 {
w.write_all(&buf)?;
buf.clear();
}
}
for t in &m.triangles {
buf.push(3);
for v in t {
buf.extend_from_slice(&v.to_le_bytes());
}
if buf.len() >= 1 << 16 {
w.write_all(&buf)?;
buf.clear();
}
}
w.write_all(&buf)?;
Ok(())
}