TTK
Loading...
Searching...
No Matches
ttkDepthImageBasedGeometryApproximation.cpp
Go to the documentation of this file.
2#include <ttkMacros.h>
3#include <ttkUtils.h>
4
5#include <vtkCellData.h>
6#include <vtkImageData.h>
7#include <vtkMultiBlockDataSet.h>
8#include <vtkPointData.h>
9#include <vtkSmartPointer.h>
10#include <vtkUnstructuredGrid.h>
11
12#include <vtkCellArray.h>
13#include <vtkDoubleArray.h>
14
15#include <vtkInformation.h>
16// #include <vtkInformationVector.h>
17
19
22 this->SetNumberOfInputPorts(1);
23 this->SetNumberOfOutputPorts(1);
24}
27 = default;
28
30 int port, vtkInformation *info) {
31 if(port == 0)
32 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkMultiBlockDataSet");
33 else
34 return 0;
35 return 1;
36}
37
39 int port, vtkInformation *info) {
40 if(port == 0)
42 else
43 return 0;
44 return 1;
45}
46
48 vtkInformation *ttkNotUsed(request),
49 vtkInformationVector **inputVector,
50 vtkInformationVector *outputVector) {
51 ttk::Timer globalTimer;
52
53 // Prepare input and output
54 auto inputMBD = vtkMultiBlockDataSet::GetData(inputVector[0]);
55 auto outputMBD = vtkMultiBlockDataSet::GetData(outputVector);
56
57 // Process each depth image individually
58 size_t nBlocks = inputMBD->GetNumberOfBlocks();
59 for(size_t i = 0; i < nBlocks; i++) {
60 // Get vtkImageData
61 auto inputImage = vtkImageData::SafeDownCast(inputMBD->GetBlock(i));
62
63 if(inputImage == nullptr) {
64 continue;
65 }
66
67 // Get depth array
68 auto depthArray = this->GetInputArrayToProcess(0, inputImage);
69
70 // Get camera parameters from field data
71 auto inputFD = inputImage->GetFieldData();
72
73 auto camHeight
74 = vtkDoubleArray::SafeDownCast(inputFD->GetAbstractArray("CamHeight"));
75 auto camPosition
76 = vtkDoubleArray::SafeDownCast(inputFD->GetAbstractArray("CamPosition"));
77 auto camDirection
78 = vtkDoubleArray::SafeDownCast(inputFD->GetAbstractArray("CamDirection"));
79 auto camUp
80 = vtkDoubleArray::SafeDownCast(inputFD->GetAbstractArray("CamUp"));
81 auto camNearFar
82 = vtkDoubleArray::SafeDownCast(inputFD->GetAbstractArray("CamNearFar"));
83 auto resolution
84 = vtkDoubleArray::SafeDownCast(inputFD->GetAbstractArray("Resolution"));
85
86 // Check if all parameters are present
87 if(depthArray == nullptr || camHeight == nullptr || camPosition == nullptr
88 || camDirection == nullptr || camUp == nullptr || camNearFar == nullptr
89 || resolution == nullptr) {
90 this->printErr("Input depth image does not have all of the required "
91 "field data arrays (see Cinema Spec D - Data Product "
92 "Specification)");
93 return 0;
94 }
95
96 const size_t resX = resolution->GetValue(0);
97 const size_t resY = resolution->GetValue(1);
98
99 // Initialize output point buffer that holds one point for each input depth
100 // image pixel
101 auto points = vtkSmartPointer<vtkPoints>::New();
102 points->SetNumberOfPoints(resX * resY);
103
104 // Prepare output cell buffer that holds two triangles for every quad
105 // consisting of 4 vertices
107 const size_t nTriangles = 2 * (resX - 1) * (resY - 1);
108
109 auto triangleDistortions = vtkSmartPointer<vtkDoubleArray>::New();
110 triangleDistortions->SetName("TriangleDistortion");
111 triangleDistortions->SetNumberOfComponents(1);
112 triangleDistortions->SetNumberOfTuples(nTriangles);
113
114 auto connectivityArray = vtkSmartPointer<vtkIntArray>::New();
115 connectivityArray->SetNumberOfTuples(3 * nTriangles);
116
117 auto offsetArray = vtkSmartPointer<vtkIntArray>::New();
118 offsetArray->SetNumberOfTuples(nTriangles + 1);
119
120 // Approximate geometry
121 int status = 0;
122 switch(depthArray->GetDataType()) {
123 vtkTemplateMacro(
124 (status = this->execute<VTK_TT, int>(
125 // Output
126 static_cast<float *>(ttkUtils::GetVoidPointer(points)),
127 static_cast<double *>(ttkUtils::GetVoidPointer(triangleDistortions)),
128 static_cast<int *>(ttkUtils::GetVoidPointer(connectivityArray)),
129 static_cast<int *>(ttkUtils::GetVoidPointer(offsetArray)),
130
131 // Input
132 static_cast<VTK_TT *>(ttkUtils::GetVoidPointer(depthArray)),
133 static_cast<double *>(ttkUtils::GetVoidPointer(camPosition)),
134 static_cast<double *>(ttkUtils::GetVoidPointer(camDirection)),
135 static_cast<double *>(ttkUtils::GetVoidPointer(camUp)),
136 static_cast<double *>(ttkUtils::GetVoidPointer(camNearFar)),
137 static_cast<double *>(ttkUtils::GetVoidPointer(camHeight)),
138 static_cast<double *>(ttkUtils::GetVoidPointer(resolution)))));
139 }
140 if(!status)
141 return 0;
142
143 // Represent approximated geometry via VTK
145 {
146 auto cellArray = vtkSmartPointer<vtkCellArray>::New();
147 cellArray->SetData(offsetArray, connectivityArray);
148
149 mesh->SetPoints(points);
150 mesh->SetCells(VTK_TRIANGLE, cellArray);
151 mesh->GetCellData()->AddArray(triangleDistortions);
152 }
153
154 // copy image point data to point data of approximation
155 {
156 auto meshPD = mesh->GetPointData();
157 auto inputPD = inputImage->GetPointData();
158 for(int p = 0; p < inputPD->GetNumberOfArrays(); p++)
159 meshPD->AddArray(inputPD->GetAbstractArray(p));
160
161 outputMBD->SetBlock(i, mesh);
162 }
163 }
164
165 // Print status
167 this->printMsg("Complete (#images: " + std::to_string(nBlocks) + ")", 1,
168 globalTimer.getElapsedTime(), this->threadNumber_);
170
171 return 1;
172}
#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 approximates the geometry that is depicted by a set of depth images.
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:225
int printMsg(const std::string &msg, const debug::Priority &priority=debug::Priority::INFO, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cout) const
Definition: Debug.h:118
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition: Debug.h:149
double getElapsedTime()
Definition: Timer.h:15
vtkStandardNewMacro(ttkDepthImageBasedGeometryApproximation)