conspire/geometry/mesh/tessellation/write/
mod.rs1#[cfg(test)]
2mod test;
3
4use crate::{
5 geometry::mesh::{Connectivity, tessellation::Tessellation},
6 io::Write,
7 math::Tensor,
8};
9use std::{
10 fs::File,
11 io::{BufWriter, Error as ErrorIO, Write as WriteIO},
12 path::Path,
13};
14
15impl<P> Write<P> for Tessellation
16where
17 P: AsRef<Path>,
18{
19 type Error = ErrorIO;
20 fn write(&self, path: P) -> Result<(), Self::Error> {
21 let mut writer = BufWriter::new(File::create(path)?);
22 writer.write_all(&[0_u8; 80])?;
23 writer.write_all(&(self.mesh.number_of_elements() as u32).to_le_bytes())?;
24 self.mesh
25 .connectivities()
26 .iter()
27 .zip(self.normals.iter())
28 .try_for_each(|(connectivity, normals)| match connectivity {
29 Connectivity::Triangular(triangles) => triangles
30 .iter()
31 .zip(normals.iter())
32 .try_for_each(|(nodes, normal)| {
33 normal.iter().try_for_each(|&component| {
34 writer.write_all(&(component as f32).to_le_bytes())
35 })?;
36 nodes.iter().try_for_each(|&node| {
37 self.mesh.coordinates()[node]
38 .iter()
39 .try_for_each(|&coordinate| {
40 writer.write_all(&(coordinate as f32).to_le_bytes())
41 })
42 })?;
43 writer.write_all(&0_u16.to_le_bytes())
44 }),
45 _ => panic!("STL only supports triangular blocks"),
46 })?;
47 writer.flush()
48 }
49}