VTK
dox/Common/DataModel/vtkPixelExtent.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkPixelExtenth.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00029 #ifndef __vtkPixelExtent_h
00030 #define __vtkPixelExtent_h
00031 
00032 #include "vtkSystemIncludes.h" // for VTK's system header config
00033 #include "vtkCommonDataModelModule.h" // for export
00034 
00035 #include <deque> // for inline impl
00036 #include <algorithm> // for inline impl
00037 #include <iostream> // for inline impl
00038 #include <climits> // for inline impl
00039 
00040 class VTKCOMMONDATAMODEL_EXPORT vtkPixelExtent
00041 {
00042 public:
00043   vtkPixelExtent();
00044 
00045   template<typename T>
00046   vtkPixelExtent(const T *ext);
00047 
00048   template<typename T>
00049   vtkPixelExtent(T ilo, T ihi, T jlo, T jhi);
00050 
00051   template<typename T>
00052   vtkPixelExtent(T width, T height)
00053     { this->SetData(T(0), width-T(1), T(0), height-T(1)); }
00054 
00055   vtkPixelExtent(const vtkPixelExtent &other);
00056 
00057   vtkPixelExtent &operator=(const vtkPixelExtent &other);
00058 
00060 
00061   int &operator[](int i){ return this->Data[i]; }
00062   const int &operator[](int i) const { return this->Data[i]; }
00064 
00066   void SetData(const vtkPixelExtent &ext);
00067 
00068   template<typename T>
00069   void SetData(const T *ext);
00070 
00071   template<typename T>
00072   void SetData(T ilo, T ihi, T jlo, T jhi);
00073   void Clear();
00074 
00076 
00077   int *GetData(){ return this->Data; }
00078   const int *GetData() const { return this->Data; }
00080 
00081   template<typename T>
00082   void GetData(T data[4]) const;
00083 
00084   unsigned int *GetDataU()
00085     { return reinterpret_cast<unsigned int*>(this->Data); }
00086 
00087   const unsigned int *GetDataU() const
00088     { return reinterpret_cast<const unsigned int*>(this->Data); }
00089 
00091 
00092   void GetStartIndex(int first[2]) const;
00093   void GetStartIndex(int first[2], const int origin[2]) const;
00094   void GetEndIndex(int last[2]) const;
00096 
00098   int Empty() const;
00099 
00101   int operator==(const vtkPixelExtent &other) const;
00102 
00104 
00105   int Contains(const vtkPixelExtent &other) const;
00106   int Contains(int i, int j) const;
00108 
00110   int Disjoint(vtkPixelExtent other) const;
00111 
00113 
00114   template<typename T>
00115   void Size(T nCells[2]) const;
00117 
00119   size_t Size() const;
00120 
00121 
00123   void operator&=(const vtkPixelExtent &other);
00124 
00126   void operator|=(const vtkPixelExtent &other);
00127 
00128 
00129 
00131 
00132   void Grow(int n);
00133   void Grow(int q, int n);
00134   void GrowLow(int q, int n);
00135   void GrowHigh(int q, int n);
00137 
00139 
00140   void Shrink(int n);
00141   void Shrink(int q, int n);
00143 
00145   void Shift();
00146 
00148   void Shift(const vtkPixelExtent &ext);
00149 
00151   void Shift(int *n);
00152 
00154   void Shift(int q, int n);
00155 
00159   vtkPixelExtent Split(int dir);
00160 
00161 
00162 
00164 
00166   void CellToNode();
00167   void NodeToCell();
00169 
00170 
00171 
00173 
00174   template<typename T>
00175   static
00176   void Size(const vtkPixelExtent &ext, T nCells[2]);
00178 
00180 
00181   static
00182   size_t Size(const vtkPixelExtent &ext);
00184 
00187   static vtkPixelExtent Grow(const vtkPixelExtent &inputExt, int n);
00188 
00189   static vtkPixelExtent Grow(
00190       const vtkPixelExtent &inputExt,
00191       const vtkPixelExtent &problemDomain,
00192       int n);
00193 
00194   static vtkPixelExtent GrowLow(
00195       const vtkPixelExtent &ext,
00196       int q,
00197       int n);
00198 
00199   static vtkPixelExtent GrowHigh(
00200       const vtkPixelExtent &ext,
00201       int q,
00202       int n);
00203 
00205 
00207   static vtkPixelExtent Shrink(
00208       const vtkPixelExtent &inputExt,
00209       const vtkPixelExtent &problemDomain,
00210       int n);
00212 
00213   static vtkPixelExtent Shrink(
00214       const vtkPixelExtent &inputExt,
00215       int n);
00216 
00219   static vtkPixelExtent NodeToCell(const vtkPixelExtent &inputExt);
00220 
00223   static vtkPixelExtent CellToNode(const vtkPixelExtent &inputExt);
00224 
00226 
00227   static void Shift(int *ij, int n);
00228   static void Shift(int *ij, int *n);
00230 
00231 
00233 
00235   static void Split(
00236         int i,
00237         int j,
00238         const vtkPixelExtent &ext,
00239         std::deque<vtkPixelExtent> &newExts);
00241 
00243 
00245   static void Subtract(
00246         const vtkPixelExtent &A,
00247         vtkPixelExtent B,
00248         std::deque<vtkPixelExtent> &newExts);
00250 
00254   static void Merge(std::deque<vtkPixelExtent> &exts);
00255 
00256 private:
00257   int Data[4];
00258 };
00259 
00261 
00262 VTKCOMMONDATAMODEL_EXPORT
00263 std::ostream &operator<<(std::ostream &os, const vtkPixelExtent &ext);
00265 
00266 //-----------------------------------------------------------------------------
00267 template<typename T>
00268 void vtkPixelExtent::SetData(const T *ext)
00269 {
00270   Data[0] = static_cast<int>(ext[0]);
00271   Data[1] = static_cast<int>(ext[1]);
00272   Data[2] = static_cast<int>(ext[2]);
00273   Data[3] = static_cast<int>(ext[3]);
00274 }
00275 
00276 //-----------------------------------------------------------------------------
00277 template<typename T>
00278 void vtkPixelExtent::SetData(T ilo, T ihi, T jlo, T jhi)
00279 {
00280   T ext[4] = {ilo, ihi, jlo, jhi};
00281   this->SetData(ext);
00282 }
00283 
00284 //-----------------------------------------------------------------------------
00285 inline
00286 void vtkPixelExtent::SetData(const vtkPixelExtent &other)
00287 {
00288   this->SetData(other.GetData());
00289 }
00290 
00291 //-----------------------------------------------------------------------------
00292 template<typename T>
00293 void vtkPixelExtent::GetData(T data[4]) const
00294 {
00295   data[0] = static_cast<T>(this->Data[0]);
00296   data[1] = static_cast<T>(this->Data[1]);
00297   data[2] = static_cast<T>(this->Data[2]);
00298   data[3] = static_cast<T>(this->Data[3]);
00299 }
00300 
00301 //-----------------------------------------------------------------------------
00302 inline
00303 void vtkPixelExtent::Clear()
00304 {
00305   this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
00306 }
00307 
00308 //-----------------------------------------------------------------------------
00309 inline
00310 vtkPixelExtent::vtkPixelExtent()
00311 {
00312   this->Clear();
00313 }
00314 
00315 //-----------------------------------------------------------------------------
00316 template<typename T>
00317 vtkPixelExtent::vtkPixelExtent(const T *ext)
00318 {
00319   this->SetData(ext);
00320 }
00321 
00322 //-----------------------------------------------------------------------------
00323 template<typename T>
00324 vtkPixelExtent::vtkPixelExtent(
00325       T ilo,
00326       T ihi,
00327       T jlo,
00328       T jhi)
00329 {
00330   this->SetData(ilo, ihi, jlo, jhi);
00331 }
00332 
00333 //-----------------------------------------------------------------------------
00334 inline
00335 vtkPixelExtent &vtkPixelExtent::operator=(const vtkPixelExtent &other)
00336 {
00337   if (&other == this)
00338     {
00339     return *this;
00340     }
00341   this->SetData(other);
00342   return *this;
00343 }
00344 
00345 //-----------------------------------------------------------------------------
00346 inline
00347 vtkPixelExtent::vtkPixelExtent(const vtkPixelExtent &other)
00348 {
00349   *this = other;
00350 }
00351 
00352 //-----------------------------------------------------------------------------
00353 template<typename T>
00354 void vtkPixelExtent::Size(const vtkPixelExtent &ext, T nCells[2])
00355 {
00356   nCells[0] = ext[1] - ext[0] + 1;
00357   nCells[1] = ext[3] - ext[2] + 1;
00358 }
00359 
00360 //-----------------------------------------------------------------------------
00361 inline
00362 size_t vtkPixelExtent::Size(const vtkPixelExtent &ext)
00363 {
00364   return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
00365 }
00366 
00367 //-----------------------------------------------------------------------------
00368 template<typename T>
00369 void vtkPixelExtent::Size(T nCells[2]) const
00370 {
00371   vtkPixelExtent::Size(*this, nCells);
00372 }
00373 
00374 //-----------------------------------------------------------------------------
00375 inline
00376 size_t vtkPixelExtent::Size() const
00377 {
00378   return vtkPixelExtent::Size(*this);
00379 }
00380 
00381 //-----------------------------------------------------------------------------
00382 inline
00383 void vtkPixelExtent::GetStartIndex(int first[2]) const
00384 {
00385   first[0] = this->Data[0];
00386   first[1] = this->Data[2];
00387 }
00388 
00389 //-----------------------------------------------------------------------------
00390 inline
00391 void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
00392 {
00393   first[0] = this->Data[0] - origin[0];
00394   first[1] = this->Data[2] - origin[1];
00395 }
00396 
00397 //-----------------------------------------------------------------------------
00398 inline
00399 void vtkPixelExtent::GetEndIndex(int last[2]) const
00400 {
00401   last[0] = this->Data[1];
00402   last[1] = this->Data[3];
00403 }
00404 
00405 //-----------------------------------------------------------------------------
00406 inline
00407 int vtkPixelExtent::Empty() const
00408 {
00409   if ( this->Data[0] > this->Data[1]
00410     || this->Data[2] > this->Data[3])
00411     {
00412     return 1;
00413     }
00414   return 0;
00415 }
00416 
00417 //-----------------------------------------------------------------------------
00418 inline
00419 int vtkPixelExtent::operator==(const vtkPixelExtent &other) const
00420 {
00421   if ( (this->Data[0] == other.Data[0])
00422     && (this->Data[1] == other.Data[1])
00423     && (this->Data[2] == other.Data[2])
00424     && (this->Data[3] == other.Data[3]) )
00425     {
00426     return 1;
00427     }
00428   return 0;
00429 }
00430 
00431 //-----------------------------------------------------------------------------
00432 inline
00433 int vtkPixelExtent::Contains(const vtkPixelExtent &other) const
00434 {
00435   if ( (this->Data[0] <= other.Data[0])
00436     && (this->Data[1] >= other.Data[1])
00437     && (this->Data[2] <= other.Data[2])
00438     && (this->Data[3] >= other.Data[3]) )
00439     {
00440     return 1;
00441     }
00442   return 0;
00443 }
00444 
00445 //-----------------------------------------------------------------------------
00446 inline
00447 int vtkPixelExtent::Contains(int i, int j) const
00448 {
00449   if ( (this->Data[0] <= i)
00450     && (this->Data[1] >= i)
00451     && (this->Data[2] <= j)
00452     && (this->Data[3] >= j) )
00453     {
00454     return 1;
00455     }
00456   return 0;
00457 }
00458 
00459 
00460 //-----------------------------------------------------------------------------
00461 inline
00462 void vtkPixelExtent::operator&=(const vtkPixelExtent &other)
00463 {
00464   if (this->Empty())
00465     {
00466     return;
00467     }
00468 
00469   if (other.Empty())
00470     {
00471     this->Clear();
00472     return;
00473     }
00474 
00475   this->Data[0] = std::max(this->Data[0], other.Data[0]);
00476   this->Data[1] = std::min(this->Data[1], other.Data[1]);
00477   this->Data[2] = std::max(this->Data[2], other.Data[2]);
00478   this->Data[3] = std::min(this->Data[3], other.Data[3]);
00479 
00480   if (this->Empty())
00481     {
00482     this->Clear();
00483     }
00484 }
00485 
00486 //-----------------------------------------------------------------------------
00487 inline
00488 void vtkPixelExtent::operator|=(const vtkPixelExtent &other)
00489 {
00490   if (other.Empty())
00491     {
00492     return;
00493     }
00494 
00495   if (this->Empty())
00496     {
00497     this->SetData(other.GetData());
00498     return;
00499     }
00500 
00501   this->Data[0] = std::min(this->Data[0], other.Data[0]);
00502   this->Data[1] = std::max(this->Data[1], other.Data[1]);
00503   this->Data[2] = std::min(this->Data[2], other.Data[2]);
00504   this->Data[3] = std::max(this->Data[3], other.Data[3]);
00505 }
00506 
00507 //-----------------------------------------------------------------------------
00508 inline
00509 int vtkPixelExtent::Disjoint(vtkPixelExtent other) const
00510 {
00511   other &= *this;
00512   return other.Empty();
00513 }
00514 
00515 //-----------------------------------------------------------------------------
00516 inline
00517 void vtkPixelExtent::Grow(int n)
00518 {
00519   this->Data[0] -= n;
00520   this->Data[1] += n;
00521   this->Data[2] -= n;
00522   this->Data[3] += n;
00523 }
00524 
00525 //-----------------------------------------------------------------------------
00526 inline
00527 void vtkPixelExtent::Grow(int q, int n)
00528 {
00529   q *= 2;
00530 
00531   this->Data[q  ] -= n;
00532   this->Data[q+1] += n;
00533 }
00534 
00535 //-----------------------------------------------------------------------------
00536 inline
00537 void vtkPixelExtent::GrowLow(int q, int n)
00538 {
00539   this->Data[2*q] -= n;
00540 }
00541 
00542 //-----------------------------------------------------------------------------
00543 inline
00544 void vtkPixelExtent::GrowHigh(int q, int n)
00545 {
00546   this->Data[2*q+1] += n;
00547 }
00548 
00549 //-----------------------------------------------------------------------------
00550 inline
00551 void vtkPixelExtent::Shrink(int n)
00552 {
00553   this->Data[0] += n;
00554   this->Data[1] -= n;
00555   this->Data[2] += n;
00556   this->Data[3] -= n;
00557 }
00558 
00559 //-----------------------------------------------------------------------------
00560 inline
00561 void vtkPixelExtent::Shrink(int q, int n)
00562 {
00563   q *= 2;
00564   this->Data[q  ] += n;
00565   this->Data[q+1] -= n;
00566 }
00567 
00568 //-----------------------------------------------------------------------------
00569 inline
00570 void vtkPixelExtent::Shift(int *n)
00571 {
00572   this->Data[0] += n[0];
00573   this->Data[1] += n[0];
00574   this->Data[2] += n[1];
00575   this->Data[3] += n[1];
00576 }
00577 
00578 //-----------------------------------------------------------------------------
00579 inline
00580 void vtkPixelExtent::Shift(int q, int n)
00581 {
00582   q *= 2;
00583   this->Data[q  ] += n;
00584   this->Data[q+1] += n;
00585 }
00586 
00587 //-----------------------------------------------------------------------------
00588 inline
00589 void vtkPixelExtent::Shift(const vtkPixelExtent &other)
00590 {
00591   for (int q=0; q<2; ++q)
00592     {
00593     int qq = q*2;
00594     int n = -other[qq];
00595 
00596     this->Data[qq  ] += n;
00597     this->Data[qq+1] += n;
00598     }
00599 }
00600 
00601 //-----------------------------------------------------------------------------
00602 inline
00603 void vtkPixelExtent::Shift()
00604 {
00605   for (int q=0; q<2; ++q)
00606     {
00607     int qq = q*2;
00608     int n =- this->Data[qq];
00609 
00610     this->Data[qq  ] += n;
00611     this->Data[qq+1] += n;
00612     }
00613 }
00614 
00615 //-----------------------------------------------------------------------------
00616 inline
00617 vtkPixelExtent vtkPixelExtent::Split(int dir)
00618 {
00619   vtkPixelExtent half;
00620 
00621   int q = 2 * dir;
00622   int l = this->Data[q+1] - this->Data[q] + 1;
00623   int s = l/2;
00624 
00625   if (s)
00626     {
00627     s += this->Data[q];
00628     half = *this;
00629     half.Data[q] = s;
00630     this->Data[q+1] = s - 1;
00631     }
00632 
00633   return half;
00634 }
00635 
00636 //-----------------------------------------------------------------------------
00637 inline
00638 void vtkPixelExtent::CellToNode()
00639 {
00640   ++this->Data[1];
00641   ++this->Data[3];
00642 }
00643 
00644 //-----------------------------------------------------------------------------
00645 inline
00646 void vtkPixelExtent::NodeToCell()
00647 {
00648   --this->Data[1];
00649   --this->Data[3];
00650 }
00651 
00652 //-----------------------------------------------------------------------------
00653 inline
00654 bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
00655 {
00656   return l.Size() < r.Size();
00657 }
00658 
00659 #endif
00660 // VTK-HeaderTest-Exclude: vtkPixelExtent.h