How to test if two STL files intersect?

I want to get intersection between two stl files

so I use vtkBooleanOperationPolyDataFilter, but it didn’t work well.

usually cases work well, but some cases have error

this is my code

reader1 = vtk.vtkSTLReader()
reader2 = vtk.vtkSTLReader()

reader1.SetFileName("D:/backup/LG_Project/data/65_inch/stl/av1.stl")
reader2.SetFileName("D:/backup/LG_Project/data/65_inch/stl/test.stl")

mapper1 = vtk.vtkPolyDataMapper()
mapper2 = vtk.vtkPolyDataMapper()
if vtk.VTK_MAJOR_VERSION <= 5:
    mapper1.SetInput(reader1.GetOutput())
    mapper2.SetInput(reader2.GetOutput())
else:
    mapper1.SetInputConnection(reader1.GetOutputPort())
    mapper2.SetInputConnection(reader2.GetOutputPort())

actor1 = vtk.vtkActor()
actor2 = vtk.vtkActor()
actor1.SetMapper(mapper1)
actor2.SetMapper(mapper2)

vtk.vtkTriangl

booleanOperation = vtk.vtkBooleanOperationPolyDataFilter()
booleanOperation.SetOperationToIntersection()
booleanOperation.SetInputConnection(0, reader1.GetOutputPort())
booleanOperation.SetInputConnection(1, reader2.GetOutputPort())
booleanOperation.Update()
booleanOperationMapper = vtk.vtkPolyDataMapper()
booleanOperationMapper.SetInputConnection(booleanOperation.GetOutputPort())
booleanOperationMapper.ScalarVisibilityOff()
booleanOperationActor = vtk.vtkActor()
booleanOperationActor.SetMapper(booleanOperationMapper)

ren= vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
# Create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Assign actor to the renderer
ren.AddActor(booleanOperationActor)

# Enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()

and this is error massage
even if I run code under the same conditions, it have different error massage .
1.
Process finished with exit code -1073741819 (0xC0000005)

Warning: In C:\VPP\standalone-build\VTK-source\Filters\General\vtkIntersectionPolyDataFilter.cxx, line 1675
vtkIntersectionPolyDataFilter (0000018963EB6490): No cell with correct orientation found

ERROR: In C:\VPP\standalone-build\VTK-source\Common\ExecutionModel\vtkExecutive.cxx, line 784
vtkCompositeDataPipeline (000001895E62D580): Algorithm vtkIntersectionPolyDataFilter(0000018963EB6490) returned failure for request: vtkInformation (0000018963E86E40)
Debug: Off
Modified Time: 5400
Reference Count: 1
Registered Events: (none)
Request: REQUEST_DATA
ALGORITHM_AFTER_FORWARD: 1
FORWARD_DIRECTION: 0
FROM_OUTPUT_PORT: 0

ERROR: In C:\VPP\standalone-build\VTK-source\Common\ExecutionModel\vtkExecutive.cxx, line 784
vtkCompositeDataPipeline (000001895E62C160): Algorithm vtkBooleanOperationPolyDataFilter(000001895E6DBA70) returned failure for request: vtkInformation (0000018963E880B0)
Debug: Off
Modified Time: 5155
Reference Count: 1
Registered Events: (none)
Request: REQUEST_DATA
ALGORITHM_AFTER_FORWARD: 1
FORWARD_DIRECTION: 0
FROM_OUTPUT_PORT: 0

These filters are known to have issues. They can randomly fail on valid inputs. Search on this forum for “Boolean” to learn more. I would suggest to try Roemer’s vtkbool package (it is reported to work better than the Boolean filters in VTK) or convert your polydata to images, perform Boolean operations on the images, and convert the results back to polydata.

Please send us your polydatas :grinning:

Thanks for your opinion.
I just found another way to find two stl files intersection.
That is vtkCollisionDetectionFilter.
and this filter is work very well. :slight_smile:

Yes, of course, if you just need to know if the two polydata intersects or not then you should not use Boolean operations filter (which perform very heavy computations to give you the intersection result).

When you ask a question next time, give a high-level summary of your overall goal. That way there is a higher chance that we solve your actual problem (and not what you think your problem is).