# How to merge two PolyData into one

**URL:** <https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431>\
**Category:** Development\
**Created:** [October 15, 2020, 5:27pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431 "2020-10-15T17:27:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![siaeleni](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.vtk.org/u/siaeleni)\
**Post date:** [October 15, 2020, 5:27pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/1 "2020-10-15T17:27:13Z")

</div>

Hello,

I would like to merge two vtkpolydata if it is possible, but I want to know at the end which point comes from which dataset. Also, I want to be sure that BuildLinks() will work and be able to find the correct neighbor points at the end.

I am not sure which is the best approach and if it is feasible.

Thanks

---

<div class="post-metadata">

**Author:** ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)\
**Post date:** [October 15, 2020, 7:03pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/2 "2020-10-15T19:03:37Z")

</div>

You could identify each PolyData with a scalar point data array (PolyDataIdx). There might be a filter to do this, I’m not sure there is.

Setup a pipeline with vtkAppendPolyData----\>vtkCleanPolyData. Add inputs to Append and Update(). Call BuildLinks() on pipeline’s output after clean up.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [October 15, 2020, 7:29pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/3 "2020-10-15T19:29:02Z")

</div>

> [@jaswantp](#):
>
> You could identify each PolyData with a scalar point data array (PolyDataIdx). There might be a filter to do this, I’m not sure there is.

You can create an array that contains a constant value using [vtkArrayCalculator](https://vtk.org/doc/nightly/html/classvtkArrayCalculator.html).

> [@jaswantp](#):
>
> Setup a pipeline with vtkAppendPolyData----\>vtkCleanPolyData.

This works well if the two meshes do not intersect. If they do then you need to do Boolean mesh operation.

---

<div class="post-metadata">

**Author:** ![siaeleni](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.vtk.org/u/siaeleni)\
**Post date:** [October 15, 2020, 8:29pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/4 "2020-10-15T20:29:24Z")

</div>

Thank you for the information.

I try the following:

> union = vtk.vtkBooleanOperationPolyDataFilter()  
> union.SetOperationToUnion()  
> union.SetInputData(0, polydata)  
> union.SetInputData(1, polydata2)  
> union.Update()
> 
> outPoly = union.GetOutput()  
> outPoly.GetNumberOfCells()  
> 0  
> outPoly.GetNumberOfPoints()  
> 0

but the output has 0 cells or points. Do I miss something here?

Thanks

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [October 15, 2020, 9:16pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/5 "2020-10-15T21:16:34Z")

</div>

Boolean mesh operations in VTK do not work reliably (may provide invalid outputs for completely valid inputs, randomly). [vtkBool](https://github.com/zippy84/vtkbool) is supposed to work much better.

---

<div class="post-metadata">

**Author:** ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)\
**Post date:** [October 16, 2020, 2:10am UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/6 "2020-10-16T02:10:57Z")

</div>

Are the polys 2D or 3D? Last I checked, the Boolean filters didn’t work for polydata with 2D cells. Straight up outputs an empty polydata. What I mean is the BooleanOperation filters will work only when your polydata has a surface that encloses some volume.

---

<div class="post-metadata">

**Author:** ![siaeleni](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.vtk.org/u/siaeleni)\
**Post date:** [October 16, 2020, 3:47am UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/7 "2020-10-16T03:47:13Z")

</div>

My polys are 3D, does it mean that it should work? Could you please give me a working example with data?  
Thanks

---

<div class="post-metadata">

**Author:** ![sandress](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@sandress](https://discourse.vtk.org/u/sandress)\
**Post date:** [October 19, 2020, 12:29pm UTC](https://discourse.vtk.org/t/how-to-merge-two-polydata-into-one/4431/8 "2020-10-19T12:29:21Z")

</div>

Will vtkBool be integrated in the vtk library at some point? The unreliable vtkBooleanOperationPolyDataFilter is a real drawback.
