MeshLib Documentation
Loading...
Searching...
No Matches
Loading and saving mesh files

Following code presents example of loading and saving mesh file

  • C++
    #include <MRMesh/MRMesh.h>
    #include <iostream>
    int main()
    {
    std::filesystem::path inFilePath = "mesh.stl";
    auto loadRes = MR::MeshLoad::fromAnySupportedFormat( inFilePath );
    if ( loadRes.has_value() )
    {
    std::filesystem::path outFilePath = "mesh.ply";
    auto saveRes = MR::MeshSave::toAnySupportedFormat( loadRes.value(), outFilePath );
    if ( !saveRes.has_value() )
    std::cerr << saveRes.error() << std::endl;
    }
    else
    std::cerr << loadRes.error() << std::endl;
    return 0;
    }
    MRMESH_API Expected< Mesh > fromAnySupportedFormat(const std::filesystem::path &file, const MeshLoadSettings &settings={})
    detects the format from file extension and loads mesh from it
    MRMESH_API Expected< void > toAnySupportedFormat(const Mesh &mesh, const std::filesystem::path &file, const SaveSettings &settings={})
    detects the format from file extension and save mesh to it
    Further examples won't check return values for sake of clarity
    See also
    MR::MeshLoad
    MR::MeshSave
  • Python
    import meshlib.mrmeshpy as mrmeshpy
    try:
    mesh = mrmeshpy.loadMesh("mesh.stl")
    except ValueError as e:
    print(e)
    mrmeshpy.saveMesh(mesh, "mesh.ply")
  • C
    #include <MRMeshC/MRMesh.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main( int argc, char* argv[] )
    {
    int rc = EXIT_FAILURE;
    if ( argc != 3 )
    {
    fprintf( stderr, "Usage: %s INPUTFILE OUTPUTFILE", argv[0] );
    goto out;
    }
    const char* inputFile = argv[1];
    const char* outputFile = argv[2];
    // error messages will be stored here
    MRString* errorString = NULL;
    MRMesh* mesh1 = mrMeshLoadFromAnySupportedFormat( inputFile, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to load mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out;
    }
    // extract vertices
    const MRVector3f* vertices = mrMeshPoints( mesh1 );
    size_t verticesNum = mrMeshPointsNum( mesh1 );
    // you can access coordinates via struct fields...
    if ( verticesNum > 0 )
    printf( "The first vertex coordinates: %f; %f; %f", vertices[0].x, vertices[0].y, vertices[0].z );
    // ..or cast them to a row-major float array
    float* vertexData = malloc( sizeof( float ) * 3 * verticesNum );
    memcpy( vertexData, vertices, sizeof( MRVector3f ) * verticesNum );
    // extract faces
    MRTriangulation* t = mrMeshGetTriangulation( mesh1 );
    const MRThreeVertIds* faces = t->data;
    size_t facesNum = t->size;
    // faces are stored as vertex id triples...
    if ( facesNum > 0 )
    printf( "The first face's vertex ids: %d, %d, %d", faces[0][0].id, faces[0][1].id, faces[0][2].id );
    // ...and can also be casted to an integer array
    int* faceData = malloc( sizeof( int ) * 3 * facesNum );
    memcpy( faceData, faces, sizeof( MRThreeVertIds ) * facesNum );
    // meshes can be constructed from these data arrays
    MRMesh* mesh2 = mrMeshFromTriangles( (const MRVector3f*)vertexData, verticesNum, (const MRThreeVertIds*)faceData, facesNum );
    mrMeshSaveToAnySupportedFormat( mesh2, outputFile, &errorString );
    if ( errorString )
    {
    fprintf( stderr, "Failed to save mesh: %s", mrStringData( errorString ) );
    mrStringFree( errorString );
    goto out_mesh2;
    }
    rc = EXIT_SUCCESS;
    out_mesh2:
    mrMeshFree( mesh2 );
    out_faceData:
    free( faceData );
    out_t:
    mrTriangulationFree( t );
    out_vertexData:
    free( vertexData );
    out_mesh1:
    mrMeshFree( mesh1 );
    out:
    return rc;
    }
    MRVertId MRThreeVertIds[3]
    a set of 3 vertices; useful for representing a face via its vertex indices
    Definition MRMeshC/MRId.h:21
    struct MRMesh MRMesh
    Definition MRMeshC/MRMeshFwd.h:43
    typedefMR_EXTERN_C_BEGIN struct MRString MRString
    Definition MRMeshC/MRMeshFwd.h:32
    MRMESHC_API const MRVector3f * mrMeshPoints(const MRMesh *mesh)
    gets read-only access to the mesh vertex coordinates
    MRMESHC_API MRTriangulation * mrMeshGetTriangulation(const MRMesh *mesh)
    MRMESHC_API size_t mrMeshPointsNum(const MRMesh *mesh)
    gets count of the mesh vertex coordinates
    MRMESHC_API MRMesh * mrMeshFromTriangles(const MRVector3f *vertexCoordinates, size_t vertexCoordinatesNum, const MRThreeVertIds *t, size_t tNum)
    constructs a mesh from vertex coordinates and a set of triangles with given ids
    MRMESHC_API void mrMeshFree(MRMesh *mesh)
    deallocates a Mesh object
    MRMESHC_API void mrStringFree(MRString *str)
    deallocates the string object
    MR_EXTERN_C_BEGIN MRMESHC_API const char * mrStringData(const MRString *str)
    gets read-only access to the string data
    MRMESHC_API MRMesh * mrMeshLoadFromAnySupportedFormat(const char *file, MRString **errorStr)
    MRMESHC_API void mrMeshSaveToAnySupportedFormat(const MRMesh *mesh, const char *file, MRString **errorStr)
    three-dimensional vector
    Definition MRMeshC/MRVector3.h:9