vtkStructuredAMRGridConnectivity.h 26.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*=========================================================================

 Program:   Visualization Toolkit
 Module:    vtkStructuredAMRGridConnectivity.h

 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.

 =========================================================================*/
15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * @class   vtkStructuredAMRGridConnectivity
 *  grid connectivity.
 *
 *
 *  A concrete instance of vtkAbstractGridConnectivity that implements
 *  functionality for computing the neighboring topology within a structured
 *  AMR grid, as well as, generating ghost-layers. Support is provided for
 *  1-D, 2-D (XY,XZ,YZ) and 3-D cell-centered datasets. This implementation
 *  does not have any support for distributed data. For the parallel
 *  implementation see vtkPStructuredAMRGridConnectivity.
 *
 * @sa
28
 *  vtkPStructuredAMRGridConnectivity vtkAbstractGridConnectivity
29 30
*/

31 32
#ifndef vtkStructuredAMRGridConnectivity_h
#define vtkStructuredAMRGridConnectivity_h
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#include "vtkFiltersGeometryModule.h" // For export macro
#include "vtkAbstractGridConnectivity.h"

#include "vtkStructuredAMRNeighbor.h" // For vtkStructuredAMRNeighbor def.


// C++ includes
#include <map>     // For STL map
#include <ostream> // For STL stream
#include <set>     // For STL set
#include <vector>  // For STL vector

class VTKFILTERSGEOMETRY_EXPORT vtkStructuredAMRGridConnectivity :
  public vtkAbstractGridConnectivity
{
public:
  static vtkStructuredAMRGridConnectivity* New();
  vtkTypeMacro(vtkStructuredAMRGridConnectivity, vtkAbstractGridConnectivity);
52
  void PrintSelf(ostream& os, vtkIndent indent ) override;
53

54 55 56 57 58 59 60
  /**
   * Initializes this instance of vtkStructuredAMRGridConnectivity where N
   * is the total number of grids in the AMR hierarchy. Optionally, if the
   * AMR dataset has a constant refinement, it should be specified during
   * initialization as the code optimizes for it. If a -1 or no refinement
   * ratio is specified a varying refinement ratio is assumed.
   */
61 62 63 64
  void Initialize(
      const unsigned int NumberOfLevels,
      const unsigned int N, const int RefinementRatio=-1);

65 66 67
  /**
   * Computes neighboring information.
   */
68
  void ComputeNeighbors() override;
69

70 71 72
  /**
   * Creates ghost layers.
   */
73
  void CreateGhostLayers(const int N=1) override;
74

75 76 77 78 79
  /**
   * Registers the AMR grid with the given global linear grid ID (starting
   * numbering from 0) and level and refinement ratio. This method is to be
   * used when the refinement ratio is not constant.
   */
80 81 82 83 84 85 86 87 88
  virtual void RegisterGrid(
        const int gridIdx, const int level, const int refinementRatio,
        int extents[6],
        vtkUnsignedCharArray* nodesGhostArray,
        vtkUnsignedCharArray* cellGhostArray,
        vtkPointData* pointData,
        vtkCellData* cellData,
        vtkPoints* gridNodes);

89 90 91 92 93
  /**
   * Registers the AMR grid with the given global linear grid ID (starting
   * numbering from 0) and level. The extents of the grid are expected to be
   * global node extents.
   */
94 95 96 97 98 99 100 101
  virtual void RegisterGrid(
      const int gridIdx, const int level, int extents[6],
      vtkUnsignedCharArray* nodesGhostArray,
      vtkUnsignedCharArray* cellGhostArray,
      vtkPointData* pointData,
      vtkCellData* cellData,
      vtkPoints* gridNodes);

102 103 104 105 106 107
  //@{
  /**
   * Get/Set macro for BalancedRefinement property, default is true. If the
   * refinement is balanced, then, adjacent grids in the AMR hierarchy can
   * only differ by one level. By default, a balanced refinement is assumed.
   */
108 109
  vtkSetMacro(BalancedRefinement,bool);
  vtkGetMacro(BalancedRefinement,bool);
110 111 112 113
  //@}

  //@{
  /**
luz.paz's avatar
luz.paz committed
114
   * Get/Set macro NodeCentered property which indicates if the data is
115 116 117
   * node-centered or cell-centered. By default, node-centered is set to false
   * since AMR datasets are primarily cell-centered.
   */
118 119
  vtkSetMacro(NodeCentered,bool);
  vtkGetMacro(NodeCentered,bool);
120
  //@}
121

122 123 124 125 126
  //@{
  /**
   * Get/Set CellCentered property which indicates if the data is cell-centered
   * By default, cell-centered is set to true.
   */
127 128
  vtkSetMacro(CellCentered,bool);
  vtkGetMacro(CellCentered,bool);
129
  //@}
130

131 132 133 134
  /**
   * Returns the number of neighbors for the grid corresponding to the given
   * grid ID.
   */
135 136
  int GetNumberOfNeighbors(const int gridID);

137 138 139
  /**
   * Returns the ghost extend for the grid corresponding to the given grid ID.
   */
140 141
  void GetGhostedExtent(const int gridID, int ext[6]);

142 143 144
  /**
   * Returns the AMR neighbor for the patch with the corresponding grid ID.
   */
145 146 147 148
  vtkStructuredAMRNeighbor GetNeighbor(const int gridID, const int nei);

protected:
  vtkStructuredAMRGridConnectivity();
149
  ~vtkStructuredAMRGridConnectivity() override;
150

151 152 153
  /**
   * Sets the total number of grids(blocks) in the AMR hierarchy
   */
154
  void SetNumberOfGrids( const unsigned int N ) override;
155

156 157 158
  /**
   * Creates the ghosted mask arrays
   */
159 160
  void CreateGhostedMaskArrays(const int gridID);

161 162 163
  /**
   * Creates the ghosted extent of the given grid
   */
164 165
  void CreateGhostedExtent(const int gridID, const int N);

166 167 168
  /**
   * Sets the ghost extent for the grid corresponding to the given grid ID.
   */
169 170
  void SetGhostedExtent(const int gridID, int ext[6]);

171 172 173
  /**
   * Gets the coarsened extent for the grid with the given grid index.
   */
174 175 176
  void GetCoarsenedExtent(
      const int gridIdx, int fromLevel, int toLevel, int ext[6]);

177
  /**
luz.paz's avatar
luz.paz committed
178
   * Gets the refined extent for the grid with the given grid index.
179
   */
180 181 182
  void GetRefinedExtent(
      const int gridIdx, int fromLevel, int toLevel, int ext[6]);

183 184 185
  /**
   * Refines the given extent.
   */
186 187 188
  void RefineExtent(
      int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);

189 190 191 192
  /**
   * Given the global i,j,k index of a cell at a coarse level, fromLevel, this
   * method computes the range of cells on the refined grid.
   */
193 194 195 196 197 198
  void GetCellRefinedExtent(
        int orient[3], int ndim,
        const int i, const int j, const int k,
        const int fromLevel, const int toLevel,
        int ext[6]);

199 200 201
  /**
   * Coarsens the given extent.
   */
202 203 204
  void CoarsenExtent(
      int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);

205 206 207
  /**
   * Gets the grid extent for the grid with the given grid ID.
   */
208 209
  void GetGridExtent( const int gridIdx, int ext[6] );

210 211 212
  /**
   * Returns the level of the grid with the corresponding grid ID.
   */
213 214
  int GetGridLevel( const int gridIdx );

215 216 217
  /**
   * Checks if the given level has been registered
   */
218 219
  bool LevelExists( const int level );

220 221 222
  /**
   * Checks if the node is an interior node in the given extent.
   */
223 224 225
  bool IsNodeInterior(
      const int i, const int j, const int k, int ext[6]);

226 227 228
  /**
   * Checks if the node is within the extent.
   */
229 230 231
  bool IsNodeWithinExtent(
      const int i, const int j, const int k, int ext[6]);

232 233 234
  /**
   * Checks if the node is on a shared boundary.
   */
235 236 237 238
  bool IsNodeOnSharedBoundary(
        const int i, const int j, const int k,
        const int gridId,int gridExt[6]);

239 240 241
  /**
   * Checks if the node is on the boundary of the given extent.
   */
242 243 244
  bool IsNodeOnBoundaryOfExtent(
          const int i, const int j, const int k, int ext[6] );

245 246 247
  /**
   * Inserts the grid corresponding to the given ID at the prescribed level.
   */
248 249
  void InsertGridAtLevel( const int level, const int gridID);

250 251 252 253
  /**
   * Loops through the neighbors of this grid and computes the send and rcv
   * extents for the N requested ghost layers.
   */
254 255
  void ComputeNeighborSendAndRcvExtent(const int gridID, const int N);

256 257 258 259
  /**
   * Computes the whole extent w.r.t. level 0 as well as the AMR dataset
   * description and dimension.
   */
260 261
  void ComputeWholeExtent();

262 263 264 265
  /**
   * Gets the whole extent with respect to the given level.
   * NOTE: This method assument that the whole extent has been computed.
   */
266 267
  void GetWholeExtentAtLevel(const int level, int ext[6]);

268
  /**
luz.paz's avatar
luz.paz committed
269
   * Establishes neighboring relationship between grids i,j wheren i,j are
270 271
   * global indices.
   */
272 273
  void EstablishNeighbors(const int i, const int j);

274 275 276
  /**
   * Computes the node orientation tuple for the given i,j,k node.
   */
277 278 279 280
  void GetNodeOrientation(
      const int i, const int j, const int k,
      int gridExt[6], int nodeOrientation[3]);

281 282 283 284 285 286 287
  /**
   * Establishes the orientation vector and dimension based on the computed
   * data description. The orientation vector is a 3-tuple, which encodes the
   * dimensions that are used. For example, let's say that we want to define
   * the orientation to be in the XZ plane, then, the orient array would be
   * constructed as follows: {0,2 -1}, where -1 indicates a NIL value.
   */
288 289 290
  void GetOrientationVector(
      const int dataDescription, int orient[3], int &ndim);

291 292 293
  /**
   * Checks if a constant refinement ratio has been specified.
   */
294 295
  bool HasConstantRefinementRatio( );

296 297 298
  /**
   * Sets the refinement ratio at the given level.
   */
299 300
  void SetRefinementRatioAtLevel( const int level, const int r);

301 302 303
  /**
   * Returns the refinement ratio at the given level.
   */
304 305
  int GetRefinementRatioAtLevel(const int level);

306 307 308
  /**
   * Checks if the extent ext1 and ext2 are equal.
   */
309 310
  bool AreExtentsEqual(int ext1[6], int ext2[6] );

311 312 313
  /**
   * Constructs the block topology for the given grid.
   */
314 315
  void SetBlockTopology(const int gridID);

316 317 318 319 320 321 322
  /**
   * Returns the number of faces of the block corresponding to the given grid
   * ID that are adjacent to at least one other block. Note, this is not the
   * total number of neighbors for the block. This method simply checks how
   * many out of the 6 block faces have connections. Thus, the return value
   * has an upper-bound of 6.
   */
323 324
  int GetNumberOfConnectingBlockFaces( const int gridID );

325 326 327 328
  //@{
  /**
   * Checks if the block corresponding to the given grid ID has a block
   * adjacent to it in the given block direction.
luz.paz's avatar
luz.paz committed
329
   * NOTE: The block direction is essentially one of the 6 faces of the
330 331 332 333 334 335 336 337 338 339
   * block defined as follows:
   * <ul>
   * <li> FRONT  = 0 (+k diretion)  </li>
   * <li> BACK   = 1 (-k direction) </li>
   * <li> RIGHT  = 2 (+i direction) </li>
   * <li> LEFT   = 3 (-i direction) </li>
   * <li> TOP    = 4 (+j direction) </li>
   * <li> BOTTOM = 5 (-j direction) </li>
   * </ul>
   */
340 341 342 343 344 345 346 347 348 349 350
  bool HasBlockConnection(const int gridID, const int blockDirection)
  {
    // Sanity check
    assert("pre: gridID is out-of-bounds" &&
          (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
    assert("pre: BlockTopology has not been properly allocated" &&
          (this->NumberOfGrids == this->BlockTopology.size()));
    assert("pre: blockDirection is out-of-bounds" &&
          (blockDirection >= 0) && (blockDirection < 6) );
    bool status = false;
    if( this->BlockTopology[ gridID ] & (1 << blockDirection) )
351
    {
352
      status = true;
353
    }
354 355
    return( status );
  }
356 357 358 359 360
  //@}

  /**
   * Removes a block connection along the given direction for the block
   * corresponding to the given gridID.
luz.paz's avatar
luz.paz committed
361
   * NOTE: The block direction is essentially one of the 6 faces of the
362 363 364 365 366 367 368 369 370 371
   * block defined as follows:
   * <ul>
   * <li> FRONT  = 0 (+k diretion)  </li>
   * <li> BACK   = 1 (-k direction) </li>
   * <li> RIGHT  = 2 (+i direction) </li>
   * <li> LEFT   = 3 (-i direction) </li>
   * <li> TOP    = 4 (+j direction) </li>
   * <li> BOTTOM = 5 (-j direction) </li>
   * </ul>
   */
372 373
  void RemoveBlockConnection(const int gridID, const int blockDirection);

374 375 376
  /**
   * Adds a block connection along the given direction for the block
   * corresponding to the given gridID.
luz.paz's avatar
luz.paz committed
377
   * NOTE: The block direction is essentially one of the 6 faces of the
378 379 380 381 382 383 384 385 386 387
   * block defined as follows:
   * <ul>
   * <li> FRONT  = 0 (+k diretion)  </li>
   * <li> BACK   = 1 (-k direction) </li>
   * <li> RIGHT  = 2 (+i direction) </li>
   * <li> LEFT   = 3 (-i direction) </li>
   * <li> TOP    = 4 (+j direction) </li>
   * <li> BOTTOM = 5 (-j direction) </li>
   * </ul>
   */
388 389
  void AddBlockConnection(const int gridID, const int blockDirection);

390
  /**
luz.paz's avatar
luz.paz committed
391
   * Clears all block connections for the block corresponding to the given
392 393
   * grid ID.
   */
394 395
  void ClearBlockConnections( const int gridID );

396 397 398
  /**
   * Marks the ghost property for the given node.
   */
399 400 401 402 403
  virtual void MarkNodeProperty(
      const int gridId, const int i, const int j, const int k,
      int gridExt[6], int wholeExt[6],
      unsigned char &p);

404 405 406
  /**
   * Fills the node ghost arrays for the given grid
   */
407 408 409
  virtual void FillNodesGhostArray(
      const int gridId, vtkUnsignedCharArray* nodesArray);

410 411 412
  /**
   * Fills the cell ghost arrays for the given grid
   */
413 414 415
  virtual void FillCellsGhostArray(
      const int gridId, vtkUnsignedCharArray* cellArray);

416 417 418
  /**
   * Fills ghost arrays.
   */
419
  void FillGhostArrays(
420 421
            const int gridId,
            vtkUnsignedCharArray* nodesArray,
422
            vtkUnsignedCharArray* cellsArray ) override;
423

424 425 426 427 428 429 430 431 432 433 434
  /**
   * Compute the AMR neighbor of grid "i" and its neighbor grid "j".

   * Given the structured neighbors computed in normalized space (i.e., at
   * the same level) between the two grids, this method computes the
   * corresponding AMR neighbor which essentially adds other bits of
   * information, such as level, relationship type, etc.

   * NOTE:
   * The extents next1 and next2 for each grid are the normalized extents
   */
435 436 437 438 439 440 441
  vtkStructuredAMRNeighbor GetAMRNeighbor(
      const int i, const int iLevel, int next1[6],
      const int j, const int jLevel, int next2[6],
      const int normalizedLevel,
      const int levelDiff,
      vtkStructuredNeighbor &nei);

442 443 444 445 446
  /**
   * A Helper method to compute the AMR neighbor overlap extents. The method
   * coarsens/refines the gridOverlap and neiOverlap extents accordingly s.t.
   * they are w.r.t. to the level of the grid they refer to.
   */
447 448 449 450 451 452 453
  void ComputeAMRNeighborOverlapExtents(
      const int iLevel, const int jLevel, const int normalizedLevel,
      const vtkStructuredNeighbor &nei,
      int orient[3], int ndim,
      int gridOverlapExtent[6],
      int neiOverlapExtent[6]);

454 455 456
  /**
   * Get 1-D orientation.
   */
457 458 459 460
  int Get1DOrientation(
      const int idx, const int ExtentLo, const int ExtentHi,
      const int OnLo, const int OnHi, const int NotOnBoundary);

461 462 463
  /**
   * Prints the extent
   */
464 465
  void PrintExtent(std::ostream& os, int ext[6]);

466 467 468
  /**
   * Initializes the ghost data-structures
   */
469 470
  void InitializeGhostData( const int gridID );

471 472 473
  /**
   * Transfers the data of the registered grid, to the ghosted data-structures.
   */
474 475
  void TransferRegisteredDataToGhostedData( const int gridID );

476 477 478
  /**
   * Transfers local node-centered neighbor data
   */
479 480 481
  void TransferLocalNodeCenteredNeighborData(
          const int gridID, vtkStructuredAMRNeighbor &nei);

482 483 484 485
  /**
   * Copy cell center value from a coarser level by direct-injection, i.e., the
   * values within the coarse cell is assumed to be constant.
   */
486 487 488
  void GetLocalCellCentersFromCoarserLevel(
          const int gridID, vtkStructuredAMRNeighbor &nei);

489 490 491
  /**
   * Copy cell center values from a finer level by cell averaging.
   */
492 493 494
  void GetLocalCellCentersFromFinerLevel(
          const int gridID, vtkStructuredAMRNeighbor &nei);

495 496 497 498
  /**
   * Copy cell center values to fill in the ghost levels from a neighbor at
   * the same level as the grid corresponding to the given grid ID.
   */
499 500 501
  void GetLocalCellCentersAtSameLevel(
          const int gridID, vtkStructuredAMRNeighbor &nei);

502 503 504
  /**
   * Transfers local cell-centered neighbor data
   */
505 506 507
  void TransferLocalCellCenteredNeighborData(
          const int gridID, vtkStructuredAMRNeighbor &nei);

508 509 510
  /**
   * Transfers local neighbor data
   */
511 512 513
  void TransferLocalNeighborData(
      const int gridID, vtkStructuredAMRNeighbor &nei);

514 515 516
  /**
   * Fills in the ghost data from the neighbors
   */
517 518
  virtual void TransferGhostDataFromNeighbors(const int gridID);

519 520 521 522
  /**
   * Loops through all arrays and computes the average of the supplied source
   * indices and stores the corresponding average
   */
523 524 525 526
  void AverageFieldData(
      vtkFieldData *source, vtkIdType *sourceIds, const int N,
      vtkFieldData *target, vtkIdType targetIdx);

527 528 529
  /**
   * Loops through all arrays in the source and for each array, it copies the
   * tuples from sourceIdx to the target at targetIdx. This method assumes
luz.paz's avatar
luz.paz committed
530
   * that the source and target have a one-to-one array correspondence, that
531 532
   * is array i in the source corresponds to array i in the target.
   */
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  void CopyFieldData(
      vtkFieldData *source, vtkIdType sourceIdx,
      vtkFieldData *target, vtkIdType targetIdx);


  unsigned int NumberOfLevels; // The total number of levels;
  int DataDimension;    // The dimension of the data, i.e. 2 or 3
  int DataDescription;  // The data description, i.e., VTK_XY_PLANE, etc.
  int WholeExtent[6];   // The whole extent w.r.t. to the root level, level 0.
  int MaxLevel;         // The max level of the AMR hierarchy
  int RefinementRatio;  // The refinement ratio, set in the initialization,iff,
                        // a constant refinement ratio is used. A value of -1
                        // indicates that the refinement ratio is not constant
                        // and the RefinementRatios vector is used instead.

  bool NodeCentered;  // Indicates if the data is node-centered
  bool CellCentered;  // Indicates if the data is cell-centered

  bool BalancedRefinement; // If Balanced refinement is true, then adjacent
                            // grids in the hierarchy can only differ by one
                            // level.

luz.paz's avatar
luz.paz committed
555
  // AMRHierarchy stores the set of grid Ids in [0,N] for each level
556 557 558 559 560 561
  std::map< int, std::set<int> > AMRHierarchy;

  // For each grid, [0,N] store the grid extents,level, and list of neighbors
  std::vector< int > GridExtents; // size of this vector is 6*N
  std::vector< int > GhostedExtents; // size of this vector is 6*N
  std::vector< unsigned char > BlockTopology; // size of this vector is N
Kunda's avatar
Kunda committed
562
  std::vector< int > GridLevels;  // size of this vector is N
563 564 565 566 567 568 569 570 571 572 573 574 575
  std::vector< std::vector<vtkStructuredAMRNeighbor> > Neighbors;

  // For each grid, [0,N], store the donor level,grid and cell information, a
  // DonorLevel of -1 indicates that the cell is not receiving any information
  // from a donor.
  std::vector< std::vector<int> > CellCenteredDonorLevel;


  // RefinementRatios stores the refinement ratio at each level, this vector
  // is used only when the refinement ratio varies across levels
  std::vector< int > RefinementRatios;

private:
576 577
   vtkStructuredAMRGridConnectivity(const vtkStructuredAMRGridConnectivity&) = delete;
   void operator=(const vtkStructuredAMRGridConnectivity&) = delete;
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
};

//=============================================================================
//  INLINE METHODS
//=============================================================================

//------------------------------------------------------------------------------
inline int
vtkStructuredAMRGridConnectivity::GetNumberOfNeighbors(
    const int gridID)
{
  assert("pre: grid ID is out-of-bounds" &&
         (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids)));
  assert("pre: neighbors vector has not been properly allocated" &&
          (this->Neighbors.size()==this->NumberOfGrids));
593
  return( static_cast<int>(this->Neighbors[gridID].size()) );
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
}

//------------------------------------------------------------------------------
inline vtkStructuredAMRNeighbor
vtkStructuredAMRGridConnectivity::GetNeighbor(
      const int gridID, const int nei)
{
  assert("pre: grid ID is out-of-bounds" &&
         (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids)));
  assert("pre: neighbors vector has not been properly allocated" &&
         (this->Neighbors.size()==this->NumberOfGrids));
  assert("pre: nei index is out-of-bounds" &&
         (nei >= 0) &&
         (nei < static_cast<int>(this->Neighbors[gridID].size())));
  return( this->Neighbors[gridID][nei] );
}

//------------------------------------------------------------------------------
inline int
vtkStructuredAMRGridConnectivity::Get1DOrientation(
    const int idx, const int ExtentLo, const int ExtentHi,
    const int OnLo, const int OnHi, const int NotOnBoundary)
{
  if( idx == ExtentLo )
618
  {
619
      return OnLo;
620
  }
621
    else if( idx == ExtentHi )
622
    {
623
      return OnHi;
624
    }
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    return NotOnBoundary;
}

//------------------------------------------------------------------------------
inline
int vtkStructuredAMRGridConnectivity::GetNumberOfConnectingBlockFaces(
    const int gridID )
{
  // Sanity check
  assert("pre: gridID is out-of-bounds" &&
        (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
  assert("pre: BlockTopology has not been properly allocated" &&
        (this->NumberOfGrids == this->BlockTopology.size()));

  int count = 0;
  for( int i=0; i < 6; ++i )
641
  {
642
    if( this->HasBlockConnection( gridID, i ) )
643
    {
644 645
      ++count;
    }
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
  assert( "post: count must be in [0,5]" && (count >=0 && count <= 6) );
  return( count );
}

//------------------------------------------------------------------------------
inline void vtkStructuredAMRGridConnectivity::RemoveBlockConnection(
    const int gridID, const int blockDirection )
{
  // Sanity check
  assert("pre: gridID is out-of-bounds" &&
        (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
  assert("pre: BlockTopology has not been properly allocated" &&
        (this->NumberOfGrids == this->BlockTopology.size()));
  assert("pre: blockDirection is out-of-bounds" &&
        (blockDirection >= 0) && (blockDirection < 6) );

  this->BlockTopology[ gridID ] &= ~(1 << blockDirection);
}

//------------------------------------------------------------------------------
inline void vtkStructuredAMRGridConnectivity::AddBlockConnection(
    const int gridID, const int blockDirection )
{
  // Sanity check
  assert("pre: gridID is out-of-bounds" &&
        (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
  assert("pre: BlockTopology has not been properly allocated" &&
        (this->NumberOfGrids == this->BlockTopology.size()));
  assert("pre: blockDirection is out-of-bounds" &&
        (blockDirection >= 0) && (blockDirection < 6) );
  this->BlockTopology[ gridID ] |= (1 << blockDirection);
}

//------------------------------------------------------------------------------
inline void vtkStructuredAMRGridConnectivity::ClearBlockConnections(
    const int gridID )
{
  // Sanity check
  assert("pre: gridID is out-of-bounds" &&
        (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
  assert("pre: BlockTopology has not been properly allocated" &&
        (this->NumberOfGrids == this->BlockTopology.size()));
  for( int i=0; i < 6; ++i )
690
  {
691
    this->RemoveBlockConnection( gridID, i );
692
  } // END for all block directions
693 694 695 696 697 698 699 700
}

//------------------------------------------------------------------------------
inline
bool vtkStructuredAMRGridConnectivity::AreExtentsEqual(
        int ext1[6], int ext2[6])
{
  for( int i=0; i < 6; ++i )
701
  {
702
    if( ext1[i] != ext2[i] )
703
    {
704
      return false;
705 706
    }
  } // END for
707 708 709 710 711 712 713 714 715
  return true;
}

//------------------------------------------------------------------------------
inline
void vtkStructuredAMRGridConnectivity::PrintExtent(
      std::ostream& os, int ext[6])
{
  for( int i=0; i < 6; i+=2 )
716
  {
717 718 719
    os << "[";
    os << ext[i]   << " ";
    os << ext[i+1] << "] ";
720
  } // END for
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
}

//------------------------------------------------------------------------------
inline
int vtkStructuredAMRGridConnectivity::GetGridLevel(const int gridIdx)
{
  assert("pre: grid Index is out-of-bounds!" &&
         (gridIdx < static_cast<int>(this->NumberOfGrids)));
  assert("pre: grid levels vector has not been allocated" &&
         (this->GridLevels.size()==this->NumberOfGrids) );
  return( this->GridLevels[gridIdx] );
}

//------------------------------------------------------------------------------
inline
void vtkStructuredAMRGridConnectivity::SetRefinementRatioAtLevel(
    const int level, const int r)
{
luz.paz's avatar
luz.paz committed
739
  assert("pre: RefinementRatios vector is not properly allocated" &&
740 741 742 743 744 745 746 747 748 749 750 751 752 753
         this->RefinementRatios.size()==this->NumberOfLevels);
  assert("pre: leve is out-of-bounds!" &&
         (level >= 0) &&
         (level < static_cast<int>(this->RefinementRatios.size())) );
  assert("pre: invalid refinement ratio" && (r >= 2) );

  this->RefinementRatios[ level ] = r;
}

//------------------------------------------------------------------------------
inline
int vtkStructuredAMRGridConnectivity::GetRefinementRatioAtLevel(
      const int level)
{
luz.paz's avatar
luz.paz committed
754
  assert( "pre: RefinementRatios vector is not properly allocated" &&
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
          this->RefinementRatios.size()==this->NumberOfLevels);
  assert( "pre: leve is out-of-bounds!" &&
          (level >= 0) &&
          (level < static_cast<int>(this->RefinementRatios.size())));
  assert( "pre: refinement ratio for level has not been set" &&
          (this->RefinementRatios[ level ] >= 2) );

 return(this->RefinementRatios[level]);
}

//------------------------------------------------------------------------------
inline
bool vtkStructuredAMRGridConnectivity::HasConstantRefinementRatio()
{
  if( this->RefinementRatio < 2 )
770
  {
771
    return false;
772
  }
773 774 775 776 777 778 779 780 781 782 783 784
  return true;
}

//------------------------------------------------------------------------------
inline void vtkStructuredAMRGridConnectivity::GetGridExtent(
                      const int gridIdx, int ext[6])
{
  assert("pre: grid index is out-of-bounds" &&
         ( (gridIdx >= 0) &&
           (gridIdx < static_cast<int>(this->GridExtents.size()) ) ) );

  for( int i=0; i < 6; ++i )
785
  {
786
    ext[ i ] = this->GridExtents[ gridIdx*6+i ];
787
  }
788 789 790 791 792 793 794
}

//------------------------------------------------------------------------------
inline bool vtkStructuredAMRGridConnectivity::LevelExists(
              const int level )
{
  if( this->AMRHierarchy.find(level) != this->AMRHierarchy.end() )
795
  {
796
    return true;
797
  }
798 799 800 801 802 803 804 805
  return false;
}

//------------------------------------------------------------------------------
inline void vtkStructuredAMRGridConnectivity::InsertGridAtLevel(
      const int level, const int gridID )
{
  if( this->LevelExists( level ) )
806
  {
807
    this->AMRHierarchy[ level ].insert( gridID );
808
  }
809
  else
810
  {
811 812 813
    std::set<int> grids;
    grids.insert( gridID );
    this->AMRHierarchy[ level ] = grids;
814
  }
815 816 817
}

#endif /* VTKSTRUCTUREDAMRGRIDCONNECTIVITY_H_ */