forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkArrayToTable.cxx
229 lines (186 loc) · 8.32 KB
/
vtkArrayToTable.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkArrayToTable.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkArrayData.h"
#include "vtkArrayToTable.h"
#include "vtkDenseArray.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkCharArray.h"
#include "vtkShortArray.h"
#include "vtkIntArray.h"
#include "vtkLongArray.h"
#include "vtkLongLongArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedShortArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedLongLongArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkSparseArray.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkUnicodeStringArray.h"
#include <sstream>
#include <stdexcept>
/// Convert a 1D array to a table with one column ...
template<typename ValueT, typename ColumnT>
static bool ConvertVector(vtkArray* Array, vtkTable* Output)
{
if(Array->GetDimensions() != 1)
return false;
vtkTypedArray<ValueT>* const array = vtkTypedArray<ValueT>::SafeDownCast(Array);
if(!array)
return false;
const vtkArrayRange extents = array->GetExtent(0);
ColumnT* const column = ColumnT::New();
column->SetNumberOfTuples(extents.GetSize());
column->SetName(array->GetName());
for(vtkIdType i = extents.GetBegin(); i != extents.GetEnd(); ++i)
{
column->SetValue(i - extents.GetBegin(), array->GetValue(i));
}
Output->AddColumn(column);
column->Delete();
return true;
}
/// Convert a 2D array to a table with 1-or-more columns ...
template<typename ValueT, typename ColumnT>
static bool ConvertMatrix(vtkArray* Array, vtkTable* Output)
{
if(Array->GetDimensions() != 2)
return false;
vtkTypedArray<ValueT>* const array = vtkTypedArray<ValueT>::SafeDownCast(Array);
if(!array)
return false;
vtkSparseArray<ValueT>* const sparse_array = vtkSparseArray<ValueT>::SafeDownCast(array);
const vtkIdType non_null_count = array->GetNonNullSize();
const vtkArrayRange columns = array->GetExtent(1);
const vtkArrayRange rows = array->GetExtent(0);
std::vector<ColumnT*> new_columns;
for(vtkIdType j = columns.GetBegin(); j != columns.GetEnd(); ++j)
{
std::ostringstream column_name;
column_name << j;
ColumnT* const column = ColumnT::New();
column->SetNumberOfTuples(rows.GetSize());
column->SetName(column_name.str().c_str());
if(sparse_array)
{
for(vtkIdType i = 0; i != rows.GetSize(); ++i)
column->SetValue(i, sparse_array->GetNullValue());
}
Output->AddColumn(column);
column->Delete();
new_columns.push_back(column);
}
for(vtkIdType n = 0; n != non_null_count; ++n)
{
vtkArrayCoordinates coordinates;
array->GetCoordinatesN(n, coordinates);
new_columns[coordinates[1] - columns.GetBegin()]->SetValue(coordinates[0] - rows.GetBegin(), array->GetValueN(n));
}
return true;
}
// ----------------------------------------------------------------------
vtkStandardNewMacro(vtkArrayToTable);
// ----------------------------------------------------------------------
vtkArrayToTable::vtkArrayToTable()
{
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
}
// ----------------------------------------------------------------------
vtkArrayToTable::~vtkArrayToTable()
{
}
// ----------------------------------------------------------------------
void vtkArrayToTable::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
int vtkArrayToTable::FillInputPortInformation(int port, vtkInformation* info)
{
switch(port)
{
case 0:
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkArrayData");
return 1;
}
return 0;
}
// ----------------------------------------------------------------------
int vtkArrayToTable::RequestData(
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
try
{
vtkArrayData* const input_array_data = vtkArrayData::GetData(inputVector[0]);
if(!input_array_data)
throw std::runtime_error("Missing vtkArrayData on input port 0.");
if(input_array_data->GetNumberOfArrays() != 1)
throw std::runtime_error("vtkArrayToTable requires a vtkArrayData containing exactly one array.");
vtkArray* const input_array = input_array_data->GetArray(static_cast<vtkIdType>(0));
if(input_array->GetDimensions() > 2)
throw std::runtime_error("vtkArrayToTable input array must have 1 or 2 dimensions.");
vtkTable* const output_table = vtkTable::GetData(outputVector);
if(ConvertVector<double, vtkDoubleArray>(input_array, output_table)) return 1;
if(ConvertVector<unsigned char, vtkUnsignedCharArray>(input_array, output_table)) return 1;
if(ConvertVector<unsigned short, vtkUnsignedShortArray>(input_array, output_table)) return 1;
if(ConvertVector<unsigned int, vtkUnsignedIntArray>(input_array, output_table)) return 1;
if(ConvertVector<unsigned long, vtkUnsignedLongArray>(input_array, output_table)) return 1;
if(ConvertVector<unsigned long long, vtkUnsignedLongLongArray>(input_array, output_table)) return 1;
if(ConvertVector<long unsigned int, vtkUnsignedLongArray>(input_array, output_table)) return 1;
if(ConvertVector<char, vtkCharArray>(input_array, output_table)) return 1;
if(ConvertVector<short, vtkShortArray>(input_array, output_table)) return 1;
if(ConvertVector<long, vtkLongArray>(input_array, output_table)) return 1;
if(ConvertVector<long long, vtkLongLongArray>(input_array, output_table)) return 1;
if(ConvertVector<long int, vtkLongArray>(input_array, output_table)) return 1;
if(ConvertVector<vtkIdType, vtkIdTypeArray>(input_array, output_table)) return 1;
if(ConvertVector<vtkStdString, vtkStringArray>(input_array, output_table)) return 1;
if(ConvertVector<vtkUnicodeString, vtkUnicodeStringArray>(input_array, output_table)) return 1;
if(ConvertMatrix<double, vtkDoubleArray>(input_array, output_table)) return 1;
if(ConvertMatrix<unsigned char, vtkUnsignedCharArray>(input_array, output_table)) return 1;
if(ConvertMatrix<unsigned short, vtkUnsignedShortArray>(input_array, output_table)) return 1;
if(ConvertMatrix<unsigned int, vtkUnsignedIntArray>(input_array, output_table)) return 1;
if(ConvertMatrix<unsigned long, vtkUnsignedLongArray>(input_array, output_table)) return 1;
if(ConvertMatrix<unsigned long long, vtkUnsignedLongLongArray>(input_array, output_table)) return 1;
if(ConvertMatrix<long unsigned int, vtkUnsignedLongArray>(input_array, output_table)) return 1;
if(ConvertMatrix<char, vtkCharArray>(input_array, output_table)) return 1;
if(ConvertMatrix<short, vtkShortArray>(input_array, output_table)) return 1;
if(ConvertMatrix<int, vtkIntArray>(input_array, output_table)) return 1;
if(ConvertMatrix<long, vtkLongArray>(input_array, output_table)) return 1;
if(ConvertMatrix<long long, vtkLongLongArray>(input_array, output_table)) return 1;
if(ConvertMatrix<long int, vtkLongArray>(input_array, output_table)) return 1;
if(ConvertMatrix<vtkIdType, vtkIdTypeArray>(input_array, output_table)) return 1;
if(ConvertMatrix<vtkStdString, vtkStringArray>(input_array, output_table)) return 1;
if(ConvertMatrix<vtkUnicodeString, vtkUnicodeStringArray>(input_array, output_table)) return 1;
throw std::runtime_error("Unhandled input array type.");
}
catch(std::exception& e)
{
vtkErrorMacro(<< "caught exception: " << e.what() << endl);
}
catch(...)
{
vtkErrorMacro(<< "caught unknown exception." << endl);
}
return 0;
}