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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkDeserializer.h"
#include "vtkFieldData.h"
#include "vtkObjectBase.h"
#include "vtkSerializer.h"
// clang-format off
#include "vtk_nlohmannjson.h"
#include VTK_NLOHMANN_JSON(json.hpp)
// clang-format on
extern "C"
{
/**
* Register the (de)serialization handlers of vtkFieldData
* @param ser a vtkSerializer instance
* @param deser a vtkDeserializer instance
*/
int RegisterHandlers_vtkFieldDataSerDesHelper(void* ser, void* deser, void* invoker);
}
VTK_ABI_NAMESPACE_BEGIN
class VTKCOMMONDATAMODEL_NO_EXPORT vtkFieldDataSerDesHelper
{
public:
//----------------------------------------------------------------------------
static nlohmann::json Serialize_vtkFieldData(vtkObjectBase* object, vtkSerializer* serializer)
{
using nlohmann::json;
if (auto* fd = vtkFieldData::SafeDownCast(object))
{
json state;
if (auto superSerializer = serializer->GetHandler(typeid(vtkFieldData::Superclass)))
{
state = superSerializer(object, serializer);
}
state["NumberOfArrays"] = fd->GetNumberOfArrays();
auto& dst = state["Arrays"] = json::array();
for (int i = 0; i < fd->GetNumberOfArrays(); ++i)
{
dst.push_back(serializer->SerializeJSON(fd->Data[i]));
}
state["GhostsToSkip"] = fd->GetGhostsToSkip();
state["NumberOfTuples"] = fd->GetNumberOfTuples();
return state;
}
else
{
return {};
}
}
//----------------------------------------------------------------------------
static void Deserialize_vtkFieldData(
const nlohmann::json& state, vtkObjectBase* object, vtkDeserializer* deserializer)
{
using nlohmann::json;
if (auto* fd = vtkFieldData::SafeDownCast(object))
{
if (auto superDeserializer = deserializer->GetHandler(typeid(vtkFieldData::Superclass)))
{
superDeserializer(state, object, deserializer);
}
auto* context = deserializer->GetContext();
const auto& stateOfArrays = state["Arrays"];
// vector used to keep existing arrays alive so that fd->RemoveArray doesn't destroy the
// vtkAbstractArray object.
std::vector<vtkSmartPointer<vtkAbstractArray>> arrays;
for (auto& stateOfarray : stateOfArrays)
{
const auto identifier = stateOfarray["Id"].get<vtkTypeUInt32>();
auto subObject = context->GetObjectAtId(identifier);
deserializer->DeserializeJSON(identifier, subObject);
if (auto* array = vtkAbstractArray::SafeDownCast(subObject))
{
arrays.emplace_back(array);
}
}
// Now remove arrays from the collection.
// If arrays already existed before entering this function, it does not invoke
// destructor on the vtkAbstractArray because a reference is held by the vector of arrays.
if (static_cast<std::size_t>(fd->GetNumberOfArrays()) != arrays.size())
{
while (fd->GetNumberOfArrays() > 0)
{
auto* array = fd->GetAbstractArray(0);
context->UnRegisterObject(context->GetId(array));
fd->RemoveArray(0);
}
for (const auto& array : arrays)
{
fd->AddArray(array);
}
}
else
{
int i = 0;
for (const auto& array : arrays)
{
// vtkFieldData::SetArray only marks the vtkFieldData as modified if the array is
// different from the one already present in the vtkFieldData. This is important because
// the vtkFieldData::MTime affects the MTime of a vtkPolyData object. We need to be very
// careful here because unnecessary modification of the vtkFieldData::MTime will cause the
// vtkPolyData to be marked as modified and, in turn, will force a mapper to upload the
// data again.
fd->SetArray(i, array);
++i;
}
}
VTK_DESERIALIZE_VALUE_FROM_STATE(NumberOfTuples, int, state, fd);
VTK_DESERIALIZE_VALUE_FROM_STATE(GhostsToSkip, int, state, fd);
}
}
};
VTK_ABI_NAMESPACE_END
int RegisterHandlers_vtkFieldDataSerDesHelper(void* ser, void* deser, void* vtkNotUsed(invoker))
{
int success = 0;
if (auto* asObjectBase = static_cast<vtkObjectBase*>(ser))
{
if (auto* serializer = vtkSerializer::SafeDownCast(asObjectBase))
{
serializer->RegisterHandler(
typeid(vtkFieldData), vtkFieldDataSerDesHelper::Serialize_vtkFieldData);
success = 1;
}
}
if (auto* asObjectBase = static_cast<vtkObjectBase*>(deser))
{
if (auto* deserializer = vtkDeserializer::SafeDownCast(asObjectBase))
{
deserializer->RegisterHandler(
typeid(vtkFieldData), vtkFieldDataSerDesHelper::Deserialize_vtkFieldData);
deserializer->RegisterConstructor("vtkFieldData", vtkFieldData::New);
success = 1;
}
}
return success;
}
|