vtkAssignAttribute.cxx 12 KB
Newer Older
Berk Geveci's avatar
Berk Geveci committed
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkAssignAttribute.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Berk Geveci's avatar
Berk Geveci committed
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Berk Geveci's avatar
Berk Geveci committed
13
14
15

=========================================================================*/
#include "vtkAssignAttribute.h"
16

17
#include "vtkCellData.h"
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
18
#include "vtkDataArray.h"
19
#include "vtkDataSet.h"
Berk Geveci's avatar
Berk Geveci committed
20
#include "vtkDataSetAttributes.h"
21
#include "vtkGraph.h"
22
23
#include "vtkInformation.h"
#include "vtkInformationVector.h"
24
#include "vtkObjectFactory.h"
25
#include "vtkPointData.h"
Sean McBride's avatar
Sean McBride committed
26
#include <cctype>
Berk Geveci's avatar
Berk Geveci committed
27

28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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;
}

Brad King's avatar
Brad King committed
42
43
vtkStandardNewMacro(vtkAssignAttribute);

44
char vtkAssignAttribute::AttributeLocationNames[vtkAssignAttribute::NUM_ATTRIBUTE_LOCS][12]
Berk Geveci's avatar
Berk Geveci committed
45
= { "POINT_DATA",
46
47
48
    "CELL_DATA",
    "VERTEX_DATA",
    "EDGE_DATA"};
Berk Geveci's avatar
Berk Geveci committed
49

50
char vtkAssignAttribute::AttributeNames[vtkDataSetAttributes::NUM_ATTRIBUTES][20]  = { {0} };
Berk Geveci's avatar
Berk Geveci committed
51
52
53
54

vtkAssignAttribute::vtkAssignAttribute()
{
  this->FieldName = 0;
55
  this->AttributeLocationAssignment = -1;
Berk Geveci's avatar
Berk Geveci committed
56
57
  this->AttributeType = -1;
  this->InputAttributeType = -1;
58
  this->FieldTypeAssignment = -1;
59
60

  //convert the attribute names to uppercase for local use
61
  if (vtkAssignAttribute::AttributeNames[0][0] == 0)
62
63
64
    {
    for (int i = 0; i < vtkDataSetAttributes::NUM_ATTRIBUTES; i++)
      {
65
      int l = static_cast<int>(strlen(vtkDataSetAttributes::GetAttributeTypeAsString(i)));
66
      for (int c = 0; c < l && c < 19; c++)
67
        {
68
        vtkAssignAttribute::AttributeNames[i][c] =
69
70
71
72
          toupper(vtkDataSetAttributes::GetAttributeTypeAsString(i)[c]);
        }
      }
    }
Berk Geveci's avatar
Berk Geveci committed
73
74
75
76
77
78
79
80
}

vtkAssignAttribute::~vtkAssignAttribute()
{
  delete[] this->FieldName;
  this->FieldName = 0;
}

81
void vtkAssignAttribute::Assign(const char* fieldName, int attributeType,
82
                                int attributeLoc)
Berk Geveci's avatar
Berk Geveci committed
83
84
85
86
87
88
{
  if (!fieldName)
    {
    return;
    }

89
  if ( (attributeType < 0) ||
Berk Geveci's avatar
Berk Geveci committed
90
91
92
93
94
95
       (attributeType > vtkDataSetAttributes::NUM_ATTRIBUTES) )
    {
    vtkErrorMacro("Wrong attribute type.");
    return;
    }

96
97
  if ( (attributeLoc < 0) ||
       (attributeLoc > vtkAssignAttribute::NUM_ATTRIBUTE_LOCS) )
Berk Geveci's avatar
Berk Geveci committed
98
99
100
101
102
    {
    vtkErrorMacro("The source for the field is wrong.");
    return;
    }

103
  this->Modified();
Berk Geveci's avatar
Berk Geveci committed
104
105
106
107
108
  delete[] this->FieldName;
  this->FieldName = new char[strlen(fieldName)+1];
  strcpy(this->FieldName, fieldName);

  this->AttributeType = attributeType;
109
110
  this->AttributeLocationAssignment = attributeLoc;
  this->FieldTypeAssignment = vtkAssignAttribute::NAME;
Berk Geveci's avatar
Berk Geveci committed
111
112
}

113
void vtkAssignAttribute::Assign(int inputAttributeType, int attributeType,
114
                                int attributeLoc)
Berk Geveci's avatar
Berk Geveci committed
115
{
116
  if ( (attributeType < 0) ||
Berk Geveci's avatar
Berk Geveci committed
117
       (attributeType > vtkDataSetAttributes::NUM_ATTRIBUTES) ||
118
       (inputAttributeType < 0) ||
Berk Geveci's avatar
Berk Geveci committed
119
120
121
122
123
124
       (inputAttributeType > vtkDataSetAttributes::NUM_ATTRIBUTES))
    {
    vtkErrorMacro("Wrong attribute type.");
    return;
    }

125
126
  if ( (attributeLoc < 0) ||
       (attributeLoc > vtkAssignAttribute::NUM_ATTRIBUTE_LOCS) )
Berk Geveci's avatar
Berk Geveci committed
127
128
129
130
131
    {
    vtkErrorMacro("The source for the field is wrong.");
    return;
    }

132
  this->Modified();
Berk Geveci's avatar
Berk Geveci committed
133
134
  this->AttributeType = attributeType;
  this->InputAttributeType = inputAttributeType;
135
136
  this->AttributeLocationAssignment = attributeLoc;
  this->FieldTypeAssignment = vtkAssignAttribute::ATTRIBUTE;
Berk Geveci's avatar
Berk Geveci committed
137
138
}

139
140
void vtkAssignAttribute::Assign(const char* name,
                                const char* attributeType,
141
                                const char* attributeLoc)
Berk Geveci's avatar
Berk Geveci committed
142
143
144
145
146
147
148
{
  if (!name || !attributeType || !attributeLoc)
    {
    return;
    }

  int numAttr = vtkDataSetAttributes::NUM_ATTRIBUTES;
149
  int numAttributeLocs = vtkAssignAttribute::NUM_ATTRIBUTE_LOCS;
Berk Geveci's avatar
Berk Geveci committed
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
  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);
    }
}

203
204
205
206
207
208
209
210
211
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) &&
212
      (this->AttributeLocationAssignment != -1) && (this->FieldTypeAssignment != -1))
213
    {
214
    int fieldAssociation = vtkDataObject::FIELD_ASSOCIATION_POINTS;
215
    switch (this->AttributeLocationAssignment)
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      {
      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;
      }
230
    if (this->FieldTypeAssignment == vtkAssignAttribute::NAME && this->FieldName)
231
232
233
      {
      vtkDataObject::SetActiveAttribute(outInfo, fieldAssociation,
        this->FieldName, this->AttributeType);
234
235
236
237
238
239
240
241
242
243
244
      vtkInformation *inputAttributeInfo = vtkDataObject::GetNamedFieldInformation(
        inInfo, fieldAssociation, this->FieldName);
      if (inputAttributeInfo)
        {
        const int type = inputAttributeInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE());
        const int numComponents = inputAttributeInfo->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS());
        const int numTuples = inputAttributeInfo->Get(vtkDataObject::FIELD_NUMBER_OF_TUPLES());

        vtkDataObject::SetActiveAttributeInfo(outInfo, fieldAssociation,
          this->AttributeType, this->FieldName, type, numComponents, numTuples);
        }
245
      }
246
    else if (this->FieldTypeAssignment == vtkAssignAttribute::ATTRIBUTE  &&
247
248
249
250
251
252
      this->InputAttributeType != -1)
      {
      vtkInformation *inputAttributeInfo = vtkDataObject::GetActiveFieldInformation(
        inInfo, fieldAssociation, this->InputAttributeType);
      if (inputAttributeInfo) // do we have an active field of requested type
        {
253
254
255
256
257
258
259
260
        const char * name = inputAttributeInfo->Get(vtkDataObject::FIELD_NAME());
        const int type = inputAttributeInfo->Get(vtkDataObject::FIELD_ARRAY_TYPE());
        const int numComponents = inputAttributeInfo->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS());
        const int numTuples = inputAttributeInfo->Get(vtkDataObject::FIELD_NUMBER_OF_TUPLES());

        vtkDataObject::SetActiveAttribute(outInfo, fieldAssociation, name, this->AttributeType);
        vtkDataObject::SetActiveAttributeInfo(outInfo, fieldAssociation,
          this->AttributeType, name, type, numComponents, numTuples);
261
262
263
264
265
266
267
268
        }
      }
    }

  return 1;
}


269
270
271
272
int vtkAssignAttribute::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
Berk Geveci's avatar
Berk Geveci committed
273
{
274
275
276
277
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

278
  // get the input and output
279
280
  vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
  vtkDataObject *output = outInfo->Get(vtkDataObject::DATA_OBJECT());
281

282
283
  vtkDataSetAttributes* ods=0;
  if (vtkDataSet::SafeDownCast(input))
284
    {
285
286
287
288
    vtkDataSet *dsInput = vtkDataSet::SafeDownCast(input);
    vtkDataSet *dsOutput = vtkDataSet::SafeDownCast(output);
    // This has to be here because it initialized all field datas.
    dsOutput->CopyStructure( dsInput );
Berk Geveci's avatar
Berk Geveci committed
289

290
291
292
293
294
295
    if ( dsOutput->GetFieldData() && dsInput->GetFieldData() )
      {
      dsOutput->GetFieldData()->PassData( dsInput->GetFieldData() );
      }
    dsOutput->GetPointData()->PassData( dsInput->GetPointData() );
    dsOutput->GetCellData()->PassData( dsInput->GetCellData() );
296
    switch (this->AttributeLocationAssignment)
Berk Geveci's avatar
Berk Geveci committed
297
298
      {
      case vtkAssignAttribute::POINT_DATA:
299
        ods = dsOutput->GetPointData();
300
        break;
Berk Geveci's avatar
Berk Geveci committed
301
      case vtkAssignAttribute::CELL_DATA:
302
        ods = dsOutput->GetCellData();
303
        break;
304
305
306
      default:
        vtkErrorMacro(<<"Data must be point or cell for vtkDataSet");
        return 0;
Berk Geveci's avatar
Berk Geveci committed
307
      }
308
309
310
311
312
313
    }
  else
    {
    vtkGraph *graphInput = vtkGraph::SafeDownCast(input);
    vtkGraph *graphOutput = vtkGraph::SafeDownCast(output);
    graphOutput->ShallowCopy( graphInput );
314
    switch (this->AttributeLocationAssignment)
315
316
317
318
319
320
321
322
323
324
325
326
327
328
      {
      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) &&
329
      (this->AttributeLocationAssignment != -1) && (this->FieldTypeAssignment != -1))
330
331
    {
    // Get the appropriate output DataSetAttributes
332
    if (this->FieldTypeAssignment == vtkAssignAttribute::NAME && this->FieldName)
Berk Geveci's avatar
Berk Geveci committed
333
334
335
      {
      ods->SetActiveAttribute(this->FieldName, this->AttributeType);
      }
336
    else if (this->FieldTypeAssignment == vtkAssignAttribute::ATTRIBUTE  &&
337
             (this->InputAttributeType != -1))
Berk Geveci's avatar
Berk Geveci committed
338
339
340
341
      {
      // If labeling an attribute as another attribute, we
      // need to get it's index and call SetActiveAttribute()
      // with that index
342
343
344
      //int attributeIndices[vtkDataSetAttributes::NUM_ATTRIBUTES];
      //ods->GetAttributeIndices(attributeIndices);
      // if (attributeIndices[this->InputAttributeType] != -1)
345
346
      vtkAbstractArray *oaa = ods->GetAbstractAttribute(this->InputAttributeType);
      if (oaa)
347
        {
348
349
350
351
352
        // 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);
353
        }
Berk Geveci's avatar
Berk Geveci committed
354
355
      }
    }
356
357

  return 1;
Berk Geveci's avatar
Berk Geveci committed
358
359
}

360
361
362
363
364
365
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");
366
  return 1;
367
368
}

Berk Geveci's avatar
Berk Geveci committed
369
370
void vtkAssignAttribute::PrintSelf(ostream& os, vtkIndent indent)
{
Brad King's avatar
Brad King committed
371
  this->Superclass::PrintSelf(os,indent);
Berk Geveci's avatar
Berk Geveci committed
372
373
374
375
376
377
378
379
380
  os << indent << "Field name: ";
  if (this->FieldName)
    {
    os << this->FieldName << endl;
    }
  else
    {
    os << "(none)" << endl;
    }
381
  os << indent << "Field type: " << this->FieldTypeAssignment << endl;
Berk Geveci's avatar
Berk Geveci committed
382
383
384
  os << indent << "Attribute type: " << this->AttributeType << endl;
  os << indent << "Input attribute type: " << this->InputAttributeType
     << endl;
385
  os << indent << "Attribute location: " << this->AttributeLocationAssignment << endl;
Berk Geveci's avatar
Berk Geveci committed
386
}