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 =========================================================================*/
30 #ifndef vtkPixelExtent_h
31 #define vtkPixelExtent_h
32 
33 #include "vtkSystemIncludes.h" // for VTK's system header config
34 #include "vtkCommonDataModelModule.h" // for export
35 
36 #include <deque> // for inline impl
37 #include <algorithm> // for inline impl
38 #include <iostream> // for inline impl
39 #include <climits> // 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  { this->SetData(T(0), width-T(1), T(0), height-T(1)); }
55 
56  vtkPixelExtent(const vtkPixelExtent &other);
57 
58  vtkPixelExtent &operator=(const vtkPixelExtent &other);
59 
63  int &operator[](int i){ return this->Data[i]; }
64  const int &operator[](int i) const { return this->Data[i]; }
65 
69  void SetData(const vtkPixelExtent &ext);
70 
71  template<typename T>
72  void SetData(const T *ext);
73 
74  template<typename T>
75  void SetData(T ilo, T ihi, T jlo, T jhi);
76  void Clear();
77 
81  int *GetData(){ return this->Data; }
82  const int *GetData() const { return this->Data; }
83 
84  template<typename T>
85  void GetData(T data[4]) const;
86 
87  unsigned int *GetDataU()
88  { return reinterpret_cast<unsigned int*>(this->Data); }
89 
90  const unsigned int *GetDataU() const
91  { 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 
136 
140  void operator&=(const vtkPixelExtent &other);
141 
145  void operator|=(const vtkPixelExtent &other);
146 
147 
148 
150 
153  void Grow(int n);
154  void Grow(int q, int n);
155  void GrowLow(int q, int n);
156  void GrowHigh(int q, int n);
158 
160 
163  void Shrink(int n);
164  void Shrink(int q, int n);
166 
170  void Shift();
171 
175  void Shift(const vtkPixelExtent &ext);
176 
180  void Shift(int *n);
181 
185  void Shift(int q, int n);
186 
193  vtkPixelExtent Split(int dir);
194 
195 
196 
198 
201  void CellToNode();
202  void NodeToCell();
204 
205 
206 
210  template<typename T>
211  static
212  void Size(const vtkPixelExtent &ext, T nCells[2]);
213 
217  static
218  size_t Size(const vtkPixelExtent &ext);
219 
225  static vtkPixelExtent Grow(const vtkPixelExtent &inputExt, int n);
226 
227  static vtkPixelExtent Grow(
228  const vtkPixelExtent &inputExt,
229  const vtkPixelExtent &problemDomain,
230  int n);
231 
232  static vtkPixelExtent GrowLow(
233  const vtkPixelExtent &ext,
234  int q,
235  int n);
236 
237  static vtkPixelExtent GrowHigh(
238  const vtkPixelExtent &ext,
239  int q,
240  int n);
241 
246  static vtkPixelExtent Shrink(
247  const vtkPixelExtent &inputExt,
248  const vtkPixelExtent &problemDomain,
249  int n);
250 
251  static vtkPixelExtent Shrink(
252  const vtkPixelExtent &inputExt,
253  int n);
254 
259  static vtkPixelExtent NodeToCell(const vtkPixelExtent &inputExt);
260 
265  static vtkPixelExtent CellToNode(const vtkPixelExtent &inputExt);
266 
268 
271  static void Shift(int *ij, int n);
272  static void Shift(int *ij, int *n);
274 
275 
281  static void Split(
282  int i,
283  int j,
284  const vtkPixelExtent &ext,
285  std::deque<vtkPixelExtent> &newExts);
286 
293  static void Subtract(
294  const vtkPixelExtent &A,
295  const vtkPixelExtent& B,
296  std::deque<vtkPixelExtent> &newExts);
297 
303  static void Merge(std::deque<vtkPixelExtent> &exts);
304 
305 private:
306  int Data[4];
307 };
308 
312 VTKCOMMONDATAMODEL_EXPORT
313 std::ostream &operator<<(std::ostream &os, const vtkPixelExtent &ext);
314 
315 //-----------------------------------------------------------------------------
316 template<typename T>
317 void vtkPixelExtent::SetData(const T *ext)
318 {
319  Data[0] = static_cast<int>(ext[0]);
320  Data[1] = static_cast<int>(ext[1]);
321  Data[2] = static_cast<int>(ext[2]);
322  Data[3] = static_cast<int>(ext[3]);
323 }
324 
325 //-----------------------------------------------------------------------------
326 template<typename T>
327 void vtkPixelExtent::SetData(T ilo, T ihi, T jlo, T jhi)
328 {
329  T ext[4] = {ilo, ihi, jlo, jhi};
330  this->SetData(ext);
331 }
332 
333 //-----------------------------------------------------------------------------
334 inline
336 {
337  this->SetData(other.GetData());
338 }
339 
340 //-----------------------------------------------------------------------------
341 template<typename T>
343 {
344  data[0] = static_cast<T>(this->Data[0]);
345  data[1] = static_cast<T>(this->Data[1]);
346  data[2] = static_cast<T>(this->Data[2]);
347  data[3] = static_cast<T>(this->Data[3]);
348 }
349 
350 //-----------------------------------------------------------------------------
351 inline
353 {
354  this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
355 }
356 
357 //-----------------------------------------------------------------------------
358 inline
360 {
361  this->Clear();
362 }
363 
364 //-----------------------------------------------------------------------------
365 template<typename T>
367 {
368  this->SetData(ext);
369 }
370 
371 //-----------------------------------------------------------------------------
372 template<typename T>
374  T ilo,
375  T ihi,
376  T jlo,
377  T jhi)
378 {
379  this->SetData(ilo, ihi, jlo, jhi);
380 }
381 
382 //-----------------------------------------------------------------------------
383 inline
385 {
386  if (&other != this)
387  {
388  this->Data[0] = other.Data[0];
389  this->Data[1] = other.Data[1];
390  this->Data[2] = other.Data[2];
391  this->Data[3] = other.Data[3];
392  }
393  return *this;
394 }
395 
396 //-----------------------------------------------------------------------------
397 inline
399 {
400  *this = other;
401 }
402 
403 //-----------------------------------------------------------------------------
404 template<typename T>
405 void vtkPixelExtent::Size(const vtkPixelExtent &ext, T nCells[2])
406 {
407  nCells[0] = ext[1] - ext[0] + 1;
408  nCells[1] = ext[3] - ext[2] + 1;
409 }
410 
411 //-----------------------------------------------------------------------------
412 inline
414 {
415  return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
416 }
417 
418 //-----------------------------------------------------------------------------
419 template<typename T>
420 void vtkPixelExtent::Size(T nCells[2]) const
421 {
422  vtkPixelExtent::Size(*this, nCells);
423 }
424 
425 //-----------------------------------------------------------------------------
426 inline
427 size_t vtkPixelExtent::Size() const
428 {
429  return vtkPixelExtent::Size(*this);
430 }
431 
432 //-----------------------------------------------------------------------------
433 inline
434 void vtkPixelExtent::GetStartIndex(int first[2]) const
435 {
436  first[0] = this->Data[0];
437  first[1] = this->Data[2];
438 }
439 
440 //-----------------------------------------------------------------------------
441 inline
442 void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
443 {
444  first[0] = this->Data[0] - origin[0];
445  first[1] = this->Data[2] - origin[1];
446 }
447 
448 //-----------------------------------------------------------------------------
449 inline
450 void vtkPixelExtent::GetEndIndex(int last[2]) const
451 {
452  last[0] = this->Data[1];
453  last[1] = this->Data[3];
454 }
455 
456 //-----------------------------------------------------------------------------
457 inline
459 {
460  if ( this->Data[0] > this->Data[1]
461  || this->Data[2] > this->Data[3])
462  {
463  return 1;
464  }
465  return 0;
466 }
467 
468 //-----------------------------------------------------------------------------
469 inline
471 {
472  if ( (this->Data[0] == other.Data[0])
473  && (this->Data[1] == other.Data[1])
474  && (this->Data[2] == other.Data[2])
475  && (this->Data[3] == other.Data[3]) )
476  {
477  return 1;
478  }
479  return 0;
480 }
481 
482 //-----------------------------------------------------------------------------
483 inline
485 {
486  if ( (this->Data[0] <= other.Data[0])
487  && (this->Data[1] >= other.Data[1])
488  && (this->Data[2] <= other.Data[2])
489  && (this->Data[3] >= other.Data[3]) )
490  {
491  return 1;
492  }
493  return 0;
494 }
495 
496 //-----------------------------------------------------------------------------
497 inline
498 int vtkPixelExtent::Contains(int i, int j) const
499 {
500  if ( (this->Data[0] <= i)
501  && (this->Data[1] >= i)
502  && (this->Data[2] <= j)
503  && (this->Data[3] >= j) )
504  {
505  return 1;
506  }
507  return 0;
508 }
509 
510 
511 //-----------------------------------------------------------------------------
512 inline
514 {
515  if (this->Empty())
516  {
517  return;
518  }
519 
520  if (other.Empty())
521  {
522  this->Clear();
523  return;
524  }
525 
526  this->Data[0] = std::max(this->Data[0], other.Data[0]);
527  this->Data[1] = std::min(this->Data[1], other.Data[1]);
528  this->Data[2] = std::max(this->Data[2], other.Data[2]);
529  this->Data[3] = std::min(this->Data[3], other.Data[3]);
530 
531  if (this->Empty())
532  {
533  this->Clear();
534  }
535 }
536 
537 //-----------------------------------------------------------------------------
538 inline
540 {
541  if (other.Empty())
542  {
543  return;
544  }
545 
546  if (this->Empty())
547  {
548  this->SetData(other.GetData());
549  return;
550  }
551 
552  this->Data[0] = std::min(this->Data[0], other.Data[0]);
553  this->Data[1] = std::max(this->Data[1], other.Data[1]);
554  this->Data[2] = std::min(this->Data[2], other.Data[2]);
555  this->Data[3] = std::max(this->Data[3], other.Data[3]);
556 }
557 
558 //-----------------------------------------------------------------------------
559 inline
561 {
562  other &= *this;
563  return other.Empty();
564 }
565 
566 //-----------------------------------------------------------------------------
567 inline
569 {
570  this->Data[0] -= n;
571  this->Data[1] += n;
572  this->Data[2] -= n;
573  this->Data[3] += n;
574 }
575 
576 //-----------------------------------------------------------------------------
577 inline
578 void vtkPixelExtent::Grow(int q, int n)
579 {
580  q *= 2;
581 
582  this->Data[q ] -= n;
583  this->Data[q+1] += n;
584 }
585 
586 //-----------------------------------------------------------------------------
587 inline
588 void vtkPixelExtent::GrowLow(int q, int n)
589 {
590  this->Data[2*q] -= n;
591 }
592 
593 //-----------------------------------------------------------------------------
594 inline
595 void vtkPixelExtent::GrowHigh(int q, int n)
596 {
597  this->Data[2*q+1] += n;
598 }
599 
600 //-----------------------------------------------------------------------------
601 inline
603 {
604  this->Data[0] += n;
605  this->Data[1] -= n;
606  this->Data[2] += n;
607  this->Data[3] -= n;
608 }
609 
610 //-----------------------------------------------------------------------------
611 inline
612 void vtkPixelExtent::Shrink(int q, int n)
613 {
614  q *= 2;
615  this->Data[q ] += n;
616  this->Data[q+1] -= n;
617 }
618 
619 //-----------------------------------------------------------------------------
620 inline
622 {
623  this->Data[0] += n[0];
624  this->Data[1] += n[0];
625  this->Data[2] += n[1];
626  this->Data[3] += n[1];
627 }
628 
629 //-----------------------------------------------------------------------------
630 inline
631 void vtkPixelExtent::Shift(int q, int n)
632 {
633  q *= 2;
634  this->Data[q ] += n;
635  this->Data[q+1] += n;
636 }
637 
638 //-----------------------------------------------------------------------------
639 inline
641 {
642  for (int q=0; q<2; ++q)
643  {
644  int qq = q*2;
645  int n = -other[qq];
646 
647  this->Data[qq ] += n;
648  this->Data[qq+1] += n;
649  }
650 }
651 
652 //-----------------------------------------------------------------------------
653 inline
655 {
656  for (int q=0; q<2; ++q)
657  {
658  int qq = q*2;
659  int n =- this->Data[qq];
660 
661  this->Data[qq ] += n;
662  this->Data[qq+1] += n;
663  }
664 }
665 
666 //-----------------------------------------------------------------------------
667 inline
669 {
670  vtkPixelExtent half;
671 
672  int q = 2 * dir;
673  int l = this->Data[q+1] - this->Data[q] + 1;
674  int s = l/2;
675 
676  if (s)
677  {
678  s += this->Data[q];
679  half = *this;
680  half.Data[q] = s;
681  this->Data[q+1] = s - 1;
682  }
683 
684  return half;
685 }
686 
687 //-----------------------------------------------------------------------------
688 inline
690 {
691  ++this->Data[1];
692  ++this->Data[3];
693 }
694 
695 //-----------------------------------------------------------------------------
696 inline
698 {
699  --this->Data[1];
700  --this->Data[3];
701 }
702 
703 //-----------------------------------------------------------------------------
704 inline
705 bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
706 {
707  return l.Size() < r.Size();
708 }
709 
710 #endif
711 // 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.
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.
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.
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
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()