OGS
VtkImageDataToSurfacePointsFilter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ** INCLUDES **
6
7#include <vtkIdList.h>
8#include <vtkImageData.h>
9#include <vtkInformation.h>
10#include <vtkInformationVector.h>
11#include <vtkLine.h>
12#include <vtkObjectFactory.h>
13#include <vtkPointData.h>
14#include <vtkPolyData.h>
15#include <vtkSmartPointer.h>
16
17#include <algorithm>
18#include <vector>
19
21
23 default;
24
25void VtkImageDataToSurfacePointsFilter::PrintSelf(ostream& os, vtkIndent indent)
26{
27 this->Superclass::PrintSelf(os, indent);
28}
29
31 int /*port*/, vtkInformation* info)
32{
33 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
34 return 1;
35}
36
38 vtkInformation* /*request*/,
39 vtkInformationVector** inputVector,
40 vtkInformationVector* outputVector)
41{
42 vtkDebugMacro(<< "Executing VtkImageDataToSurfacePointsFilter");
43
44 vtkInformation* input_info = inputVector[0]->GetInformationObject(0);
45 vtkInformation* output_info = outputVector->GetInformationObject(0);
46 vtkImageData* input = vtkImageData::SafeDownCast(
47 input_info->Get(vtkDataObject::DATA_OBJECT()));
48 vtkPolyData* output = vtkPolyData::SafeDownCast(
49 output_info->Get(vtkDataObject::DATA_OBJECT()));
50
51 void* pixvals = input->GetScalarPointer();
52 int n_comp = input->GetNumberOfScalarComponents();
53 if (n_comp < 1)
54 {
55 vtkDebugMacro("Error reading number of components.");
56 }
57 if (n_comp > 2)
58 {
59 vtkDebugMacro(
60 "RGB colours detected. Using only first channel for computation.");
61 }
62
63 auto const n_points = static_cast<std::size_t>(input->GetNumberOfPoints());
64 if (n_points == 0)
65 {
66 vtkDebugMacro("No data found!");
67 return -1;
68 }
69
70 double spacing[3];
71 input->GetSpacing(spacing);
72 int dimensions[3];
73 input->GetDimensions(dimensions);
74 double origin[3];
75 input->GetOrigin(origin);
76 MathLib::Point3d const ll(
77 std::array<double, 3>{{origin[0], origin[1], origin[2]}});
78
79 std::vector<double> pixels;
80 pixels.reserve(n_points);
81 for (std::size_t i = 0; i < n_points; ++i)
82 {
83 if ((n_comp == 2 || n_comp == 4) &&
84 ((static_cast<float*>(pixvals))[(i + 1) * n_comp - 1] <
85 0.00000001f))
86 {
87 pixels.push_back(-9999);
88 }
89 else
90 {
91 pixels.push_back((static_cast<float*>(pixvals))[i * n_comp]);
92 }
93 }
94 GeoLib::Raster const* const raster(new GeoLib::Raster(
95 {static_cast<std::size_t>(dimensions[0]),
96 static_cast<std::size_t>(dimensions[1]), 1, ll, spacing[0], -9999},
97 pixels.begin(),
98 pixels.end()));
99
100 vtkSmartPointer<vtkPoints> new_points = vtkSmartPointer<vtkPoints>::New();
101 new_points->SetNumberOfPoints(PointsPerPixel * n_points);
102 vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
103 cells->Allocate(PointsPerPixel * n_points);
104
105 double const half_cellsize(spacing[0] / 2.0);
106 std::size_t pnt_idx(0);
107 for (std::size_t i = 0; i < static_cast<std::size_t>(n_points); ++i)
108 {
109 // Skip transparent pixels
110 if (n_comp == 2 || n_comp == 4)
111 {
112 if ((static_cast<float*>(pixvals))[(i + 1) * n_comp - 1] <
113 0.00000001f)
114 {
115 continue;
116 }
117 }
118
119 double p[3];
120 input->GetPoint(i, p);
121 MathLib::Point3d min_pnt{std::array<double, 3>{
122 {p[0] - half_cellsize, p[1] - half_cellsize, 0}}};
123 MathLib::Point3d max_pnt{std::array<double, 3>{
124 {p[0] + half_cellsize, p[1] + half_cellsize, 0}}};
126 new_points, cells, pnt_idx, min_pnt, max_pnt, *raster);
127 pnt_idx += PointsPerPixel;
128 }
129
130 output->SetPoints(new_points);
131 output->SetVerts(cells);
132 output->Squeeze();
133
134 vtkDebugMacro(<< "Created " << new_points->GetNumberOfPoints()
135 << " points.");
136 return 1;
137}
138
140 vtkSmartPointer<vtkPoints>& points,
141 vtkSmartPointer<vtkCellArray>& cells,
142 std::size_t pnt_idx,
143 MathLib::Point3d const& min_pnt,
144 MathLib::Point3d const& max_pnt,
145 GeoLib::Raster const& raster)
146{
147 auto const n_points(static_cast<std::size_t>(this->GetPointsPerPixel()));
148 for (std::size_t i = 0; i < n_points; ++i)
149 {
150 double p[3];
151 p[0] = getRandomNumber(min_pnt[0], max_pnt[0]);
152 p[1] = getRandomNumber(min_pnt[1], max_pnt[1]);
153 p[2] = raster.interpolateValueAtPoint(
154 MathLib::Point3d(std::array<double, 3>{{p[0], p[1], 0}}));
155 points->SetPoint(pnt_idx + i, p);
156 cells->InsertNextCell(1);
157 cells->InsertCellPoint(pnt_idx + i);
158 }
159}
160
162 double const& min, double const& max) const
163{
164 return (static_cast<double>(std::rand()) / RAND_MAX) * (max - min) + min;
165}
vtkStandardNewMacro(VtkImageDataToSurfacePointsFilter)
Class Raster is used for managing raster data.
Definition Raster.h:39
double interpolateValueAtPoint(const MathLib::Point3d &pnt) const
Definition Raster.cpp:75
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
Updates the graphical object.
void createPointSurface(vtkSmartPointer< vtkPoints > &points, vtkSmartPointer< vtkCellArray > &cells, std::size_t pnt_idx, MathLib::Point3d const &min_pnt, MathLib::Point3d const &max_pnt, GeoLib::Raster const &raster)
Creates the point objects based on the parameters set by the user.
void PrintSelf(ostream &os, vtkIndent indent) override
Prints information about itself.
int FillInputPortInformation(int port, vtkInformation *info) override
Sets input port to vtkImageData.
double getRandomNumber(double const &min, double const &max) const
Returns a random number in [min, max].