forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkImageToAMR.cxx
359 lines (304 loc) · 10.3 KB
/
vtkImageToAMR.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageToAMR.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkImageToAMR.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkOverlappingAMR.h"
#include "vtkSmartPointer.h"
#include "vtkTuple.h"
#include "vtkAMRBox.h"
#include "vtkUniformGrid.h"
#include "vtkNew.h"
#include "vtkAMRUtilities.h"
#include <algorithm>
#include <vector>
#include "vtkAMRInformation.h"
namespace
{
//Split one box to eight
int SplitXYZ(vtkAMRBox inBox, int refinementRatio, std::vector<vtkAMRBox>& out)
{
inBox.Refine(refinementRatio);
const int* lo = inBox.GetLoCorner();
const int* hi = inBox.GetHiCorner();
//the cartesian product A[0][0..n[0]] X A[1][0..n[1] X A[2][0..n[2]] is the refined grid
int A[3][3], n[3];
for(int d=0; d<3; d++)
{
A[d][0] = lo[d]-1;
A[d][2] = hi[d];
if(inBox.EmptyDimension(d))
{
n[d]=1;
A[d][1] = hi[d];
}
else
{
n[d]=2;
A[d][1] = (lo[d]+hi[d])/2;
}
}
//create the refined boxes and push them to the output stack
int numOut(0);
for(int i=0; i<n[0]; i++)
{
for(int j=0; j<n[1]; j++)
{
for(int k=0; k<n[2]; k++)
{
vtkAMRBox box;
box.SetDimensions( A[0][i]+1, A[1][j]+1, A[2][k]+1,
A[0][i+1], A[1][j+1], A[2][k+1]);
out.push_back(box);
numOut++;
}
}
}
return numOut;
}
int ComputeTreeHeight(int maxNumNodes, int degree)
{
if(maxNumNodes<=0)
{
return 0;
}
//could have used a formula, but this is more clear
int height = 1;
int numNodes= 1;
while(numNodes<=maxNumNodes)
{
numNodes = numNodes + degree*numNodes;
height++;
}
height--;
return height;
}
//split the blocks into a tree that starts out as a single stem
//than turn a full tree. This shape is designed so that numLevels and maxNumBlocks
//constraint can be satisfied
void Split(const vtkAMRBox& rootBox, int numLevels, int refinementRatio, int maxNumBlocks,
std::vector<std::vector<vtkAMRBox> >& out)
{
out.clear();
out.resize(1);
out.back().push_back(rootBox);
maxNumBlocks--;
int treeDegree = rootBox.ComputeDimension()*2;
int numTreeLevels= std::min(numLevels,ComputeTreeHeight(maxNumBlocks-(numLevels-1), treeDegree))-1; //minus one because root already has one
int level=1;
for(; level<numLevels-numTreeLevels; level++)
{
out.push_back(std::vector<vtkAMRBox>());
const std::vector<vtkAMRBox>& parentBoxes = out[level-1];
std::vector<vtkAMRBox>& childBoxes = out[level];
vtkAMRBox child = parentBoxes.back();
child.Refine(refinementRatio);
childBoxes.push_back(child);
}
for(; level<numLevels;level++)
{
out.push_back(std::vector<vtkAMRBox>());
const std::vector<vtkAMRBox>& parentBoxes = out[level-1];
std::vector<vtkAMRBox>& childBoxes = out[level];
for(size_t i = 0;i<parentBoxes.size();i++)
{
const vtkAMRBox& parent = parentBoxes[i];
SplitXYZ(parent,refinementRatio,childBoxes);
}
}
};
//create a grid by sampling from input using the indices in box
vtkUniformGrid* ConstructGrid(vtkImageData *input, const vtkAMRBox& box, int coarsenRatio, double* origin, double* spacing)
{
int numPoints[3];
box.GetNumberOfNodes(numPoints);
vtkUniformGrid* grid = vtkUniformGrid::New();
grid->Initialize();
grid->SetDimensions(numPoints);
grid->SetSpacing(spacing);
grid->SetOrigin(origin);
vtkPointData *inPD=input->GetPointData(), *outPD = grid->GetPointData();
vtkCellData *inCD=input->GetCellData(), *outCD = grid->GetCellData();
outPD->CopyAllocate(inPD, grid->GetNumberOfPoints());
outCD->CopyAllocate(inCD, grid->GetNumberOfCells());
vtkAMRBox box0(box);
box0.Refine(coarsenRatio); //refine it to the image data level
int extents[6]; input->GetExtent(extents);
int imLo[3] = {extents[0],extents[2], extents[4]};
const int *lo=box.GetLoCorner();
for( int iz=0; iz<numPoints[2]; iz++ )
{
for( int iy=0; iy<numPoints[1]; iy++ )
{
for( int ix=0; ix<numPoints[0]; ix++ )
{
int ijkDst[3] = {ix,iy,iz};
vtkIdType idDst = grid->ComputePointId(ijkDst);
int ijkSrc[3] = {(lo[0]+ix)*coarsenRatio + imLo[0],
(lo[1]+iy)*coarsenRatio + imLo[1],
(lo[2]+iz)*coarsenRatio + imLo[2]};
vtkIdType idSrc = input->ComputePointId(ijkSrc);
outPD->CopyData(inPD,idSrc, idDst);
}
}
}
int numCells[3];
for(int d=0; d<3; d++)
{
numCells[d] = std::max(numPoints[d]-1,1);
}
for( int iz=0; iz<numCells[2]; iz++ )
{
for( int iy=0; iy<numCells[1]; iy++ )
{
for( int ix=0; ix<numCells[0]; ix++ )
{
int ijkDst[3] = {ix,iy,iz};
vtkIdType idDst = grid->ComputeCellId(ijkDst);
int ijkSrc[3] = {(lo[0]+ix)*coarsenRatio + imLo[0],
(lo[1]+iy)*coarsenRatio + imLo[1],
(lo[2]+iz)*coarsenRatio + imLo[2]};
vtkIdType idSrc = input->ComputeCellId(ijkSrc);
outCD->CopyData(inCD,idSrc, idDst);
}
}
}
return grid;
}
};
vtkStandardNewMacro(vtkImageToAMR);
//----------------------------------------------------------------------------
vtkImageToAMR::vtkImageToAMR()
{
this->NumberOfLevels = 2;
this->RefinementRatio = 2;
this->MaximumNumberOfBlocks = 100;
}
//----------------------------------------------------------------------------
vtkImageToAMR::~vtkImageToAMR()
{
}
//----------------------------------------------------------------------------
int vtkImageToAMR::FillInputPortInformation(int , vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(),"vtkImageData");
return 1;
}
//----------------------------------------------------------------------------
int vtkImageToAMR::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkImageData* input = vtkImageData::GetData(inputVector[0], 0);
vtkOverlappingAMR* amr = vtkOverlappingAMR::GetData(outputVector);
if(input->GetDataDimension()<2)
{
vtkErrorMacro("Image dimension must be at least two.");
return 0;
}
int whole_extent[6];
inInfo->Get(vtkCompositeDataPipeline::WHOLE_EXTENT(), whole_extent);
int dims[3] = { whole_extent[1] - whole_extent[0] + 1,
whole_extent[3] - whole_extent[2] + 1,
whole_extent[5] - whole_extent[4] + 1 };
double inputBounds[6];
input->GetBounds(inputBounds);
double inputOrigin[3]= {inputBounds[0],inputBounds[2],inputBounds[4]};
double inputSpacing[3];
input->GetSpacing(inputSpacing);
int gridDescription = vtkStructuredData::GetDataDescription(dims);
//check whether the parameters are valid
//and compute the base image resolution
int dims0[3];
double spacing0[3];
for(int d=0; d<3; d++)
{
if(dims[d]<=1)
{
if(dims[d]==0)
{
vtkWarningMacro("Zero dimension? Really?");
}
dims0[d] = 1;
spacing0[d] = 1.0;
}
else
{
int r = (int)(pow(static_cast<double>(this->RefinementRatio),this->NumberOfLevels-1));
if((dims[d]-1)%r!=0)
{
vtkErrorMacro("Image cannot be refined");
return 0;
}
dims0[d] = 1+(dims[d]-1)/r;
spacing0[d] = r*inputSpacing[d];
}
}
vtkAMRBox rootBox(inputOrigin, dims0, spacing0, inputOrigin, gridDescription);
std::vector<std::vector<vtkAMRBox> > amrBoxes;
Split(rootBox,this->NumberOfLevels, this->RefinementRatio, this->MaximumNumberOfBlocks, amrBoxes);
std::vector<int> blocksPerLevel;
for(size_t i=0; i<amrBoxes.size();i++)
{
blocksPerLevel.push_back(static_cast<int>(amrBoxes[i].size()));
}
unsigned int numLevels = static_cast<unsigned int>(blocksPerLevel.size());
amr->Initialize(static_cast<int>(numLevels), &blocksPerLevel[0]);
amr->SetOrigin(inputOrigin);
amr->SetGridDescription(gridDescription);
double spacingi[3] = {spacing0[0],spacing0[1],spacing0[2]};
for(unsigned int i=0; i<numLevels; i++)
{
amr->SetSpacing(i,spacingi);
for(int d=0;d<3;d++)
{
spacingi[d]/=this->RefinementRatio;
}
}
for(unsigned int level = 0; level<numLevels; level++)
{
const std::vector<vtkAMRBox>& boxes = amrBoxes[level];
for(size_t i=0; i<boxes.size();i++)
{
amr->SetAMRBox(level,static_cast<unsigned int>(i), boxes[i]);
}
}
for(unsigned int level = 0; level< numLevels; level++)
{
double spacing[3];
amr->GetSpacing(level, spacing);
int coarsenRatio = (int)pow( static_cast<double>(this->RefinementRatio), static_cast<int>(numLevels- 1 - level));//againt the finest level
for(size_t i=0; i<amr->GetNumberOfDataSets(level);i++)
{
const vtkAMRBox& box = amr->GetAMRBox(level,static_cast<unsigned int>(i));
double origin[3];
vtkAMRBox::GetBoxOrigin(box,inputOrigin,spacing,origin);
vtkUniformGrid* grid = ConstructGrid(input,box,coarsenRatio,origin,spacing);
amr->SetDataSet(level,static_cast<unsigned int>(i), grid);
grid->Delete();
}
}
vtkAMRUtilities::BlankCells(amr);
return 1;
}
//----------------------------------------------------------------------------
void vtkImageToAMR::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "NumberOfLevels: " << this->NumberOfLevels << endl;
os << indent << "RefinementRatio: " << this->RefinementRatio << endl;
}