# Reversed triangles in vtkPolyData created from points and cells

**URL:** <https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223>\
**Category:** Development\
**Created:** [July 13, 2024, 1:15pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223 "2024-07-13T13:15:02Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 13, 2024, 1:15pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/1 "2024-07-13T13:15:02Z")

</div>

Hi,  
I want to create a mesh vtkPolyData from points and triangles. Here’s my mesh topology.  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/e/e9603e0b6d497f97b16e8aa9a2dd9d24bc0f700d.png)

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

 ![image](https://discourse.vtk.org/uploads/default/original/2X/3/36d8decbbfed439b7f887f88f1ee63768999aa8f.png)

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

 ![image](https://discourse.vtk.org/uploads/default/original/2X/3/3e814fcfcd3d2520758792fc904f6b4a5df5038e.png)

the result would be a valid mesh without any reversed triangles

 ![image](https://discourse.vtk.org/uploads/default/original/2X/c/c23d40a12dc0a8ce0482479732eeacb1abd6aaad.png)

Why in the first case, there are reversed triangles?

Thanks

---

<div class="post-metadata">

**Author:** ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)\
**Post date:** [July 13, 2024, 9:48pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/2 "2024-07-13T21:48:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 14, 2024, 12:41am UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/3 "2024-07-14T00:41:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)\
**Post date:** [July 14, 2024, 12:48pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/4 "2024-07-14T12:48:48Z")

</div>

Hello,

Try to recompute the normals with the `vtkPolyDataNormals` filter:

```cpp
    // 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

---

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 14, 2024, 1:56pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/5 "2024-07-14T13:56:55Z")

</div>

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.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/0/072fe2653e630a62d6a6c8ec3b9a57804d8ce128.png)

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

```

---

<div class="post-metadata">

**Author:** ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)\
**Post date:** [July 14, 2024, 2:40pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/6 "2024-07-14T14:40:20Z")

</div>

Hello,

If you think it’s a bug, please, report it here: [Issues · VTK / VTK · GitLab (kitware.com)](https://gitlab.kitware.com/vtk/vtk/-/issues).

regards,

PC

---

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 15, 2024, 1:42am UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/7 "2024-07-15T01:42:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)\
**Post date:** [July 15, 2024, 11:04am UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/8 "2024-07-15T11:04:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [July 15, 2024, 11:40am UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/9 "2024-07-15T11:40:02Z")

</div>

What is your gitlab username ?

---

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 15, 2024, 12:32pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/10 "2024-07-15T12:32:55Z")

</div>

it’s veggiegJie

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [July 15, 2024, 1:32pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/11 "2024-07-15T13:32:03Z")

</div>

You should now be able to post on gitlab.

---

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 15, 2024, 1:56pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/12 "2024-07-15T13:56:15Z")

</div>

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.”?

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [July 15, 2024, 1:57pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/13 "2024-07-15T13:57:43Z")

</div>

Our antispam system is a bit agressive with new users.

---

<div class="post-metadata">

**Author:** ![gongwaner](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gongwaner/32/6996_2.png) [@gongwaner](https://discourse.vtk.org/u/gongwaner)\
**Post date:** [July 15, 2024, 1:58pm UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/14 "2024-07-15T13:58:51Z")

</div>

I see. Thanks.

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [July 16, 2024, 7:34am UTC](https://discourse.vtk.org/t/reversed-triangles-in-vtkpolydata-created-from-points-and-cells/14223/15 "2024-07-16T07:34:41Z")

</div>

[https://gitlab.kitware.com/vtk/vtk/-/issues/19404](https://gitlab.kitware.com/vtk/vtk/-/issues/19404)
