Question about the boolean operation in vtk

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:

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;

}

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 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.

Or you can use the filter mentioned here: vtkbool now available at conda-forge :slightly_smiling_face:

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?

For instructions on how to install the VESPA library, you can follow the guidelines of the README.

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.