Examples and tutorials » Working with MMG » Simple input/output of MMG objects

How to read and write MMG meshes in MFEM and MEDIT formats.

Introduction

The MMG module provides a mesh class (MMG::Mesh) that extends the standard Mesh with remeshing capabilities. This example shows how to load and save MMG meshes in the supported formats.

Creating an MMG Mesh

An MMG::Mesh can be created using the same UniformGrid() method as a regular mesh. The key difference is that MMG::Mesh stores additional metadata (corners, ridges) that MMG uses during remeshing:

#include <Rodin/Geometry.h>
#include <Rodin/MMG.h>

using namespace Rodin;
using namespace Rodin::Geometry;

int main()
{
  MMG::Mesh mesh;
  mesh = mesh.UniformGrid(Polytope::Type::Triangle, { 16, 16 });
  mesh.getConnectivity().compute(1, 2);

  // Mark corners (points that must be preserved during remeshing)
  mesh.setCorner(0);
  mesh.setCorner(15);
  mesh.setCorner(240);
  mesh.setCorner(255);

  // Mark boundary edges as ridges (sharp features)
  for (auto it = mesh.getBoundary(); !it.end(); ++it)
    mesh.setRidge(it->getIndex());

  return 0;
}

Saving in MEDIT Format

The MEDIT format (.mesh) is the native format for MMG. Use IO::FileFormat::MEDIT:

mesh.save("output.medit.mesh", IO::FileFormat::MEDIT);

Loading and Re-saving

Loading is symmetric:

MMG::Mesh mesh2;
mesh2.load("output.medit.mesh", IO::FileFormat::MEDIT);
mesh2.save("copy.medit.mesh", IO::FileFormat::MEDIT);

Full Source Code

See Also