File: vtkAMRBox.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 118,532 kB
  • ctags: 138,251
  • sloc: cpp: 1,443,749; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,127; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 471; makefile: 95; objc: 17
file content (720 lines) | stat: -rw-r--r-- 20,024 bytes parent folder | download | duplicates (8)
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkAMRBox.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 "vtkAMRBox.h"

#include "vtkUnsignedCharArray.h"
#include "vtkCellData.h"
#include "vtkType.h"
#include "vtkStructuredData.h"
#include "vtkImageData.h"
#include "vtkMath.h"

#include <algorithm>
#include <cstring>
#include <cassert>
#include <sstream>
#include <fstream>

//-----------------------------------------------------------------------------
vtkAMRBox::vtkAMRBox()
{
  this->Initialize();
}

//-----------------------------------------------------------------------------
vtkAMRBox::vtkAMRBox(const double* origin, const int* dimensions, const double* spacing, const double* globalOrigin,int gridDescription)
{
  int ndim[3];
  for( int d=0; d<3; ++d )
    {
    ndim[d] = dimensions[d]-1;
    }
  int lo[3], hi[3];
  for( int d=0; d<3; ++d )
    {
    lo[d] = spacing[d]>0? vtkMath::Round( (origin[d] - globalOrigin[d])/spacing[d] ): 0;
    hi[d] = lo[d] + ndim[d]-1;
    }

  this->SetDimensions( lo, hi, gridDescription);
}

//-----------------------------------------------------------------------------
vtkAMRBox::vtkAMRBox(
    int ilo,int jlo,int klo,
    int ihi,int jhi,int khi)
{
  this->BuildAMRBox( ilo, jlo, klo, ihi,jhi,khi);
}

//-----------------------------------------------------------------------------
vtkAMRBox::vtkAMRBox(const int *lo, const int *hi)
{
  this->BuildAMRBox( lo[0],lo[1],lo[2],hi[0],hi[1],hi[2] );
}

//-----------------------------------------------------------------------------
vtkAMRBox::vtkAMRBox(const int *dims)
{
  this->BuildAMRBox(dims[0],dims[2],dims[4],dims[1],dims[3],dims[5] );
}



//-----------------------------------------------------------------------------
void vtkAMRBox::BuildAMRBox(
    const int ilo, const int jlo, const int klo,
    const int ihi, const int jhi, const int khi  )
{
  this->Initialize();
  this->SetDimensions(ilo,jlo,klo,ihi,jhi,khi);
}

//-----------------------------------------------------------------------------
vtkAMRBox::vtkAMRBox(const vtkAMRBox &other)
{
  *this = other;
}

//-----------------------------------------------------------------------------
vtkAMRBox &vtkAMRBox::operator=(const vtkAMRBox &other)
{
 assert( "pre: AMR Box instance is invalid" && !other.IsInvalid() );

  if (this==&other) return *this;
  for(int i=0; i<3; i++)
    {
    this->LoCorner[i] = other.LoCorner[i];
    this->HiCorner[i] = other.HiCorner[i];
    }
  return *this;
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Initialize( )
{
  for( int i=0; i < 3; ++i )
    {
    this->LoCorner[ i ] = 0;
    this->HiCorner[ i ] = 0;
    }
}

//-----------------------------------------------------------------------------
void vtkAMRBox::SetDimensions(
    int ilo,int jlo,int klo,
    int ihi,int jhi,int khi, int desc)
{
  assert (ihi-ilo>=-1 && jhi-jlo>=-1 && khi-klo>=-1);
  this->LoCorner[0]= ilo;
  this->LoCorner[1]= jlo;
  this->LoCorner[2]= klo;
  this->HiCorner[0]= ihi;
  this->HiCorner[1]= jhi;
  this->HiCorner[2]= khi;

  switch( desc )
    {
    case VTK_XY_PLANE:
      this->HiCorner[2] = this->LoCorner[2]-1;
      break;
    case VTK_XZ_PLANE:
      this->HiCorner[1] = this->LoCorner[1]-1;
      break;
    case VTK_YZ_PLANE:
      this->HiCorner[0] = this->LoCorner[0]-1;
      break;
    }
}

//-----------------------------------------------------------------------------
void vtkAMRBox::SetDimensions(const int *lo, const int *hi, int desc)
{
  this->SetDimensions(
      lo[0],lo[1],lo[2],
      hi[0],hi[1],hi[2], desc);
}

//-----------------------------------------------------------------------------
void vtkAMRBox::SetDimensions(const int *dims, int desc)
{
  this->SetDimensions(
      dims[0],dims[2],dims[4],
      dims[1],dims[3],dims[5], desc);
}

//-----------------------------------------------------------------------------
void vtkAMRBox::GetDimensions(int *lo, int *hi) const
{
 assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  for (int q=0; q < 3; ++q)
    {
    lo[q]=this->LoCorner[q];
    hi[q]=this->HiCorner[q];
    }
}

//-----------------------------------------------------------------------------
void vtkAMRBox::GetDimensions(int dims[6]) const
{
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  dims[0]=this->LoCorner[0];
  dims[1]=this->HiCorner[0];
  dims[2]=this->LoCorner[1];
  dims[3]=this->HiCorner[1];
  dims[4]=this->LoCorner[2];
  dims[5]=this->HiCorner[2];
}

//-----------------------------------------------------------------------------
void vtkAMRBox::GetValidHiCorner(int *hi) const
{
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  for (int q=0; q < 3; ++q)
    {
    hi[q] = this->EmptyDimension(q)? this->LoCorner[q]: this->HiCorner[q];
    }
}

//-----------------------------------------------------------------------------
void vtkAMRBox::GetBoxOrigin(const vtkAMRBox& extent, const double X0[3], const double spacing[3],double x0[3])
{
  assert( "pre: input array is NULL" && (x0 != NULL) );
  x0[0] = x0[1] = x0[2] = 0.0;

  for( int i=0; i < 3; ++i )
    {
    x0[i] = X0[i]+ extent.GetLoCorner()[i]*spacing[i];
    }
}


//-----------------------------------------------------------------------------
void vtkAMRBox::GetBounds(const vtkAMRBox& extent, const double origin[3], const double spacing[3],  double bounds[6])
{
  int i, j;
  for( i=0, j=0; i < 3; ++i )
    {
    bounds[ j++ ] = origin[i]+extent.LoCorner[i]*spacing[i];
    bounds[ j++ ] = origin[i]+(extent.HiCorner[i]+1)*spacing[i];
    }
}

//-----------------------------------------------------------------------------
bool vtkAMRBox::HasPoint(const vtkAMRBox& box, const double origin[3], const double spacing[3],  double x, double y,  double z )
{
  assert( "pre: AMR Box instance is invalid" && !box.IsInvalid() );

  double bb[6];
  vtkAMRBox::GetBounds(box,origin, spacing,bb);
  double min[3]={bb[0],bb[2],bb[4]};
  double max[3]={bb[1],bb[3],bb[5]};

  if( x >= min[0] && x <= max[0] &&
      y >= min[1] && y <= max[1] &&
      z >= min[2] && z <= max[2]   )
    {
    return true;
    }
  return false;

}

//-----------------------------------------------------------------------------
bool vtkAMRBox::operator==(const vtkAMRBox &other) const
{
  if ((this->Empty() && other.Empty())
      ||(this->LoCorner[0]==other.LoCorner[0]
         && this->LoCorner[1]==other.LoCorner[1]
         && this->LoCorner[2]==other.LoCorner[2]
         && this->HiCorner[0]==other.HiCorner[0]
         && this->HiCorner[1]==other.HiCorner[1]
         && this->HiCorner[2]==other.HiCorner[2]))
    {
    return true;
    }
  return false;
}

//-----------------------------------------------------------------------------
ostream &vtkAMRBox::Print(ostream &os) const
{
  os << "-D AMR box => "
     << "Low: ("  << this->LoCorner[0]
     << ","  << this->LoCorner[1]
     << ","  << this->LoCorner[2]
     << ") High: (" << this->HiCorner[0]
     << ","  << this->HiCorner[1]
     << ","  << this->HiCorner[2]
     << ")";
  return os;
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Serialize( unsigned char*& buffer, vtkIdType& bytesize)
{
  assert( "pre: input buffer is expected to be NULL" && (buffer==NULL) );

  bytesize       = vtkAMRBox::GetBytesize();
  buffer         = new unsigned char[ bytesize ];
  assert( buffer != NULL );

  // STEP 0: set pointer to traverse the buffer
  unsigned char* ptr = buffer;

  // STEP 7: serialize the low corner
  std::memcpy(ptr, &(this->LoCorner), 3*sizeof(int) );
  ptr += 3*sizeof( int );

  // STEP 8: serialize the high corner
  std::memcpy(ptr, &(this->HiCorner), 3*sizeof(int) );
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Serialize( int* buffer) const
{
  memcpy(buffer,  this->LoCorner, 3*sizeof(int));
  memcpy(buffer+3, this->HiCorner, 3*sizeof(int));
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Deserialize(
    unsigned char* buffer, const vtkIdType& vtkNotUsed(bytesize) )
{
  assert( "pre: input buffer is NULL" && (buffer != NULL) );

  // STEP 0: set pointer to traverse the buffer
  unsigned char *ptr = buffer;


  // STEP 7: de-serialize the low corner
  std::memcpy( &(this->LoCorner), ptr, 3*sizeof(int) );
  ptr += 3*sizeof( int );
  assert( ptr != NULL );

  // STEP 8: de-serialize the high corner
  std::memcpy(&(this->HiCorner), ptr,  3*sizeof(int) );
  ptr += 3*sizeof( int );
  assert( ptr != NULL );
}


//-----------------------------------------------------------------------------
bool vtkAMRBox::IntersectBoxAlongDimension(const vtkAMRBox &other, const int q)
{
  assert( "pre: dimension is out-of-bounds!" && (q >= 0) && (q <= 2) );
  if(this->EmptyDimension(q) || other.EmptyDimension(q))
    {
    return false;
    }
  if( this->LoCorner[q] <= other.LoCorner[q] )
    {
    this->LoCorner[q] = other.LoCorner[q];
    }
  if (this->HiCorner[q] >= other.HiCorner[q])
    {
    this->HiCorner[q] = other.HiCorner[q];
    }
  if (this->LoCorner[q] >= this->HiCorner[q])
    {
    return false;
    }
  return true;
}

bool vtkAMRBox::Intersect(const vtkAMRBox &other)
{
  if( !this->IntersectBoxAlongDimension(other,0) ||
      !this->IntersectBoxAlongDimension(other,1) ||
      !this->IntersectBoxAlongDimension(other,2) )
    {
    return false;
    }
  return true;
}


int vtkAMRBox::GetCellLinearIndex(const vtkAMRBox& box, const int i, const int j, const int k, int dim[3])
{
  // Convert to local numbering
  int I[3]={  i-box.GetLoCorner()[0],
              j-box.GetLoCorner()[1],
              k-box.GetLoCorner()[2]};

  // Get Cell sizes
  int N[3] = {dim[0]-1, dim[1]-1,dim[2]-1};

  // Reduce the sizes and indices to those
  // that correspond to the non-null dimensions
  int nd(0);
  for(int d=0; d<3; d++)
    {
    if(!box.EmptyDimension(d))
      {
      N[nd] = N[d];
      I[nd] = I[d];
      assert(I[nd]>=0 && I[nd]<N[nd]);
      nd++;
      }
    }

  int idx=0;
  for(int d=nd-1; d>=0; d--)
    {
    idx= idx*N[d]+I[d];
    }
  return idx;
}



void vtkAMRBox::Coarsen(int r)
{
  assert( "pre: Input refinement ratio must be >= 2" && (r >= 2)  );
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );

  if( this->Empty() )
    {
    std::cerr << "WARNING: tried refining an empty AMR box!\n";
    std::cerr << "FILE:" << __FILE__ << std::endl;
    std::cerr << "LINE:" << __LINE__ << std::endl;
    std::cerr.flush();
    return;
    }
  for(int q=0; q < 3; ++q )
    {
    if(this->EmptyDimension(q))
      {
      continue;
      }
    this->LoCorner[q]=( (this->LoCorner[q] < 0)?
            -abs(this->LoCorner[q]+1)/r-1 : this->LoCorner[q]/r);
    this->HiCorner[q]=( this->HiCorner[q]<0 ?
            -abs(this->HiCorner[q]+1)/r-1 : this->HiCorner[q]/r);
    }
  assert( "post: Coarsened AMR box should not be empty!" && !this->Empty() );
  assert( "post: Coarsened AMR Box instance is invalid" && !this->IsInvalid() );
}

void vtkAMRBox::Refine(int r)
{
  assert( "pre: Input refinement ratio must be >= 1" && (r >= 1)  );
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );

  if( this->Empty() )
    {
    std::cerr << "WARNING: tried refining an empty AMR box!\n";
    std::cerr << "FILE:" << __FILE__ << std::endl;
    std::cerr << "LINE:" << __LINE__ << std::endl;
    std::cerr.flush();
    return;
    }
  for(int q=0; q < 3; ++q )
    {
    if(!this->EmptyDimension(q))
      {
      this->LoCorner[q]=this->LoCorner[q]*r;
      this->HiCorner[q]=(this->HiCorner[q]+1)*r-1;
      }
    }
  assert( "post: Refined AMR box should not be empty!" && !this->Empty() );
  assert( "post: Refined AMR Box instance is invalid" && !this->IsInvalid() );

}

//-----------------------------------------------------------------------------
bool vtkAMRBox::DoesBoxIntersectAlongDimension(
      const vtkAMRBox &other, const int q) const
{
  if(this->EmptyDimension(q) && other.EmptyDimension(q))
    {
    return true;
    }
  int minVal = 0;
  int maxVal = 0;
  minVal = (this->LoCorner[q] < other.LoCorner[q]) ?
      other.LoCorner[q] : this->LoCorner[q];
  maxVal = (this->HiCorner[q] > other.HiCorner[q]) ?
      other.HiCorner[q] : this->HiCorner[q];

  if (minVal >= maxVal)
    {
    return false;
    }
  return true;
}


bool vtkAMRBox::DoesIntersect(const vtkAMRBox &other) const
{
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  if (this->Empty())
    {
    return false;
    }
  if (other.Empty())
    {
    return false;
    }
  if( !this->DoesBoxIntersectAlongDimension(other,0) ||
      !this->DoesBoxIntersectAlongDimension(other,1) ||
      !this->DoesBoxIntersectAlongDimension(other,2) )
    {
    return false;
    }
  return true;
}

int vtkAMRBox::ComputeStructuredCoordinates(const vtkAMRBox& box, const double dataOrigin[3],
                                 const double h[3], const double x[3], int ijk[3],  double pcoords[3])
{
  double origin[3];
  vtkAMRBox::GetBoxOrigin(box,dataOrigin,h,origin);

  int num[3];
  box.GetNumberOfNodes(num);
  int extent[6] = {0,num[0]-1,0,num[1]-1,0,num[2]-1};

  double bounds[6];
  vtkAMRBox::GetBounds(box,dataOrigin,h,bounds);

  return vtkImageData::ComputeStructuredCoordinates(x,ijk,pcoords,extent, h, origin,bounds);
}

//------------------------------------------------------------------------------
void vtkAMRBox::GetGhostVector(int r, int nghost[6]) const
{
  // STEP 0: initialize nghost
  for( int i=0; i < 3; ++i )
    {
    nghost[i*2] = nghost[i*2+1] = 0;
    }

  // STEP 1: compute number of ghost layers along each dimension's min and max.
  // Detecting partially overlapping boxes is based on the following:
  // Cell location k at level L-1 holds the range [k*r,k*r+(r-1)] of
  // level L, where r is the refinement ratio. Consequently, if the
  // min extent of the box is greater than k*r or if the max extent
  // of the box is less than k*r+(r-1), then the grid partially overlaps.

  vtkAMRBox coarsenedBox = *this;
  coarsenedBox.Coarsen(r);
  for( int i=0; i < 3; ++i )
    {
    if(!this->EmptyDimension(i))
      {
      int minRange[2];
      minRange[0] = coarsenedBox.LoCorner[i]*r;
      minRange[1] = coarsenedBox.LoCorner[i]*r + (r-1);
      if( this->LoCorner[i] > minRange[0] )
        {
        nghost[i*2]=(minRange[1]+1)- this->LoCorner[i];
        }

      int maxRange[2];
      maxRange[0] = coarsenedBox.HiCorner[i]*r;
      maxRange[1] = coarsenedBox.HiCorner[i]*r + (r-1);
      if( this->HiCorner[i] < maxRange[1] )
        {
        nghost[i*2+1] = this->HiCorner[i]-(maxRange[0]-1);
        }
      }
    } // END for all dimensions
}

void vtkAMRBox::RemoveGhosts(int r)
{
  // Detecting partially overlapping boxes is based on the following:
  // Cell location k at level L-1 holds the range [k*r,k*r+(r-1)] of
  // level L, where r is the refinement ratio. Consequently, if the
  // min extent of the box is greater than k*r or if the max extent
  // of the box is less than k*r+(r-1), then the grid partially overlaps.
  vtkAMRBox coarsenedBox = *this;
  coarsenedBox.Coarsen(r);
  for( int i=0; i < 3; ++i )
    {
    if(!this->EmptyDimension(i))
      {
      int minRange[2];
      minRange[0] = coarsenedBox.LoCorner[i]*r;
      minRange[1] = coarsenedBox.LoCorner[i]*r + (r-1);
      if( this->LoCorner[i] > minRange[0] )
        {
        this->LoCorner[i]=(minRange[1]+1);
        }

      int maxRange[2];
      maxRange[0] = coarsenedBox.HiCorner[i]*r;
      maxRange[1] = coarsenedBox.HiCorner[i]*r + (r-1);
      if( this->HiCorner[i] < maxRange[1] )
        {
        this->HiCorner[i] = (maxRange[0]-1);
        }
      }
    } // END for all dimensions
}


int vtkAMRBox::ComputeDimension() const
{
 int dim(3);
 for(int i=0;i<3; i++)
   {
   if(this->EmptyDimension(i))
     {
     dim--;
     }
   }
 return( dim );
}

void vtkAMRBox::GetNumberOfCells(int ext[3]) const
{
  ext[0] = this->HiCorner[0]-this->LoCorner[0]+1;
  ext[1] = this->HiCorner[1]-this->LoCorner[1]+1;
  ext[2] = this->HiCorner[2]-this->LoCorner[2]+1;
}

vtkIdType vtkAMRBox::GetNumberOfCells() const
{
  int cellExtent[3];
  this->GetNumberOfCells(cellExtent);
  int numCells = 0;
  for( int i=0; i < 3; ++i )
    {
    if( numCells == 0 )
      {
      numCells = cellExtent[i];
      }
    else if( cellExtent[i] != 0 )
      {
      numCells *= cellExtent[i];
      }
    }
  return( numCells );
}

//-----------------------------------------------------------------------------
bool vtkAMRBox::Contains(int i,int j,int k) const
{
  int ijk[3] = {i,j,k};
  return this->Contains(ijk);
}


//-----------------------------------------------------------------------------
bool vtkAMRBox::Contains(const int *I) const
{
  bool res(true);
  for(int i=0; i<3; i++)
    {
    if (!this->EmptyDimension(i) &&  (this->LoCorner[i]>I[i] || this->HiCorner[i]<I[i]))
      {
      res = false;
      }
    }
  return res;
}

//-----------------------------------------------------------------------------
bool vtkAMRBox::Contains(const vtkAMRBox &other) const
{
  const int *lo=other.LoCorner;
  const int *hi=other.HiCorner;
  return this->Contains(lo) && this->Contains(hi);
}

void vtkAMRBox::GetNumberOfNodes(int *ext) const
{
  ext[0] = this->HiCorner[0]-this->LoCorner[0]+2;
  ext[1] = this->HiCorner[1]-this->LoCorner[1]+2;
  ext[2] = this->HiCorner[2]-this->LoCorner[2]+2;
  assert(ext[0]>=1 && ext[1]>=1 && ext[2]>=1);
}

//-----------------------------------------------------------------------------
vtkIdType vtkAMRBox::GetNumberOfNodes() const
{
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  int ext[3];
  this->GetNumberOfNodes( ext );
  int numNodes = 0;
  for( int i=0; i < 3; ++i )
    {
    if( numNodes == 0 )
      {
      numNodes = ext[i];
      }
    else if( ext[i] != 0 )
      {
      numNodes *= ext[i];
      }
    }
  return( numNodes );
}


//-----------------------------------------------------------------------------
void vtkAMRBox::Shift(int i, int j, int k)
{
  int ijk[3] = {i,j,k};
  this->Shift(ijk);
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Shift(const int *ijk)
{
  for(int q=0; q < 3; ++q )
    {
    this->LoCorner[q] = this->LoCorner[q]+ijk[q];
    this->HiCorner[q] = this->HiCorner[q]+ijk[q];
    }
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Grow(int byN)
{
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  // TODO: One question here is, should we allow negative indices?
  //       Or should we otherwise, ensure that the box is grown with
  //       bounds.
  for(int q=0; q < 3; ++q )
    {
    if(!this->EmptyDimension(q))
      {
      this->LoCorner[q]-=byN;
      this->HiCorner[q]+=byN;
      }
    }
  assert( "post: Grown AMR Box instance is invalid" && !this->IsInvalid() );
}

//-----------------------------------------------------------------------------
void vtkAMRBox::Shrink(int byN)
{
  assert( "pre: AMR Box instance is invalid" && !this->IsInvalid() );
  // TODO: One question here is, should we allow negative indices?
  //       Or should we otherwise, ensure that the box is grown with
  //       bounds.
  for(int q=0; q < 3; ++q )
    {
    if(!this->EmptyDimension(q))
      {
      this->LoCorner[q]+=byN;
      this->HiCorner[q]-=byN;
      }
    }
  assert( "post: Grown AMR Box instance is invalid" && !this->IsInvalid() );
}