write_stl/
write_stl.rs

1extern crate manifold_rs;
2
3use manifold_rs::output::WriteStl;
4
5fn generate_circle(radius: f64, offset: (f64, f64), segments: usize) -> Vec<f64> {
6    let mut circle = Vec::new();
7    for i in 0..segments {
8        let angle = 2.0 * std::f64::consts::PI * i as f64 / segments as f64;
9        circle.append(&mut vec![
10            radius * angle.cos() + offset.0,
11            radius * angle.sin() + offset.1,
12        ]);
13    }
14    circle
15}
16
17fn main() -> std::io::Result<()> {
18    // Write sphere to an STL file
19    manifold_rs::Manifold::sphere(4.0, 128).write_stl_to_file("sphere.stl")?;
20
21    // Write cylinder to an STL file
22    {
23        let manifold = manifold_rs::Manifold::cylinder(1.0, 4.0, 3.0, 32);
24
25        // Convert the manifold to a mesh and back to a manifold, just for testing
26        let mesh = manifold.to_mesh();
27        let manifold = mesh.to_manifold();
28
29        manifold.write_stl_to_file("cylinder.stl")?;
30    }
31
32    // Generate torus with `revolve` and write resulting mesh to an STL file
33    {
34        // Generate circle with 32 vertices
35        let circle = generate_circle(2.0, (4.0, 0.0), 32);
36
37        // Revolve the circle 360° around the z-axis
38        let manifold = manifold_rs::Manifold::revolve(&[circle.as_slice()], 32, 360.0);
39
40        manifold.write_stl_to_file("torus.stl")?;
41    }
42
43    // Generate a tube via `extrude` and write resulting mesh to an STL file
44    {
45        // Generate circle with 32 vertices
46        let inner_circle = generate_circle(0.3, (0.0, 0.0), 32);
47        let outer_circle = generate_circle(1.0, (0.0, 0.0), 32);
48
49        // CCW winding order to create a hole in the tube
50        let inner_circle = inner_circle
51            .into_iter()
52            .enumerate()
53            .map(|(i, x)| if i % 2 == 0 { x } else { -x })
54            .collect::<Vec<_>>();
55
56        // Extrude the circle along the z-axis
57        let manifold = manifold_rs::Manifold::extrude(
58            &[outer_circle.as_slice(), inner_circle.as_slice()],
59            4.0,
60            16,
61            0.0,
62            1.0,
63            1.0,
64        );
65
66        manifold.write_stl_to_file("tube.stl")?;
67    }
68
69    // Convex hull of two circles
70    {
71        let left_circle = generate_circle(1.0, (-1.0, 0.0), 32);
72        let right_circle = generate_circle(1.0, (1.0, 0.0), 32);
73
74        // Extrude the circle along the z-axis
75        let manifold = manifold_rs::Manifold::extrude(
76            &[left_circle.as_slice(), right_circle.as_slice()],
77            4.0,
78            16,
79            0.0,
80            1.0,
81            1.0,
82        );
83        let manifold = manifold.hull();
84
85        manifold.write_stl_to_file("hull.stl")?;
86    }
87
88    // Trim a cylinder by a plane
89    {
90        let manifold = manifold_rs::Manifold::cylinder(1.0, 1.0, 3.0, 32);
91        let manifold = manifold.trim_by_plane(0.5, 0.5, 0.5, 0.0);
92
93        manifold.write_stl_to_file("cylinder_trimmed.stl")?;
94    }
95    Ok(())
96}