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

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

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( MR::begin( vertDistances ), MR::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 ( MR_uint64_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;
    }