# Segfault while finding intersection of vtkPolyData with python

**URL:** https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317
**Category:** Support
**Created:** [February 25, 2019, 4:23am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317 "2019-02-25T04:23:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dimkal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dimkal/32/133_2.png) [@dimkal](https://discourse.vtk.org/u/dimkal)
#### Post date: [February 25, 2019, 4:23am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317/1 "2019-02-25T04:23:12Z")

</div>

Visualizing two polyData objects, I see that they overlap, however, I cannot calculate their intersection with VTK in python. What am I doing wrong??? I get the following error:

> $ python test.py  
> VOLUME of probe1: 8.360612727741916 # of Polys: 344  
> VOLUME of probe2: 8.440116557580266 # of Polys: 480  
> Warning: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Filters/General/vtkIntersectionPolyDataFilter.cxx, line 1675  
> vtkIntersectionPolyDataFilter (0x7fe2c5463bb0): No cell with correct orientation found
> 
> ERROR: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Common/ExecutionModel/vtkExecutive.cxx, line 784  
> vtkCompositeDataPipeline (0x7fe2c54649e0): Algorithm vtkIntersectionPolyDataFilter(0x7fe2c5463bb0) returned failure for request: vtkInformation (0x7fe2c54654b0)  
> Debug: Off  
> Modified Time: 1314  
> Reference Count: 1  
> Registered Events: (none)  
> Request: REQUEST\_DATA  
> FORWARD\_DIRECTION: 0  
> ALGORITHM\_AFTER\_FORWARD: 1  
> FROM\_OUTPUT\_PORT: 0
> 
> ERROR: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Common/ExecutionModel/vtkExecutive.cxx, line 784  
> vtkCompositeDataPipeline (0x7fe2c545eee0): Algorithm vtkBooleanOperationPolyDataFilter(0x7fe2c545f530) returned failure for request: vtkInformation (0x7fe2c545fb50)  
> Debug: Off  
> Modified Time: 1069  
> Reference Count: 1  
> Registered Events: (none)  
> Request: REQUEST\_DATA  
> FORWARD\_DIRECTION: 0  
> ALGORITHM\_AFTER\_FORWARD: 1  
> FROM\_OUTPUT\_PORT: 0
> 
> 0

My code is below, inputs are [probe0.vtk](https://pastebin.com/h3HYKp9H) and [probe1.vtk](https://pastebin.com/k4Ji3Txg)  
import vtk

```
def get_volume(vtk_poly):
    if vtk_poly.GetNumberOfPolys() == 0:
        return 0
    Mass = vtk.vtkMassProperties()
    Mass.SetInputData(vtk_poly)
    Mass.Update()
    return Mass.GetVolume()

def read_polydata(vtkfilename):
    reader_poly = vtk.vtkPolyDataReader()
    reader_poly.SetFileName(vtkfilename)
    reader_poly.Update()
    return reader_poly.GetOutput()

def get_mesh_intersection(mesh1, mesh2):
    '''
    Find the intersection volume between mesh vtk objects
    and return the volume of intersection
    '''
    booleanOperationFilter = vtk.vtkBooleanOperationPolyDataFilter()
    #booleanOperationFilter.GlobalWarningDisplayOff()
    booleanOperationFilter.SetInputData(0, mesh1)
    booleanOperationFilter.SetInputData(1, mesh2)
    booleanOperationFilter.SetOperationToIntersection()
    booleanOperationFilter.Update()
    intersection = booleanOperationFilter.GetOutput()
    return get_volume(intersection)

mesh_i = read_polydata('probe0.vtk')
mesh_j = read_polydata('probe1.vtk')
print('VOLUME of probe1:\t', get_volume(mesh_i), '# of Polys:\t', mesh_i.GetNumberOfPolys())
print('VOLUME of probe2:\t', get_volume(mesh_j), '# of Polys:\t', mesh_j.GetNumberOfPolys())

print(get_mesh_intersection(mesh_i, mesh_j))

```

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [February 27, 2019, 8:47am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317/2 "2019-02-27T08:47:46Z")

</div>

This is very similar to [How to extract surface from polydata](https://discourse.vtk.org/t/how-to-extract-surface-from-polydata/321/4)

The `vtk.vtkBooleanOperationPolyDataFilter` is simply not producing a result which is causing the segfault down your pipeline. @cory.quammen would likely have more details on how to get the `vtk.vtkBooleanOperationPolyDataFilter` working

Also, note a lot of this code can be simplified with [`vtki`](http://docs.vtki.org/en/latest/). Perhaps if we figure out how to implement this, it could become a common routine in `vtki`…

```python
import vtki
import vtk

def get_mesh_intersection(mesh1, mesh2):
    '''
    Find the intersection volume between mesh vtk objects
    and return the volume of intersection
    '''
    alg = vtk.vtkBooleanOperationPolyDataFilter()
    alg.SetInputData(0, mesh2)
    alg.SetInputData(1, mesh1)
    alg.SetOperationToIntersection()
    alg.Update()
    intersection = vtki.wrap(alg.GetOutput())
    return intersection

# Load meshes
mesh_i = vtki.read('probe0.vtk')
mesh_j = vtki.read('probe1.vtk')

print('VOLUME of probe1:\t', mesh_i.volume, '# of Polys:\t', mesh_i.n_faces)
print('VOLUME of probe2:\t', mesh_j.volume, '# of Polys:\t', mesh_j.n_faces)
intersection = get_mesh_intersection(mesh_i, mesh_j)
print('INTERSECTION VOLUME:\t', intersection.volume)

```

with output:

```nohighlight
VOLUME of probe1: 8.360612727741916 # of Polys: 344
VOLUME of probe2: 8.440116557580266 # of Polys: 480
INTERSECTION VOLUME: 0.0

```

Also, plotting is easy:

```python
vtki.MultiBlock([mesh_i, mesh_j]).plot(show_edges=1, multi_colors=1)

```

 ![download](https://discourse.vtk.org/uploads/default/original/1X/9667bc12223f57023f460617d1b6286d51e81af0.png)

or

```python
p = vtki.Plotter()
p.add_mesh(mesh_i, color='g', show_edges=1)
p.add_mesh(mesh_j, color='b', show_edges=1)
p.show()

```

---

<div class="post-metadata">

### Author: ![dimkal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dimkal/32/133_2.png) [@dimkal](https://discourse.vtk.org/u/dimkal)
#### Post date: [March 1, 2019, 2:57am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317/3 "2019-03-01T02:57:56Z")

</div>

thank you so much! this is extremely helpful, and vtki makes working with vtk so much more pythonic. Thank you again!

I’ve used your exact example and I’m getting similar results but I also get errors. See below.

Could this be an issue with my installation? I’ve used `pip install` on a MacOS 10.14.2. to install both vtk and vtki

> VOLUME of probe1: 8.360612727741916 # of Polys: 344  
> VOLUME of probe2: 8.440116557580266 # of Polys: 480  
> Warning: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Filters/General/vtkIntersectionPolyDataFilter.cxx, line 1675  
> vtkIntersectionPolyDataFilter (0x7ff71e259ff0): No cell with correct orientation found
> 
> ERROR: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Common/ExecutionModel/vtkExecutive.cxx, line 784  
> vtkCompositeDataPipeline (0x7ff71e25b4c0): Algorithm vtkIntersectionPolyDataFilter(0x7ff71e259ff0) returned failure for request: vtkInformation (0x7ff71e2caa90)  
> Debug: Off  
> Modified Time: 1787  
> Reference Count: 1  
> Registered Events: (none)  
> Request: REQUEST\_DATA  
> FORWARD\_DIRECTION: 0  
> ALGORITHM\_AFTER\_FORWARD: 1  
> FROM\_OUTPUT\_PORT: 0
> 
> ERROR: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Common/ExecutionModel/vtkExecutive.cxx, line 784  
> vtkCompositeDataPipeline (0x7ff71e2dfb00): Algorithm vtkBooleanOperationPolyDataFilter(0x7ff71e249650) returned failure for request: vtkInformation (0x7ff71bedff80)  
> Debug: Off  
> Modified Time: 1542  
> Reference Count: 1  
> Registered Events: (none)  
> Request: REQUEST\_DATA  
> FORWARD\_DIRECTION: 0  
> ALGORITHM\_AFTER\_FORWARD: 1  
> FROM\_OUTPUT\_PORT: 0
> 
> ERROR: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Filters/Core/vtkMassProperties.cxx, line 95  
> vtkMassProperties (0x7ff71e259210): No data to measure…!
> 
> INTERSECTION VOLUME: 0.0

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [March 1, 2019, 4:11am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317/4 "2019-03-01T04:11:29Z")

</div>

That’s not an issue with the installation or anything but is purely the `vtk.vtkBooleanOperationPolyDataFilter` (I am also getting that error). The more I read up on the `vtk.vtkBooleanOperationPolyDataFilter`, the less I understand how it works… but I think the cells in the two input meshes have to have specific orientations.

FYI: [http://vtk.1045678.n5.nabble.com/How-to-use-vtkBooleanOperationPolyDataFilter-correctly-td5728839.html](http://vtk.1045678.n5.nabble.com/How-to-use-vtkBooleanOperationPolyDataFilter-correctly-td5728839.html)

---

<div class="post-metadata">

### Author: ![Kaushik\_Tiwari](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kaushik_tiwari/32/3318_2.png) [@Kaushik\_Tiwari](https://discourse.vtk.org/u/Kaushik_Tiwari)
#### Post date: [April 29, 2021, 10:04am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317/5 "2021-04-29T10:04:44Z")

</div>

why intersection.volume is 0.0 even they are joining?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [May 4, 2021, 1:54am UTC](https://discourse.vtk.org/t/segfault-while-finding-intersection-of-vtkpolydata-with-python/317/6 "2021-05-04T01:54:33Z")

</div>

Unfortunately, vtkBooleanOperationPolyDataFilter is very unreliable. It can give incorrect output for simple, completely valid input or can randomly crash. Classes in [vtkbool](https://github.com/zippy84/vtkbool/) work much better and its maintainer is usually willing to look into problems and fix them.
