# mesh flaw occurs when using vtkBooleanOperation

**URL:** https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502
**Category:** Support
**Created:** [August 7, 2019, 5:13am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502 "2019-08-07T05:13:26Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![hyunseoki](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hyunseoki/32/737_2.png) [@hyunseoki](https://discourse.vtk.org/u/hyunseoki)
#### Post date: [August 7, 2019, 5:13am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/1 "2019-08-07T05:13:26Z")

</div>

Hi, I am using vtk to reconstruct blood vessel surface from vessel skeleton (center line along the vessel)

I constructed two tube type of vessel and going to glue them using vtkBooleanOperation.  
The vessel is made by vtkTubeFilter

However, there occurs some mesh flaw in the joint area

I cannot understand whole algorithm of vtkBooleanOperation, and I need some help  
I cannot find what is wrong

What I want to construct is like following

 ![image](https://discourse.vtk.org/uploads/default/original/1X/cae4241d4e933438efeb49d63556071e72eb7045.jpeg)

However, what I got is (when using SetOperationToUnion)

 ![image](https://discourse.vtk.org/uploads/default/original/1X/4226c565f7d1be7a4a6e8a36bc3af3b77022a905.jpeg)

Refer, when using SetOperationToIntersection

 ![image](https://discourse.vtk.org/uploads/default/original/1X/f1f7e15f7f64b85f73334b496e1e2a84a1cbab2f.png)

main code :

```
merger = ObjectManager.Merger()
merger.SetInput(tubeFilter1, tubeFilter2)
unionActor = merger.Merge()
unionActor.GetProperty().SetOpacity(0.9)

```

Merger class code :

```
def __init__ (self):
    self.m_filter = vtk.vtkBooleanOperationPolyDataFilter()
    self.m_normal = vtk.vtkPolyDataNormals()
    self.m_mapper = vtk.vtkPolyDataMapper()
    self.m_triFilter1 = vtk.vtkTriangleFilter()
    self.m_triFilter2 = vtk.vtkTriangleFilter()
    self.m_actor = vtk.vtkActor()

def SetInput(self, object1, object2):
    self.m_triFilter1.SetInputConnection(object1.GetOutputPort())
    self.m_triFilter2.SetInputConnection(object2.GetOutputPort())
    self.m_filter.SetInputConnection(0, self.m_triFilter1.GetOutputPort())
    self.m_filter.SetInputConnection(1, self.m_triFilter2.GetOutputPort())

def Merge(self):
    self.m_filter.SetOperationToUnion()
    self.m_filter.Update()

    self.m_normal.SetInputData(self.m_filter.GetOutput())
    self.m_normal.ComputePointNormalsOn()
    self.m_normal.Update()

    self.m_mapper.SetInputConnection(self.m_normal.GetOutputPort())
    self.m_actor.SetMapper(self.m_mapper)
    self.m_actor.GetProperty().SetRepresentationToWireframe()

    return self.m_actor
```

---

<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: [August 7, 2019, 5:40am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/2 "2019-08-07T05:40:12Z")

</div>

Boolean mesh implementations are really hard to implement correctly and they often fail (even in very expensive commercial software). Unfortunately, VTK’s implementation is quite fragile and it often generates incorrect meshes for complex inputs (and sometimes even for quite simple inputs, too). You may try to slightly move around the meshes until there are no errors in the output mesh.

If you need a definitive solution that always produces correct results then one option is to convert the mesh to binary labelmap, perform Boolean operations on the labelmap, then convert the labelmap back to surface mesh. Disadvantage of this approach that original model points are not preserved and you may need a lot of memory if you need preserve mesh geometry very accurately. If you want to give it a try without implementing anything, then you can perform these steps in 3D Slicer’s Segment Editor module (load models, import them to segmentation, use Logical operators effect, export resulting segment to model).

If you use Python, then another option is to use Boolean mesh operations of Blender, which is reported to be quite robust and very fast. You can import Blender Python package and process VTK meshes as described [here](https://projectweek.na-mic.org/PW32_2019_London_Canada/Projects/BronchoscopeLocalizationFromDepthMaps/#installing-and-importing-blender-within-slicer).

---

<div class="post-metadata">

### Author: ![hyunseoki](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hyunseoki/32/737_2.png) [@hyunseoki](https://discourse.vtk.org/u/hyunseoki)
#### Post date: [August 7, 2019, 5:58am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/4 "2019-08-07T05:58:29Z")

</div>

Thanks for replying

My program needs computation time within 1sec (without visual rendering, only needs surface model like stl format)  
Now, I consider blender or using construct mesh manually…  
anyway, thanks again

---

<div class="post-metadata">

### Author: ![hyunseoki](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hyunseoki/32/737_2.png) [@hyunseoki](https://discourse.vtk.org/u/hyunseoki)
#### Post date: [August 7, 2019, 6:56am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/5 "2019-08-07T06:56:10Z")

</div>

blender works properly  
I used boolean operation in blender and I got following

thanks again @lassoan

 ![image](https://discourse.vtk.org/uploads/default/original/1X/3be8829f0ce955932b72323ee61970f62e180088.jpeg)

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [August 7, 2019, 1:31pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/6 "2019-08-07T13:31:07Z")

</div>

There is an [alternative boolean operations implementation](https://github.com/zippy84/vtkbool) (Apache License, Version 2.0) by @Ron84 that may be more robust. It could be a good project for someone to compare the performance and accuracy of both, with the aim of potentially integrating this implementation into VTK proper.

---

<div class="post-metadata">

### Author: ![at123](https://discourse.vtk.org/user_avatar/discourse.vtk.org/at123/32/748_2.png) [@at123](https://discourse.vtk.org/u/at123)
#### Post date: [August 7, 2019, 2:35pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/7 "2019-08-07T14:35:11Z")

</div>

Do you know how to remove both the inputs of port 0 and 1 in a vtkBooleanOperationPolyDataFIlter. I tried using RemoveAllInputs() but it only removed the data in port 0.

---

<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: [August 7, 2019, 6:14pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/8 "2019-08-07T18:14:12Z")

</div>

> [@cory.quammen](#):
>
> There is an [alternative boolean operations implementation](https://github.com/zippy84/vtkbool)

Looks promising, thanks for sharing. Next time we need Boolean mesh operations, we’ll give it a try.

---

<div class="post-metadata">

### Author: ![hyunseoki](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hyunseoki/32/737_2.png) [@hyunseoki](https://discourse.vtk.org/u/hyunseoki)
#### Post date: [August 8, 2019, 4:37am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/9 "2019-08-08T04:37:09Z")

</div>

[alternative boolean operations implementation](https://github.com/zippy84/vtkbool) works well too

But, I cannot compare the performance at now, it needs more consideration  
Anyway, thanks too

 ![image](https://discourse.vtk.org/uploads/default/original/1X/0786a6fd1ffc7782e1200c9a2c861a170a9b6ffd.jpeg)

---

<div class="post-metadata">

### Author: ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)
#### Post date: [August 9, 2019, 11:07am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/10 "2019-08-09T11:07:52Z")

</div>

> [@hyunseoki](#):
>
> [alternative boolean operations implementation](https://github.com/zippy84/vtkbool) works well too

😀

---

<div class="post-metadata">

### Author: ![hyunseoki](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hyunseoki/32/737_2.png) [@hyunseoki](https://discourse.vtk.org/u/hyunseoki)
#### Post date: [August 9, 2019, 12:21pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/11 "2019-08-09T12:21:58Z")

</div>

Hello, Ron,  
Nice to meet you

I followed your instruction which is described in github and installed vtk 8.2 with vtkbool.  
I found vtkbool.dll and lib, but there is no python package for vtkbool  
I checked again that my vtk is installed with python wrapped

I completed the work with C++ but I wonder why vtkbool python package is not installed.  
Can you give me some help?

Following is CMake configuration what I have done

 ![image](https://discourse.vtk.org/uploads/default/original/1X/f451bad98d93a4d825f125987b519e50965eeb08.png)  
 ![image](https://discourse.vtk.org/uploads/default/original/1X/e1e1279e2eea2ff064ade08d8dd14fe4e1cc1c7e.png)

---

<div class="post-metadata">

### Author: ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)
#### Post date: [August 9, 2019, 2:01pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/12 "2019-08-09T14:01:20Z")

</div>

Hello @hyunseoki .

The wrapper currently does not work with VTK8 ☹.

But next time I will fix it.

---

<div class="post-metadata">

### Author: ![hyunseoki](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hyunseoki/32/737_2.png) [@hyunseoki](https://discourse.vtk.org/u/hyunseoki)
#### Post date: [August 9, 2019, 2:08pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/13 "2019-08-09T14:08:02Z")

</div>

Thanks replying @Ron84

Oh… I hope it will be soon.

Then, is it compatible with vtk 7.x ?

---

<div class="post-metadata">

### Author: ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)
#### Post date: [August 9, 2019, 2:12pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/14 "2019-08-09T14:12:38Z")

</div>

Yes it works fine with VTK6 and VTK7.

---

<div class="post-metadata">

### Author: ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)
#### Post date: [August 16, 2019, 5:03pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/15 "2019-08-16T17:03:29Z")

</div>

@hyunseoki vtkbool now works with VTK8. Try it out! If CMake cannot detect VTK’s Python version, set py\_ver manually. E.g. with `set(py_ver "3.7")`.

Best regards  
Ronald

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [August 16, 2019, 9:03pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/16 "2019-08-16T21:03:14Z")

</div>

@Ron84 I could take a shot at making this a Remote Module. That way you could still use your version with VTK. Ii it’s OK with you I could start soon. I will fork your repo and add what I need to make it a remote module. Eventually, you could take my changes and offer this option.

Bill

---

<div class="post-metadata">

### Author: ![Ron84](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ron84/32/630_2.png) [@Ron84](https://discourse.vtk.org/u/Ron84)
#### Post date: [August 16, 2019, 9:20pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/17 "2019-08-16T21:20:09Z")

</div>

Oh yeah, sounds nice. Make the changes and then create a pull request. 😀

---

<div class="post-metadata">

### Author: ![Suhaim01](https://discourse.vtk.org/user_avatar/discourse.vtk.org/suhaim01/32/9655_2.png) [@Suhaim01](https://discourse.vtk.org/u/Suhaim01)
#### Post date: [December 26, 2024, 9:36am UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/18 "2024-12-26T09:36:06Z")

</div>

@hyunseoki, what run time were you getting using Blender?  
For me the boolean operation was taking upto 8s for my specific meshes.  
I also have a time critical task at hand and need a low run time.

```auto
    objMapper = vtkmodules.vtkRenderingCore.vtkPolyDataMapper()
    objMapper.SetInputData(mesh1)
    objMapper.Update()
    converter = PolyDataMapperToBlender(objMapper)
    firstBpy = converter.convert_data()

    objMapper = vtkmodules.vtkRenderingCore.vtkPolyDataMapper()
    objMapper.SetInputData(mesh2)
    objMapper.Update()
    converter = PolyDataMapperToBlender(objMapper)
    secondBpy = converter.convert_data()
    
    me1 = bpy.data.meshes.new("test1")
    me1 = firstBpy.copy()
    ob1 = bpy.data.objects.new("object1", me1)
    bpy.context.collection.objects.link(ob1)
    
    me2 = bpy.data.meshes.new("test2")
    me2 = secondBpy.copy()
    ob2 = bpy.data.objects.new("object2", me2)
    bpy.context.collection.objects.link(ob2)
    
    bool_one = ob2.modifiers.new(type="BOOLEAN", name="bool1")
    bool_one.object = ob1
    bool_one.operation = 'INTERSECT'
    bpy.context.view_layer.objects.active = ob2
    bpy.ops.object.modifier_apply(modifier='bool1')

    intersection = ob2.data
    convertedBack = pv.wrap(BlenderToPolyData.convert(intersection))

```

Here I am using 2 pyVista meshes - mesh1 and mesh2(Same as a vtk PolyData mesh), importing them into Blender for the boolean operation. Once it’s done I convert it back to pyVista for further use.  
I use the Blender python package bpy though.  
[bpy - Blender python API](https://docs.blender.org/api/current/index.html)

In between I use PolyDataMapperToBlender() which is part of an external package, that helps in converting VTK meshes into Blender meshes. The package increase the number of cells in the mesh by 2.5 times always(not sure why).  
[VTKtoBlender](https://github.com/cwant/VTKBlender/tree/master)

---

<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: [December 26, 2024, 2:32pm UTC](https://discourse.vtk.org/t/mesh-flaw-occurs-when-using-vtkbooleanoperation/1502/19 "2024-12-26T14:32:39Z")

</div>

In addition to vtkbool and bpy, your can check out these other high-quality free libraries (with permissive license) for Boolean operations:

- [manifold](https://github.com/elalish/manifold)
- [CinoLib + RobustMesh](https://github.com/gcherchi/InteractiveAndRobustMeshBooleans)
- [geogram](https://anaconda.org/saedrna/geogram)

Ithink they all have Python wrapping, so it should not be to hard to try them. Let us know how well they work for you.
