TTK
Loading...
Searching...
No Matches
ttkPointDataConverter.cpp
Go to the documentation of this file.
1#include <vtkCharArray.h>
2#include <vtkDataArray.h>
3#include <vtkDataSet.h>
4#include <vtkDoubleArray.h>
5#include <vtkFloatArray.h>
6#include <vtkIdTypeArray.h>
7#include <vtkInformation.h>
8#include <vtkIntArray.h>
9#include <vtkNew.h>
10#include <vtkObjectFactory.h>
11#include <vtkPointData.h>
12#include <vtkShortArray.h>
13#include <vtkUnsignedCharArray.h>
14#include <vtkUnsignedShortArray.h>
15
17#include <ttkUtils.h>
18
19#include <limits>
20
22
24 this->setDebugMsgPrefix("PointDataConverter");
25 this->SetNumberOfInputPorts(1);
26 this->SetNumberOfOutputPorts(1);
27
28 vtkWarningMacro("`TTK PointDataConverter' is now deprecated. Please use "
29 "`Calculator' instead.");
30}
31
33 vtkInformation *info) {
34 if(port == 0) {
35 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
36 return 1;
37 }
38 return 0;
39}
40
42 vtkInformation *info) {
43 if(port == 0) {
45 return 1;
46 }
47 return 0;
48}
49
50template <typename InputFieldType,
51 typename OutputFieldType,
52 typename OutputVTKArrayType>
53int ttkPointDataConverter::convert(vtkDataArray *inputData,
54 vtkDataSet *output) {
55 const auto input_ptr
56 = static_cast<InputFieldType *>(ttkUtils::GetVoidPointer(inputData));
57 int const n = inputData->GetNumberOfComponents();
58 vtkIdType const N = inputData->GetNumberOfTuples();
59
60 vtkNew<OutputVTKArrayType> outputData;
61 outputData->SetName(inputData->GetName());
62 outputData->SetNumberOfComponents(n);
63 outputData->SetNumberOfTuples(N);
64 auto output_ptr
65 = static_cast<OutputFieldType *>(ttkUtils::GetVoidPointer(outputData));
66
67 if(UseNormalization) {
68 auto type_min
69 = static_cast<double>(std::numeric_limits<OutputFieldType>::min());
70 auto type_max
71 = static_cast<double>(std::numeric_limits<OutputFieldType>::max());
72 for(int k = 0; k < n; ++k) {
73 double *input_limits = inputData->GetRange(k);
74
75 for(vtkIdType i = 0; i < N; ++i) {
76 auto d = (double)input_ptr[i * n + k];
77 d = (d - input_limits[0]) / (input_limits[1] - input_limits[0]);
78 d = d * (type_max - type_min) + type_min;
79 output_ptr[i * n + k] = (OutputFieldType)d;
80 }
81 }
82 } else
83 for(vtkIdType i = 0; i < N * n; ++i) {
84 // NOLINTNEXTLINE (bugprone-signed-char-misuse)
85 output_ptr[i] = (OutputFieldType)input_ptr[i];
86 }
87
88 output->GetPointData()->AddArray(outputData);
89
90 return 0;
91}
92
94 vtkInformationVector **inputVector,
95 vtkInformationVector *outputVector) {
96
97 vtkDataSet *input = vtkDataSet::GetData(inputVector[0]);
98 vtkDataSet *output = vtkDataSet::GetData(outputVector);
99
100 output->ShallowCopy(input);
101
102 const auto inputScalarField = this->GetInputArrayToProcess(0, input);
103 if(inputScalarField == nullptr) {
104 this->printErr("No such input scalar field");
105 return 0;
106 }
107 auto InputType = inputScalarField->GetDataType();
108
109 bool const oldUseNormalization{UseNormalization};
110 if(OutputType == SupportedType::Float or OutputType == SupportedType::Double)
111 UseNormalization = false;
112
113 if(InputType == VTK_CHAR) {
114 if(OutputType == SupportedType::Double)
115 convert<char, double, vtkDoubleArray>(inputScalarField, output);
116 else if(OutputType == SupportedType::Float)
117 convert<char, float, vtkFloatArray>(inputScalarField, output);
118 else if(OutputType == SupportedType::IdType)
119 convert<char, vtkIdType, vtkIdTypeArray>(inputScalarField, output);
120 else if(OutputType == SupportedType::Int)
121 convert<char, int, vtkIntArray>(inputScalarField, output);
122 else if(OutputType == SupportedType::Short)
123 convert<char, short, vtkShortArray>(inputScalarField, output);
124 else if(OutputType == SupportedType::UnsignedShort)
125 convert<char, unsigned short, vtkUnsignedShortArray>(
126 inputScalarField, output);
127 else if(OutputType == SupportedType::UnsignedChar)
128 convert<char, unsigned char, vtkUnsignedCharArray>(
129 inputScalarField, output);
130 } else if(InputType == VTK_DOUBLE) {
131 if(OutputType == SupportedType::Char)
132 convert<double, char, vtkCharArray>(inputScalarField, output);
133 else if(OutputType == SupportedType::Float)
134 convert<double, float, vtkFloatArray>(inputScalarField, output);
135 else if(OutputType == SupportedType::IdType)
136 convert<double, vtkIdType, vtkIdTypeArray>(inputScalarField, output);
137 else if(OutputType == SupportedType::Int)
138 convert<double, int, vtkIntArray>(inputScalarField, output);
139 else if(OutputType == SupportedType::Short)
140 convert<double, short, vtkShortArray>(inputScalarField, output);
141 else if(OutputType == SupportedType::UnsignedShort)
142 convert<double, unsigned short, vtkUnsignedShortArray>(
143 inputScalarField, output);
144 else if(OutputType == SupportedType::UnsignedChar)
145 convert<double, unsigned char, vtkUnsignedCharArray>(
146 inputScalarField, output);
147 } else if(InputType == VTK_FLOAT) {
148 if(OutputType == SupportedType::Char)
149 convert<float, char, vtkCharArray>(inputScalarField, output);
150 else if(OutputType == SupportedType::Double)
151 convert<float, double, vtkDoubleArray>(inputScalarField, output);
152 else if(OutputType == SupportedType::IdType)
153 convert<float, vtkIdType, vtkIdTypeArray>(inputScalarField, output);
154 else if(OutputType == SupportedType::Int)
155 convert<float, int, vtkIntArray>(inputScalarField, output);
156 else if(OutputType == SupportedType::Short)
157 convert<float, short, vtkShortArray>(inputScalarField, output);
158 else if(OutputType == SupportedType::UnsignedShort)
159 convert<float, unsigned short, vtkUnsignedShortArray>(
160 inputScalarField, output);
161 else if(OutputType == SupportedType::UnsignedChar)
162 convert<float, unsigned char, vtkUnsignedCharArray>(
163 inputScalarField, output);
164 } else if(InputType == VTK_INT) {
165 if(OutputType == SupportedType::Char)
166 convert<int, char, vtkCharArray>(inputScalarField, output);
167 else if(OutputType == SupportedType::Double)
168 convert<int, double, vtkDoubleArray>(inputScalarField, output);
169 else if(OutputType == SupportedType::Float)
170 convert<int, float, vtkFloatArray>(inputScalarField, output);
171 else if(OutputType == SupportedType::IdType)
172 convert<int, vtkIdType, vtkIdTypeArray>(inputScalarField, output);
173 else if(OutputType == SupportedType::Short)
174 convert<int, short, vtkShortArray>(inputScalarField, output);
175 else if(OutputType == SupportedType::UnsignedShort)
176 convert<int, unsigned short, vtkUnsignedShortArray>(
177 inputScalarField, output);
178 else if(OutputType == SupportedType::UnsignedChar)
179 convert<int, unsigned char, vtkUnsignedCharArray>(
180 inputScalarField, output);
181 } else if(InputType == VTK_ID_TYPE) {
182 if(OutputType == SupportedType::Char)
183 convert<vtkIdType, char, vtkCharArray>(inputScalarField, output);
184 else if(OutputType == SupportedType::Double)
185 convert<vtkIdType, double, vtkDoubleArray>(inputScalarField, output);
186 else if(OutputType == SupportedType::Float)
187 convert<vtkIdType, float, vtkFloatArray>(inputScalarField, output);
188 else if(OutputType == SupportedType::Short)
189 convert<vtkIdType, short, vtkShortArray>(inputScalarField, output);
190 else if(OutputType == SupportedType::UnsignedShort)
191 convert<vtkIdType, unsigned short, vtkUnsignedShortArray>(
192 inputScalarField, output);
193 else if(OutputType == SupportedType::UnsignedChar)
194 convert<vtkIdType, unsigned char, vtkUnsignedCharArray>(
195 inputScalarField, output);
196 } else if(InputType == VTK_LONG) {
197 if(OutputType == SupportedType::Char)
198 convert<long long int, char, vtkCharArray>(inputScalarField, output);
199 else if(OutputType == SupportedType::Double)
200 convert<long long int, double, vtkDoubleArray>(inputScalarField, output);
201 else if(OutputType == SupportedType::Float)
202 convert<long long int, float, vtkFloatArray>(inputScalarField, output);
203 else if(OutputType == SupportedType::IdType)
204 convert<long long int, vtkIdType, vtkIdTypeArray>(
205 inputScalarField, output);
206 else if(OutputType == SupportedType::Int)
207 convert<long long int, int, vtkIntArray>(inputScalarField, output);
208 else if(OutputType == SupportedType::Short)
209 convert<long long int, short, vtkShortArray>(inputScalarField, output);
210 else if(OutputType == SupportedType::UnsignedShort)
211 convert<long long int, unsigned short, vtkUnsignedShortArray>(
212 inputScalarField, output);
213 else if(OutputType == SupportedType::UnsignedChar)
214 convert<long long int, unsigned char, vtkUnsignedCharArray>(
215 inputScalarField, output);
216 } else if(InputType == VTK_SHORT) {
217 if(OutputType == SupportedType::Char)
218 convert<short, char, vtkCharArray>(inputScalarField, output);
219 else if(OutputType == SupportedType::Double)
220 convert<short, double, vtkDoubleArray>(inputScalarField, output);
221 else if(OutputType == SupportedType::Float)
222 convert<short, float, vtkFloatArray>(inputScalarField, output);
223 else if(OutputType == SupportedType::IdType)
224 convert<short, vtkIdType, vtkIdTypeArray>(inputScalarField, output);
225 else if(OutputType == SupportedType::Int)
226 convert<short, int, vtkIntArray>(inputScalarField, output);
227 else if(OutputType == SupportedType::UnsignedShort)
228 convert<short, unsigned short, vtkUnsignedShortArray>(
229 inputScalarField, output);
230 else if(OutputType == SupportedType::UnsignedChar)
231 convert<short, unsigned char, vtkUnsignedCharArray>(
232 inputScalarField, output);
233 } else if(InputType == VTK_UNSIGNED_SHORT) {
234 if(OutputType == SupportedType::Char)
235 convert<unsigned short, char, vtkCharArray>(inputScalarField, output);
236 else if(OutputType == SupportedType::Double)
237 convert<unsigned short, double, vtkDoubleArray>(inputScalarField, output);
238 else if(OutputType == SupportedType::Float)
239 convert<unsigned short, float, vtkFloatArray>(inputScalarField, output);
240 else if(OutputType == SupportedType::IdType)
241 convert<unsigned short, vtkIdType, vtkIdTypeArray>(
242 inputScalarField, output);
243 else if(OutputType == SupportedType::Int)
244 convert<unsigned short, int, vtkIntArray>(inputScalarField, output);
245 else if(OutputType == SupportedType::Short)
246 convert<unsigned short, short, vtkShortArray>(inputScalarField, output);
247 else if(OutputType == SupportedType::UnsignedChar)
248 convert<unsigned short, unsigned char, vtkUnsignedCharArray>(
249 inputScalarField, output);
250 } else if(InputType == VTK_UNSIGNED_CHAR) {
251 if(OutputType == SupportedType::Char)
252 convert<unsigned char, char, vtkCharArray>(inputScalarField, output);
253 else if(OutputType == SupportedType::Double)
254 convert<unsigned char, double, vtkDoubleArray>(inputScalarField, output);
255 else if(OutputType == SupportedType::Float)
256 convert<unsigned char, float, vtkFloatArray>(inputScalarField, output);
257 else if(OutputType == SupportedType::IdType)
258 convert<unsigned char, vtkIdType, vtkIdTypeArray>(
259 inputScalarField, output);
260 else if(OutputType == SupportedType::Int)
261 convert<unsigned char, int, vtkIntArray>(inputScalarField, output);
262 else if(OutputType == SupportedType::Short)
263 convert<unsigned char, short, vtkShortArray>(inputScalarField, output);
264 else if(OutputType == SupportedType::UnsignedShort)
265 convert<unsigned char, unsigned short, vtkUnsignedShortArray>(
266 inputScalarField, output);
267 } else {
268 this->printErr("Unsupported data type");
269 }
270
271 UseNormalization = oldUseNormalization;
272
273 return 1;
274}
#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 converts data types for point-based scalar fields (for instance,...
int FillInputPortInformation(int port, vtkInformation *info) override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int convert(vtkDataArray *inputData, vtkDataSet *output)
int FillOutputPortInformation(int port, vtkInformation *info) override
static void * GetVoidPointer(vtkDataArray *array, vtkIdType start=0)
Definition ttkUtils.cpp:226
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(ttkPointDataConverter)