MeshLib Documentation
Loading...
Searching...
No Matches
Adding and removing noise Example
  • C++
    #include <MRMesh/MRAddNoise.h>
    #include <MRMesh/MRBox.h>
    #include <MRMesh/MRMesh.h>
    #include <MRMesh/MRMeshLoad.h>
    #include <MRMesh/MRMeshSave.h>
    #include <MRMesh/MRNormalDenoising.h>
    int main()
    {
    // Load mesh
    auto mesh = MR::MeshLoad::fromAnySupportedFormat( "mesh.stl" );
    assert( mesh );
    // Add noise to the mesh
    MR::addNoise( mesh->points, mesh->topology.getValidVerts(), {
    .sigma = mesh->computeBoundingBox().diagonal() * 0.0001f,
    } );
    // Invalidate the mesh because of the external vertex changes
    mesh->invalidateCaches();
    // Save the noised mesh
    MR::MeshSave::toAnySupportedFormat( *mesh, "noised_mesh.stl" );
    // Denoise the mesh with sharpening for sharp edges
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    // Save the denoised mesh
    MR::MeshSave::toAnySupportedFormat( *mesh, "denoised_mesh.stl" );
    }
    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 Expected< void > meshDenoiseViaNormals(Mesh &mesh, const DenoiseViaNormalsSettings &settings={})
    MRMESH_API Expected< void > addNoise(VertCoords &points, const VertBitSet &validVerts, NoiseSettings settings)
  • Python
    Note
    Python API version 3 and later
    from meshlib import mrmeshpy as mm
    # Load mesh
    mesh = mm.loadMesh("mesh.stl")
    # Create parameters for adding noise
    nSettings = mm.NoiseSettings()
    nSettings.sigma = mesh.computeBoundingBox().diagonal()*0.0001
    # Add noise to mesh
    mm.addNoise(mesh.points,mesh.topology.getValidVerts(),nSettings)
    # Save noised mesh
    mm.saveMesh(mesh,"noised_mesh.stl")
    # Denoise mesh with sharpening for sharp edges
    # see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    mm.meshDenoiseViaNormals( mesh )
    # Save denoised mesh
    mm.saveMesh(mesh,"denoised_mesh.stl")
  • C
    #include <MRMeshC/MRAddNoise.h>
    #include <MRMeshC/MRMesh.h>
    #include <MRMeshC/MRMeshLoad.h>
    #include <MRMeshC/MRMeshSave.h>
    #include <MRMeshC/MRNormalDenoising.h>
    #include <stdlib.h>
    int main( int argc, char* argv[] )
    {
    // Load mesh
    MRMesh* mesh = mrMeshLoadFromAnySupportedFormat( "mesh.stl", NULL );
    // Add noise to the mesh
    MRBox3f box = mrMeshComputeBoundingBox( mesh, NULL );
    noiseSettings.sigma = mrBox3fDiagonal( &box ) * 0.0001f;
    mrAddNoiseToMesh( mesh, NULL, &noiseSettings, NULL );
    // Invalidate the mesh because of the external vertex changes
    mrMeshInvalidateCaches( mesh, true );
    // Save the noised mesh
    mrMeshSaveToAnySupportedFormat( mesh, "noised_mesh.stl", NULL, NULL );
    // Denoise the mesh with sharpening for sharp edges
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    mrMeshDenoiseViaNormals( mesh, NULL, NULL );
    // Save the denoised mesh
    mrMeshSaveToAnySupportedFormat( mesh, "denoised_mesh.stl", NULL, NULL );
    mrMeshFree( mesh );
    return EXIT_SUCCESS;
    }
    MRMESHC_API MRNoiseSettings mrNoiseSettingsNew(void)
    MRMESHC_API void mrAddNoiseToMesh(MRMesh *mesh, const MRVertBitSet *region, const MRNoiseSettings *settings, MRString **errorString)
    MRMESHC_API float mrBox3fDiagonal(const MRBox3f *box)
    struct MRMesh MRMesh
    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 MRBox3f mrMeshComputeBoundingBox(const MRMesh *mesh, const MRAffineXf3f *toWorld)
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    MRMESHC_API void mrMeshInvalidateCaches(MRMesh *mesh, bool pointsChanged)
    MRMESHC_API void mrMeshDenoiseViaNormals(MRMesh *mesh, const MRDenoiseViaNormalsSettings *settings, MRString **errorString)