File: vtkDataSetAttributesSerDesHelper.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; 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: 181; javascript: 165; objc: 153; tcl: 59
file content (102 lines) | stat: -rw-r--r-- 3,395 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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkDataSetAttributes.h"
#include "vtkDataSetAttributesFieldList.h"
#include "vtkDeserializer.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 vtkDataSetAttributes
   * @param ser   a vtkSerializer instance
   * @param deser a vtkDeserializer instance
   */
  int RegisterHandlers_vtkDataSetAttributesSerDesHelper(void* ser, void* deser, void* invoker);
}

static nlohmann::json Serialize_vtkDataSetAttributes(
  vtkObjectBase* object, vtkSerializer* serializer)
{
  using nlohmann::json;
  if (auto* dsa = vtkDataSetAttributes::SafeDownCast(object))
  {
    json state;
    if (auto superSerializer = serializer->GetHandler(typeid(vtkDataSetAttributes::Superclass)))
    {
      state = superSerializer(object, serializer);
    }
    std::vector<int> attrIndices(vtkDataSetAttributes::NUM_ATTRIBUTES, -1);
    dsa->GetAttributeIndices(attrIndices.data());
    state["AttributeIndices"] = attrIndices;
    return state;
  }
  else
  {
    return {};
  }
}

static void Deserialize_vtkDataSetAttributes(
  const nlohmann::json& state, vtkObjectBase* object, vtkDeserializer* deserializer)
{
  using nlohmann::json;
  if (auto* dsa = vtkDataSetAttributes::SafeDownCast(object))
  {
    if (auto superDeserializer = deserializer->GetHandler(typeid(vtkDataSetAttributes::Superclass)))
    {
      superDeserializer(state, object, deserializer);
    }
    const auto& attributeIndices = state["AttributeIndices"];
    if (attributeIndices.size() != vtkDataSetAttributes::NUM_ATTRIBUTES)
    {
      vtkWarningWithObjectMacro(deserializer,
        << "Failed to deserialize active attribute types in the dataset attributes object. "
           "The number of attribute indices in state is not "
           "equal to vtkDataSetAttributes::NUM_ATTRIBUTES("
        << vtkDataSetAttributes::NUM_ATTRIBUTES << ")!");
      return;
    }

    std::vector<int> existingAttributeIndices(vtkDataSetAttributes::NUM_ATTRIBUTES, -1);
    dsa->GetAttributeIndices(existingAttributeIndices.data());
    for (int attributeType = 0; attributeType < vtkDataSetAttributes::NUM_ATTRIBUTES;
         ++attributeType)
    {
      if (existingAttributeIndices[attributeType] != attributeIndices[attributeType])
      {
        dsa->SetActiveAttribute(attributeIndices[attributeType], attributeType);
      }
    }
  }
}

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