Commit 315e5f7e authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'issue/data_array_names' into 'master'

Store IDs of DataArrays using section name and Name attribute

See merge request extensions/dune-vtk!6
parents 13c0549b c7e99f56
...@@ -43,6 +43,7 @@ namespace Dune ...@@ -43,6 +43,7 @@ namespace Dune
// Type storing information about read data // Type storing information about read data
struct DataArrayAttributes struct DataArrayAttributes
{ {
std::string name;
Vtk::DataTypes type; Vtk::DataTypes type;
unsigned int components = 1; unsigned int components = 1;
std::uint64_t offset = 0; std::uint64_t offset = 0;
...@@ -105,11 +106,11 @@ namespace Dune ...@@ -105,11 +106,11 @@ namespace Dune
/// NOTE: requires an aforegoing call to \ref read() /// NOTE: requires an aforegoing call to \ref read()
GridFunction<Vtk::PointContext> getPointData (std::string const& name) const GridFunction<Vtk::PointContext> getPointData (std::string const& name) const
{ {
auto const& data = dataArray_.at(name); auto const& data = dataArray_.at("PointData." + name);
VTK_ASSERT_MSG(data.section == POINT_DATA, VTK_ASSERT_MSG(data.section == POINT_DATA,
"The data to extract is not point-data. Use `getCellData()` instead!"); "The data to extract is not point-data. Use `getCellData()` instead!");
return {*creator_, pointData_.at(name), data.components, return {*creator_, pointData_.at("PointData." + name), data.components,
vec_types, vec_offsets, vec_connectivity}; vec_types, vec_offsets, vec_connectivity};
} }
...@@ -117,11 +118,11 @@ namespace Dune ...@@ -117,11 +118,11 @@ namespace Dune
/// NOTE: requires an aforegoing call to \ref read() /// NOTE: requires an aforegoing call to \ref read()
GridFunction<Vtk::CellContext> getCellData (std::string const& name) const GridFunction<Vtk::CellContext> getCellData (std::string const& name) const
{ {
auto const& data = dataArray_.at(name); auto const& data = dataArray_.at("CellData." + name);
VTK_ASSERT_MSG(data.section == CELL_DATA, VTK_ASSERT_MSG(data.section == CELL_DATA,
"The data to extract is not cell-data. Use `getPointData()` instead!"); "The data to extract is not cell-data. Use `getPointData()` instead!");
return {*creator_, cellData_.at(name), data.components, return {*creator_, cellData_.at("CellData." + name), data.components,
vec_types, vec_offsets, vec_connectivity}; vec_types, vec_offsets, vec_connectivity};
} }
...@@ -206,6 +207,9 @@ namespace Dune ...@@ -206,6 +207,9 @@ namespace Dune
return result; return result;
} }
// Convert a section into a string
std::string toString (Sections s) const;
// Find beginning of appended binary data // Find beginning of appended binary data
std::uint64_t findAppendedDataPosition (std::ifstream& input) const; std::uint64_t findAppendedDataPosition (std::ifstream& input) const;
...@@ -237,10 +241,11 @@ namespace Dune ...@@ -237,10 +241,11 @@ namespace Dune
std::size_t numberOfPoints_ = 0; //< Number of vertices in the grid std::size_t numberOfPoints_ = 0; //< Number of vertices in the grid
// offset information for appended data // offset information for appended data
// map Name -> {DataType,NumberOfComponents,Offset} // map: id -> {Name,DataType,NumberOfComponents,Offset}
std::map<std::string, DataArrayAttributes> dataArray_; std::map<std::string, DataArrayAttributes> dataArray_;
// storage for internal point and cell data // storage for internal point and cell data
// map: id -> vector of data entries per point/cell
std::map<std::string, std::vector<FieldType>> pointData_; std::map<std::string, std::vector<FieldType>> pointData_;
std::map<std::string, std::vector<FieldType>> cellData_; std::map<std::string, std::vector<FieldType>> cellData_;
......
...@@ -43,7 +43,7 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp ...@@ -43,7 +43,7 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp
{ {
clear(); clear();
std::string compressor = ""; std::string compressor = "";
std::string data_name = "", data_format = ""; std::string data_id = "", data_format = "";
Vtk::DataTypes data_type = Vtk::UNKNOWN; Vtk::DataTypes data_type = Vtk::UNKNOWN;
unsigned int data_components = 0; unsigned int data_components = 0;
std::uint64_t data_offset = 0; std::uint64_t data_offset = 0;
...@@ -110,10 +110,12 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp ...@@ -110,10 +110,12 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp
data_type = Vtk::Map::to_datatype[attr["type"]]; data_type = Vtk::Map::to_datatype[attr["type"]];
if (!attr["Name"].empty()) // Use Section.Name as id
data_name = Vtk::to_lower(attr["Name"]); data_id = toString(section) + "." + attr["Name"];
else if (section == POINTS)
data_name = "points"; if (section == POINTS)
// In the Points section must only be one DataArray with id=Points
data_id = "Points";
data_components = 1; data_components = 1;
if (!attr["NumberOfComponents"].empty()) if (!attr["NumberOfComponents"].empty())
...@@ -135,7 +137,7 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp ...@@ -135,7 +137,7 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp
} }
// Store attributes of DataArray // Store attributes of DataArray
dataArray_[data_name] = {data_type, data_components, data_offset, section}; dataArray_[data_id] = {attr["Name"], data_type, data_components, data_offset, section};
// Skip section in appended mode // Skip section in appended mode
if (data_format == "appended") { if (data_format == "appended") {
...@@ -179,7 +181,7 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp ...@@ -179,7 +181,7 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp
VTK_ASSERT_MSG(attr["encoding"] == "raw", "base64 encoding not supported"); VTK_ASSERT_MSG(attr["encoding"] == "raw", "base64 encoding not supported");
offset0_ = findAppendedDataPosition(input); offset0_ = findAppendedDataPosition(input);
if (dataArray_["points"].type == Vtk::FLOAT32) if (dataArray_["Points"].type == Vtk::FLOAT32)
readPointsAppended<float>(input); readPointsAppended<float>(input);
else else
readPointsAppended<double>(input); readPointsAppended<double>(input);
...@@ -187,18 +189,18 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp ...@@ -187,18 +189,18 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp
readCellsAppended(input); readCellsAppended(input);
// read point an cell data // read point an cell data
for (auto const& [name,data] : dataArray_) { for (auto const& [id,data] : dataArray_) {
if (data.section == POINT_DATA) { if (data.section == POINT_DATA) {
if (data.type == Vtk::FLOAT32) if (data.type == Vtk::FLOAT32)
readPointDataAppended<float>(input, name); readPointDataAppended<float>(input, id);
else else
readPointDataAppended<double>(input, name); readPointDataAppended<double>(input, id);
} }
else if (data.section == CELL_DATA) { else if (data.section == CELL_DATA) {
if (data.type == Vtk::FLOAT32) if (data.type == Vtk::FLOAT32)
readCellDataAppended<float>(input, name); readCellDataAppended<float>(input, id);
else else
readCellDataAppended<double>(input, name); readCellDataAppended<double>(input, id);
} }
} }
...@@ -209,16 +211,16 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp ...@@ -209,16 +211,16 @@ void VtkReader<Grid,Creator,Field>::readSerialFileFromStream (std::ifstream& inp
switch (section) { switch (section) {
case PD_DATA_ARRAY: case PD_DATA_ARRAY:
section = readPointData(input, data_name); section = readPointData(input, data_id);
break; break;
case POINTS_DATA_ARRAY: case POINTS_DATA_ARRAY:
section = readPoints(input, data_name); section = readPoints(input, data_id);
break; break;
case CD_DATA_ARRAY: case CD_DATA_ARRAY:
section = readCellData(input, data_name); section = readCellData(input, data_id);
break; break;
case CELLS_DATA_ARRAY: case CELLS_DATA_ARRAY:
section = readCells(input, data_name); section = readCells(input, data_id);
break; break;
default: default:
// do nothing // do nothing
...@@ -337,13 +339,13 @@ Sections skipRestOfDataArray (IStream& input, Sections section, Sections parent_ ...@@ -337,13 +339,13 @@ Sections skipRestOfDataArray (IStream& input, Sections section, Sections parent_
// Read values stored on the cells with name `name` // Read values stored on the cells with name `name`
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
typename VtkReader<Grid,Creator,Field>::Sections typename VtkReader<Grid,Creator,Field>::Sections
VtkReader<Grid,Creator,Field>::readCellData (std::ifstream& input, std::string name) VtkReader<Grid,Creator,Field>::readCellData (std::ifstream& input, std::string id)
{ {
VTK_ASSERT(numberOfCells_ > 0); VTK_ASSERT(numberOfCells_ > 0);
unsigned int components = dataArray_[name].components; unsigned int components = dataArray_[id].components;
Sections sec; Sections sec;
std::vector<Field>& values = cellData_[name]; std::vector<Field>& values = cellData_[id];
sec = readDataArray(input, values, components*numberOfCells_, CD_DATA_ARRAY, CELL_DATA); sec = readDataArray(input, values, components*numberOfCells_, CD_DATA_ARRAY, CELL_DATA);
if (sec != CELL_DATA) if (sec != CELL_DATA)
sec = skipRestOfDataArray(input, CD_DATA_ARRAY, CELL_DATA); sec = skipRestOfDataArray(input, CD_DATA_ARRAY, CELL_DATA);
...@@ -356,29 +358,29 @@ VtkReader<Grid,Creator,Field>::readCellData (std::ifstream& input, std::string n ...@@ -356,29 +358,29 @@ VtkReader<Grid,Creator,Field>::readCellData (std::ifstream& input, std::string n
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
template <class T> template <class T>
void VtkReader<Grid,Creator,Field>::readCellDataAppended (std::ifstream& input, std::string name) void VtkReader<Grid,Creator,Field>::readCellDataAppended (std::ifstream& input, std::string id)
{ {
VTK_ASSERT(numberOfCells_ > 0); VTK_ASSERT(numberOfCells_ > 0);
unsigned int components = dataArray_[name].components; unsigned int components = dataArray_[id].components;
std::vector<T> values; std::vector<T> values;
readAppended(input, values, dataArray_[name].offset); readAppended(input, values, dataArray_[id].offset);
VTK_ASSERT(values.size() == components*numberOfCells_); VTK_ASSERT(values.size() == components*numberOfCells_);
cellData_[name].resize(values.size()); cellData_[id].resize(values.size());
std::copy(values.begin(), values.end(), cellData_[name].begin()); std::copy(values.begin(), values.end(), cellData_[id].begin());
} }
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
typename VtkReader<Grid,Creator,Field>::Sections typename VtkReader<Grid,Creator,Field>::Sections
VtkReader<Grid,Creator,Field>::readPointData (std::ifstream& input, std::string name) VtkReader<Grid,Creator,Field>::readPointData (std::ifstream& input, std::string id)
{ {
VTK_ASSERT(numberOfPoints_ > 0); VTK_ASSERT(numberOfPoints_ > 0);
unsigned int components = dataArray_[name].components; unsigned int components = dataArray_[id].components;
Sections sec; Sections sec;
std::vector<Field>& values = pointData_[name]; std::vector<Field>& values = pointData_[id];
sec = readDataArray(input, values, components*numberOfPoints_, PD_DATA_ARRAY, POINT_DATA); sec = readDataArray(input, values, components*numberOfPoints_, PD_DATA_ARRAY, POINT_DATA);
if (sec != POINT_DATA) if (sec != POINT_DATA)
sec = skipRestOfDataArray(input, PD_DATA_ARRAY, POINT_DATA); sec = skipRestOfDataArray(input, PD_DATA_ARRAY, POINT_DATA);
...@@ -391,27 +393,27 @@ VtkReader<Grid,Creator,Field>::readPointData (std::ifstream& input, std::string ...@@ -391,27 +393,27 @@ VtkReader<Grid,Creator,Field>::readPointData (std::ifstream& input, std::string
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
template <class T> template <class T>
void VtkReader<Grid,Creator,Field>::readPointDataAppended (std::ifstream& input, std::string name) void VtkReader<Grid,Creator,Field>::readPointDataAppended (std::ifstream& input, std::string id)
{ {
VTK_ASSERT(numberOfPoints_ > 0); VTK_ASSERT(numberOfPoints_ > 0);
unsigned int components = dataArray_[name].components; unsigned int components = dataArray_[id].components;
std::vector<T> values; std::vector<T> values;
readAppended(input, values, dataArray_[name].offset); readAppended(input, values, dataArray_[id].offset);
VTK_ASSERT(values.size() == components*numberOfPoints_); VTK_ASSERT(values.size() == components*numberOfPoints_);
pointData_[name].resize(values.size()); pointData_[id].resize(values.size());
std::copy(values.begin(), values.end(), pointData_[name].begin()); std::copy(values.begin(), values.end(), pointData_[id].begin());
} }
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
typename VtkReader<Grid,Creator,Field>::Sections typename VtkReader<Grid,Creator,Field>::Sections
VtkReader<Grid,Creator,Field>::readPoints (std::ifstream& input, std::string name) VtkReader<Grid,Creator,Field>::readPoints (std::ifstream& input, std::string id)
{ {
using T = typename GlobalCoordinate::value_type; using T = typename GlobalCoordinate::value_type;
VTK_ASSERT(numberOfPoints_ > 0); VTK_ASSERT(numberOfPoints_ > 0);
VTK_ASSERT(dataArray_["points"].components == 3u); VTK_ASSERT(dataArray_["Points"].components == 3u);
Sections sec; Sections sec;
...@@ -442,9 +444,9 @@ template <class Grid, class Creator, class Field> ...@@ -442,9 +444,9 @@ template <class Grid, class Creator, class Field>
void VtkReader<Grid,Creator,Field>::readPointsAppended (std::ifstream& input) void VtkReader<Grid,Creator,Field>::readPointsAppended (std::ifstream& input)
{ {
VTK_ASSERT(numberOfPoints_ > 0); VTK_ASSERT(numberOfPoints_ > 0);
VTK_ASSERT(dataArray_["points"].components == 3u); VTK_ASSERT(dataArray_["Points"].components == 3u);
std::vector<T> point_values; std::vector<T> point_values;
readAppended(input, point_values, dataArray_["points"].offset); readAppended(input, point_values, dataArray_["Points"].offset);
VTK_ASSERT(point_values.size() == 3*numberOfPoints_); VTK_ASSERT(point_values.size() == 3*numberOfPoints_);
// extract points from continuous values // extract points from continuous values
...@@ -462,20 +464,20 @@ void VtkReader<Grid,Creator,Field>::readPointsAppended (std::ifstream& input) ...@@ -462,20 +464,20 @@ void VtkReader<Grid,Creator,Field>::readPointsAppended (std::ifstream& input)
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
typename VtkReader<Grid,Creator,Field>::Sections typename VtkReader<Grid,Creator,Field>::Sections
VtkReader<Grid,Creator,Field>::readCells (std::ifstream& input, std::string name) VtkReader<Grid,Creator,Field>::readCells (std::ifstream& input, std::string id)
{ {
Sections sec = CELLS_DATA_ARRAY; Sections sec = CELLS_DATA_ARRAY;
VTK_ASSERT(numberOfCells_ > 0); VTK_ASSERT(numberOfCells_ > 0);
if (name == "types") { if (id == "Cells.types") {
sec = readDataArray(input, vec_types, numberOfCells_, CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_types, numberOfCells_, CELLS_DATA_ARRAY, CELLS);
VTK_ASSERT(vec_types.size() == numberOfCells_); VTK_ASSERT(vec_types.size() == numberOfCells_);
} else if (name == "offsets") { } else if (id == "Cells.offsets") {
sec = readDataArray(input, vec_offsets, numberOfCells_, CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_offsets, numberOfCells_, CELLS_DATA_ARRAY, CELLS);
VTK_ASSERT(vec_offsets.size() == numberOfCells_); VTK_ASSERT(vec_offsets.size() == numberOfCells_);
} else if (name == "connectivity") { } else if (id == "Cells.connectivity") {
sec = readDataArray(input, vec_connectivity, std::size_t(-1), CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_connectivity, std::size_t(-1), CELLS_DATA_ARRAY, CELLS);
} else if (name == "global_point_ids") { } else if (id == "Cells.global_point_ids") {
sec = readDataArray(input, vec_point_ids, numberOfPoints_, CELLS_DATA_ARRAY, CELLS); sec = readDataArray(input, vec_point_ids, numberOfPoints_, CELLS_DATA_ARRAY, CELLS);
VTK_ASSERT(vec_point_ids.size() == numberOfPoints_); VTK_ASSERT(vec_point_ids.size() == numberOfPoints_);
} }
...@@ -488,9 +490,9 @@ template <class Grid, class Creator, class Field> ...@@ -488,9 +490,9 @@ template <class Grid, class Creator, class Field>
void VtkReader<Grid,Creator,Field>::readCellsAppended (std::ifstream& input) void VtkReader<Grid,Creator,Field>::readCellsAppended (std::ifstream& input)
{ {
VTK_ASSERT(numberOfCells_ > 0); VTK_ASSERT(numberOfCells_ > 0);
auto types_data = dataArray_["types"]; auto types_data = dataArray_["Cells.types"];
auto offsets_data = dataArray_["offsets"]; auto offsets_data = dataArray_["Cells.offsets"];
auto connectivity_data = dataArray_["connectivity"]; auto connectivity_data = dataArray_["Cells.connectivity"];
VTK_ASSERT(types_data.type == Vtk::UINT8); VTK_ASSERT(types_data.type == Vtk::UINT8);
readAppended(input, vec_types, types_data.offset); readAppended(input, vec_types, types_data.offset);
...@@ -518,8 +520,8 @@ void VtkReader<Grid,Creator,Field>::readCellsAppended (std::ifstream& input) ...@@ -518,8 +520,8 @@ void VtkReader<Grid,Creator,Field>::readCellsAppended (std::ifstream& input)
else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell connectivity."); } else { DUNE_THROW(Dune::NotImplemented, "Unsupported DataType in Cell connectivity."); }
VTK_ASSERT(vec_connectivity.size() == std::size_t(vec_offsets.back())); VTK_ASSERT(vec_connectivity.size() == std::size_t(vec_offsets.back()));
if (dataArray_.count("global_point_ids") > 0) { if (dataArray_.count("Cells.global_point_ids") > 0) {
auto point_id_data = dataArray_["global_point_ids"]; auto point_id_data = dataArray_["Cells.global_point_ids"];
VTK_ASSERT(point_id_data.type == Vtk::UINT64); VTK_ASSERT(point_id_data.type == Vtk::UINT64);
readAppended(input, vec_point_ids, point_id_data.offset); readAppended(input, vec_point_ids, point_id_data.offset);
VTK_ASSERT(vec_point_ids.size() == numberOfPoints_); VTK_ASSERT(vec_point_ids.size() == numberOfPoints_);
...@@ -628,6 +630,39 @@ void VtkReader<Grid,Creator,Field>::fillGridCreator (bool insertPieces) ...@@ -628,6 +630,39 @@ void VtkReader<Grid,Creator,Field>::fillGridCreator (bool insertPieces)
creator_->insertPieces(pieces_); creator_->insertPieces(pieces_);
} }
// Convert section into string
template <class Grid, class Creator, class Field>
std::string VtkReader<Grid,Creator,Field>::toString(Sections s) const
{
switch (s) {
case VTK_FILE:
return "VTKFile";
case UNSTRUCTURED_GRID:
return "UnstructuredGrid";
case PIECE:
return "Piece";
case POINT_DATA:
return "PointData";
case CELL_DATA:
return "CellData";
case POINTS:
return "Points";
case CELLS:
return "Cells";
case APPENDED_DATA:
return "AppendedData";
case PD_DATA_ARRAY:
case CD_DATA_ARRAY:
case POINTS_DATA_ARRAY:
case CELLS_DATA_ARRAY:
return "DataArray";
default:
return "Unknown";
}
}
// Assume input already read the line <AppendedData ...> // Assume input already read the line <AppendedData ...>
template <class Grid, class Creator, class Field> template <class Grid, class Creator, class Field>
std::uint64_t VtkReader<Grid,Creator,Field>::findAppendedDataPosition (std::ifstream& input) const std::uint64_t VtkReader<Grid,Creator,Field>::findAppendedDataPosition (std::ifstream& input) const
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment