parry3d_f64/transformation/
wavefront.rs

1use crate::shape::TriMesh;
2use alloc::{string::ToString, vec};
3use obj::{Group, IndexTuple, ObjData, ObjError, Object, SimplePolygon};
4use std::path::PathBuf;
5
6impl TriMesh {
7    /// Outputs a Wavefront (`.obj`) file at the given path.
8    ///
9    /// This function is enabled by the `wavefront` feature flag.
10    pub fn to_obj_file(&self, path: &PathBuf) -> Result<(), ObjError> {
11        let mut file = std::fs::File::create(path).unwrap();
12
13        ObjData {
14            #[expect(clippy::unnecessary_cast)]
15            position: self
16                .vertices()
17                .iter()
18                .map(|v| [v.x as f32, v.y as f32, v.z as f32])
19                .collect(),
20            objects: vec![Object {
21                groups: vec![Group {
22                    polys: self
23                        .indices()
24                        .iter()
25                        .map(|tri| {
26                            SimplePolygon(vec![
27                                IndexTuple(tri[0] as usize, None, None),
28                                IndexTuple(tri[1] as usize, None, None),
29                                IndexTuple(tri[2] as usize, None, None),
30                            ])
31                        })
32                        .collect(),
33                    name: "".to_string(),
34                    index: 0,
35                    material: None,
36                }],
37                name: "".to_string(),
38            }],
39            ..Default::default()
40        }
41        .write_to_buf(&mut file)
42    }
43}