/
vtkAssignAttribute.cxx
370 lines (331 loc) · 10.9 KB
/
vtkAssignAttribute.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAssignAttribute.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.
=========================================================================*/
#include "vtkAssignAttribute.h"
#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkDataSetAttributes.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <cctype>
#include <cassert>
static int vtkGetArrayIndex(vtkDataSetAttributes* dsa, vtkAbstractArray* array)
{
for (int cc=0; cc < dsa->GetNumberOfArrays(); cc++)
{
if (dsa->GetAbstractArray(cc) == array)
{
return cc;
}
}
return -1;
}
vtkStandardNewMacro(vtkAssignAttribute);
char vtkAssignAttribute::AttributeLocationNames[vtkAssignAttribute::NUM_ATTRIBUTE_LOCS][12]
= { "POINT_DATA",
"CELL_DATA",
"VERTEX_DATA",
"EDGE_DATA"};
char vtkAssignAttribute::AttributeNames[vtkDataSetAttributes::NUM_ATTRIBUTES][20] = { {0} };
vtkAssignAttribute::vtkAssignAttribute()
{
this->FieldName = 0;
this->AttributeLocationAssignment = -1;
this->AttributeType = -1;
this->InputAttributeType = -1;
this->FieldTypeAssignment = -1;
//convert the attribute names to uppercase for local use
if (vtkAssignAttribute::AttributeNames[0][0] == 0)
{
for (int i = 0; i < vtkDataSetAttributes::NUM_ATTRIBUTES; i++)
{
int l = static_cast<int>(strlen(vtkDataSetAttributes::GetAttributeTypeAsString(i)));
for (int c = 0; c < l && c < 19; c++)
{
vtkAssignAttribute::AttributeNames[i][c] =
toupper(vtkDataSetAttributes::GetAttributeTypeAsString(i)[c]);
}
}
}
}
vtkAssignAttribute::~vtkAssignAttribute()
{
delete[] this->FieldName;
this->FieldName = 0;
}
void vtkAssignAttribute::Assign(const char* fieldName, int attributeType,
int attributeLoc)
{
if (!fieldName)
{
return;
}
if ( (attributeType < 0) ||
(attributeType > vtkDataSetAttributes::NUM_ATTRIBUTES) )
{
vtkErrorMacro("Wrong attribute type.");
return;
}
if ( (attributeLoc < 0) ||
(attributeLoc > vtkAssignAttribute::NUM_ATTRIBUTE_LOCS) )
{
vtkErrorMacro("The source for the field is wrong.");
return;
}
this->Modified();
delete[] this->FieldName;
this->FieldName = new char[strlen(fieldName)+1];
strcpy(this->FieldName, fieldName);
this->AttributeType = attributeType;
this->AttributeLocationAssignment = attributeLoc;
this->FieldTypeAssignment = vtkAssignAttribute::NAME;
}
void vtkAssignAttribute::Assign(int inputAttributeType, int attributeType,
int attributeLoc)
{
if ( (attributeType < 0) ||
(attributeType > vtkDataSetAttributes::NUM_ATTRIBUTES) ||
(inputAttributeType < 0) ||
(inputAttributeType > vtkDataSetAttributes::NUM_ATTRIBUTES))
{
vtkErrorMacro("Wrong attribute type.");
return;
}
if ( (attributeLoc < 0) ||
(attributeLoc > vtkAssignAttribute::NUM_ATTRIBUTE_LOCS) )
{
vtkErrorMacro("The source for the field is wrong.");
return;
}
this->Modified();
this->AttributeType = attributeType;
this->InputAttributeType = inputAttributeType;
this->AttributeLocationAssignment = attributeLoc;
this->FieldTypeAssignment = vtkAssignAttribute::ATTRIBUTE;
}
void vtkAssignAttribute::Assign(const char* name,
const char* attributeType,
const char* attributeLoc)
{
if (!name || !attributeType || !attributeLoc)
{
return;
}
int numAttr = vtkDataSetAttributes::NUM_ATTRIBUTES;
int numAttributeLocs = vtkAssignAttribute::NUM_ATTRIBUTE_LOCS;
int i;
// Convert strings to ints and call the appropriate Assign()
int inputAttributeType=-1;
for(i=0; i<numAttr; i++)
{
if (!strcmp(name, AttributeNames[i]))
{
inputAttributeType = i;
break;
}
}
int attrType=-1;
for(i=0; i<numAttr; i++)
{
if (!strcmp(attributeType, AttributeNames[i]))
{
attrType = i;
break;
}
}
if ( attrType == -1 )
{
vtkErrorMacro("Target attribute type is invalid.");
return;
}
int loc=-1;
for(i=0; i<numAttributeLocs; i++)
{
if (!strcmp(attributeLoc, AttributeLocationNames[i]))
{
loc = i;
break;
}
}
if (loc == -1)
{
vtkErrorMacro("Target location for the attribute is invalid.");
return;
}
if ( inputAttributeType == -1 )
{
this->Assign(name, attrType, loc);
}
else
{
this->Assign(inputAttributeType, attrType, loc);
}
}
int vtkAssignAttribute::RequestInformation(vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
if ((this->AttributeType != -1) &&
(this->AttributeLocationAssignment != -1) && (this->FieldTypeAssignment != -1))
{
int fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_POINTS;
switch (this->AttributeLocationAssignment)
{
case POINT_DATA:
fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_POINTS;
break;
case CELL_DATA:
fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_CELLS;
break;
case VERTEX_DATA:
fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_VERTICES;
break;
default:
fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_EDGES;
break;
}
if (this->FieldTypeAssignment == vtkAssignAttribute::NAME && this->FieldName)
{
vtkDataObject::SetActiveAttribute(outInfo, fieldAssociation,
this->FieldName, this->AttributeType);
}
else if (this->FieldTypeAssignment == vtkAssignAttribute::ATTRIBUTE &&
this->InputAttributeType != -1)
{
vtkInformation *inputAttributeInfo = vtkDataObject::GetActiveFieldInformation(
inInfo, fieldAssociation, this->InputAttributeType);
if (inputAttributeInfo) // do we have an active field of requested type
{
vtkDataObject::SetActiveAttribute(outInfo, fieldAssociation,
inputAttributeInfo->Get( vtkDataObject::FIELD_NAME() ),
this->AttributeType);
}
}
}
return 1;
}
int vtkAssignAttribute::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataObject *output = outInfo->Get(vtkDataObject::DATA_OBJECT());
vtkDataSetAttributes* ods=0;
if (vtkDataSet::SafeDownCast(input))
{
vtkDataSet *dsInput = vtkDataSet::SafeDownCast(input);
vtkDataSet *dsOutput = vtkDataSet::SafeDownCast(output);
// This has to be here because it initialized all field datas.
dsOutput->CopyStructure( dsInput );
if ( dsOutput->GetFieldData() && dsInput->GetFieldData() )
{
dsOutput->GetFieldData()->PassData( dsInput->GetFieldData() );
}
dsOutput->GetPointData()->PassData( dsInput->GetPointData() );
dsOutput->GetCellData()->PassData( dsInput->GetCellData() );
switch (this->AttributeLocationAssignment)
{
case vtkAssignAttribute::POINT_DATA:
ods = dsOutput->GetPointData();
break;
case vtkAssignAttribute::CELL_DATA:
ods = dsOutput->GetCellData();
break;
default:
vtkErrorMacro(<<"Data must be point or cell for vtkDataSet");
return 0;
}
}
else
{
vtkGraph *graphInput = vtkGraph::SafeDownCast(input);
vtkGraph *graphOutput = vtkGraph::SafeDownCast(output);
graphOutput->ShallowCopy( graphInput );
switch (this->AttributeLocationAssignment)
{
case vtkAssignAttribute::VERTEX_DATA:
ods = graphOutput->GetVertexData();
break;
case vtkAssignAttribute::EDGE_DATA:
ods = graphOutput->GetEdgeData();
break;
default:
vtkErrorMacro(<<"Data must be vertex or edge for vtkGraph");
return 0;
}
}
if ((this->AttributeType != -1) &&
(this->AttributeLocationAssignment != -1) && (this->FieldTypeAssignment != -1))
{
// Get the appropriate output DataSetAttributes
if (this->FieldTypeAssignment == vtkAssignAttribute::NAME && this->FieldName)
{
ods->SetActiveAttribute(this->FieldName, this->AttributeType);
}
else if (this->FieldTypeAssignment == vtkAssignAttribute::ATTRIBUTE &&
(this->InputAttributeType != -1))
{
// If labeling an attribute as another attribute, we
// need to get it's index and call SetActiveAttribute()
// with that index
//int attributeIndices[vtkDataSetAttributes::NUM_ATTRIBUTES];
//ods->GetAttributeIndices(attributeIndices);
// if (attributeIndices[this->InputAttributeType] != -1)
vtkAbstractArray *oaa = ods->GetAbstractAttribute(this->InputAttributeType);
if (oaa)
{
// Use array index to mark this array active and not its name since VTK
// arrays not necessarily have names.
int arrIndex = vtkGetArrayIndex(ods, oaa);
assert (arrIndex >= 0); // arrIndex cannot be -1. Doesn't make sense.
ods->SetActiveAttribute(arrIndex,this->AttributeType);
}
}
}
return 1;
}
int vtkAssignAttribute::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
// This algorithm may accept a vtkPointSet or vtkGraph.
info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
return 1;
}
void vtkAssignAttribute::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Field name: ";
if (this->FieldName)
{
os << this->FieldName << endl;
}
else
{
os << "(none)" << endl;
}
os << indent << "Field type: " << this->FieldTypeAssignment << endl;
os << indent << "Attribute type: " << this->AttributeType << endl;
os << indent << "Input attribute type: " << this->InputAttributeType
<< endl;
os << indent << "Attribute location: " << this->AttributeLocationAssignment << endl;
}