Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions splashsurf/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::{anyhow, Context};
use log::info;
use splashsurf_lib::mesh::{Mesh3d, MeshWithData, TriMesh3d};
use splashsurf_lib::nalgebra::Vector3;
use splashsurf_lib::profile;
use splashsurf_lib::Real;
use splashsurf_lib::{
mesh::{Mesh3d, MeshWithData, TriMesh3d},
vtkio::model::DataSet,
};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
Expand Down Expand Up @@ -166,11 +169,14 @@ pub fn read_surface_mesh<R: Real, P: AsRef<Path>>(
}

/// Writes a mesh to the given file path, automatically detects the file format
pub fn write_mesh<R: Real, M: Mesh3d<R>, P: AsRef<Path>>(
mesh: &MeshWithData<R, M>,
pub fn write_mesh<'a, R: Real, MeshT: Mesh3d<R>, P: AsRef<Path>>(
mesh: &'a MeshWithData<R, MeshT>,
output_file: P,
_format_params: &OutputFormatParameters,
) -> Result<(), anyhow::Error> {
) -> Result<(), anyhow::Error>
where
&'a MeshWithData<R, MeshT>: Into<DataSet>,
{
let output_file = output_file.as_ref();
info!(
"Writing mesh with {} vertices and {} cells to \"{}\"...",
Expand All @@ -187,11 +193,27 @@ pub fn write_mesh<R: Real, M: Mesh3d<R>, P: AsRef<Path>>(
.ok_or(anyhow!("Invalid extension of output file"))?;

match extension.to_lowercase().as_str() {
"obj" => obj_format::mesh_to_obj(mesh, &output_file)?,
"vtk" => {
vtk_format::write_vtk(mesh, &output_file, "mesh").with_context(|| {
format!(
"Failed to write reconstructed surface to output file '{}'",
output_file.to_string_lossy()
)
})?;
}
"obj" => {
obj_format::mesh_to_obj(mesh, &output_file).with_context(|| {
format!(
"Failed to write reconstructed surface to output file '{}'",
output_file.to_string_lossy()
)
})?;
}
_ => {
return Err(anyhow!(
"Unsupported file format extension \"{}\" for writing meshes",
extension
"Unsupported file format extension \"{}\" for writing mesh to output file '{}'",
extension,
output_file.to_string_lossy(),
));
}
}
Expand Down
29 changes: 25 additions & 4 deletions splashsurf/src/io/obj_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,35 @@ pub fn mesh_to_obj<R: Real, M: Mesh3d<R>, P: AsRef<Path>>(
.context("Failed to open file handle for writing OBJ file")?;
let mut writer = BufWriter::with_capacity(100000, file);

let mesh = &mesh.mesh;
for v in mesh.vertices() {
let mesh_vertices = &mesh.mesh;

for v in mesh_vertices.vertices() {
write!(&mut writer, "v {} {} {}\n", v.x, v.y, v.z)?;
}

for f in mesh.cells() {
let normals = mesh
.point_attributes
.iter()
.find(|attrib| attrib.name == "normals");

if let Some(normals) = normals {
match &normals.data {
splashsurf_lib::mesh::AttributeData::Vector3Real(normals) => {
for n in normals {
write!(&mut writer, "vn {} {} {}\n", n.x, n.y, n.z)?;
}
}
_ => {}
}
}

for f in mesh_vertices.cells() {
write!(writer, "f")?;
f.try_for_each_vertex(|v| write!(writer, " {}", v + 1))?;
if let Some(_) = normals {
f.try_for_each_vertex(|v| write!(writer, " {}//{}", v + 1, v + 1))?;
} else {
f.try_for_each_vertex(|v| write!(writer, " {}", v + 1))?;
}
write!(writer, "\n")?;
}

Expand Down
11 changes: 5 additions & 6 deletions splashsurf/src/reconstruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,11 @@ pub(crate) fn reconstruction_pipeline_generic<I: Index, R: Real>(
"Writing surface mesh to \"{}\"...",
paths.output_file.to_string_lossy()
);
io::vtk_format::write_vtk(&mesh, &paths.output_file, "mesh").with_context(|| {
format!(
"Failed to write reconstructed surface to output file '{}'",
paths.output_file.to_string_lossy()
)
})?;

let io_params = io::FormatParameters::default();

io::write_mesh(&mesh, paths.output_file.clone(), &io_params.output)?;

info!("Done.");
}

Expand Down