VTK  9.2.6
vtkPixelExtent.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkPixelExtenth.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
30 #ifndef vtkPixelExtent_h
31 #define vtkPixelExtent_h
32 
33 #include "vtkCommonDataModelModule.h" // for export
34 #include "vtkSystemIncludes.h" // for VTK's system header config
35 
36 #include <algorithm> // for inline impl
37 #include <climits> // for inline impl
38 #include <deque> // for inline impl
39 #include <iostream> // for inline impl
40 
41 class VTKCOMMONDATAMODEL_EXPORT vtkPixelExtent
42 {
43 public:
45 
46  template <typename T>
47  vtkPixelExtent(const T* ext);
48 
49  template <typename T>
50  vtkPixelExtent(T ilo, T ihi, T jlo, T jhi);
51 
52  template <typename T>
53  vtkPixelExtent(T width, T height)
54  {
55  this->SetData(T(0), width - T(1), T(0), height - T(1));
56  }
57 
58  vtkPixelExtent(const vtkPixelExtent& other);
59 
60  vtkPixelExtent& operator=(const vtkPixelExtent& other);
61 
65  int& operator[](int i) { return this->Data[i]; }
66  const int& operator[](int i) const { return this->Data[i]; }
67 
71  void SetData(const vtkPixelExtent& ext);
72 
73  template <typename T>
74  void SetData(const T* ext);
75 
76  template <typename T>
77  void SetData(T ilo, T ihi, T jlo, T jhi);
78  void Clear();
79 
83  int* GetData() { return this->Data; }
84  const int* GetData() const { return this->Data; }
85 
86  template <typename T>
87  void GetData(T data[4]) const;
88 
89  unsigned int* GetDataU() { return reinterpret_cast<unsigned int*>(this->Data); }
90 
91  const unsigned int* GetDataU() const { return reinterpret_cast<const unsigned int*>(this->Data); }
92 
94 
97  void GetStartIndex(int first[2]) const;
98  void GetStartIndex(int first[2], const int origin[2]) const;
99  void GetEndIndex(int last[2]) const;
101 
105  int Empty() const;
106 
110  bool operator==(const vtkPixelExtent& other) const;
111 
113 
116  int Contains(const vtkPixelExtent& other) const;
117  int Contains(int i, int j) const;
119 
123  int Disjoint(vtkPixelExtent other) const;
124 
128  template <typename T>
129  void Size(T nCells[2]) const;
130 
134  size_t Size() const;
135 
139  void operator&=(const vtkPixelExtent& other);
140 
144  void operator|=(const vtkPixelExtent& other);
145 
147 
150  void Grow(int n);
151  void Grow(int q, int n);
152  void GrowLow(int q, int n);
153  void GrowHigh(int q, int n);
155 
157 
160  void Shrink(int n);
161  void Shrink(int q, int n);
163 
167  void Shift();
168 
172  void Shift(const vtkPixelExtent& ext);
173 
177  void Shift(int* n);
178 
182  void Shift(int q, int n);
183 
190  vtkPixelExtent Split(int dir);
191 
193 
196  void CellToNode();
197  void NodeToCell();
199 
203  template <typename T>
204  static void Size(const vtkPixelExtent& ext, T nCells[2]);
205 
209  static size_t Size(const vtkPixelExtent& ext);
210 
216  static vtkPixelExtent Grow(const vtkPixelExtent& inputExt, int n);
217 
218  static vtkPixelExtent Grow(
219  const vtkPixelExtent& inputExt, const vtkPixelExtent& problemDomain, int n);
220 
221  static vtkPixelExtent GrowLow(const vtkPixelExtent& ext, int q, int n);
222 
223  static vtkPixelExtent GrowHigh(const vtkPixelExtent& ext, int q, int n);
224 
229  static vtkPixelExtent Shrink(
230  const vtkPixelExtent& inputExt, const vtkPixelExtent& problemDomain, int n);
231 
232  static vtkPixelExtent Shrink(const vtkPixelExtent& inputExt, int n);
233 
238  static vtkPixelExtent NodeToCell(const vtkPixelExtent& inputExt);
239 
244  static vtkPixelExtent CellToNode(const vtkPixelExtent& inputExt);
245 
247 
250  static void Shift(int* ij, int n);
251  static void Shift(int* ij, int* n);
253 
259  static void Split(int i, int j, const vtkPixelExtent& ext, std::deque<vtkPixelExtent>& newExts);
260 
267  static void Subtract(
268  const vtkPixelExtent& A, const vtkPixelExtent& B, std::deque<vtkPixelExtent>& C);
269 
275  static void Merge(std::deque<vtkPixelExtent>& exts);
276 
277 private:
278  int Data[4];
279 };
280 
284 VTKCOMMONDATAMODEL_EXPORT
285 std::ostream& operator<<(std::ostream& os, const vtkPixelExtent& ext);
286 
287 //-----------------------------------------------------------------------------
288 template <typename T>
289 void vtkPixelExtent::SetData(const T* ext)
290 {
291  Data[0] = static_cast<int>(ext[0]);
292  Data[1] = static_cast<int>(ext[1]);
293  Data[2] = static_cast<int>(ext[2]);
294  Data[3] = static_cast<int>(ext[3]);
295 }
296 
297 //-----------------------------------------------------------------------------
298 template <typename T>
299 void vtkPixelExtent::SetData(T ilo, T ihi, T jlo, T jhi)
300 {
301  T ext[4] = { ilo, ihi, jlo, jhi };
302  this->SetData(ext);
303 }
304 
305 //-----------------------------------------------------------------------------
306 inline void vtkPixelExtent::SetData(const vtkPixelExtent& other)
307 {
308  this->SetData(other.GetData());
309 }
310 
311 //-----------------------------------------------------------------------------
312 template <typename T>
314 {
315  data[0] = static_cast<T>(this->Data[0]);
316  data[1] = static_cast<T>(this->Data[1]);
317  data[2] = static_cast<T>(this->Data[2]);
318  data[3] = static_cast<T>(this->Data[3]);
319 }
320 
321 //-----------------------------------------------------------------------------
323 {
324  this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
325 }
326 
327 //-----------------------------------------------------------------------------
329 {
330  this->Clear();
331 }
332 
333 //-----------------------------------------------------------------------------
334 template <typename T>
336 {
337  this->SetData(ext);
338 }
339 
340 //-----------------------------------------------------------------------------
341 template <typename T>
342 vtkPixelExtent::vtkPixelExtent(T ilo, T ihi, T jlo, T jhi)
343 {
344  this->SetData(ilo, ihi, jlo, jhi);
345 }
346 
347 //-----------------------------------------------------------------------------
349 {
350  if (&other != this)
351  {
352  this->Data[0] = other.Data[0];
353  this->Data[1] = other.Data[1];
354  this->Data[2] = other.Data[2];
355  this->Data[3] = other.Data[3];
356  }
357  return *this;
358 }
359 
360 //-----------------------------------------------------------------------------
362 {
363  *this = other;
364 }
365 
366 //-----------------------------------------------------------------------------
367 template <typename T>
368 void vtkPixelExtent::Size(const vtkPixelExtent& ext, T nCells[2])
369 {
370  nCells[0] = ext[1] - ext[0] + 1;
371  nCells[1] = ext[3] - ext[2] + 1;
372 }
373 
374 //-----------------------------------------------------------------------------
375 inline size_t vtkPixelExtent::Size(const vtkPixelExtent& ext)
376 {
377  return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
378 }
379 
380 //-----------------------------------------------------------------------------
381 template <typename T>
382 void vtkPixelExtent::Size(T nCells[2]) const
383 {
384  vtkPixelExtent::Size(*this, nCells);
385 }
386 
387 //-----------------------------------------------------------------------------
388 inline size_t vtkPixelExtent::Size() const
389 {
390  return vtkPixelExtent::Size(*this);
391 }
392 
393 //-----------------------------------------------------------------------------
394 inline void vtkPixelExtent::GetStartIndex(int first[2]) const
395 {
396  first[0] = this->Data[0];
397  first[1] = this->Data[2];
398 }
399 
400 //-----------------------------------------------------------------------------
401 inline void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
402 {
403  first[0] = this->Data[0] - origin[0];
404  first[1] = this->Data[2] - origin[1];
405 }
406 
407 //-----------------------------------------------------------------------------
408 inline void vtkPixelExtent::GetEndIndex(int last[2]) const
409 {
410  last[0] = this->Data[1];
411  last[1] = this->Data[3];
412 }
413 
414 //-----------------------------------------------------------------------------
415 inline int vtkPixelExtent::Empty() const
416 {
417  if (this->Data[0] > this->Data[1] || this->Data[2] > this->Data[3])
418  {
419  return 1;
420  }
421  return 0;
422 }
423 
424 //-----------------------------------------------------------------------------
425 inline bool vtkPixelExtent::operator==(const vtkPixelExtent& other) const
426 {
427  if ((this->Data[0] == other.Data[0]) && (this->Data[1] == other.Data[1]) &&
428  (this->Data[2] == other.Data[2]) && (this->Data[3] == other.Data[3]))
429  {
430  return true;
431  }
432  return false;
433 }
434 
435 //-----------------------------------------------------------------------------
436 inline int vtkPixelExtent::Contains(const vtkPixelExtent& other) const
437 {
438  if ((this->Data[0] <= other.Data[0]) && (this->Data[1] >= other.Data[1]) &&
439  (this->Data[2] <= other.Data[2]) && (this->Data[3] >= other.Data[3]))
440  {
441  return 1;
442  }
443  return 0;
444 }
445 
446 //-----------------------------------------------------------------------------
447 inline int vtkPixelExtent::Contains(int i, int j) const
448 {
449  if ((this->Data[0] <= i) && (this->Data[1] >= i) && (this->Data[2] <= j) && (this->Data[3] >= j))
450  {
451  return 1;
452  }
453  return 0;
454 }
455 
456 //-----------------------------------------------------------------------------
458 {
459  if (this->Empty())
460  {
461  return;
462  }
463 
464  if (other.Empty())
465  {
466  this->Clear();
467  return;
468  }
469 
470  this->Data[0] = std::max(this->Data[0], other.Data[0]);
471  this->Data[1] = std::min(this->Data[1], other.Data[1]);
472  this->Data[2] = std::max(this->Data[2], other.Data[2]);
473  this->Data[3] = std::min(this->Data[3], other.Data[3]);
474 
475  if (this->Empty())
476  {
477  this->Clear();
478  }
479 }
480 
481 //-----------------------------------------------------------------------------
483 {
484  if (other.Empty())
485  {
486  return;
487  }
488 
489  if (this->Empty())
490  {
491  this->SetData(other.GetData());
492  return;
493  }
494 
495  this->Data[0] = std::min(this->Data[0], other.Data[0]);
496  this->Data[1] = std::max(this->Data[1], other.Data[1]);
497  this->Data[2] = std::min(this->Data[2], other.Data[2]);
498  this->Data[3] = std::max(this->Data[3], other.Data[3]);
499 }
500 
501 //-----------------------------------------------------------------------------
503 {
504  other &= *this;
505  return other.Empty();
506 }
507 
508 //-----------------------------------------------------------------------------
509 inline void vtkPixelExtent::Grow(int n)
510 {
511  this->Data[0] -= n;
512  this->Data[1] += n;
513  this->Data[2] -= n;
514  this->Data[3] += n;
515 }
516 
517 //-----------------------------------------------------------------------------
518 inline void vtkPixelExtent::Grow(int q, int n)
519 {
520  q *= 2;
521 
522  this->Data[q] -= n;
523  this->Data[q + 1] += n;
524 }
525 
526 //-----------------------------------------------------------------------------
527 inline void vtkPixelExtent::GrowLow(int q, int n)
528 {
529  this->Data[2 * q] -= n;
530 }
531 
532 //-----------------------------------------------------------------------------
533 inline void vtkPixelExtent::GrowHigh(int q, int n)
534 {
535  this->Data[2 * q + 1] += n;
536 }
537 
538 //-----------------------------------------------------------------------------
539 inline void vtkPixelExtent::Shrink(int n)
540 {
541  this->Data[0] += n;
542  this->Data[1] -= n;
543  this->Data[2] += n;
544  this->Data[3] -= n;
545 }
546 
547 //-----------------------------------------------------------------------------
548 inline void vtkPixelExtent::Shrink(int q, int n)
549 {
550  q *= 2;
551  this->Data[q] += n;
552  this->Data[q + 1] -= n;
553 }
554 
555 //-----------------------------------------------------------------------------
556 inline void vtkPixelExtent::Shift(int* n)
557 {
558  this->Data[0] += n[0];
559  this->Data[1] += n[0];
560  this->Data[2] += n[1];
561  this->Data[3] += n[1];
562 }
563 
564 //-----------------------------------------------------------------------------
565 inline void vtkPixelExtent::Shift(int q, int n)
566 {
567  q *= 2;
568  this->Data[q] += n;
569  this->Data[q + 1] += n;
570 }
571 
572 //-----------------------------------------------------------------------------
573 inline void vtkPixelExtent::Shift(const vtkPixelExtent& other)
574 {
575  for (int q = 0; q < 2; ++q)
576  {
577  int qq = q * 2;
578  int n = -other[qq];
579 
580  this->Data[qq] += n;
581  this->Data[qq + 1] += n;
582  }
583 }
584 
585 //-----------------------------------------------------------------------------
587 {
588  for (int q = 0; q < 2; ++q)
589  {
590  int qq = q * 2;
591  int n = -this->Data[qq];
592 
593  this->Data[qq] += n;
594  this->Data[qq + 1] += n;
595  }
596 }
597 
598 //-----------------------------------------------------------------------------
600 {
601  vtkPixelExtent half;
602 
603  int q = 2 * dir;
604  int l = this->Data[q + 1] - this->Data[q] + 1;
605  int s = l / 2;
606 
607  if (s)
608  {
609  s += this->Data[q];
610  half = *this;
611  half.Data[q] = s;
612  this->Data[q + 1] = s - 1;
613  }
614 
615  return half;
616 }
617 
618 //-----------------------------------------------------------------------------
620 {
621  ++this->Data[1];
622  ++this->Data[3];
623 }
624 
625 //-----------------------------------------------------------------------------
627 {
628  --this->Data[1];
629  --this->Data[3];
630 }
631 
632 //-----------------------------------------------------------------------------
633 inline bool operator<(const vtkPixelExtent& l, const vtkPixelExtent& r)
634 {
635  return l.Size() < r.Size();
636 }
637 
638 #endif
639 // VTK-HeaderTest-Exclude: vtkPixelExtent.h
void GetStartIndex(int first[2]) const
Get the start/end index.
int Disjoint(vtkPixelExtent other) const
Return non-zero if the extent is disjoint from the other.
const unsigned int * GetDataU() const
vtkPixelExtent(T width, T height)
int Contains(const vtkPixelExtent &other) const
Return non-zero if this extent contains the other.
void GrowLow(int q, int n)
Expand the extents by n.
int * GetData()
Direct access to internal data.
const int & operator[](int i) const
bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
bool operator==(const vtkPixelExtent &other) const
Test for equivalence.
vtkPixelExtent & operator=(const vtkPixelExtent &other)
vtkPixelExtent Split(int dir)
Divide the extent in half in the given direction.
bool VTKCOMMONDATAMODEL_EXPORT operator==(vtkEdgeBase e1, vtkEdgeBase e2)
void operator|=(const vtkPixelExtent &other)
In place union.
VTKCOMMONDATAMODEL_EXPORT std::ostream & operator<<(std::ostream &os, const vtkPixelExtent &ext)
Stream insertion operator for formatted output of pixel extents.
void CellToNode()
In-place conversion from cell based to node based extent, and vise-versa.
vtkSmartPointer< vtkDataArray > GetData(const Ioss::GroupingEntity *entity, const std::string &fieldname, Ioss::Transform *transform=nullptr, Cache *cache=nullptr, const std::string &cachekey=std::string())
Returns a VTK array for a given field (fieldname) on the chosen block (or set) entity.
void GrowHigh(int q, int n)
Expand the extents by n.
size_t Size() const
Get the total number.
void NodeToCell()
In-place conversion from cell based to node based extent, and vise-versa.
Representation of a cartesian pixel plane and common operations on it.
void SetData(const vtkPixelExtent &ext)
Set the extent.
void operator&=(const vtkPixelExtent &other)
In place intersection.
void GetEndIndex(int last[2]) const
Get the start/end index.
int Empty() const
Return true if empty.
void Size(T nCells[2]) const
Get the number in each direction.
void Shrink(int n)
Shrink the extent by n.
const int * GetData() const
void Shift()
Shifts by low corner of this, moving to the origin.
int & operator[](int i)
Element access.
void Grow(int n)
Expand the extents by n.
#define max(a, b)
unsigned int * GetDataU()