# Creation of triangular mesh

Hello everyone,
I’m new to VTK and making my way trough it.
I’m working on a ray-casting project, where I’m supposed to run an algorithm that finds the point of intersection of rays with terrain.
My code works like this

1. It generates the points on a uniformly spaced grid
2. It creates a triangular mesh out of the points
3. It builds an OBBTree and find the intersections

The problem arises when I build the mesh, using the vtkTriangleStrip primitive. I wanted to add different TriangleStrips (one for each row of the mesh) to the same vtkCellArray, but I get a random crash on the second cycle (for sure I’m using the memory in an unexpected way). A few strips are added (the number varies according to the run) and then the program crashes.
Do you see anything strange in this code?

vtkNew<vtkPoints>           l_vMeshPoints;
vtkNew<vtkCellArray>        l_vMeshTriangles;

/* Generate the sample points */
l_iN = l_dGridWidth / l_dGridSpacing_m + 1;

for (int l_iRow = 0; l_iRow < l_iN; l_iRow ++)
{
for (int l_iCol = 0; l_iCol < l_iN; l_iCol ++)
{
l_dX = -l_dGridWidth* 0.5 + l_iCol * l_dGridSpacing_m;
l_dY = -l_dGridWidth* 0.5 + l_iRow * l_dGridSpacing_m;
l_dZ = 50;

l_vMeshPoints->InsertNextPoint(l_dX, l_dY, l_dZ);
}
}

for (int l_iRow = 0; l_iRow < l_iN - 1; l_iRow ++)
{
/* Create a new triangle strip */
vtkNew<vtkTriangleStrip>    l_vTriangleStrip;

/* Set the number of expected Ids */
l_vTriangleStrip->GetPointIds()->SetNumberOfIds(2 + 2 * (l_iN - 1));

/* Configure the ids of the points to be assigned to this strip */
int l_iOffset = l_iRow * l_iN;
for (int l_iCol = 0; l_iCol < l_iN ; l_iCol ++)
{
l_vTriangleStrip->GetPointIds()->SetId(l_iOffset + l_iCol, l_iOffset + l_iCol);
l_vTriangleStrip->GetPointIds()->SetId(l_iOffset + l_iCol + l_iN, l_iOffset + l_iCol + l_iN);
}

/* Store this stip in the overall cell-array structure */
l_vMeshTriangles->InsertNextCell(l_vTriangleStrip);
}

Hello, I found the issue and report here the solution.
The SetId function should be called with first parameter as progressive (i.e., for triangles, 0, 1, 2), while the second is the point index.
Mattia