# Incorrect result of boolean difference

**URL:** https://discourse.vtk.org/t/incorrect-result-of-boolean-difference/12422
**Category:** Development
**Tags:** rendering, python
**Created:** [October 5, 2023, 10:03am UTC](https://discourse.vtk.org/t/incorrect-result-of-boolean-difference/12422 "2023-10-05T10:03:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Arnav-2004](https://discourse.vtk.org/user_avatar/discourse.vtk.org/arnav-2004/32/7782_2.png) [@Arnav-2004](https://discourse.vtk.org/u/Arnav-2004)
#### Post date: [October 5, 2023, 10:03am UTC](https://discourse.vtk.org/t/incorrect-result-of-boolean-difference/12422/1 "2023-10-05T10:03:43Z")

</div>

```py
import vtk

cube1 = vtk.vtkCubeSource()
cube1.SetXLength(1.1)
cube1.SetYLength(10)
cube1.SetZLength(10)
cube1.Update()

tri_filter1 = vtk.vtkTriangleFilter()
tri_filter1.SetInputData(cube1.GetOutput())
tri_filter1.Update()

cube2 = vtk.vtkCubeSource()
cube2.SetXLength(1)
cube2.SetYLength(18)
cube2.SetZLength(27)
cube2.Update()

tri_filter2 = vtk.vtkTriangleFilter()
tri_filter2.SetInputData(cube2.GetOutput())
tri_filter2.Update()

boolean_operation = vtk.vtkBooleanOperationPolyDataFilter()
boolean_operation.SetOperationToDifference()
boolean_operation.SetInputConnection(0, tri_filter2.GetOutputPort())
boolean_operation.SetInputConnection(1, tri_filter1.GetOutputPort())
boolean_operation.Update()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(boolean_operation.GetOutput())

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

ren = vtk.vtkRenderer()
ren_win = vtk.vtkRenderWindow()
ren_win.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)

ren.AddActor(actor)
ren.SetBackground(1.0, 1.0, 1.0)

ren_win.Render()
iren.Start()

```

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

This issue was originally raised in PyVista project  
Issue: [Incorrect result of boolean\_difference · Issue #4843 · pyvista/pyvista · GitHub](https://github.com/pyvista/pyvista/issues/4843)

---

<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: [October 7, 2023, 9:47am UTC](https://discourse.vtk.org/t/incorrect-result-of-boolean-difference/12422/2 "2023-10-07T09:47:54Z")

</div>

There was a post related to boolean operation flaws: [mesh flaw occurs when using vtkBooleanOperation - #5 by hyunseoki](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/5)  
You can try vtkbool which is more robust than vtk’s own boolean difference.

---

<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: [October 9, 2023, 7:30am UTC](https://discourse.vtk.org/t/incorrect-result-of-boolean-difference/12422/3 "2023-10-09T07:30:11Z")

</div>

@Charles_Gueunet

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [October 9, 2023, 7:44am UTC](https://discourse.vtk.org/t/incorrect-result-of-boolean-difference/12422/4 "2023-10-09T07:44:03Z")

</div>

@Arnav-2004 You may also want to have a look at the [vespa library](https://gitlab.kitware.com/vtk-cgal/vespa). Leveragin [CGAL](https://www.cgal.org/), it is a VTK module (and a ParaView plugin), described with more details into [this blog](https://www.kitware.com/vespa-advanced-mesh-processing-based-on-cgal-for-vtk-and-paraview/).

It contains a `vtkCGALBooleanOperation` class, you can find an example usage in its corresponding [test](https://gitlab.kitware.com/vtk-cgal/vespa/-/blob/master/vespa/PolygonMeshProcessing/Testing/TestPMPBooleanExecution.cxx?ref_type=heads).
