forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkTransferAttributes.cxx
328 lines (292 loc) · 11.2 KB
/
vtkTransferAttributes.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTransferAttributes.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 "vtkTransferAttributes.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkDataObject.h"
#include "vtkDataSet.h"
#include "vtkEdgeListIterator.h"
#include "vtkFloatArray.h"
#include "vtkGraph.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkTree.h"
#include "vtkVariantArray.h"
#include <vtksys/stl/map>
using vtksys_stl::map;
//---------------------------------------------------------------------------
template <typename T>
vtkVariant vtkGetValue(T* arr, vtkIdType index)
{
return vtkVariant(arr[index]);
}
//---------------------------------------------------------------------------
static vtkVariant vtkGetVariantValue(vtkAbstractArray* arr, vtkIdType i)
{
vtkVariant val;
switch(arr->GetDataType())
{
vtkExtraExtendedTemplateMacro(val = vtkGetValue(
static_cast<VTK_TT*>(arr->GetVoidPointer(0)), i));
}
return val;
}
vtkStandardNewMacro(vtkTransferAttributes);
vtkTransferAttributes::vtkTransferAttributes()
{
this->SetNumberOfInputPorts(2);
this->DirectMapping = false;
this->DefaultValue = 1;
this->SourceArrayName = 0;
this->TargetArrayName = 0;
this->SourceFieldType=vtkDataObject::FIELD_ASSOCIATION_POINTS;
this->TargetFieldType=vtkDataObject::FIELD_ASSOCIATION_POINTS;
}
vtkTransferAttributes::~vtkTransferAttributes()
{
this->SetSourceArrayName(0);
this->SetTargetArrayName(0);
}
int vtkTransferAttributes::FillInputPortInformation(int port, vtkInformation* info)
{
if( port == 0 )
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataObject");
return 1;
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataObject");
return 1;
}
return 0;
}
int vtkTransferAttributes::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *targetInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *sourceInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkDataObject* sourceInput = sourceInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject* targetInput = targetInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject* output = outInfo->Get(vtkDataObject::DATA_OBJECT());
output->ShallowCopy(targetInput);
// get the input and output
int item_count_source = 0;
vtkDataSetAttributes* dsa_source = 0;
if (vtkDataSet::SafeDownCast(sourceInput) &&
this->SourceFieldType == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
dsa_source = vtkDataSet::SafeDownCast(sourceInput)->GetPointData();
item_count_source = vtkDataSet::SafeDownCast(sourceInput)->GetNumberOfPoints();
}
else if (vtkDataSet::SafeDownCast(sourceInput) &&
this->SourceFieldType == vtkDataObject::FIELD_ASSOCIATION_CELLS)
{
dsa_source = vtkDataSet::SafeDownCast(sourceInput)->GetCellData();
item_count_source = vtkDataSet::SafeDownCast(sourceInput)->GetNumberOfCells();
}
else if (vtkGraph::SafeDownCast(sourceInput) &&
this->SourceFieldType == vtkDataObject::FIELD_ASSOCIATION_VERTICES)
{
dsa_source = vtkGraph::SafeDownCast(sourceInput)->GetVertexData();
item_count_source = vtkGraph::SafeDownCast(sourceInput)->GetNumberOfVertices();
}
else if (vtkGraph::SafeDownCast(sourceInput) &&
this->SourceFieldType == vtkDataObject::FIELD_ASSOCIATION_EDGES)
{
dsa_source = vtkGraph::SafeDownCast(sourceInput)->GetEdgeData();
item_count_source = vtkGraph::SafeDownCast(sourceInput)->GetNumberOfEdges();
}
else if (vtkTable::SafeDownCast(sourceInput) &&
this->SourceFieldType == vtkDataObject::FIELD_ASSOCIATION_ROWS)
{
dsa_source = vtkTable::SafeDownCast(sourceInput)->GetRowData();
item_count_source = vtkTable::SafeDownCast(sourceInput)->GetNumberOfRows();
}
else
{
// ERROR
vtkErrorMacro( "Input type must be specified as a dataset, graph or table." );
return 0;
}
vtkDataSetAttributes* dsa_target = 0;
vtkDataSetAttributes* dsa_out = 0;
int item_count_target = 0;
if (vtkDataSet::SafeDownCast(targetInput) &&
this->TargetFieldType == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
dsa_target = vtkDataSet::SafeDownCast(targetInput)->GetPointData();
dsa_out = vtkDataSet::SafeDownCast(output)->GetPointData();
item_count_target = vtkDataSet::SafeDownCast(targetInput)->GetNumberOfPoints();
}
else if (vtkDataSet::SafeDownCast(targetInput) &&
this->TargetFieldType == vtkDataObject::FIELD_ASSOCIATION_CELLS)
{
dsa_target = vtkDataSet::SafeDownCast(targetInput)->GetCellData();
dsa_out = vtkDataSet::SafeDownCast(output)->GetCellData();
item_count_target = vtkDataSet::SafeDownCast(targetInput)->GetNumberOfCells();
}
else if (vtkGraph::SafeDownCast(targetInput) &&
this->TargetFieldType == vtkDataObject::FIELD_ASSOCIATION_VERTICES)
{
dsa_target = vtkGraph::SafeDownCast(targetInput)->GetVertexData();
dsa_out = vtkGraph::SafeDownCast(output)->GetVertexData();
item_count_target = vtkGraph::SafeDownCast(targetInput)->GetNumberOfVertices();
}
else if (vtkGraph::SafeDownCast(targetInput) &&
this->TargetFieldType == vtkDataObject::FIELD_ASSOCIATION_EDGES)
{
dsa_target = vtkGraph::SafeDownCast(targetInput)->GetEdgeData();
dsa_out = vtkGraph::SafeDownCast(output)->GetEdgeData();
item_count_target = vtkGraph::SafeDownCast(targetInput)->GetNumberOfEdges();
}
else if (vtkTable::SafeDownCast(targetInput) &&
this->TargetFieldType == vtkDataObject::FIELD_ASSOCIATION_ROWS)
{
dsa_target = vtkTable::SafeDownCast(targetInput)->GetRowData();
dsa_out = vtkTable::SafeDownCast(output)->GetRowData();
item_count_target = vtkTable::SafeDownCast(targetInput)->GetNumberOfRows();
}
else
{
// ERROR
vtkErrorMacro( "Input type must be specified as a dataset, graph or table." );
return 0;
}
if( this->SourceArrayName == 0 || this->TargetArrayName == 0 )
{
vtkErrorMacro( "Must specify source and target array names for the transfer." );
return 0;
}
vtkAbstractArray* sourceIdArray = dsa_source->GetPedigreeIds();
vtkAbstractArray* targetIdArray = dsa_target->GetPedigreeIds();
// Check for valid pedigree id arrays.
if (!sourceIdArray)
{
vtkErrorMacro("SourceInput pedigree id array not found.");
return 0;
}
if (!targetIdArray)
{
vtkErrorMacro("TargetInput pedigree id array not found.");
return 0;
}
if( item_count_source != sourceIdArray->GetNumberOfTuples() )
{
vtkErrorMacro( "The number of pedigree ids must be equal to the number of items in the source data object." );
return 0;
}
if( item_count_target != targetIdArray->GetNumberOfTuples() )
{
vtkErrorMacro( "The number of pedigree ids must be equal to the number of items in the target data object." );
return 0;
}
// Create a map from sourceInput indices to targetInput indices
// If we are using DirectMapping this is trivial
// we just create an identity map
int i;
map<vtkIdType, vtkIdType> sourceIndexToTargetIndex;
if (this->DirectMapping)
{
if (sourceIdArray->GetNumberOfTuples() > targetIdArray->GetNumberOfTuples())
{
vtkErrorMacro("Cannot have more sourceInput tuples than targetInput values using direct mapping.");
return 0;
}
// Create identity map.
for( i = 0; i < sourceIdArray->GetNumberOfTuples(); i++)
{
sourceIndexToTargetIndex[i] = i;
}
}
// Okay if we do not have direct mapping then we need
// to do some templated madness to go from an arbitrary
// type to a nice vtkIdType to vtkIdType mapping
if (!this->DirectMapping)
{
map<vtkVariant,vtkIdType,vtkVariantLessThan> sourceInputIdMap;
// Create a map from sourceInput id to sourceInput index
for( i=0; i<sourceIdArray->GetNumberOfTuples(); i++)
{
sourceInputIdMap[vtkGetVariantValue(sourceIdArray,i)] = i;
}
// Now create the map from sourceInput index to targetInput index
for( i=0; i < targetIdArray->GetNumberOfTuples(); i++)
{
vtkVariant id = vtkGetVariantValue(targetIdArray,i);
if (sourceInputIdMap.count(id))
{
sourceIndexToTargetIndex[sourceInputIdMap[id]] = i;
}
}
}
vtkAbstractArray *sourceArray = dsa_source->GetAbstractArray( this->SourceArrayName );
vtkAbstractArray *targetArray = vtkAbstractArray::CreateArray(sourceArray->GetDataType());
targetArray->SetName( this->TargetArrayName );
targetArray->SetNumberOfComponents(sourceArray->GetNumberOfComponents());
targetArray->SetNumberOfTuples(targetIdArray->GetNumberOfTuples());
for( i = 0; i < targetArray->GetNumberOfTuples(); i++)
{
targetArray->InsertVariantValue(i, this->DefaultValue);
}
for( i = 0; i < sourceArray->GetNumberOfTuples(); i++)
{
if( sourceArray->GetVariantValue(i) < 0 )
{
cout << sourceIndexToTargetIndex[i] << " "
<< sourceArray->GetVariantValue(i).ToString() << " "
<< sourceArray->GetNumberOfTuples() << " "
<< sourceIdArray->GetNumberOfTuples() << " "
<< i << endl;
vtkErrorMacro( "Bad value..." );
continue;
}
targetArray->SetTuple(sourceIndexToTargetIndex[i], i, sourceArray);
}
dsa_out->AddArray(targetArray);
targetArray->Delete();
return 1;
}
vtkVariant vtkTransferAttributes::GetDefaultValue()
{
return this->DefaultValue;
}
void vtkTransferAttributes::SetDefaultValue(vtkVariant value)
{
this->DefaultValue = value;
}
void vtkTransferAttributes::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "DirectMapping: " << this->DirectMapping << endl;
os << indent << "DefaultValue: " << this->DefaultValue.ToString() << endl;
os << indent << "SourceArrayName: " << (this->SourceArrayName ? this->SourceArrayName : "(none)") << endl;
os << indent << "TargetArrayName: " << (this->TargetArrayName ? this->TargetArrayName : "(none)") << endl;
os << indent << "SourceFieldType: " << this->SourceFieldType << endl;
os << indent << "TargetFieldType: " << this->TargetFieldType << endl;
}