Struct Mesh

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

Wrapper around a C++ mesh object.

Implementations§

Source§

impl Mesh

Implementations for the Mesh struct.

Source

pub fn new(vertices: &[f32], indices: &[u32]) -> Self

Create a new mesh from vertices and indices.

Source

pub fn num_props(&self) -> u32

Number of properties per vertex

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

pub fn vertices(&self) -> Vec<f32>

Get the vertices of the mesh.

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

pub fn indices(&self) -> Vec<u32>

Get the indices of the mesh.

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

pub fn to_manifold(&self) -> Manifold

Get the manifold representation of the mesh.

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

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 Mesh

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 Mesh

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§

§

impl Freeze for Mesh

§

impl RefUnwindSafe for Mesh

§

impl !Send for Mesh

§

impl !Sync for Mesh

§

impl Unpin for Mesh

§

impl UnwindSafe for Mesh

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.