VTK  9.7.1
vtkAMRBox.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
20 #ifndef vtkAMRBox_h
21 #define vtkAMRBox_h
22 
23 #include "vtkCommonDataModelModule.h" // For export macro
24 #include "vtkObject.h"
25 #include "vtkStructuredData.h" // For vtkStructuredData::VTK_STRUCTURED_XYZ_GRID definition
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class vtkUniformGrid;
29 class VTKCOMMONDATAMODEL_EXPORT vtkAMRBox
30 {
31 public:
35  vtkAMRBox();
36 
40  vtkAMRBox(const vtkAMRBox& other);
41 
45  vtkAMRBox(int ilo, int jlo, int klo, int ihi, int jhi, int khi);
46 
51  vtkAMRBox(const double* origin, const int* dimensions, const double* spacing,
52  const double* globalOrigin, int gridDescription = vtkStructuredData::VTK_STRUCTURED_XYZ_GRID);
53 
57  vtkAMRBox(const int lo[3], const int hi[3]);
58 
59  vtkAMRBox(const int dims[6]);
60 
64  vtkAMRBox& operator=(const vtkAMRBox& other);
65 
66  virtual ~vtkAMRBox() = default;
67 
69 
72  void Invalidate()
73  {
74  this->LoCorner[0] = this->LoCorner[1] = this->LoCorner[2] = 0;
75  this->HiCorner[0] = this->HiCorner[1] = this->HiCorner[2] = -2;
76  }
78 
82  bool EmptyDimension(int i) const { return HiCorner[i] <= LoCorner[i] - 1; }
83 
87  void SetDimensions(int ilo, int jlo, int klo, int ihi, int jhi, int khi,
89 
93  void SetDimensions(
94  const int lo[3], const int hi[3], int desc = vtkStructuredData::VTK_STRUCTURED_XYZ_GRID);
95 
99  void SetDimensions(const int dims[6], int desc = vtkStructuredData::VTK_STRUCTURED_XYZ_GRID);
100 
104  void GetDimensions(int lo[3], int hi[3]) const;
105 
109  void GetDimensions(int dims[6]) const;
110 
112 
115  vtkIdType GetNumberOfCells() const;
116  void GetNumberOfCells(int num[3]) const;
118 
120 
124  void GetNumberOfNodes(int ext[3]) const;
125  vtkIdType GetNumberOfNodes() const;
127 
133  int ComputeDimension() const;
134 
138  const int* GetLoCorner() const { return this->LoCorner; }
139  const int* GetHiCorner() const { return this->HiCorner; }
140 
146  void GetValidHiCorner(int hi[3]) const;
147 
148  bool Empty() const { return this->IsInvalid(); }
149 
153  bool IsInvalid() const
154  {
155  return ((this->HiCorner[0] < this->LoCorner[0] - 1) ||
156  (this->HiCorner[1] < this->LoCorner[1] - 1) || (this->HiCorner[2] < this->LoCorner[2] - 1));
157  }
158 
164  bool operator==(const vtkAMRBox& other) const;
165 
171  bool operator!=(const vtkAMRBox& other) const { return (!(*this == other)); }
172 
176  ostream& Print(ostream& os) const;
177 
179 
190  void Serialize(unsigned char*& buffer, vtkIdType& bytesize);
191  void Serialize(int* buffer) const;
193 
200  void Deserialize(unsigned char* buffer, const vtkIdType& bytesize);
201 
208  bool DoesBoxIntersectAlongDimension(const vtkAMRBox& other, int q) const;
209 
210  bool DoesIntersect(const vtkAMRBox& other) const;
211 
215  void Coarsen(int r);
216 
220  void Refine(int r);
221 
223 
226  void Grow(int byN);
227  void Shrink(int byN);
229 
231 
234  void Shift(int i, int j, int k);
235  void Shift(const int I[3]);
237 
239 
242  void Add(int i, int j, int k);
243  void Add(const int ijk[3]);
245 
251  bool Intersect(const vtkAMRBox& other);
252 
254 
257  bool Contains(int i, int j, int k) const;
258  bool Contains(const int I[3]) const;
260 
264  bool Contains(const vtkAMRBox&) const;
265 
271  void GetGhostVector(int r, int nghost[6]) const;
272 
277  void RemoveGhosts(int r);
278 
284  static vtkIdType GetBytesize() { return 6 * sizeof(int); }
285 
289  static int GetCellLinearIndex(const vtkAMRBox& box, int i, int j, int k, int imageDimension[3]);
290 
294  static void GetBounds(
295  const vtkAMRBox& box, const double origin[3], const double spacing[3], double bounds[6]);
296 
301  static void GetBoxOrigin(
302  const vtkAMRBox& box, const double X0[3], const double spacing[3], double x0[3]);
303 
308  static bool HasPoint(const vtkAMRBox& box, const double origin[3], const double spacing[3],
309  double x, double y, double z);
310 
314  static int ComputeStructuredCoordinates(const vtkAMRBox& box, const double dataOrigin[3],
315  const double h[3], const double x[3], int ijk[3], double pcoords[3]);
316 
323  int InitializeGrid(vtkUniformGrid* grid, double* origin, double* spacing) const;
324 
332  int InitializeGrid(vtkUniformGrid* grid, double* origin, double* spacing, int nGhosts) const;
333 
341  int InitializeGrid(
342  vtkUniformGrid* grid, double* origin, double* spacing, const int nGhosts[3]) const;
343 
352  int InitializeGrid(vtkUniformGrid* grid, double* origin, double* spacing, int nGhostsI,
353  int nGhostsJ, int nGhostsK) const;
354 
355 protected:
359  void Initialize();
360 
367  bool IntersectBoxAlongDimension(const vtkAMRBox& other, int q);
368 
369 private:
370  int LoCorner[3]; // lo corner cell id.
371  int HiCorner[3]; // hi corner cell id.
372 
374 
379  void BuildAMRBox(int ilo, int jlo, int klo, int ihi, int jhi, int khi);
381 };
382 
383 //*****************************************************************************
385 
389 template <typename T>
390 void FillRegion(T* pArray, const vtkAMRBox& arrayRegion, const vtkAMRBox& destRegion, T fillValue)
391 {
392  // Convert regions to array index space. VTK arrays
393  // always start with 0,0,0.
394  int ofs[3];
395  ofs[0] = -arrayRegion.GetLoCorner()[0];
396  ofs[1] = -arrayRegion.GetLoCorner()[1];
397  ofs[2] = -arrayRegion.GetLoCorner()[2];
398  vtkAMRBox arrayDims(arrayRegion);
399  arrayDims.Shift(ofs);
400  vtkAMRBox destDims(destRegion);
401  destDims.Shift(ofs);
402  // Quick sanity check.
403  if (!arrayRegion.Contains(destRegion))
404  {
405  vtkGenericWarningMacro(<< "ERROR: Array must enclose the destination region. "
406  << "Aborting the fill.");
407  }
408  // Get the bounds of the indices we fill.
409  const int* destLo = destDims.GetLoCorner();
410  int destHi[3];
411  destDims.GetValidHiCorner(destHi);
412  // Get the array dimensions.
413  int arrayHi[3];
414  arrayDims.GetNumberOfCells(arrayHi);
415  // Fill.
416  for (int k = destLo[2]; k <= destHi[2]; ++k)
417  {
418  vtkIdType kOfs = k * arrayHi[0] * arrayHi[1];
419  for (int j = destLo[1]; j <= destHi[1]; ++j)
420  {
421  vtkIdType idx = kOfs + j * arrayHi[0] + destLo[0];
422  for (int i = destLo[0]; i <= destHi[0]; ++i)
423  {
424  pArray[idx] = fillValue;
425  ++idx;
426  }
427  }
428  }
430 }
431 
432 VTK_ABI_NAMESPACE_END
433 #endif
434 // VTK-HeaderTest-Exclude: vtkAMRBox.h
void GetBounds(T a, double bds[6])
VTKIOHDF_EXPORT std::vector< hsize_t > GetDimensions(hid_t fileID, const char *datasetName)
Returns the dimensions of a HDF dataset of a file.
Encloses a rectangular region of voxel like cells.
Definition: vtkAMRBox.h:29
void FillRegion(T *pArray, const vtkAMRBox &arrayRegion, const vtkAMRBox &destRegion, T fillValue)
Fill the region of "pArray" enclosed by "destRegion" with "fillValue" "pArray" is defined on "arrayRe...
Definition: vtkAMRBox.h:390
void Print(const std::vector< T > &input, const std::string &name)
Print a vector with an associated name.
int vtkIdType
Definition: vtkType.h:363
bool VTKCOMMONCORE_EXPORT operator==(const std::string &a, const vtkStringToken &b)
void GetValidHiCorner(int hi[3]) const
Return a high corner.
void Shift(int i, int j, int k)
Shifts the box in index space.
bool IsInvalid() const
Check to see if the AMR box instance is invalid.
Definition: vtkAMRBox.h:153
vtkIdType GetNumberOfCells() const
Gets the number of cells enclosed by the box.
const int * GetLoCorner() const
Get the low corner index.
Definition: vtkAMRBox.h:138
Deprecated vtkImageData.
static vtkIdType GetBytesize()
Returns the number of bytes allocated by this instance.
Definition: vtkAMRBox.h:284
bool EmptyDimension(int i) const
Whether dimension i is empty, e.g.
Definition: vtkAMRBox.h:82
const int * GetHiCorner() const
Definition: vtkAMRBox.h:139
bool Empty() const
Definition: vtkAMRBox.h:148
bool operator!=(const vtkAMRBox &other) const
Test if this box is NOT equal with the box instance on the rhs.
Definition: vtkAMRBox.h:171
void Invalidate()
Set the box to be invalid;.
Definition: vtkAMRBox.h:72
bool Contains(int i, int j, int k) const
Test to see if a given cell index is inside this box.