I got the exception when run booleanFilter->GetOutput();
.
polydata2.stl (934 Bytes)
polydata1.stl (1.3 MB)
Exception caused at 0x000007FFEE806D84E (vtkFilters General 9.3. dll) (located in VtkExample. exe): 0xC000000 5:Access conflict occurred while reading location 0xFFFFFFFFFFFF.
vtkSmartPointer<vtkPolyData> readStl(std::string file)
{
vtkNew<vtkSTLReader> reader;
reader->SetFileName(file.c_str());
reader->Update();
return reader->GetOutput();
}
vtkSmartPointer<vtkPolyData> triangleFilter(vtkSmartPointer<vtkPolyData> polyData) {
vtkNew<vtkTriangleFilter> triangleFilter;
triangleFilter->SetInputData(polyData);
triangleFilter->Update();
vtkNew<vtkCleanPolyData> clean;
clean->SetInputConnection(triangleFilter->GetOutputPort());
clean->Update();
return clean->GetOutput();
}
std::vector<vtkSmartPointer<vtkActor>> testBoxBool(std::string file1, std::string file2)
{
vtkSmartPointer<vtkPolyData> polyData1 = readStl(file1);
vtkSmartPointer<vtkPolyData> polyData2 = readStl(file2);
vtkNew<vtkBooleanOperationPolyDataFilter> booleanFilter;
booleanFilter->SetInputData(0, triangleFilter(polyData1));
booleanFilter->SetInputData(1, triangleFilter(polyData2));
booleanFilter->SetOperationToIntersection();
booleanFilter->Update();
vtkSmartPointer<vtkPolyData> appendData = booleanFilter->GetOutput();
std::vector<vtkSmartPointer<vtkPolyData>> polyDatas;
polyDatas.push_back(polyData1);
polyDatas.push_back(polyData2);
polyDatas.push_back(appendData);
std::vector<vtkSmartPointer<vtkActor>> res;
for (vtkSmartPointer<vtkPolyData> data : polyDatas) {
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(data);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(0, 0, 1);
res.push_back(actor);
}
vtkSmartPointer<vtkTransform> trans = vtkSmartPointer<vtkTransform>::New();
trans->PostMultiply();
trans->Translate(0,0,10);
res[0]->SetUserTransform(trans);
res[1]->SetUserTransform(trans);
res[2]->GetProperty()->SetColor(1, 0, 0);
return res;
}
int main(int, char* [])
{
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer.GetPointer());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.GetPointer());
std::vector<vtkSmartPointer<vtkActor>> actors = testBool("F:/desktop/polydata1.stl", "F:/desktop/polydata2.stl");
for (vtkSmartPointer<vtkActor> actor : actors) {
renderer->AddActor(actor);
}
renderer->SetBackground(1,1,1);
renWin->SetSize(640, 480);
renWin->SetWindowName("Bottle");
renWin->Render();
renderer->GetActiveCamera()->SetPosition(1, 0, 0);
renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
renderer->GetActiveCamera()->SetViewUp(0, 0, 1);
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
// render the image
//
iren->Start();
return EXIT_SUCCESS;
}