TTK
Loading...
Searching...
No Matches
ttkStringArrayConverter.cpp
Go to the documentation of this file.
2
3#include <vtkDataSet.h>
4#include <vtkFieldData.h>
5#include <vtkIdTypeArray.h>
6#include <vtkInformation.h>
7#include <vtkPointData.h>
8#include <vtkStringArray.h>
9
10#include <ttkUtils.h>
11
12#include <map>
13#include <set>
14
16
18 this->setDebugMsgPrefix("StringArrayConverter");
19
20 this->SetNumberOfInputPorts(1);
21 this->SetNumberOfOutputPorts(1);
22}
23
25 vtkInformation *info) {
26 if(port == 0) {
27 info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkDataSet");
28 return 1;
29 }
30 return 0;
31}
32
34 vtkInformation *info) {
35 if(port == 0) {
37 return 1;
38 }
39 return 0;
40}
41
43 vtkInformationVector **inputVector,
44 vtkInformationVector *outputVector) {
45 const auto input = vtkDataSet::GetData(inputVector[0]);
46 auto output = vtkDataSet::GetData(outputVector);
47
48 if(input == nullptr || output == nullptr) {
49 this->printErr("Null input data, aborting");
50 return 0;
51 }
52
53 // string array
54 const auto sa = vtkStringArray::SafeDownCast(
55 this->GetInputAbstractArrayToProcess(0, inputVector));
56
57 if(sa == nullptr) {
58 this->printErr("Cannot find the required string array");
59 return 0;
60 }
61
62 const auto nvalues = sa->GetNumberOfTuples();
63
64 std::set<std::string> values{};
65
66 for(vtkIdType i = 0; i < nvalues; ++i) {
67 values.emplace(sa->GetValue(i));
68 }
69
70 std::map<std::string, size_t> valInd{};
71
72 {
73 size_t i = 0;
74 for(const auto &el : values) {
75 valInd[el] = i;
76 i++;
77 }
78 }
79
80 // shallow-copy input
81 output->ShallowCopy(input);
82
83 const auto pdo = output->GetPointData();
84
85 // array of indices
86 vtkNew<vtkIdTypeArray> ia{};
87 // array name
88 std::string colname{sa->GetName()};
89 colname += "Int";
90 ia->SetName(colname.data());
91 ia->SetNumberOfTuples(nvalues);
92 // fill array with corresponding string values indices
93 for(vtkIdType i = 0; i < nvalues; ++i) {
94 ia->SetValue(i, valInd[sa->GetValue(i)]);
95 }
96
97 pdo->AddArray(ia);
98
99 return 1;
100}
#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 reads a Cinema Spec D Database.
int FillOutputPortInformation(int port, vtkInformation *info) override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int FillInputPortInformation(int port, vtkInformation *info) 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(ttkStringArrayConverter)