MeshLib Documentation
Loading...
Searching...
No Matches
Signed Distances

Example of usage to get signed distances from points of mesh to another reference mesh

▶ See it live in the browser

Note
Please note that unlike findSignedDistance, this function return distances for each valid points
  • C++
    #include "MRMesh/MRMeshLoad.h"
    #include "MRMesh/MRPointsToMeshProjector.h"
    #include "MRMesh/MRMesh.h"
    #include <iostream>
    int main()
    {
    auto refMesh = MR::MeshLoad::fromAnySupportedFormat( "mesh1.ctm" );
    if ( !refMesh.has_value() )
    {
    std::cerr << refMesh.error();
    return 1;
    }
    auto mesh = MR::MeshLoad::fromAnySupportedFormat( "mesh2.ctm" );
    if ( !mesh.has_value() )
    {
    std::cerr << mesh.error();
    return 1;
    }
    // get object of VertScalars - set of distances between points of target mesh and reference mesh
    auto vertDistances = MR::findSignedDistances( *refMesh, *mesh );
    auto minmax = std::minmax_element( begin( vertDistances ), end( vertDistances ) );
    std::cout << "Distance between reference mesh and the closest point of target mesh is " << *minmax.first << "\n";
    std::cout << "Distance between reference mesh and the farthest point of target mesh is " << *minmax.second << "\n";
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
  • Python
    from meshlib import mrmeshpy
    ref_mesh = mrmeshpy.loadMesh("mesh1.ctm")
    mesh = mrmeshpy.loadMesh("mesh2.ctm")
    # get object of mrmeshpy.VertScalars - set of distances between points of target mesh and reference mesh
    vert_distances = mrmeshpy.findSignedDistances(ref_mesh, mesh)
    # mrmeshpy.VertScalars is iterable so we can use min() or max() functions
    print("Distance between reference mesh and the closest point of target mesh is " + str(min(vert_distances)))
    print("Distance between reference mesh and the farthest point of target mesh is " + str(max(vert_distances)))
  • C
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshPart.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRVector.h>
    #include <MRCMesh/MRPointsToMeshProjector.h>
    #include <MRCMisc/std_string.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <float.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Load mesh.
    MR_expected_MR_Mesh_std_string* mesh1Res = MR_MeshLoad_fromAnySupportedFormat_2( "mesh1.ctm", NULL, NULL );
    MR_Mesh* refMesh = MR_expected_MR_Mesh_std_string_value_mut( mesh1Res );
    if ( !refMesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( mesh1Res ) ) );
    goto fail_load_1;
    }
    MR_expected_MR_Mesh_std_string* mesh2Res = MR_MeshLoad_fromAnySupportedFormat_2( "mesh2.ctm", NULL, NULL );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( mesh2Res );
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( mesh2Res ) ) );
    goto fail_load_2;
    }
    // get object of VertScalars - set of distances between points of target mesh and reference mesh
    MR_VertScalars* vertDistances = MR_findSignedDistances_4( refMesh, mesh, NULL, NULL );
    float minDist = FLT_MAX;
    float maxDist = -FLT_MAX;
    for ( size_t i = 0; i < MR_VertScalars_size( vertDistances ); ++i )
    {
    float dist = MR_VertScalars_data( vertDistances )[i];
    if ( dist < minDist )
    minDist = dist;
    if ( dist > maxDist )
    maxDist = dist;
    }
    fprintf( stdout, "Distance between reference mesh and the closest point of target mesh is: %f\n", minDist );
    fprintf( stdout, "Distance between reference mesh and the farthest point of target mesh is: %f\n", maxDist );
    rc = EXIT_SUCCESS;
    MR_VertScalars_Destroy( vertDistances );
    fail_load_2:
    MR_expected_MR_Mesh_std_string_Destroy( mesh2Res );
    fail_load_1:
    MR_expected_MR_Mesh_std_string_Destroy( mesh1Res );
    return rc;
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    using refMesh = ml.MeshLoad.fromAnySupportedFormat('mesh1.ply');
    using mesh = ml.MeshLoad.fromAnySupportedFormat('mesh2.ply');
    // distances between the points of the target mesh and the reference mesh
    // (the JS binding requires a parameters object; its defaults match the C++ overload)
    using params = new ml.MeshProjectionParameters();
    const vertDistances = ml.findSignedDistances(refMesh, mesh, params);
    // find the closest and farthest distances (a loop, not Math.min(...arr), because the
    // typed array may be large enough to overflow the call stack when spread)
    let closest = Infinity;
    let farthest = -Infinity;
    for (const d of vertDistances) {
    if (d < closest) closest = d;
    if (d > farthest) farthest = d;
    }
    console.log(`Distance between reference mesh and the closest point of target mesh is ${closest}`);
    console.log(`Distance between reference mesh and the farthest point of target mesh is ${farthest}`);
    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).