MeshLib Documentation
Loading...
Searching...
No Matches
Points to Mesh Example

Example of generating a triangulation from a pointcloud.

  • C++
    #include <iostream>
    int main()
    {
    // CTM, JPEG and other formats beyond the basic ones come with the MRIOExtras library
    // load points
    auto loadRes = MR::PointsLoad::fromAnySupportedFormat( "Points.ply" );
    if ( !loadRes.has_value() )
    {
    std::cerr << loadRes.error() << "\n";
    return 1; // error while loading file
    }
    auto triangulationRes = MR::triangulatePointCloud( *loadRes );
    assert( triangulationRes ); // can be nullopt only if canceled by progress callback
    auto saveRes = MR::MeshSave::toAnySupportedFormat( *triangulationRes, "Mesh.ctm" );
    if ( !saveRes.has_value() )
    {
    std::cerr << saveRes.error() << "\n";
    return 1; // error while saving file
    }
    return 0;
    }
    int main()
    Definition LaplacianDeformation.cpp:4
    void loadIOExtras()
    Expected< PointCloud > fromAnySupportedFormat(const std::filesystem::path &file, const PointsLoadSettings &settings={})
    Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    std::optional< Mesh > triangulatePointCloud(const PointCloud &pointCloud, const TriangulationParameters &params={}, const ProgressCallback &progressCb={})
  • Python
    from meshlib import mrmeshpy as mm
    from pathlib import Path
    pc = mm.loadPoints("Points.ply")
    nefertiti_mesh = mm.triangulatePointCloud(pc)
    mm.saveMesh(nefertiti_mesh, "Mesh.ctm")
  • C
    #include <MRCMesh/MRPointsLoad.h>
    #include <MRCMesh/MRPointCloud.h>
    #include <MRCMesh/MRPointCloudTriangulation.h>
    #include <MRCMesh/MRMeshSave.h>
    #include <MRCMisc/expected_MR_Mesh_std_string.h>
    #include <MRCMisc/expected_MR_PointCloud_std_string.h>
    #include <MRCMisc/expected_void_std_string.h>
    #include <MRCMisc/std_optional_MR_Mesh.h>
    #include <MRCMisc/std_string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( void )
    {
    int rc = EXIT_FAILURE;
    // load points
    MR_expected_MR_PointCloud_std_string* loadRes = MR_PointsLoad_fromAnySupportedFormat( "Points.ply", NULL, NULL );
    MR_PointCloud* pointCloud = MR_expected_MR_PointCloud_std_string_value_mut( loadRes );
    if ( !pointCloud )
    {
    fprintf( stderr, "Failed to load points: %s\n", MR_std_string_data( MR_expected_MR_PointCloud_std_string_error( loadRes ) ) );
    goto fail_load; // error while loading file
    }
    MR_std_optional_MR_Mesh* triangulationRes = MR_triangulatePointCloud( pointCloud, NULL, NULL );
    MR_Mesh* mesh = MR_std_optional_MR_Mesh_value_mut( triangulationRes );
    if ( !mesh )
    {
    fprintf( stderr, "Triangulation canceled\n" );
    goto fail_triangulation; // can be nullopt only if canceled by progress callback
    }
    MR_expected_void_std_string* saveEx = MR_MeshSave_toAnySupportedFormat( mesh, "Mesh.ctm", 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; // error while saving file
    }
    rc = EXIT_SUCCESS;
    fail_save:
    MR_expected_void_std_string_Destroy( saveEx );
    fail_triangulation:
    MR_std_optional_MR_Mesh_Destroy( triangulationRes );
    fail_load:
    MR_expected_MR_PointCloud_std_string_Destroy( loadRes );
    return rc;
    }
    const std::shared_ptr< const PointCloud > & pointCloud() const
    const std::shared_ptr< const Mesh > & mesh() const
  • C#
    public static class PointsToMeshExample
    {
    public static void Main(string[] args)
    {
    // load points
    var pc = MR.PointsLoad.fromAnySupportedFormat("Points.ply");
    var mesh = MR.triangulatePointCloud(pc).value()!; // can be null only if canceled by progress callback
    MR.MeshSave.toAnySupportedFormat(mesh, "Mesh.ctm");
    }
    }
  • JavaScript
    import createMeshLib from '@meshinspector/meshlib';
    const ml = await createMeshLib();
    // load points
    using pointCloud = ml.PointsLoad.fromAnySupportedFormat('Points.ply');
    // triangulate (the JS binding requires a parameters object)
    using params = new ml.TriangulationParameters();
    using mesh = ml.triangulatePointCloud(pointCloud, params);
    if (!mesh)
    throw new Error('triangulation was cancelled');
    ml.MeshSave.toAnySupportedFormat(mesh, 'Mesh.ply');
    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
    fn main() -> Result<(), String>
    {
    // load points
    let points = meshlib::points_load::from_any_supported_format("Points.ply", None).into_result()?;
    let triangulation_res = meshlib::triangulate_point_cloud(&points, None, None).into_option().expect("can be None only if canceled by progress callback");
    meshlib::mesh_save::to_any_supported_format(&triangulation_res, "Mesh.ctm", None).into_result()?;
    Ok(())
    }
  • Java
    import meshlib.Functions;
    import meshlib.PointCloud;
    import meshlib.std_optional_MR_Mesh;
    // The class isn't public because of the `.dox.java` file name, the launcher doesn't mind.
    class PointsToMesh
    {
    public static void main(String[] args)
    {
    // load points. An error is thrown as `UnexpectedResultException`.
    PointCloud pointCloud = meshlib.pointsload.Functions.fromAnySupportedFormat("Points.ply", null);
    std_optional_MR_Mesh triangulationRes = Functions.triangulatePointCloud(pointCloud, null, null);
    assert triangulationRes.has_value(); // can be empty only if canceled by progress callback
    meshlib.meshsave.Functions.toAnySupportedFormat(triangulationRes.value(), "Mesh.ctm", null);
    }
    }