Examples and tutorials » Input/output examples » Converting a MEDIT mesh

Write a mesh, load it again and convert it to MFEM format.

Mesh data and file formats

A mesh file stores coordinates, element connectivity and integer attributes. MEDIT is used by MMG for mesh exchange. MFEM provides another text representation of the same basic mesh data. Both formats use the .mesh extension; the format argument selects the reader or writer.

This program creates a unit-square mesh with 25 vertices and 32 triangles, saves it in MEDIT format and converts the saved mesh to MFEM format.

Write and reload the mesh

Five vertices in each coordinate direction give four intervals. Scaling by one quarter places the grid on the unit square.

  Mesh square = Mesh().UniformGrid(Polytope::Type::Triangle, {5, 5});
  square.scale(0.25);
  square.save("Square.medit.mesh", IO::FileFormat::MEDIT);

The output is Square.medit.mesh. The program takes no arguments and writes into its working directory.

Convert the loaded mesh

Loading fills a new mesh object. Saving that object with a different format performs the conversion; changing the filename extension alone leaves the stored representation unchanged.

  Mesh loaded;
  loaded.load("Square.medit.mesh", IO::FileFormat::MEDIT);
  loaded.save("Square.mfem.mesh", IO::FileFormat::MFEM);

  Mesh converted;
  converted.load("Square.mfem.mesh", IO::FileFormat::MFEM);

The program reloads Square.mfem.mesh and compares its vertex and cell counts with the original. It returns EXIT_FAILURE if this count check fails. Applications exchanging curved geometry or specialized metadata should also check that their required features are supported by both formats.

Continue with MMG

The MMG I/O example covers loading and saving meshes through the MMG integration. Conversion through the generic mesh interface is useful when only the supported geometry, connectivity and attributes are required.

Build and run

Build the Rodin_IO_MEDIT example target and run the resulting executable from a scratch directory. The expected output reports 25 vertices, 32 triangles and a successful mesh count round trip.

The complete program is available in MEDIT.cpp.

Next steps