1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkCellGridIOQuery.h"
#include "vtkCellGrid.h"
#include "vtkDataArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkObjectFactory.h"
#include <vtk_nlohmannjson.h> // For API.
#include VTK_NLOHMANN_JSON(json.hpp) // For API.
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkCellGridIOQuery);
void vtkCellGridIOQuery::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Data: ";
if (this->Data)
{
os << this->Data->dump(2) << "\n";
}
else
{
os << "null\n";
}
}
void vtkCellGridIOQuery::PrepareToDeserialize(const nlohmann::json& sourceData,
const nlohmann::json& attributeData, std::vector<vtkCellAttribute*>& attributeList)
{
this->Data = const_cast<nlohmann::json*>(&sourceData);
this->AttributeData = const_cast<nlohmann::json*>(&attributeData);
this->AttributeList = &attributeList;
this->ArrayLocations.clear();
this->Serializing = false;
}
void vtkCellGridIOQuery::PrepareToSerialize(nlohmann::json& destination,
nlohmann::json& attributeData,
const std::unordered_map<vtkAbstractArray*, vtkStringToken>& arrayLocations)
{
this->Data = &destination;
this->AttributeData = &attributeData;
this->AttributeList = nullptr;
this->ArrayLocations = arrayLocations;
this->Serializing = true;
}
bool vtkCellGridIOQuery::ExtractCellTypeAttributeInfo(vtkCellGrid* grid,
vtkCellAttribute::CellTypeInfo& cellTypeInfo, const nlohmann::json& jsonInfo,
vtkStringToken cellTypeName)
{
if (!cellTypeName.IsValid())
{
return false;
}
auto it = jsonInfo.find(cellTypeName.Data());
if (it == jsonInfo.end())
{
return false;
}
vtkStringToken invalid;
// dof-sharing is not mandatory (and absent for discontinuous attributes):
cellTypeInfo.DOFSharing =
it->contains("dof-sharing") ? (*it)["dof-sharing"].get<std::string>() : invalid;
// function-space, basis, and order are mandatory
cellTypeInfo.FunctionSpace = (*it)["function-space"].get<vtkStringToken>();
cellTypeInfo.Basis = (*it)["basis"].get<vtkStringToken>();
cellTypeInfo.Order = (*it)["order"].get<int>();
if (it->contains("arrays"))
{
nlohmann::json jArrays = it->at("arrays");
for (const auto& arraySpec : jArrays.items())
{
vtkStringToken group(arraySpec.value()[0].get<std::string>());
vtkStringToken arrayName(arraySpec.value()[1].get<std::string>());
// std::cout << cellTypeName.Data() << " " << arraySpec.key() << " has " << group.Data() <<
// ", " << arrayName.Data() << "\n";
auto* arrayGroup = grid->GetAttributes(group.GetId());
if (arrayGroup)
{
auto* array = arrayGroup->GetArray(arrayName.Data().c_str());
if (array)
{
cellTypeInfo.ArraysByRole[arraySpec.key()] = array;
}
else
{
vtkWarningMacro(
"Array \"" << arrayName.Data() << "\" not present in \"" << group.Data() << "\".");
}
}
}
}
return true;
}
bool vtkCellGridIOQuery::InsertCellTypeAttributeInfo(vtkCellGrid* grid,
const vtkCellAttribute::CellTypeInfo& cellTypeInfo, nlohmann::json& jsonInfo,
vtkStringToken cellTypeName)
{
(void)grid;
bool ok = true;
nlohmann::json jCellTypeInfo;
nlohmann::json jCellBlock;
nlohmann::json arraysByRole;
for (const auto& entry : cellTypeInfo.ArraysByRole)
{
auto groupIt = this->ArrayLocations.find(entry.second);
if (groupIt == this->ArrayLocations.end())
{
ok = false;
vtkWarningMacro(
"Unmanaged array " << entry.second << " in role " << entry.first.Data() << " skipped.");
continue;
}
arraysByRole[entry.first.Data()] =
nlohmann::json::array({ groupIt->second.Data(), entry.second->GetName() });
}
jCellBlock = { { "function-space", cellTypeInfo.FunctionSpace.Data() },
{ "basis", cellTypeInfo.Basis.Data() }, { "order", cellTypeInfo.Order } };
if (!arraysByRole.empty())
{
jCellBlock["arrays"] = arraysByRole;
}
if (cellTypeInfo.DOFSharing.IsValid())
{
jCellBlock["dof-sharing"] = cellTypeInfo.DOFSharing.Data();
}
jCellTypeInfo[cellTypeName.Data()] = jCellBlock;
jsonInfo["cell-info"] = jCellTypeInfo;
return ok;
}
nlohmann::json& vtkCellGridIOQuery::AddMetadataEntry(vtkStringToken cellTypeName)
{
nlohmann::json entry;
entry["type"] = cellTypeName.Data();
this->Data->push_back(entry);
return this->Data->back();
}
VTK_ABI_NAMESPACE_END
|