# Question about the boolean operation in vtk

**URL:** https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375
**Category:** Support
**Created:** [May 4, 2023, 1:58pm UTC](https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375 "2023-05-04T13:58:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ljx006barbatos](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ljx006barbatos/32/7015_2.png) [@ljx006barbatos](https://discourse.vtk.org/u/ljx006barbatos)
#### Post date: [May 4, 2023, 1:58pm UTC](https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375/1 "2023-05-04T13:58:22Z")

</div>

Context: I want to have the union of two walls, the walls may have some offset in both the x and y directions. But the result of my testing is terrible.

The result of testing:

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

Codes:  
int main(int, char\* []) {

```
float thickness = 0.5;

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(0, 0, 0);
points->InsertNextPoint(2, 0, 0);
points->InsertNextPoint(2, 2, 0);
points->InsertNextPoint(0, 2, 0);
points->InsertNextPoint(0 + 1, 1, thickness);
points->InsertNextPoint(2 + 1, 1, thickness);
points->InsertNextPoint(2 +1, 2 +1, thickness);
points->InsertNextPoint(0 +1, 2 +1, thickness);

vtkSmartPointer<vtkCellArray> cellArray1 = vtkSmartPointer<vtkCellArray>::New();
cellArray1->InsertNextCell(4);
cellArray1->InsertCellPoint(0);
cellArray1->InsertCellPoint(1);
cellArray1->InsertCellPoint(2);
cellArray1->InsertCellPoint(3);

vtkSmartPointer<vtkCellArray> cellArray2 = vtkSmartPointer<vtkCellArray>::New();
cellArray2->InsertNextCell(4);
cellArray2->InsertCellPoint(4);
cellArray2->InsertCellPoint(5);
cellArray2->InsertCellPoint(6);
cellArray2->InsertCellPoint(7);

vtkSmartPointer<vtkPolyData> polyData1 = vtkSmartPointer<vtkPolyData>::New();
polyData1->SetPoints(points);
polyData1->SetPolys(cellArray1);

vtkSmartPointer<vtkPolyData> polyData2 = vtkSmartPointer<vtkPolyData>::New();
polyData2->SetPoints(points);
polyData2->SetPolys(cellArray2);

vtkSmartPointer<vtkLinearExtrusionFilter> extrudeA = vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extrudeA->SetInputData(polyData1);
extrudeA->SetExtrusionTypeToNormalExtrusion();
extrudeA->SetVector(0, 0, 1);
extrudeA->SetScaleFactor(thickness);
extrudeA->Update();

vtkSmartPointer<vtkLinearExtrusionFilter> extrudeB = vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extrudeB->SetInputData(polyData2);
extrudeB->SetExtrusionTypeToNormalExtrusion();
extrudeB->SetVector(0, 0, -1);
extrudeB->SetScaleFactor(thickness);
extrudeB->Update();

vtkNew<vtkTriangleFilter> filter1;
filter1->SetInputData(extrudeA->GetOutput());
filter1->Update();
vtkNew<vtkTriangleFilter> filter2;
filter2->SetInputData(extrudeB->GetOutput());
filter2->Update();
vtkNew<vtkBooleanOperationPolyDataFilter> filter_merge;
filter_merge->SetInputData(0, filter1->GetOutput());
filter_merge->SetInputData(1, filter2->GetOutput());
//filter_merge->SetOperationToDifference();//显示差集
filter_merge->SetOperationToUnion();//显示并集
//filter_merge->SetOperationToIntersection();//显示交集
filter_merge->Update();

vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(filter_merge->GetOutputPort());

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);

vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
interactor->Initialize();
interactor->Start();

return EXIT_SUCCESS;

```

}

---

<div class="post-metadata">

### Author: ![Tiffany\_Chhim](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tiffany_chhim/32/6419_2.png) [@Tiffany\_Chhim](https://discourse.vtk.org/u/Tiffany_Chhim)
#### Post date: [May 5, 2023, 7:34am UTC](https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375/2 "2023-05-05T07:34:25Z")

</div>

Hello @ljx006barbatos,

Unfortunately, the filter `vtkBooleanOperationPolyDataFilter` is known to be unstable and sometimes produces wrong outputs like you what you got.

I suggest you try the `vtkCGALBooleanOperation` filter that is available in the [VESPA library here](https://gitlab.kitware.com/vtk-cgal/vespa) and is much more reliable. You can use it as as part of the VTK module `vtkCGALPMP`.

For more details, you may have a look at this blog post we published a little while ago: [VESPA: Advanced Mesh Processing Based on CGAL for VTK and ParaView](https://www.kitware.com/vespa-advanced-mesh-processing-based-on-cgal-for-vtk-and-paraview/).

---

<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: [May 5, 2023, 9:57am UTC](https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375/3 "2023-05-05T09:57:17Z")

</div>

Or you can use the filter mentioned here: [vtkbool now available at conda-forge](https://discourse.vtk.org/t/vtkbool-now-available-at-conda-forge/11345) 🙂

---

<div class="post-metadata">

### Author: ![ljx006barbatos](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ljx006barbatos/32/7015_2.png) [@ljx006barbatos](https://discourse.vtk.org/u/ljx006barbatos)
#### Post date: [May 9, 2023, 10:35am UTC](https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375/4 "2023-05-09T10:35:39Z")

</div>

Thank you very much for your suggestion. I want to know if this means I need to reinstall the VTK library to select the VTK module vtkCGALPMP?

---

<div class="post-metadata">

### Author: ![Tiffany\_Chhim](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tiffany_chhim/32/6419_2.png) [@Tiffany\_Chhim](https://discourse.vtk.org/u/Tiffany_Chhim)
#### Post date: [May 9, 2023, 11:26am UTC](https://discourse.vtk.org/t/question-about-the-boolean-operation-in-vtk/11375/5 "2023-05-09T11:26:44Z")

</div>

For instructions on how to install the VESPA library, you can follow [the guidelines of the README](https://gitlab.kitware.com/vtk-cgal/vespa/-/blob/master/README.md).

You can skip the “ParaView” section if you are only using VTK.  
Make sure that your already installed version of VTK is **at least 9.0**.
