# Substraction between one volume respect other two volumes does not affect the intersected areas

**URL:** https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760
**Category:** Support
**Created:** [March 5, 2020, 10:16am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760 "2020-03-05T10:16:01Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 5, 2020, 10:16am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/1 "2020-03-05T10:16:01Z")

</div>

Hello,

I have a doubt I hope someone can give me an idea. I want to substract one volume (obtained from a polydata with vtkPolyDataToImageStencil) from several cubes which are polydatas too. I append the cubes with vtkAppendPolyData to make a single substraction and i also transform it into volume. And i make a boolean substraction . The substracion in the areas with cubes that are intersecting the substraction is not applied. I solved it by substracting each cube one by one to the volume but it becomes slower. I wonder how the filters treat the intersected areas. I don´t want to do this operation with polydatas because i have many problems clipping and closing surfaces because the data can come with many issues.

Thank you very much

![intersection](https://discourse.vtk.org/uploads/default/original/2X/f/f7add66f941c62a9e5a285cfe558bc06b14c14e7.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: [March 5, 2020, 11:45am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/2 "2020-03-05T11:45:01Z")

</div>

Do you have any code and sample files for us?

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 5, 2020, 12:06pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/3 "2020-03-05T12:06:30Z")

</div>

Sure, here it is:

```
vtkSmartPointer<vtkPolyDataCollection > allCylindersRed = widget_mgr_.lock()->getAllCylindersOfImplantActor(0); //0 => RED CYLINDER
vtkSmartPointer<vtkAppendPolyData> appendFilterRedCylinders = vtkSmartPointer<vtkAppendPolyData>::New();
//Create a polydata with all the implants for the boolean
for (allCylindersRed->InitTraversal();
	vtkPolyData* PolyData = allCylindersRed->GetNextItem();) {
	//Append the two meshes
	appendFilterRedCylinders->AddInputData(PolyData);
	appendFilterRedCylinders->Update();
}

  vtkSmartPointer<vtkImageData> RedCylinders_img = getFromPolydataToImage(appendFilterRedCylinders->GetOutput(), final_bounds);

  extrude_WithoutRedCylinders_img = booleanSubstractionTwoPolyDatas(extrude, RedCylinders_img );

vtkSmartPointer<vtkImageData> booleanSubstractionTwoPolyDatas(vtkSmartPointer<vtkImageData> extrude1, vtkSmartPointer<vtkImageData> extrude2) {

}

//Substract one image from the other
vtkSmartPointer<vtkImageMathematics> boolean = vtkSmartPointer<vtkImageMathematics>::New();
boolean->SetInput1Data(extrude1);
boolean->SetInput2Data(extrude2);
boolean->SetOperationToSubtract();
boolean->Update();

return boolean->GetOutput();
}
```

---

<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: [March 5, 2020, 12:52pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/4 "2020-03-05T12:52:18Z")

</div>

Using images for a boolean operation is often a very bad idea. What’s with vtkBooleanOperationPolyDataFilter or [https://github.com/zippy84/vtkbool](https://github.com/zippy84/vtkbool)?

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 5, 2020, 12:53pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/5 "2020-03-05T12:53:11Z")

</div>

Thanks for answering. I tried it but it was very slow

---

<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: [March 5, 2020, 12:57pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/6 "2020-03-05T12:57:53Z")

</div>

Could you please provide your polydatas!

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 17, 2020, 11:00am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/7 "2020-03-17T11:00:55Z")

</div>

Hi, here you have the stl and i tried to substract a cylinder and it takes a long time.

> **[case 1 gingiva.stl](https://wetransfer.com/downloads/2e37c557aee2d5dc4c48a9b3aeddfa4d20200317105945/102726c0e44329eda6fb76c7c288cc2c20200317105945/a16894)**
>
> 1 file sent via WeTransfer, the simplest way to send your files around the world

Thanks

---

<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: [March 17, 2020, 1:06pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/8 "2020-03-17T13:06:03Z")

</div>

@zandarina One stl is not enough. Where is the cylinder?

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 17, 2020, 1:38pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/9 "2020-03-17T13:38:04Z")

</div>

Hello, thanks for answering i use this piece of code for the cylinder and i move it with a bounding box and i initialize it with the position of the mouse

```
cylinder->SetHeight(height_red_cyl);
const double red_radius = implant->GetHoleDiameter() / 2;
cylinder->SetRadius(red_radius);
cylinder->SetResolution(100);
//cylinder->Update();
//cylinder->SetAxis(0, 1, 0);

//Cylinder red
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(cylinder->GetOutputPort());
mapper->Update();
vtkNew<vtkActor> actor_cylinder;
actor_cylinder->SetMapper(mapper);
actor_cylinder->GetProperty()->SetColor(1, 0, 0);
actor_cylinder->GetProperty()->SetOpacity(0.5);

```

thanks

---

<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: [March 17, 2020, 1:46pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/10 "2020-03-17T13:46:13Z")

</div>

I need two stl’s or vtk’s! I don’t know your parameters. Try to export one of you cylinders.

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 17, 2020, 3:16pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/11 "2020-03-17T15:16:09Z")

</div>

sure, i have added the polydatas to make the substraction

> **[data.rar](https://wetransfer.com/downloads/d0356823d5f089f6d8e1d9f0c9f0f04020200317151451/6aa9c5)**
>
> 1 file sent via WeTransfer, the simplest way to send your files around the world

Thanks

---

<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: [March 17, 2020, 7:00pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/12 "2020-03-17T19:00:01Z")

</div>

There is no contact:

 ![Bildschirmfoto_2020-03-17_19-59-07](https://discourse.vtk.org/uploads/default/original/2X/0/09559e1de7d43045e0f77f370a01bdff5e0b01a5.png)

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 18, 2020, 9:23am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/13 "2020-03-18T09:23:00Z")

</div>

Hi sorry i don´t understand why it did not apply the transformation. I checked the vtk files and both polydatas intersect. I hope it is ok this time.

> **[scene.rar](https://wetransfer.com/downloads/5d02728d81245b25dfa566fad513985920200318091918/64628a)**
>
> 1 file sent via WeTransfer, the simplest way to send your files around the world

Thanks

---

<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: [March 18, 2020, 5:24pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/14 "2020-03-18T17:24:14Z")

</div>

With my filter you will get this result:

 ![Bildschirmfoto_2020-03-18_18-22-41](https://discourse.vtk.org/uploads/default/original/2X/1/1aad0821cd11da9a04025b70c954bbb7982b66e2.png)

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [March 18, 2020, 5:38pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/15 "2020-03-18T17:38:21Z")

</div>

Awesome results. How long did it take? Is the result a closed surface?.

Thanks for trying and congrats for your library

---

<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: [March 18, 2020, 6:22pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/16 "2020-03-18T18:22:33Z")

</div>

Only a few milliseconds…

Just apply CleanPolyData and you will have one surface.

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [April 15, 2020, 12:35pm UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/17 "2020-04-15T12:35:30Z")

</div>

Hi, I am trying to use your library as you suggested but when i try to susbtract two polydatas to empty the extruded stl, it goes very slow. How can i speed up the susbstraction?. Thank you very much

```
vtkNew<vtkSTLReader> reader;
reader->SetFileName("filename.stl");
reader->Update();

///// 1
/////// extrude towards height to make the hole
vtkSmartPointer<vtkLinearExtrusionFilter> extrude =
	vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extrude->SetInputData(reader->GetOutput());
extrude->SetExtrusionTypeToVectorExtrusion();
extrude->SetVector(0, 0, 1);
extrude->SetScaleFactor(5.2);
extrude->Update();

///// 2

// Make the basis
vtkSmartPointer<vtkPolyDataNormals> normals2 =
	vtkSmartPointer<vtkPolyDataNormals>::New();
normals2->SetInputData(reader->GetOutput());
normals2->ConsistencyOn();
normals2->SetComputeCellNormals(1);
normals2->SplittingOff();
normals2->Update();

/////// original cut /////
vtkSmartPointer<vtkLinearExtrusionFilter> extrude2 =
	vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extrude2->SetInputData(normals2->GetOutput());
extrude2->SetExtrusionTypeToNormalExtrusion();
//extrude->SetVector(0, 0, -1);
extrude2->SetScaleFactor(3.0);
extrude2->Update();

vtkPolyDataBooleanFilter *bf2 =   
  vtkPolyDataBooleanFilter::New();
bf2->SetInputConnection(0, extrude->GetOutputPort());
bf2->SetInputConnection(1, extrude2->GetOutputPort());
bf2->SetOperModeToDifference();
bf2->Update();

vtkSmartPointer<vtkPolyDataMapper> Mapper4 =
	vtkSmartPointer<vtkPolyDataMapper>::New();
Mapper4->SetInputData(bf2->GetOutput());
Mapper4->ScalarVisibilityOff();

vtkSmartPointer<vtkActor> Actor4 =
	vtkSmartPointer<vtkActor>::New();
Actor4->SetMapper(Mapper4);
Actor4->GetProperty()->SetColor(0, 0, 1);

// Create renderers and add actors of plane and cube
vtkSmartPointer<vtkRenderer> renderer =
	vtkSmartPointer<vtkRenderer>::New();

renderer->AddActor(Actor4); 

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

vtkSmartPointer<vtkRenderWindowInteractor> interactor =
	vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
renderer->SetBackground(0, 0, 0);
renderWindow->Render();

interactor->Start();
```

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [June 26, 2020, 9:54am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/18 "2020-06-26T09:54:27Z")

</div>

Hi, one last question, I was thinking again about the booleans you suggested me by now I have it all in volume operations and I got a decent result. But I am thinking that combining both options it can be a good idea to obtain even a better polydata.

But I was wondering in the picture you show why the bottom of the cylinder is not removed from the output of the boolean operation substraction. The result should be a hole. I does not make sense to me. I also got the same issue with the substraction of the polydata with the cylinder.

Thanks

![Screenshot_1](https://discourse.vtk.org/uploads/default/original/2X/7/7fe64465e7aee26a89a6c54378a0cf08559f6a82.png)

---

<div class="post-metadata">

### Author: ![zandarina](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/z/4af34b/32.png) [@zandarina](https://discourse.vtk.org/u/zandarina)
#### Post date: [July 29, 2020, 7:37am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/19 "2020-07-29T07:37:28Z")

</div>

I found that this is caused in debug but not a release. Amd the vtkAppendPolyData if there is intersection does not work eaither. But I fixed it doing one by one. I will look into the vtkAppendPolyData to see what’s wrong with this filter in the intersections. If I find it out I will let you know because it happens in volume and polydata in boolean operations. Thanks

---

<div class="post-metadata">

### Author: ![Koly](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/a3d4f5/32.png) [@Koly](https://discourse.vtk.org/u/Koly)
#### Post date: [January 24, 2022, 2:51am UTC](https://discourse.vtk.org/t/substraction-between-one-volume-respect-other-two-volumes-does-not-affect-the-intersected-areas/2760/20 "2022-01-24T02:51:41Z")

</div>

Here I have two meshes, I used vtkbool library([GitHub - zippy84/vtkbool: A new boolean operations filter for VTK](https://github.com/zippy84/vtkbool)) to get the result of which the operation of boolean difference, but the result modes have some errors, like holes. I want to known how to solve the problem?Thanks a lot.  
[A.stl](https://discourse.vtk.org/uploads/short-url/rghjxqC97vIwA5qSxrTSmf0PQ0K.stl) (684 Bytes)  
[B.stl](https://discourse.vtk.org/uploads/short-url/oDlcwLw8rVOq5EXo3hDPr7SPe9W.stl) (61.0 KB)  
[result.stl](https://discourse.vtk.org/uploads/short-url/xo5Y0ul54MKPwH6HjW6p0DVOgwj.stl) (82.0 KB)
