Centroid Calculation for vtkPyramid Incorrect
The centroid calculation for vtkPyramid seems to be incorrect. This can be seen following code similar to the python Pyramid example.
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import (
vtkCellArray,
vtkPyramid,
vtkUnstructuredGrid
)
points = vtkPoints()
p0 = [1.0, 1.0, 1.0]
p1 = [-1.0, 1.0, 1.0]
p2 = [-1.0, -1.0, 1.0]
p3 = [1.0, -1.0, 1.0]
p4 = [0.0, 0.0, 0.0]
points.InsertNextPoint(p0)
points.InsertNextPoint(p1)
points.InsertNextPoint(p2)
points.InsertNextPoint(p3)
points.InsertNextPoint(p4)
pyramid = vtkPyramid()
pyramid.GetPointIds().SetId(0, 0)
pyramid.GetPointIds().SetId(1, 1)
pyramid.GetPointIds().SetId(2, 2)
pyramid.GetPointIds().SetId(3, 3)
pyramid.GetPointIds().SetId(4, 4)
cells = vtkCellArray()
cells.InsertNextCell(pyramid)
ug = vtkUnstructuredGrid()
ug.SetPoints(points)
ug.InsertNextCell(pyramid.GetCellType(), pyramid.GetPointIds())
cell = ug.GetCell(0)
centroid = [0.0, 0.0, 0.0]
cell.GetCentroid(centroid)
print(f"centroid: {centroid}")
This should print something like [0.0, 0.0, 0.8]
, instead this yields centroid: [0.16666666666666666, 0.0, 0.16666666666666666]
This part of the code https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/DataModel/vtkPyramid.cxx#L179-L183 calculates the centroid on one of the triangular faces, particularly the [3, 0, 4]
face. The centroid of this face is [2/3, 0, 2/3]
, which then results in the incorrect final centroid. It should calculate the centroid of the vtkQuad
instead which has a centroid of [0.0, 0.0, 1.0]
which would result in the correct final centroid.
I haven't checked the else block to see if this part is also incorrect, but it should also be checked.