# Got Exception when using BooleanOperation.

**URL:** <https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331>\
**Category:** Development\
**Created:** [July 31, 2024, 6:40am UTC](https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331 "2024-07-31T06:40:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LItterBoy-GB](https://discourse.vtk.org/user_avatar/discourse.vtk.org/litterboy-gb/32/9093_2.png) [@LItterBoy-GB](https://discourse.vtk.org/u/LItterBoy-GB)\
**Post date:** [July 31, 2024, 6:40am UTC](https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331/1 "2024-07-31T06:40:34Z")

</div>

I got the exception when run `booleanFilter->GetOutput();`.  
[polydata2.stl](https://discourse.vtk.org/uploads/short-url/7CaYIwCH5aSUGpHFuTOvoUZzK6z.stl) (934 Bytes)  
[polydata1.stl](https://discourse.vtk.org/uploads/short-url/vk6ZIG5ywLjC4IzSYIv9bjueYnn.stl) (1.3 MB)

```plaintext
Exception caused at 0x000007FFEE806D84E (vtkFilters General 9.3. dll) (located in VtkExample. exe): 0xC000000 5:Access conflict occurred while reading location 0xFFFFFFFFFFFF.

```

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [August 1, 2024, 6:07am UTC](https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331/2 "2024-08-01T06:07:07Z")

</div>

> [@LItterBoy-GB](#):
>
> `testBool`

does not exist. Did you mean `testBoxBool`?

---

<div class="post-metadata">

**Author:** ![LItterBoy-GB](https://discourse.vtk.org/user_avatar/discourse.vtk.org/litterboy-gb/32/9093_2.png) [@LItterBoy-GB](https://discourse.vtk.org/u/LItterBoy-GB)\
**Post date:** [August 2, 2024, 1:07am UTC](https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331/3 "2024-08-02T01:07:34Z")

</div>

> [@amaclean](#):
>
> does not exist. Did you mean `testBoxBool`?

Yes. Thank you.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)\
**Post date:** [August 6, 2024, 6:52am UTC](https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331/4 "2024-08-06T06:52:02Z")

</div>

Your problem lies with `polydata2.stl` the orientation of the cells is wrong.

I replaced `polydata1`with a sphere you’ll see that the orientation of the cells in `polydata2` is wrong:

 ![polydata2_sphere](https://discourse.vtk.org/uploads/default/original/2X/a/a56e9cf26278791d8d320a5d65c68b79bfbc51b5.png)

If we intersect `polydata1` with the sphere (this replaces `polydata2`) everything works Ok, the red area is the intersection:

 ![polydata1_sphere](https://discourse.vtk.org/uploads/default/original/2X/8/8701e3231936cea370e9a88498b9e71d2dff7483.png)

---

<div class="post-metadata">

**Author:** ![LItterBoy-GB](https://discourse.vtk.org/user_avatar/discourse.vtk.org/litterboy-gb/32/9093_2.png) [@LItterBoy-GB](https://discourse.vtk.org/u/LItterBoy-GB)\
**Post date:** [August 6, 2024, 9:49am UTC](https://discourse.vtk.org/t/got-exception-when-using-booleanoperation/14331/5 "2024-08-06T09:49:09Z")

</div>

I encountered a new problem when using two simple square boxes for cropping.  
[polyData1.stl](https://discourse.vtk.org/uploads/short-url/g4rWBT7S5SyATcou6lA2a9kXHSe.stl) (684 Bytes)  
[polyData2.stl](https://discourse.vtk.org/uploads/short-url/4eEVW8KSYRFJRicWl71oL5ciKN2.stl) (684 Bytes)

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