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  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  return *this;
389  }
390  this->SetData(other);
391  return *this;
392 }
393 
394 //-----------------------------------------------------------------------------
395 inline
397 {
398  *this = other;
399 }
400 
401 //-----------------------------------------------------------------------------
402 template<typename T>
403 void vtkPixelExtent::Size(const vtkPixelExtent &ext, T nCells[2])
404 {
405  nCells[0] = ext[1] - ext[0] + 1;
406  nCells[1] = ext[3] - ext[2] + 1;
407 }
408 
409 //-----------------------------------------------------------------------------
410 inline
412 {
413  return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
414 }
415 
416 //-----------------------------------------------------------------------------
417 template<typename T>
418 void vtkPixelExtent::Size(T nCells[2]) const
419 {
420  vtkPixelExtent::Size(*this, nCells);
421 }
422 
423 //-----------------------------------------------------------------------------
424 inline
425 size_t vtkPixelExtent::Size() const
426 {
427  return vtkPixelExtent::Size(*this);
428 }
429 
430 //-----------------------------------------------------------------------------
431 inline
432 void vtkPixelExtent::GetStartIndex(int first[2]) const
433 {
434  first[0] = this->Data[0];
435  first[1] = this->Data[2];
436 }
437 
438 //-----------------------------------------------------------------------------
439 inline
440 void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
441 {
442  first[0] = this->Data[0] - origin[0];
443  first[1] = this->Data[2] - origin[1];
444 }
445 
446 //-----------------------------------------------------------------------------
447 inline
448 void vtkPixelExtent::GetEndIndex(int last[2]) const
449 {
450  last[0] = this->Data[1];
451  last[1] = this->Data[3];
452 }
453 
454 //-----------------------------------------------------------------------------
455 inline
457 {
458  if ( this->Data[0] > this->Data[1]
459  || this->Data[2] > this->Data[3])
460  {
461  return 1;
462  }
463  return 0;
464 }
465 
466 //-----------------------------------------------------------------------------
467 inline
469 {
470  if ( (this->Data[0] == other.Data[0])
471  && (this->Data[1] == other.Data[1])
472  && (this->Data[2] == other.Data[2])
473  && (this->Data[3] == other.Data[3]) )
474  {
475  return 1;
476  }
477  return 0;
478 }
479 
480 //-----------------------------------------------------------------------------
481 inline
483 {
484  if ( (this->Data[0] <= other.Data[0])
485  && (this->Data[1] >= other.Data[1])
486  && (this->Data[2] <= other.Data[2])
487  && (this->Data[3] >= other.Data[3]) )
488  {
489  return 1;
490  }
491  return 0;
492 }
493 
494 //-----------------------------------------------------------------------------
495 inline
496 int vtkPixelExtent::Contains(int i, int j) const
497 {
498  if ( (this->Data[0] <= i)
499  && (this->Data[1] >= i)
500  && (this->Data[2] <= j)
501  && (this->Data[3] >= j) )
502  {
503  return 1;
504  }
505  return 0;
506 }
507 
508 
509 //-----------------------------------------------------------------------------
510 inline
512 {
513  if (this->Empty())
514  {
515  return;
516  }
517 
518  if (other.Empty())
519  {
520  this->Clear();
521  return;
522  }
523 
524  this->Data[0] = std::max(this->Data[0], other.Data[0]);
525  this->Data[1] = std::min(this->Data[1], other.Data[1]);
526  this->Data[2] = std::max(this->Data[2], other.Data[2]);
527  this->Data[3] = std::min(this->Data[3], other.Data[3]);
528 
529  if (this->Empty())
530  {
531  this->Clear();
532  }
533 }
534 
535 //-----------------------------------------------------------------------------
536 inline
538 {
539  if (other.Empty())
540  {
541  return;
542  }
543 
544  if (this->Empty())
545  {
546  this->SetData(other.GetData());
547  return;
548  }
549 
550  this->Data[0] = std::min(this->Data[0], other.Data[0]);
551  this->Data[1] = std::max(this->Data[1], other.Data[1]);
552  this->Data[2] = std::min(this->Data[2], other.Data[2]);
553  this->Data[3] = std::max(this->Data[3], other.Data[3]);
554 }
555 
556 //-----------------------------------------------------------------------------
557 inline
559 {
560  other &= *this;
561  return other.Empty();
562 }
563 
564 //-----------------------------------------------------------------------------
565 inline
567 {
568  this->Data[0] -= n;
569  this->Data[1] += n;
570  this->Data[2] -= n;
571  this->Data[3] += n;
572 }
573 
574 //-----------------------------------------------------------------------------
575 inline
576 void vtkPixelExtent::Grow(int q, int n)
577 {
578  q *= 2;
579 
580  this->Data[q ] -= n;
581  this->Data[q+1] += n;
582 }
583 
584 //-----------------------------------------------------------------------------
585 inline
586 void vtkPixelExtent::GrowLow(int q, int n)
587 {
588  this->Data[2*q] -= n;
589 }
590 
591 //-----------------------------------------------------------------------------
592 inline
593 void vtkPixelExtent::GrowHigh(int q, int n)
594 {
595  this->Data[2*q+1] += n;
596 }
597 
598 //-----------------------------------------------------------------------------
599 inline
601 {
602  this->Data[0] += n;
603  this->Data[1] -= n;
604  this->Data[2] += n;
605  this->Data[3] -= n;
606 }
607 
608 //-----------------------------------------------------------------------------
609 inline
610 void vtkPixelExtent::Shrink(int q, int n)
611 {
612  q *= 2;
613  this->Data[q ] += n;
614  this->Data[q+1] -= n;
615 }
616 
617 //-----------------------------------------------------------------------------
618 inline
620 {
621  this->Data[0] += n[0];
622  this->Data[1] += n[0];
623  this->Data[2] += n[1];
624  this->Data[3] += n[1];
625 }
626 
627 //-----------------------------------------------------------------------------
628 inline
629 void vtkPixelExtent::Shift(int q, int n)
630 {
631  q *= 2;
632  this->Data[q ] += n;
633  this->Data[q+1] += n;
634 }
635 
636 //-----------------------------------------------------------------------------
637 inline
639 {
640  for (int q=0; q<2; ++q)
641  {
642  int qq = q*2;
643  int n = -other[qq];
644 
645  this->Data[qq ] += n;
646  this->Data[qq+1] += n;
647  }
648 }
649 
650 //-----------------------------------------------------------------------------
651 inline
653 {
654  for (int q=0; q<2; ++q)
655  {
656  int qq = q*2;
657  int n =- this->Data[qq];
658 
659  this->Data[qq ] += n;
660  this->Data[qq+1] += n;
661  }
662 }
663 
664 //-----------------------------------------------------------------------------
665 inline
667 {
668  vtkPixelExtent half;
669 
670  int q = 2 * dir;
671  int l = this->Data[q+1] - this->Data[q] + 1;
672  int s = l/2;
673 
674  if (s)
675  {
676  s += this->Data[q];
677  half = *this;
678  half.Data[q] = s;
679  this->Data[q+1] = s - 1;
680  }
681 
682  return half;
683 }
684 
685 //-----------------------------------------------------------------------------
686 inline
688 {
689  ++this->Data[1];
690  ++this->Data[3];
691 }
692 
693 //-----------------------------------------------------------------------------
694 inline
696 {
697  --this->Data[1];
698  --this->Data[3];
699 }
700 
701 //-----------------------------------------------------------------------------
702 inline
703 bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
704 {
705  return l.Size() < r.Size();
706 }
707 
708 #endif
709 // 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 conatins 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()