File: vtkAlgorithmSerDesHelper.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,916 kB
  • sloc: cpp: 2,336,565; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 178; javascript: 165; objc: 153; tcl: 59
file content (156 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download
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
150
151
152
153
154
155
156
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkAlgorithm.h"
#include "vtkDataObject.h"
#include "vtkDeserializer.h"
#include "vtkInformation.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 vtkAlgorithm
   * @param ser   a vtkSerializer instance
   * @param deser a vtkDeserializer instance
   */
  int RegisterHandlers_vtkAlgorithmSerDesHelper(void* ser, void* deser, void* invoker);
}

static nlohmann::json Serialize_vtkAlgorithm(vtkObjectBase* object, vtkSerializer* serializer)
{
  using json = nlohmann::json;
  if (auto* algorithm = vtkAlgorithm::SafeDownCast(object))
  {
    json state;
    if (auto superSerializer = serializer->GetHandler(typeid(vtkAlgorithm::Superclass)))
    {
      state = superSerializer(object, serializer);
    }
    state["SuperClassNames"].push_back("vtkObject");
    if (algorithm->GetNumberOfOutputPorts() > 0)
    {
      if (auto outputDataObject = algorithm->GetOutputDataObject(0))
      {
        state["OutputDataObject"] =
          serializer->SerializeJSON(reinterpret_cast<vtkObjectBase*>(outputDataObject));
      }
    }
    state["AbortExecute"] = algorithm->GetAbortExecute();
    // the pipeline is servered here by capturing only the input data objects
    // in the state.
    auto& statesOfInputDataObjects = state["InputDataObjects"] = json::array();
    for (int port = 0; port < algorithm->GetNumberOfInputPorts(); ++port)
    {
      auto stateOfInputDataObjects = json::array();
      for (int index = 0; index < algorithm->GetNumberOfInputConnections(port); ++index)
      {
        auto* inputAlgorithm = algorithm->GetInputAlgorithm(port, index);
        inputAlgorithm->Update();
        auto* inputDataObject = algorithm->GetInputDataObject(port, index);
        stateOfInputDataObjects.push_back(serializer->SerializeJSON(inputDataObject));
      }
      statesOfInputDataObjects.push_back(stateOfInputDataObjects);
    }

    state["Information"] =
      serializer->SerializeJSON(reinterpret_cast<vtkObjectBase*>(algorithm->GetInformation()));

    return state;
  }
  else
  {
    return {};
  }
}

static void Deserialize_vtkAlgorithm(
  const nlohmann::json& state, vtkObjectBase* object, vtkDeserializer* deserializer)
{
  using json = nlohmann::json;
  auto* algorithm = vtkAlgorithm::SafeDownCast(object);
  if (!algorithm)
  {
    return;
  }
  VTK_DESERIALIZE_VALUE_FROM_STATE(AbortExecute, int, state, algorithm);

  VTK_DESERIALIZE_VTK_OBJECT_FROM_STATE(
    Information, vtkInformation, state, algorithm, deserializer);

  {
    const auto* context = deserializer->GetContext();
    const auto iter = state.find("InputDataObjects");
    if ((iter != state.end()) && !iter->is_null())
    {
      auto statesOfInputDataObjects = iter->get<json::array_t>();
      if (algorithm->GetNumberOfInputPorts() != static_cast<int>(statesOfInputDataObjects.size()))
      {
        vtkWarningWithObjectMacro(context,
          << deserializer->GetObjectDescription()
          << " failed because number of input ports in state (" << statesOfInputDataObjects.size()
          << ") does not match for algorithm=" << algorithm->GetObjectDescription() << " ("
          << algorithm->GetNumberOfInputPorts() << ")");
        return;
      }
      for (int port = 0; port < algorithm->GetNumberOfInputPorts(); ++port)
      {
        std::vector<vtkSmartPointer<vtkDataObject>> inputDataObjects;
        auto stateOfInputDataObjects = statesOfInputDataObjects[port].get<json::array_t>();
        const bool hasMultipleConnections = stateOfInputDataObjects.size() > 1;
        for (std::size_t index = 0; index < stateOfInputDataObjects.size(); ++index)
        {
          const auto identifier = stateOfInputDataObjects[index]["Id"].get<vtkTypeUInt32>();
          auto subObject = context->GetObjectAtId(identifier);
          deserializer->DeserializeJSON(identifier, subObject);
          if (auto* dataObject = vtkDataObject::SafeDownCast(subObject))
          {
            if (hasMultipleConnections)
            {
              inputDataObjects.emplace_back(dataObject);
            }
            else
            {
              algorithm->SetInputDataObject(port, dataObject);
            }
          }
        }
        if (hasMultipleConnections)
        {
          algorithm->RemoveAllInputConnections(port);
          for (auto& dataObject : inputDataObjects)
          {
            algorithm->AddInputDataObject(port, dataObject);
          }
        }
      }
    }
  }
}

int RegisterHandlers_vtkAlgorithmSerDesHelper(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(vtkAlgorithm), Serialize_vtkAlgorithm);
      success = 1;
    }
  }
  if (auto* asObjectBase = static_cast<vtkObjectBase*>(deser))
  {
    if (auto* deserializer = vtkDeserializer::SafeDownCast(asObjectBase))
    {
      deserializer->RegisterHandler(typeid(vtkAlgorithm), Deserialize_vtkAlgorithm);
      deserializer->RegisterConstructor("vtkAlgorithm", vtkAlgorithm::New);
      success = 1;
    }
  }
  return success;
}