File: vtkArrayWriter.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 (392 lines) | stat: -rw-r--r-- 11,646 bytes parent folder | download | duplicates (2)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkArrayWriter.h"
#include "vtkArrayData.h"
#include "vtkArrayPrint.h"
#include "vtkDenseArray.h"
#include "vtkExecutive.h"
#include "vtkInformation.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkSparseArray.h"
#include "vtksys/FStream.hxx"

#include <cmath>
#include <limits>
#include <sstream>
#include <stdexcept>

VTK_ABI_NAMESPACE_BEGIN
namespace
{

template <typename T>
inline void WriteValue(std::ostream& stream, const T& value)
{
  stream << value;
}

inline void WriteValue(std::ostream& stream, const double& value)
{
  if (std::abs(value) < std::numeric_limits<double>::min())
    stream << 0;
  else
    stream << value;
}

void WriteHeader(const std::string& array_type, const std::string& type_name, vtkArray* array,
  ostream& stream, bool WriteBinary)
{
  // Serialize the array type ...
  stream << array_type << " " << type_name << "\n";

  // Serialize output format, binary or ascii
  WriteBinary ? stream << "binary"
                       << "\n"
              : stream << "ascii"
                       << "\n";

  const vtkArrayExtents extents = array->GetExtents();
  const vtkIdType dimensions = array->GetDimensions();

  // Serialize the array name ...
  stream << array->GetName() << "\n";

  // Serialize the array extents and number of non-nullptr values ...
  for (vtkIdType i = 0; i != dimensions; ++i)
    stream << extents[i].GetBegin() << " " << extents[i].GetEnd() << " ";
  stream << array->GetNonNullSize() << "\n";

  // Serialize the dimension-label for each dimension ...
  for (vtkIdType i = 0; i != dimensions; ++i)
    stream << array->GetDimensionLabel(i) << "\n";
}

void WriteEndianOrderMark(ostream& stream)
{
  // Serialize an endian-order mark ...
  const vtkTypeUInt32 endian_order = 0x12345678;
  stream.write(reinterpret_cast<const char*>(&endian_order), sizeof(endian_order));
}

template <typename ValueT>
bool WriteSparseArrayBinary(const std::string& type_name, vtkArray* array, ostream& stream)
{
  vtkSparseArray<ValueT>* const concrete_array = vtkSparseArray<ValueT>::SafeDownCast(array);
  if (!concrete_array)
    return false;

  // Write the array header ...
  WriteHeader("vtk-sparse-array", type_name, array, stream, true);
  WriteEndianOrderMark(stream);

  // Serialize the array nullptr value ...
  stream.write(reinterpret_cast<const char*>(&concrete_array->GetNullValue()), sizeof(ValueT));

  // Serialize the array coordinates ...
  for (vtkIdType i = 0; i != array->GetDimensions(); ++i)
  {
    stream.write(reinterpret_cast<char*>(concrete_array->GetCoordinateStorage(i)),
      concrete_array->GetNonNullSize() * sizeof(vtkIdType));
  }

  // Serialize the array values ...
  stream.write(reinterpret_cast<char*>(concrete_array->GetValueStorage()),
    concrete_array->GetNonNullSize() * sizeof(ValueT));

  return true;
}

template <>
bool WriteSparseArrayBinary<vtkStdString>(
  const std::string& type_name, vtkArray* array, ostream& stream)
{
  vtkSparseArray<vtkStdString>* const concrete_array =
    vtkSparseArray<vtkStdString>::SafeDownCast(array);
  if (!concrete_array)
    return false;

  // Write the array header ...
  WriteHeader("vtk-sparse-array", type_name, array, stream, true);
  WriteEndianOrderMark(stream);

  // Serialize the array nullptr value ...
  stream.write(concrete_array->GetNullValue().c_str(), concrete_array->GetNullValue().size() + 1);

  // Serialize the array coordinates ...
  for (vtkIdType i = 0; i != array->GetDimensions(); ++i)
  {
    stream.write(reinterpret_cast<char*>(concrete_array->GetCoordinateStorage(i)),
      concrete_array->GetNonNullSize() * sizeof(vtkIdType));
  }

  // Serialize the array values as a set of packed, nullptr-terminated strings ...
  const vtkIdType value_count = array->GetNonNullSize();
  for (vtkIdType n = 0; n != value_count; ++n)
  {
    const std::string& value = concrete_array->GetValueN(n);

    stream.write(value.c_str(), value.size() + 1);
  }

  return true;
}

template <typename ValueT>
bool WriteDenseArrayBinary(const std::string& type_name, vtkArray* array, ostream& stream)
{
  vtkDenseArray<ValueT>* const concrete_array = vtkDenseArray<ValueT>::SafeDownCast(array);
  if (!concrete_array)
    return false;

  // Write the array header ...
  WriteHeader("vtk-dense-array", type_name, array, stream, true);
  WriteEndianOrderMark(stream);

  // Serialize the array values ...
  stream.write(reinterpret_cast<char*>(concrete_array->GetStorage()),
    concrete_array->GetNonNullSize() * sizeof(ValueT));

  return true;
}

template <>
bool WriteDenseArrayBinary<vtkStdString>(
  const std::string& type_name, vtkArray* array, ostream& stream)
{
  vtkDenseArray<vtkStdString>* const concrete_array =
    vtkDenseArray<vtkStdString>::SafeDownCast(array);
  if (!concrete_array)
    return false;

  // Write the array header ...
  WriteHeader("vtk-dense-array", type_name, array, stream, true);
  WriteEndianOrderMark(stream);

  // Serialize the array values as a set of packed, nullptr-terminated strings ...
  const vtkIdType value_count = array->GetNonNullSize();
  for (vtkIdType n = 0; n != value_count; ++n)
  {
    const std::string& value = concrete_array->GetValueN(n);

    stream.write(value.c_str(), value.size() + 1);
  }

  return true;
}

template <typename ValueT>
bool WriteSparseArrayAscii(const std::string& type_name, vtkArray* array, ostream& stream)
{
  vtkSparseArray<ValueT>* const concrete_array = vtkSparseArray<ValueT>::SafeDownCast(array);
  if (!concrete_array)
    return false;

  // Write the header ...
  WriteHeader("vtk-sparse-array", type_name, array, stream, false);

  // Ensure that floating-point types are serialized with full precision
  if (std::numeric_limits<ValueT>::is_specialized)
    stream.precision(std::numeric_limits<ValueT>::digits10 + 1);

  // Write the array nullptr value ...
  WriteValue(stream, concrete_array->GetNullValue());
  stream << "\n";

  // Write the array contents ...
  const vtkIdType dimensions = array->GetDimensions();
  const vtkIdType non_null_size = array->GetNonNullSize();

  vtkArrayCoordinates coordinates;
  for (vtkIdType n = 0; n != non_null_size; ++n)
  {
    array->GetCoordinatesN(n, coordinates);
    for (vtkIdType i = 0; i != dimensions; ++i)
      stream << coordinates[i] << " ";
    WriteValue(stream, concrete_array->GetValueN(n));
    stream << "\n";
  }

  return true;
}

template <typename ValueT>
bool WriteDenseArrayAscii(const std::string& type_name, vtkArray* array, ostream& stream)
{
  vtkDenseArray<ValueT>* const concrete_array = vtkDenseArray<ValueT>::SafeDownCast(array);
  if (!concrete_array)
    return false;

  // Write the header ...
  WriteHeader("vtk-dense-array", type_name, array, stream, false);

  // Write the array contents ...
  const vtkArrayExtents extents = array->GetExtents();

  // Ensure that floating-point types are serialized with full precision
  if (std::numeric_limits<ValueT>::is_specialized)
    stream.precision(std::numeric_limits<ValueT>::digits10 + 1);

  vtkArrayCoordinates coordinates;
  for (vtkArrayExtents::SizeT n = 0; n != extents.GetSize(); ++n)
  {
    extents.GetRightToLeftCoordinatesN(n, coordinates);
    WriteValue(stream, concrete_array->GetValue(coordinates));
    stream << "\n";
  }

  return true;
}

} // End anonymous namespace

vtkStandardNewMacro(vtkArrayWriter);

vtkArrayWriter::vtkArrayWriter()
  : FileName(nullptr)
  , Binary(false)
  , WriteToOutputString(false)
{
}

vtkArrayWriter::~vtkArrayWriter()
{
  this->SetFileName(nullptr);
}

void vtkArrayWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "FileName: " << (this->FileName ? this->FileName : "(none)") << endl;
  os << indent << "Binary: " << this->Binary << endl;
  os << indent << "WriteToOutputString: " << (this->WriteToOutputString ? "on" : "off") << endl;
  os << indent << "OutputString: " << this->OutputString << endl;
}

int vtkArrayWriter::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkArrayData");
  return 1;
}

void vtkArrayWriter::WriteData()
{
  if (this->WriteToOutputString)
  {
    this->OutputString = this->Write(this->Binary > 0);
  }
  else
  {
    this->Write(this->FileName ? this->FileName : "", this->Binary > 0);
  }
}

int vtkArrayWriter::Write()
{
  return Superclass::Write();
}

bool vtkArrayWriter::Write(const vtkStdString& file_name, bool WriteBinary)
{
  vtksys::ofstream file(file_name.c_str(), std::ios::binary);
  return this->Write(file, WriteBinary);
}

bool vtkArrayWriter::Write(vtkArray* array, const vtkStdString& file_name, bool WriteBinary)
{
  vtksys::ofstream file(file_name.c_str(), std::ios::binary);
  return vtkArrayWriter::Write(array, file, WriteBinary);
}

bool vtkArrayWriter::Write(ostream& stream, bool WriteBinary)
{
  try
  {
    if (this->GetNumberOfInputConnections(0) != 1)
      throw std::runtime_error("Exactly one input required.");

    vtkArrayData* const array_data =
      vtkArrayData::SafeDownCast(this->GetExecutive()->GetInputData(0, 0));
    if (!array_data)
      throw std::runtime_error("vtkArrayData input required.");

    if (array_data->GetNumberOfArrays() != 1)
      throw std::runtime_error("vtkArrayData with exactly one array required.");

    vtkArray* const array = array_data->GetArray(static_cast<vtkIdType>(0));
    if (!array)
      throw std::runtime_error("Cannot serialize nullptr vtkArray.");

    return vtkArrayWriter::Write(array, stream, WriteBinary);
  }
  catch (std::exception& e)
  {
    vtkErrorMacro("caught exception: " << e.what());
  }
  return false;
}

bool vtkArrayWriter::Write(vtkArray* array, ostream& stream, bool WriteBinary)
{
  try
  {
    if (!array)
      throw std::runtime_error("Cannot serialize nullptr vtkArray.");

    if (WriteBinary)
    {
      if (WriteSparseArrayBinary<vtkIdType>("integer", array, stream))
        return true;
      if (WriteSparseArrayBinary<double>("double", array, stream))
        return true;
      if (WriteSparseArrayBinary<vtkStdString>("string", array, stream))
        return true;

      if (WriteDenseArrayBinary<vtkIdType>("integer", array, stream))
        return true;
      if (WriteDenseArrayBinary<double>("double", array, stream))
        return true;
      if (WriteDenseArrayBinary<vtkStdString>("string", array, stream))
        return true;
    }
    else
    {
      if (WriteSparseArrayAscii<vtkIdType>("integer", array, stream))
        return true;
      if (WriteSparseArrayAscii<double>("double", array, stream))
        return true;
      if (WriteSparseArrayAscii<vtkStdString>("string", array, stream))
        return true;

      if (WriteDenseArrayAscii<vtkIdType>("integer", array, stream))
        return true;
      if (WriteDenseArrayAscii<double>("double", array, stream))
        return true;
      if (WriteDenseArrayAscii<vtkStdString>("string", array, stream))
        return true;
    }

    throw std::runtime_error(std::string("Unhandled array type: ") + array->GetClassName());
  }
  catch (std::exception& e)
  {
    vtkGenericWarningMacro("caught exception: " << e.what());
  }
  return false;
}

vtkStdString vtkArrayWriter::Write(bool WriteBinary)
{
  std::ostringstream oss;
  this->Write(oss, WriteBinary);
  return oss.str();
}

vtkStdString vtkArrayWriter::Write(vtkArray* array, bool WriteBinary)
{
  std::ostringstream oss;
  vtkArrayWriter::Write(array, oss, WriteBinary);
  return oss.str();
}
VTK_ABI_NAMESPACE_END