Reversed triangles in vtkPolyData created from points and cells

Hi,
I want to create a mesh vtkPolyData from points and triangles. Here’s my mesh topology.
image

Given the topology, I can create vtkPolyData from the points and triangles.
suedocode:

    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);

where GetTriangle(int, int, int) returns a vtkSmartPointer<vtkTriangle>.
When I run the program and export the result vtkPolyData to a .stl file, it looks like this

the 2 pink triangles are the ones with reversed normal.
Initially I thought this is because the vertices order of these triangles are wrong, but even if I changed the vertices order, the result is still the same.

Then I thought maybe my code is wrong. I checked the code by keeping the vertices the same, and get rid of triangle (4,2,3) and (4,0,2), as shown in picture

the result would be a valid mesh without any reversed triangles

Why in the first case, there are reversed triangles?

Thanks

Hello,

If the indexes of your cartoon match that of the points in code, then all your faces are counter-clockwise when seen from the inside of the pyramid, which would result in consistent normals (either all pointing to inside or all to the outside). I’d do a double-check on the part of the code where you set the vertexes. They may be in an order different from the cartoon’s.

best,

PC

Hi Paulo,
Thanks for reply. As I said, initially I thought it’s the order problem. ie

 cells->InsertNextCell(GetTriangle(4, 2, 3));
 cells->InsertNextCell(GetTriangle(4, 0, 2));

these 2 lines produce reversed triangles. The vertices are in counter-clockwise order. So if I reverse the vertices of these 2 triangles

 cells->InsertNextCell(GetTriangle(3, 2, 4));
 cells->InsertNextCell(GetTriangle(2, 0, 4));

the vertices are in clockwise order. But this produces the exact same result.

Hello,

Try to recompute the normals with the vtkPolyDataNormals filter:

    // Compute normals
    vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New();
    normals->SetInputData(mesh);
    normals->ComputePointNormalsOn();
    normals->ComputeCellNormalsOn();
    normals->Update();

Use the object returned by normals->GetOutput() or normals->GetOutputPort() depending on whatever the remainder of your pipeline code is.

best,

PC

Hi Paulo,
Thanks for the help. Unfortunately, even with your code, the result mesh exported to .stl file still looks the same as before. However, I did find something interesting: If I change export file extension to PLY instead of STL, I get the correct result with my original code.

I think there might be a bug with vtkSTLWriter?
Here’s my code for reproducing the result above.

#include <vtkVector.h>
#include <vtkTriangle.h>
#include <vtkPolyData.h>
#include <vtkPolyDataNormals.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();
}

Hello,

If you think it’s a bug, please, report it here: Issues · VTK / VTK · GitLab (kitware.com).

regards,

PC

Hi Paulo,
I tried to submit an issue, but GitLab keeps poping up

The form contains the following error:

* Your issue has been recognized as spam. Please, change the content to proceed.

What should I do to get rid of this error message?

Greetings,

Dunno. I’ve never had such message before. I don’k know what you’re trying to post, but do as told: review the message. Describe the problem with the like you did here.

best,

PC

What is your gitlab username ?

it’s veggiegJie

You should now be able to post on gitlab.

Thanks, I created the issue successfully.
May I know what’s the cause of the error message “Your issue has been recognized as spam. Please, change the content to proceed.”?

Our antispam system is a bit agressive with new users.

I see. Thanks.

https://gitlab.kitware.com/vtk/vtk/-/issues/19404