1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkExtractSelectionBase.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkExtractSelectionBase.h"
16 
17 #include "vtkGraph.h"
18 #include "vtkInformation.h"
19 #include "vtkInformationVector.h"
20 #include "vtkMultiBlockDataSet.h"
21 #include "vtkObjectFactory.h"
22 #include "vtkTable.h"
23 #include "vtkUnstructuredGrid.h"
24 
25 //----------------------------------------------------------------------------
vtkExtractSelectionBase()26 vtkExtractSelectionBase::vtkExtractSelectionBase()
27 {
28   this->PreserveTopology = 0;
29   this->SetNumberOfInputPorts(2);
30 }
31 
32 //----------------------------------------------------------------------------
33 vtkExtractSelectionBase::~vtkExtractSelectionBase() = default;
34 
35 //----------------------------------------------------------------------------
FillInputPortInformation(int port,vtkInformation * info)36 int vtkExtractSelectionBase::FillInputPortInformation(
37   int port, vtkInformation* info)
38 {
39   if (port==0)
40   {
41     // Cannot work with composite datasets.
42     info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
43     info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
44     info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGraph");
45     info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
46   }
47   else
48   {
49     info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkSelection");
50     info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 1);
51   }
52   return 1;
53 }
54 
55 //----------------------------------------------------------------------------
56 // Needed because parent class sets output type to input type
57 // and we sometimes want to change it to make an UnstructuredGrid regardless of
58 // input type
RequestDataObject(vtkInformation *,vtkInformationVector ** inputVector,vtkInformationVector * outputVector)59 int vtkExtractSelectionBase::RequestDataObject(
60   vtkInformation*,
61   vtkInformationVector** inputVector ,
62   vtkInformationVector* outputVector)
63 {
64   vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
65   if (!inInfo)
66   {
67     return 0;
68   }
69 
70   vtkDataSet *input = vtkDataSet::GetData(inInfo);
71   vtkInformation* outInfo = outputVector->GetInformationObject(0);
72 
73   if (input)
74   {
75     int passThrough = this->PreserveTopology? 1 : 0;
76 
77     vtkDataSet *output = vtkDataSet::GetData(outInfo);
78     if (!output ||
79       (passThrough && !output->IsA(input->GetClassName())) ||
80       (!passThrough && !output->IsA("vtkUnstructuredGrid")))
81     {
82       vtkDataSet* newOutput = nullptr;
83       if (!passThrough)
84       {
85         // The mesh will be modified.
86         newOutput = vtkUnstructuredGrid::New();
87       }
88       else
89       {
90         // The mesh will not be modified.
91         newOutput = input->NewInstance();
92       }
93       outInfo->Set(vtkDataObject::DATA_OBJECT(), newOutput);
94       newOutput->Delete();
95     }
96     return 1;
97   }
98 
99   vtkGraph *graphInput = vtkGraph::GetData(inInfo);
100   if (graphInput)
101   {
102     // Accept graph input, but we don't produce the correct extracted
103     // graph as output yet.
104     return 1;
105   }
106 
107   vtkTable *tableInput = vtkTable::GetData(inInfo);
108   if (tableInput)
109   {
110     vtkTable *output = vtkTable::GetData(outInfo);
111     if (!output)
112     {
113       output = vtkTable::New();
114       outInfo->Set(vtkDataObject::DATA_OBJECT(), output);
115       output->Delete();
116     }
117     return 1;
118   }
119 
120   return 0;
121 }
122 
123 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)124 void vtkExtractSelectionBase::PrintSelf(ostream& os, vtkIndent indent)
125 {
126   this->Superclass::PrintSelf(os, indent);
127   os << indent << "PreserveTopology: " << this->PreserveTopology << endl;
128 }
129