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

Example of mesh decimation

  • C++
    #include <MRMesh/MRMesh.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, "decimatedMesh.stl" );
    }
    MRMESH_API DecimateResult decimateMesh(Mesh &mesh, const DecimateSettings &settings={})
    Collapse edges in mesh region according to the settings.
    MRMESH_API Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    detects the format from file extension and loads mesh from it
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    detects the format from file extension and save mesh to it
    Parameters structure for MR::decimateMesh.
    int maxDeletedFaces
    Limit on the number of deleted faces.
    Definition MRMesh/MRMeshDecimate.h:69
    int subdivideParts
    Definition MRMesh/MRMeshDecimate.h:142
    float maxError
    Definition MRMesh/MRMeshDecimate.h:39
    Definition MRMesh/MRMesh.h:23
    MRMESH_API PackMapping packOptimally(bool preserveAABBTree=true)
  • 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")
  • C
    #include <MRMeshC/MRMesh.h>
    #include <stdio.h>
    #include <stdlib.h>
    // print progress every 10%
    int gProgress = -1;
    bool onProgress( float v )
    {
    int progress = (int)( 10.f * v );
    if ( progress != gProgress )
    {
    gProgress = progress;
    printf( "%d%%...\n", progress * 10 );
    }
    return true;
    }
    int main( int argc, char* argv[] )
    {
    int rc = EXIT_FAILURE;
    if ( argc != 2 && argc != 3 )
    {
    fprintf( stderr, "Usage: %s INPUT [OUTPUT]", argv[0] );
    goto out;
    }
    const char* input = argv[1];
    const char* output = ( argc == 2 ) ? argv[1] : argv[2];
    // error messages will be stored here
    MRString* errorString = NULL;
    MRMesh* mesh = mrMeshLoadFromAnySupportedFormat( input, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to load mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out;
    }
    // you can set various parameters for the mesh decimation; see the documentation for more info
    // maximum permitted deviation
    const MRBox3f bbox = mrMeshComputeBoundingBox( mesh, NULL );
    params.maxError = 1e-5f * mrBox3fDiagonal( &bbox );
    // maximum length of edges to be collapsed
    params.tinyEdgeLength = 1e-3f;
    // pack mesh after decimation
    params.packMesh = true;
    // set progress callback
    params.progressCallback = onProgress;
    MRDecimateResult result = mrDecimateMesh( mesh, &params );
    if ( !result.cancelled )
    printf( "Removed %d vertices, %d faces", result.vertsDeleted, result.facesDeleted );
    else
    {
    fprintf( stderr, "Cancelled" );
    goto out_mesh;
    }
    mrMeshSaveToAnySupportedFormat( mesh, output, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to save mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out_mesh;
    }
    rc = EXIT_SUCCESS;
    out_mesh:
    mrMeshFree( mesh );
    out:
    return rc;
    }
    MRMESHC_API float mrBox3fDiagonal(const MRBox3f *box)
    computes length from min to max
    @ MRDecimateStrategyMinimizeError
    the next edge to collapse will be the one that introduced minimal error to the surface
    Definition MRMeshC/MRMeshDecimate.h:11
    MRMESHC_API MRDecimateSettings mrDecimateSettingsNew(void)
    initializes a default instance
    struct MRMesh MRMesh
    Definition MRMeshC/MRMeshFwd.h:43
    typedefMR_EXTERN_C_BEGIN struct MRString MRString
    Definition MRMeshC/MRMeshFwd.h:32
    MRMESHC_API MRBox3f mrMeshComputeBoundingBox(const MRMesh *mesh, const MRAffineXf3f *toWorld)
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    deallocates a Mesh object
    MRMESHC_API void mrStringFree(MRString *str)
    deallocates the string object
    MR_EXTERN_C_BEGIN MRMESHC_API const char * mrStringData(const MRString *str)
    gets read-only access to the string data
    MRMESHC_API MRMesh * mrMeshLoadFromAnySupportedFormat(const char *file, MRString **errorStr)
    MRMESHC_API void mrMeshSaveToAnySupportedFormat(const MRMesh *mesh, const char *file, MRString **errorStr)
    MRMESHC_API MRDecimateResult mrDecimateMesh(MRMesh *mesh, const MRDecimateSettings *settings)
    Collapse edges in mesh region according to the settings.
    Definition MRMeshC/MRBox.h:9
    results of mrDecimateMesh
    Definition MRMeshC/MRMeshDecimate.h:89
    int vertsDeleted
    Number deleted verts. Same as the number of performed collapses.
    Definition MRMeshC/MRMeshDecimate.h:91
    int facesDeleted
    Number deleted faces.
    Definition MRMeshC/MRMeshDecimate.h:93
    bool cancelled
    whether the algorithm was cancelled by the callback
    Definition MRMeshC/MRMeshDecimate.h:100
    parameters for mrDecimateMesh
    Definition MRMeshC/MRMeshDecimate.h:18
    float maxError
    Definition MRMeshC/MRMeshDecimate.h:24
    bool packMesh
    whether to pack mesh at the end
    Definition MRMeshC/MRMeshDecimate.h:69
    MRDecimateStrategy strategy
    Definition MRMeshC/MRMeshDecimate.h:19
    float tinyEdgeLength
    edges not longer than this value will be collapsed even if it results in appearance of a triangle wit...
    Definition MRMeshC/MRMeshDecimate.h:35
    MRProgressCallback progressCallback
    callback to report algorithm progress and cancel it by user request
    Definition MRMeshC/MRMeshDecimate.h:71
  • C#
    using MR.DotNet;
    using System;
    using System.Reflection;
    class Program
    {
    static void Main(string[] args)
    {
    if (args.Length != 1 && args.Length != 2)
    {
    Console.WriteLine("Usage: {0} INPUT [OUTPUT]", Assembly.GetExecutingAssembly().GetName().Name);
    return;
    }
    try
    {
    string input = args[0];
    string output = args.Length == 2 ? args[1] : args[0];
    var mesh = Mesh.FromAnySupportedFormat( input );
    DecimateParameters dp = new DecimateParameters();
    dp.strategy = DecimateStrategy.MinimizeError;
    dp.maxError = 1e-5f * mesh.BoundingBox.Diagonal();
    dp.tinyEdgeLength = 1e-3f;
    dp.packMesh = true;
    var result = MeshDecimate.Decimate(mesh, dp);
    Mesh.ToAnySupportedFormat(mesh, output);
    }
    catch (Exception e)
    {
    Console.WriteLine("Error: {0}", e.Message);
    }
    }
    }
    Definition MeshDecimate.dox.py:1