MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/splashsurf / mesh_to_obj

Function mesh_to_obj

splashsurf_lib/src/io/obj_format.rs:17–71  ·  view source on GitHub ↗

Writes the given mesh to an OBJ file, supports outputting normals

(
    mesh: &MeshWithData<R, M>,
    filename: P,
)

Source from the content-addressed store, hash-verified

15
16/// Writes the given mesh to an OBJ file, supports outputting normals
17pub fn mesh_to_obj<R: Real, M: Mesh3d<R>, P: AsRef<Path>>(
18 mesh: &MeshWithData<R, M>,
19 filename: P,
20) -> Result<(), anyhow::Error> {
21 profile!("mesh_to_obj");
22 let file = fs::OpenOptions::new()
23 .read(true)
24 .write(true)
25 .create(true)
26 .truncate(true)
27 .open(filename)
28 .context("Failed to open file handle for writing OBJ file")?;
29 let mut writer = BufWriter::with_capacity(100000, file);
30
31 let mesh_vertices = &mesh.mesh;
32
33 for v in mesh_vertices.vertices() {
34 writeln!(&mut writer, "v {} {} {}", v.x, v.y, v.z)?;
35 }
36
37 let normals = mesh
38 .point_attributes
39 .iter()
40 .find(|attrib| attrib.name == "normals");
41
42 if let Some(normals) = normals {
43 if let OwnedAttributeData::Vector3Real(normals) = &normals.data {
44 for n in normals.iter() {
45 writeln!(&mut writer, "vn {} {} {}", n.x, n.y, n.z)?;
46 }
47 }
48 }
49
50 if normals.is_some() {
51 for f in mesh_vertices.cells() {
52 write!(writer, "f")?;
53 f.vertices()
54 .iter()
55 .copied()
56 .try_for_each(|v| write!(writer, " {}//{}", v + 1, v + 1))?;
57 writeln!(writer)?;
58 }
59 } else {
60 for f in mesh_vertices.cells() {
61 write!(writer, "f")?;
62 f.vertices()
63 .iter()
64 .copied()
65 .try_for_each(|v| write!(writer, " {}", v + 1))?;
66 writeln!(writer)?;
67 }
68 }
69
70 Ok(())
71}
72
73pub fn surface_mesh_from_obj<R: Real, P: AsRef<Path>>(
74 obj_path: P,

Callers 3

test_halfedge_icoFunction · 0.85
test_halfedge_planeFunction · 0.85
write_meshFunction · 0.85

Calls 4

writeMethod · 0.80
cellsMethod · 0.80
verticesMethod · 0.45
iterMethod · 0.45

Tested by 2

test_halfedge_icoFunction · 0.68
test_halfedge_planeFunction · 0.68