Incorrect result of boolean difference

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()

This issue was originally raised in PyVista project
Issue: Incorrect result of boolean_difference · Issue #4843 · pyvista/pyvista · GitHub

1 Like

There was a post related to boolean operation flaws: mesh flaw occurs when using vtkBooleanOperation - #5 by hyunseoki
You can try vtkbool which is more robust than vtk’s own boolean difference.

@Charles_Gueunet

@Arnav-2004 You may also want to have a look at the vespa library. Leveragin CGAL, it is a VTK module (and a ParaView plugin), described with more details into this blog.

It contains a vtkCGALBooleanOperation class, you can find an example usage in its corresponding test.