forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkHyperTreeGridAxisCut.cxx
322 lines (266 loc) · 9.88 KB
/
vtkHyperTreeGridAxisCut.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkHyperTreeGridAxisCut.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 "vtkHyperTreeGridAxisCut.h"
#include "vtkBitArray.h"
#include "vtkCellData.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkHyperTree.h"
#include "vtkHyperTreeCursor.h"
#include "vtkHyperTreeGrid.h"
#include "vtkHyperTreeGridCursor.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkNew.h"
#include "vtkPointData.h"
vtkStandardNewMacro(vtkHyperTreeGridAxisCut);
//-----------------------------------------------------------------------------
vtkHyperTreeGridAxisCut::vtkHyperTreeGridAxisCut()
{
// Default normal axis is Z
this->PlaneNormalAxis = 0;
// Default place intercept is 0
this->PlanePosition = 0.;
// Default mask is empty
this->MaterialMask = nullptr;
// Output indices begin at 0
this->CurrentId = 0;
}
//-----------------------------------------------------------------------------
vtkHyperTreeGridAxisCut::~vtkHyperTreeGridAxisCut()
{
if( this->MaterialMask )
{
this->MaterialMask->Delete();
this->MaterialMask = nullptr;
}
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridAxisCut::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "PlaneNormalAxis : " << this->PlaneNormalAxis << endl;
os << indent << "PlanePosition : " << this->PlanePosition << endl;
os << indent << "MaterialMask: " << this->MaterialMask << endl;
os << indent << "CurrentId: " << this->CurrentId << endl;
}
//-----------------------------------------------------------------------------
int vtkHyperTreeGridAxisCut::FillOutputPortInformation( int, vtkInformation *info )
{
info->Set( vtkDataObject::DATA_TYPE_NAME(), "vtkHyperTreeGrid" );
return 1;
}
//-----------------------------------------------------------------------------
int vtkHyperTreeGridAxisCut::ProcessTrees( vtkHyperTreeGrid* input,
vtkDataObject* outputDO )
{
// Skip empty trees
if (input->GetNumberOfLeaves() == 0)
{
return 1;
}
// Downcast output data object to hyper tree grid
vtkHyperTreeGrid* output = vtkHyperTreeGrid::SafeDownCast( outputDO );
if ( ! output )
{
vtkErrorMacro( "Incorrect type of output: "
<< outputDO->GetClassName() );
return 0;
}
// This filter works only with 3D grids
if ( input->GetDimension() != 3 )
{
vtkErrorMacro (<< "Bad input dimension:"
<< input->GetDimension());
return 0;
}
output->SetDimension( 2 );
// Retrieve normal axis and intercept of cut plane
int axis = this->PlaneNormalAxis;
double inter = this->PlanePosition;
// Set output orientation
output->SetOrientation( axis );
// Set output grid sizes; must be 1 in the direction of cut plane normal
unsigned int size[3];
input->GetGridSize( size );
size[axis] = 1;
output->SetGridSize( size );
// Duplicate coordinates along plane axes; set to zeros along normal
vtkNew<vtkDoubleArray> zeros;
zeros->SetNumberOfValues( 2 );
zeros->SetValue( 0, inter );
zeros->SetValue( 1, inter );
switch ( axis )
{
case 0:
// Cut along yz-plane
output->SetXCoordinates( zeros );
output->SetYCoordinates( input->GetYCoordinates() );
output->SetZCoordinates( input->GetZCoordinates());
break;
case 1:
// Cut along xz-plane
output->SetXCoordinates( input->GetXCoordinates() );
output->SetYCoordinates( zeros );
output->SetZCoordinates( input->GetZCoordinates());
break;
case 2:
// Cut along xz-plane
output->SetXCoordinates( input->GetXCoordinates() );
output->SetYCoordinates( input->GetYCoordinates() );
output->SetZCoordinates( zeros );
break;
default:
return 0;
}
// Other grid parameters are identical
output->SetTransposedRootIndexing( input->GetTransposedRootIndexing() );
output->SetBranchFactor( input->GetBranchFactor() );
output->SetHasInterface( input->GetHasInterface() );
output->SetInterfaceNormalsName( input->GetInterfaceNormalsName() );
output->SetInterfaceInterceptsName( input->GetInterfaceInterceptsName() );
// Initialize output point data
this->InData = input->GetPointData();
this->OutData = output->GetPointData();
this->OutData->CopyAllocate( this->InData );
// Output indices begin at 0
this->CurrentId = 0;
// Create material mask bit array if one is present on input
if( input->HasMaterialMask() )
{
this->MaterialMask = vtkBitArray::New();
}
// Retrieve material mask
vtkBitArray* mask
= this->MaterialMask ? input->GetMaterialMask() : nullptr;
// Storage for root cell Cartesian coordinates
unsigned int i,j,k;
// Storage for material mask indices computed together with output grid
vtkNew<vtkIdTypeArray> position;
// Iterate over all input hyper trees
vtkIdType inIndex;
vtkIdType outIndex = 0;
vtkHyperTreeGrid::vtkHyperTreeGridIterator it;
input->InitializeTreeIterator( it );
while ( it.GetNextTree( inIndex ) )
{
// Initialize new geometric cursor at root of current input tree
vtkHyperTreeGridCursor* inCursor = input->NewGeometricCursor( inIndex );
// Retrieve geometric features of input cursor
double* origin = inCursor->GetOrigin();
double* _size = inCursor->GetSize();
// Check whether root cell is intersected by plane
if ( origin[axis] <= inter && ( origin[axis] + _size[axis] >= inter ) )
{
// Root is intersected by plane, descend into current child
input->GetLevelZeroCoordinatesFromIndex( inIndex, i, j, k );
// Get root index into output hyper tree grid, depending on cut axes
switch ( axis )
{
case 0:
output->GetIndexFromLevelZeroCoordinates( outIndex, 0, j, k );
break;
case 1:
output->GetIndexFromLevelZeroCoordinates( outIndex, i, 0, k );
break;
case 2:
output->GetIndexFromLevelZeroCoordinates( outIndex, i, j, 0);
break;
default:
vtkErrorMacro( "Incorrect orientation of output: "
<< axis );
return 0;
} // switch ( axis )
// Initialize new cursor at root of current output tree
vtkHyperTreeCursor* outCursor = output->NewCursor( outIndex, true );
outCursor->ToRoot();
// Cut tree recursively
this->RecursivelyProcessTree( inCursor, outCursor, mask );
// Store current output index then increment it
position->InsertNextValue( outIndex );
++ outIndex;
// Clean up
outCursor->Delete();
} // if origin
// Clean up
inCursor->Delete();
} // it
// Set material mask index
output->SetMaterialMaskIndex( position );
// Squeeze and set output material mask if necessary
if( this->MaterialMask )
{
this->MaterialMask->Squeeze();
output->SetMaterialMask( this->MaterialMask );
}
return 1;
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridAxisCut::RecursivelyProcessTree( vtkHyperTreeGridCursor* inCursor,
vtkHyperTreeCursor* outCursor,
vtkBitArray* mask )
{
// Retrieve input grid
vtkHyperTreeGrid* input = inCursor->GetGrid();
// Retrieve global index of input cursor
vtkIdType inId = inCursor->GetGlobalNodeIndex();
// Increase index count on output: postfix is intended
vtkIdType outId = this->CurrentId ++;
// Retrieve output tree and set global index of output cursor
vtkHyperTree* outTree = outCursor->GetTree();
outTree->SetGlobalIndexFromLocal( outCursor->GetVertexId(), outId );
// Update material mask if relevant
if( mask )
{
this->MaterialMask->InsertValue( outId, mask->GetValue( inId ) );
}
// Copy output cell data from that of input cell
this->OutData->CopyData( this->InData, inId, outId );
// Descend further into input trees only if cursor is not at leaf
if ( ! inCursor->IsLeaf() )
{
// Cursor is not at leaf, subdivide output tree one level further
outTree->SubdivideLeaf( outCursor );
// Initialize output children index
int outChild = 0;
// If cursor is not at leaf, recurse to all children
int numChildren = input->GetNumberOfChildren();
for ( int inChild = 0; inChild < numChildren; ++ inChild )
{
// Create child cursor from parent
vtkHyperTreeGridCursor* childCursor = inCursor->Clone();
childCursor->ToChild( inChild );
// Retrieve normal axis and intercept of plane
int axis = this->PlaneNormalAxis;
double inter = this->PlanePosition;
// Retrieve geometric features of input cursor
double* origin = childCursor->GetOrigin();
double* size = childCursor->GetSize();
// Check whether child is intersected by plane
if ( origin[axis] <= inter && ( origin[axis] + size[axis] > inter ) )
{
// Child is intersected by plane, descend into current child
outCursor->ToChild( outChild );
// Recurse
this->RecursivelyProcessTree( childCursor, outCursor, mask );
// Return to parent
outCursor->ToParent();
// Increment output children count
++ outChild;
}
// Clean up
childCursor->Delete();
childCursor = nullptr;
} // inChild
} // if ( ! cursor->IsLeaf() )
}