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

Example of mesh decimation

▶ See it live in the browser

  • C++
    #include <MRMesh/MRMesh.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 without building an AABB tree, which decimation invalidates anyway.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing
    mesh.packOptimally( false );
    // Setup decimate parameters
    // 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 for parallel processing, greatly improves performance at the cost of minor quality loss.
    // 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead
    settings.subdivideParts = 16;
    // 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
    DecimateResult decimateMesh(Mesh &mesh, const DecimateSettings &settings={})
    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={})
    Source mesh
    After decimate
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    # Load mesh
    mesh = mrmeshpy.loadMesh("mesh.stl")
    # Repack mesh optimally without building an AABB tree, which decimation invalidates anyway.
    # It's not necessary but highly recommended to achieve the best performance in parallel processing
    mesh.packOptimally(False)
    # 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 for parallel processing, greatly improves performance at the cost of minor quality loss.
    # 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead
    settings.subdivideParts = 16
    # Decimate mesh
    mrmeshpy.decimateMesh(mesh, settings)
    # Save result
    mrmeshpy.saveMesh(mesh, "decimatedMesh.stl")
    Source mesh
    After decimate
  • C
    #include <MRCMesh/MRBuffer.h>
    #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( "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;
    }
    // Repack mesh optimally without building an AABB tree, which decimation invalidates anyway.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing
    bool preserveAABBTree = false;
    MR_PackMapping_Destroy( MR_Mesh_packOptimally_1( mesh, &preserveAABBTree ) );
    // 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 for parallel processing, greatly improves performance at the cost of minor quality loss.
    // 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead
    MR_DecimateSettings_Set_subdivideParts( params, 16 );
    // Decimate mesh
    MR_DecimateResult_Destroy( MR_decimateMesh( mesh, params ) );
    MR_DecimateSettings_Destroy( params );
    // Save result
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( 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 static class MeshDecimateExample
    {
    public static void Main(string[] args)
    {
    // Load mesh
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    // Repack mesh optimally without building an AABB tree, which decimation invalidates anyway.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing
    mesh.packOptimally(false);
    // Setup decimate parameters
    MR.DecimateSettings settings = new();
    // 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 for parallel processing, greatly improves performance at the cost of minor quality loss.
    // 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead
    settings.subdivideParts = 16;
    // Decimate mesh
    MR.decimateMesh(mesh, settings);
    // Save result
    MR.MeshSave.toAnySupportedFormat(mesh, "decimated_mesh.stl");
    }
    }
    Source mesh
    After decimate
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // Load mesh
    using mesh = ml.MeshLoad.fromAnySupportedFormat('mesh.stl');
    // Repack the mesh without building an AABB tree, which decimation invalidates anyway.
    // Not necessary, but highly recommended to achieve the best performance in parallel processing.
    mesh.packOptimally(false);
    // Setup decimate parameters
    using settings = new ml.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 for parallel processing, greatly improves performance at the cost of minor quality loss.
    // 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead
    settings.subdivideParts = 16;
    // Decimate mesh
    ml.decimateMesh(mesh, settings);
    // Save result
    ml.MeshSave.toAnySupportedFormat(mesh, 'decimated_mesh.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).
    Source mesh
    After decimate
  • Rust
    fn main() -> Result<(), String>
    {
    // Load mesh.
    let mut mesh = meshlib::mesh_load::from_any_supported_format("mesh.stl", None).into_result().map_err(|error| format!("Failed to load mesh: {error}"))?;
    // Repack mesh optimally without building an AABB tree, which decimation invalidates anyway.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing.
    mesh.pack_optimally_1(Some(false));
    // Setup decimate parameters.
    let mut settings = meshlib::DecimateSettings::new();
    // Decimation stop thresholds, you may specify one or both.
    settings.set_max_deleted_faces(1000); // Number of faces to be deleted.
    settings.set_max_error(0.05); // Maximum error when decimation stops.
    // Number of parts for parallel processing, greatly improves performance at the cost of minor quality loss.
    // 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead.
    settings.set_subdivide_parts(16);
    // Decimate mesh.
    meshlib::decimate_mesh(&mut mesh, Some(&settings));
    // Save result.
    meshlib::mesh_save::to_any_supported_format(&mesh, "decimated_mesh.stl", None).into_result().map_err(|error| format!("Failed to save mesh: {error}"))?;
    Ok(())
    }
    Source mesh
    After decimate
  • Java
    import meshlib.DecimateSettings;
    import meshlib.Functions;
    import meshlib.Mesh;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class MeshDecimate
    {
    public static void main(String[] args)
    {
    // Load mesh. An error is thrown as `UnexpectedResultException`.
    Mesh mesh = meshlib.meshload.Functions.fromAnySupportedFormat("mesh.stl", null);
    // Repack mesh optimally without building an AABB tree, which decimation invalidates anyway.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing.
    mesh.packOptimally(false);
    // Setup decimate parameters.
    DecimateSettings settings = new DecimateSettings();
    // Decimation stop thresholds, you may specify one or both.
    settings.setMaxDeletedFaces(1000); // Number of faces to be deleted.
    settings.setMaxError(0.05f); // Maximum error when decimation stops.
    // Number of parts for parallel processing, greatly improves performance at the cost of minor quality loss.
    // 16 is a good choice even with fewer CPU cores, since smaller parts are processed faster; much larger values add per-part overhead.
    settings.setSubdivideParts(16);
    // Decimate mesh.
    Functions.decimateMesh(mesh, settings);
    // Save result.
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "decimated_mesh.stl", null);
    }
    }
    Source mesh
    After decimate