Skip to content

Load / Save a mesh

Load & Save

A mesh can be opened by using the generic function read_by_extension and written using write_by_extension:

#include "helpers.h"
#include <ultimaille/all.h>


using namespace UM;

int main(int argc, char** argv) {

    // Get path of current executable
    std::string path = getAssetPath();

    // Declare a mesh with triangle surface
    Triangles m;
    // Loading catorus.geogram into m
    read_by_extension(path + "catorus.geogram", m);

    // Display the number of vertices, facets and corners
    std::cout 
        << "n vertices: " << m.nverts() 
        << ", n facets: " << m.nfacets() 
        << ", n corners: " << m.ncorners() << std::endl;

    // Save mesh
    write_by_extension("catorus_cpy.obj", m);

    return 0;
}

Source code here

Note

If the file fails to open, the user will be notified by an error message, for example when you try to open a non-existent file: Failed to open /home/tex/Projects/ultimaille-examples/build/examples/assets/unavailable_catorus.geogram

Warning

If the mesh type you're trying to open is not consistent with the mesh type supplied as a parameter, you won't get any errors, but only the vertices will be loaded. For example, you're trying to load a tri surface mesh into a Quads structure.

Supported file types

Ultimaille currently support reading and writing into the following file formats:

  • .geogram (graphite)
  • .mesh
  • .vtk
  • .xyz
  • .obj