This documentation is being rewritten. If something looks off, please cross-check with the Boost 1.91.0 Boost.Graph docs and open an issue.

Mesh Generator (Input Iterator)

Generate a 2D mesh (grid) graph.

Defined in: <boost/graph/mesh_graph_generator.hpp>

Synopsis

template <typename Graph>
class mesh_iterator;

mesh_iterator();
mesh_iterator(vertices_size_type x, vertices_size_type y, bool toroidal = true);

Generates edges for an x by y mesh graph. Vertices are numbered in row-major order (for a 3x2 mesh: 0,1,2 in the first row, 3,4,5 in the second). Each interior vertex is connected to its 4 neighbors (up, down, left, right). If toroidal is true, boundary vertices wrap around to form a torus. Both x and y must be greater than 1.

Supports both directed and undirected graphs (detected from Graph traits). For directed graphs each adjacency produces edges in all four directions; for undirected graphs each adjacency produces one edge.

Parameters

Direction Parameter Description

IN

vertices_size_type x

Number of columns. Must be > 1.

IN

vertices_size_type y

Number of rows. Must be > 1.

IN

bool toroidal

If true, wrap edges around (torus). Default: true.

Example

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/mesh_graph_generator.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>

int main() {
    using namespace boost;
    using Graph = adjacency_list<>;
    using MeshGen = mesh_iterator<Graph>;

    bool toroidal = false;
    Graph g(MeshGen(2, 3, toroidal), MeshGen(), 6);

    std::cout << num_vertices(g) << " vertices, " << num_edges(g) << " edges\n";
    for (auto e : make_iterator_range(edges(g))) {
        std::cout << "  " << source(e, g) << " -> " << target(e, g) << "\n";
    }
}
6 vertices, 14 edges
  0 -> 1
  0 -> 2
  1 -> 0
  1 -> 3
  2 -> 0
  2 -> 3
  2 -> 4
  3 -> 1
  3 -> 2
  3 -> 5
  4 -> 2
  4 -> 5
  5 -> 3
  5 -> 4