# How can I combine a series of polydata to a 3D object in c++?

**URL:** https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726
**Category:** Support
**Tags:** code
**Created:** [June 15, 2023, 6:39am UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726 "2023-06-15T06:39:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![yen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yen](https://discourse.vtk.org/u/yen)
#### Post date: [June 15, 2023, 6:39am UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/1 "2023-06-15T06:39:32Z")

</div>

Hello, I am a newcomer to VTK recently. Now I have a task to finish. I need to construct a 3D object from a several sections of an object. To be more specifically, I need to first construct a section from multiple points and then stack all scetions into a 3D object.

Here is my current thought and code. I combine points in a single section and store them in vtkpolyData. Later I want to stack all vtkpolyData and then show them in the vtk renderer windows.

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

dsg\_ch0 is the section variable, whose structure is vector\<vector\>, Distance contains x, y, z coordination.

Does anyone know how to solve this problem properly? Thank you

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [June 15, 2023, 2:10pm UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/2 "2023-06-15T14:10:08Z")

</div>

If you need the ability to access properties, pick each individual polydata, you can use the `vtkCompositeDataSet` / `vtkPartitionedDataSetCollection` API for managing the whole model.  
If you would simply like to combine them into a single vtkPolyData object, use `vtkAppendPolyData`.

---

<div class="post-metadata">

### Author: ![yen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yen](https://discourse.vtk.org/u/yen)
#### Post date: [June 16, 2023, 12:09am UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/3 "2023-06-16T00:09:56Z")

</div>

Thank you sir! I actually tried vtkAppendPolyData before. However, it seemed to not work with vtkNew.

Here is my code using vtkAppendPolyData:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/9/90e6de4b12297ceaa0bfca1a58ab6ee377318759.png)

And here is the result I got:  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/4/402d6e69abba0839c32cdd756b552792bc63066b.png)

I am quite confused what happended. If I use vtkNew to replace all the pointer, it still not work.

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

When I did not use vtkAppendPolyData, it will work well to show me the polynomial line. Do you know what’s going on?

---

<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: [June 16, 2023, 4:19am UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/4 "2023-06-16T04:19:25Z")

</div>

I think it is your use of **Get** here: `appendFilter->AddInputData(polyData.Get());` , `polyData.Get())` is returning a raw pointer to the contained object. In this case it goes out of scope after the for loop exits, hence no PolyData.

Don’t use **Get** and see what happens. Also look at [CombinePolyData](https://examples.vtk.org/site/Cxx/Filtering/CombinePolyData/).

---

<div class="post-metadata">

### Author: ![yen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yen](https://discourse.vtk.org/u/yen)
#### Post date: [June 16, 2023, 6:11am UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/5 "2023-06-16T06:11:42Z")

</div>

I change to this, but it still not work.

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

But since you mentioned raw pointer. I come out that I first store all polydata I get in my own class. And then I use appendpolydata to store the polydata I store in the class.

BTW, I want to ask does this issue relative to the difference between vtkNew and vtkSmartPointer?

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [June 16, 2023, 2:16pm UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/6 "2023-06-16T14:16:27Z")

</div>

@yen I think the issue is that your polydata doesn’t have any cells. You are using `InsertCellPoint` without calling `InsertNextCell`. You need both of those calls for adding a valid cell to the lines cellarray.

Something like:

```cpp
lines->InsertNextCell(dsb_cho[i].size());
for(...)
{
   points->InsertNextPoint(...)
   lines->InsertCellPoint(j)
}

```

---

<div class="post-metadata">

### Author: ![yen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/y/50afbb/32.png) [@yen](https://discourse.vtk.org/u/yen)
#### Post date: [June 16, 2023, 11:59pm UTC](https://discourse.vtk.org/t/how-can-i-combine-a-series-of-polydata-to-a-3d-object-in-c/11726/8 "2023-06-16T23:59:44Z")

</div>

Thank you very much, I indeed miss InsertNextCell. Thank you very much, now I can continue my work!
