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

Example of extrude faces on mesh

  • C++
    #include <MRMesh/MRBitSet.h>
    #include <MRMesh/MRBitSetParallelFor.h>
    #include <MRMesh/MRId.h>
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRMeshExtrude.h>
    #include <MRMesh/MRMeshLoad.h>
    #include <MRMesh/MRMeshSave.h>
    #include <MRMesh/MRRegionBoundary.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
    MR::makeDegenerateBandAroundRegion( mesh, facesToExtrude );
    // 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
  • 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.vec[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_2( "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_BitSet_autoResizeSet_2( MR_FaceBitSet_MutableUpcastTo_MR_BitSet( facesToExtrude ), 1, NULL );
    MR_BitSet_autoResizeSet_2( MR_FaceBitSet_MutableUpcastTo_MR_BitSet( facesToExtrude ), 2, NULL );
    // Create duplicated verts on region boundary
    MR_makeDegenerateBandAroundRegion( mesh, facesToExtrude, NULL );
    // Find vertices that will be moved
    MR_VertBitSet* vertsToMove = MR_getIncidentVerts_2_MR_FaceBitSet( 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){ i } ) )
    points[i] = MR_add_MR_Vector3f( &points[i], &shift );
    // Invalidate internal caches after manual changing
    MR_Mesh_invalidateCaches( mesh, NULL );
    // Save result
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat_3( mesh, "extruded_mesh.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_VertBitSet_Destroy( vertsToMove );
    MR_FaceBitSet_Destroy( facesToExtrude );
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }