MeshLib Documentation
Loading...
Searching...
No Matches
Stitching holes

Example of stitching holes (connect two holes with a cylinder)

  • C++
    #include <MRMesh/MRMesh.h>
    int main()
    {
    // Load meshes
    auto meshARes = MR::MeshLoad::fromAnySupportedFormat( "meshAwithHole.stl" );
    auto meshBRes = MR::MeshLoad::fromAnySupportedFormat( "meshBwithHole.stl" );
    // Unite meshes
    MR::Mesh mesh = std::move( meshARes.value() );
    mesh.addMesh( meshBRes.value() );
    // Find holes (expect that there are exactly 2 holes)
    std::vector<MR::EdgeId> edges = mesh.topology.findHoleRepresentiveEdges();
    if ( edges.size() != 2 )
    return 1;
    // Connect two holes
    params.metric = MR::getUniversalMetric( mesh );
    MR::stitchHoles( mesh, edges.front(), edges.back(), params );
    // Save result
    auto saveRes = MR::MeshSave::toAnySupportedFormat( mesh, "stitchedMesh.stl" );
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    void stitchHoles(Mesh &mesh, EdgeId a, EdgeId b, const StitchHolesParams &params={})
    FillHoleMetric getUniversalMetric(const Mesh &mesh)
    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 meshes
    mesh_a = mrmeshpy.loadMesh("meshAwithHole.stl")
    mesh_b = mrmeshpy.loadMesh("meshBwithHole.stl")
    # Unite meshes
    mesh = mrmeshpy.mergeMeshes([mesh_a, mesh_b])
    # Find holes
    edges = mesh.topology.findHoleRepresentiveEdges()
    # Connect two holes
    params = mrmeshpy.StitchHolesParams()
    params.metric = mrmeshpy.getUniversalMetric(mesh)
    mrmeshpy.stitchHoles(mesh, edges[0], edges[1], params)
    # Save result
    mrmeshpy.saveMesh(mesh, "stitchedMesh.stl")
  • C
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshFillHole.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshMetrics.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRMeshTopology.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_string.h>
    #include <MRCMisc/std_vector_MR_EdgeId.h>
    #include <inttypes.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Load meshes
    MR_expected_MR_Mesh_std_string* meshEx = MR_MeshLoad_fromAnySupportedFormat( "meshAwithHole.stl", NULL, NULL );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( meshEx );
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh A: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshEx ) ) );
    goto fail_mesh_loading_a;
    }
    MR_expected_MR_Mesh_std_string* mesh2Ex = MR_MeshLoad_fromAnySupportedFormat( "meshBwithHole.stl", NULL, NULL );
    MR_Mesh* mesh2 = MR_expected_MR_Mesh_std_string_value_mut( mesh2Ex );
    if ( !mesh2 )
    {
    fprintf( stderr, "Failed to load mesh B: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( mesh2Ex ) ) );
    goto fail_mesh_loading_b;
    }
    // Unite meshes
    MR_Mesh_addMesh( mesh, mesh2, NULL, NULL );
    // Find holes (expect that there are exactly 2 holes)
    MR_std_vector_MR_EdgeId* edges = MR_MeshTopology_findHoleRepresentiveEdges( MR_Mesh_Get_topology( mesh ), NULL );
    if ( MR_std_vector_MR_EdgeId_size( edges ) != 2 )
    {
    fprintf( stderr, "Expected exactly 2 holes to stitch, but found %zu\n", MR_std_vector_MR_EdgeId_size( edges ) );
    goto fail_not_two_holes;
    }
    // Connect two holes
    MR_StitchHolesParams* params = MR_StitchHolesParams_DefaultConstruct();
    MR_FillHoleMetric* metric = MR_getUniversalMetric( mesh );
    MR_StitchHolesParams_Set_metric( params, MR_PassBy_Move, metric );
    MR_FillHoleMetric_Destroy( metric );
    // We also have a version of this function (`MR_stitchHoles_auto()`) that finds the two holes automatically.
    // Here we've found them manually for demonstration purposes.
    MR_stitchHoles( mesh, *MR_std_vector_MR_EdgeId_at( edges, 0 ), *MR_std_vector_MR_EdgeId_at( edges, 1 ), params );
    MR_StitchHolesParams_Destroy( params );
    // Save result
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( mesh, "stitchedMesh.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 ) ) );
    goto fail_save;
    }
    rc = EXIT_SUCCESS;
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_not_two_holes:
    MR_std_vector_MR_EdgeId_Destroy( edges );
    fail_mesh_loading_b:
    MR_expected_MR_Mesh_std_string_Destroy( mesh2Ex );
    fail_mesh_loading_a:
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }
    const UiRenderParams * params
  • C#
    public static class MeshStitchHoleExample
    {
    public static void Main(string[] args)
    {
    // Load meshes
    var mesh = MR.MeshLoad.fromAnySupportedFormat("meshAwithHole.stl");
    var meshB = MR.MeshLoad.fromAnySupportedFormat("meshBwithHole.stl");
    // Unite meshes
    mesh.addMesh(meshB);
    // Find holes (expect that there are exactly 2 holes)
    var edges = mesh.topology.findHoleRepresentiveEdges();
    if (edges.size() != 2)
    Environment.Exit(1);
    // Connect two holes
    MR.StitchHolesParams parameters = new();
    parameters.metric.assign(MR.getUniversalMetric(mesh));
    MR.stitchHoles(mesh, edges[0], edges[1], parameters);
    // Save result
    MR.MeshSave.toAnySupportedFormat(mesh, "stitchedMesh.stl");
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // Load meshes
    using mesh = ml.MeshLoad.fromAnySupportedFormat('meshAwithHole.stl');
    using meshB = ml.MeshLoad.fromAnySupportedFormat('meshBwithHole.stl');
    // Unite meshes
    mesh.addMesh(meshB);
    // Find holes (expect that there are exactly 2 holes)
    using topology = mesh.topology;
    const edges = topology.findHoleRepresentiveEdges();
    if (edges.length !== 2)
    throw new Error(`expected exactly 2 holes, found ${edges.length}`);
    // Connect two holes
    using params = new ml.StitchHolesParams();
    using metric = ml.getUniversalMetric(mesh);
    params.metric = metric;
    ml.stitchHoles(mesh, edges[0], edges[1], params);
    // Save result
    ml.MeshSave.toAnySupportedFormat(mesh, 'stitchedMesh.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::StitchHolesParams;
    fn main() -> Result<(), String>
    {
    // Load meshes
    let mut mesh = meshlib::mesh_load::from_any_supported_format("meshAwithHole.stl", None).into_result()?;
    let mesh_b = meshlib::mesh_load::from_any_supported_format("meshBwithHole.stl", None).into_result()?;
    // Unite meshes
    mesh.add_mesh(&mesh_b, None, None);
    // Find holes (expect that there are exactly 2 holes)
    let edges = mesh.topology().find_hole_representive_edges(None);
    if edges.size() != 2
    {
    return Err(format!("Expected 2 holes, found {}", edges.size()));
    }
    // Connect two holes
    let mut params = StitchHolesParams::new();
    params.set_metric(&meshlib::get_universal_metric(&mesh));
    meshlib::stitch_holes(&mut mesh, *edges.at(0), *edges.at(1), Some(&params));
    // Save result
    meshlib::mesh_save::to_any_supported_format(&mesh, "stitchedMesh.stl", None).into_result()?;
    Ok(())
    }
  • Java
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.StitchHolesParams;
    import meshlib.std_vector_MR_EdgeId;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshStitchHole
    {
    public static void main(String[] args)
    {
    // Load meshes
    Mesh mesh = meshlib.meshload.Functions.fromAnySupportedFormat("meshAwithHole.stl", null);
    Mesh meshB = meshlib.meshload.Functions.fromAnySupportedFormat("meshBwithHole.stl", null);
    // Unite meshes
    mesh.addMesh(meshB, null, null);
    // Find holes (expect that there are exactly 2 holes)
    std_vector_MR_EdgeId edges = mesh.topology().findHoleRepresentiveEdges(null);
    if (edges.size() != 2)
    System.exit(1);
    // Connect two holes
    StitchHolesParams params = new StitchHolesParams();
    params.setMetric(Functions.getUniversalMetric(mesh));
    Functions.stitchHoles(mesh, edges.front(), edges.back(), params);
    // Save result
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "stitchedMesh.stl", null);
    }
    }