File: vtkMergeFields.cxx

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (448 lines) | stat: -rw-r--r-- 10,830 bytes parent folder | download | duplicates (4)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkMergeFields.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 "vtkMergeFields.h"

#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDataSetAttributes.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"

vtkStandardNewMacro(vtkMergeFields);

char vtkMergeFields::FieldLocationNames[3][12] 
= { "DATA_OBJECT",
    "POINT_DATA",
    "CELL_DATA" };

typedef vtkMergeFields::Component Component;

vtkMergeFields::vtkMergeFields()
{
  this->FieldName = 0;
  this->FieldLocation = -1;
  this->NumberOfComponents = 0;

  this->Head = 0;
  this->Tail = 0;
}

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

void vtkMergeFields::SetOutputField(const char* name, int fieldLoc)
{
  if (!name)
    {
    return;
    }

  if ( (fieldLoc !=  vtkMergeFields::DATA_OBJECT) &&
       (fieldLoc !=  vtkMergeFields::POINT_DATA) &&
       (fieldLoc !=  vtkMergeFields::CELL_DATA) )
    {
    vtkErrorMacro("The source for the field is wrong.");
    return;
    }

  this->Modified();
  this->FieldLocation = fieldLoc;

  delete[] this->FieldName;
  this->FieldName = new char[strlen(name)+1];
  strcpy(this->FieldName, name);
}


void vtkMergeFields::SetOutputField(const char* name, const char* fieldLoc)
{
  if ( !name || !fieldLoc)
    {
    return;
    }

  int numFieldLocs = 3;
  int i;

  // Convert fieldLoc to int an call the other SetOutputField()
  int loc=-1;
  for(i=0; i<numFieldLocs; i++)
    {
    if (!strcmp(fieldLoc, FieldLocationNames[i]))
      {
      loc = i;
      break;
      }
    }
  if (loc == -1)
    {
    vtkErrorMacro("Location for the field is invalid.");
    return;
    }

  this->SetOutputField(name, loc);

}

void vtkMergeFields::Merge(int component, const char* arrayName, 
                           int sourceComp)
{
  if (!arrayName)
    {
    return;
    }

  this->Modified();
  Component* comp = this->FindComponent(component);
  if ( comp )
    {
    // If component already exists, replace information
    comp->SetName(arrayName);
    comp->SourceIndex = sourceComp;
    }
  else
    {
    // otherwise create a new one
    comp = new Component;
    comp->SetName(arrayName);
    comp->Index = component;
    comp->SourceIndex = sourceComp;
    this->AddComponent(comp);
    }
}

int vtkMergeFields::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
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkDataSet *output = vtkDataSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  // This has to be here because it initialized all field datas.
  output->CopyStructure( input );
  
  // Pass all. (data object's field data is passed by the
  // superclass after this method)
  output->GetPointData()->PassData( input->GetPointData() );
  output->GetCellData()->PassData( input->GetCellData() );

  vtkFieldData* fd = 0;
  vtkFieldData* outputFD = 0;
  Component* cur = this->GetFirst();
  Component* before;

  if (!cur) { return 1; }

  // Get the input and output field data
  if ( this->FieldLocation == vtkMergeFields::DATA_OBJECT)
    {
    fd = input->GetFieldData();
    outputFD = output->GetFieldData();
    if (!fd || !outputFD)
      {
      vtkErrorMacro("No field data in vtkDataObject.");
      return 1;
      }
    }
  else if ( this->FieldLocation == vtkMergeFields::POINT_DATA )
    {
    fd = input->GetPointData();
    outputFD = output->GetPointData();
    }
  else if ( this->FieldLocation == vtkMergeFields::CELL_DATA )
    {
    fd = input->GetCellData();
    outputFD = output->GetCellData();
    }

  // Check if the data types of the input fields are the same
  // Otherwise warn the user.
  // Check if the number of tuples are the same for all arrays.
  vtkDataArray* inputArray;
  int dataType=-1;
  int sameDataType=1;
  int numTuples=-1;
  int sameNumTuples=1;
  do
    {
    before = cur;
    cur = cur->Next;
    inputArray = fd->GetArray(before->FieldName);
    if (!inputArray)
      {
      continue;
      }
    else
      {
      if (dataType == -1)
        {
        dataType = inputArray->GetDataType();
        }
      else
        {
        if ( inputArray->GetDataType() != dataType )
          {
          sameDataType = 0;
          }
        }
      if (numTuples == -1)
        {
        numTuples = inputArray->GetNumberOfTuples();
        }
      else
        {
        if ( inputArray->GetNumberOfTuples() != numTuples )
          {
          sameNumTuples = 0;
          }
        }
      }
    } 
  while (cur);
  if (!sameNumTuples)
    {
    vtkErrorMacro("The number of tuples in the input arrays do not match.");
    return 1;
    }
  if ( dataType == -1 )
    {
    vtkErrorMacro("No input array(s) were found.");
    return 1;
    }
  vtkDataArray* outputArray;
  if (!sameDataType)
    {
    vtkWarningMacro("The input data types do not match. The output will be "<<
                    "float. This will potentially cause accuracy and speed.");
    outputArray = vtkFloatArray::New();
    }
  else
    {
    outputArray = vtkDataArray::CreateDataArray(dataType);
    }

  if (this->NumberOfComponents <= 0)
    {
    vtkErrorMacro("NumberOfComponents has be set prior to the execution of "
                  "this filter");
    }

  outputArray->SetNumberOfComponents(this->NumberOfComponents);
  outputArray->SetNumberOfTuples(numTuples);
  outputArray->SetName(this->FieldName);

  // Merge
  cur = this->GetFirst();
  do
    {
    before = cur;
    cur = cur->Next;
    inputArray = fd->GetArray(before->FieldName);
    if (inputArray)
      {
      if (!this->MergeArray(inputArray, outputArray, 
                            before->SourceIndex, before->Index))
        {
        outputArray->Delete();
        return 1;
        }
      }
    else
      {
      if (before->FieldName)
        {
        vtkWarningMacro("Input array " << before->FieldName 
                        << " does not exist.");
        }
      continue;
      }
    } 
  while (cur);
  outputFD->AddArray(outputArray);
  outputArray->Delete();

  return 1;
}

// fast pointer copy
template <class T>
void vtkMergeFieldsCopyTuples(T* input, T* output, vtkIdType numTuples, 
                int numInComp, int numOutComp, int inComp, int outComp)
{
  for (int i=0; i<numTuples; i++)
    {
    output[numOutComp*i+outComp] = input[numInComp*i+inComp];
    }
}

int vtkMergeFields::MergeArray(vtkDataArray* in, vtkDataArray* out,
                               int inComp, int outComp)
{
  if ( (inComp < 0) || (inComp > in->GetNumberOfComponents()) ||
       (outComp < 0) || (outComp > out->GetNumberOfComponents()) )
    {
    vtkErrorMacro("Invalid component. Can not merge.");
    return 0;
    }

  int numTuples = in->GetNumberOfTuples();
  int i;

  if ( numTuples > 0 )
    {
    // If data types match, use templated, fast method
    if ( in->GetDataType() == out->GetDataType() )
      {
      switch (out->GetDataType())
        {
        vtkTemplateMacro(
          vtkMergeFieldsCopyTuples((VTK_TT *)in->GetVoidPointer(0), 
                                   (VTK_TT *)out->GetVoidPointer(0), numTuples,
                                   in->GetNumberOfComponents(), 
                                   out->GetNumberOfComponents(), 
                                   inComp, outComp ));
        // This is not supported by the template macro.
        // Switch to using the float interface.
        case VTK_BIT:
        {
        for(i=0; i<numTuples; i++)
          {
          out->SetComponent(i, outComp, in->GetComponent(i, inComp));
          }
        }
        break;
        default:
          vtkErrorMacro(<<"Sanity check failed: Unsupported data type.");
          return 0;
        }
      }
    // otherwise use slow float copy
    else
      {
      for(i=0; i<numTuples; i++)
        {
        out->SetComponent(i, outComp, in->GetComponent(i, inComp));
        }
      }
    }
  
  return 1;

}

// linked list methods
void vtkMergeFields::AddComponent(Component* op)
{
  op->Next = 0;

  if (!this->Head)
    {
    this->Head = op;
    this->Tail = op;
    return;
    }
  this->Tail->Next = op;
  this->Tail = op;
}

Component* vtkMergeFields::FindComponent(int index)
{
  Component* cur = this->GetFirst();
  if (!cur) { return 0; }

  if (cur->Index == index) { return cur; }
  while (cur->Next)
    {
    if (cur->Next->Index == index)
      {
      return cur->Next;
      }
    cur = cur->Next;
    }
  return 0;
}

void vtkMergeFields::DeleteAllComponents()
{
  Component* cur = this->GetFirst();
  if (!cur) {return;}
  Component* before;
  do 
    {
    before = cur;
    cur = cur->Next;
    delete before;
    } 
  while (cur);
  this->Head = 0;
  this->Tail = 0;
}

void vtkMergeFields::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 location: " << this->FieldLocation << endl;
  os << indent << "Linked list head: " << this->Head << endl;
  os << indent << "Linked list tail: " << this->Tail << endl;
  os << indent << "NumberOfComponents: " << this->NumberOfComponents << endl;
  os << indent << "Components: " << endl;
  this->PrintAllComponents(os, indent.GetNextIndent());
}

void vtkMergeFields::PrintComponent(Component* op, ostream& os, vtkIndent indent)
{
  os << indent << "Field name: " << op->FieldName << endl;
  os << indent << "Component index: " << op->Index << endl;
  os << indent << "Source component index: " << op->SourceIndex << endl;
}

void vtkMergeFields::PrintAllComponents(ostream& os, vtkIndent indent)
{
  Component* cur = this->GetFirst();
  if (!cur) { return; }
  Component* before;
  do
    {
    before = cur;
    cur = cur->Next;
    os << endl;
    this->PrintComponent(before, os, indent);
    } 
  while (cur);
}