Negative cell volumes for some hexahedrons
I am using the CellSize
filter to compute cell volumes and it looks like for some cells its returning a negative volume.
I've reproduced this with the following ProgrammableSource
that outputs a vtk.vtkUnstructureGrid
:
# get output
uGrid = self.GetOutputDataObject(0)
# eight points for hexahedron
pointCoordinates = [
[0.0, 0.0, -6.1],
[0.604932, 0.0, -6.1],
[0.604932, 0.0, -5.49507],
[0, 0, -5.49507],
[0, 0.604932, -6.1],
[0.604932, 0.604932, -6.1],
[0.604932, 0.604932, -5.49507],
[0, 0.604932, -5.49507],
]
# create the points
points = vtk.vtkPoints()
# create a hexahedron from the points
hexahedron = vtk.vtkHexahedron()
for i, pt in enumerate(pointCoordinates):
points.InsertNextPoint(pt)
hexahedron.GetPointIds().SetId(i, i)
# add the hexahedron to a cell array
hexs = vtk.vtkCellArray()
hexs.InsertNextCell(hexahedron)
# add the points and hexahedron to an unstructured grid
uGrid.SetPoints(points)
uGrid.SetCells(hexahedron.GetCellType(), hexs)
Which returns a cell volume of -0.22137.
A screenshot in ParaView is below.
It seems the points should be in counter-clockwise order on the two opposite faces which they appear to be. I plot the points below.
import matplotlib.pyplot as plt
import numpy
fig = plt.figure()
ax = plt.axes(projection="3d")
pts = numpy.array([
[0.0, 0.0, -6.1],
[0.604932, 0.0, -6.1],
[0.604932, 0.0, -5.49507],
[0,0,-5.49507],
[0,0.604932,-6.1],
[0.604932,0.604932,-6.1],
[0.604932,0.604932,-5.49507],
[0,0.604932,-5.49507],
])
x = pts[:, 0]
y = pts[:, 1]
z = pts[:, 2]
ax.scatter(x[0], y[0], z[0], color="red")
ax.plot(x, y, z)
ax.scatter(x[-1], y[-1], z[-1], color="magenta")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
I am wondering if these cells' points are in the order ParaView expects or if there is a different way I should be computing the cell volume? Thanks.
Its works as expected on other hexahedrons. For example:
pointCoordinates = list()
pointCoordinates.append([0.0, 0.0, 0.0]) # Face 1
pointCoordinates.append([1.0, 0.0, 0.0])
pointCoordinates.append([1.0, 1.0, 0.0])
pointCoordinates.append([0.0, 1.0, 0.0])
pointCoordinates.append([0.0, 0.0, 1.0]) # Face 2
pointCoordinates.append([1.0, 0.0, 1.0])
pointCoordinates.append([1.0, 1.0, 1.0])
pointCoordinates.append([0.0, 1.0, 1.0])