# vtkPolyDataNormals appears to behave incorrectly

**URL:** <https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988>\
**Category:** Development\
**Created:** [July 20, 2023, 5:58am UTC](https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988 "2023-07-20T05:58:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![whereismymoney](https://discourse.vtk.org/user_avatar/discourse.vtk.org/whereismymoney/32/6859_2.png) [@whereismymoney](https://discourse.vtk.org/u/whereismymoney)\
**Post date:** [July 20, 2023, 5:58am UTC](https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988/1 "2023-07-20T05:58:52Z")

</div>

The sample code I provide here allows you to easily compare SetComputePointNormals(true) and SetComputePointNormals(false), so we can compare the correct behavior with the incorrect behavior, shown in this image:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/e/e346b89f92bf58f9fb936e6dce619705505ea986.png)  
Here is the code:

```auto
#include <vtkActor.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkCleanPolyData.h>

int main(int argc, char* argv[])
{
	if(argc != 3) {
		std::cout <<
		"Error: We need the filename in argv[1], and argv[2] should be Y or N for ComputePointNormals"
		<< std::endl;
		return EXIT_FAILURE;
	}

	vtkNew<vtkPolyDataReader> reader;
	reader->SetFileName(argv[1]);
	reader->Update();

	vtkNew<vtkCleanPolyData> cleaner;
	cleaner->SetInputData(reader->GetOutput());
	cleaner->ConvertPolysToLinesOn();
	cleaner->ConvertLinesToPointsOn();

	vtkNew<vtkPolyDataNormals> normals;
	normals->SetInputConnection(cleaner->GetOutputPort());
	normals->ComputeCellNormalsOn();
	normals->SetComputePointNormals(argv[2][0] == 'Y');

	vtkNew<vtkPolyDataMapper> mapper;
	mapper->SetInputConnection(normals->GetOutputPort());

	vtkNew<vtkActor> actor;
	actor->SetMapper(mapper);

	vtkNew<vtkRenderer> renderer;
	renderer->AddActor(actor);
	renderer->SetBackground(0.1, 0.4, 0.1);

	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->AddRenderer(renderer);
	renderWindow->SetSize(1500, 1000);
	renderWindow->SetWindowName(argv[2]);

	vtkNew<vtkRenderWindowInteractor> interactor;
	interactor->SetRenderWindow(renderWindow);

	interactor->Initialize();
	renderWindow->Render();
	renderWindow->Render();
	interactor->Start();

	return EXIT_SUCCESS;
}

```

And here is the file containing the polydata:  
[SourceData.vtk](https://discourse.vtk.org/uploads/short-url/yP1CBTlPm1bbFyi7KoONT99JOn.vtk) (282.7 KB)

You run the program like this:  
programName SourceData.vtk Y  
or like this:  
programName SourceData.vtk N  
The Y/N on the command line controls the boolean going into SetComputePointNormals()

---

<div class="post-metadata">

**Author:** ![jfausty](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jfausty/32/5372_2.png) [@jfausty](https://discourse.vtk.org/u/jfausty)\
**Post date:** [July 20, 2023, 3:45pm UTC](https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988/2 "2023-07-20T15:45:45Z")

</div>

Hi @whereismymoney,

Thanks for posting!

Indeed, that doesn’t look great and it could be related to the normals calculation.

Could you try the same thing without the clean filter please?

Best,  
Julien

---

<div class="post-metadata">

**Author:** ![whereismymoney](https://discourse.vtk.org/user_avatar/discourse.vtk.org/whereismymoney/32/6859_2.png) [@whereismymoney](https://discourse.vtk.org/u/whereismymoney)\
**Post date:** [July 20, 2023, 4:53pm UTC](https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988/3 "2023-07-20T16:53:43Z")

</div>

Hi Julien,  
Thank you for your response. Without the cleaner, both the SetComputePointNormals(true) and SetComputePointNormals(false) cases look good.  
Best,  
Matthias

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

---

<div class="post-metadata">

**Author:** ![whereismymoney](https://discourse.vtk.org/user_avatar/discourse.vtk.org/whereismymoney/32/6859_2.png) [@whereismymoney](https://discourse.vtk.org/u/whereismymoney)\
**Post date:** [July 20, 2023, 4:59pm UTC](https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988/4 "2023-07-20T16:59:21Z")

</div>

In our product, we must include the clean filter [with ConvertPolysToLinesOn() and ConvertLinesToPointsOn()] to avoid a crash in vtkPolyDataNormals for other more complicated test cases.

---

<div class="post-metadata">

**Author:** ![whereismymoney](https://discourse.vtk.org/user_avatar/discourse.vtk.org/whereismymoney/32/6859_2.png) [@whereismymoney](https://discourse.vtk.org/u/whereismymoney)\
**Post date:** [July 22, 2023, 11:33pm UTC](https://discourse.vtk.org/t/vtkpolydatanormals-appears-to-behave-incorrectly/11988/5 "2023-07-22T23:33:55Z")

</div>

The polydata I provided has a lot of problems. Here is a report prepared by Mathematica

 ![image](https://discourse.vtk.org/uploads/default/original/2X/a/a628c9b4e4f62de410ee984764c4029a1cdc9a64.jpeg)
