TTK
Loading...
Searching...
No Matches
ttkComponentSize.cpp
Go to the documentation of this file.
1#include <ttkComponentSize.h>
2#include <ttkUtils.h>
3
4#include <vtkCellData.h>
5#include <vtkConnectivityFilter.h>
6#include <vtkDoubleArray.h>
7#include <vtkInformation.h>
8#include <vtkPointData.h>
9#include <vtkUnstructuredGrid.h>
10
12
14 this->setDebugMsgPrefix("ComponentSize");
15
16 this->SetNumberOfInputPorts(1);
17 this->SetNumberOfOutputPorts(1);
18
19 vtkWarningMacro("`TTK ComponentSize' is now deprecated. Please use "
20 "`Connectivity' instead.");
21}
22
24
25int ttkComponentSize::FillInputPortInformation(int port, vtkInformation *info) {
26 if(port == 0) {
27 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "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
42int ttkComponentSize::RequestData(vtkInformation *ttkNotUsed(request),
43 vtkInformationVector **inputVector,
44 vtkInformationVector *outputVector) {
45 ttk::Timer t;
46 size_t const threadNumber = this->getThreadNumber();
47
48 this->printMsg(
49 "Computing connected components", 0, 0, ttk::debug::LineMode::REPLACE);
50
51 auto connectivityFilter = vtkSmartPointer<vtkConnectivityFilter>::New();
52 connectivityFilter->SetInputData(vtkDataSet::GetData(inputVector[0]));
53 connectivityFilter->SetExtractionModeToAllRegions();
54 connectivityFilter->ColorRegionsOn();
55 connectivityFilter->Update();
56
57 size_t const nRegions = connectivityFilter->GetNumberOfExtractedRegions();
58 if(nRegions < 1) {
59 this->printErr("Unable to compute connected components.");
60 return 0;
61 }
62
63 this->printMsg(
64 "Computing connected components (" + std::to_string(nRegions) + ")", 1,
65 t.getElapsedTime());
66
67 t.reStart();
68 this->printMsg("Computing component sizes", 0, 0, threadNumber,
70
71 auto output = vtkDataSet::GetData(outputVector);
72 output->ShallowCopy(connectivityFilter->GetOutput());
73
74 size_t const nVertices = output->GetNumberOfPoints();
75 size_t const nCells = output->GetNumberOfCells();
76
77 auto vertexIds = (vtkIdType *)ttkUtils::GetVoidPointer(
78 output->GetPointData()->GetArray("RegionId"));
79 auto cellIds = (vtkIdType *)ttkUtils::GetVoidPointer(
80 output->GetCellData()->GetArray("RegionId"));
81
82 if(!vertexIds || !cellIds) {
83 this->printErr("Unable to retrieve vertex and cell Identifiers.");
84 return 0;
85 }
86
87 this->printMsg("Computing component sizes", 0.1, t.getElapsedTime(),
88 threadNumber, ttk::debug::LineMode::REPLACE);
89
90 // count vertices per region
91 std::vector<double> regionIdToVertexCountMap(nRegions, 0);
92#ifdef TTK_ENABLE_OPENMP
93#pragma omp parallel for num_threads(threadNumber)
94#endif
95 for(size_t i = 0; i < nVertices; i++) {
96 auto regionId = vertexIds[i];
97
98#ifdef TTK_ENABLE_OPENMP
99#pragma omp atomic update
100#endif
101 regionIdToVertexCountMap[regionId]++;
102 }
103
104 // count cells per region
105 std::vector<double> regionIdToCellCountMap(nRegions, 0);
106#ifdef TTK_ENABLE_OPENMP
107#pragma omp parallel for num_threads(threadNumber)
108#endif
109 for(size_t i = 0; i < nCells; i++) {
110 auto regionId = cellIds[i];
111
112#ifdef TTK_ENABLE_OPENMP
113#pragma omp atomic update
114#endif
115 regionIdToCellCountMap[regionId]++;
116 }
117
118 // generate vertex number fields
119 {
120 auto vertexNumbers = vtkSmartPointer<vtkDoubleArray>::New();
121 vertexNumbers->SetNumberOfComponents(1);
122 vertexNumbers->SetNumberOfTuples(nVertices);
123 vertexNumbers->SetName("VertexNumber");
124 auto vertexNumbersData = (double *)ttkUtils::GetVoidPointer(vertexNumbers);
125
126#ifdef TTK_ENABLE_OPENMP
127#pragma omp parallel for num_threads(threadNumber)
128#endif
129 for(size_t i = 0; i < nVertices; i++) {
130 vertexNumbersData[i] = regionIdToVertexCountMap[vertexIds[i]];
131 }
132
133 output->GetPointData()->AddArray(vertexNumbers);
134 }
135
136 // generate cell number fields
137 {
138 auto cellNumbers = vtkSmartPointer<vtkDoubleArray>::New();
139 cellNumbers->SetNumberOfComponents(1);
140 cellNumbers->SetNumberOfTuples(nCells);
141 cellNumbers->SetName("CellNumber");
142 auto cellNumbersData = (double *)ttkUtils::GetVoidPointer(cellNumbers);
143
144#ifdef TTK_ENABLE_OPENMP
145#pragma omp parallel for num_threads(threadNumber)
146#endif
147 for(size_t i = 0; i < nCells; i++) {
148 cellNumbersData[i] = regionIdToCellCountMap[cellIds[i]];
149 }
150
151 output->GetCellData()->AddArray(cellNumbers);
152 }
153
154 this->printMsg(
155 "Computing component sizes", 1, t.getElapsedTime(), threadNumber);
156
157 return 1;
158}
#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 computes the connected components of a data-set, and that computes their size (nu...
~ttkComponentSize() override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int FillInputPortInformation(int port, vtkInformation *info) override
int FillOutputPortInformation(int port, vtkInformation *info) override
static void * GetVoidPointer(vtkDataArray *array, vtkIdType start=0)
Definition ttkUtils.cpp:226
int getThreadNumber() const
Definition BaseClass.h:76
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
void reStart()
Definition Timer.h:21
double getElapsedTime()
Definition Timer.h:15
vtkStandardNewMacro(ttkComponentSize)
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)