如何在单元质心处存储 vtkUnstructuredGrid 的矢量场

How to store a vector field for vtkUnstructuredGrid at the cell centroid

是否可以使用 vtkStructuredGrid 在四面体网格的质心处存储矢量场?我尝试了以下代码,但 VTK(8.1 版)对此有抱怨,我猜这是因为这个矢量场是在单元质心处定义的。

Warning: In c:\vtk\src\common\datamodel\vtkdataset.cxx, line 443
vtkUnstructuredGrid (0000020A4EC10D50): Point array  with 3 components, has 137 tuples but there are only 64 points

矢量场定义为:

vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();

// ... I already populated uGrid with points and tetrahedral information
// numberOfPoints = 64
// numberOfTetrahedra = 103

// Add a vector field at the centroid of each tetrahedral
vtkSmartPointer<vtkDoubleArray> vectors = vtkSmartPointer<vtkDoubleArray>::New();
vectors->SetNumberOfTuples(numberOfTetrahedra);
vectors->SetNumberOfComponents(3);
for (vtkIdType ielement = 0; ielement < numberOfTetrahedra; ielement++)
{
    vectors->InsertNextValue(vec[3 * ielement]);
    vectors->InsertNextValue(vec[3 * ielement + 1]);
    vectors->InsertNextValue(vec[3 * ielement + 2]);
}
uGrid->GetPointData()->SetVectors(vectors);

// Write the data to file
vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer = vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
writer->SetFileName("vtk_test_write_unstructured_grid.vtu");
writer->SetInputData(uGrid);
writer->Write();

如果有任何 tips/suggestions 解决此问题,我将不胜感激。

对于与单元格关联的数据,您应该使用 vtkCellData。因此,在您的代码中,请尝试将设置 vector 的行替换为

uGrid->GetCellData()->SetVectors(vectors);

这些向量与细胞有关。您可能会将它们解释为与细胞质心相关联,尽管在 .vtu 文件中您可能找不到与细胞质心的任何此类明确关联。

要将以单元为中心的场映射到网格的点,请考虑探索 vtkCellDataToPointData class。