# Meshes

In this guide, you will learn fundamental elements to understand and manipulate meshes in OpenGeode. Let's begin by introducing all kind of meshes you can work with in OpenGeode.

## Mesh gallery

There are several kind of meshes representing points, lines, surfaces and solids. All these meshes are available in 2D and 3D (excepted solids that are definitely three-dimensional, and vertex sets and graphs that are dimensionless). The dimension is given by a template parameter. For example, two aliases are defined for `TriangulatedSurface`: TriangulatedSurface2D and TriangulatedSurface3D.

## Mesh definitions

We will take the example of a `PolygonalSurface` mesh to give some definitions on meshes and to show how to manipulate meshes. Potential differences with other kind of meshes will be pointed if necessary. Let's take the above polygonal mesh as the example.

### Vertices and Polygons

This `PolygonalSurface` is defined by 11 vertices (left) and 6 polygons (right) as shown on this image. Indexing of vertices is continuous, starts at `0` and ends at `number of vertices - 1`. It is exactly the same of polygon indexing.

To known the number of vertices and the polygons, use the following methods:

``` PolygonalSurface2D surface; const auto nbv = surface.nb_vertices(); const auto nbp = surface.nb_polygons(); ```

Polygons are defined by an ordered list of vertices. For example, the first polygon (with index 0) is defined by vertices: 6, 5, 8, 9, 10. It is easy to get the index of vertices defining a polygon using:

``` PolygonalSurface2D surface; const auto pv0 = surface.polygon_vertex( {0, 0} ); // 6 const auto pv1 = surface.polygon_vertex( {0, 1} ); // 5 const auto pv2 = surface.polygon_vertex( {0, 2} ); // 8 ... ``` This is called `PolygonVertex`.

NB: For Solid, this is called `PolyhedronVertex`.

### Edges and Facets

Even if meshes are entirely defined by vertices and polygons, you can access to mesh facets and edges. Edges are defined for Surfaces and Solids and facets for Solids. As for vertices and polygons, edge and facet indexings are continuous and starts at 0. In the example, the polygonal surface is composed of 16 edges indexed from 0 to 15. As for vertices, it is possible to have access to the global index of a polygon edge. A `PolygonEdge` is the edge that starts from the same `PolygonVertex`: `PolygonEdge` (2, 1) is an edge of the polygon 2 that starts from the `PolygonVertex` (2, 1). NB: For Solid, there are `PolyhedronFacet` and `PolyhedronFacetEdge`.

### Topological queries

Meshes provide numerous methods to query the mesh topology. One information you can get is to know which polygon is adjacent to another one. More precisely, to known which polygon is adjacent to another one through a given `PolygonEdge`, you can use the following code:

``` PolygonalSurface2D surface; const auto polygon = surface.polygon_adjacent( {0, 0} ); // 1 ``` You can have more information and get the adjacent `PolygonEdge`, using the following code:

``` PolygonalSurface2D surface; const auto polygon = surface.polygon_adjacent_edge( {0, 0} ); // {1, 2} ```

If there is no polygon adjacent to the given `PolygonEdge`, the results of these methods are `NO_ID` or the `PolygonEdge` `{NO_ID, NO_ID}`. The given `PolygonEdge` is on border. There is a method to known if a `PolygonEdge` is on border:

``` PolygonalSurface2D surface; const auto on_border0 = surface.is_edge_on_border( {0, 0} ); // false const auto on_border1 = surface.is_edge_on_border( {0, 2} ); // true ``` You can go from one edge on border to anther one using:

``` PolygonalSurface2D surface; PolygonEdge start{0, 2}; auto next_edge = surface.next_on_border( start ); // {0, 3} next_edge = surface.next_on_border( next_edge ); // {0, 4} next_edge = surface.next_on_border( next_edge ); // {1, 0} auto prev_edge = surface.previous_on_border( next_edge ); // {0, 4} ... ```

You can notice that "next" turns in the same direction that polygon vertices, and "previous" turns therefore in the inverse direction.

An exception is raised if you give as method parameter a `PolygonEdge` that is not on border.

Another topological query is to get the polygon list around a vertex:

``` PolygonalSurface2D surface; const auto polygons_around4 = surface.polygons_around( 4 ); // an inlined vector containing {4, 3, 1, 2, 5} ``` ### Geometrical queries

Beside topological queries, you also can ask for geometrical mesh queries such as polygon area, edge length, polygon barycenter and so on.

You can get the (axis-aligned) bounding box of a mesh using:

``` PolygonalSurface2D surface; const auto bbox = surface.bounding_box(); ``` ## Manipulating meshes

All the queries illustrated above are const mesh methods. As a consequence, you can work with a const mesh and have access to all information you want. But if you want to create or modify a mesh, you should use a builder as explained in the following guide.