MeshLib Documentation
Loading...
Searching...
No Matches
Mesh extrude

Example of extrude faces on mesh

  • C++
    #include <MRMesh/MRId.h>
    #include <MRMesh/MRMesh.h>
    #include <iostream>
    int main()
    {
    // Load mesh
    auto loadRes = MR::MeshLoad::fromAnySupportedFormat( "mesh.stl" );
    if ( !loadRes.has_value() )
    {
    std::cerr << loadRes.error() << std::endl;
    return 1;
    }
    MR::Mesh& mesh = *loadRes;
    // Select faces to extrude
    MR::FaceBitSet facesToExtrude;
    facesToExtrude.autoResizeSet( MR::FaceId( 1 ) );
    facesToExtrude.autoResizeSet( MR::FaceId( 2 ) );
    // Create duplicated verts on region boundary
    // Find vertices that will be moved
    auto vertsForMove = MR::getIncidentVerts( mesh.topology, facesToExtrude );
    MR::BitSetParallelFor( vertsForMove, [&] ( MR::VertId v )
    {
    // Move each vertex
    mesh.points[v] += MR::Vector3f::plusZ();
    } );
    // Invalidate internal caches after manual changing
    mesh.invalidateCaches();
    // Save mesh
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( mesh, "extrudedMesh.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    auto BitSetParallelFor(const BS &bs, F &&f, Cb &&... cb)
    VertBitSet getIncidentVerts(const MeshTopology &topology, const FaceBitSet &faces)
    void makeDegenerateBandAroundRegion(Mesh &mesh, const FaceBitSet &region, const MakeDegenerateBandAroundRegionParams &params={})
    const std::shared_ptr< const Mesh > & mesh() const
    Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    # Load mesh
    mesh = mrmeshpy.loadMesh("mesh.stl")
    # Prepare region to extrude
    faces_to_extrude = mrmeshpy.FaceBitSet()
    faces_to_extrude.resize(3, False)
    faces_to_extrude.set(mrmeshpy.FaceId(1), True)
    faces_to_extrude.set(mrmeshpy.FaceId(2), True)
    # Create duplicated verts on region boundary
    mrmeshpy.makeDegenerateBandAroundRegion(mesh, faces_to_extrude)
    # Find vertices that will be moved
    verts_for_move = mrmeshpy.getIncidentVerts(mesh.topology, faces_to_extrude)
    # Move each vertex
    for v in range(verts_for_move.size()):
    if verts_for_move.test(mrmeshpy.VertId(v)):
    mesh.points[mrmeshpy.VertId(v)] += mrmeshpy.Vector3f(0.0, 0.0, 1.0)
    # Invalidate internal caches after manual changing
    mesh.invalidateCaches()
    # Save mesh
    mrmeshpy.saveMesh(mesh, "extrudedMesh.stl")
  • C
    #include <MRCMesh/MRBitSet.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshExtrude.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRRegionBoundary.h>
    #include <MRCMesh/MRVector.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Load mesh.
    MR_expected_MR_Mesh_std_string* meshEx = MR_MeshLoad_fromAnySupportedFormat( "mesh.stl", NULL, NULL );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( meshEx );
    // Handle failure to load mesh.
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshEx ) ) );
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return 1;
    }
    // Select faces to extrude
    MR_FaceBitSet* facesToExtrude = MR_FaceBitSet_DefaultConstruct();
    MR_FaceBitSet_autoResizeSet( facesToExtrude, (MR_FaceId){1}, NULL );
    MR_FaceBitSet_autoResizeSet( facesToExtrude, (MR_FaceId){2}, NULL );
    // Create duplicated verts on region boundary
    MR_makeDegenerateBandAroundRegion( mesh, facesToExtrude, NULL );
    // Find vertices that will be moved
    MR_VertBitSet* vertsToMove = MR_getIncidentVerts( MR_Mesh_Get_topology( mesh ), facesToExtrude );
    MR_Vector3f* points = MR_VertCoords_data_mut( MR_Mesh_GetMutable_points( mesh ) );
    MR_Vector3f shift = MR_Vector3f_plusZ();
    size_t numPoints = MR_VertCoords_size( MR_Mesh_GetMutable_points( mesh ) );
    for ( size_t i = 0; i < numPoints; ++i )
    if ( MR_VertBitSet_test( vertsToMove, (MR_VertId){ (int)i } ) )
    points[i] = MR_add_MR_Vector3f( &points[i], &shift );
    // Invalidate internal caches after manual changing
    MR_Mesh_invalidateCaches( mesh, NULL );
    // Save mesh
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( mesh, "extrudedMesh.stl", NULL, NULL);
    if ( MR_expected_void_std_string_error( saveEx ) )
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveEx ) ) );
    else
    rc = EXIT_SUCCESS;
    MR_expected_void_std_string_Destroy( saveEx );
    MR_VertBitSet_Destroy( vertsToMove );
    MR_FaceBitSet_Destroy( facesToExtrude );
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }
    Vector3f shift() const
    VertCoords & points() const
  • C#
    public static class MeshExtrudeExample
    {
    public static void Main(string[] args)
    {
    // Load mesh
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    // Select faces to extrude
    MR.FaceBitSet facesToExtrude = new();
    facesToExtrude.autoResizeSet(new MR.FaceId(1));
    facesToExtrude.autoResizeSet(new MR.FaceId(2));
    // Create duplicated verts on region boundary
    MR.makeDegenerateBandAroundRegion(mesh, facesToExtrude);
    // Find vertices that will be moved
    var vertsForMove = MR.getIncidentVerts(mesh.topology, facesToExtrude);
    foreach (var v in vertsForMove)
    {
    // Move each vertex
    mesh.points[v] += MR.Vector3f.plusZ();
    }
    // Invalidate internal caches after manual changing
    mesh.invalidateCaches();
    // Save mesh
    MR.MeshSave.toAnySupportedFormat(mesh, "extrudedMesh.stl");
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    using mesh = ml.MeshLoad.fromAnySupportedFormat('mesh.stl');
    // select the faces to extrude
    using facesToExtrude = ml.FaceBitSet.fromIndices(new Uint32Array([1, 2]));
    // create duplicated vertices along the region boundary
    ml.makeDegenerateBandAroundRegion(mesh, facesToExtrude);
    // find the vertices to move and shift them along +Z
    using topology = mesh.topology;
    using vertsForMove = ml.getIncidentVertsFromFaces(topology, facesToExtrude);
    using coords = mesh.points;
    for (const v of vertsForMove.toIndices())
    coords.set(v, ml.Vector3.add(coords.get(v), ml.Vector3.plusZ()));
    mesh.points = coords;
    // invalidate internal caches after the manual vertex changes
    mesh.invalidateCaches();
    ml.MeshSave.toAnySupportedFormat(mesh, 'extrudedMesh.stl');
    Note
    The using declaration requires Node.js 24+ or a modern browser. On older runtimes, call .delete() on each object instead (see Memory Management in the JavaScript Setup Guide).
  • Rust
    use meshlib::{FaceBitSet, FaceId, Vector3f};
    fn main() -> Result<(), String>
    {
    // Load mesh
    let mut mesh = meshlib::mesh_load::from_any_supported_format("mesh.stl", None).into_result()?;
    // Select faces to extrude
    let mut faces_to_extrude = FaceBitSet::new();
    faces_to_extrude.auto_resize_set(FaceId {id_: 1}, None);
    faces_to_extrude.auto_resize_set(FaceId {id_: 2}, None);
    // Create duplicated verts on region boundary
    meshlib::make_degenerate_band_around_region(&mut mesh, &faces_to_extrude, None);
    // Find vertices that will be moved
    let verts_for_move = meshlib::get_incident_verts(mesh.topology(), &faces_to_extrude);
    let mut v = verts_for_move.find_first();
    while v.valid()
    {
    // Move each vertex
    meshlib::op_add_assign_mr_vector3f(mesh.points_mut().index_mut(v), &Vector3f::plus_z());
    v = verts_for_move.find_next(v);
    }
    // Invalidate internal caches after manual changing
    mesh.invalidate_caches(None);
    // Save mesh
    meshlib::mesh_save::to_any_supported_format(&mesh, "extrudedMesh.stl", None).into_result()?;
    Ok(())
    }
  • Java
    import meshlib.FaceBitSet;
    import meshlib.FaceId;
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.Vector3f;
    import meshlib.VertBitSet;
    import meshlib.VertId;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshExtrude
    {
    public static void main(String[] args)
    {
    // Load mesh. An error is thrown as `UnexpectedResultException`.
    Mesh mesh = meshlib.meshload.Functions.fromAnySupportedFormat("mesh.stl", null);
    // Select faces to extrude
    FaceBitSet facesToExtrude = new FaceBitSet();
    facesToExtrude.autoResizeSet(new FaceId(1), true);
    facesToExtrude.autoResizeSet(new FaceId(2), true);
    // Create duplicated verts on region boundary
    Functions.makeDegenerateBandAroundRegion(mesh, facesToExtrude, null);
    // Find vertices that will be moved
    VertBitSet vertsForMove = Functions.getIncidentVerts(mesh.topology(), facesToExtrude);
    for (VertId v = vertsForMove.find_first(); v.valid(); v = vertsForMove.find_next(v))
    {
    // Move each vertex
    Functions.opAddAssign(mesh.points().index_mut(v), Vector3f.plusZ());
    }
    // Invalidate internal caches after manual changing
    mesh.invalidateCaches(null);
    // Save mesh
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "extrudedMesh.stl", null);
    }
    }