forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkStringToNumeric.cxx
352 lines (321 loc) · 10 KB
/
vtkStringToNumeric.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
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStringToNumeric.cxx
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.
=========================================================================*/
/*-------------------------------------------------------------------------
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.
-------------------------------------------------------------------------*/
#include "vtkStringToNumeric.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkDemandDrivenPipeline.h"
#include "vtkFieldData.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
#include "vtkUnicodeStringArray.h"
#include "vtkVariant.h"
vtkStandardNewMacro(vtkStringToNumeric);
vtkStringToNumeric::vtkStringToNumeric()
{
this->ConvertFieldData = true;
this->ConvertPointData = true;
this->ConvertCellData = true;
this->ForceDouble = false;
this->DefaultIntegerValue = 0;
this->DefaultDoubleValue = 0.0;
this->TrimWhitespacePriorToNumericConversion = false;
}
vtkStringToNumeric::~vtkStringToNumeric()
{
}
int vtkStringToNumeric::CountItemsToConvert(vtkFieldData *fieldData)
{
int count = 0;
for (int arr = 0; arr < fieldData->GetNumberOfArrays(); arr++)
{
vtkAbstractArray *array = fieldData->GetAbstractArray(arr);
vtkStringArray* stringArray = vtkArrayDownCast<vtkStringArray>(array);
vtkUnicodeStringArray* unicodeArray =
vtkArrayDownCast<vtkUnicodeStringArray>(array);
if (!stringArray && !unicodeArray)
{
continue;
}
else
{
count += array->GetNumberOfTuples() * array->GetNumberOfComponents();
}
}
return count;
}
int vtkStringToNumeric::RequestData(
vtkInformation*,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// Get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// Get the input and output objects
vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject* output = outInfo->Get(vtkDataObject::DATA_OBJECT());
output->ShallowCopy(input);
vtkDataSet* outputDataSet = vtkDataSet::SafeDownCast(output);
vtkGraph* outputGraph = vtkGraph::SafeDownCast(output);
vtkTable* outputTable = vtkTable::SafeDownCast(output);
// Figure out how many items we have to process
int itemCount = 0;
if (this->ConvertFieldData)
{
itemCount += this->CountItemsToConvert(output->GetFieldData());
}
if (outputDataSet && this->ConvertPointData)
{
itemCount += this->CountItemsToConvert(outputDataSet->GetPointData());
}
if (outputDataSet && this->ConvertCellData)
{
itemCount += this->CountItemsToConvert(outputDataSet->GetCellData());
}
if (outputGraph && this->ConvertPointData)
{
itemCount += this->CountItemsToConvert(outputGraph->GetVertexData());
}
if (outputGraph && this->ConvertCellData)
{
itemCount += this->CountItemsToConvert(outputGraph->GetEdgeData());
}
if (outputTable && this->ConvertPointData)
{
itemCount += this->CountItemsToConvert(outputTable->GetRowData());
}
this->ItemsToConvert = itemCount;
this->ItemsConverted = 0;
if (this->ConvertFieldData)
{
this->ConvertArrays(output->GetFieldData());
}
if (outputDataSet && this->ConvertPointData)
{
this->ConvertArrays(outputDataSet->GetPointData());
}
if (outputDataSet && this->ConvertCellData)
{
this->ConvertArrays(outputDataSet->GetCellData());
}
if (outputGraph && this->ConvertPointData)
{
this->ConvertArrays(outputGraph->GetVertexData());
}
if (outputGraph && this->ConvertCellData)
{
this->ConvertArrays(outputGraph->GetEdgeData());
}
if (outputTable && this->ConvertPointData)
{
this->ConvertArrays(outputTable->GetRowData());
}
return 1;
}
void vtkStringToNumeric::ConvertArrays(vtkFieldData* fieldData)
{
for (int arr = 0; arr < fieldData->GetNumberOfArrays(); arr++)
{
vtkStringArray* stringArray = vtkArrayDownCast<vtkStringArray>(
fieldData->GetAbstractArray(arr));
vtkUnicodeStringArray* unicodeArray = vtkArrayDownCast<vtkUnicodeStringArray>(
fieldData->GetAbstractArray(arr));
if (!stringArray && !unicodeArray)
{
continue;
}
vtkIdType numTuples,numComps;
vtkStdString arrayName;
if (stringArray)
{
numTuples = stringArray->GetNumberOfTuples();
numComps = stringArray->GetNumberOfComponents();
arrayName = stringArray->GetName();
}
else
{
numTuples = unicodeArray->GetNumberOfTuples();
numComps = unicodeArray->GetNumberOfComponents();
arrayName = unicodeArray->GetName();
}
// Set up the output array
vtkDoubleArray* doubleArray = vtkDoubleArray::New();
doubleArray->SetNumberOfComponents(numComps);
doubleArray->SetNumberOfTuples(numTuples);
doubleArray->SetName(arrayName);
// Set up the output array
vtkIntArray* intArray = vtkIntArray::New();
intArray->SetNumberOfComponents(numComps);
intArray->SetNumberOfTuples(numTuples);
intArray->SetName(arrayName);
// Convert the strings to time point values
bool allInteger = true;
bool allNumeric = true;
for (vtkIdType i = 0; i < numTuples*numComps; i++)
{
++ this->ItemsConverted;
if (this->ItemsConverted % 100 == 0)
{
this->UpdateProgress(static_cast<double>(this->ItemsConverted) /
static_cast<double>(this->ItemsToConvert));
}
vtkStdString str;
if (stringArray)
{
str = stringArray->GetValue(i);
}
else
{
str = unicodeArray->GetValue(i).utf8_str();
}
if (this->TrimWhitespacePriorToNumericConversion)
{
size_t startPos = str.find_first_not_of(" \n\t\r");
if (startPos == vtkStdString::npos)
{
str = "";
}
else
{
size_t endPos = str.find_last_not_of(" \n\t\r");
str = str.substr(startPos, endPos-startPos+1);
}
}
bool ok;
if (allInteger)
{
if (str.length() == 0)
{
intArray->SetValue(i, this->DefaultIntegerValue);
doubleArray->SetValue(i, this->DefaultDoubleValue);
continue;
}
int intValue = vtkVariant(str).ToInt(&ok);
if (ok)
{
double doubleValue = intValue;
intArray->SetValue(i, intValue);
doubleArray->SetValue(i, doubleValue);
}
else
{
allInteger = false;
}
}
if (!allInteger)
{
if (str.length() == 0)
{
doubleArray->SetValue(i, this->DefaultDoubleValue);
continue;
}
double doubleValue = vtkVariant(str).ToDouble(&ok);
if (!ok)
{
allNumeric = false;
break;
}
else
{
doubleArray->SetValue(i, doubleValue);
}
}
}
if (allNumeric)
{
// Calling AddArray will replace the old array since the names match.
// Are they all ints, and did I test anything?
if (!this->ForceDouble && allInteger && (numTuples*numComps))
{
fieldData->AddArray(intArray);
}
else
{
fieldData->AddArray(doubleArray);
}
}
intArray->Delete();
doubleArray->Delete();
}
}
//----------------------------------------------------------------------------
int vtkStringToNumeric::ProcessRequest(
vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// create the output
if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA_OBJECT()))
{
return this->RequestDataObject(request, inputVector, outputVector);
}
return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}
//----------------------------------------------------------------------------
int vtkStringToNumeric::RequestDataObject(
vtkInformation*,
vtkInformationVector** inputVector ,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
if (!inInfo)
{
return 0;
}
vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
if (input)
{
// for each output
for(int i=0; i < this->GetNumberOfOutputPorts(); ++i)
{
vtkInformation* info = outputVector->GetInformationObject(i);
vtkDataObject *output = info->Get(vtkDataObject::DATA_OBJECT());
if (!output || !output->IsA(input->GetClassName()))
{
vtkDataObject* newOutput = input->NewInstance();
info->Set(vtkDataObject::DATA_OBJECT(), newOutput);
newOutput->Delete();
}
}
return 1;
}
return 0;
}
void vtkStringToNumeric::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ConvertFieldData: "
<< (this->ConvertFieldData ? "on" : "off") << endl;
os << indent << "ConvertPointData: "
<< (this->ConvertPointData ? "on" : "off") << endl;
os << indent << "ConvertCellData: "
<< (this->ConvertCellData ? "on" : "off") << endl;
os << indent << "ForceDouble: "
<< (this->ForceDouble ? "on" : "off") << endl;
os << indent << "DefaultIntegerValue: "
<< this->DefaultIntegerValue << endl;
os << indent << "DefaultDoubleValue: "
<< this->DefaultDoubleValue << endl;
os << indent << "TrimWhitespacePriorToNumericConversion: "
<< (this->TrimWhitespacePriorToNumericConversion ? "on" : "off") << endl;
}