reversed triangles produced by vtkSTLWriter
Hi,
I was creating vtkPolyData from points and cells. To check the result, I exported the result vtkPolyData to .stl file. And found reversed triangles in result mesh.
Here's my mesh topology.
And my code to create and export mesh to .stl file
#include <vtkVector.h>
#include <vtkTriangle.h>
#include <vtkPolyData.h>
#include <vtkSTLWriter.h>
vtkSmartPointer<vtkTriangle> GetTriangle(const int vid0, const int vid1, const int vid2)
{
auto triangle = vtkSmartPointer<vtkTriangle>::New();
triangle->GetPointIds()->SetId(0, vid0);
triangle->GetPointIds()->SetId(1, vid1);
triangle->GetPointIds()->SetId(2, vid2);
return triangle;
}
void Test()
{
std::vector<vtkVector3d> inputPoints(5);
inputPoints[0] = vtkVector3d(11.97, 0, 4.75);
inputPoints[1] = vtkVector3d(1.83, 0, 20.15);
inputPoints[2] = vtkVector3d(9.38, 0, 11.95);
inputPoints[3] = vtkVector3d(16, 0, 15);
inputPoints[4] = vtkVector3d(22.73, 0, 17);
auto points = vtkSmartPointer<vtkPoints>::New();
for(const auto& point: inputPoints)
points->InsertNextPoint(point.GetData());
auto cells = vtkSmartPointer<vtkCellArray>::New();
cells->InsertNextCell(GetTriangle(0, 1, 2));
cells->InsertNextCell(GetTriangle(2, 1, 3));
cells->InsertNextCell(GetTriangle(3, 1, 4));
cells->InsertNextCell(GetTriangle(4, 2, 3));
cells->InsertNextCell(GetTriangle(4, 0, 2));
auto mesh = vtkSmartPointer<vtkPolyData>::New();
mesh->SetPoints(points);
mesh->SetPolys(cells);
//const auto outDir = "test.stl";
auto writer = vtkSmartPointer<vtkSTLWriter>::New();
writer->SetFileName(outDir);
writer->SetInputData(mesh);
writer->Write();
}
and the result I get looks like this:
the pink triangles are reversed triangles.
Initially I thought the problem stems from wrong vertices order when creating triangle. But even if I reverse the vertices order of the reversed triangles, I still get the same result. Then I found if I export the mesh to .ply instead of .stl, I get the correct result.
I posted this in VTK discourse forum, if you want more details check this link.
Regards