vtkConvertSelection.cxx 37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkConvertSelection.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 (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkConvertSelection.h"

#include "vtkCellData.h"
22
#include "vtkCommand.h"
23
#include "vtkCompositeDataSet.h"
24
25
#include "vtkDataArrayTemplate.h"
#include "vtkDataSet.h"
26
27
#include "vtkDoubleArray.h"
#include "vtkExtractSelectedThresholds.h"
28
29
#include "vtkExtractSelection.h"
#include "vtkFieldData.h"
30
#include "vtkGraph.h"
31
#include "vtkHierarchicalBoxDataIterator.h"
32
#include "vtkHierarchicalBoxDataSet.h"
33
34
35
36
37
38
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
39
#include "vtkSelectionNode.h"
40
41
42
43
#include "vtkSignedCharArray.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkTable.h"
44
#include "vtkUnsignedIntArray.h"
45
46
#include "vtkVariantArray.h"

47
#include <vtkstd/algorithm>
48
#include <vtkstd/iterator>
49
#include <vtkstd/map>
50
51
#include <vtkstd/set>
#include <vtkstd/vector>
52
53
54
55
56
57
58
59
60
61
62

#define VTK_CREATE(type,name) \
  vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

vtkCxxSetObjectMacro(vtkConvertSelection, ArrayNames, vtkStringArray);

vtkStandardNewMacro(vtkConvertSelection);
//----------------------------------------------------------------------------
vtkConvertSelection::vtkConvertSelection()
{
  this->SetNumberOfInputPorts(2);
63
  this->OutputType = vtkSelectionNode::INDICES;
64
  this->ArrayNames = 0;
65
  this->InputFieldType = -1;
66
  this->MatchAnyValues = false;
67
68
69
70
71
72
73
74
}

//----------------------------------------------------------------------------
vtkConvertSelection::~vtkConvertSelection()
{
  this->SetArrayNames(0);
}

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//----------------------------------------------------------------------------
void vtkConvertSelection::AddArrayName(const char* name)
{
  if (!this->ArrayNames)
    {
    this->ArrayNames = vtkStringArray::New();
    }
  this->ArrayNames->InsertNextValue(name);
}

//----------------------------------------------------------------------------
void vtkConvertSelection::ClearArrayNames()
{
  if (this->ArrayNames)
    {
    this->ArrayNames->Initialize();
    }
}

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
//----------------------------------------------------------------------------
void vtkConvertSelection::SetArrayName(const char* name)
{
  if (!this->ArrayNames)
    {
    this->ArrayNames = vtkStringArray::New();
    }
  this->ArrayNames->Initialize();
  this->ArrayNames->InsertNextValue(name);
}

//----------------------------------------------------------------------------
const char* vtkConvertSelection::GetArrayName()
{
  if (this->ArrayNames && this->ArrayNames->GetNumberOfValues() > 0)
    {
    return this->ArrayNames->GetValue(0);
    }
  return 0;
}

//----------------------------------------------------------------------------
int vtkConvertSelection::SelectTableFromTable(
  vtkTable* selTable, 
  vtkTable* dataTable, 
  vtkIdTypeArray* indices)
{
121
  vtkstd::set<vtkIdType> matching;
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  VTK_CREATE(vtkIdList, list);
  for (vtkIdType row = 0; row < selTable->GetNumberOfRows(); row++)
    {
    matching.clear();
    bool initialized = false;
    for (vtkIdType col = 0; col < selTable->GetNumberOfColumns(); col++)
      {
      vtkAbstractArray* from = selTable->GetColumn(col);
      vtkAbstractArray* to = dataTable->GetColumnByName(from->GetName());
      if (to)
        {
        to->LookupValue(selTable->GetValue(row, col), list);
        if (!initialized)
          {
          matching.insert(
            list->GetPointer(0), 
            list->GetPointer(0) + list->GetNumberOfIds());
          initialized = true;
          }
        else
          {
143
144
145
          vtkstd::set<vtkIdType> intersection;
          vtkstd::sort(list->GetPointer(0), list->GetPointer(0) + list->GetNumberOfIds());
          vtkstd::set_intersection(
146
            matching.begin(), matching.end(), 
147
            list->GetPointer(0), list->GetPointer(0) + list->GetNumberOfIds(), 
148
            vtkstd::inserter(intersection, intersection.begin()));
149
150
151
152
          matching = intersection;
          }
        }
      }
153
    vtkstd::set<vtkIdType>::iterator it, itEnd = matching.end();
154
155
156
157
    for (it = matching.begin(); it != itEnd; ++it)
      {
      indices->InsertNextValue(*it);
      }
158
159
160
161
162
    if (row % 100 == 0)
      {
      double progress = 0.8 * row / selTable->GetNumberOfRows();
      this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
      }
163
164
165
166
167
168
    }
  return 1;
}

//----------------------------------------------------------------------------
int vtkConvertSelection::ConvertToIndexSelection(
169
  vtkSelectionNode* input, 
170
  vtkDataSet* data,
171
  vtkSelectionNode* output)
172
{
173
174
175
176
  vtkSmartPointer<vtkSelection> tempInput =
    vtkSmartPointer<vtkSelection>::New();
  tempInput->AddNode(input);

177
  // Use the extraction filter to create an insidedness array.
178
179
  vtkSmartPointer<vtkExtractSelection> extract =
    vtkSmartPointer<vtkExtractSelection>::New();
180
  extract->PreserveTopologyOn();
181
  extract->SetInput(0, data);
182
  extract->SetInput(1, tempInput);
183
  extract->Update();
184
  vtkDataSet* const extracted = vtkDataSet::SafeDownCast(extract->GetOutput());
185
  
186
187
188
  output->SetContentType(vtkSelectionNode::INDICES);
  int type = input->GetFieldType();
  output->SetFieldType(type);
189
  vtkSignedCharArray* insidedness = 0;
190
  if (type == vtkSelectionNode::CELL)
191
192
193
194
    {
    insidedness = vtkSignedCharArray::SafeDownCast(
      extracted->GetCellData()->GetAbstractArray("vtkInsidedness"));
    }
195
  else if (type == vtkSelectionNode::POINT)
196
197
198
199
200
201
202
203
204
205
206
207
    {
    insidedness = vtkSignedCharArray::SafeDownCast(
      extracted->GetPointData()->GetAbstractArray("vtkInsidedness"));
    }
  else
    {
    vtkErrorMacro("Unknown field type");
    return 0;
    }
  
  if (!insidedness)
    {
208
    // Empty selection
209
210
211
212
    return 0;
    }
  
  // Convert the insidedness array into an index input.
213
214
  vtkSmartPointer<vtkIdTypeArray> indexArray =
    vtkSmartPointer<vtkIdTypeArray>::New();
215
216
217
218
219
220
221
222
223
224
225
  for (vtkIdType i = 0; i < insidedness->GetNumberOfTuples(); i++)
    {
    if (insidedness->GetValue(i) == 1)
      {
      indexArray->InsertNextValue(i);
      }
    }
  output->SetSelectionList(indexArray);
  return 1;
}

226
227
228
229
230
//----------------------------------------------------------------------------
int vtkConvertSelection::ConvertToBlockSelection(
  vtkSelection* input, vtkCompositeDataSet* data, vtkSelection* output)
{
  vtkstd::set<unsigned int> indices;
231
  for (unsigned int n = 0; n < input->GetNumberOfNodes(); ++n)
232
    {
233
    vtkSmartPointer<vtkSelectionNode> inputNode = input->GetNode(n);
234
    if (inputNode->GetContentType() == vtkSelectionNode::GLOBALIDS)
235
236
237
238
      {
      // global id selection does not have COMPOSITE_INDEX() key, so we convert
      // it to an index base selection, so that we can determine the composite
      // indices.
239
240
241
242
243
244
      vtkSmartPointer<vtkSelection> tempSel =
        vtkSmartPointer<vtkSelection>::New();
      tempSel->AddNode(inputNode);
      vtkSmartPointer<vtkSelection> tempOutput;
      tempOutput.TakeReference(vtkConvertSelection::ToIndexSelection(tempSel, data));
      inputNode = tempOutput->GetNode(0);
245
      }
246
247
248
    vtkInformation* properties = inputNode->GetProperties();
    if (properties->Has(vtkSelectionNode::CONTENT_TYPE()) &&
      properties->Has(vtkSelectionNode::COMPOSITE_INDEX()))
249
250
      {
      indices.insert(static_cast<unsigned int>(
251
          properties->Get(vtkSelectionNode::COMPOSITE_INDEX())));
252
      }
253
254
255
    else if (properties->Has(vtkSelectionNode::CONTENT_TYPE()) &&
      properties->Has(vtkSelectionNode::HIERARCHICAL_INDEX()) &&
      properties->Has(vtkSelectionNode::HIERARCHICAL_LEVEL()) && 
256
257
258
259
260
261
      data->IsA("vtkHierarchicalBoxDataSet"))
      {
       // convert hierarchical index to composite index.
       vtkHierarchicalBoxDataSet* hbox = static_cast<vtkHierarchicalBoxDataSet*>(data);
       indices.insert(
         hbox->GetFlatIndex(
262
263
           static_cast<unsigned int>(properties->Get(vtkSelectionNode::HIERARCHICAL_LEVEL())),
           static_cast<unsigned int>(properties->Get(vtkSelectionNode::HIERARCHICAL_INDEX()))));
264
265

      }
266
267
    }
 
268
269
  vtkSmartPointer<vtkUnsignedIntArray> selectionList =
    vtkSmartPointer<vtkUnsignedIntArray>::New();
270
271
  selectionList->SetNumberOfTuples(indices.size());
  vtkstd::set<unsigned int>::iterator siter;
272
  vtkIdType index = 0;
273
274
275
276
  for (siter = indices.begin(); siter != indices.end(); ++siter, ++index)
    {
    selectionList->SetValue(index, *siter);
    }
277
278
279
280
281
  vtkSmartPointer<vtkSelectionNode> outputNode =
    vtkSmartPointer<vtkSelectionNode>::New();
  outputNode->SetContentType(vtkSelectionNode::BLOCKS);
  outputNode->SetSelectionList(selectionList);
  output->AddNode(outputNode);
282
283
284
  return 1;
}

285
286
287
288
289
290
//----------------------------------------------------------------------------
int vtkConvertSelection::ConvertCompositeDataSet(
  vtkSelection* input,
  vtkCompositeDataSet* data,
  vtkSelection* output)
{
291
  // If this->OutputType == vtkSelectionNode::BLOCKS we just want to create a new
292
  // selection with the chosen block indices.
293
  if (this->OutputType == vtkSelectionNode::BLOCKS)
294
295
296
297
    {
    return this->ConvertToBlockSelection(input, data, output);
    }

298
  for (unsigned int n = 0; n < input->GetNumberOfNodes(); ++n)
299
    {
300
    vtkSelectionNode* inputNode = input->GetNode(n);
301

302
303
304
305
306
307
308
309
    // *  If input has no composite keys then it implies that it applies to all
    //    nodes in the data. If input has composite keys, output will have
    //    composite keys unless outputContentType == GLOBALIDS. 
    //    If input does not have composite keys, then composite
    //    keys are only added for outputContentType == INDICES, FRUSTUM and
    //    PEDIGREEIDS.
    bool has_composite_key =
      inputNode->GetProperties()->Has(vtkSelectionNode::COMPOSITE_INDEX()) != 0;
310

311
312
313
    unsigned int composite_index = has_composite_key?
      static_cast<unsigned int>(
          inputNode->GetProperties()->Get(vtkSelectionNode::COMPOSITE_INDEX())) : 0;
314

315
316
317
    bool has_hieararchical_key = 
      inputNode->GetProperties()->Has(vtkSelectionNode::HIERARCHICAL_INDEX()) != 0 &&
      inputNode->GetProperties()->Has(vtkSelectionNode::HIERARCHICAL_LEVEL()) != 0;
318

319
320
321
322
323
324
    unsigned int hierarchical_level = has_hieararchical_key?
      static_cast<unsigned int>(
          inputNode->GetProperties()->Get(vtkSelectionNode::HIERARCHICAL_LEVEL())) : 0;
    unsigned int hierarchical_index = has_hieararchical_key?
      static_cast<unsigned int>(
          inputNode->GetProperties()->Get(vtkSelectionNode::HIERARCHICAL_INDEX())) : 0;
325
  
326
327
    vtkSmartPointer<vtkCompositeDataIterator> iter;
    iter.TakeReference(data->NewIterator());
328

329
330
    vtkHierarchicalBoxDataIterator* hbIter =
      vtkHierarchicalBoxDataIterator::SafeDownCast(iter);
331

332
    for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
333
      {
334
335
336
337
338
339
      if (has_hieararchical_key && hbIter &&
          (hbIter->GetCurrentLevel() != hierarchical_level ||
           hbIter->GetCurrentIndex() != hierarchical_index))
        {
        continue;
        }
340

341
342
      if (has_composite_key &&
          iter->GetCurrentFlatIndex() != composite_index)
343
        {
344
        continue;
345
        }
346
347
348
349
350
351
352

      vtkSmartPointer<vtkSelection> outputNodes =
        vtkSmartPointer<vtkSelection>::New();
      vtkSmartPointer<vtkSelection> tempSel =
        vtkSmartPointer<vtkSelection>::New();
      tempSel->AddNode(inputNode);
      if (!this->Convert(tempSel, iter->GetCurrentDataObject(), outputNodes))
353
        {
354
        return 0;
355
        }
356
357

      for (unsigned int j = 0; j < outputNodes->GetNumberOfNodes(); ++j)
358
        {
359
360
361
362
363
364
        vtkSelectionNode* outputNode = outputNodes->GetNode(j);
        if ((has_hieararchical_key || has_composite_key || 
            this->OutputType == vtkSelectionNode::INDICES ||
            this->OutputType == vtkSelectionNode::PEDIGREEIDS || 
            this->OutputType == vtkSelectionNode::FRUSTUM) &&
            this->OutputType != vtkSelectionNode::GLOBALIDS) 
365
          {
366
367
          outputNode->GetProperties()->Set(vtkSelectionNode::COMPOSITE_INDEX(),
              iter->GetCurrentFlatIndex());
368

369
370
371
372
373
374
375
376
377
378
379
380
          if (has_hieararchical_key && hbIter)
            {
            outputNode->GetProperties()->Set(vtkSelectionNode::HIERARCHICAL_LEVEL(),
                hierarchical_level);
            outputNode->GetProperties()->Set(vtkSelectionNode::HIERARCHICAL_INDEX(),
                hierarchical_index);
            }
          }
        output->Union(outputNode);
        } // for each output node
      } // for each block
    } // for each input selection node
381
382
383
384

  return 1;
}

385
386
387
388
389
390
//----------------------------------------------------------------------------
int vtkConvertSelection::Convert(
  vtkSelection* input,
  vtkDataObject* data,
  vtkSelection* output)
{
391
  for (unsigned int n = 0; n < input->GetNumberOfNodes(); ++n)
392
    {
393
394
395
396
397
398
    vtkSelectionNode* inputNode = input->GetNode(n);
    vtkSmartPointer<vtkSelectionNode> outputNode =
      vtkSmartPointer<vtkSelectionNode>::New();

    outputNode->ShallowCopy(inputNode);
    outputNode->SetContentType(this->OutputType);
399
  
400
401
402
403
    // If it is the same type, we are done
    if (inputNode->GetContentType() != vtkSelectionNode::VALUES &&
        inputNode->GetContentType() != vtkSelectionNode::THRESHOLDS &&
        inputNode->GetContentType() == this->OutputType)
404
      {
405
406
      output->Union(outputNode);
      continue;
407
408
      }
  
409
410
411
412
    // If the input is a values or thresholds selection, we need array names
    // on the selection arrays to perform the selection.
    if (inputNode->GetContentType() == vtkSelectionNode::VALUES ||
        inputNode->GetContentType() == vtkSelectionNode::THRESHOLDS)
413
      {
414
415
      vtkFieldData* selData = inputNode->GetSelectionData();
      for (int i = 0; i < selData->GetNumberOfArrays(); i++)
416
        {
417
418
419
420
421
        if (!selData->GetAbstractArray(i)->GetName())
          {
          vtkErrorMacro("Array name must be specified for values or thresholds selection.");
          return 0;
          }
422
423
424
        }
      }
  
425
426
427
    // If the output is a threshold selection, we need exactly one array name.
    if (this->OutputType == vtkSelectionNode::THRESHOLDS &&
        (this->ArrayNames == 0 || this->ArrayNames->GetNumberOfValues() != 1))
428
      {
429
      vtkErrorMacro("One array name must be specified for thresholds selection.");
430
431
      return 0;
      }
432
433
434
435
  
    // If the output is a values selection, we need at lease one array name.
    if (this->OutputType == vtkSelectionNode::VALUES &&
        (this->ArrayNames == 0 || this->ArrayNames->GetNumberOfValues() == 0))
436
      {
437
438
      vtkErrorMacro("At least one array name must be specified for values selection.");
      return 0;
439
      }
440
441
442
443
444
445
446
  
    // If we are converting a thresholds or values selection to
    // a selection on the same arrays, we are done.
    if ((inputNode->GetContentType() == vtkSelectionNode::VALUES ||
        inputNode->GetContentType() == vtkSelectionNode::THRESHOLDS) &&
        this->OutputType == inputNode->GetContentType() &&
        this->ArrayNames->GetNumberOfValues() == inputNode->GetSelectionData()->GetNumberOfArrays())
447
      {
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
      bool same = true;
      vtkFieldData* selData = inputNode->GetSelectionData();
      for (int i = 0; i < selData->GetNumberOfArrays(); i++)
        {
        if (strcmp(selData->GetAbstractArray(i)->GetName(), this->ArrayNames->GetValue(i)))
          {
          same = false;
          break;
          }
        }
      if (same)
        {
        output->Union(outputNode);
        continue;
        }
463
      }
464
465
466
467
468
469

    // Check whether we can do the conversion
    if (this->OutputType != vtkSelectionNode::VALUES &&
        this->OutputType != vtkSelectionNode::GLOBALIDS &&
        this->OutputType != vtkSelectionNode::PEDIGREEIDS &&
        this->OutputType != vtkSelectionNode::INDICES)
470
      {
471
472
      vtkErrorMacro("Cannot convert to type " << this->OutputType 
        << " unless input type matches.");
473
474
      return 0;
      }
475
476
477
478
479
  
    // Get the correct field data
    vtkFieldData* fd = 0;
    vtkDataSetAttributes* dsa = 0;
    if (vtkDataSet::SafeDownCast(data))
480
      {
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
      if (!inputNode->GetProperties()->Has(vtkSelectionNode::FIELD_TYPE()) ||
          inputNode->GetFieldType() == vtkSelectionNode::CELL)
        {
        dsa = vtkDataSet::SafeDownCast(data)->GetCellData();
        }
      else if (inputNode->GetFieldType() == vtkSelectionNode::POINT)
        {
        dsa = vtkDataSet::SafeDownCast(data)->GetPointData();
        }
      else if (inputNode->GetFieldType() == vtkSelectionNode::FIELD)
        {
        fd = data->GetFieldData();
        }
      else
        {
        vtkErrorMacro("Inappropriate selection type for a vtkDataSet");
        return 0;
        }
499
      }
500
    else if (vtkGraph::SafeDownCast(data))
501
      {
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
      if (!inputNode->GetProperties()->Has(vtkSelectionNode::FIELD_TYPE()) ||
          inputNode->GetFieldType() == vtkSelectionNode::EDGE)
        {
        dsa = vtkGraph::SafeDownCast(data)->GetEdgeData();
        }
      else if (inputNode->GetFieldType() == vtkSelectionNode::VERTEX)
        {
        dsa = vtkGraph::SafeDownCast(data)->GetVertexData();
        }
      else if (inputNode->GetFieldType() == vtkSelectionNode::FIELD)
        {
        fd = data->GetFieldData();
        }
      else
        {
        vtkErrorMacro("Inappropriate selection type for a vtkGraph");
        return 0;
        }
520
      }
521
    else if (vtkTable::SafeDownCast(data))
522
      {
523
      if (!inputNode->GetProperties()->Has(vtkSelectionNode::FIELD_TYPE()) ||
524
          inputNode->GetFieldType() != vtkSelectionNode::FIELD)
525
526
527
528
529
        {
        dsa = vtkTable::SafeDownCast(data)->GetRowData();
        }
      else
        {
530
        fd = data->GetFieldData();
531
        }
532
533
534
      }
    else
      {
535
536
537
538
539
540
541
542
543
544
      if (!inputNode->GetProperties()->Has(vtkSelectionNode::FIELD_TYPE()) ||
          inputNode->GetFieldType() == vtkSelectionNode::FIELD)
        {
        fd = data->GetFieldData();
        }
      else
        {
        vtkErrorMacro("Inappropriate selection type for a non-dataset, non-graph");
        return 0;
        }
545
546
      }
  
547
548
549
550
551
552
    //
    // First, convert the selection to a list of indices
    //
    
    vtkSmartPointer<vtkIdTypeArray> indices =
      vtkSmartPointer<vtkIdTypeArray>::New();
553
  
554
555
    if (inputNode->GetContentType() == vtkSelectionNode::FRUSTUM || 
        inputNode->GetContentType() == vtkSelectionNode::LOCATIONS)
556
      {
557
      if (!vtkDataSet::SafeDownCast(data))
558
        {
559
560
        vtkErrorMacro("Can only convert from frustum or locations if the input is a vtkDataSet");
        return 0;
561
        }
562
563
564
565
566
567
      // Use the extract selection filter to create an index selection
      vtkSmartPointer<vtkSelectionNode> indexNode =
        vtkSmartPointer<vtkSelectionNode>::New();
      this->ConvertToIndexSelection(inputNode, vtkDataSet::SafeDownCast(data), indexNode);
      // TODO: We should shallow copy this, but the method is not defined.
      indices->DeepCopy(indexNode->GetSelectionList());
568
      }
569
    else if (inputNode->GetContentType() == vtkSelectionNode::THRESHOLDS)
570
      {
571
572
573
574
575
576
577
      vtkDoubleArray *lims = vtkDoubleArray::SafeDownCast(inputNode->GetSelectionList());
      if (!lims)
        {
        vtkErrorMacro("Thresholds selection requires vtkDoubleArray selection list.");
        return 0;
        }
      vtkDataArray *dataArr = 0;
578
      if (dsa)
579
        {
580
        dataArr = vtkDataArray::SafeDownCast(dsa->GetAbstractArray(lims->GetName()));
581
        }
582
      else if (fd)
583
        {
584
        dataArr = vtkDataArray::SafeDownCast(fd->GetAbstractArray(lims->GetName()));
585
        }
586
      if (!dataArr)
587
        {
588
589
590
591
592
593
        vtkErrorMacro("Could not find vtkDataArray for thresholds selection.");
        return 0;
        }
      for (vtkIdType id = 0; id < dataArr->GetNumberOfTuples(); id++)
        {
        int keepPoint = vtkExtractSelectedThresholds::EvaluateValue(dataArr, id, lims);
594
        if (keepPoint)
595
596
597
          {
          indices->InsertNextValue(id);
          }
598
599
        }
      }
600
    else if (inputNode->GetContentType() == vtkSelectionNode::INDICES)
601
      {
602
603
      // TODO: We should shallow copy this, but the method is not defined.
      indices->DeepCopy(inputNode->GetSelectionList());
604
      }
605
    else if (inputNode->GetContentType() == vtkSelectionNode::VALUES)
606
      {
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
      vtkFieldData* selData = inputNode->GetSelectionData();
      vtkSmartPointer<vtkTable> selTable =
        vtkSmartPointer<vtkTable>::New();
      selTable->GetRowData()->ShallowCopy(selData);
      vtkSmartPointer<vtkTable> dataTable =
        vtkSmartPointer<vtkTable>::New();
      for (vtkIdType col = 0; col < selTable->GetNumberOfColumns(); col++)
        {
        vtkAbstractArray* dataArr = 0;
        if (dsa)
          {
          dataArr = dsa->GetAbstractArray(selTable->GetColumn(col)->GetName());
          }
        else if (fd)
          {
          dataArr = fd->GetAbstractArray(selTable->GetColumn(col)->GetName());
          }
        if (dataArr)
          {
          dataTable->AddColumn(dataArr);
          }
        }
      // Select rows matching selTable from the input dataTable
      // and put the matches in the index array.
      this->SelectTableFromTable(selTable, dataTable, indices);
632
      }
633
634
    else if (inputNode->GetContentType() == vtkSelectionNode::PEDIGREEIDS ||
        inputNode->GetContentType() == vtkSelectionNode::GLOBALIDS)
635
      {
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
      // Get the appropriate array
      vtkAbstractArray* selArr = inputNode->GetSelectionList();
      vtkAbstractArray* dataArr = 0;
      if (dsa && inputNode->GetContentType() == vtkSelectionNode::PEDIGREEIDS)
        {
        dataArr = dsa->GetPedigreeIds();
        }
      else if (dsa && inputNode->GetContentType() == vtkSelectionNode::GLOBALIDS)
        {
        dataArr = dsa->GetGlobalIds();
        }
      else if (fd && selArr->GetName())
        {
        // Since data objects only have field data which does not have attributes,
        // use the array name to try to match the incoming selection's array.
        dataArr = fd->GetAbstractArray(selArr->GetName());
        }
      else
        {
        vtkErrorMacro("Tried to use array name to match global or pedigree ids on data object,"
            << "but name not set on selection array.");
        return 0;
        }
659

660
661
      // Check array compatibility
      if (!dataArr)
662
        {
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
        vtkErrorMacro("Selection array does not exist in input dataset.");
        return 0;
        }

      // Handle the special case where we have a domain array.
      vtkStringArray* domainArr = dsa ? vtkStringArray::SafeDownCast(dsa->GetAbstractArray("domain")) : 0;
      if (inputNode->GetContentType() == vtkSelectionNode::PEDIGREEIDS &&
          domainArr && selArr->GetName())
        {
        // Perform the lookup, keeping only those items in the correct domain.
        vtkStdString domain = selArr->GetName();
        vtkIdType numTuples = selArr->GetNumberOfTuples();
        vtkSmartPointer<vtkIdList> list =
          vtkSmartPointer<vtkIdList>::New();
        for (vtkIdType i = 0; i < numTuples; i++)
678
          {
679
680
681
          dataArr->LookupValue(selArr->GetVariantValue(i), list);
          vtkIdType numIds = list->GetNumberOfIds();
          for (vtkIdType j = 0; j < numIds; j++)
682
            {
683
684
685
686
            if (domainArr->GetValue(list->GetId(j)) == domain)
              {
              indices->InsertNextValue(list->GetId(j));
              }
687
688
689
            }
          }
        }
690
691
692
693
694
      // If no domain array, the name of the selection and data arrays
      // must match (if they exist).
      else if (inputNode->GetContentType() != vtkSelectionNode::PEDIGREEIDS ||
               !selArr->GetName() || !dataArr->GetName() ||
               !strcmp(selArr->GetName(), dataArr->GetName()))
695
        {
696
697
698
699
700
        // Perform the lookup
        vtkIdType numTuples = selArr->GetNumberOfTuples();
        vtkSmartPointer<vtkIdList> list =
          vtkSmartPointer<vtkIdList>::New();
        for (vtkIdType i = 0; i < numTuples; i++)
701
          {
702
703
704
705
706
707
          dataArr->LookupValue(selArr->GetVariantValue(i), list);
          vtkIdType numIds = list->GetNumberOfIds();
          for (vtkIdType j = 0; j < numIds; j++)
            {
            indices->InsertNextValue(list->GetId(j));
            }
708
          }
709
        }
710
      }
711
712
713

    double progress = 0.8;
    this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
714
  
715
716
717
718
    //
    // Now that we have the list of indices, convert the selection by indexing
    // values in another array.
    //
719

720
721
    // If it is an index selection, we are done.
    if (this->OutputType == vtkSelectionNode::INDICES)
722
      {
723
724
725
726
727
728
729
730
731
      outputNode->SetSelectionList(indices);
      output->Union(outputNode);
      continue;
      }
  
    vtkIdType numOutputArrays = 1;
    if (this->OutputType == vtkSelectionNode::VALUES)
      {
      numOutputArrays = this->ArrayNames->GetNumberOfValues();
732
733
      }

734
735
736
737
    // Handle the special case where we have a pedigree id selection with a domain array.
    vtkStringArray* outputDomainArr = vtkStringArray::SafeDownCast(
        dsa->GetAbstractArray("domain"));
    if (this->OutputType == vtkSelectionNode::PEDIGREEIDS && outputDomainArr)
738
      {
739
740
741
      vtkAbstractArray* outputDataArr = dsa->GetPedigreeIds();
      // Check array existence.
      if (!outputDataArr)
742
        {
743
744
        vtkErrorMacro("Output selection array does not exist in input dataset.");
        return 0;
745
        }
746
747
748
749
750

      vtkstd::map<vtkStdString, vtkSmartPointer<vtkAbstractArray> > domainArrays;
      vtkIdType numTuples = outputDataArr->GetNumberOfTuples();
      vtkIdType numIndices = indices->GetNumberOfTuples();
      for (vtkIdType i = 0; i < numIndices; ++i)
751
        {
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
        vtkIdType index = indices->GetValue(i);
        if (index >= numTuples)
          {
          continue;
          }
        vtkStdString domain = outputDomainArr->GetValue(index);
        if (domainArrays.count(domain) == 0)
          {
          domainArrays[domain].TakeReference(
              vtkAbstractArray::CreateArray(outputDataArr->GetDataType()));
          domainArrays[domain]->SetName(domain);
          }
        vtkAbstractArray* domainArr = domainArrays[domain];
        domainArr->InsertNextTuple(index, outputDataArr);
        if (i % 1000 == 0)
          {
          progress = 0.8 + (0.2 * i / numIndices);
          this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
          }
771
        }
772
773
774
775
      vtkstd::map<vtkStdString, vtkSmartPointer<vtkAbstractArray> >::iterator it, itEnd;
      it = domainArrays.begin();
      itEnd = domainArrays.end();
      for (; it != itEnd; ++it)
776
        {
777
778
779
780
781
        vtkSmartPointer<vtkSelectionNode> node = vtkSmartPointer<vtkSelectionNode>::New();
        node->SetContentType(vtkSelectionNode::PEDIGREEIDS);
        node->SetFieldType(inputNode->GetFieldType());
        node->SetSelectionList(it->second);
        output->Union(node);
782
        }
783
      continue;
784
      }
785
  
786
787
788
789
790
791
792
    vtkSmartPointer<vtkDataSetAttributes> outputData =
      vtkSmartPointer<vtkDataSetAttributes>::New();
    for (vtkIdType ind = 0; ind < numOutputArrays; ind++)
      {  
      // Find the output array where to get the output selection values.
      vtkAbstractArray* outputDataArr = 0;
      if (dsa && this->OutputType == vtkSelectionNode::VALUES)
793
        {
794
795
796
797
798
799
800
801
802
803
804
805
806
        outputDataArr = dsa->GetAbstractArray(this->ArrayNames->GetValue(ind));
        }
      else if (fd && this->OutputType == vtkSelectionNode::VALUES)
        {
        outputDataArr = fd->GetAbstractArray(this->ArrayNames->GetValue(ind));
        }
      else if (dsa && this->OutputType == vtkSelectionNode::PEDIGREEIDS)
        {
        outputDataArr = dsa->GetPedigreeIds();
        }
      else if (dsa && this->OutputType == vtkSelectionNode::GLOBALIDS)
        {
        outputDataArr = dsa->GetGlobalIds();
807
808
809
        }
      else
        {
810
811
812
813
814
815
        // TODO: Make this error go away.
        vtkErrorMacro("BUG: Currently you can only specify pedigree and global ids on a vtkDataSet.");
        return 0;
        }
    
      // Check array existence.
Jeff Baumes's avatar
Jeff Baumes committed
816
817
818
819
820
821
822
823
      if (outputDataArr)
        {
        // Put the array's values into the selection.
        vtkAbstractArray* outputArr = vtkAbstractArray::CreateArray(outputDataArr->GetDataType());
        outputArr->SetName(outputDataArr->GetName());
        vtkIdType numTuples = outputDataArr->GetNumberOfTuples();
        vtkIdType numIndices = indices->GetNumberOfTuples();
        for (vtkIdType i = 0; i < numIndices; ++i)
824
          {
Jeff Baumes's avatar
Jeff Baumes committed
825
826
827
828
829
830
831
832
833
834
          vtkIdType index = indices->GetValue(i);
          if (index < numTuples)
            {
            outputArr->InsertNextTuple(index, outputDataArr);
            }
          if (i % 1000 == 0)
            {
            progress = 0.8 + (0.2 * (ind*numIndices + i)) / (numOutputArrays*numIndices);
            this->InvokeEvent(vtkCommand::ProgressEvent, &progress);
            }
835
          }
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850

        if(this->MatchAnyValues)
          {
          vtkSmartPointer<vtkSelectionNode> outNode =
            vtkSmartPointer<vtkSelectionNode>::New();
          outNode->ShallowCopy(inputNode);
          outNode->SetContentType(this->OutputType);
          outNode->SetSelectionList(outputArr);
          output->AddNode(outNode);
          }
        else
          {
          outputData->AddArray(outputArr);
          }

Jeff Baumes's avatar
Jeff Baumes committed
851
        outputArr->Delete();
852
        }
853
      }
Jeff Baumes's avatar
Jeff Baumes committed
854
855
856
857
858
859
860
861
862
863
864

    // If there are no output arrays, just add a dummy one so
    // that the selection list is not null.
    if (outputData->GetNumberOfArrays() == 0)
      {
      vtkSmartPointer<vtkIdTypeArray> arr =
        vtkSmartPointer<vtkIdTypeArray>::New();
      arr->SetName("Empty");
      outputData->AddArray(arr);
      }

865
866
    outputNode->SetSelectionData(outputData);
    output->Union(outputNode);
867
868
869
870
871
872
873
874
875
876
877
    }
  return 1;
}

//----------------------------------------------------------------------------
int vtkConvertSelection::RequestData(
  vtkInformation* vtkNotUsed(request),
  vtkInformationVector** inputVector,
  vtkInformationVector* outputVector)
{
  vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
878
  vtkSelection* origInput = vtkSelection::SafeDownCast(
879
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
880
881
882
883
884
885
886
887
888
889

  vtkSmartPointer<vtkSelection> input = vtkSmartPointer<vtkSelection>::New();
  input->ShallowCopy(origInput);
  if (this->InputFieldType != -1)
    {
    for (unsigned int i = 0; i < input->GetNumberOfNodes(); ++i)
      {
      input->GetNode(i)->SetFieldType(this->InputFieldType);
      }
    }
890
891
892
893
894
895
896
  
  vtkInformation* dataInfo = inputVector[1]->GetInformationObject(0);
  vtkDataObject* data = dataInfo->Get(vtkDataObject::DATA_OBJECT());
  
  vtkInformation* outInfo = outputVector->GetInformationObject(0);
  vtkSelection* output = vtkSelection::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));
897
898
899
900
901
  if (data && data->IsA("vtkCompositeDataSet"))
    {
    return this->ConvertCompositeDataSet(input,
      static_cast<vtkCompositeDataSet*>(data), output);
    }
902
903
904
905
  
  return this->Convert(input, data, output);
}

906
907
908
909
910
911
//----------------------------------------------------------------------------
void vtkConvertSelection::SetDataObjectConnection(vtkAlgorithmOutput* in)
{
  this->SetInputConnection(1, in);
}

912
913
914
915
916
917
918
919
920
921
922
//----------------------------------------------------------------------------
int vtkConvertSelection::FillInputPortInformation(
  int port, vtkInformation* info)
{
  // now add our info
  if (port == 0)
    {
    info->Set(vtkConvertSelection::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection");
    }
  else if (port == 1)
    {
923
924
    // Can convert from a vtkDataSet, vtkGraph, or vtkTable
    info->Remove(vtkConvertSelection::INPUT_REQUIRED_DATA_TYPE());
925
    info->Append(vtkConvertSelection::INPUT_REQUIRED_DATA_TYPE(), "vtkCompositeDataSet");
926
927
928
    info->Append(vtkConvertSelection::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
    info->Append(vtkConvertSelection::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
    info->Append(vtkConvertSelection::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
929
930
931
932
    }
  return 1;
}

933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
//----------------------------------------------------------------------------
void vtkConvertSelection::GetSelectedItems(
  vtkSelection* input,
  vtkDataObject* data,
  int fieldType,
  vtkIdTypeArray* indices)
{
  vtkSelection* indexSel = vtkConvertSelection::ToSelectionType(input, data, vtkSelectionNode::INDICES);
  for (unsigned int n = 0; n < indexSel->GetNumberOfNodes(); ++n)
    {
    vtkSelectionNode* node = indexSel->GetNode(n);
    vtkIdTypeArray* list = vtkIdTypeArray::SafeDownCast(node->GetSelectionList());
    if (node->GetFieldType() == fieldType && node->GetContentType() == vtkSelectionNode::INDICES && list)
      {
      for (vtkIdType i = 0; i < list->GetNumberOfTuples(); ++i)
        {
        vtkIdType cur = list->GetValue(i);
        if (indices->LookupValue(cur) < 0)
          {
          indices->InsertNextValue(cur);
          }
        }
      }
    }
957
  indexSel->Delete();
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
}

//----------------------------------------------------------------------------
void vtkConvertSelection::GetSelectedVertices(
  vtkSelection* input,
  vtkGraph* data,
  vtkIdTypeArray* indices)
{
  vtkConvertSelection::GetSelectedItems(input, data, vtkSelectionNode::VERTEX, indices);
}

//----------------------------------------------------------------------------
void vtkConvertSelection::GetSelectedEdges(
  vtkSelection* input,
  vtkGraph* data,
  vtkIdTypeArray* indices)
{
  vtkConvertSelection::GetSelectedItems(input, data, vtkSelectionNode::EDGE, indices);
}

//----------------------------------------------------------------------------
void vtkConvertSelection::GetSelectedPoints(
  vtkSelection* input,
  vtkDataSet* data,
  vtkIdTypeArray* indices)
{
  vtkConvertSelection::GetSelectedItems(input, data, vtkSelectionNode::POINT, indices);
}

//----------------------------------------------------------------------------
void vtkConvertSelection::GetSelectedCells(
  vtkSelection* input,
  vtkDataSet* data,
  vtkIdTypeArray* indices)
{
  vtkConvertSelection::GetSelectedItems(input, data, vtkSelectionNode::CELL, indices);
}

//----------------------------------------------------------------------------
void vtkConvertSelection::GetSelectedRows(
  vtkSelection* input,
  vtkTable* data,
  vtkIdTypeArray* indices)
{
  vtkConvertSelection::GetSelectedItems(input, data, vtkSelectionNode::ROW, indices);
}

1005
1006
1007
1008
1009
//----------------------------------------------------------------------------
vtkSelection* vtkConvertSelection::ToIndexSelection(
  vtkSelection* input, 
  vtkDataObject* data)
{
1010
  return vtkConvertSelection::ToSelectionType(input, data, vtkSelectionNode::INDICES);
1011
1012
1013
1014
1015
1016
1017
}

//----------------------------------------------------------------------------
vtkSelection* vtkConvertSelection::ToGlobalIdSelection(
  vtkSelection* input, 
  vtkDataObject* data)
{
1018
  return vtkConvertSelection::ToSelectionType(input, data, vtkSelectionNode::GLOBALIDS);
1019
1020
1021
1022
1023
1024
1025
}

//----------------------------------------------------------------------------
vtkSelection* vtkConvertSelection::ToPedigreeIdSelection(
  vtkSelection* input, 
  vtkDataObject* data)
{
1026
  return vtkConvertSelection::ToSelectionType(input, data, vtkSelectionNode::PEDIGREEIDS);
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
}

//----------------------------------------------------------------------------
vtkSelection* vtkConvertSelection::ToValueSelection(
  vtkSelection* input, 
  vtkDataObject* data, 
  const char* arrayName)
{
  VTK_CREATE(vtkStringArray, names);
  names->InsertNextValue(arrayName);
1037
  return vtkConvertSelection::ToSelectionType(input, data, vtkSelectionNode::VALUES, names);
1038
1039
1040
1041
1042
1043
1044
1045
}

//----------------------------------------------------------------------------
vtkSelection* vtkConvertSelection::ToValueSelection(
  vtkSelection* input, 
  vtkDataObject* data, 
  vtkStringArray* arrayNames)
{
1046
  return vtkConvertSelection::ToSelectionType(input, data, vtkSelectionNode::VALUES, arrayNames);
1047
1048
1049
1050
1051
1052
1053
}

//----------------------------------------------------------------------------
vtkSelection* vtkConvertSelection::ToSelectionType(
  vtkSelection* input, 
  vtkDataObject* data, 
  int type, 
1054
1055
  vtkStringArray* arrayNames,
  int inputFieldType)
1056
1057
{
  VTK_CREATE(vtkConvertSelection, convert);
1058
1059
  vtkDataObject* dataCopy = data->NewInstance();
  dataCopy->ShallowCopy(data);
1060
  VTK_CREATE(vtkSelection, inputCopy);
1061
  inputCopy->ShallowCopy(input);
1062
1063
  convert->SetInput(0, inputCopy);
  convert->SetInput(1, dataCopy);
1064
1065
  convert->SetOutputType(type);
  convert->SetArrayNames(arrayNames);
1066
  convert->SetInputFieldType(inputFieldType);
1067
1068
1069
  convert->Update();
  vtkSelection* output = convert->GetOutput();
  output->Register(0);
1070
  dataCopy->Delete();
1071
1072
1073
1074
1075
1076
1077
  return output;
}

//----------------------------------------------------------------------------
void vtkConvertSelection::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
1078
  os << indent << "InputFieldType: " << this->InputFieldType << endl;
1079
  os << indent << "OutputType: " << this->OutputType << endl;
1080
  os << indent << "MatchAnyValues: " << (this->MatchAnyValues ? "true" : "false") << endl;
1081
1082
1083
1084
1085
1086
  os << indent << "ArrayNames: " << (this->ArrayNames ? "" : "(null)") << endl;
  if (this->ArrayNames)
    {
    this->ArrayNames->PrintSelf(os, indent.GetNextIndent());
    }
}