# normals of the polydata are messed up after transform

**URL:** https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067
**Category:** Support
**Created:** [August 2, 2022, 7:17pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067 "2022-08-02T19:17:34Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 7:17pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/1 "2022-08-02T19:17:34Z")

</div>

```
   vtkNew<vtkTransform> transform;
transform->PostMultiply();
transform->Scale(1.0, vtkMath::Norm(axisY) / m_Thickness, 1.0);
transform->Concatenate(C);
transform->Update();

vtkNew<vtkTransformPolyDataFilter> transformPolyDataFilter;
transformPolyDataFilter->SetInputData(tubeFilter->GetOutput());
transformPolyDataFilter->SetTransform(transform);
transformPolyDataFilter->Update();

vtkNew<vtkTriangleFilter> triangle;
triangle->SetInputData(transformPolyDataFilter->GetOutput());
triangle->Update();

vtkNew<vtkPolyDataNormals> normalGenerator;
normalGenerator->SetInputData(triangle->GetOutput());
normalGenerator->ComputePointNormalsOn();
normalGenerator->ComputeCellNormalsOn();
normalGenerator->AutoOrientNormalsOn();
normalGenerator->NonManifoldTraversalOff();
normalGenerator->ConsistencyOff();
normalGenerator->Update();

```

Code is as above. So I create a tube and then apply transform to it (C is the matrix) and then display the normalGenerator-\>GetOutput()

 ![1659467782778](https://discourse.vtk.org/uploads/default/original/2X/2/2db31c0ce6820b08c6e868852eab18accc77e866.png)  
 ![1659467835744](https://discourse.vtk.org/uploads/default/original/2X/a/aff60d5dd5fb64becfbf8cdc9579ca5b13099ad2.png)

But here you can see, one of the face is pointing inward. (Blue means normals are pointing outwards and pink means inwards)

Can anyone helps me with this?  
Thank you!

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 7:58pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/2 "2022-08-02T19:58:28Z")

</div>

It looks like one of the end faces is wound the wrong way. That would cause vtkPolyDataNormals to produce “inward” normals instead of “outward” normals.

I suspect that vtkTransformPolyDataFilter is not the problem. Instead, I think you might have discovered a bug in vtkTubeFilter: it winds one end cap correctly, but winds the other end cap inside-out.

If you remove vtkTransformPolyDataFilter, but keep vtkPolyDataNormals, do you get the same result? Try the following pipeline:

```
vtkTubeFilter -> vtkTriangleFilter -> vtkPolyDataNormals

```

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 8:01pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/3 "2022-08-02T20:01:28Z")

</div>

If I remove vtkTransformPolyDataFilter, everything looks correct. And If I only remove the Concatenation, it also works. I also tried some combinations of rotation matrix. And some work but some don’t.  
Also, I tried to use vtkCylinderSource as well. It works the same as tube. They both have issues.

Thank you!

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 8:09pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/4 "2022-08-02T20:09:42Z")

</div>

Can you give a sample `C` matrix that doesn’t work?

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 8:12pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/5 "2022-08-02T20:12:50Z")

</div>

For example, I just did this:  
transform-\>RotateX(45);  
transform-\>RotateY(60);  
transform-\>RotateZ(80);

And the Tube’s axis is along z axis

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 9:12pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/6 "2022-08-02T21:12:02Z")

</div>

I can confirm that the problem exists. In fact, for some rotations both of the end caps are incorrectly oriented.  
 ![Cylinder](https://discourse.vtk.org/uploads/default/original/2X/3/3faccd5e0e52187aef2fd5d463699dd4fe4a3566.png)  
But the problem goes away with this small change:

```diff
- normalGenerator->AutoOrientNormalsOn();
+ normalGenerator->AutoOrientNormalsOff();

```

This is very strange, because rotating the polydata should have absolutely no impact on this setting. This indicates either a bug in vtkTransformPolyDataFilter (which I doubt), or a bug in the AutoOrientNormals code in vtkPolyDataNormals (which seems more likely).

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 9:24pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/7 "2022-08-02T21:24:35Z")

</div>

I tried that too.  
If it is turned off, the whole object is pointing inwards. If I turn it on, there is this one face pointing inwards.  
Is there a good solution for me to make them all pointing outwards? I can simply flip it but not sure if it is always the case

 ![1659475449739](https://discourse.vtk.org/uploads/default/original/2X/d/d56957ebbcfd0d04aa76b6f9b1d6219a0e9c9265.png)

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 9:28pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/8 "2022-08-02T21:28:17Z")

</div>

Yeah. In my cases, different rotations make different situations.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 9:32pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/9 "2022-08-02T21:32:04Z")

</div>

It seems the only reason that _all_ normals would point inwards is if the `m_Thickness` in your code is negative.

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 9:48pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/10 "2022-08-02T21:48:36Z")

</div>

Just checked, it is positive. The bug is still there even I comment out the scale

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 9:52pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/11 "2022-08-02T21:52:34Z")

</div>

You’ll have to show how to reproduce the “all pointing inwards” situation, then.

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 9:55pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/12 "2022-08-02T21:55:50Z")

</div>

```
lineSource->SetPoint1(0.0, 0.0, 1.0);
lineSource->SetPoint2(0.0, 0.0, -1.0);
lineSource->Update();

vtkNew<vtkTubeFilter> tubeFilter;
tubeFilter->SetInputData(lineSource->GetOutput());
tubeFilter->SetNumberOfSides(30);
tubeFilter->SetRadius(1.0);
tubeFilter->CappingOn();
tubeFilter->Update();

```

This is the tube creation  
Then I just turned off the AutoOrientNormals

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 2, 2022, 10:04pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/13 "2022-08-02T22:04:20Z")

</div>

This is the C matrix:

-0.99830902570539681 0.057867716719294471 0.0055151207454731122 -4.1893909586657259 0.053899618858214371 0.95701416540486317 -0.28498897926301042 -0.17257266383903502 0.021769710197448333 0.28420980731869339 0.9585149269269575 6.0625220235805708  
0 0 0 1

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 10:11pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/14 "2022-08-02T22:11:52Z")

</div>

That’s not a rotation matrix, it has a determinant of -1 instead of +1. How was it computed?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 2, 2022, 10:36pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/15 "2022-08-02T22:36:50Z")

</div>

In any case, you can probably fix the issue by using `AutoOrientNormalsOff()` and by adding the following to your pipeline ( ~~it doesn’t matter where,~~ any place after vtkTriangleFilter will work).

```nohighlight
// check if the matrix turns the data inside-out
bool flipped = (C->Determinant() < 0.0);

vtkNew<vtkReverseSense> reverse;
reverse->SetInputData(xxx->GetOutput());
reverse->SetReverseNormals(flipped);
reverse->SetReverseCells(flipped);
reverse->Update();

```

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 3, 2022, 1:36pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/16 "2022-08-03T13:36:43Z")

</div>

Yeah! It works! I might screw up the coordinates. But thank you!

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 4, 2022, 2:23pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/17 "2022-08-04T14:23:55Z")

</div>

Hi David, just some follow ups. I used an OBB Tree for an object, and then use (max, mid, min) as the new axes for the new coordinates. And I found out that (max, mid, min) do not make a right-handed coordinates. This is the issue causing the negative Determinant

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [August 4, 2022, 2:39pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/18 "2022-08-04T14:39:03Z")

</div>

Ah. When this happens, one option is to use a different corner to define the bounding box, e.g.

```nohighlight
   if (vtkMath::Determinant3x3(max, mid, min) < 0.0)
   {
     for (int i = 0; i < 3; ++i)
     {
       corner[i] += size[2]*min[i];
       min[i] = -min[i];
     }
   }

```

---

<div class="post-metadata">

### Author: ![Hanxin\_Hua](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hanxin_hua/32/3787_2.png) [@Hanxin\_Hua](https://discourse.vtk.org/u/Hanxin_Hua)
#### Post date: [August 4, 2022, 2:40pm UTC](https://discourse.vtk.org/t/normals-of-the-polydata-are-messed-up-after-transform/9067/19 "2022-08-04T14:40:13Z")

</div>

Thank you!
