ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
vtkBoundedVolumeSource.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include "vtkBoundingBox.h"
11 #include "vtkImageData.h"
12 #include "vtkInformation.h"
13 #include "vtkInformationVector.h"
14 #include "vtkNew.h"
15 #include "vtkObjectFactory.h"
16 #include "vtkStreamingDemandDrivenPipeline.h"
17 #include "vtkVectorOperators.h"
18 
20 //----------------------------------------------------------------------------
22  this->SetNumberOfInputPorts(0);
23  this->Origin[0] = this->Origin[1] = this->Origin[2] = 0.0;
24  this->Scale[0] = this->Scale[1] = this->Scale[2] = 1.0;
25  this->Resolution[0] = this->Resolution[1] = this->Resolution[2] = 64;
27  this->CellSize = 1.0;
28  this->Padding = 0;
29 }
30 
31 //----------------------------------------------------------------------------
33 
34 //----------------------------------------------------------------------------
36  vtkInformation*,
37  vtkInformationVector**,
38  vtkInformationVector* outputVector) {
39  vtkBoundingBox bbox(this->Origin[0], this->Origin[0] + this->Scale[0],
40  this->Origin[1], this->Origin[1] + this->Scale[1],
41  this->Origin[2], this->Origin[2] + this->Scale[2]);
42  bbox.Inflate(this->Padding);
43 
44  vtkNew<vtkImageData> tmp;
45  if (this->RefinementMode == USE_RESOLUTION) {
47  tmp, bbox, vtkVector3i(this->Resolution))) {
48  return 0;
49  }
50  } else {
52  this->CellSize)) {
53  return 0;
54  }
55  }
56 
57  vtkInformation* outInfo = outputVector->GetInformationObject(0);
58  outInfo->Set(vtkDataObject::ORIGIN(), tmp->GetOrigin(), 3);
59  outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
60  tmp->GetExtent(), 6);
61  outInfo->Set(vtkDataObject::SPACING(), tmp->GetSpacing(), 3);
62  vtkDataObject::SetPointDataActiveScalarInfo(outInfo, VTK_FLOAT, 1);
63  outInfo->Set(CAN_PRODUCE_SUB_EXTENT(), 1);
64  return 1;
65 }
66 
67 //----------------------------------------------------------------------------
69  vtkDataObject* vtkNotUsed(odata), vtkInformation* outInfo) {
70  vtkBoundingBox bbox(this->Origin[0], this->Origin[0] + this->Scale[0],
71  this->Origin[1], this->Origin[1] + this->Scale[1],
72  this->Origin[2], this->Origin[2] + this->Scale[2]);
73  bbox.Inflate(this->Padding);
74 
75  vtkImageData* data = vtkImageData::GetData(outInfo);
76  if (this->RefinementMode == USE_RESOLUTION) {
78  data, bbox, vtkVector3i(this->Resolution))) {
79  vtkErrorMacro("Failed to determine image parameters.");
80  return;
81  }
82  } else {
84  this->CellSize)) {
85  vtkErrorMacro("Failed to determine image parameters.");
86  return;
87  }
88  }
89 
90  // limit extent to update extent.
91  int* updateExt =
92  outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT());
93  data->SetExtent(updateExt);
94 }
95 
96 //----------------------------------------------------------------------------
98  const vtkBoundingBox& bbox,
99  const vtkVector3i& resolution) {
100  if (image == nullptr || !bbox.IsValid() || resolution[0] < 1 ||
101  resolution[1] < 1 || resolution[2] < 1) {
102  return false;
103  }
104 
105  image->SetExtent(0, resolution[0], 0, resolution[1], 0, resolution[2]);
106 
107  vtkVector3d lengths;
108  bbox.GetLengths(lengths.GetData());
109 
110  vtkVector3d origin;
111  bbox.GetMinPoint(origin[0], origin[1], origin[2]);
112  image->SetOrigin(origin.GetData());
113 
114  const vtkVector3d resolutionD(static_cast<double>(resolution[0]),
115  static_cast<double>(resolution[1]),
116  static_cast<double>(resolution[2]));
117  vtkVector3d spacing = lengths / resolutionD;
118  image->SetSpacing(spacing.GetData());
119  return true;
120 }
121 
122 //----------------------------------------------------------------------------
124  const vtkBoundingBox& bbox,
125  const double cellSize) {
126  if (image == nullptr || !bbox.IsValid() || cellSize <= 0) {
127  return false;
128  }
129 
130  vtkVector3d lengths;
131  bbox.GetLengths(lengths.GetData());
132 
133  vtkVector3i resolution;
134  resolution[0] = static_cast<int>(std::ceil(lengths[0] / cellSize));
135  resolution[1] = static_cast<int>(std::ceil(lengths[1] / cellSize));
136  resolution[2] = static_cast<int>(std::ceil(lengths[2] / cellSize));
137  assert(resolution[0] > 0 && resolution[1] > 0 && resolution[2] > 0);
138 
139  image->SetExtent(0, resolution[0], 0, resolution[1], 0, resolution[2]);
140 
141  // since old bounds may not exactly match, we compute new bounds keeping the
142  // center same.
143  vtkVector3d center;
144  bbox.GetCenter(center.GetData());
145 
146  lengths[0] = resolution[0] * cellSize;
147  lengths[1] = resolution[1] * cellSize;
148  lengths[2] = resolution[2] * cellSize;
149  vtkVector3d origin = center - (lengths / vtkVector3d(2.0));
150  image->SetOrigin(origin.GetData());
151  image->SetSpacing(cellSize, cellSize, cellSize);
152  return true;
153 }
154 
155 //----------------------------------------------------------------------------
156 void vtkBoundedVolumeSource::PrintSelf(ostream& os, vtkIndent indent) {
157  this->Superclass::PrintSelf(os, indent);
158  os << indent << "Origin: " << this->Origin[0] << ", " << this->Origin[1]
159  << ", " << this->Origin[2] << endl;
160  os << indent << "Scale: " << this->Scale[0] << ", " << this->Scale[1]
161  << ", " << this->Scale[2] << endl;
162  os << indent << "RefinementMode: " << this->RefinementMode << endl;
163  os << indent << "Resolution: " << this->Resolution[0] << ", "
164  << this->Resolution[1] << ", " << this->Resolution[2] << endl;
165  os << indent << "CellSize: " << this->CellSize << endl;
166 }
std::shared_ptr< core::Tensor > image
Eigen::Vector3d origin
Definition: VoxelGridIO.cpp:26
int RequestInformation(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
void ExecuteDataWithInformation(vtkDataObject *data, vtkInformation *outInfo) override
void PrintSelf(ostream &os, vtkIndent indent) override
static bool SetImageParameters(vtkImageData *image, const vtkBoundingBox &bbox, const vtkVector3i &resolution)
GraphType data
Definition: graph_cut.cc:138
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
vtkStandardNewMacro(vtkBoundedVolumeSource)