VTK  9.3.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 VTK_XYZ_GRID definition
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class VTKCOMMONDATAMODEL_EXPORT vtkAMRBox
29 {
30 public:
34  vtkAMRBox();
35 
39  vtkAMRBox(const vtkAMRBox& other);
40 
44  vtkAMRBox(int ilo, int jlo, int klo, int ihi, int jhi, int khi);
45 
50  vtkAMRBox(const double* origin, const int* dimensions, const double* spacing,
51  const double* globalOrigin, int gridDescription = VTK_XYZ_GRID);
52 
56  vtkAMRBox(const int lo[3], const int hi[3]);
57 
58  vtkAMRBox(const int dims[6]);
59 
63  vtkAMRBox& operator=(const vtkAMRBox& other);
64 
65  virtual ~vtkAMRBox() = default;
66 
68 
71  void Invalidate()
72  {
73  this->LoCorner[0] = this->LoCorner[1] = this->LoCorner[2] = 0;
74  this->HiCorner[0] = this->HiCorner[1] = this->HiCorner[2] = -2;
75  }
77 
81  bool EmptyDimension(int i) const { return HiCorner[i] <= LoCorner[i] - 1; }
82 
86  void SetDimensions(int ilo, int jlo, int klo, int ihi, int jhi, int khi, int desc = VTK_XYZ_GRID);
87 
91  void SetDimensions(const int lo[3], const int hi[3], int desc = VTK_XYZ_GRID);
92 
96  void SetDimensions(const int dims[6], int desc = VTK_XYZ_GRID);
97 
101  void GetDimensions(int lo[3], int hi[3]) const;
102 
106  void GetDimensions(int dims[6]) const;
107 
109 
112  vtkIdType GetNumberOfCells() const;
113  void GetNumberOfCells(int num[3]) const;
115 
117 
121  void GetNumberOfNodes(int ext[3]) const;
122  vtkIdType GetNumberOfNodes() const;
124 
130  int ComputeDimension() const;
131 
135  const int* GetLoCorner() const { return this->LoCorner; }
136  const int* GetHiCorner() const { return this->HiCorner; }
137 
143  void GetValidHiCorner(int hi[3]) const;
144 
145  bool Empty() const { return this->IsInvalid(); }
146 
150  bool IsInvalid() const
151  {
152  return ((this->HiCorner[0] < this->LoCorner[0] - 1) ||
153  (this->HiCorner[1] < this->LoCorner[1] - 1) || (this->HiCorner[2] < this->LoCorner[2] - 1));
154  }
155 
161  bool operator==(const vtkAMRBox& other) const;
162 
168  bool operator!=(const vtkAMRBox& other) const { return (!(*this == other)); }
169 
173  ostream& Print(ostream& os) const;
174 
176 
187  void Serialize(unsigned char*& buffer, vtkIdType& bytesize);
188  void Serialize(int* buffer) const;
190 
197  void Deserialize(unsigned char* buffer, const vtkIdType& bytesize);
198 
205  bool DoesBoxIntersectAlongDimension(const vtkAMRBox& other, int q) const;
206 
207  bool DoesIntersect(const vtkAMRBox& other) const;
208 
212  void Coarsen(int r);
213 
217  void Refine(int r);
218 
220 
223  void Grow(int byN);
224  void Shrink(int byN);
226 
228 
231  void Shift(int i, int j, int k);
232  void Shift(const int I[3]);
234 
240  bool Intersect(const vtkAMRBox& other);
241 
243 
246  bool Contains(int i, int j, int k) const;
247  bool Contains(const int I[3]) const;
249 
253  bool Contains(const vtkAMRBox&) const;
254 
260  void GetGhostVector(int r, int nghost[6]) const;
261 
266  void RemoveGhosts(int r);
267 
273  static vtkIdType GetBytesize() { return 6 * sizeof(int); }
274 
278  static int GetCellLinearIndex(const vtkAMRBox& box, int i, int j, int k, int imageDimension[3]);
279 
283  static void GetBounds(
284  const vtkAMRBox& box, const double origin[3], const double spacing[3], double bounds[6]);
285 
290  static void GetBoxOrigin(
291  const vtkAMRBox& box, const double X0[3], const double spacing[3], double x0[3]);
292 
297  static bool HasPoint(const vtkAMRBox& box, const double origin[3], const double spacing[3],
298  double x, double y, double z);
299 
303  static int ComputeStructuredCoordinates(const vtkAMRBox& box, const double dataOrigin[3],
304  const double h[3], const double x[3], int ijk[3], double pcoords[3]);
305 
306 protected:
310  void Initialize();
311 
318  bool IntersectBoxAlongDimension(const vtkAMRBox& other, int q);
319 
320 private:
321  int LoCorner[3]; // lo corner cell id.
322  int HiCorner[3]; // hi corner cell id.
323 
325 
330  void BuildAMRBox(int ilo, int jlo, int klo, int ihi, int jhi, int khi);
332 };
333 
334 //*****************************************************************************
336 
340 template <typename T>
341 void FillRegion(T* pArray, const vtkAMRBox& arrayRegion, const vtkAMRBox& destRegion, T fillValue)
342 {
343  // Convert regions to array index space. VTK arrays
344  // always start with 0,0,0.
345  int ofs[3];
346  ofs[0] = -arrayRegion.GetLoCorner()[0];
347  ofs[1] = -arrayRegion.GetLoCorner()[1];
348  ofs[2] = -arrayRegion.GetLoCorner()[2];
349  vtkAMRBox arrayDims(arrayRegion);
350  arrayDims.Shift(ofs);
351  vtkAMRBox destDims(destRegion);
352  destDims.Shift(ofs);
353  // Quick sanity check.
354  if (!arrayRegion.Contains(destRegion))
355  {
356  vtkGenericWarningMacro(<< "ERROR: Array must enclose the destination region. "
357  << "Aborting the fill.");
358  }
359  // Get the bounds of the indices we fill.
360  const int* destLo = destDims.GetLoCorner();
361  int destHi[3];
362  destDims.GetValidHiCorner(destHi);
363  // Get the array dimensions.
364  int arrayHi[3];
365  arrayDims.GetNumberOfCells(arrayHi);
366  // Fill.
367  for (int k = destLo[2]; k <= destHi[2]; ++k)
368  {
369  vtkIdType kOfs = k * arrayHi[0] * arrayHi[1];
370  for (int j = destLo[1]; j <= destHi[1]; ++j)
371  {
372  vtkIdType idx = kOfs + j * arrayHi[0] + destLo[0];
373  for (int i = destLo[0]; i <= destHi[0]; ++i)
374  {
375  pArray[idx] = fillValue;
376  ++idx;
377  }
378  }
379  }
381 }
382 
383 VTK_ABI_NAMESPACE_END
384 #endif
385 // VTK-HeaderTest-Exclude: vtkAMRBox.h
void GetBounds(T a, double bds[6])
Encloses a rectangular region of voxel like cells.
Definition: vtkAMRBox.h:28
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:341
void Print(const std::vector< T > &input, const std::string &name)
Print a vector with an associated name.
int vtkIdType
Definition: vtkType.h:315
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:150
vtkIdType GetNumberOfCells() const
Gets the number of cells enclosed by the box.
#define VTK_XYZ_GRID
const int * GetLoCorner() const
Get the low corner index.
Definition: vtkAMRBox.h:135
static vtkIdType GetBytesize()
Returns the number of bytes allocated by this instance.
Definition: vtkAMRBox.h:273
bool EmptyDimension(int i) const
Whether dimension i is empty, e.g.
Definition: vtkAMRBox.h:81
const int * GetHiCorner() const
Definition: vtkAMRBox.h:136
bool Empty() const
Definition: vtkAMRBox.h:145
bool operator!=(const vtkAMRBox &other) const
Test if this box is NOT equal with the box instance on the rhs.
Definition: vtkAMRBox.h:168
void Invalidate()
Set the box to be invalid;.
Definition: vtkAMRBox.h:71
bool Contains(int i, int j, int k) const
Test to see if a given cell index is inside this box.