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>
    int main()
    {
    // Load mesh
    // Repack mesh optimally.
    // It's not necessary but highly recommended to achieve the best performance in parallel processing
    mesh.packOptimally();
    // 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 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
    MR::MeshSave::toAnySupportedFormat( mesh, "decimated_mesh.stl" );
    return 0;
    }
    MRMESH_API DecimateResult decimateMesh(Mesh &mesh, const DecimateSettings &settings={})
    MRMESH_API Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    MRMESH_API PackMapping packOptimally(bool preserveAABBTree=true)
    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
    # 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")
    None saveMesh(Mesh mesh, os.PathLike|str|bytes file, SaveSettings settings='{}')
    DecimateResult decimateMesh(Mesh mesh, DecimateSettings settings='{}')
    Mesh loadMesh(os.PathLike|str|bytes file, MeshLoadSettings settings='{}')
    Source mesh
    After decimate
  • C
    #include <MRMeshC/MRMesh.h>
    #include <MRMeshC/MRMeshDecimate.h>
    #include <MRMeshC/MRMeshLoad.h>
    #include <MRMeshC/MRMeshSave.h>
    #include <MRMeshC/MRString.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main( int argc, char* argv[] )
    {
    int rc = EXIT_FAILURE;
    // error messages will be stored here
    MRString* errorString = NULL;
    // Load mesh
    MRMesh* mesh = mrMeshLoadFromAnySupportedFormat( "mesh.stl", &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to load mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out;
    }
    // Setup decimate parameters
    // Decimation stop thresholds, you may specify one or both
    params.maxDeletedFaces = 1000; // Number of faces to be deleted
    params.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
    params.subdivideParts = 64;
    // Decimate mesh
    MRDecimateResult result = mrDecimateMesh( mesh, &params );
    printf( "Removed %d vertices, %d faces", result.vertsDeleted, result.facesDeleted );
    // Save result
    mrMeshSaveToAnySupportedFormat( mesh, "decimated_mesh.stl", &saveSettings, &errorString);
    if ( errorString )
    {
    fprintf( stderr, "Failed to save mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out;
    }
    rc = EXIT_SUCCESS;
    out:
    mrMeshFree( mesh );
    return rc;
    }
    MRMESHC_API MRDecimateSettings mrDecimateSettingsNew(void)
    MRMESHC_API MRDecimateResult mrDecimateMesh(MRMesh *mesh, const MRDecimateSettings *settings)
    struct MRMesh MRMesh
    typedefMR_EXTERN_C_BEGIN struct MRString MRString
    MR_EXTERN_C_BEGIN MRMESHC_API MRMesh * mrMeshLoadFromAnySupportedFormat(const char *file, MRString **errorStr)
    MR_EXTERN_C_BEGIN MRMESHC_API void mrMeshSaveToAnySupportedFormat(const MRMesh *mesh, const char *file, const MRSaveSettings *settings, MRString **errorStr)
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    MRMESHC_API MRSaveSettings mrSaveSettingsNew(void)
    MRMESHC_API void mrStringFree(MRString *str)
    MR_EXTERN_C_BEGIN MRMESHC_API const char * mrStringData(const MRString *str)
    Source mesh
    After decimate
  • C#
    using static MR.DotNet;
    public class MeshDecimateExample
    {
    public static void Run(string[] args)
    {
    try
    {
    // Load mesh
    var mesh = MeshLoad.FromAnySupportedFormat("mesh.stl");
    // Setup decimate parameters
    DecimateParameters dp = new DecimateParameters();
    dp.strategy = DecimateStrategy.MinimizeError;
    dp.maxError = 1e-5f * mesh.BoundingBox.Diagonal();
    dp.tinyEdgeLength = 1e-3f;
    dp.packMesh = true;
    // Decimate mesh
    var result = Decimate(ref mesh, dp);
    // Save result
    MeshSave.ToAnySupportedFormat(mesh, "decimated_mesh.stl");
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }
    Source mesh
    After decimate