TTK
Loading...
Searching...
No Matches
ttkPointDataSelector.cpp
Go to the documentation of this file.
1#include <vtkDataArray.h>
2#include <vtkDataSet.h>
3#include <vtkInformation.h>
4#include <vtkIntArray.h>
5#include <vtkNew.h>
6#include <vtkObjectFactory.h>
7#include <vtkPointData.h>
8#include <vtkSmartPointer.h>
9
10#include <algorithm>
11#include <regex>
13
15
17 this->setDebugMsgPrefix("PointDataSelector");
18 this->SetNumberOfInputPorts(1);
19 this->SetNumberOfOutputPorts(1);
20}
21
23 vtkInformation *info) {
24 if(port == 0) {
25 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
26 return 1;
27 }
28 return 0;
29}
30
32 vtkInformation *info) {
33 if(port == 0) {
35 return 1;
36 }
37 return 0;
38}
39
41 int const nbScalars = input->GetPointData()->GetNumberOfArrays();
42 AvailableFields.clear();
43 AvailableFields.resize(nbScalars);
44 for(int i = 0; i < nbScalars; ++i) {
45 AvailableFields[i] = input->GetPointData()->GetArrayName(i);
46 }
47}
48
50 vtkInformation *request,
51 vtkInformationVector **inputVector,
52 vtkInformationVector *outputVector) {
53
54 vtkDataSet *input = vtkDataSet::GetData(inputVector[0]);
56 return ttkAlgorithm::RequestInformation(request, inputVector, outputVector);
57}
58
59int ttkPointDataSelector::RequestData(vtkInformation *ttkNotUsed(request),
60 vtkInformationVector **inputVector,
61 vtkInformationVector *outputVector) {
62
63 vtkDataSet *input = vtkDataSet::GetData(inputVector[0]);
64 vtkDataSet *output = vtkDataSet::GetData(outputVector);
65
66 output->ShallowCopy(input);
67
68 vtkPointData *inputPointData = input->GetPointData();
69 vtkNew<vtkPointData> outputPointData{};
70
71#ifndef TTK_ENABLE_KAMIKAZE
72 if(!inputPointData) {
73 this->printErr("Input has no point data.");
74 return -1;
75 }
76
77 if(!outputPointData) {
78 this->printErr("vtkPointData memory allocation problem.");
79 return -1;
80 }
81#endif
82
83 if(AvailableFields.empty()) {
84 // when loading from statefiles or vtk script
86 }
87
88 try {
89 for(auto &scalar : SelectedFields) {
90 // valid array
91 if(scalar.empty()) {
92 continue;
93 }
94 // check bounds in the range
95 std::ptrdiff_t const pos
96 = std::find(AvailableFields.begin(), AvailableFields.end(), scalar)
97 - AvailableFields.begin();
98 if(pos < RangeId[0] || pos > RangeId[1]) {
99 continue;
100 }
101 // retrieve array if match
102 if(!std::regex_match(scalar, std::regex(RegexpString))) {
103 continue;
104 }
105 // Add the array
106 vtkDataArray *arr = inputPointData->GetArray(scalar.c_str());
107 if(arr) {
108
109 if(RenameSelected) {
110 if(SelectedFields.size() != 1 && RangeId[1] - RangeId[0] != 0) {
111 this->printErr("Can't rename more than one field.");
112 return 0;
113 }
114
115 vtkSmartPointer<vtkDataArray> const localFieldCopy{
116 arr->NewInstance()};
117 if(localFieldCopy) {
118 localFieldCopy->DeepCopy(arr);
119 localFieldCopy->SetName(SelectedFieldName.data());
120 arr = localFieldCopy;
121 }
122 }
123
124 outputPointData->AddArray(arr);
125 }
126 }
127 } catch(std::regex_error &) {
128 this->printErr("Bad regexp.");
129 }
130
131 output->GetPointData()->ShallowCopy(outputPointData);
132
133 return 1;
134}
#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()
virtual int RequestInformation(vtkInformation *ttkNotUsed(request), vtkInformationVector **ttkNotUsed(inputVectors), vtkInformationVector *ttkNotUsed(outputVector))
TTK VTK-filter that selects scalar fields on input with shallow copy.
int FillOutputPortInformation(int port, vtkInformation *info) override
int FillInputPortInformation(int port, vtkInformation *info) override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
void FillAvailableFields(vtkDataSet *input)
int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) 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(ttkPointDataSelector)