# vtk Insertnextcell is too expensive

**URL:** https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866
**Category:** Dashboards
**Tags:** code, proposal
**Created:** [July 5, 2022, 9:48am UTC](https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866 "2022-07-05T09:48:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zzusunquan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zzusunquan/32/5449_2.png) [@zzusunquan](https://discourse.vtk.org/u/zzusunquan)
#### Post date: [July 5, 2022, 9:48am UTC](https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866/1 "2022-07-05T09:48:40Z")

</div>

```auto
// cell_x the triangle vertex index x
// cell_y the triangle vertex index y
// cell_z the triangle vertex index z
// _ptCellArray is a vtkCellArray object 
// the code is c# 
for(int i = 0; i < cellNum; i++)
{
   var triangle = vtkTriangle.New();
   triangle.GetPointIds().SetId(0, cell_x[i]);
   triangle.GetPointIds().SetId(1, cell_y[i]);
   triangle.GetPointIds().SetId(2, cell_z[i]);
   _ptCellArray.InsertNextCell(triangle);
}

```

the above code is too expensive, every 100 points need 1ms. how can i accelerate it.

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [July 5, 2022, 10:32am UTC](https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866/2 "2022-07-05T10:32:23Z")

</div>

```auto
__ptCellArray.Allocate(cellNum * 3);
for(int i = 0; i < cellNum; i++)
{
   __ptCellArray.InsertNextCell(3);
   __ptCellArray.InsertCellPoint(cell_x[i]);
   __ptCellArray.InsertCellPoint(cell_y[i]);
   __ptCellArray.InsertCellPoint(cell_z[i]);
}

```

---

<div class="post-metadata">

### Author: ![zzusunquan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zzusunquan/32/5449_2.png) [@zzusunquan](https://discourse.vtk.org/u/zzusunquan)
#### Post date: [July 6, 2022, 1:15am UTC](https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866/3 "2022-07-06T01:15:18Z")

</div>

thank you every much, your code worked！ It cost 3 percent of the previous time。  
I have another question want to ask you, I use the following code

```auto
// _pd is a vtkPolyData object only with points
// the code is c# 
var delaunay = vtkDelaunay2D.New();
delaunay.SetTolerance(0.001);
delaunay.SetInput(_pd);
delaunay.Update();

```

the above code cost 100 millisecond level while the \_pd have 10,000 points,.  
So i use c++ CGAL build delaunay2D ， in c++ code, CGAL build delaunay2D cost 10 ms level with the same points. Then I Pass cell information to c#.  
I think I’m using it wrong, can you tell me how to speed up the delaunay2D of vtk? I just need cell and point information。

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [July 6, 2022, 10:04am UTC](https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866/4 "2022-07-06T10:04:58Z")

</div>

You should post under another topic. For your second question, I have no idea.

---

<div class="post-metadata">

### Author: ![zzusunquan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zzusunquan/32/5449_2.png) [@zzusunquan](https://discourse.vtk.org/u/zzusunquan)
#### Post date: [July 6, 2022, 10:21am UTC](https://discourse.vtk.org/t/vtk-insertnextcell-is-too-expensive/8866/5 "2022-07-06T10:21:39Z")

</div>

thank you very much， i will do it
