Struct Manifold

Source
pub struct Manifold(/* private fields */);
Expand description

Manifold rust wrapper for C++ manifold object.

Implementations§

Source§

impl Manifold

Source

pub fn slice(&self, height: f64) -> Polygons

Slice the manifold into a set of polygons.

Source

pub fn project(&self) -> Polygons

Project the manifold onto a plane and return the resulting polygons.

Source

pub fn trim_by_plane(&self, x: f64, y: f64, z: f64, offset: f64) -> Self

Trim by a plane.

Examples found in repository?
examples/write_stl.rs (line 91)
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}
Source

pub fn hull(&self) -> Self

Convex hull.

Examples found in repository?
examples/write_stl.rs (line 83)
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}
Source

pub fn translate(&self, x: f64, y: f64, z: f64) -> Self

Translate the manifold.

Examples found in repository?
examples/bevy.rs (line 18)
8    fn setup(
9        mut commands: Commands,
10        mut meshes: ResMut<Assets<Mesh>>,
11        mut materials: ResMut<Assets<StandardMaterial>>,
12    ) {
13        let tube = Self::cylinder_manifold(1.0, 3.0)
14            .boolean_op(
15                &Self::cylinder_manifold(0.5, 4.0),
16                manifold_rs::BooleanOp::Difference,
17            )
18            .translate(-1.5, -1.5, 0.0);
19
20        Self::add_manifold(&mut commands, &mut meshes, &mut materials, tube);
21
22        let tube_with_normals = Self::cylinder_manifold(1.0, 3.0)
23            .boolean_op(
24                &Self::cylinder_manifold(0.5, 4.0),
25                manifold_rs::BooleanOp::Difference,
26            )
27            .calculate_normals(0, 50.0)
28            .translate(1.5, 1.5, 0.0);
29
30        Self::add_manifold(
31            &mut commands,
32            &mut meshes,
33            &mut materials,
34            tube_with_normals,
35        );
36    }
More examples
Hide additional examples
examples/write_ply.rs (line 25)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn scale(&self, x: f64, y: f64, z: f64) -> Self

Scale the manifold.

Source

pub fn rotate(&self, x: f64, y: f64, z: f64) -> Self

Rotate the manifold.

Source

pub fn sphere(radius: f64, segments: u32) -> Self

Create a sphere manifold.

Examples found in repository?
examples/write_ply.rs (line 26)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
More examples
Hide additional examples
examples/write_stl.rs (line 19)
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}
Source

pub fn cube(x_size: f64, y_size: f64, z_size: f64) -> Self

Create a cube manifold.

Examples found in repository?
examples/write_ply.rs (line 10)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn cylinder( radius_low: f64, radius_high: f64, height: f64, segments: u32, ) -> Self

Create a cylinder manifold.

Examples found in repository?
examples/bevy.rs (line 70)
69    fn cylinder_manifold(d: f64, h: f64) -> manifold_rs::Manifold {
70        manifold_rs::Manifold::cylinder(d, d, h, (d * 32.0) as u32)
71    }
More examples
Hide additional examples
examples/write_ply.rs (line 42)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
examples/write_stl.rs (line 23)
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}
Source

pub fn union(&self, b: &Self) -> Self

Get the union of two manifolds.

Source

pub fn intersection(&self, b: &Self) -> Self

Get the intersection of two manifolds.

Examples found in repository?
examples/write_ply.rs (line 26)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn difference(&self, b: &Self) -> Self

Get the difference of two manifolds.

Examples found in repository?
examples/write_ply.rs (line 43)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn boolean_op(&self, b: &Self, op: BooleanOp) -> Self

Boolean operation on manifolds.

Examples found in repository?
examples/bevy.rs (lines 14-17)
8    fn setup(
9        mut commands: Commands,
10        mut meshes: ResMut<Assets<Mesh>>,
11        mut materials: ResMut<Assets<StandardMaterial>>,
12    ) {
13        let tube = Self::cylinder_manifold(1.0, 3.0)
14            .boolean_op(
15                &Self::cylinder_manifold(0.5, 4.0),
16                manifold_rs::BooleanOp::Difference,
17            )
18            .translate(-1.5, -1.5, 0.0);
19
20        Self::add_manifold(&mut commands, &mut meshes, &mut materials, tube);
21
22        let tube_with_normals = Self::cylinder_manifold(1.0, 3.0)
23            .boolean_op(
24                &Self::cylinder_manifold(0.5, 4.0),
25                manifold_rs::BooleanOp::Difference,
26            )
27            .calculate_normals(0, 50.0)
28            .translate(1.5, 1.5, 0.0);
29
30        Self::add_manifold(
31            &mut commands,
32            &mut meshes,
33            &mut materials,
34            tube_with_normals,
35        );
36    }
Source

pub fn extrude( multi_polygon_data: &[&[f64]], height: f64, n_divisions: u32, twist_degrees: f64, scale_top_x: f64, scale_top_y: f64, ) -> Self

Extrude a polygon to create a manifold.

Examples found in repository?
examples/write_stl.rs (lines 57-64)
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}
Source

pub fn revolve( multi_polygon_data: &[&[f64]], circular_segments: u32, revolve_degrees: f64, ) -> Self

Revolve a polygon to create a manifold.

Examples found in repository?
examples/write_stl.rs (line 38)
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}
Source

pub fn refine(self: &Manifold, n: i32) -> Self

Refine manifold.

Source

pub fn refine_to_length(self: &Manifold, t: f64) -> Self

Refine manifold to Length.

Examples found in repository?
examples/write_ply.rs (line 30)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn refine_to_tolerance(self: &Manifold, t: f64) -> Self

Refine to tolerance.

Source

pub fn smooth_by_normals(self: &Manifold, normal_idx: i32) -> Self

Smooth by normals.

Source

pub fn smooth_out( self: &Manifold, min_sharp_angle: f64, min_smoothness: f64, ) -> Self

Smooth out.

Examples found in repository?
examples/write_ply.rs (line 31)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn calculate_normals( self: &Manifold, normal_idx: i32, min_sharp_angle: f64, ) -> Self

Calculate normals for the manifold and return a new one.

Examples found in repository?
examples/bevy.rs (line 27)
8    fn setup(
9        mut commands: Commands,
10        mut meshes: ResMut<Assets<Mesh>>,
11        mut materials: ResMut<Assets<StandardMaterial>>,
12    ) {
13        let tube = Self::cylinder_manifold(1.0, 3.0)
14            .boolean_op(
15                &Self::cylinder_manifold(0.5, 4.0),
16                manifold_rs::BooleanOp::Difference,
17            )
18            .translate(-1.5, -1.5, 0.0);
19
20        Self::add_manifold(&mut commands, &mut meshes, &mut materials, tube);
21
22        let tube_with_normals = Self::cylinder_manifold(1.0, 3.0)
23            .boolean_op(
24                &Self::cylinder_manifold(0.5, 4.0),
25                manifold_rs::BooleanOp::Difference,
26            )
27            .calculate_normals(0, 50.0)
28            .translate(1.5, 1.5, 0.0);
29
30        Self::add_manifold(
31            &mut commands,
32            &mut meshes,
33            &mut materials,
34            tube_with_normals,
35        );
36    }
More examples
Hide additional examples
examples/write_ply.rs (line 17)
6fn main() -> std::io::Result<()> {
7    // Generate a cube
8    {
9        let size = 10.0;
10        let manifold = Manifold::cube(size, size, size);
11        manifold.write_ply_to_file("cube.ply")?;
12    }
13
14    // Generate a cube with normals
15    {
16        let size = 10.0;
17        let manifold = Manifold::cube(size, size, size).calculate_normals(0, 30.0);
18        manifold.write_ply_to_file("cube_normals.ply")?;
19    }
20
21    // Generate a dice and smooth it
22    {
23        let size = 10.0;
24        let manifold = Manifold::cube(size, size, size)
25            .translate(-size / 2.0, -size / 2.0, -size / 2.0)
26            .intersection(&Manifold::sphere(size / 2.0_f64.sqrt(), 64));
27
28        let manifold = manifold
29            .calculate_normals(0, 40.0)
30            .refine_to_length(0.2)
31            .smooth_out(40.0, 1.0);
32
33        manifold.write_ply_to_file("dice.ply")?;
34    }
35
36    // Generate tube with normals
37    {
38        let inner_radius = 0.3;
39        let outer_radius = 1.0;
40        let height = 2.0;
41
42        let manifold = Manifold::cylinder(outer_radius, outer_radius, height, 32)
43            .difference(&Manifold::cylinder(inner_radius, inner_radius, height, 32));
44        let manifold = manifold.calculate_normals(0, 40.0);
45
46        manifold.write_ply_to_file("tube_normals.ply")?;
47    }
48
49    Ok(())
50}
Source

pub fn to_mesh(&self) -> Mesh

Get the mesh representation of the manifold.

Examples found in repository?
examples/bevy.rs (line 89)
88    fn manifold_to_bevy_mesh(manifold: manifold_rs::Manifold) -> Mesh {
89        let mesh = manifold.to_mesh();
90
91        let vertices = mesh.vertices();
92        let indices = mesh.indices();
93
94        match mesh.num_props() {
95            // Vertex without normals
96            3 => {
97                let vertices = vertices
98                    .chunks(3)
99                    .map(|c| -> [f32; 3] { c.try_into().expect("Chunk size should be 3") })
100                    .collect::<Vec<[f32; 3]>>();
101
102                Mesh::new(
103                    bevy::render::mesh::PrimitiveTopology::TriangleList,
104                    bevy::asset::RenderAssetUsages::MAIN_WORLD
105                        | bevy::asset::RenderAssetUsages::RENDER_WORLD,
106                )
107                .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
108                .with_inserted_indices(bevy::render::mesh::Indices::U32(indices))
109                .with_duplicated_vertices()
110                .with_computed_flat_normals()
111            }
112            // Vertex with normals
113            6 => {
114                let normals = vertices
115                    .chunks(6)
116                    .map(|c| -> [f32; 3] { [c[3], c[4], c[5]] })
117                    .collect::<Vec<[f32; 3]>>();
118
119                let vertices = vertices
120                    .chunks(6)
121                    .map(|c| -> [f32; 3] { [c[0], c[1], c[2]] })
122                    .collect::<Vec<[f32; 3]>>();
123
124                Mesh::new(
125                    bevy::render::mesh::PrimitiveTopology::TriangleList,
126                    bevy::asset::RenderAssetUsages::MAIN_WORLD
127                        | bevy::asset::RenderAssetUsages::RENDER_WORLD,
128                )
129                .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vertices)
130                .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
131                .with_inserted_indices(bevy::render::mesh::Indices::U32(indices))
132            }
133            num_props => panic!("Invalid property count {num_props}"),
134        }
135    }
More examples
Hide additional examples
examples/write_stl.rs (line 26)
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}
Source

pub fn from_mesh(mesh: Mesh) -> Self

Create a manifold from a mesh.

Trait Implementations§

Source§

impl From<Manifold> for Mesh

Convert Manifold to Mesh struct

Source§

fn from(manifold: Manifold) -> Self

Converts to this type from the input type.
Source§

impl From<Mesh> for Manifold

Convert Mesh to Manifold struct

Source§

fn from(mesh: Mesh) -> Self

Converts to this type from the input type.
Source§

impl WritePly for Manifold

Source§

fn write_ply(&self, writer: &mut impl Write) -> Result<()>

Source§

fn write_ply_to_file(&self, filename: impl AsRef<Path>) -> Result<()>

Source§

impl WriteStl for Manifold

Source§

fn write_stl(&self, writer: &mut impl Write) -> Result<()>

Source§

fn write_stl_to_file(&self, filename: impl AsRef<Path>) -> Result<()>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.