MeshLib Documentation
Loading...
Searching...
No Matches
Adding and removing noise Example

  • C++
    #include <MRMesh/MRBox.h>
    #include <MRMesh/MRMesh.h>
    #include <iostream>
    int main()
    {
    // Load mesh
    auto loadRes = MR::MeshLoad::fromAnySupportedFormat( "mesh.stl" );
    if ( !loadRes.has_value() )
    {
    std::cerr << loadRes.error() << std::endl;
    return 1;
    }
    MR::Mesh& mesh = *loadRes;
    // Add noise to the mesh
    std::ignore = 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
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( mesh, "noised_mesh.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    // Denoise the mesh with sharpening for sharp edges
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    // Save the denoised mesh
    if ( auto saveRes = MR::MeshSave::toAnySupportedFormat( mesh, "denoised_mesh.stl" ); !saveRes )
    {
    std::cerr << saveRes.error() << std::endl;
    return 1;
    }
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    Expected< void > addNoise(VertCoords &points, const VertBitSet &validVerts, NoiseSettings settings)
    void meshDenoiseViaNormals(Mesh &mesh, const DenoiseViaNormalsSettings &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={})
  • 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")
    static constexpr SymMatrix2 diagonal(T diagVal) noexcept
  • C
    #include <MRCMesh/MRAddNoise.h>
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMesh/MRNormalDenoising.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 );
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshEx ) ) );
    goto fail_load;
    }
    // Add noise to the mesh
    MR_Box3f box = MR_Mesh_computeBoundingBox( mesh, NULL );
    MR_NoiseSettings* noiseSettings = MR_NoiseSettings_DefaultConstruct();
    MR_NoiseSettings_Set_sigma( noiseSettings, MR_Box3f_diagonal( &box ) * 0.0001f );
    MR_expected_void_std_string* noiseEx = MR_addNoise_MR_Mesh( mesh, NULL, noiseSettings );
    MR_NoiseSettings_Destroy( noiseSettings );
    if ( MR_expected_void_std_string_error( noiseEx ) )
    {
    fprintf( stderr, "Failed to add noise: %s\n", MR_std_string_data( MR_expected_void_std_string_error( noiseEx ) ) );
    goto fail_noise;
    }
    // Invalidate the mesh because of the external vertex changes
    MR_Mesh_invalidateCaches( mesh, NULL );
    // Save the noised mesh
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( mesh, "noised_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_save;
    }
    // Denoise the mesh with sharpening for sharp edges
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    MR_meshDenoiseViaNormals( mesh, NULL );
    // Save the denoised mesh
    MR_expected_void_std_string* saveEx2 = MR_MeshSave_toAnySupportedFormat( mesh, "denoised_mesh.stl", NULL, NULL);
    if ( MR_expected_void_std_string_error( saveEx2 ) )
    {
    fprintf( stderr, "Failed to save mesh: %s\n", MR_std_string_data( MR_expected_void_std_string_error( saveEx2 ) ) );
    goto fail_save2;
    }
    rc = EXIT_SUCCESS;
    fail_save2:
    MR_expected_void_std_string_Destroy( saveEx2 );
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_noise:
    MR_expected_void_std_string_Destroy( noiseEx );
    fail_load:
    MR_expected_MR_Mesh_std_string_Destroy( meshEx );
    return rc;
    }
    const BoxT & box(NodeId n) const
  • C#
    public static class NoiseDenoiseExample
    {
    public static void Main(string[] args)
    {
    // Load mesh
    var mesh = MR.MeshLoad.fromAnySupportedFormat("mesh.stl");
    // Add noise to the mesh
    MR.NoiseSettings noiseSettings = new();
    noiseSettings.sigma = mesh.computeBoundingBox().diagonal() * 0.0001f;
    MR.addNoise(mesh.points, mesh.topology.getValidVerts(), noiseSettings);
    // 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"
    MR.meshDenoiseViaNormals(mesh);
    // Save the denoised mesh
    MR.MeshSave.toAnySupportedFormat(mesh, "denoised_mesh.stl");
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    using mesh = ml.MeshLoad.fromAnySupportedFormat('mesh.stl');
    // add noise to the mesh, scaled to its bounding box
    using bbox = mesh.computeBoundingBox();
    using noiseSettings = new ml.NoiseSettings();
    noiseSettings.sigma = bbox.diagonal() * 0.0001;
    ml.addNoise(mesh, noiseSettings);
    mesh.invalidateCaches();
    ml.MeshSave.toAnySupportedFormat(mesh, 'noised_mesh.stl');
    // denoise the mesh, keeping sharp edges sharp
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    ml.meshDenoiseViaNormals(mesh);
    ml.MeshSave.toAnySupportedFormat(mesh, 'denoised_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).
  • Rust
    use meshlib::NoiseSettings;
    fn main() -> Result<(), String>
    {
    // Load mesh
    let mut mesh = meshlib::mesh_load::from_any_supported_format("mesh.stl", None).into_result()?;
    // Add noise to the mesh
    let mut noise_settings = NoiseSettings::new();
    noise_settings.set_sigma(mesh.compute_bounding_box(None).diagonal() * 0.0001);
    let _ = meshlib::add_noise_mr_mesh(&mut mesh, None, Some(&noise_settings));
    // Invalidate the mesh because of the external vertex changes
    mesh.invalidate_caches(None);
    // Save the noised mesh
    meshlib::mesh_save::to_any_supported_format(&mesh, "noised_mesh.stl", None).into_result()?;
    // Denoise the mesh with sharpening for sharp edges
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    meshlib::mesh_denoise_via_normals(&mut mesh, None);
    // Save the denoised mesh
    meshlib::mesh_save::to_any_supported_format(&mesh, "denoised_mesh.stl", None).into_result()?;
    Ok(())
    }
  • Java
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.NoiseSettings;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class NoiseDenoise
    {
    public static void main(String[] args)
    {
    // Load mesh. An error is thrown as `UnexpectedResultException`.
    Mesh mesh = meshlib.meshload.Functions.fromAnySupportedFormat("mesh.stl", null);
    // Add noise to the mesh
    NoiseSettings settings = new NoiseSettings();
    settings.setSigma(mesh.computeBoundingBox(null).diagonal() * 0.0001f);
    Functions.addNoise(mesh.points(), mesh.topology().getValidVerts(), settings);
    // Invalidate the mesh because of the external vertex changes
    mesh.invalidateCaches(null);
    // Save the noised mesh
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "noised_mesh.stl", null);
    // Denoise the mesh with sharpening for sharp edges
    // see the article "Mesh Denoising via a Novel Mumford-Shah Framework"
    Functions.meshDenoiseViaNormals(mesh, null);
    // Save the denoised mesh
    meshlib.meshsave.Functions.toAnySupportedFormat(mesh, "denoised_mesh.stl", null);
    }
    }