Merge branch 'vtkwriter-save-global-fields' into 'master'

Write global fields when saving to VTK files

See merge request vtk/vtk-m!2684
This commit is contained in:
Gunther Weber 2024-07-03 08:28:31 -04:00
commit 4725bd66d7

@ -392,6 +392,46 @@ void WriteCellFields(std::ostream& out,
} }
} }
void WriteGlobalFields(std::ostream& out,
const vtkm::cont::DataSet& dataSet,
vtkm::io::FileType fileType)
{
// count number of global arrays
int n = 0;
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
{
if (dataSet.GetField(f).IsFieldGlobal())
n++;
}
if (n < 1)
return;
// write header
out << "FIELD FieldData " << n << '\n';
// write fields
for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++)
{
const vtkm::cont::Field field = dataSet.GetField(f);
if (!field.IsFieldGlobal())
{
continue;
}
std::string name = field.GetName();
int nComponents = field.GetData().GetNumberOfComponentsFlat();
int nValues = field.GetData().GetNumberOfValues();
std::string typeName = GetFieldTypeName(field.GetData());
out << name << " " << nComponents << " " << nValues << " " << typeName << '\n';
OutputArrayData(field.GetData(), out, fileType);
out << "METADATA\nINFORMATION 0\n\n";
}
}
template <class CellSetType> template <class CellSetType>
void WriteDataSetAsUnstructured(std::ostream& out, void WriteDataSetAsUnstructured(std::ostream& out,
const vtkm::cont::DataSet& dataSet, const vtkm::cont::DataSet& dataSet,
@ -406,10 +446,13 @@ void WriteDataSetAsUnstructured(std::ostream& out,
template <vtkm::IdComponent DIM> template <vtkm::IdComponent DIM>
void WriteDataSetAsStructuredPoints(std::ostream& out, void WriteDataSetAsStructuredPoints(std::ostream& out,
const vtkm::cont::ArrayHandleUniformPointCoordinates& points, const vtkm::cont::ArrayHandleUniformPointCoordinates& points,
const vtkm::cont::CellSetStructured<DIM>& cellSet) const vtkm::cont::CellSetStructured<DIM>& cellSet,
const vtkm::cont::DataSet& dataSet,
vtkm::io::FileType fileType)
{ {
out << "DATASET STRUCTURED_POINTS\n"; out << "DATASET STRUCTURED_POINTS\n";
WriteGlobalFields(out, dataSet, fileType);
WriteDimensions(out, cellSet); WriteDimensions(out, cellSet);
auto portal = points.ReadPortal(); auto portal = points.ReadPortal();
@ -472,7 +515,11 @@ void WriteDataSetAsStructured(std::ostream& out,
{ {
// uniform is written as "structured points" // uniform is written as "structured points"
WriteDataSetAsStructuredPoints( WriteDataSetAsStructuredPoints(
out, coordSystem.AsArrayHandle<vtkm::cont::ArrayHandleUniformPointCoordinates>(), cellSet); out,
coordSystem.AsArrayHandle<vtkm::cont::ArrayHandleUniformPointCoordinates>(),
cellSet,
dataSet,
fileType);
} }
else if (coordSystem.IsType<ArrayHandleRectilinearCoordinates<vtkm::Float32>>()) else if (coordSystem.IsType<ArrayHandleRectilinearCoordinates<vtkm::Float32>>())
{ {