General concepts » Meshes » Mesh iteration

Traverse vertices, cells, facets and incident entities.

Iterator state

A mesh iterator identifies an entity of a particular dimension. Dereference it while !iterator.end() holds, then advance with ++iterator. Entity indices are local to their dimension. Topology changes can invalidate iterators and indices; finish traversal before rebuilding the mesh.

Select the entity family

Vertices

mesh.getVertex() traverses vertices. The dereferenced object provides its physical coordinates through getCoordinates().

Cells

mesh.getCell() traverses the highest-dimensional entities. The checked example prints each cell's optional attribute and geometric measure.

  for (auto cell = mesh.getCell(); !cell.end(); ++cell)
  {
    std::cout << "Cell " << cell->getIndex();
    if (const auto attribute = cell->getAttribute())
      std::cout << ": attribute " << *attribute;
    std::cout << ", measure " << cell->getMeasure() << '\n';
  }
  size_t boundaryCount = 0;
  for (auto face = mesh.getBoundary(); !face.end(); ++face)
    ++boundaryCount;

Facets

mesh.getFace() traverses codimension-one facets. Compute the relevant connectivity first because edges must exist before a triangular mesh can report its complete facet set.

Select a dimension

mesh.getPolytope(d) traverses entities of dimension d. The same index value in two dimensions identifies different entities. Compute the required lower-dimensional entities before traversing them.

Entity properties

Index

iterator->getIndex() identifies the current entity within its dimension. Mesh rebuilding can assign new indices.

Coordinates

Vertex coordinates describe mesh geometry. An interior sample is represented by a Geometry::Point on a cell; the quadrature example shows this distinction.

Attribute

getAttribute() returns an optional label. Test it before dereferencing or using its value as a key. The source snippet above includes both operations.

Reference shape

getGeometry() returns the Polytope::Type of the current entity. This selects the reference shape for local geometry and finite element operations.

Start at an index

mesh.getCell(i), mesh.getFace(i) and mesh.getVertex(i) return iterators positioned at an existing entity. Check the corresponding entity count before using an externally supplied index.

Cell neighbors

Compute mesh.getConnectivity().compute(d, d) for the cell dimension d before traversing cell->getAdjacent(). The detailed connectivity guide describes incidence and adjacency separately.

Boundary facets

mesh.getBoundary() traverses facets with one incident cell, after facet-to-cell connectivity has been computed. The example counts six boundary segments around its rectangle.

Interior facets

mesh.getInterface() traverses interior facets. For transmission problems, select the desired pair of region attributes before assigning interface labels; see Interfaces, traces, and discontinuous Galerkin.

Complete source

MeshBasics.cpp constructs the mixed mesh, computes incidences, traverses cells and extracts its boundary.

Repeated traversal

Prepare the required incidence relations once before repeated traversal. Retain only the quantities needed by the algorithm; coordinate evaluation and geometric measures may require additional geometry calculations.

Continue