Hello everyone,
I need to work with 3d points of a loaded mesh. Do you provide any example which shows how to access points of a mesh? Thank you for help!
Hello Pierre.
I assume that you have loaded your mesh via a vtk-reader object like vtkOBJReader, vtkPLYReader or vtkSTLReader. So the reader output is of type vtkPolyData*.
Then you can access the points with the GetPoint-Method of vtkPolyData.
Here is a short example:
vtkNew<vtkSTLReader> reader;
reader->SetFileName("myMesh.stl");
reader->Update();
vtkSmartPointer<vtkPolyData> mesh = reader->GetOutput();
for(vtkIdType i = 0; i < mesh->GetNumberOfPoints(); i++)
{
double p[3];
mesh->GetPoint(i, p); // get the i-th point of the mesh and store it in p[3]
// do what you want with the point p[3] here
// p[0] -> x-component, p[1] -> y-component, p[2] -> z-component
}
If you want to access the 3 points of each triangle of the mesh then you can do the following:
vtkSmartPointer<vtkPoints> points = mesh->GetPoints();
vtkSmartPointer<vtkDataArray> dataArray = points->GetData();
vtkIdType numberOfTriangles = mesh->GetNumberOfCells();
vtkNew<vtkIdList> faceIndex;
for(vtkIdType i = 0; i < numberOfTriangles; i++)
{
mesh->GetCellPoints(i, faceIndex);
vtkIdType vertexIndex;
vtkVector3d vertex[3];
for(int j = 0; j < 3; j++)
{
// get all three vertex indices of triangle number i
vertexIndex = faceIndex->GetId(j);
vertex[j][0] = dataArray->GetComponent(vertexIndex, 0);
vertex[j][1] = dataArray->GetComponent(vertexIndex, 1);
vertex[j][2] = dataArray->GetComponent(vertexIndex, 2);
}
// p0, p1, p2 are the points of the i-th triangle
vtkVector3d p0(vertex[0][0], vertex[0][1], vertex[0][2]);
vtkVector3d p1(vertex[1][0], vertex[1][1], vertex[1][2]);
vtkVector3d p2(vertex[2][0], vertex[2][1], vertex[2][2]);
}
Hope it helps.
Thank you a lot Berti, it is exactly what I was looking for. Everything is now well integrated in my application.