TTK
Loading...
Searching...
No Matches
ttkTableDistanceMatrix.cpp
Go to the documentation of this file.
2#include <ttkUtils.h>
3
4#include <vtkDataSet.h>
5#include <vtkDoubleArray.h>
6#include <vtkInformation.h>
7#include <vtkInformationVector.h>
8#include <vtkNew.h>
9#include <vtkObjectFactory.h>
10#include <vtkTable.h>
11
12#include <regex>
13
15
17 SetNumberOfInputPorts(1);
18 SetNumberOfOutputPorts(1);
19}
20
22 vtkInformation *info) {
23 if(port == 0) {
24 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkTable");
25 return 1;
26 }
27 return 0;
28}
29
31 vtkInformation *info) {
32 if(port == 0) {
33 info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkTable");
34 return 1;
35 }
36 return 0;
37}
38
39int ttkTableDistanceMatrix::RequestData(vtkInformation * /*request*/,
40 vtkInformationVector **inputVector,
41 vtkInformationVector *outputVector) {
42 ttk::Timer tm{};
43
44 auto *input = vtkTable::GetData(inputVector[0]);
45 auto *output = vtkTable::GetData(outputVector);
46 output->ShallowCopy(input);
47
48 if(SelectFieldsWithRegexp) {
49 // select all input columns whose name is matching the regexp
50 ScalarFields.clear();
51 const auto n = input->GetNumberOfColumns();
52 for(int i = 0; i < n; ++i) {
53 const auto &name = input->GetColumnName(i);
54 if(std::regex_match(name, std::regex(RegexpString))) {
55 ScalarFields.emplace_back(name);
56 }
57 }
58 }
59
60 const auto numberOfRows = input->GetNumberOfRows();
61 const auto numberOfColumns = ScalarFields.size();
62
63 if(numberOfRows <= 0 || numberOfColumns <= 0) {
64 this->printErr("Input matrix has invalid dimensions (rows: "
65 + std::to_string(numberOfRows)
66 + ", columns: " + std::to_string(numberOfColumns) + ")");
67 return 0;
68 }
69
70 std::vector<vtkAbstractArray *> arrays{};
71 arrays.reserve(ScalarFields.size());
72 for(const auto &s : ScalarFields) {
73 arrays.push_back(input->GetColumnByName(s.data()));
74 }
75
76 std::vector<std::vector<double>> inputMatrix(numberOfRows);
77#ifdef TTK_ENABLE_OPENMP
78#pragma omp parallel for num_threads(this->threadNumber_)
79#endif // TTK_ENABLE_OPENMP
80 for(int i = 0; i < numberOfRows; ++i) {
81 for(size_t j = 0; j < numberOfColumns; ++j) {
82 inputMatrix[i].emplace_back(arrays[j]->GetVariantValue(i).ToDouble());
83 }
84 }
85
86 std::vector<const double *> inputPtrs(inputMatrix.size());
87 for(size_t i = 0; i < inputMatrix.size(); ++i) {
88 const auto &vec{inputMatrix[i]};
89 inputPtrs[i] = vec.data();
90 }
91
92 std::vector<double *> distanceMatrix(inputMatrix.size());
93
94 // zero-padd column name to keep Row Data columns ordered
95 const auto zeroPad
96 = [](std::string &colName, const size_t numberCols, const size_t colIdx) {
97 std::string const max{std::to_string(numberCols - 1)};
98 std::string const cur{std::to_string(colIdx)};
99 std::string const zer(max.size() - cur.size(), '0');
100 colName.append(zer).append(cur);
101 };
102
103 // copy distance matrix to output
104 for(int i = 0; i < numberOfRows; ++i) {
105 std::string name{"Distance"};
106 zeroPad(name, distanceMatrix.size(), i);
107
108 vtkNew<vtkDoubleArray> col{};
109 col->SetNumberOfTuples(numberOfRows);
110 col->SetName(name.c_str());
111 output->AddColumn(col);
112 distanceMatrix[i]
113 = ttkUtils::GetPointer<double>(vtkDoubleArray::SafeDownCast(col));
114 }
115
116 this->execute(distanceMatrix, inputPtrs, inputMatrix[0].size());
117
118 this->printMsg("Complete (#dimensions: " + std::to_string(numberOfColumns)
119 + ", #points: " + std::to_string(numberOfRows) + ")",
120 1.0, tm.getElapsedTime(), this->threadNumber_);
121
122 return 1;
123}
Computes a distance matrix using LDistance from a vtkTable.
int FillOutputPortInformation(int port, vtkInformation *info) override
int FillInputPortInformation(int port, vtkInformation *info) override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
int execute(std::vector< TOut * > &output, const std::vector< const TIn * > &inputs, const size_t nPoints) const
vtkStandardNewMacro(ttkTableDistanceMatrix)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)