# VTK9 create different stencil than vtk8

**URL:** https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560
**Category:** Support
**Created:** [January 30, 2023, 2:12pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560 "2023-01-30T14:12:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lucas\_rouhier](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucas_rouhier/32/6471_2.png) [@lucas\_rouhier](https://discourse.vtk.org/u/lucas_rouhier)
#### Post date: [January 30, 2023, 2:12pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/1 "2023-01-30T14:12:46Z")

</div>

Hello,  
I am having some weird results while trying to update my VTK.

I am converting a VTK polydata to Image data with the following code:  
vtkSmartPointer VtkPolyDataToImage(vtkPolyData\* poly, vtkImageData\* templateImage)  
{  
vtkSmartPointer image = vtkSmartPointer::New();  
image-\>DeepCopy(templateImage);

// fill the image with foreground voxels:  
short inval = 1;  
short outval = 0;  
vtkIdType count = templateImage-\>GetNumberOfPoints();  
for (vtkIdType i = 0; i \< count; ++i)  
{  
image-\>GetPointData()-\>GetScalars()-\>SetTuple1(i, inval);  
}

// polygonal data → image stencil:  
vtkSmartPointer pol2stenc =  
vtkSmartPointer::New();  
pol2stenc-\>SetInputData(poly);  
pol2stenc-\>SetOutputOrigin(image-\>GetOrigin());  
pol2stenc-\>SetOutputSpacing(image-\>GetSpacing());  
pol2stenc-\>SetOutputWholeExtent(image-\>GetExtent());  
pol2stenc-\>Update();

// cut the corresponding white image and set the background:  
vtkSmartPointer imgstenc = vtkSmartPointer::New();  
imgstenc-\>SetInputData(image);  
imgstenc-\>SetStencilConnection(pol2stenc-\>GetOutputPort());  
imgstenc-\>ReverseStencilOff();  
imgstenc-\>SetBackgroundValue(outval);  
imgstenc-\>Update();

vtkSmartPointer outputImage = imgstenc-\>GetOutput();  
return outputImage;  
}

However, After upgrading the created image is different (like 0.2 max difference).

Do you have any idea about what could the issue here ? Is it a known issue ?

Thanks in advance

---

<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: [January 30, 2023, 4:06pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/2 "2023-01-30T16:06:57Z")

</div>

Can you explain what you mean by “0.2 max difference”? I don’t understad what the “0.2” refers to.

The vtkPolyDataToImageStencil class has only been stylistically modified between VTK 8.2.0 and VTK 9.2.5. The algorithm hasn’t changed.

---

<div class="post-metadata">

### Author: ![lucas\_rouhier](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucas_rouhier/32/6471_2.png) [@lucas\_rouhier](https://discourse.vtk.org/u/lucas_rouhier)
#### Post date: [January 30, 2023, 4:35pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/3 "2023-01-30T16:35:45Z")

</div>

Sure ! I remeshed the image back to an STL and computed the difference between the 2 meshes (vtk 8 one and vtk9 one) (point by point euclidean distance). That is what gave me the 0.2 difference.

---

<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: [January 30, 2023, 4:42pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/4 "2023-01-30T16:42:16Z")

</div>

You weren’t directly comparing the images to each other? What did you use to do the remeshing? And what was the voxel spacing?

---

<div class="post-metadata">

### Author: ![lucas\_rouhier](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucas_rouhier/32/6471_2.png) [@lucas\_rouhier](https://discourse.vtk.org/u/lucas_rouhier)
#### Post date: [January 30, 2023, 4:49pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/5 "2023-01-30T16:49:37Z")

</div>

No I did not because I first saw the error on remeshed STL’s and I did not think about the meshing part… Which will also be impacted by that vtk9 → vtk9 upgrade

here is the code used to remesh the image : [Meshing code text code | WTOOLS](https://wtools.io/paste-code/bJpp) (using marching cube)

Voxel spacing is 0.15

---

<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: [January 30, 2023, 4:57pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/6 "2023-01-30T16:57:44Z")

</div>

The vtkQuadricDecimation filter has definitely been changed since VTK 8.2.

To check vtkPolyDataToImageStencil, please do a voxel-by-voxel comparison of the image outputs. Or if that isn’t possible, then compare the vtkMarchingCubes meshes without decimating them first.

---

<div class="post-metadata">

### Author: ![lucas\_rouhier](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucas_rouhier/32/6471_2.png) [@lucas\_rouhier](https://discourse.vtk.org/u/lucas_rouhier)
#### Post date: [January 30, 2023, 5:01pm UTC](https://discourse.vtk.org/t/vtk9-create-different-stencil-than-vtk8/10560/7 "2023-01-30T17:01:45Z")

</div>

Perfect thanks a lot for your help ! Hopefully that will provide some new information and perhaps a solution.
