d06 Chapter Contents
d06 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_mesh2d_join (d06dbc)

## 1  Purpose

nag_mesh2d_join (d06dbc) joins together (restitches) two adjacent, or overlapping, meshes.

## 2  Specification

 #include #include
 void nag_mesh2d_join (double eps, Integer nv1, Integer nelt1, Integer nedge1, const double coor1[], const Integer edge1[], const Integer conn1[], const Integer reft1[], Integer nv2, Integer nelt2, Integer nedge2, const double coor2[], const Integer edge2[], const Integer conn2[], const Integer reft2[], Integer *nv3, Integer *nelt3, Integer *nedge3, double coor3[], Integer edge3[], Integer conn3[], Integer reft3[], Integer itrace, const char *outfile, NagError *fail)

## 3  Description

nag_mesh2d_join (d06dbc) joins together two adjacent, or overlapping, meshes. If the two meshes are adjacent then vertices belonging to the part of the boundary forming the common interface should coincide. If the two meshes overlap then vertices and triangles in the overlapping zone should coincide too.
This function is partly derived from material in the MODULEF package from INRIA (Institut National de Recherche en Informatique et Automatique).

None.

## 5  Arguments

1:     epsdoubleInput
On entry: the relative precision of the restitching of the two input meshes (see Section 8).
Suggested value: $0.001$.
Constraint: ${\mathbf{eps}}>0.0$.
2:     nv1IntegerInput
On entry: the total number of vertices in the first input mesh.
Constraint: ${\mathbf{nv1}}\ge 3$.
3:     nelt1IntegerInput
On entry: the number of triangular elements in the first input mesh.
Constraint: ${\mathbf{nelt1}}\le 2×{\mathbf{nv1}}-1$.
4:     nedge1IntegerInput
On entry: the number of boundary edges in the first input mesh.
Constraint: ${\mathbf{nedge1}}\ge 1$.
5:     coor1[$2×{\mathbf{nv1}}$]const doubleInput
On entry: ${\mathbf{coor1}}\left[\left(\mathit{i}-1\right)×2+0\right]$ contains the $x$ coordinate of the $\mathit{i}$th vertex of the first input mesh, for $\mathit{i}=1,2,\dots ,{\mathbf{nv1}}$; while ${\mathbf{coor1}}\left[\left(\mathit{i}-1\right)×2+1\right]$ contains the corresponding $y$ coordinate.
6:     edge1[$3×{\mathbf{nedge1}}$]const IntegerInput
On entry: the specification of the boundary edges of the first input mesh. ${\mathbf{edge1}}\left[\left(j-1\right)×3+0\right]$ and ${\mathbf{edge1}}\left[\left(j-1\right)×3+1\right]$ contain the vertex numbers of the two end points of the $j$th boundary edge. ${\mathbf{edge1}}\left[\left(j-1\right)×3+2\right]$ is a user-supplied tag for the $j$th boundary edge.
Constraint: $1\le {\mathbf{edge1}}\left[\left(\mathit{j}-1\right)×3+\mathit{i}-1\right]\le {\mathbf{nv1}}$ and ${\mathbf{edge1}}\left[\left(\mathit{j}-1\right)×3+0\right]\ne {\mathbf{edge1}}\left[\left(\mathit{j}-1\right)×3+1\right]$, for $\mathit{i}=1,2$ and $\mathit{j}=1,2,\dots ,{\mathbf{nedge1}}$.
7:     conn1[$3×{\mathbf{nelt1}}$]const IntegerInput
On entry: the connectivity between triangles and vertices of the first input mesh. For each triangle $\mathit{j}$, ${\mathbf{conn1}}\left[\left(\mathit{j}-1\right)×3+\mathit{i}-1\right]$ gives the indices of its three vertices (in anticlockwise order), for $\mathit{i}=1,2,3$ and $\mathit{j}=1,2,\dots ,{\mathbf{nelt1}}$.
Constraints:
• $1\le {\mathbf{conn1}}\left[\left(j-1\right)×3+i-1\right]\le {\mathbf{nv1}}$;
• ${\mathbf{conn1}}\left[\left(j-1\right)×3+0\right]\ne {\mathbf{conn1}}\left[\left(j-1\right)×3+1\right]$;
• ${\mathbf{conn1}}\left[\left(\mathit{j}-1\right)×3+0\right]\ne {\mathbf{conn1}}\left[\left(\mathit{j}-1\right)×3+2\right]$ and ${\mathbf{conn1}}\left[\left(\mathit{j}-1\right)×3+1\right]\ne {\mathbf{conn1}}\left[\left(\mathit{j}-1\right)×3+2\right]$, for $\mathit{i}=1,2,3$ and $\mathit{j}=1,2,\dots ,{\mathbf{nelt1}}$.
8:     reft1[nelt1]const IntegerInput
On entry: ${\mathbf{reft1}}\left[\mathit{k}-1\right]$ contains the user-supplied tag of the $\mathit{k}$th triangle from the first input mesh, for $\mathit{k}=1,2,\dots ,{\mathbf{nelt1}}$.
9:     nv2IntegerInput
On entry: the total number of vertices in the second input mesh.
Constraint: ${\mathbf{nv2}}\ge 3$.
10:   nelt2IntegerInput
On entry: the number of triangular elements in the second input mesh.
Constraint: ${\mathbf{nelt2}}\le 2×{\mathbf{nv2}}-1$.
11:   nedge2IntegerInput
On entry: the number of boundary edges in the second input mesh.
Constraint: ${\mathbf{nedge2}}\ge 1$.
12:   coor2[$2×{\mathbf{nv2}}$]const doubleInput
On entry: ${\mathbf{coor2}}\left[\left(\mathit{i}-1\right)×2+0\right]$ contains the $x$ coordinate of the $\mathit{i}$th vertex of the second input mesh, for $\mathit{i}=1,2,\dots ,{\mathbf{nv2}}$; while ${\mathbf{coor2}}\left[\left(\mathit{i}-1\right)×2+1\right]$ contains the corresponding $y$ coordinate.
13:   edge2[$3×{\mathbf{nedge2}}$]const IntegerInput
On entry: the specification of the boundary edges of the second input mesh. ${\mathbf{edge2}}\left[\left(j-1\right)×3+0\right]$ and ${\mathbf{edge2}}\left[\left(j-1\right)×3+1\right]$ contain the vertex numbers of the two end points of the $j$th boundary edge. ${\mathbf{edge2}}\left[\left(j-1\right)×3+2\right]$ is a user-supplied tag for the $j$th boundary edge.
Constraint: $1\le {\mathbf{edge2}}\left[\left(\mathit{j}-1\right)×3+\mathit{i}-1\right]\le {\mathbf{nv2}}$ and ${\mathbf{edge2}}\left[\left(\mathit{j}-1\right)×3+0\right]\ne {\mathbf{edge2}}\left[\left(\mathit{j}-1\right)×3+1\right]$, for $\mathit{i}=1,2$ and $\mathit{j}=1,2,\dots ,{\mathbf{nedge2}}$.
14:   conn2[$3×{\mathbf{nelt2}}$]const IntegerInput
On entry: the connectivity between triangles and vertices of the second input mesh. For each triangle $\mathit{j}$, ${\mathbf{conn2}}\left[\left(\mathit{j}-1\right)×3+\mathit{i}-1\right]$ gives the indices of its three vertices (in anticlockwise order), for $\mathit{i}=1,2,3$ and $\mathit{j}=1,2,\dots ,{\mathbf{nelt2}}$.
Constraints:
• $1\le {\mathbf{conn2}}\left[\left(j-1\right)×3+i-1\right]\le {\mathbf{nv2}}$;
• ${\mathbf{conn2}}\left[\left(j-1\right)×3+0\right]\ne {\mathbf{conn2}}\left[\left(j-1\right)×3+1\right]$;
• ${\mathbf{conn2}}\left[\left(\mathit{j}-1\right)×3+0\right]\ne {\mathbf{conn2}}\left[\left(\mathit{j}-1\right)×3+2\right]$ and ${\mathbf{conn2}}\left[\left(\mathit{j}-1\right)×3+1\right]\ne {\mathbf{conn2}}\left[\left(\mathit{j}-1\right)×3+2\right]$, for $\mathit{i}=1,2,3$ and $\mathit{j}=1,2,\dots ,{\mathbf{nelt2}}$.
15:   reft2[nelt2]const IntegerInput
On entry: ${\mathbf{reft2}}\left[\mathit{k}-1\right]$ contains the user-supplied tag of the $\mathit{k}$th triangle from the second input mesh, for $\mathit{k}=1,2,\dots ,{\mathbf{nelt2}}$.
16:   nv3Integer *Output
On exit: the total number of vertices in the resulting mesh.
17:   nelt3Integer *Output
On exit: the number of triangular elements in the resulting mesh.
18:   nedge3Integer *Output
On exit: the number of boundary edges in the resulting mesh.
19:   coor3[$\mathit{dim}$]doubleOutput
Note: the dimension, dim, of the array coor3 must be at least $2×\left({\mathbf{nv1}}+{\mathbf{nv2}}\right)$.
On exit: ${\mathbf{coor3}}\left[\left(\mathit{i}-1\right)×2+0\right]$ will contain the $x$ coordinate of the $\mathit{i}$th vertex of the resulting mesh, for $\mathit{i}=1,2,\dots ,{\mathbf{nv3}}$; while ${\mathbf{coor3}}\left[\left(i-1\right)×2+1\right]$ will contain the corresponding $y$ coordinate.
20:   edge3[$\mathit{dim}$]IntegerOutput
Note: the dimension, dim, of the array edge3 must be at least $3×\left({\mathbf{nedge1}}+{\mathbf{nedge2}}\right)$. This may be reduced to ${\mathbf{nedge3}}$ once that value is known.
On exit: the specification of the boundary edges of the resulting mesh. ${\mathbf{edge3}}\left[\left(j-1\right)×3+i-1\right]$ will contain the vertex number of the $i$th end point ($i=1,2$) of the $j$th boundary or interface edge.
If the two meshes overlap, ${\mathbf{edge3}}\left[\left(j-1\right)×3+2\right]$ will contain the same tag as the corresponding edge belonging to the first and/or the second input mesh.
If the two meshes are adjacent,
 (i) if the $j$th edge is part of the partition interface, then ${\mathbf{edge3}}\left[\left(j-1\right)×3+2\right]$ will contain the value $1000×{k}_{1}+{k}_{2}$ where ${k}_{1}$ and ${k}_{2}$ are the tags for the same edge of the first and the second mesh respectively; (ii) otherwise, ${\mathbf{edge3}}\left[\left(j-1\right)×3+2\right]$ will contain the same tag as the corresponding edge belonging to the first and/or the second input mesh.
21:   conn3[$\mathit{dim}$]IntegerOutput
Note: the dimension, dim, of the array conn3 must be at least $3×\left({\mathbf{nelt1}}+{\mathbf{nelt2}}\right)$. This may be reduced to ${\mathbf{nelt3}}$ once that value is known.
On exit: the connectivity between triangles and vertices of the resulting mesh. ${\mathbf{conn3}}\left[\left(\mathit{j}-1\right)×3+\mathit{i}-1\right]$ will give the indices of its three vertices (in anticlockwise order), for $\mathit{i}=1,2,3$ and $\mathit{j}=1,2,\dots ,{\mathbf{nelt3}}$.
22:   reft3[$\mathit{dim}$]IntegerOutput
Note: the dimension, dim, of the array reft3 must be at least ${\mathbf{nelt1}}+{\mathbf{nelt2}}$. This may be reduced to ${\mathbf{nelt3}}$ once that value is known.
On exit: if the two meshes form a partition, ${\mathbf{reft3}}\left[\mathit{k}-1\right]$ will contain the same tag as the corresponding triangle belonging to the first or the second input mesh, for $\mathit{k}=1,2,\dots ,{\mathbf{nelt3}}$. If the two meshes overlap, then ${\mathbf{reft3}}\left[\mathit{k}-1\right]$ will contain the value $1000×{\mathit{k}}_{1}+{\mathit{k}}_{2}$ where ${\mathit{k}}_{1}$ and ${\mathit{k}}_{2}$ are the user-supplied tags for the same triangle of the first and the second mesh respectively, for $\mathit{k}=1,2,\dots ,{\mathbf{nelt3}}$.
23:   itraceIntegerInput
On entry: the level of trace information required from nag_mesh2d_join (d06dbc).
${\mathbf{itrace}}\le 0$
No output is generated.
${\mathbf{itrace}}\ge 1$
Details about the common vertices, edges and triangles to both meshes are printed.
24:   outfileconst char *Input
On entry: the name of a file to which diagnostic output will be directed. If outfile is NULL the diagnostic output will be directed to standard output.
25:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{nedge1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nedge1}}\ge 1$.
On entry, ${\mathbf{nedge2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nedge2}}\ge 1$.
On entry, ${\mathbf{nv1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nv1}}\ge 3$.
On entry, ${\mathbf{nv2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nv2}}\ge 3$.
NE_INT_2
On entry, ${\mathbf{nelt1}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nv1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nelt1}}\le 2×{\mathbf{nv1}}-1$.
On entry, ${\mathbf{nelt2}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nv2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nelt2}}\le 2×{\mathbf{nv2}}-1$.
On entry, the endpoints of edge $j$ in the first mesh have the same index $i$: $j=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, the endpoints of edge $j$ in the second mesh have the same index $i$: $j=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, vertices $1$ and $2$ of triangle $k$ in the first mesh have the same index $i$: $k=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, vertices $1$ and $2$ of triangle $k$ in the second mesh have the same index $i$: $k=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, vertices $1$ and $3$ of triangle $k$ in the first mesh have the same index $i$: $k=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, vertices $1$ and $3$ of triangle $k$ in the second mesh have the same index $i$: $k=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, vertices $2$ and $3$ of triangle $k$ in the first mesh have the same index $i$: $k=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
On entry, vertices $2$ and $3$ of triangle $k$ in the second mesh have the same index $i$: $k=〈\mathit{\text{value}}〉$ and $i=〈\mathit{\text{value}}〉$.
NE_INT_4
On entry, ${\mathbf{CONN1}}\left(i,j\right)=〈\mathit{\text{value}}〉$, $i=〈\mathit{\text{value}}〉$, $j=〈\mathit{\text{value}}〉$ and ${\mathbf{nv1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{CONN1}}\left(i,j\right)\ge 1$ and ${\mathbf{CONN1}}\left(i,j\right)\le {\mathbf{nv1}}$, where ${\mathbf{CONN1}}\left(i,j\right)$ denotes ${\mathbf{conn1}}\left[\left(j-1\right)×3+i-1\right]$.
On entry, ${\mathbf{CONN2}}\left(i,j\right)=〈\mathit{\text{value}}〉$, $i=〈\mathit{\text{value}}〉$, $j=〈\mathit{\text{value}}〉$ and ${\mathbf{nv2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{CONN2}}\left(i,j\right)\ge 1$ and ${\mathbf{CONN2}}\left(i,j\right)\le {\mathbf{nv2}}$, where ${\mathbf{CONN2}}\left(i,j\right)$ denotes ${\mathbf{conn2}}\left[\left(j-1\right)×3+i-1\right]$.
On entry, ${\mathbf{EDGE1}}\left(i,j\right)=〈\mathit{\text{value}}〉$, $i=〈\mathit{\text{value}}〉$, $j=〈\mathit{\text{value}}〉$ and ${\mathbf{nv1}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{EDGE1}}\left(i,j\right)\ge 1$ and ${\mathbf{EDGE1}}\left(i,j\right)\le {\mathbf{nv1}}$, where ${\mathbf{EDGE1}}\left(i,j\right)$ denotes ${\mathbf{edge1}}\left[\left(j-1\right)×3+i-1\right]$.
On entry, ${\mathbf{EDGE2}}\left(i,j\right)=〈\mathit{\text{value}}〉$, $i=〈\mathit{\text{value}}〉$, $j=〈\mathit{\text{value}}〉$ and ${\mathbf{nv2}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{EDGE2}}\left(i,j\right)\ge 1$ and ${\mathbf{EDGE2}}\left(i,j\right)\le {\mathbf{nv2}}$, where ${\mathbf{EDGE2}}\left(i,j\right)$ denotes ${\mathbf{edge2}}\left[\left(j-1\right)×3+i-1\right]$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_MESH_ERROR
A serious error has occurred in an internal call to the restitching routine. Check the input of the two meshes, especially the edges/vertices and/or the triangles/vertices connectivities. Seek expert help.
The function has detected a different number of coincident edges from the two meshes on the partition interface $〈\mathit{\text{value}}〉$ $〈\mathit{\text{value}}〉$. Check the input of the two meshes, especially the edges/vertices connectivity.
The function has detected a different number of coincident triangles from the two meshes in the overlapping zone $〈\mathit{\text{value}}〉$ $〈\mathit{\text{value}}〉$. Check the input of the two meshes, especially the triangles/vertices connectivity.
The function has detected only $〈\mathit{\text{value}}〉$ coincident vertices with a precision ${\mathbf{eps}}=〈\mathit{\text{value}}〉$. Either eps should be changed or the two meshes are not restitchable.
NE_NOT_CLOSE_FILE
Cannot close file $〈\mathit{\text{value}}〉$.
NE_NOT_WRITE_FILE
Cannot open file $〈\mathit{\text{value}}〉$ for writing.
NE_REAL
On entry, ${\mathbf{eps}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{eps}}>0.0$.

## 7  Accuracy

Not applicable.

nag_mesh2d_join (d06dbc) finds all the common vertices between the two input meshes using the relative precision of the restitching argument eps. You are advised to vary the value of eps in the neighbourhood of $0.001$ with ${\mathbf{itrace}}\ge 1$ to get the optimal value for the meshes under consideration.

## 9  Example

For this function two examples are presented. There is a single example program for nag_mesh2d_join (d06dbc), with a main program and the code to solve the two example problems given in Example 1 (ex1) and Example 2 (ex2).
Example 1 (ex1)
This example involves the unit square ${\left[0,1\right]}^{2}$ meshed uniformly, and then translated by a vector $\stackrel{\to }{u}=\left(\begin{array}{c}{u}_{1}\\ {u}_{2}\end{array}\right)$ (using nag_mesh2d_trans (d06dac)). This translated mesh is then restitched with the original mesh. Two cases are considered:
 (a) overlapping meshes (${u}_{1}=15.0$, ${u}_{2}=17.0$), (b) partitioned meshes (${u}_{1}=19.0$, ${u}_{2}=0.0$).
The mesh on the unit square has $400$ vertices, $722$ triangles and its boundary has $76$ edges. In the overlapping case the resulting geometry is shown in Figure 1 and Figure 2. The resulting geometry for the partitioned meshes is shown in Figure 3.
Example 2 (ex2)
This example restitches three geometries by calling the function nag_mesh2d_join (d06dbc) twice. The result is a mesh with three partitions. The first geometry is meshed by the Delaunay–Voronoi process (using nag_mesh2d_delaunay (d06abc)), the second one meshed by an Advancing Front algorithm (using nag_mesh2d_front (d06acc)), while the third one is the result of a rotation (by $-\pi /2$) of the second one (using nag_mesh2d_trans (d06dac)). The resulting geometry is shown in Figure 4 and Figure 5.

### 9.1  Program Text

Program Text (d06dbce.c)

### 9.2  Program Data

Program Data (d06dbce.d)

### 9.3  Program Results

Program Results (d06dbce.r)

Figure 1: The boundary and the interior interfaces of the two partitioned squares geometry

Figure 2: The interior mesh of the two partitioned squares geometry

Figure 3: The boundary and the interior interfaces (left); the interior mesh (right)
of the two overlapping squares geometry

Figure 4: The boundary and the interior interfaces of the double restitched geometry

Figure 5: The interior mesh of the double restitched geometry