shashashamti2008
shashashamti2008

Reputation: 2327

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

Is it possible to store a vector field at the centroid of a tetrahedral mesh using vtkStructuredGrid? I tried the following code but VTK (version 8.1) complains about it, which I guess is due to the fact that this vector field is defined at the cell centroid.

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

The vector field is defined by:

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();

I would appreciate for having any tips/suggestions to solve this issue.

Upvotes: 1

Views: 963

Answers (1)

user6764549
user6764549

Reputation:

For data associated with cells you should use vtkCellData. So in your code try replacing the line where you set vector to

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

These vectors are associated with the cell. You may interpret them as being associated with the cell centroid, although in the .vtu file you may not find any such explicit association with the centroids of the cells.

To map cell-centered fields to points of the mesh, consider exploring vtkCellDataToPointData class.

Upvotes: 2

Related Questions