将单元中心作为属性添加到数据当中
"""
Append Cell Centers
~~~~~~~~~~~~~~~~~~~
This example will demonstrate how to append a dataset's cell centers as a length 3 tuple array.
This example demonstrates :class:`PVGeo.filters.AppendCellCenters`
"""
import pyvista
from PVGeo.filters import AppendCellCenters
###############################################################################
# Use an example mesh from pyvista
mesh = pyvista.RectilinearGrid("rectilinear.vtk")
print(mesh)
###############################################################################
# Run the PVGeo algorithm
centers = AppendCellCenters().apply(mesh)
print(centers)
###############################################################################
centers.plot()
RectilinearGrid (0x21462eca588)
N Cells: 16146
N Points: 18144
X Bounds: -3.500e+02, 1.350e+03
Y Bounds: -4.000e+02, 1.350e+03
Z Bounds: -8.500e+02, 0.000e+00
Dimensions: 27, 28, 24
N Arrays: 1
RectilinearGrid (0x21463fa2048)
N Cells: 16146
N Points: 18144
X Bounds: -3.500e+02, 1.350e+03
Y Bounds: -4.000e+02, 1.350e+03
Z Bounds: -8.500e+02, 0.000e+00
Dimensions: 27, 28, 24
N Arrays: 2
Append Cell Centers 源码:
- 获取中心点
vtkCellCenters
- 将中心点转换为
numy
数组 - 将数组作为单元属性添加到源数据当中
所以从打印的结果可以看出N Arrays: 2
pdi = self.GetInputData(inInfo, 0, 0)
pdo = self.GetOutputData(outInfo, 0)
# Find cell centers
filt = vtk.vtkCellCenters()
filt.SetInputDataObject(pdi)
filt.Update()
# I use the dataset adapter/numpy interface because its easy
centers = dsa.WrapDataObject(filt.GetOutput()).Points
centers = interface.convert_array(centers)
centers.SetName('Cell Centers')
# Copy input data and add cell centers as tuple array
pdo.DeepCopy(pdi)
pdo.GetCellData().AddArray(centers)