TTK
Loading...
Searching...
No Matches
ttkForEach.cpp
Go to the documentation of this file.
1#include <ttkForEach.h>
2
3#include <vtkDataSet.h>
4#include <vtkInformation.h>
5
6#include <vtkCellData.h>
7#include <vtkDoubleArray.h>
8#include <vtkFieldData.h>
9#include <vtkInformationVector.h>
10#include <vtkMultiBlockDataSet.h>
11#include <vtkPointData.h>
12#include <vtkStreamingDemandDrivenPipeline.h>
13#include <vtkTable.h>
14
16
18 this->setDebugMsgPrefix("ForEach");
19
20 this->SetNumberOfInputPorts(1);
21 this->SetNumberOfOutputPorts(1);
22}
23
24ttkForEach::~ttkForEach() = default;
25
26static int
27 addRecursivelyToFieldData(vtkDataObject *object,
28 const vtkSmartPointer<vtkDataArray> &array) {
29 object->GetFieldData()->AddArray(array);
30 if(object->IsA("vtkMultiBlockDataSet")) {
31 auto objectAsMB = (vtkMultiBlockDataSet *)object;
32 for(size_t i = 0; i < objectAsMB->GetNumberOfBlocks(); i++)
33 addRecursivelyToFieldData(objectAsMB->GetBlock(i), array);
34 }
35 return 1;
36}
37
38int ttkForEach::RequestData(vtkInformation *request,
39 vtkInformationVector **inputVector,
40 vtkInformationVector *outputVector) {
41 // Get Input and Output
42 auto input = vtkDataObject::GetData(inputVector[0]);
43
44 if(this->LastInput != input || this->IterationIdx >= this->IterationNumber) {
45 this->LastInput = input;
46 this->IterationIdx = 0;
47 }
48
49 // Determine Mode
50 auto mode = this->GetExtractionMode();
51 if(mode == EXTRACTION_MODE::AUTO) {
52 if(input->IsA("vtkMultiBlockDataSet"))
54 else if(input->IsA("vtkTable"))
56 else {
57 this->printErr("Unable to automatically determine iteration mode.");
58 return 0;
59 }
60 }
61
62 // Get Iteration Bounds
63 switch(mode) {
65 auto inputAsMB = vtkMultiBlockDataSet::SafeDownCast(input);
66 if(!inputAsMB) {
67 this->printErr(
68 "Block iteration requires 'vtkMultiBlockDataSet' input.");
69 return 0;
70 }
71 this->IterationNumber = inputAsMB->GetNumberOfBlocks();
72 break;
73 }
74
76 auto inputAsMB = vtkMultiBlockDataSet::SafeDownCast(input);
77
78 if(!inputAsMB || inputAsMB->GetNumberOfBlocks() < 1) {
79 this->printErr(
80 "Block Tuple iteration requires 'vtkMultiBlockDataSet' input that "
81 "contains at least one 'vtkMultiBlockDataSet'.");
82 return 0;
83 }
84 auto firstComponent
85 = vtkMultiBlockDataSet::SafeDownCast(inputAsMB->GetBlock(0));
86 if(!firstComponent) {
87 this->printErr(
88 "Block Tuple iteration requires 'vtkMultiBlockDataSet' input that "
89 "contains at least one 'vtkMultiBlockDataSet'.");
90 return 0;
91 }
92 this->IterationNumber = firstComponent->GetNumberOfBlocks();
93 break;
94 }
95
97 auto inputAsT = vtkTable::SafeDownCast(input);
98 if(!inputAsT) {
99 this->printErr("Row iteration requires 'vtkTable' input.");
100 return 0;
101 }
102 this->IterationNumber = inputAsT->GetNumberOfRows();
103 break;
104 }
105
107 auto inputArray = this->GetInputArrayToProcess(0, inputVector);
108 if(!inputArray) {
109 this->printErr("Unable to retrieve input array.");
110 return 0;
111 }
112 this->IterationNumber = inputArray->GetNumberOfTuples();
113 break;
114 }
115
117 auto inputFD
118 = input->GetAttributesAsFieldData(this->GetArrayAttributeType());
119 if(inputFD) {
120 this->IterationNumber = inputFD->GetNumberOfArrays();
121 } else {
122 this->printErr("Unable to retrieve attribute type.");
123 return 0;
124 }
125 break;
126 }
127
128 default: {
129 this->printErr("Unsupported mode");
130 return 0;
131 }
132 }
133
134 // Iteration info
135 auto iterationInformation = vtkSmartPointer<vtkDoubleArray>::New();
136 iterationInformation->SetName("_ttk_IterationInfo");
137 iterationInformation->SetNumberOfComponents(2);
138 iterationInformation->SetNumberOfTuples(1);
139 iterationInformation->SetValue(0, this->IterationIdx);
140 iterationInformation->SetValue(1, this->IterationNumber);
141
142 std::string const modeStrings[6] = {"B", "R", "G", "V", "A", "BT"};
143 this->printMsg("[" + modeStrings[static_cast<int>(mode)] + "] Iteration: ( "
144 + std::to_string(this->IterationIdx) + " / "
145 + std::to_string(this->IterationNumber - 1) + " ) ",
147
148 this->SetExpressionString(std::to_string(this->IterationIdx));
149 this->SetExtractUniqueValues(false);
150 this->SetOutputArrayName("IterationArray");
151
152 if(!ttkExtract::RequestData(request, inputVector, outputVector))
153 return 0;
154
155 // Add iteration info to output
156 auto output = vtkDataObject::GetData(outputVector);
157 addRecursivelyToFieldData(output, iterationInformation);
158
159 this->IterationIdx++;
160
161 return 1;
162}
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
virtual void SetExpressionString(const std::string &)
virtual void SetOutputArrayName(const std::string &)
virtual int GetArrayAttributeType()
virtual void SetExtractUniqueValues(bool)
virtual EXTRACTION_MODE GetExtractionMode() const
TTK VTK-filter that works in conjunction with the ttkEndFor filter to iterate over blocks,...
Definition ttkForEach.h:33
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
~ttkForEach() override
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
vtkStandardNewMacro(ttkForEach)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)