MeshLib Documentation
Loading...
Searching...
No Matches
Signed Distance Point To Mesh

Example of usage to get signed distance from point to mesh

▶ See it live in the browser

  • C++
    #include "MRMesh/MRMesh.h"
    #include <iostream>
    int main()
    {
    // CTM, JPEG and other formats beyond the basic ones come with the MRIOExtras library
    auto mesh1Res = MR::MeshLoad::fromAnySupportedFormat( "mesh1.ctm" );
    if ( !mesh1Res.has_value() )
    {
    std::cerr << mesh1Res.error();
    return 1;
    }
    auto point = MR::Vector3f( 1.5f, 1.0f, 0.5f );
    auto dist = MR::findSignedDistance( point, *mesh1Res );
    if ( dist )
    std::cout << "Signed distance from point to mesh " << dist->dist << "\n";
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    MeshMeshSignedDistanceResult findSignedDistance(const MeshPart &a, const MeshPart &b, const AffineXf3f *rigidB2A=nullptr, float upDistLimitSq=FLT_MAX)
    float dist() const
    void loadIOExtras()
    Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
  • Python
    from meshlib import mrmeshpy
    mesh = mrmeshpy.loadMesh("mesh1.ctm")
    point = mrmeshpy.Vector3f(1.5, 1, 0.5)
    a = mrmeshpy.findSignedDistance(point, mesh)
    print(f"Signed distance from point to mesh {a.dist}")
  • C
    #include <MRCMesh/MRMesh.h>
    #include <MRCMesh/MRMeshPart.h>
    #include <MRCMesh/MRMeshLoad.h>
    #include <MRCMesh/MRMeshProject.h>
    #include <MRCMisc/std_string.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/std_optional_MR_SignedDistanceToMeshResult.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // Load mesh.
    MR_expected_MR_Mesh_std_string* meshRes = MR_MeshLoad_fromAnySupportedFormat( "mesh1.ctm", NULL, NULL );
    MR_Mesh* mesh = MR_expected_MR_Mesh_std_string_value_mut( meshRes );
    if ( !mesh )
    {
    fprintf( stderr, "Failed to load mesh: %s\n", MR_std_string_data( MR_expected_MR_Mesh_std_string_error( meshRes ) ) );
    goto fail_load;
    }
    MR_Vector3f point = MR_Vector3f_Construct( 1.5f, 1.0f, 0.5f );
    MR_MeshPart* mp = MR_MeshPart_Construct( mesh, NULL );
    MR_std_optional_MR_SignedDistanceToMeshResult* resOpt = MR_findSignedDistance_MR_Vector3f( &point, mp, NULL, NULL );
    MR_SignedDistanceToMeshResult* res = MR_std_optional_MR_SignedDistanceToMeshResult_value_mut( resOpt );
    if ( res )
    fprintf( stdout, "Signed distance from point to mesh: %f\n", *MR_SignedDistanceToMeshResult_Get_dist( res ) );
    rc = EXIT_SUCCESS;
    MR_std_optional_MR_SignedDistanceToMeshResult_Destroy( resOpt );
    MR_MeshPart_Destroy( mp );
    fail_load:
    MR_expected_MR_Mesh_std_string_Destroy( meshRes );
    return rc;
    }
    const std::shared_ptr< const Mesh > & mesh() const
  • C#
    public static class SignedDistancePointToMeshExample
    {
    public static void Main(string[] args)
    {
    var mesh1 = MR.MeshLoad.fromAnySupportedFormat("mesh1.ctm");
    var point = new MR.Vector3f(1.5f, 1.0f, 0.5f);
    var dist = MR.findSignedDistance(point, mesh1).value();
    if (dist is not null)
    Console.WriteLine("Signed distance from point to mesh {0}", dist.dist);
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    using mesh = ml.MeshLoad.fromAnySupportedFormat('mesh1.ply');
    const point = { x: 1.5, y: 1.0, z: 0.5 };
    const result = ml.findSignedDistanceFromPoint(point, mesh);
    if (result)
    console.log(`Signed distance from point to mesh ${result.dist}`);
    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::{MeshPart, Vector3f};
    fn main() -> Result<(), String>
    {
    let mesh1 = meshlib::mesh_load::from_any_supported_format("mesh1.ctm", None).into_result()?;
    let point = Vector3f {x: 1.5, y: 1.0, z: 0.5};
    let dist = meshlib::find_signed_distance_mr_vector3f(&point, &MeshPart::construct(&mesh1, None), None, None);
    if let Some(dist) = dist.value()
    {
    println!("Signed distance from point to mesh {}", dist.dist());
    }
    Ok(())
    }
  • Java
    import meshlib.Functions;
    import meshlib.Mesh;
    import meshlib.MeshPart;
    import meshlib.Vector3f;
    import meshlib.std_optional_MR_SignedDistanceToMeshResult;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class SignedDistancePointToMesh
    {
    public static void main(String[] args)
    {
    // An error is thrown as `UnexpectedResultException`.
    Mesh mesh1 = meshlib.meshload.Functions.fromAnySupportedFormat("mesh1.ctm", null);
    Vector3f point = new Vector3f(1.5f, 1.0f, 0.5f);
    std_optional_MR_SignedDistanceToMeshResult dist = Functions.findSignedDistance(point, new MeshPart(mesh1, null), null, null);
    if (dist.has_value())
    System.out.println("Signed distance from point to mesh " + dist.value().dist());
    }
    }