File: vtkAMRResampleFilter.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (1350 lines) | stat: -rw-r--r-- 42,952 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
/*=========================================================================

 Program:   Visualization Toolkit
 Module:    vtkAMRToUniformGrid.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 "vtkAMRResampleFilter.h"
#include "vtkAMRInformation.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkOverlappingAMR.h"
#include "vtkMultiProcessController.h"
#include "vtkUniformGrid.h"
#include "vtkIndent.h"
#include "vtkAMRUtilities.h"
#include "vtkBoundingBox.h"
#include "vtkMath.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkFieldData.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkCell.h"
//#include "vtkXMLImageDataWriter.h"
#include "vtkExtentRCBPartitioner.h"
#include "vtkUniformGridPartitioner.h"
#include "vtkDataArray.h"

#include "vtkTimerLog.h"

#include <cassert>
#include <cmath>
#include <sstream>
#include <cmath>
#include <algorithm>


vtkStandardNewMacro( vtkAMRResampleFilter );

//-----------------------------------------------------------------------------
vtkAMRResampleFilter::vtkAMRResampleFilter()
{
  this->TransferToNodes      = 1;
  this->DemandDrivenMode     = 0;
  this->NumberOfPartitions   = 1;
  this->LevelOfResolution    = 0;
  this->AMRMetaData          = NULL;
  this->NumberOfSamples[0]   = this->NumberOfSamples[1] = this->NumberOfSamples[2] = 10;
  this->Controller           = vtkMultiProcessController::GetGlobalController();
  this->ROI                  = vtkMultiBlockDataSet::New();

  for( int i=0; i < 3; ++i )
  {
    this->Min[i] = 0.;
    this->Max[i] = 1.;
  }
  this->SetNumberOfInputPorts( 1 );
  this->SetNumberOfOutputPorts( 1 );
  this->UseBiasVector = false;
  this->BiasVector[0] = this->BiasVector[1] = this->BiasVector[2] = 0.0;
}

//-----------------------------------------------------------------------------
vtkAMRResampleFilter::~vtkAMRResampleFilter()
{
  this->BlocksToLoad.clear();

  if( this->ROI != NULL )
  {
    this->ROI->Delete();
  }
  this->ROI = NULL;

//  if( this->AMRMetaData != NULL )
//    {
//    this->AMRMetaData->Delete();
//    }
//  this->AMRMetaData = NULL;
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::PrintSelf( std::ostream &oss, vtkIndent indent )
{
  this->Superclass::PrintSelf( oss, indent );
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::FillInputPortInformation(
    int vtkNotUsed(port), vtkInformation *info )
{
  assert( "pre: information object is NULL" && (info != NULL) );
  info->Set(
   vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkOverlappingAMR" );
  return 1;
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::FillOutputPortInformation(
    int vtkNotUsed(port), vtkInformation *info )
{
  assert( "pre: information object is NULL" && (info != NULL) );
  info->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkMultiBlockDataSet");
  return 1;
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::RequestUpdateExtent(
    vtkInformation*, vtkInformationVector **inputVector,
    vtkInformationVector* vtkNotUsed(outputVector) )
{
  assert( "pre: inputVector is NULL" && (inputVector != NULL) );
  vtkInformation *info = inputVector[0]->GetInformationObject(0);
  assert( "pre: info is NULL" && (info != NULL) );

  if( this->DemandDrivenMode == 1 )
  {
    // Tell reader to load all requested blocks.
    info->Set( vtkCompositeDataPipeline::LOAD_REQUESTED_BLOCKS(), 1 );

    // Tell reader which blocks this process requires
    info->Set(
        vtkCompositeDataPipeline::UPDATE_COMPOSITE_INDICES(),
        &this->BlocksToLoad[0], static_cast<int>(this->BlocksToLoad.size()));
  }
  return 1;
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::RequestInformation(
    vtkInformation* vtkNotUsed(rqst),
    vtkInformationVector** inputVector,
    vtkInformationVector* vtkNotUsed(outputVector) )
{

  assert( "pre: inputVector is NULL" && (inputVector != NULL) );

  vtkInformation *input = inputVector[0]->GetInformationObject( 0 );
  assert( "pre: input is NULL" && (input != NULL)  );

  if( this->DemandDrivenMode == 1 &&
      input->Has(vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA() ) )
  {
//    this->AMRMetaData = vtkOverlappingAMR::New();
    this->AMRMetaData =
    vtkOverlappingAMR::SafeDownCast(
      input->Get( vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA() ) );


    // Get Region
    double h[3];
    this->ComputeAndAdjustRegionParameters( this->AMRMetaData, h );
    this->GetRegion( h );

    // Compute which blocks to load
    this->ComputeAMRBlocksToLoad( this->AMRMetaData );
  }

// Don't we need to call this->Modified() here?
// this->Modified();
 return 1;
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::RequestData(
    vtkInformation* vtkNotUsed(rqst), vtkInformationVector** inputVector,
    vtkInformationVector* outputVector )
{
  cerr << "Running Resampler\n";

  // STEP 0: Get input object
  vtkInformation *input = inputVector[0]->GetInformationObject( 0 );
  assert( "pre: Null information object!" && (input != NULL) );
  vtkOverlappingAMR *amrds=
     vtkOverlappingAMR::SafeDownCast(
      input->Get(vtkDataObject::DATA_OBJECT()));
  assert( "pre: input AMR dataset is NULL" && (amrds != NULL) );

  // STEP 1: Get output object
   vtkInformation *output = outputVector->GetInformationObject( 0 );
   assert( "pre: Null output information object!" && (output != NULL) );

   vtkMultiBlockDataSet *mbds =
      vtkMultiBlockDataSet::SafeDownCast(
          output->Get(vtkDataObject::DATA_OBJECT() ) );
   assert( "pre: output grid is NULL" && (mbds != NULL) );

  // STEP 2: Get Metadata
  if( this->DemandDrivenMode == 1 )
  {
    assert( "pre: Metadata must have been populated in RqstInfo" &&
            (this->AMRMetaData != NULL) );
    this->ExtractRegion( amrds, mbds, this->AMRMetaData );
  }
  else
  {
    // GetRegion
    double h[3];
    this->ComputeAndAdjustRegionParameters(amrds, h );
    this->GetRegion( h );
    this->ExtractRegion( amrds, mbds, amrds);
  }

  return 1;
}


//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::FoundDonor(
    double q[3],vtkUniformGrid *&donorGrid,int &cellIdx)
{
  assert( "pre: donor grid is NULL" && (donorGrid != NULL) );
  double gbounds[6];
  // Lets do a trival spatial check
  this->NumberOfBlocksTested++;
  donorGrid->GetBounds(gbounds);
  if ((q[0] < gbounds[0]) || (q[0] > gbounds[1]) ||
      (q[1] < gbounds[2]) || (q[1] > gbounds[3]) ||
      (q[2] < gbounds[4]) || (q[2] > gbounds[5]))
  {
    return false;
  }
  int ijk[3];
  double pcoords[3];
  int status = donorGrid->ComputeStructuredCoordinates( q, ijk, pcoords );
  if( status == 1 )
  {
    cellIdx=vtkStructuredData::ComputeCellId(donorGrid->GetDimensions(),ijk);
    return true;
  }
  return false;
}


//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::InitializeFields(
      vtkFieldData *f, vtkIdType size, vtkCellData *src )
{
  assert( "pre: field data is NULL!" && (f != NULL) );
  assert( "pre: source cell data is NULL" && (src != NULL) );

  for( int arrayIdx=0; arrayIdx < src->GetNumberOfArrays(); ++arrayIdx )
  {
    int dataType        = src->GetArray( arrayIdx )->GetDataType();
    vtkDataArray *array = vtkDataArray::CreateDataArray( dataType );
    assert( "pre: failed to create array!" && (array != NULL) );

    array->SetName( src->GetArray(arrayIdx)->GetName() );
    array->SetNumberOfComponents(
             src->GetArray(arrayIdx)->GetNumberOfComponents() );
    array->SetNumberOfTuples( size );
    assert( "post: array size mismatch" &&
            (array->GetNumberOfTuples() == size) );

    f->AddArray( array );
    array->Delete();

//      int myIndex = -1;
//      vtkDataArray *arrayPtr = f->GetArray(
//          src->GetArray(arrayIdx)->GetName(), myIndex );
//      assert( "post: array index mismatch" && (myIndex == arrayIdx) );
//      assert( "post: array size mismatch" &&
//              (arrayPtr->GetNumberOfTuples()==size) );

    assert( "post: array size mismatch" &&
            (f->GetArray( arrayIdx)->GetNumberOfTuples() == size) );
  } // END for all arrays

}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::CopyData(
    vtkFieldData *target, vtkIdType targetIdx,
    vtkCellData *src, vtkIdType srcIdx )
{
  assert( "pre: target field data is NULL" && (target != NULL) );
  assert( "pre: source field data is NULL" && (src != NULL) );
  assert( "pre: number of arrays does not match" &&
           (target->GetNumberOfArrays() == src->GetNumberOfArrays() ) );

  int arrayIdx = 0;
  for( ; arrayIdx < src->GetNumberOfArrays(); ++arrayIdx )
  {
    vtkDataArray *targetArray = target->GetArray( arrayIdx );
    vtkDataArray *srcArray    = src->GetArray( arrayIdx );
    assert( "pre: target array is NULL!" && (targetArray != NULL) );
    assert( "pre: source array is NULL!" && (srcArray != NULL) );
    assert( "pre: targer/source array number of components mismatch!" &&
            (targetArray->GetNumberOfComponents()==
             srcArray->GetNumberOfComponents() ) );
    assert( "pre: target/source array names mismatch!" &&
            (strcmp(targetArray->GetName(),srcArray->GetName()) == 0) );
    assert( "pre: source index is out-of-bounds" &&
            (srcIdx >=0) &&
            (srcIdx < srcArray->GetNumberOfTuples() ) );
    assert( "pre: target index is out-of-bounds" &&
            (targetIdx >= 0) &&
            (targetIdx < targetArray->GetNumberOfTuples() ) );

    int c=0;
    for( ; c < srcArray->GetNumberOfComponents(); ++c )
    {
      double f = srcArray->GetComponent( srcIdx, c );
      targetArray->SetComponent( targetIdx, c, f );
    } // END for all componenents

  } // END for all arrays
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeCellCentroid(
    vtkUniformGrid *g, const vtkIdType cellIdx, double c[3] )
{
  assert( "pre: uniform grid is NULL" && (g != NULL) );
  assert( "pre: centroid is NULL" && (c != NULL) );
  assert( "pre: cell index out-of-bounds" &&
           (cellIdx >= 0) && (cellIdx < g->GetNumberOfCells()) );


  vtkCell *myCell = g->GetCell( cellIdx );
  assert( "post: cell is NULL!" && (myCell != NULL) );

  double pc[3]; // the parametric center
  double *weights = new double[ myCell->GetNumberOfPoints() ];
  assert( "post: weights vector is NULL" && (weights != NULL) );

  int subId = myCell->GetParametricCenter( pc );
  myCell->EvaluateLocation( subId, pc, c, weights );
  delete [] weights;
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::TransferToCellCenters(
        vtkUniformGrid *g, vtkOverlappingAMR *amrds )
{
  assert( "pre: uniform grid is NULL" && (g != NULL) );
  assert( "pre: AMR data-strucutre is NULL" && (amrds != NULL) );

  // STEP 0: Get the first block so that we know the arrays
//  vtkUniformGrid *refGrid = amrds->GetDataSet(0,0);
//  assert( "pre: Block(0,0) is NULL!" && (refGrid != NULL) );
  vtkUniformGrid *refGrid = this->GetReferenceGrid( amrds );

  // STEP 1: Get the cell-data of the reference grid
  vtkCellData *CD = refGrid->GetCellData();
  assert( "pre: Donor CellData is NULL!" && (CD != NULL)  );

  // STEP 2: Get the cell data of the resampled grid
  vtkCellData *fieldData = g->GetCellData();
  assert( "pre: Target PointData is NULL!" && (fieldData != NULL) );

  // STEP 3: Initialize the fields on the resampled grid
  this->InitializeFields( fieldData, g->GetNumberOfCells(), CD );

  if(fieldData->GetNumberOfArrays() == 0)
  {
    return;
  }

  // TODO: this is a very naive implementation and should be optimized. However,
  // mostly this filter is used to transfer the solution to the grid nodes and
  // not on the cell nodes.
  vtkIdType cellIdx = 0;
  for( ; cellIdx < g->GetNumberOfCells(); ++cellIdx )
  {
    double qPoint[3];
    this->ComputeCellCentroid( g, cellIdx, qPoint );

    unsigned int level=0;
    for( ; level < amrds->GetNumberOfDataSets( level ); ++level )
    {
      unsigned int dataIdx = 0;
      for( ; dataIdx < amrds->GetNumberOfDataSets( level ); ++dataIdx )
      {
          int donorCellIdx = -1;
          vtkUniformGrid *donorGrid = amrds->GetDataSet(level,dataIdx);
          if( (donorGrid!=NULL) &&
              this->FoundDonor(qPoint,donorGrid,donorCellIdx) )
          {
            assert( "pre: donorCellIdx is invalid" &&
                    (donorCellIdx >= 0) &&
                    (donorCellIdx < donorGrid->GetNumberOfCells()) );
            CD = donorGrid->GetCellData();
            this->CopyData( fieldData, cellIdx, CD, donorCellIdx );
          } // END if
      } // END for all datasets
    } // END for all levels
  } // END for all cells
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::SearchForDonorGridAtLevel(
    double q[3], vtkOverlappingAMR *amrds,
    unsigned int level, unsigned int& donorGridId,
    int &donorCellIdx )
{
  assert( "pre: AMR dataset is NULL" && (amrds != NULL) );
  this->NumberOfBlocksTestedForLevel = 0;
  std::ostringstream oss;
  oss << "SearchLevel-" << level;

  vtkTimerLog::MarkStartEvent( oss.str().c_str() );

  for(donorGridId = 0; donorGridId < amrds->GetNumberOfDataSets(level); ++donorGridId )
  {
    donorCellIdx = -1;
    this->NumberOfBlocksTestedForLevel++;
    if(amrds->GetAMRInfo()->FindCell(q, level,donorGridId,donorCellIdx))
    {
     assert( "pre: donorCellIdx is invalid" &&
             (donorCellIdx >= 0));//  &&
             // (donorCellIdx < donorGrid->GetNumberOfCells()) );
     vtkTimerLog::MarkEndEvent( oss.str().c_str() );
     return true;
    } // END if

  } // END for all data at level


  // No suitable grid is found at the requested level, set donorGrid to NULL
  // to indicate that to the caller.
  vtkTimerLog::MarkEndEvent( oss.str().c_str() );
  return false;
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::ProbeGridPointInAMR(
  double q[3], unsigned int &donorLevel, unsigned int& donorGridId,
    vtkOverlappingAMR *amrds, unsigned int maxLevel, bool hadDonorGrid)
{
  assert( "pre: AMR dataset is NULL" && amrds != NULL );

  vtkUniformGrid *currentGrid = NULL;
  int currentCellIdx          = -1;
  int donorCellIdx            = -1;
  unsigned int currentLevel = 0;
  unsigned int currentGridId = 0;
  vtkUniformGrid* donorGrid = hadDonorGrid? amrds->GetDataSet(donorLevel,donorGridId): NULL;

  // STEP 0: Check the previously cached donor-grid
  if( hadDonorGrid)
  {
    this->NumberOfBlocksTested++;
    bool res(true);
    if(!amrds->GetAMRInfo()->FindCell( q, donorLevel, donorGridId, donorCellIdx ) )
    {
      // Lets see if the point is contained by a grid at the same donar level
      res = this->SearchForDonorGridAtLevel(q,amrds,donorLevel,donorGridId,
                                                 donorCellIdx);
      donorGrid = res? amrds->GetDataSet(donorLevel,donorGridId) : NULL;
      this->NumberOfBlocksTested += this->NumberOfBlocksTestedForLevel;
    }

    // If donorGrid is still not NULL then we found the grid and potential starting
    // level
    if (res)
    {
      assert( "pre: donorCellIdx is invalid" &&
              (donorCellIdx >= 0) && (donorCellIdx < donorGrid->GetNumberOfCells()) );

      this->NumberOfTimesFoundOnDonorLevel++;

      // Initialize values for step 1 s.t. that the search will start from the
      // current donorLevel
      currentGrid    = donorGrid;
      currentGridId = donorGridId;
      currentCellIdx = donorCellIdx;
      currentLevel = donorLevel;
      assert(!donorGrid || amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
    }
    else if (donorLevel == 0)
    {
      //if we are here then the point is not contained in any of the level 0
      // blocks!
      this->NumberOfFailedPoints++;
      donorGrid    = NULL;
      donorLevel = 0;
      return -1;
    }
    else
    {
      // If we are here then we know the point is not on the donor level
      // and therefore not contained in any of the more refined levels -
      // Base on the assumption of overlapping AMR
      assert("pre:Donor Level is 0" && donorLevel != 0);
      // Initialize values for step 1 s.t. the search will start from level 0.
      donorGrid  = NULL;
      maxLevel = donorLevel;
      donorLevel = 0;
      currentLevel = 0;
    }
  }

  // If we didn't have an initial donor grid or if we still have one
  // we need to test higher res grids
  int startLevel, endLevel;
  int incLevel;
  if (!((donorGrid == NULL) && hadDonorGrid))
  {
    startLevel = donorGrid==NULL? currentLevel : currentLevel+1;
    endLevel = maxLevel;
    incLevel = 1;
  }
  else
  {
    startLevel = maxLevel-1;
    endLevel = -1;
    incLevel = -1;
  }
  // STEP 1: Search in the AMR hierarchy for the donor-grid
  for( int level=startLevel; level != endLevel; level += incLevel )
  {
    if (incLevel == 1)
    {
      this->NumberOfTimesLevelUp++;
    }
    else
    {
      this->NumberOfTimesLevelDown++;
    }
    bool res = this->SearchForDonorGridAtLevel(q,amrds,level,donorGridId,donorCellIdx);
    donorGrid = res? amrds->GetDataSet(level,donorGridId) : NULL;

    this->NumberOfBlocksTested += this->NumberOfBlocksTestedForLevel;
    if( res )
    {
      donorLevel = level;
      // if we are going from fine to coarse then we can stop the search
      if (incLevel == -1)
      {
        assert(amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
        return donorCellIdx;
      }

      // Lets see if this is the highest resolution grid that contains the
      // point
      if (donorGrid->IsCellVisible(donorCellIdx))
      {
        //return donorCellIdx;
      }
      // we found a grid that contains the point at level l, let's store it
      // here temporatily in case there is a grid at a higher resolution that
      // we need to use.
      currentGrid    = donorGrid;
      currentCellIdx = donorCellIdx;
      currentLevel = level;
      currentGridId = donorGridId;
    }
    else if( currentGrid != NULL )
    {
      // we did not find the point at a higher res, but, we did find at a lower
      // resolution, so we will use the solution we found previously
      // THIS SHOULD NOW NOT HAPPEN!!
      //vtkErrorMacro("Could not find point in an unblanked cell.");
      this->NumberOfBlocksVisSkipped +=  this->NumberOfBlocksTestedForLevel;
      donorGrid    = currentGrid;
      donorCellIdx = currentCellIdx;
      donorLevel = currentLevel;
      donorGridId = currentGridId;
      assert(!donorGrid || amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
      break;
    }
    else
    {
      // we are not able to find a grid/cell that contains the query point, in
      // this case we will just return.
      this->NumberOfFailedPoints++;
      donorCellIdx = -1;
      donorGrid    = NULL;
      donorLevel = 0;
      break;
    }
  } // END for all levels
  assert(!donorGrid || amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
  return( donorCellIdx );
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::SearchGridAncestors(double q[3],
                                               vtkOverlappingAMR *amrds,
                                               unsigned int &level,
                                               unsigned int &gridId,
                                               int &cellId )
{
  assert( "pre: AMR dataset is NULL" && (amrds != NULL) );
  unsigned int *parents, plevel;
  for (; level > 0; --level)
  {
    ++this->NumberOfTimesLevelUp;
    // Get the parents of the grid

    unsigned int numParents;
    parents = amrds->GetParents(level, gridId,numParents);
    plevel = level - 1;
    // There should be at least 1 parent
    assert( "Found non-level 0 grid with no parents" && (parents != NULL) && (numParents > 0) );
    if (numParents > 1)
    {
      vtkDebugMacro( "Number of parents: " << numParents << " - Only processing 1 route");
    }
    gridId = parents[0];
    if (amrds->GetAMRInfo()->FindCell( q, plevel,gridId, cellId ))
    {
      level = plevel;
      return true;
    }
  }
  // If we are here then we could not find an ancestor
  cellId = -1;
  return false;
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::SearchGridDecendants(double q[3],
                                                vtkOverlappingAMR *amrds,
                                                unsigned int maxLevel,
                                                unsigned int &level,
                                                unsigned int &gridId,
                                                int &cellId)
{
  assert( "pre: AMR dataset is NULL" && (amrds != NULL) );
  unsigned int *children, clevel, n, i;
  for (; level < maxLevel-1; ++level)
  {
    // Get the children of the grid
    children = amrds->GetChildren(level, gridId,n);
    clevel = level + 1;
    // If there are no children then we found the grid!
    if (children == NULL)
    {
      return;
    }
//    assert(n == children[0]);
    for (i = 0; i < n; ++i)
    {
      if (amrds->GetAMRInfo()->FindCell( q, clevel,children[i], cellId ))
      {
        // We found a decendant so stop searching the
        // children and can instead search that grid's
        // children
        gridId = children[i];
        ++this->NumberOfTimesLevelDown;
        break;
      }
    }
    if (i >= n)
    {
      // We tested some children that we didn't need to if
      // we had visibility info
      this->NumberOfBlocksVisSkipped += n;
      // If we are here then no child contains the point
      // so don't search any further
      return;
    }
  }
}



//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::
ProbeGridPointInAMRGraph(double q[3],
                         unsigned int &donorLevel,  unsigned int &donorGridId,
                         vtkOverlappingAMR *amrds, unsigned int maxLevel, bool useCached)
{
  assert( "pre: AMR dataset is NULL" && amrds != NULL );

  int donorCellIdx = -1;

  vtkUniformGrid* donorGrid = NULL;
  // STEP 0: Check the previously cached donor-grid
  if( useCached)
  {
    if(!amrds->GetAMRInfo()->FindCell( q, donorLevel,donorGridId, donorCellIdx ) )
    {
      // Lets find the grid's ancestor that contains the point
      bool res = this->SearchGridAncestors(q,amrds,donorLevel,donorGridId,donorCellIdx);
      donorGrid  = res? amrds->GetDataSet(donorLevel,donorGridId) : NULL;
    }
    else
    {
      donorGrid  = amrds->GetDataSet(donorLevel,donorGridId);
      ++this->NumberOfTimesFoundOnDonorLevel;
    }
    // if the point is not contained in an ancestor then lets just assume its on level
    // 0 which is the default
  }

  // If there is no initial donor grid then search level 0
  if (donorGrid == NULL)
  {
    bool res = SearchForDonorGridAtLevel(q,amrds,0,donorGridId,donorCellIdx);
    // If we still can't find a grid then the point is not contained in the
    // AMR Data
    if (!res)
    {
      this->NumberOfFailedPoints++;
      donorLevel = 0;
      return -1;
    }
  }

  // Now search the decendants of the donor grid
  this->SearchGridDecendants(q,amrds,maxLevel,donorLevel,donorGridId,donorCellIdx);
  return( donorCellIdx );
}


//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::TransferToGridNodes(
    vtkUniformGrid *g, vtkOverlappingAMR *amrds )
{
  this->NumberOfBlocksTested = 0;
  this->NumberOfBlocksVisSkipped = 0;
  this->NumberOfTimesFoundOnDonorLevel = 0;
  this->NumberOfTimesLevelUp = 0;
  this->NumberOfTimesLevelDown = 0;
  this->NumberOfFailedPoints = 0;
  this->AverageLevel = 0.0;
  assert( "pre: uniform grid is NULL" && (g != NULL) );
  assert( "pre: AMR data-structure is NULL" && (amrds != NULL) );

  // STEP 0: Initialize the fields on the grid
  vtkUniformGrid *refGrid = this->GetReferenceGrid( amrds );

  vtkCellData *CD = refGrid->GetCellData();
  assert( "pre: Donor CellData is NULL!" && (CD != NULL)  );

  vtkPointData *PD = g->GetPointData();
  assert( "pre: Target PointData is NULL!" && (PD != NULL) );

  // STEP 0: Initialize the fields on the grid
  this->InitializeFields( PD, g->GetNumberOfPoints(), CD );

  // STEP 1: If no arrays are selected, there is no need to interpolate
  // anything on the grid, just return
  if(PD->GetNumberOfArrays() == 0)
  {
    return;
  }

  // STEP 2: Fix the maximum level at which the search algorithm will operate
  unsigned int maxLevelToLoad = 0;
  if( this->LevelOfResolution < static_cast<int>(amrds->GetNumberOfLevels()) &&
    this->DemandDrivenMode == 1)
  {
    maxLevelToLoad = this->LevelOfResolution+1;
  }
  else
  {
    maxLevelToLoad = amrds->GetNumberOfLevels();
  }

  // STEP 3: Loop through all the points and find the donors.
  int numPoints           = 0;
  unsigned int donorLevel = 0;
  unsigned int donorGridId = 0;
  double qPoint[3];
  vtkIdType pIdx;
  int donorCellIdx;
  bool useCached(false);
  // Do we have parent/child meta information (yes, we always do)
  if (this->AMRMetaData)
  {
    for(pIdx = 0; pIdx < g->GetNumberOfPoints(); ++pIdx )
    {
      g->GetPoint( pIdx, qPoint );
      donorCellIdx =
        this->ProbeGridPointInAMRGraph(qPoint,
                                       donorLevel, donorGridId,
                                       amrds,maxLevelToLoad, useCached);
      if( donorCellIdx != -1 )
      {
        useCached = true;
        vtkUniformGrid *amrGrid = amrds->GetDataSet(donorLevel,donorGridId);
        this->AverageLevel += donorLevel;
        CD = amrGrid->GetCellData();
        this->CopyData( PD, pIdx, CD, donorCellIdx );
      }
      else
      {
        useCached = false;
        // Point is outside the domain, blank it
        ++ numPoints;
        g->BlankPoint( pIdx );
      }
    } // END for all grid nodes
  }
  else
  {
    for(pIdx = 0; pIdx < g->GetNumberOfPoints(); ++pIdx )
    {
      g->GetPoint( pIdx, qPoint );

      donorCellIdx =
        this->ProbeGridPointInAMR(qPoint,donorLevel,donorGridId,
                                  amrds,maxLevelToLoad, useCached);

      if( donorCellIdx != -1 )
      {
        useCached = true;
        this->AverageLevel += donorLevel;
        vtkUniformGrid*  donorGrid = amrds->GetDataSet(donorLevel,donorGridId);
        assert(donorGrid != NULL);
        CD = donorGrid->GetCellData();
        this->CopyData( PD, pIdx, CD, donorCellIdx );
      }
      else
      {
        useCached = false;
        // Point is outside the domain, blank it
        ++ numPoints;
        g->BlankPoint( pIdx );
      }
    } // END for all grid nodes
  }
  std::cerr << "********* Resample Stats *************\n";
  double c = this->NumberOfSamples[0] * this->NumberOfSamples[1] * this->NumberOfSamples[2];
  double b = g->GetNumberOfPoints();
  std::cerr << "Number of Requested Points: " << c << " Number of Actual Points: " << b << "\n";
  std::cerr << " Percentage of Requested Points in Grid: " << 100.0 * b / c << "\n";
  std::cerr << "Total Number of Blocks Tested: " << this->NumberOfBlocksTested << "\n";
  std::cerr << " Number of Blocks that could be skipped by Visibility: " << this->NumberOfBlocksVisSkipped << "\n";
  double a = 100.0 * (double)(this->NumberOfBlocksVisSkipped) / (double) this->NumberOfBlocksTested;
  std::cerr << "Percentage of Blocks skipped via Visibility: " << a << "\n";
  a = (double) this->NumberOfBlocksTested / b;
  std::cerr << "Ave Number of Blocks Tested per Point: " << a << "\n";
  a = 100.0 * (double) this->NumberOfTimesFoundOnDonorLevel / b;
  std::cerr << "Percentage of Times we found point on Previous Level: " << a << "\n";
  a = 100.0 * (double) this->NumberOfTimesLevelUp / b;
  std::cerr << "Percentage of Times went to finer level: " << a << "\n";
  a = 100.0 * (double) this->NumberOfTimesLevelDown / b;
  std::cerr << "Percentage of Times went to coarser level: " << a << "\n";
  a = this->AverageLevel / b;
  std::cerr << "Average Level: " << a << "\n";
  std::cerr << "Number Of Failed Points: " << this->NumberOfFailedPoints << "\n";

}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::TransferSolution(
    vtkUniformGrid *g, vtkOverlappingAMR *amrds)
{
  assert( "pre: uniform grid is NULL" && (g != NULL) );
  assert( "pre: AMR data-strucutre is NULL" && (amrds != NULL) );

  if( this->TransferToNodes == 1 )
  {
    this->TransferToGridNodes( g, amrds );
  }
  else
  {
    this->TransferToCellCenters( g, amrds );
  }
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ExtractRegion(
    vtkOverlappingAMR *amrds, vtkMultiBlockDataSet *mbds,
    vtkOverlappingAMR * vtkNotUsed(metadata) )
{

  assert( "pre: input AMR data-structure is NULL" && (amrds != NULL) );
  assert( "pre: resampled grid should not be NULL" && (mbds != NULL) );

//  std::cout << "NumBlocks: " << this->ROI->GetNumberOfBlocks() << std::endl;
//  std::cout << "NumProcs: "  << this->Controller->GetNumberOfProcesses() << std::endl;
//  std::cout.flush();

  assert( "pre: NumProcs must be less than or equal to NumBlocks" &&
    (!this->Controller ||
     (static_cast<int>(this->ROI->GetNumberOfBlocks()) <= this->Controller->GetNumberOfProcesses())));

  mbds->SetNumberOfBlocks( this->ROI->GetNumberOfBlocks( ) );
  for( unsigned int block=0; block < this->ROI->GetNumberOfBlocks(); ++block )
  {
    if( this->IsRegionMine( block ) )
    {
      vtkUniformGrid *grid = vtkUniformGrid::New();
      grid->ShallowCopy( this->ROI->GetBlock( block ) );
      this->TransferSolution( grid, amrds );
      mbds->SetBlock( block, grid );
      grid->Delete();
    }
    else
    {
      mbds->SetBlock( block, NULL );
    }
  } // END for all blocks

}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeAMRBlocksToLoad(
    vtkOverlappingAMR *metadata )
{
  assert( "pre: metadata is NULL" && (metadata != NULL) );

  this->BlocksToLoad.clear();

  unsigned int maxLevelToLoad = 0;
  if( this->LevelOfResolution < static_cast<int>(metadata->GetNumberOfLevels()))
  {
    maxLevelToLoad = this->LevelOfResolution+1;
  }
  else
  {
    maxLevelToLoad = metadata->GetNumberOfLevels();
  }

  unsigned int level=0;
  for( ;level < maxLevelToLoad; ++level )
  {
    unsigned int dataIdx = 0;
    for( ; dataIdx < metadata->GetNumberOfDataSets( level ); ++dataIdx )
    {
      double grd[6];
      metadata->GetBounds(level,dataIdx,grd);
      if( this->IsBlockWithinBounds( grd ) )
      {
        this->BlocksToLoad.push_back(
        metadata->GetCompositeIndex(level,dataIdx) );
      } // END check if the block is within the bounds of the ROI
    } // END for all data
  } // END for all levels

   std::sort( this->BlocksToLoad.begin(), this->BlocksToLoad.end() );
   cerr << "Number Levels Loaded = " << maxLevelToLoad << " Number of Blocks = " << BlocksToLoad.size() << "\n";
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::GetDomainParameters(
    vtkOverlappingAMR *amr,
    double domainMin[3], double domainMax[3], double h[3],
    int dims[3], double &rf )
{
  assert( "pre: AMR dataset is NULL!" && (amr != NULL) );

  rf = amr->GetRefinementRatio(1);
  amr->GetAMRInfo()->GetAMRBox(0,0).GetNumberOfNodes(dims);
  amr->GetMin(domainMin);
  amr->GetMax(domainMax);

  amr->GetSpacing(0,h);
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::SnapBounds(
    const double* vtkNotUsed(h0[3]),
    const double domainMin[3],
    const double domainMax[3],
    const int* vtkNotUsed(dims[3]), bool outside[6] )
{
  int i, j;
  for(i=0, j=0; i < 3; ++i )
  {
    // Snap the parts of the bounds that lie outside of the AMR data
    if (this->Min[i] < domainMin[i])
    {
      outside[j++] = true;
      this->GridMin[i] = domainMin[i];
    }
    else
    {
      outside[j++] = false;
      this->GridMin[i] = this->Min[i];
    }

    if (this->Max[i] > domainMax[i])
    {
      outside[j++] = true;
      this->GridMax[i] = domainMax[i];
    }
    else
    {
      outside[j++] = false;
      this->GridMax[i] = this->Max[i];
    }
  }
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeLevelOfResolution(
    const int N[3], const double h0[3], const double L[3], const double rf )
{
  this->LevelOfResolution = 0;
  for( int i=0; i < 3; ++i )
  {
    double c1        = ( ( N[i]*h0[i] )/L[i] );
    int currentLevel = vtkMath::Floor( 0.5+(log(c1)/log(rf)) );
    if( currentLevel > this->LevelOfResolution )
    {
      this->LevelOfResolution = currentLevel;
    }
  } // END for all i
  std::cerr << "Requested Max Level = " << this->LevelOfResolution << "\n";
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::RegionIntersectsWithAMR(
    double domainMin[3], double domainMax[3],
    double regionMin[3], double regionMax[3])
{
  vtkBoundingBox domain;
  domain.SetMinPoint( domainMin );
  domain.SetMaxPoint( domainMax );

  vtkBoundingBox region;
  region.SetMinPoint( regionMin );
  region.SetMaxPoint( regionMax );

  if( domain.Intersects(region) )
  {
    return true;
  }

  return false;
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::AdjustNumberOfSamplesInRegion(
    const double Rh[3],
    const bool outside[6], int N[3] )
{
  int startIndex;
  int endIndex;
  double dx = 0.0;
  for( int i=0; i < 3; ++i )
  {
    N[i] = this->NumberOfSamples[i];

    // Get ijk of the snapped bounding wrt the requested virtual grid.
    if( outside[i*2] || outside[i*2+1] )
    {
      dx = this->GridMin[i]-this->Min[i];
      if (dx > 0.0)
      {
        startIndex = static_cast<int>( dx/Rh[i]+1 );
      }
      else
      {
        startIndex = 0;
      }

      dx  = this->GridMax[i]-this->Min[i];
      endIndex = static_cast<int>( dx/Rh[i]+1 );

      if (endIndex > N[i])
      {
        endIndex = N[i];
      }
      int newN = endIndex - startIndex +1;
      if (newN <= N[i])
      {
        N[i] = newN;
      }
      else
      {
        assert("ERROR: code should not reach here!" && false );
      }
    }
  }
  std::cerr << "Request Grid Dim : " << this->NumberOfSamples[0] << ", "  << this->NumberOfSamples[1] << ", "  << this->NumberOfSamples[2] << "\n";
  std::cerr << "Computed Grid Dim: " << N[0] << ", "  << N[1] << ", "  << N[2] << "\n";
  if (this->UseBiasVector)
  {
    double a[3];
    a[0] = fabs(this->BiasVector[0]);
    a[1] = fabs(this->BiasVector[1]);
    a[2] = fabs(this->BiasVector[2]);

    // Find the max component
    int bdir =
      (a[0] > a[1]) ? ((a[0] > a[2]) ? 0 : 2) : ((a[1] > a[2]) ? 1 : 2);

    if (bdir == 0)
    {
      N[0] = std::min(N[0], std::max(N[1], N[2]));
    }
    else if (bdir == 1)
    {
      N[1] = std::min(N[1], std::max(N[0], N[2]));
    }
    else
    {
      N[2] = std::min(N[2], std::max(N[0], N[1]));
    }
    std::cerr << "Adjusted Grid Dim: " << N[0] << ", "  << N[1] << ", "  << N[2] << "\n";
  }
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeAndAdjustRegionParameters(
    vtkOverlappingAMR *amrds, double h[3] )
{
  assert( "pre: AMR dataset is NULL" && (amrds != NULL) );

  // STEP 0: Get domain parameters from root level metadata
  int dims[3];
  double h0[3];
  double domainMin[3];
  double domainMax[3];
  double rf;
  this->GetDomainParameters( amrds, domainMin, domainMax, h0, dims, rf );

  // STEP 1: Check to see if the requested region intersects the AMR domain
  if( !this->RegionIntersectsWithAMR(domainMin, domainMax, this->Min, this->Max))
  {
    h[0]=h[1]=h[2]=0.0;
    return;
  }

  // STEP 3: Get requested region parameters
  double L0[3]; // initial length of each box side
  double Rh[3]; // initial spacing based on the number of samples requested.
  for( int i=0; i < 3; ++i )
  {
    L0[i] = this->Max[i]-this->Min[i];
    Rh[i] = L0[i]/(this->NumberOfSamples[i]-1);
  }

// DEBUG
//  this->WriteUniformGrid(this->Min,this->NumberOfSamples,Rh,"RequestedGrid" );
// END DEBUG
// DEBUG
//  this->WriteUniformGrid(domainMin,dims,h0,"RootGrid");
// END DEBUG

  // STEP 4: Snap region to domain bounds
  bool outside[6];
  // Determine the Min/Max of the computed grid
  this->SnapBounds(h0, domainMin, domainMax,dims,outside);

  // STEP 5: Compute grid parameters on the snapped region
  double L[3];
  for( int i=0; i < 3; ++i )
  {
    L[i] = this->GridMax[i]-this->GridMin[i];
    h[i] = L[i]/(this->NumberOfSamples[i]-1);
  }

// DEBUG
//  this->WriteUniformGrid(min,this->NumberOfSamples,h,"SnappedGrid");
// END DEBUG

  // STEP 6: Adjust N according to how much of the requested region is cropped
  int N[3];
  this->AdjustNumberOfSamplesInRegion(Rh, outside, N );


  // STEP 7: Adjust region parameters
  for( int i=0; i < 3; ++i )
  {
    this->GridNumberOfSamples[i] = (N[i] > 1)? N[i] : 2;
    h[i] = L[i]/(this->GridNumberOfSamples[i]-1);
  }

  this->ComputeLevelOfResolution(this->GridNumberOfSamples,h0,L,rf);
}

//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::GetRegion( double h[3] )
{
  assert( "pre: Region of interest is NULL!" && (this->ROI != NULL) );

  unsigned int block = 0;
  for( ; block < this->ROI->GetNumberOfBlocks(); ++block  )
  {
    this->ROI->RemoveBlock( block );
  }

  if( h[0]==0.0 && h[1]==0.0 && h[2]==0.0 )
  {
    return;
  }

  vtkUniformGrid *grd = vtkUniformGrid::New();
  grd->SetOrigin( this->GridMin );
  grd->SetSpacing( h );
  grd->SetDimensions( this->GridNumberOfSamples );
  if( grd ->GetNumberOfPoints() == 0 )
  {
    vtkErrorMacro( "Empty Grid!" );
    return;
  }

  vtkUniformGridPartitioner *gridPartitioner = vtkUniformGridPartitioner::New();
  gridPartitioner->SetInputData( grd );
  grd->Delete();

  gridPartitioner->SetNumberOfPartitions( this->NumberOfPartitions );
  gridPartitioner->Update();

  this->ROI->DeepCopy( gridPartitioner->GetOutput() );

  gridPartitioner->Delete();
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::GridsIntersect( double *g1, double *g2 )
{
  assert( "pre: g1 is NULL" && (g1 != NULL) );
  assert( "pre: g2 is NULL" && (g2 != NULL) );

  vtkBoundingBox b1;
  b1.SetBounds( g1);

  vtkBoundingBox b2;
  b2.SetBounds( g2 );

  if( b1.IntersectBox( b2 ) )
  {
    return true;
  }

  return false;
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::IsBlockWithinBounds( double *grd )
{
  assert( "pre: Input AMR grid is NULL" && (grd != NULL) );

  for( unsigned int block=0; block < this->ROI->GetNumberOfBlocks(); ++block )
  {
    if( this->IsRegionMine( block ) )
    {
      vtkUniformGrid *blk =
          vtkUniformGrid::SafeDownCast( this->ROI->GetBlock( block ) );
      assert( "pre: block is NULL" && (blk != NULL) );

      if( this->GridsIntersect( grd, blk->GetBounds() ) )
      {
        return true;
      }
    } // END if region is mine
  } // END for all blocks

  return false;
}

//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::GetRegionProcessId( const int regionIdx )
{
  if( !this->IsParallel() )
  {
    return 0;
  }

  int N = this->Controller->GetNumberOfProcesses();
  return( regionIdx%N );
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::IsRegionMine( const int regionIdx )
{
  if( !this->IsParallel() )
  {
    return true;
  }

  int myRank = this->Controller->GetLocalProcessId();
  if( myRank == this->GetRegionProcessId( regionIdx ) )
  {
    return true;
  }

  return false;
}

//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::IsParallel()
{
  if( this->Controller == NULL )
  {
    return false;
  }

  if( this->Controller->GetNumberOfProcesses() > 1 )
  {
    return true;
  }

  return false;
}

//-----------------------------------------------------------------------------
vtkUniformGrid* vtkAMRResampleFilter::GetReferenceGrid(
    vtkOverlappingAMR *amrds)
{
  assert( "pre:AMR dataset is  NULL" && (amrds != NULL) );

  unsigned int numLevels = amrds->GetNumberOfLevels();
  for(unsigned int l=0; l < numLevels; ++l )
  {
    unsigned int numDatasets = amrds->GetNumberOfDataSets( l );
    for( unsigned int dataIdx=0; dataIdx < numDatasets; ++dataIdx )
    {
      vtkUniformGrid *refGrid = amrds->GetDataSet( l, dataIdx );
      if( refGrid != NULL )
      {
        return( refGrid );
      }
    } // END for all datasets
  } // END for all number of levels

  // This process has no grids
  return NULL;
}

// //-----------------------------------------------------------------------------
// void vtkAMRResampleFilter::WriteUniformGrid(
//     double origin[3], int dims[3], double h[3],
//     std::string prefix )
// {
//   vtkUniformGrid *grd = vtkUniformGrid::New();
//   grd->SetOrigin( origin );
//   grd->SetSpacing( h );
//   grd->SetDimensions( dims );

//   this->WriteUniformGrid( grd, prefix );
//   grd->Delete();
// }

// //-----------------------------------------------------------------------------
// void vtkAMRResampleFilter::WriteUniformGrid(
//     vtkUniformGrid *g, std::string prefix )
// {
//   assert( "pre: Uniform grid (g) is NULL!" && (g != NULL) );

//   vtkXMLImageDataWriter *imgWriter = vtkXMLImageDataWriter::New();

//   std::ostringstream oss;
//   oss << prefix << "." << imgWriter->GetDefaultFileExtension();
//   imgWriter->SetFileName( oss.str().c_str() );
//   imgWriter->SetInputData( g );
//   imgWriter->Write();

//   imgWriter->Delete();
// }