I have a vtkUnstructuredGrid composed of vtkHexahedrons (originally it was a vtkPartitionedDataSet composed of vtkStructuredGrids - it made my life easier to just have one partition). The next processing stage needs to store a file that is a list of unique faces present in the volume (I didn’t design the program needing this data - I am just constructing the file).
Originally I was just going to read in the data, append all of the partitions together, create a new vtkPolyData object so that I could run vtkRemoveDuplicatePolys. The problem was that I had 12 million hexahedrons which turn into roughly 72 million quads, and then running the algorithm ate up all of memory and the process gets killed by the OOM reaper.
One possibility I wanted to explore was using a set of some sort to only add unique faces - I just didn’t know what class to use in the VTK library?
Presently the code (snippet) looks like this:
def polygon_soup(ds):
"Convert vtkHexahedron (3D cells) in the dataset to faces composed of vtkTriangles."
logging.debug(f"merging {ds.GetNumberOfPartitions()} partitions in vtkPartitionedDataSet to vtkUnstructuredGrid")
af = vtk.vtkAppendDataSets()
af.MergePointsOn()
for ind in range(ds.GetNumberOfPartitions()):
af.AddInputData(ds.GetPartition(ind))
af.Update()
data = af.GetOutputDataObject(0)
pd = vtk.vtkPolyData()
logging.debug(f"saving {data.GetNumberOfPoints()} node locations into new vtkPolyData")
pd.SetPoints(data.GetPoints())
logging.debug(f"saving {data.GetPointData().GetNumberOfArrays()} scalar arrays into new vtkPolyData")
for a_ind in range(data.GetPointData().GetNumberOfArrays()):
pd.GetPointData().AddArray(data.GetPointData().GetArray(a_ind))
ca = vtk.vtkCellArray()
for c_idx in range(ds.GetNumberOfCells()):
cell = data.GetCell(c_idx)
for f_idx in range(cell.GetNumberOfFaces()):
face = cell.GetFace(f_idx)
node_count = face.GetNumberOfPoints()
ca.InsertNextCell(node_count)
for n_idx in range(node_count):
ca.InsertCellPoint(face.GetPointId(n_idx))
logging.debug(f"converted {data.GetNumberOfCells()} vtkHexahedrons into {ca.GetNumberOfCells()} vtkQuads")
pd.SetPolys(ca)
logging.debug(f"removing duplicate vtkQuads and converting to vtkTriangles")
cpd = vtk.vtkCleanPolyData()
cpd.SetInputData(pd)
rdp = vtk.vtkRemoveDuplicatePolys()
rdp.SetInputConnection(cpd.GetOutputPort())
tf = vtk.vtkTriangleFilter()
tf.SetInputConnection(rdp.GetOutputPort())
tf.Update()
return tf.GetOutputDataObject(0)