Simplify parameters of export functions

This commit is contained in:
Hanno Braun 2025-04-11 14:00:15 +02:00
parent d6c0de415b
commit 016608d8eb
2 changed files with 13 additions and 15 deletions

View File

@ -16,7 +16,7 @@ use std::{
use thiserror::Error; use thiserror::Error;
use fj_interop::{MeshTriangle, vertices_to_indexed_vertices}; use fj_interop::vertices_to_indexed_vertices;
use fj_math::Triangle; use fj_math::Triangle;
/// # Export the provided mesh to the file at the given path /// # Export the provided mesh to the file at the given path
@ -26,8 +26,8 @@ use fj_math::Triangle;
/// ///
/// Currently 3MF & STL file types are supported. The case insensitive file /// Currently 3MF & STL file types are supported. The case insensitive file
/// extension of the provided path is used to switch between supported types. /// extension of the provided path is used to switch between supported types.
pub fn export<'r>( pub fn export(
triangles: impl IntoIterator<Item = &'r MeshTriangle>, triangles: impl IntoIterator<Item = Triangle<3>>,
path: impl AsRef<Path>, path: impl AsRef<Path>,
) -> Result<(), Error> { ) -> Result<(), Error> {
match path.as_ref().extension() { match path.as_ref().extension() {
@ -51,14 +51,12 @@ pub fn export<'r>(
} }
/// # Export the provided mesh to the provided writer in the 3MF format /// # Export the provided mesh to the provided writer in the 3MF format
pub fn export_3mf<'r>( pub fn export_3mf(
triangles: impl IntoIterator<Item = &'r MeshTriangle>, triangles: impl IntoIterator<Item = Triangle<3>>,
write: impl Write + Seek, write: impl Write + Seek,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (vertices, indices) = vertices_to_indexed_vertices( let (vertices, indices) = vertices_to_indexed_vertices(
triangles triangles.into_iter().flat_map(|triangle| triangle.points),
.into_iter()
.flat_map(|triangle| triangle.inner.points),
|point| threemf::model::Vertex { |point| threemf::model::Vertex {
x: point.x.into_f64(), x: point.x.into_f64(),
y: point.y.into_f64(), y: point.y.into_f64(),
@ -88,13 +86,13 @@ pub fn export_3mf<'r>(
} }
/// # Export the provided mesh to the provided writer in the STL format /// # Export the provided mesh to the provided writer in the STL format
pub fn export_stl<'r>( pub fn export_stl(
triangles: impl IntoIterator<Item = &'r MeshTriangle>, triangles: impl IntoIterator<Item = Triangle<3>>,
mut write: impl Write, mut write: impl Write,
) -> Result<(), Error> { ) -> Result<(), Error> {
let points = triangles let points = triangles
.into_iter() .into_iter()
.map(|triangle| triangle.inner.points) .map(|triangle| triangle.points)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let vertices = points.iter().map(|points| { let vertices = points.iter().map(|points| {
@ -135,13 +133,13 @@ pub fn export_stl<'r>(
} }
/// # Export the provided mesh to the provided writer in the OBJ format /// # Export the provided mesh to the provided writer in the OBJ format
pub fn export_obj<'r>( pub fn export_obj(
triangles: impl IntoIterator<Item = &'r MeshTriangle>, triangles: impl IntoIterator<Item = Triangle<3>>,
mut write: impl Write, mut write: impl Write,
) -> Result<(), Error> { ) -> Result<(), Error> {
for (cnt, t) in triangles.into_iter().enumerate() { for (cnt, t) in triangles.into_iter().enumerate() {
// write each point of the triangle // write each point of the triangle
for v in t.inner.points { for v in t.points {
wavefront_rs::obj::writer::Writer { auto_newline: true } wavefront_rs::obj::writer::Writer { auto_newline: true }
.write( .write(
&mut write, &mut write,

View File

@ -84,7 +84,7 @@ impl Instance {
let tri_mesh = (model, tolerance).triangulate(&mut self.core); let tri_mesh = (model, tolerance).triangulate(&mut self.core);
if let Some(path) = args.export { if let Some(path) = args.export {
export::export(tri_mesh.triangles.iter(), &path)?; export::export(tri_mesh.all_triangles(), &path)?;
return Ok(()); return Ok(());
} }