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

Example of mesh decimation

  • C++
    #include <MRMesh/MRMeshFwd.h>
    #include <MRMesh/MRMeshLoad.h>
    #include <MRMesh/MRMeshSave.h>
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRMeshDecimate.h>
    #include <MRMesh/MRBuffer.h>
    #include <iostream>
    int main()
    {
    // Load mesh
    auto meshRes = MR::MeshLoad::fromAnySupportedFormat( "mesh.stl" );
    if ( !meshRes.has_value() )
    {
    std::cerr << meshRes.error() << std::endl;
    return 1;
    }
    MR::Mesh& mesh = *meshRes;
    // Repack mesh optimally.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing
    mesh.packOptimally();
    // Setup decimate parameters
    MR::DecimateSettings settings;
    // Decimation stop thresholds, you may specify one or both
    settings.maxDeletedFaces = 1000; // Number of faces to be deleted
    settings.maxError = 0.05f; // Maximum error when decimation stops
    // Number of parts to simultaneous processing, greatly improves performance by cost of minor quality loss.
    // Recommended to set to the number of available CPU cores or more for the best performance
    settings.subdivideParts = 64;
    // Decimate mesh
    MR::decimateMesh( mesh, settings );
    // Save result
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( mesh, "decimated_mesh.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    Source mesh
    After decimate
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    # Load mesh
    mesh = mrmeshpy.loadMesh("mesh.stl")
    # Repack mesh optimally.
    # It's not necessary but highly recommended to achieve the best performance in parallel processing
    mesh.packOptimally()
    # Setup decimate parameters
    settings = mrmeshpy.DecimateSettings()
    # Decimation stop thresholds, you may specify one or both
    settings.maxDeletedFaces = 1000 # Number of faces to be deleted
    settings.maxError = 0.05 # Maximum error when decimation stops
    # Number of parts to simultaneous processing, greatly improves performance by cost of minor quality loss.
    # Recommended to set to number of CPU cores or more available for the best performance
    settings.subdivideParts = 64
    # Decimate mesh
    mrmeshpy.decimateMesh(mesh, settings)
    # Save result
    mrmeshpy.saveMesh(mesh, "decimatedMesh.stl")
    Source mesh
    After decimate
  • C
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshDecimate.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRString.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 ) ) );
    goto fail;
    }
    // Setup decimate parameters
    MR_DecimateSettings* params = MR_DecimateSettings_DefaultConstruct();
    // Decimation stop thresholds, you may specify one or both
    MR_DecimateSettings_Set_maxDeletedFaces( params, 1000 ); // Number of faces to be deleted
    MR_DecimateSettings_Set_maxError( params, 0.05f ); // Maximum error when decimation stops
    // Number of parts to simultaneous processing, greatly improves performance by cost of minor quality loss.
    // Recommended to set to the number of available CPU cores or more for the best performance
    MR_DecimateSettings_Set_subdivideParts( params, 64 );
    // Decimate mesh
    MR_DecimateResult* result = MR_decimateMesh( mesh, params );
    MR_DecimateSettings_Destroy( params );
    printf( "Removed %d vertices, %d faces\n", *MR_DecimateResult_Get_vertsDeleted( result ), *MR_DecimateResult_Get_facesDeleted( result ) );
    MR_DecimateResult_Destroy( result );
    // Save result
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat_3( mesh, "decimated_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 ) ) );
    goto fail;
    }
    rc = EXIT_SUCCESS;
    fail:
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }
    Source mesh
    After decimate
  • C#
    public class MeshDecimateExample
    {
    public static void Run(string[] args)
    {
    try
    {
    // Load mesh
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    // Setup decimate parameters
    MR.DecimateSettings ds = new();
    ds.strategy = MR.DecimateStrategy.MinimizeError;
    ds.maxError = 1e-5f * mesh.computeBoundingBox().diagonal();
    ds.tinyEdgeLength = 1e-3f;
    ds.packMesh = true;
    // Decimate mesh
    MR.DecimateResult result = MR.decimateMesh(mesh, ds);
    // Save result
    MR.MeshSave.toAnySupportedFormat(mesh, "decimated_mesh.stl");
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }
    Source mesh
    After decimate