File: vtkDGIOResponder.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (309 lines) | stat: -rw-r--r-- 8,886 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkDGIOResponder.h"

#include "vtkCellGrid.h"
#include "vtkDGCell.h"
#include "vtkDataArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkObjectFactory.h"

#include <sstream>

VTK_ABI_NAMESPACE_BEGIN

namespace
{

bool PutArray(vtkDataArray* array, const std::string& arrayRole, nlohmann::json& jSpec,
  const std::unordered_map<vtkAbstractArray*, vtkStringToken>& arrayLocations)
{
  std::string arrayGroup;
  std::string arrayName;
  auto it = arrayLocations.find(array);
  if (it != arrayLocations.end())
  {
    if (!it->second.Data().empty())
    {
      arrayGroup = it->second.Data();
    }
    else
    {
      std::ostringstream groupId;
      groupId << it->second.Data();
      arrayGroup = groupId.str();
    }
  }
  arrayName = array ? array->GetName() : "";
  if (!arrayGroup.empty() && !arrayName.empty())
  {
    jSpec[arrayRole] = nlohmann::json::array({ arrayGroup, arrayName });
  }
  else
  {
    vtkGenericWarningMacro("No array group contains array " << array << " or array is null.");
    return false;
  }
  return true;
}

bool FetchArray(vtkDataArray*& array, const std::string& arrayName, const nlohmann::json& jSpec,
  vtkCellGrid* grid, bool mandatory = true)
{
  auto jArray = jSpec.find(arrayName);
  if (jArray == jSpec.end() || !jArray->is_array() || jArray->size() != 2)
  {
    if (mandatory)
    {
      vtkGenericWarningMacro("Specification has missing or malformed connectivity.");
    }
    return false;
  }
  else
  {
    auto arrayGroupName = (*jArray)[0].get<std::string>();
    auto jArrayName = (*jArray)[1].get<std::string>();
    auto* arrayGroup = grid->FindAttributes(vtkStringToken(arrayGroupName));
    if (arrayGroup)
    {
      array = arrayGroup->GetArray(jArrayName.c_str());
    }
    else
    {
      vtkGenericWarningMacro(
        "Connectivity \"" << arrayGroupName << "\", \"" << jArrayName << "\" not found.");
      return false;
    }
  }
  return true;
}

nlohmann::json CellSourceToJSON(const vtkDGCell::Source& spec,
  const std::unordered_map<vtkAbstractArray*, vtkStringToken>& arrayLocations)
{
  nlohmann::json result;
  PutArray(spec.Connectivity, "connectivity", result, arrayLocations);
  if (spec.NodalGhostMarks)
  {
    PutArray(spec.NodalGhostMarks, "ghost-node", result, arrayLocations);
  }

  result["shape"] = vtkDGCell::GetShapeName(spec.SourceShape).Data();
  if (spec.Offset != 0)
  {
    result["offset"] = spec.Offset;
  }
  if (spec.Blanked)
  {
    result["blanked"] = true;
  }
  if (spec.SideType >= 0)
  {
    result["side-type"] = spec.SideType;
  }
  if (spec.SelectionType != -1)
  {
    result["selection-type"] = spec.SelectionType;
  }
  return result;
}

vtkDGCell::Source JSONToCellSource(const nlohmann::json& jSpec, vtkCellGrid* grid)
{
  vtkDGCell::Source result;
  FetchArray(result.Connectivity, "connectivity", jSpec, grid);
  FetchArray(result.NodalGhostMarks, "ghost-node", jSpec, grid, /*mandatory*/ false);

  auto jShape = jSpec.find("shape");
  if (jShape != jSpec.end())
  {
    auto shapeName = jShape->get<std::string>();
    result.SourceShape = vtkDGCell::GetShapeEnum(shapeName);
  }

  auto jOffset = jSpec.find("offset");
  if (jOffset != jSpec.end())
  {
    result.Offset = jOffset->get<vtkIdType>();
  }

  auto jBlank = jSpec.find("blanked");
  if (jBlank != jSpec.end())
  {
    result.Blanked = jBlank->get<bool>();
  }

  auto jSideType = jSpec.find("side-type");
  if (jSideType != jSpec.end())
  {
    result.SideType = jSideType->get<int>();
  }

  auto jSelnType = jSpec.find("selection-type");
  if (jSelnType != jSpec.end())
  {
    result.SelectionType = jSelnType->get<int>();
  }

  return result;
}

} // anonymous namespace

vtkStandardNewMacro(vtkDGIOResponder);

void vtkDGIOResponder::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

bool vtkDGIOResponder::Query(
  vtkCellGridIOQuery* query, vtkCellMetadata* cellType, vtkCellGridResponders* caches)
{
  (void)caches;
  auto* dgCell = vtkDGCell::SafeDownCast(cellType);
  if (!query || !query->GetData())
  {
    return false;
  }
  if (!dgCell)
  {
    // Allow this responder to "work" as a default for any cell type
    // by doing nothing for non-DG cells.
    // Do not register this responder with non-DG cell types if you
    // have implemented a different, non-trivial responder.
    return true;
  }

  vtkStringToken cellTypeName = dgCell->GetClassName();
  auto* grid = dgCell->GetCellGrid();
  nlohmann::json& jj(*query->GetData());
  auto& cellSpec = dgCell->GetCellSpec();
  auto& sideSpecs = dgCell->GetSideSpecs();

  bool ok = true;
  // Serialize or deserialize as requested:
  if (query->IsSerializing())
  {
    // Build a lookup table of arrays so we can find the group they belong to.
    std::unordered_map<vtkAbstractArray*, vtkStringToken> arrayLocations;
    grid->MapArrayLocations(arrayLocations);
    auto& jCellType = query->AddMetadataEntry(cellTypeName);
    // Convert CellSpec and SideSpecs to JSON
    jCellType["cell-spec"] = CellSourceToJSON(cellSpec, arrayLocations);
    if (!sideSpecs.empty())
    {
      auto& jSideSpecs = jCellType["side-specs"];
      for (const auto& sideSpec : sideSpecs)
      {
        jSideSpecs.push_back(CellSourceToJSON(sideSpec, arrayLocations));
      }
    }
    // Add CellTypeInfo for each vtkCellAttribute.
    // We assume that the order of attributes in GetCellAttributeList()
    // is identical to the order of attributes in the JSON array.
    auto& jAttributeList = *query->GetAttributeData();
    auto ait = jAttributeList.begin();
    for (const auto& cellAttId : grid->GetCellAttributeIds())
    {
      auto attribute = grid->GetCellAttributeById(cellAttId);
      if (!attribute)
      {
        continue;
      }
      auto cellTypeInfo = attribute->GetCellTypeInfo(cellTypeName);
      query->InsertCellTypeAttributeInfo(grid, cellTypeInfo, *ait, cellTypeName);
      ++ait;

      // ait and attribute must match since jAttributeList was created by
      // iterating over grid->GetCellAttributeIds().
    }
    ok = true;
  }
  else
  {
    bool didAdd = false;
    // Read CellSpec and SideSpecs from JSON
    for (const auto& jCellType : jj)
    {
      if (!jCellType.contains("type") || jCellType["type"] != dgCell->GetClassName())
      {
        continue;
      }
      cellSpec = JSONToCellSource(jCellType["cell-spec"], grid);
      // Force the class and cell shape to correspond (only for cellSpec,
      // not for sideSpecs).
      cellSpec.SourceShape = dgCell->GetShape();
      auto jit = jCellType.find("side-specs");
      if (jit != jCellType.end())
      {
        for (const auto& spec : *jit)
        {
          sideSpecs.push_back(JSONToCellSource(spec, grid));
        }
      }
      didAdd = true;
      break; // Only one entry per cell type should exist.
    }
    // Add CellTypeInfo to vtkCellAttribute instances.
    auto* jAttributes = query->GetAttributeData();
    if (!jAttributes)
    {
      ok = false;
    }
    else
    {
      auto jit = jAttributes->begin();
      for (auto* attribute : query->GetAttributeList())
      {
#if 0
        auto cit = jit->find(dgCell->GetClassName());
        if (cit == jit->end())
        {
          continue;
        }
        auto iit = cit->find("cell-info");
        if (iit == cit->end())
        {
          vtkWarningMacro(
            "No cell-info for \"" << cellTypeName.Data() << "\" cells "
            "of \"" << attribute->GetName().Data() << "\" attribute.");
          ok = false;
          continue;
        }
#else
        auto iit = jit->find("cell-info");
        if (iit == jit->end())
        {
          vtkWarningMacro("No cell-info for \"" << cellTypeName.Data()
                                                << "\" cells "
                                                   "of \""
                                                << attribute->GetName().Data() << "\" attribute.");
          ok = false;
          continue;
        }
#endif
        vtkCellAttribute::CellTypeInfo cellTypeInfo;
        if (query->ExtractCellTypeAttributeInfo(grid, cellTypeInfo, *iit, cellTypeName))
        {
          attribute->SetCellTypeInfo(cellTypeName, cellTypeInfo);
        }
        else
        {
          vtkWarningMacro("Could not fetch cell-info for \"" << cellTypeName.Data()
                                                             << "\" cells "
                                                                "of \""
                                                             << attribute->GetName().Data()
                                                             << "\" attribute.");
          ok = false;
        }
        ++jit;
      }
    }
    ok &= didAdd;
  }

  return ok;
}

VTK_ABI_NAMESPACE_END