forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkHyperTreeGridSource.cxx
1542 lines (1344 loc) · 45.3 KB
/
vtkHyperTreeGridSource.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkHyperTreeGridSource.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 "vtkHyperTreeGridSource.h"
#include "vtkBitArray.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkHyperTree.h"
#include "vtkHyperTreeCursor.h"
#include "vtkHyperTreeGrid.h"
#include "vtkIdTypeArray.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkQuadric.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include <sstream>
#include <cassert>
vtkStandardNewMacro(vtkHyperTreeGridSource);
vtkCxxSetObjectMacro(vtkHyperTreeGridSource, DescriptorBits, vtkBitArray);
vtkCxxSetObjectMacro(vtkHyperTreeGridSource, MaterialMaskBits, vtkBitArray);
vtkCxxSetObjectMacro(vtkHyperTreeGridSource, Quadric, vtkQuadric);
//----------------------------------------------------------------------------
vtkHyperTreeGridSource::vtkHyperTreeGridSource()
{
// This a source: no input ports
this->SetNumberOfInputPorts( 0 );
// Grid parameters
this->BranchFactor = 2;
this->MaximumLevel = 1;
this->BlockSize = 0;
// Grid topology
this->Dimension = 3;
this->Orientation = 0;
this->GridSize[0] = 1;
this->GridSize[1] = 1;
this->GridSize[2] = 1;
this->TransposedRootIndexing = false;
// Grid geometry
this->Origin[0] = 0.;
this->Origin[1] = 0.;
this->Origin[2] = 0.;
this->GridScale[0] = 1.;
this->GridScale[1] = 1.;
this->GridScale[2] = 1.;
this->XCoordinates = vtkDoubleArray::New();
this->XCoordinates->SetNumberOfTuples( 2 );
this->XCoordinates->SetComponent( 0, 0., 0. );
this->XCoordinates->SetComponent( 1, 0., this->GridScale[0] );
this->YCoordinates = vtkDoubleArray::New();
this->YCoordinates->SetNumberOfTuples( 2 );
this->YCoordinates->SetComponent( 0, 0., 0. );
this->YCoordinates->SetComponent( 1, 0., this->GridScale[1] );
this->ZCoordinates = vtkDoubleArray::New();
this->ZCoordinates->SetNumberOfTuples( 2 );
this->ZCoordinates->SetComponent( 0, 0., 0. );
this->ZCoordinates->SetComponent( 1, 0., this->GridScale[2] );
// By default use the descriptor string
this->UseDescriptor = true;
// By default do not use the material mask
this->UseMaterialMask = false;
// By default do not generate interface vector fields
this->GenerateInterfaceFields = false;
// Grid description & material mask as strings
this->Descriptor = new char[2];
this->Descriptor[0] = '.';
this->Descriptor[1] = 0;
this->MaterialMask = new char[2];
this->MaterialMask[0] = '0';
this->MaterialMask[1] = 0;
// Grid description & material mask as bit arrays
this->DescriptorBits = nullptr;
this->MaterialMaskBits = nullptr;
this->LevelZeroMaterialIndex = nullptr;
this->LevelZeroMaterialMap.clear();
// Default quadric is a sphere with radius 1 centered at origin
this->Quadric = vtkQuadric::New();
this->Quadric->SetCoefficients( 1., 1., 1.,
0., 0., 0.,
0., 0., 0.,
-1. );
}
//----------------------------------------------------------------------------
vtkHyperTreeGridSource::~vtkHyperTreeGridSource()
{
if ( this->XCoordinates )
{
this->XCoordinates->UnRegister( this );
this->XCoordinates = nullptr;
}
if ( this->YCoordinates )
{
this->YCoordinates->UnRegister( this );
this->YCoordinates = nullptr;
}
if ( this->ZCoordinates )
{
this->ZCoordinates->UnRegister( this );
this->ZCoordinates = nullptr;
}
if ( this->DescriptorBits )
{
this->DescriptorBits->UnRegister( this );
this->DescriptorBits = nullptr;
}
if ( this->MaterialMaskBits )
{
this->MaterialMaskBits->UnRegister( this );
this->MaterialMaskBits = nullptr;
}
if ( this->LevelZeroMaterialIndex )
{
this->LevelZeroMaterialIndex->UnRegister( this );
this->LevelZeroMaterialIndex = nullptr;
}
this->LevelZeroMaterialMap.clear();
delete [] this->Descriptor;
this->Descriptor = nullptr;
delete [] this->MaterialMask;
this->MaterialMask = nullptr;
if ( this->Quadric )
{
this->Quadric->UnRegister( this );
this->Quadric = nullptr;
}
}
//-----------------------------------------------------------------------------
void vtkHyperTreeGridSource::PrintSelf( ostream& os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "GridSize: "
<< this->GridSize[0] <<","
<< this->GridSize[1] <<","
<< this->GridSize[2] << endl;
os << indent << "Origin: "
<< this->Origin[0] <<","
<< this->Origin[1] <<","
<< this->Origin[2] << endl;
os << indent << "GridScale: "
<< this->GridScale[0] <<","
<< this->GridScale[1] <<","
<< this->GridScale[2] << endl;
os << indent << "MaximumLevel: " << this->MaximumLevel << endl;
os << indent << "Dimension: " << this->Dimension << endl;
os << indent << "Orientation: " << this->Orientation << endl;
os << indent << "BranchFactor: " << this->BranchFactor << endl;
os << indent << "BlockSize: " << this->BlockSize << endl;
os << indent << "TransposedRootIndexing: " << this->TransposedRootIndexing << endl;
if ( this->XCoordinates )
{
this->XCoordinates->PrintSelf( os, indent.GetNextIndent() );
}
if ( this->YCoordinates )
{
this->YCoordinates->PrintSelf( os, indent.GetNextIndent() );
}
if ( this->ZCoordinates )
{
this->ZCoordinates->PrintSelf( os, indent.GetNextIndent() );
}
os << indent << "UseDescriptor: " << this->UseDescriptor << endl;
os << indent << "UseMaterialMask: " << this->UseMaterialMask << endl;
os << indent << "GenerateInterfaceFields:" << this->GenerateInterfaceFields << endl;
os << indent << "LevelZeroMaterialIndex: " << this->LevelZeroMaterialIndex << endl;
os << indent << "Descriptor: " << this->Descriptor << endl;
os << indent << "MaterialMask: " << this->MaterialMask << endl;
os << indent << "LevelDescriptors: " << this->LevelDescriptors.size() << endl;
os << indent << "LevelMaterialMasks: " << this->LevelMaterialMasks.size() << endl;
os << indent << "LevelCounters: " << this->LevelCounters.size() << endl;
if ( this->Quadric )
{
this->Quadric->PrintSelf( os, indent.GetNextIndent() );
}
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridSource::SetIndexingModeToKJI()
{
this->SetTransposedRootIndexing( false );
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridSource::SetIndexingModeToIJK()
{
this->SetTransposedRootIndexing( true );
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridSource::SetLevelZeroMaterialIndex( vtkIdTypeArray* indexArray )
{
if ( this->LevelZeroMaterialIndex == indexArray )
{
return;
}
if ( this->LevelZeroMaterialIndex )
{
this->LevelZeroMaterialIndex->UnRegister( this );
}
this->LevelZeroMaterialIndex = indexArray;
this->LevelZeroMaterialIndex->Register( this );
this->LevelZeroMaterialMap.clear();
vtkIdType len = indexArray->GetNumberOfTuples();
// Fill the map index - key is leaf number, value is index in the array that
// will be used to fetch the descriptor value.
for ( vtkIdType i = 0; i < len; ++ i )
{
this->LevelZeroMaterialMap[ indexArray->GetValue( i ) ] = i;
}
this->Modified();
}
//----------------------------------------------------------------------------
unsigned int vtkHyperTreeGridSource::GetMaximumLevel()
{
assert( "post: positive_result" && this->MaximumLevel >= 1 );
return this->MaximumLevel;
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridSource::SetMaximumLevel( unsigned int levels )
{
if ( levels < 1 )
{
levels = 1;
}
if ( this->MaximumLevel == levels )
{
return;
}
this->MaximumLevel = levels;
this->Modified();
assert( "post: is_set" && this->GetMaximumLevel() == levels );
}
//----------------------------------------------------------------------------
int vtkHyperTreeGridSource::FillOutputPortInformation( int, vtkInformation* info )
{
info->Set( vtkDataObject::DATA_TYPE_NAME(), "vtkHyperTreeGrid" );
return 1;
}
//----------------------------------------------------------------------------
int vtkHyperTreeGridSource::RequestInformation( vtkInformation*,
vtkInformationVector**,
vtkInformationVector* outputVector )
{
// Get the information objects
vtkInformation* outInfo = outputVector->GetInformationObject( 0 );
// We cannot give the exact number of levels of the hypertrees
// because it is not generated yet and this process depends on the recursion formula.
// Just send an upper limit instead.
outInfo->Set( vtkHyperTreeGrid::LEVELS(), this->MaximumLevel );
outInfo->Set( vtkHyperTreeGrid::DIMENSION(), this->Dimension );
outInfo->Set( vtkHyperTreeGrid::ORIENTATION(), this->Orientation );
double origin[3];
origin[0] = this->XCoordinates->GetTuple1( 0 );
origin[1] = this->YCoordinates->GetTuple1( 0 );
origin[2] = this->ZCoordinates->GetTuple1( 0 );
outInfo->Set( vtkDataObject::ORIGIN(), origin, 3 );
int extent[6];
extent[0] = 0;
extent[1] = this->GridSize[0] - 1;
extent[2] = 0;
extent[3] = this->GridSize[1] - 1;
extent[4] = 0;
extent[5] = this->GridSize[2] - 1;
outInfo->Set( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), extent, 6 );
return 1;
}
//----------------------------------------------------------------------------
int vtkHyperTreeGridSource::RequestData( vtkInformation*,
vtkInformationVector**,
vtkInformationVector* outputVector )
{
// Retrieve the output
vtkDataObject* outputDO = vtkDataObject::GetData( outputVector, 0 );
vtkHyperTreeGrid* output = vtkHyperTreeGrid::SafeDownCast( outputDO );
if ( ! output )
{
return 0;
}
vtkPointData* outData = output->GetPointData();
this->LevelBitsIndexCnt.clear();
this->LevelBitsIndexCnt.push_back(0);
// When using descriptor-based definition, initialize descriptor parsing
if ( this->UseDescriptor )
{
// Calculate refined block size
this->BlockSize = this->BranchFactor;
for ( unsigned int i = 1; i < this->Dimension; ++ i )
{
this->BlockSize *= this->BranchFactor;
}
if ( ! this->DescriptorBits && ! this->InitializeFromStringDescriptor() )
{
return 0;
}
else if ( this->DescriptorBits && ! this->InitializeFromBitsDescriptor() )
{
return 0;
}
} // if this->UseDescriptor
// Set straightforward grid parameters
output->SetTransposedRootIndexing( this->TransposedRootIndexing );
output->SetDimension( this->Dimension );
output->SetOrientation( this->Orientation );
output->SetBranchFactor( this->BranchFactor );
output->SetMaterialMaskIndex( this->LevelZeroMaterialIndex );
// Set parameters that depend on dimension
switch ( this->Dimension )
{
case 1:
{
// Set 1D grid size depending on orientation
unsigned int axis = this->Orientation;
unsigned int gs[] = { 1, 1, 1 };
unsigned n = this->GridSize[axis];
gs[axis] = n;
output->SetGridSize( gs );
// Create null coordinate array for non-existent dimensions
++ n;
vtkNew<vtkDoubleArray> zeros;
zeros->SetNumberOfValues( 2 );
zeros->SetValue( 0, 0. );
zeros->SetValue( 1, 0. );
// Create coordinate array for existent dimension
vtkNew<vtkDoubleArray> coords;
coords->SetNumberOfValues( n );
for ( unsigned int i = 0; i < n; ++ i )
{
double coord = this->Origin[axis] + this->GridScale[axis] * static_cast<double>( i );
coords->SetValue( i, coord );
} // i
// Assign coordinates
switch ( axis )
{
case 0:
output->SetXCoordinates( coords );
output->SetYCoordinates( zeros );
output->SetZCoordinates( zeros );
break;
case 1:
output->SetXCoordinates( zeros );
output->SetYCoordinates( coords );
output->SetZCoordinates( zeros );
break;
case 2:
output->SetXCoordinates( zeros );
output->SetYCoordinates( zeros );
output->SetZCoordinates( coords );
break;
} // switch ( axis )
zeros->SetValue( 1, 0. );
} // case 1
break;
case 2:
{
// Set grid size depending on orientation
unsigned int n[3];
memcpy( n, this->GridSize, 3 * sizeof( unsigned int ) );
n[this->Orientation] = 1;
output->SetGridSize( n );
// Create null coordinate array for non-existent dimension
vtkNew<vtkDoubleArray> zeros;
zeros->SetNumberOfValues( 2 );
zeros->SetValue( 0, 0. );
zeros->SetValue( 1, 0. );
// Create null coordinate arrays for existent dimensions
unsigned int axis1 = ( this->Orientation + 1 ) % 3;
vtkNew<vtkDoubleArray> coords1;
unsigned int n1 = this->GridSize[axis1] + 1;
coords1->SetNumberOfValues( n1 );
for ( unsigned int i = 0; i < n1; ++ i )
{
double coord = this->Origin[axis1] + this->GridScale[axis1] * static_cast<double>( i );
coords1->SetValue( i, coord );
} // i
unsigned int axis2 = ( this->Orientation + 2 ) % 3;
vtkNew<vtkDoubleArray> coords2;
unsigned int n2 = this->GridSize[axis2] + 1;
coords2->SetNumberOfValues( n2 );
for ( unsigned int i = 0; i < n2; ++ i )
{
double coord = this->Origin[axis2] + this->GridScale[axis2] * static_cast<double>( i );
coords2->SetValue( i, coord );
} // i
// Assign coordinates
switch ( this->Orientation )
{
case 0:
output->SetXCoordinates( zeros );
output->SetYCoordinates( coords1 );
output->SetZCoordinates( coords2 );
break;
case 1:
output->SetXCoordinates( coords2 );
output->SetYCoordinates( zeros );
output->SetZCoordinates( coords1 );
break;
case 2:
output->SetXCoordinates( coords1 );
output->SetYCoordinates( coords2 );
output->SetZCoordinates( zeros );
break;
} // switch ( this->Orientation )
} // case 2
break;
case 3:
{
// Set grid size
output->SetGridSize( this->GridSize );
// Create x-coordinates array
vtkNew<vtkDoubleArray> coordsx;
unsigned int nx = this->GridSize[0] + 1;
coordsx->SetNumberOfValues( nx );
for ( unsigned int i = 0; i < nx; ++ i )
{
double coord = this->Origin[0] + this->GridScale[0] * static_cast<double>( i );
coordsx->SetValue( i, coord );
} // i
// Create y-coordinates array
vtkNew<vtkDoubleArray> coordsy;
unsigned int ny = this->GridSize[1] + 1;
coordsy->SetNumberOfValues( ny );
for ( unsigned int i = 0; i < ny; ++ i )
{
double coord = this->Origin[1] + this->GridScale[1] * static_cast<double>( i );
coordsy->SetValue( i, coord );
} // i
// Create z-coordinates array
vtkNew<vtkDoubleArray> coordsz;
unsigned int nz = this->GridSize[2] + 1;
coordsz->SetNumberOfValues( nz );
for ( unsigned int i = 0; i < nz; ++ i )
{
double coord = this->Origin[2] + this->GridScale[2] * static_cast<double>( i );
coordsz->SetValue( i, coord );
} // i
// Assign coordinates
output->SetXCoordinates( coordsx );
output->SetYCoordinates( coordsy );
output->SetZCoordinates( coordsz );
break;
} // case 3
default:
vtkErrorMacro(<<"Unsupported dimension: "
<< this->Dimension
<< ".");
return 0;
} // switch ( this->Dimension )
// Prepare array of doubles for depth values
vtkNew<vtkDoubleArray> depthArray;
depthArray->SetName( "Depth" );
depthArray->SetNumberOfComponents( 1 );
outData->SetScalars( depthArray );
if ( this->GenerateInterfaceFields )
{
// Prepare arrays of triples for interface surrogates
vtkNew<vtkDoubleArray> normalsArray;
normalsArray->SetName( "Normals" );
normalsArray->SetNumberOfComponents( 3 );
outData->SetVectors( normalsArray );
vtkNew<vtkDoubleArray> interceptsArray;
interceptsArray->SetName( "Intercepts" );
interceptsArray->SetNumberOfComponents( 3 );
outData->AddArray( interceptsArray );
}
if ( ! this->UseDescriptor )
{
// Prepare array of doubles for quadric values
vtkNew<vtkDoubleArray> quadricArray;
quadricArray->SetName( "Quadric" );
quadricArray->SetNumberOfComponents( 1 );
outData->AddArray( quadricArray );
}
// Iterate over constituting hypertrees
if ( ! this->ProcessTrees( 0, outputDO ) )
{
return 0;
}
// Squeeze output data arrays
for ( int a = 0; a < outData->GetNumberOfArrays(); ++ a )
{
outData->GetArray( a )->Squeeze();
}
assert( "post: dataset_and_data_size_match" && output->CheckAttributes() == 0 );
this->LevelBitsIndexCnt.clear();
this->LevelBitsIndex.clear();
return 1;
}
//----------------------------------------------------------------------------
int vtkHyperTreeGridSource::ProcessTrees( vtkHyperTreeGrid*,
vtkDataObject* outputDO )
{
// Downcast output data object to hyper tree grid
vtkHyperTreeGrid* output = vtkHyperTreeGrid::SafeDownCast( outputDO );
if ( ! output )
{
vtkErrorMacro( "Incorrect type of output: "
<< outputDO->GetClassName() );
return 0;
}
// Generate grid of empty trees
output->GenerateTrees();
// Iterate over all hyper trees
vtkIdType index;
vtkHyperTreeGrid::vtkHyperTreeGridIterator it;
output->InitializeTreeIterator( it );
while ( vtkHyperTree* tree = it.GetNextTree( index ) )
{
unsigned int i, j, k;
output->GetLevelZeroCoordinatesFromIndex( index, i, j, k );
// Initialize cursor
vtkHyperTreeCursor* cursor = output->NewCursor( index );
if ( ! cursor )
{
continue;
}
cursor->ToRoot();
// Initialize local cell index
int idx[3] = { 0, 0, 0 };
if ( this->UseDescriptor )
{
this->InitTreeFromDescriptor( output, cursor, index, idx );
}
else
{
// Initialize the tree global start index with the number of
// points added so far. This avoid the storage of a local
// to global node id per tree.
tree->SetGlobalIndexStart( this->LevelBitsIndexCnt[0] );
// Initialize coordinate system for implicit function
double origin[3];
origin[0] = ( i % this->GridSize[0] ) * this->GridScale[0];
origin[1] = ( j % this->GridSize[1] ) * this->GridScale[1];
origin[2] = ( k % this->GridSize[2] ) * this->GridScale[2];
// Subdivide based on quadric implicit function
this->SubdivideFromQuadric( output,
cursor,
0,
index,
idx,
origin,
this->GridScale );
} // else
// Clean up
cursor->UnRegister( this );
} // it
return 1;
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridSource::InitTreeFromDescriptor( vtkHyperTreeGrid* output,
vtkHyperTreeCursor* cursor,
int treeIdx,
int idx[3])
{
// Subdivide using descriptor
if ( ! this->DescriptorBits )
{
this->SubdivideFromStringDescriptor( output, cursor, 0, treeIdx, 0, idx, 0 );
}
else
{
this->SubdivideFromBitsDescriptor( output, cursor, 0, treeIdx, 0, idx, 0 );
}
}
//-----------------------------------------------------------------------------
int vtkHyperTreeGridSource::InitializeFromStringDescriptor()
{
size_t descLen = strlen( this->Descriptor );
// Verify that grid and material specifications are consistent
if ( this->UseMaterialMask && strlen( this->MaterialMask ) != descLen )
{
vtkErrorMacro(<<"Material mask is used but has length "
<< strlen( this->MaterialMask )
<< " != "
<< descLen
<< " which is the length of the grid descriptor.");
return 0;
}
// Calculate total level 0 grid size
unsigned int nTotal = this->GridSize[0] * this->GridSize[1] * this->GridSize[2];
// Parse string descriptor and material mask if used
unsigned int nRefined = 0;
unsigned int nLeaves = 0;
unsigned int nNextLevel = nTotal;
bool rootLevel = true;
std::ostringstream descriptor;
std::ostringstream mask;
for ( size_t i = 0; i < descLen; ++ i )
{
char c = this->Descriptor[i];
char m = this->UseMaterialMask ? this->MaterialMask[i] : 0;
switch ( c )
{
case ' ':
// Space is allowed as separator, verify mask consistenty if needed
if ( this->UseMaterialMask && m != ' ' )
{
vtkErrorMacro(<<"Space separators do not match between "
"descriptor and material mask.");
return 0;
}
break; // case ' '
case '|':
// A level is complete, verify mask consistenty if needed
if ( this->UseMaterialMask && m != '|' )
{
vtkErrorMacro(<<"Level separators do not match between "
"descriptor and material mask.");
return 0;
}
// Store descriptor and material mask for current level
this->LevelDescriptors.push_back( descriptor.str() );
this->LevelMaterialMasks.push_back( mask.str() );
// Check whether cursor is still at rool level
if ( rootLevel )
{
rootLevel = false;
// Verify that total number of root cells is consistent with descriptor
if ( nRefined + nLeaves != nTotal )
{
vtkErrorMacro(<<"String "
<< this->Descriptor
<< " describes "
<< nRefined + nLeaves
<< " root cells != "
<< nTotal);
return 0;
}
} // if ( rootLevel )
else
{
// Verify that level descriptor cardinality matches expected value
if ( descriptor.str().size() != nNextLevel )
{
vtkErrorMacro(<<"String level descriptor "
<< descriptor.str().c_str()
<< " has cardinality "
<< descriptor.str().size()
<< " which is not expected value of "
<< nNextLevel);
return 0;
}
} // else
// Predict next level descriptor cardinality
nNextLevel = nRefined * this->BlockSize;
// Reset per level values
descriptor.str( "" );
mask.str( "" );
nRefined = 0;
nLeaves = 0;
break; // case '|'
case '1':
case 'R':
// Refined cell, verify mask consistenty if needed
if ( this->UseMaterialMask && m == '0' )
{
vtkErrorMacro(<<"A refined branch must contain material.");
return 0;
}
// Refined cell, update branch counter
++ nRefined;
// Append characters to per level descriptor and material mask if used
descriptor << c;
if ( this->UseMaterialMask )
{
mask << m;
}
break; // case 'R'
case '0':
case '.':
// Leaf cell, update leaf counter
++ nLeaves;
// Append characters to per level descriptor and material mask if used
descriptor << c;
if ( this->UseMaterialMask )
{
mask << m;
}
break; // case '.'
default:
vtkErrorMacro(<< "Unrecognized character: "
<< c
<< " at pos " << i << " in descriptor "
<< this->Descriptor);
return 0; // default
} // switch( c )
} // c
// Verify and append last level string
if ( descriptor.str().size() != nNextLevel )
{
vtkErrorMacro(<<"String level descriptor "
<< descriptor.str().c_str()
<< " has cardinality "
<< descriptor.str().size()
<< " which is not expected value of "
<< nNextLevel);
return 0;
}
// Push per-level descriptor and material mask if used
this->LevelDescriptors.push_back( descriptor.str() );
if ( this->UseMaterialMask )
{
this->LevelMaterialMasks.push_back( mask.str() );
}
// Reset maximum depth if fewer levels are described
unsigned int nLevels =
static_cast<unsigned int>( this->LevelDescriptors.size() );
if ( nLevels < this->MaximumLevel )
{
this->MaximumLevel = nLevels;
}
// Create vector of counters as long as tree depth
for ( unsigned int i = 0; i < nLevels; ++ i )
{
this->LevelCounters.push_back( 0 );
}
this->LevelBitsIndex.clear();
this->LevelBitsIndex.push_back( 0 );
for ( unsigned int i = 1; i < nLevels; ++ i )
{
this->LevelBitsIndex.push_back
(
this->LevelBitsIndex[i-1] +
static_cast<vtkIdType>(this->LevelDescriptors[i-1].length())
);
}
this->LevelBitsIndexCnt = this->LevelBitsIndex;
return 1;
}
//----------------------------------------------------------------------------
void vtkHyperTreeGridSource::SubdivideFromStringDescriptor( vtkHyperTreeGrid* output,
vtkHyperTreeCursor* cursor,
unsigned int level,
int treeIdx,
int childIdx,
int idx[3],
int parentPos )
{
// Get handle on point data
vtkPointData* outData = output->GetPointData();
// Calculate pointer into level descriptor string
int pointer = level ? childIdx + parentPos * this->BlockSize : treeIdx;
// Calculate the node global index
vtkIdType id = this->LevelBitsIndexCnt[level];
++ this->LevelBitsIndexCnt[level];
// Set depth array value
outData->GetArray( "Depth" )->InsertTuple1( id, level );
if ( this->GenerateInterfaceFields )
{
// Set interface arrays values
double v = 1. / ( 1 << level );
outData->GetArray( "Normals" )->InsertTuple3( id, v, v, v );
outData->GetArray( "Intercepts" )->InsertTuple3( id, v, 0., 3. );
}
// Initialize global index of tree
cursor->GetTree()->SetGlobalIndexFromLocal( cursor->GetVertexId(), id );
// Subdivide further or stop recursion with terminal leaf
if ( level + 1 < this->MaximumLevel
&& this->LevelDescriptors.at( level ).at( pointer ) == 'R' )
{
// Subdivide hyper tree grid leaf
output->SubdivideLeaf( cursor, treeIdx );
// Figure out index bounds depending on dimension and orientation
int xDim = this->BranchFactor;
int yDim = this->BranchFactor;
int zDim = this->BranchFactor;
if ( this->Dimension == 1 )
{
switch ( this->Orientation )
{
case 0:
yDim = 1;
zDim = 1;
break;
case 1:
xDim = 1;
zDim = 1;
break;
case 2:
xDim = 1;
yDim = 1;
break;
default:
vtkErrorMacro(<< "Incorrect orientation in 1D: "
<< this->Orientation);
return;
} // switch ( this->Orientation )
} // if ( this->Dimension == 1 )
else if ( this->Dimension == 2 )
{
switch ( this->Orientation )
{
case 0:
xDim = 1;
break;
case 1:
yDim = 1;
break;
case 2:
zDim = 1;
break;
default:
vtkErrorMacro(<< "Incorrect orientation in 2D: "
<< this->Orientation);
return;
} // switch ( this->Orientation )
} // else if ( this->Dimension == 2 )
// Now traverse to children
int newChildIdx = 0;
int newIdx[3];
for ( int z = 0; z < zDim; ++ z )
{
newIdx[2] = idx[2] * zDim + z;
for ( int y = 0; y < yDim; ++ y )
{
newIdx[1] = idx[1] * yDim + y;
for ( int x = 0; x < xDim; ++ x )
{
newIdx[0] = idx[0] * xDim + x;
// Set cursor to child
cursor->ToChild( newChildIdx );
// Recurse
this->SubdivideFromStringDescriptor( output,
cursor,
level + 1,
treeIdx,
newChildIdx,
newIdx,
this->LevelCounters.at( level ) );
// Reset cursor to parent
cursor->ToParent();
// Increment child index
++ newChildIdx;
} // x
} // y
} // z
// Increment current level counter
++ this->LevelCounters.at( level );
if( this->UseMaterialMask )
{
output->GetMaterialMask()->InsertTuple1( id, 0 );
}
} // if ( subdivide )
else if ( this->UseMaterialMask )
{
// Blank leaf if needed
bool masked = this->LevelMaterialMasks.at( level ).at( pointer ) == '0' ? 1 : 0;
output->GetMaterialMask()->InsertTuple1( id, masked );
} // else if
}
//----------------------------------------------------------------------------
int vtkHyperTreeGridSource::InitializeFromBitsDescriptor()
{
// Verify that grid and material specifications are consistent
if ( this->UseMaterialMask && ! this->LevelZeroMaterialIndex
&& this->MaterialMaskBits->GetSize() != this->DescriptorBits->GetSize() )
{
vtkErrorMacro(<<"Material mask is used but has length "
<< this->MaterialMaskBits->GetSize()
<< " != "
<< this->DescriptorBits->GetSize()
<< " which is the length of the grid descriptor.");
return 0;