# Cell Center from GetParametricCenter and EvaluateLocation

**URL:** https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108
**Category:** Support
**Created:** [August 31, 2020, 9:16am UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108 "2020-08-31T09:16:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![a-jp1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-jp1/32/1710_2.png) [@a-jp1](https://discourse.vtk.org/u/a-jp1)
#### Post date: [August 31, 2020, 9:16am UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/1 "2020-08-31T09:16:12Z")

</div>

Hi,

I’m looking for the exact (correct) cell centre for cells of a vtkUnstructuredGrid. Exact (correct) in the sense that it would return the same result as a divergence theorem face-based integration around the faces of the cell to obtain the cell centre. I have been using:

```auto
 auto *cell = mesh->GetCell(cell);
 auto *cellPointIds = cell->GetPointIds();

 Vector pcoords{garbage, garbage, garbage};
 std::vector<double> weights(cellPointIds->GetNumberOfIds());
 int subId = cell->GetParametricCenter(pcoords.data());
 Vector center{garbage, garbage, garbage};
 cell->EvaluateLocation(subId, pcoords.data(), center.data(), weights.data());

```

My understanding is that this is the correct way to obtain the cell centre, first in parametric space, and then in real space. Is this correct? My understanding is that this is also correct for 2D cells and 3D cells. Is that also correct?

Many thanks,  
Andy

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [August 31, 2020, 4:43pm UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/2 "2020-08-31T16:43:59Z")

</div>

Is what you are looking for the same thing as the center of mass (the centroid)? If it is the case, you can get it in most `vtkCell3D` by calling `GetCentroid`, and on any polygon using the static method `vtkPolygon::ComputeCentroid`.

---

<div class="post-metadata">

### Author: ![a-jp1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-jp1/32/1710_2.png) [@a-jp1](https://discourse.vtk.org/u/a-jp1)
#### Post date: [August 31, 2020, 5:04pm UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/3 "2020-08-31T17:04:18Z")

</div>

Yep Center of mass assuming constant density would be the same. Couldn’t work out if that works for 2d cells?

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [August 31, 2020, 5:13pm UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/4 "2020-08-31T17:13:45Z")

</div>

Use `vtkPolygon::ComputeCentroid` for 2D cells.

---

<div class="post-metadata">

### Author: ![a-jp1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-jp1/32/1710_2.png) [@a-jp1](https://discourse.vtk.org/u/a-jp1)
#### Post date: [August 31, 2020, 5:15pm UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/5 "2020-08-31T17:15:07Z")

</div>

Ok thanks. Can I just confirm then. The result I’ve been getting from the previous code I was posted gives the wrong position for the centroid?

---

<div class="post-metadata">

### Author: ![Yohann\_Bearzi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yohann_bearzi/32/671_2.png) [@Yohann\_Bearzi](https://discourse.vtk.org/u/Yohann_Bearzi)
#### Post date: [August 31, 2020, 5:40pm UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/6 "2020-08-31T17:40:16Z")

</div>

I don’t know for sure, but I would say no in the general case. If you look at `vtkPolygon::EvaluateLocation`, `x` is produced from a parameterization computed internally. This parameterization is unchanged if you append the polygon with a point inside its previous convex hull, which I feel should influence the centroid’s location.

But don’t take my answer as truthful, I am just throwing a guess here. It might actually work for triangles and tetrahedrons, who have a well defined parameterization.

---

<div class="post-metadata">

### Author: ![a-jp1](https://discourse.vtk.org/user_avatar/discourse.vtk.org/a-jp1/32/1710_2.png) [@a-jp1](https://discourse.vtk.org/u/a-jp1)
#### Post date: [September 1, 2020, 1:42pm UTC](https://discourse.vtk.org/t/cell-center-from-getparametriccenter-and-evaluatelocation/4108/7 "2020-09-01T13:42:03Z")

</div>

ok thanks
