TTK
Loading...
Searching...
No Matches
ttkUncertainDataEstimator.cpp
Go to the documentation of this file.
1#include <vtkDataArray.h>
2#include <vtkDataSet.h>
3#include <vtkDoubleArray.h>
4#include <vtkInformation.h>
5#include <vtkInformationVector.h>
6#include <vtkNew.h>
7#include <vtkPointData.h>
8
10#include <ttkUtils.h>
11
13
15 SetNumberOfInputPorts(1);
16 SetNumberOfOutputPorts(3);
17}
18
20 vtkInformation *info) {
21 if(port == 0) {
22 info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
23 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
24 return 1;
25 }
26 return 0;
27}
28
30 vtkInformation *info) {
31 if(port == 0 || port == 1 || port == 2) {
33 return 1;
34 }
35 return 0;
36}
37
38// to adapt if your wrapper does not inherit from vtkDataSetAlgorithm
40 vtkInformationVector **inputVector,
41 vtkInformationVector *outputVector) {
42
43 // Unified bound fields
44 auto boundFields = vtkDataSet::GetData(outputVector, 0);
45 // Histograms
46 auto probability = vtkDataSet::GetData(outputVector, 1);
47 // Mean field
48 auto mean = vtkDataSet::GetData(outputVector, 2);
49
50 // Number of input files
51 int const numInputs = inputVector[0]->GetNumberOfInformationObjects();
52 this->printMsg(std::vector<std::vector<std::string>>{
53 {"#Inputs", std::to_string(numInputs)}});
54
55 // Get input data
56 std::vector<vtkDataSet *> input(numInputs);
57 for(int i = 0; i < numInputs; i++) {
58 input[i] = vtkDataSet::GetData(inputVector[0], i);
59 }
60
61 // Use a pointer-base copy for the input data
62 boundFields->ShallowCopy(input[0]);
63 probability->ShallowCopy(input[0]);
64 mean->ShallowCopy(input[0]);
65
66 int numFields = 0;
67 int numArrays = 0;
68
69 // Get arrays from input data
70 std::vector<vtkDataArray *> inputScalarField;
71
72 for(int i = 0; i < numInputs; i++) {
73 numArrays = input[i]->GetPointData()->GetNumberOfArrays();
74 numFields += numArrays;
75 for(int iarray = 0; iarray < numArrays; iarray++) {
76 inputScalarField.push_back(input[i]->GetPointData()->GetArray(iarray));
77 }
78 }
79
80 this->printMsg(std::vector<std::vector<std::string>>{
81 {"#Fields", std::to_string(numFields)}});
82
83 for(int i = 0; i < numFields; i++) {
84
85 // Check if inputs have the same data type and the same number of points
86 if(inputScalarField[i]->GetDataType()
87 != inputScalarField[0]->GetDataType()) {
88 this->printErr("Inputs of different data types.");
89 return -3;
90 }
91 if(inputScalarField[i]->GetNumberOfTuples()
92 != inputScalarField[0]->GetNumberOfTuples()) {
93 this->printErr("Inputs with different number of points.");
94 return -2;
95 }
96 // Check if all the inputs are here
97 if(!inputScalarField[i])
98 return -1;
99 }
100 // Allocate the memory for the output bound scalar fields
101 vtkSmartPointer<vtkDataArray> const outputLowerBoundScalarField{
102 inputScalarField[0]->NewInstance()};
103 vtkSmartPointer<vtkDataArray> const outputUpperBoundScalarField{
104 inputScalarField[0]->NewInstance()};
105 outputLowerBoundScalarField->SetName("lowerBoundField");
106 outputUpperBoundScalarField->SetName("upperBoundField");
107
108 this->printMsg(
109 std::vector<std::vector<std::string>>{{"#Bins", std::to_string(BinCount)}});
110
111 std::vector<vtkNew<vtkDoubleArray>> probabilityScalarField(BinCount);
112
113 // Create DoubleArray objects and link them to the data set
114 for(int b = 0; b < BinCount; b++) {
115 probabilityScalarField[b]->SetNumberOfTuples(input[0]->GetNumberOfPoints());
116 probability->GetPointData()->AddArray(probabilityScalarField[b]);
117 probabilityScalarField[b]->FillComponent(0, 0.);
118 }
119
120 // Mean field data set
121 // Allocate new array
122 vtkNew<vtkDoubleArray> meanField{};
123 meanField->SetNumberOfTuples(input[0]->GetNumberOfPoints());
124 meanField->SetName("meanField");
125 meanField->FillComponent(0, 0.0);
126
127 mean->GetPointData()->AddArray(meanField);
128
129 // Resize arrays and add them in the output if required
131 outputLowerBoundScalarField->SetNumberOfTuples(
132 input[0]->GetNumberOfPoints());
133 boundFields->GetPointData()->AddArray(outputLowerBoundScalarField);
134 } else {
135 outputLowerBoundScalarField->SetNumberOfTuples(0);
136 }
137
139 outputUpperBoundScalarField->SetNumberOfTuples(
140 input[0]->GetNumberOfPoints());
141 boundFields->GetPointData()->AddArray(outputUpperBoundScalarField);
142 } else {
143 outputUpperBoundScalarField->SetNumberOfTuples(0);
144 }
145
146 // Calling the executing package
147
148 if(numFields > 0) {
149 this->setVertexNumber(boundFields->GetNumberOfPoints());
150 this->setBinCount(this->BinCount);
151
152 this->setNumberOfInputs(numFields);
153 for(int i = 0; i < numFields; i++) {
155 i, ttkUtils::GetVoidPointer(inputScalarField[i]));
156 }
157
159 ttkUtils::GetVoidPointer(outputLowerBoundScalarField));
161 ttkUtils::GetVoidPointer(outputUpperBoundScalarField));
163
164 for(int b = 0; b < BinCount; b++) {
166 b, ttkUtils::GetPointer<double>(probabilityScalarField[b]));
167 }
168
169 switch(inputScalarField[0]->GetDataType()) {
170 vtkTemplateMacro(this->execute<VTK_TT>());
171 }
172
173 for(int b = 0; b < BinCount; b++) {
174 std::stringstream name{};
175 name << std::setprecision(8) << this->getBinValue(b);
176 probabilityScalarField[b]->SetName(name.str().c_str());
177 }
178 }
179
180 return 1;
181}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
static vtkInformationIntegerKey * SAME_DATA_TYPE_AS_INPUT_PORT()
TTK VTK-filter that takes an input ensemble data set (represented by a list of scalar fields) and whi...
int FillInputPortInformation(int port, vtkInformation *info) override
int FillOutputPortInformation(int port, vtkInformation *info) override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
static void * GetVoidPointer(vtkDataArray *array, vtkIdType start=0)
Definition ttkUtils.cpp:226
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
void setOutputLowerBoundField(void *const data)
void setOutputMeanField(void *const data)
void setVertexNumber(const SimplexId &vertexNumber)
int setInputDataPointer(const int idx, void *const data)
void setBinCount(const int binCount)
void setOutputUpperBoundField(void *const data)
void setOutputProbability(const int idx, double *const data)
void setNumberOfInputs(const int numberOfInputs)
vtkStandardNewMacro(ttkUncertainDataEstimator)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)