VTK
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 =========================================================================*/
27 #ifndef vtkPixelExtent_h
28 #define vtkPixelExtent_h
29 
30 #include "vtkSystemIncludes.h" // for VTK's system header config
31 #include "vtkCommonDataModelModule.h" // for export
32 
33 #include <deque> // for inline impl
34 #include <algorithm> // for inline impl
35 #include <iostream> // for inline impl
36 #include <climits> // for inline impl
37 
38 class VTKCOMMONDATAMODEL_EXPORT vtkPixelExtent
39 {
40 public:
42 
43  template<typename T>
44  vtkPixelExtent(const T *ext);
45 
46  template<typename T>
47  vtkPixelExtent(T ilo, T ihi, T jlo, T jhi);
48 
49  template<typename T>
50  vtkPixelExtent(T width, T height)
51  { this->SetData(T(0), width-T(1), T(0), height-T(1)); }
52 
53  vtkPixelExtent(const vtkPixelExtent &other);
54 
55  vtkPixelExtent &operator=(const vtkPixelExtent &other);
56 
60  int &operator[](int i){ return this->Data[i]; }
61  const int &operator[](int i) const { return this->Data[i]; }
62 
66  void SetData(const vtkPixelExtent &ext);
67 
68  template<typename T>
69  void SetData(const T *ext);
70 
71  template<typename T>
72  void SetData(T ilo, T ihi, T jlo, T jhi);
73  void Clear();
74 
78  int *GetData(){ return this->Data; }
79  const int *GetData() const { return this->Data; }
80 
81  template<typename T>
82  void GetData(T data[4]) const;
83 
84  unsigned int *GetDataU()
85  { return reinterpret_cast<unsigned int*>(this->Data); }
86 
87  const unsigned int *GetDataU() const
88  { return reinterpret_cast<const unsigned int*>(this->Data); }
89 
91 
94  void GetStartIndex(int first[2]) const;
95  void GetStartIndex(int first[2], const int origin[2]) const;
96  void GetEndIndex(int last[2]) const;
98 
102  int Empty() const;
103 
107  bool operator==(const vtkPixelExtent &other) const;
108 
110 
113  int Contains(const vtkPixelExtent &other) const;
114  int Contains(int i, int j) const;
116 
120  int Disjoint(vtkPixelExtent other) const;
121 
125  template<typename T>
126  void Size(T nCells[2]) const;
127 
131  size_t Size() const;
132 
133 
137  void operator&=(const vtkPixelExtent &other);
138 
142  void operator|=(const vtkPixelExtent &other);
143 
144 
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 
192 
193 
195 
198  void CellToNode();
199  void NodeToCell();
201 
202 
203 
207  template<typename T>
208  static
209  void Size(const vtkPixelExtent &ext, T nCells[2]);
210 
214  static
215  size_t Size(const vtkPixelExtent &ext);
216 
222  static vtkPixelExtent Grow(const vtkPixelExtent &inputExt, int n);
223 
224  static vtkPixelExtent Grow(
225  const vtkPixelExtent &inputExt,
226  const vtkPixelExtent &problemDomain,
227  int n);
228 
229  static vtkPixelExtent GrowLow(
230  const vtkPixelExtent &ext,
231  int q,
232  int n);
233 
234  static vtkPixelExtent GrowHigh(
235  const vtkPixelExtent &ext,
236  int q,
237  int n);
238 
243  static vtkPixelExtent Shrink(
244  const vtkPixelExtent &inputExt,
245  const vtkPixelExtent &problemDomain,
246  int n);
247 
248  static vtkPixelExtent Shrink(
249  const vtkPixelExtent &inputExt,
250  int n);
251 
256  static vtkPixelExtent NodeToCell(const vtkPixelExtent &inputExt);
257 
262  static vtkPixelExtent CellToNode(const vtkPixelExtent &inputExt);
263 
265 
268  static void Shift(int *ij, int n);
269  static void Shift(int *ij, int *n);
271 
272 
278  static void Split(
279  int i,
280  int j,
281  const vtkPixelExtent &ext,
282  std::deque<vtkPixelExtent> &newExts);
283 
290  static void Subtract(
291  const vtkPixelExtent &A,
292  const vtkPixelExtent& B,
293  std::deque<vtkPixelExtent> &newExts);
294 
300  static void Merge(std::deque<vtkPixelExtent> &exts);
301 
302 private:
303  int Data[4];
304 };
305 
309 VTKCOMMONDATAMODEL_EXPORT
310 std::ostream &operator<<(std::ostream &os, const vtkPixelExtent &ext);
311 
312 //-----------------------------------------------------------------------------
313 template<typename T>
314 void vtkPixelExtent::SetData(const T *ext)
315 {
316  Data[0] = static_cast<int>(ext[0]);
317  Data[1] = static_cast<int>(ext[1]);
318  Data[2] = static_cast<int>(ext[2]);
319  Data[3] = static_cast<int>(ext[3]);
320 }
321 
322 //-----------------------------------------------------------------------------
323 template<typename T>
324 void vtkPixelExtent::SetData(T ilo, T ihi, T jlo, T jhi)
325 {
326  T ext[4] = {ilo, ihi, jlo, jhi};
327  this->SetData(ext);
328 }
329 
330 //-----------------------------------------------------------------------------
331 inline
333 {
334  this->SetData(other.GetData());
335 }
336 
337 //-----------------------------------------------------------------------------
338 template<typename T>
340 {
341  data[0] = static_cast<T>(this->Data[0]);
342  data[1] = static_cast<T>(this->Data[1]);
343  data[2] = static_cast<T>(this->Data[2]);
344  data[3] = static_cast<T>(this->Data[3]);
345 }
346 
347 //-----------------------------------------------------------------------------
348 inline
350 {
351  this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
352 }
353 
354 //-----------------------------------------------------------------------------
355 inline
357 {
358  this->Clear();
359 }
360 
361 //-----------------------------------------------------------------------------
362 template<typename T>
364 {
365  this->SetData(ext);
366 }
367 
368 //-----------------------------------------------------------------------------
369 template<typename T>
371  T ilo,
372  T ihi,
373  T jlo,
374  T jhi)
375 {
376  this->SetData(ilo, ihi, jlo, jhi);
377 }
378 
379 //-----------------------------------------------------------------------------
380 inline
382 {
383  if (&other != this)
384  {
385  this->Data[0] = other.Data[0];
386  this->Data[1] = other.Data[1];
387  this->Data[2] = other.Data[2];
388  this->Data[3] = other.Data[3];
389  }
390  return *this;
391 }
392 
393 //-----------------------------------------------------------------------------
394 inline
396 {
397  *this = other;
398 }
399 
400 //-----------------------------------------------------------------------------
401 template<typename T>
402 void vtkPixelExtent::Size(const vtkPixelExtent &ext, T nCells[2])
403 {
404  nCells[0] = ext[1] - ext[0] + 1;
405  nCells[1] = ext[3] - ext[2] + 1;
406 }
407 
408 //-----------------------------------------------------------------------------
409 inline
411 {
412  return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
413 }
414 
415 //-----------------------------------------------------------------------------
416 template<typename T>
417 void vtkPixelExtent::Size(T nCells[2]) const
418 {
419  vtkPixelExtent::Size(*this, nCells);
420 }
421 
422 //-----------------------------------------------------------------------------
423 inline
424 size_t vtkPixelExtent::Size() const
425 {
426  return vtkPixelExtent::Size(*this);
427 }
428 
429 //-----------------------------------------------------------------------------
430 inline
431 void vtkPixelExtent::GetStartIndex(int first[2]) const
432 {
433  first[0] = this->Data[0];
434  first[1] = this->Data[2];
435 }
436 
437 //-----------------------------------------------------------------------------
438 inline
439 void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
440 {
441  first[0] = this->Data[0] - origin[0];
442  first[1] = this->Data[2] - origin[1];
443 }
444 
445 //-----------------------------------------------------------------------------
446 inline
447 void vtkPixelExtent::GetEndIndex(int last[2]) const
448 {
449  last[0] = this->Data[1];
450  last[1] = this->Data[3];
451 }
452 
453 //-----------------------------------------------------------------------------
454 inline
456 {
457  if ( this->Data[0] > this->Data[1]
458  || this->Data[2] > this->Data[3])
459  {
460  return 1;
461  }
462  return 0;
463 }
464 
465 //-----------------------------------------------------------------------------
466 inline
468 {
469  if ( (this->Data[0] == other.Data[0])
470  && (this->Data[1] == other.Data[1])
471  && (this->Data[2] == other.Data[2])
472  && (this->Data[3] == other.Data[3]) )
473  {
474  return 1;
475  }
476  return 0;
477 }
478 
479 //-----------------------------------------------------------------------------
480 inline
482 {
483  if ( (this->Data[0] <= other.Data[0])
484  && (this->Data[1] >= other.Data[1])
485  && (this->Data[2] <= other.Data[2])
486  && (this->Data[3] >= other.Data[3]) )
487  {
488  return 1;
489  }
490  return 0;
491 }
492 
493 //-----------------------------------------------------------------------------
494 inline
495 int vtkPixelExtent::Contains(int i, int j) const
496 {
497  if ( (this->Data[0] <= i)
498  && (this->Data[1] >= i)
499  && (this->Data[2] <= j)
500  && (this->Data[3] >= j) )
501  {
502  return 1;
503  }
504  return 0;
505 }
506 
507 
508 //-----------------------------------------------------------------------------
509 inline
511 {
512  if (this->Empty())
513  {
514  return;
515  }
516 
517  if (other.Empty())
518  {
519  this->Clear();
520  return;
521  }
522 
523  this->Data[0] = std::max(this->Data[0], other.Data[0]);
524  this->Data[1] = std::min(this->Data[1], other.Data[1]);
525  this->Data[2] = std::max(this->Data[2], other.Data[2]);
526  this->Data[3] = std::min(this->Data[3], other.Data[3]);
527 
528  if (this->Empty())
529  {
530  this->Clear();
531  }
532 }
533 
534 //-----------------------------------------------------------------------------
535 inline
537 {
538  if (other.Empty())
539  {
540  return;
541  }
542 
543  if (this->Empty())
544  {
545  this->SetData(other.GetData());
546  return;
547  }
548 
549  this->Data[0] = std::min(this->Data[0], other.Data[0]);
550  this->Data[1] = std::max(this->Data[1], other.Data[1]);
551  this->Data[2] = std::min(this->Data[2], other.Data[2]);
552  this->Data[3] = std::max(this->Data[3], other.Data[3]);
553 }
554 
555 //-----------------------------------------------------------------------------
556 inline
558 {
559  other &= *this;
560  return other.Empty();
561 }
562 
563 //-----------------------------------------------------------------------------
564 inline
566 {
567  this->Data[0] -= n;
568  this->Data[1] += n;
569  this->Data[2] -= n;
570  this->Data[3] += n;
571 }
572 
573 //-----------------------------------------------------------------------------
574 inline
575 void vtkPixelExtent::Grow(int q, int n)
576 {
577  q *= 2;
578 
579  this->Data[q ] -= n;
580  this->Data[q+1] += n;
581 }
582 
583 //-----------------------------------------------------------------------------
584 inline
585 void vtkPixelExtent::GrowLow(int q, int n)
586 {
587  this->Data[2*q] -= n;
588 }
589 
590 //-----------------------------------------------------------------------------
591 inline
592 void vtkPixelExtent::GrowHigh(int q, int n)
593 {
594  this->Data[2*q+1] += n;
595 }
596 
597 //-----------------------------------------------------------------------------
598 inline
600 {
601  this->Data[0] += n;
602  this->Data[1] -= n;
603  this->Data[2] += n;
604  this->Data[3] -= n;
605 }
606 
607 //-----------------------------------------------------------------------------
608 inline
609 void vtkPixelExtent::Shrink(int q, int n)
610 {
611  q *= 2;
612  this->Data[q ] += n;
613  this->Data[q+1] -= n;
614 }
615 
616 //-----------------------------------------------------------------------------
617 inline
619 {
620  this->Data[0] += n[0];
621  this->Data[1] += n[0];
622  this->Data[2] += n[1];
623  this->Data[3] += n[1];
624 }
625 
626 //-----------------------------------------------------------------------------
627 inline
628 void vtkPixelExtent::Shift(int q, int n)
629 {
630  q *= 2;
631  this->Data[q ] += n;
632  this->Data[q+1] += n;
633 }
634 
635 //-----------------------------------------------------------------------------
636 inline
638 {
639  for (int q=0; q<2; ++q)
640  {
641  int qq = q*2;
642  int n = -other[qq];
643 
644  this->Data[qq ] += n;
645  this->Data[qq+1] += n;
646  }
647 }
648 
649 //-----------------------------------------------------------------------------
650 inline
652 {
653  for (int q=0; q<2; ++q)
654  {
655  int qq = q*2;
656  int n =- this->Data[qq];
657 
658  this->Data[qq ] += n;
659  this->Data[qq+1] += n;
660  }
661 }
662 
663 //-----------------------------------------------------------------------------
664 inline
666 {
667  vtkPixelExtent half;
668 
669  int q = 2 * dir;
670  int l = this->Data[q+1] - this->Data[q] + 1;
671  int s = l/2;
672 
673  if (s)
674  {
675  s += this->Data[q];
676  half = *this;
677  half.Data[q] = s;
678  this->Data[q+1] = s - 1;
679  }
680 
681  return half;
682 }
683 
684 //-----------------------------------------------------------------------------
685 inline
687 {
688  ++this->Data[1];
689  ++this->Data[3];
690 }
691 
692 //-----------------------------------------------------------------------------
693 inline
695 {
696  --this->Data[1];
697  --this->Data[3];
698 }
699 
700 //-----------------------------------------------------------------------------
701 inline
702 bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
703 {
704  return l.Size() < r.Size();
705 }
706 
707 #endif
708 // VTK-HeaderTest-Exclude: vtkPixelExtent.h
const int * GetData() const
vtkPixelExtent(T width, T height)
void GrowLow(int q, int n)
Expand the extents by n.
void GetEndIndex(int last[2]) const
Get the start/end index.
int * GetData()
Direct access to internal data.
void Size(T nCells[2]) const
Get the number in each direction.
size_t Size() const
Get the total number.
bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
vtkPixelExtent & operator=(const vtkPixelExtent &other)
int Contains(const vtkPixelExtent &other) const
Return non-zero if this extent contains the other.
const int & operator[](int i) const
vtkPixelExtent Split(int dir)
Divide the extent in half in the given direction.
const unsigned int * GetDataU() const
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.
int Disjoint(vtkPixelExtent other) const
Return non-zero if the extent is disjoint from the other.
void CellToNode()
In-place conversion from cell based to node based extent, and vise-versa.
void GrowHigh(int q, int n)
Expand the extents by n.
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.
bool operator==(const vtkPixelExtent &other) const
Test for equivalence.
void SetData(const vtkPixelExtent &ext)
Set the extent.
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
int Empty() const
Return true if empty.
void Shrink(int n)
Shrink the extent by n.
void GetStartIndex(int first[2]) const
Get the start/end index.
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.
void operator &=(const vtkPixelExtent &other)
In place intersection.
#define max(a, b)
unsigned int * GetDataU()