# vtkClipPolyData incorrectly work

**URL:** https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046
**Category:** Support
**Created:** [January 10, 2024, 2:53pm UTC](https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046 "2024-01-10T14:53:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 10, 2024, 2:53pm UTC](https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046/1 "2024-01-10T14:53:45Z")

</div>

I am using `vtkClipPolyData` to cut a mesh, but it give a incorrect result.

The code is:

```auto
import vtkmodules.all as vtk
import numpy as np

def CreateActor(ply: vtk.vtkPolyData, color=[1, 0, 0]):

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(ply)
    mapper.ScalarVisibilityOff()

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    actor.GetProperty().SetColor(color[0], color[1], color[2])
    actor.GetProperty().SetOpacity(0.3)
    return actor

def CutMesh(ply: vtk.vtkPolyData):
    plyClone = vtk.vtkPolyData()
    plyClone.DeepCopy(ply)

    centers = np.array([
        [25.54791, 131.9023, 570.03764],
        [46.95663, 83.99182, 511.02341],
        [58.50379, 42.54272, 577.24325],
        [37.09508, 90.45326, 636.25748],
    ])

    ############################################################################
    ########## after remove the minus sign, this function output a wrong cut result
    normals = -np.array([ # NOTE THE MINUS SIGN
        [0.34503, -0.93555, 0.07544],
        [-0.07840, 0.05137, 0.99560],
        [-0.34503, 0.93555, -0.07544],
        [0.07840, -0.05137, -0.99560],
    ])
    ############################################################################

    vpoints = vtk.vtkPoints()
    for i in range(4):
        vpoints.InsertNextPoint(centers[i])
    vectors = vtk.vtkFloatArray()
    vectors.SetNumberOfComponents(3)
    for n in normals:
        vectors.InsertNextTuple3(n[0], n[1], n[2])

    planes = vtk.vtkPlanes()
    planes.SetPoints(vpoints)
    planes.SetNormals(vectors)

    clipper = vtk.vtkClipPolyData()
    clipper.SetInputData(plyClone)
    clipper.SetClipFunction(planes)
    clipper.GenerateClippedOutputOff()
    clipper.GenerateClipScalarsOff()
    clipper.SetValue(0)
    clipper.Update()

    newply = clipper.GetOutput()
    return CreateActor(newply, [0, 1, 0])

reader = vtk.vtkOBJReader()
reader.SetFileName('before.obj')
reader.Update()

oriMesh = CreateActor(reader.GetOutput(), [1, 0, 0])
newMesh = CutMesh(reader.GetOutput())

renderer = vtk.vtkRenderer()
renderer.AddActor(oriMesh)
renderer.AddActor(newMesh)
renderer.SetBackground(1, 1, 1)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
iren.Initialize()
iren.Start()

```

The data is: [https://github.com/zhang-qiang-github/data/blob/main/before.obj](https://github.com/zhang-qiang-github/data/blob/main/before.obj)

The result is correct:

![image](https://discourse.vtk.org/uploads/default/original/2X/5/56444f0a7d4ee091966b2d4a25857f3fb6aa9021.png)  
Fig. 1

The whole mesh is the original mesh. The yellow mesh is the cutted result.

However, if I remove the minu sign for `normals`, for example:

```auto

    ############################################################################
    ########## after remove the minus sign, this function output a wrong cutted result
    normals = np.array([ # NOTE THE MINUS SIGN
        [0.34503, -0.93555, 0.07544],
        [-0.07840, 0.05137, 0.99560],
        [-0.34503, 0.93555, -0.07544],
        [0.07840, -0.05137, -0.99560],
    ])
    ############################################################################

```

In my opinion, the cutted part should be the red part in Fig. 1. However, the result is:

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

The environment is:

```auto
win 10
python 3.9.12
vtk 9.2.6

```

Any suggestion is appreciated~~~

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 10, 2024, 3:13pm UTC](https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046/2 "2024-01-10T15:13:38Z")

</div>

Just to double check, the following comments from vtkImplicitFunction.h say the following, hopefully this is the convention you are using.

Implicit functions are real valued functions defined in 3D  
space, w = F(x,y,z). Two primitive operations are required: the ability to  
evaluate the function, and the function gradient at a given point. The  
implicit function divides space into three regions: on the surface  
(F(x,y,z)=w), outside of the surface (F(x,y,z)\>c), and inside the  
surface (F(x,y,z)\<c). (When c is zero, positive values are outside,  
negative values are inside, and zero is on the surface. Note also  
that the function gradient points from inside to outside.)

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 11, 2024, 1:18pm UTC](https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046/4 "2024-01-11T13:18:54Z")

</div>

Yes, that is the convention I am using. However, the result is wrong.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 11, 2024, 8:28pm UTC](https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046/5 "2024-01-11T20:28:36Z")

</div>

It seems you’ve found a problem, thank you. I used “clipper.InsideOutOn()” to get it to work. I also created a quick piece of code to glyph the plane normals with oriented cones (centered on the plane points). With InsideOutOn() I get a better image (with the plane normals shown):

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

I’m not exactly sure what the problem is at this point (probably vtkPlanes, but not sure without detective work), we’ll have to trace through the code etc. at some point. Can you create an issue so we don’t forget??

Here’s the (ugly) glyphing code:

def ShowPlanes():  
showPlanes = vtk.vtkPolyData()

```
centers = np.array([
    [25.54791, 131.9023, 570.03764],
    [46.95663, 83.99182, 511.02341],
    [58.50379, 42.54272, 577.24325],
    [37.09508, 90.45326, 636.25748],
])

############################################################################
########## after remove the minus sign, this function output a wrong cut result
normals = -np.array([ # NOTE THE MINUS SIGN
    [0.34503, -0.93555, 0.07544],
    [-0.07840, 0.05137, 0.99560],
    [-0.34503, 0.93555, -0.07544],
    [0.07840, -0.05137, -0.99560],
])
############################################################################

vpoints = vtk.vtkPoints()
for i in range(4):
    vpoints.InsertNextPoint(centers[i])
vectors = vtk.vtkFloatArray()
vectors.SetNumberOfComponents(3)
for n in normals:
    vectors.InsertNextTuple3(n[0], n[1], n[2])

showPlanes.SetPoints(vpoints)
showPlanes.GetPointData().SetVectors(vectors);

cone = vtk.vtkConeSource()

glypher = vtk.vtkGlyph3D()
glypher.SetInputData(showPlanes)
glypher.SetSourceConnection(cone.GetOutputPort())
glypher.SetScaleFactor(10)
glypher.Update()

glyphs = glypher.GetOutput()
return CreateActor(glyphs, [0, 0, 1])

```

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 13, 2024, 3:56am UTC](https://discourse.vtk.org/t/vtkclippolydata-incorrectly-work/13046/6 "2024-01-13T03:56:58Z")

</div>

> [@will.schroeder](#):
>
> Can you create an issue so we don’t forget??

I have created an issue: [https://gitlab.kitware.com/vtk/vtk/-/issues/19223](https://gitlab.kitware.com/vtk/vtk/-/issues/19223)
