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 =========================================================================*/
29 #ifndef vtkPixelExtent_h
30 #define vtkPixelExtent_h
31 
32 #include "vtkSystemIncludes.h" // for VTK's system header config
33 #include "vtkCommonDataModelModule.h" // for export
34 
35 #include <deque> // for inline impl
36 #include <algorithm> // for inline impl
37 #include <iostream> // for inline impl
38 #include <climits> // for inline impl
39 
41 {
42 public:
44 
45  template<typename T>
46  vtkPixelExtent(const T *ext);
47 
48  template<typename T>
49  vtkPixelExtent(T ilo, T ihi, T jlo, T jhi);
50 
51  template<typename T>
52  vtkPixelExtent(T width, T height)
53  { this->SetData(T(0), width-T(1), T(0), height-T(1)); }
54 
55  vtkPixelExtent(const vtkPixelExtent &other);
56 
57  vtkPixelExtent &operator=(const vtkPixelExtent &other);
58 
60 
61  int &operator[](int i){ return this->Data[i]; }
62  const int &operator[](int i) const { return this->Data[i]; }
64 
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 
76 
77  int *GetData(){ return this->Data; }
78  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 
92  void GetStartIndex(int first[2]) const;
93  void GetStartIndex(int first[2], const int origin[2]) const;
94  void GetEndIndex(int last[2]) const;
96 
98  int Empty() const;
99 
101  int operator==(const vtkPixelExtent &other) const;
102 
104 
105  int Contains(const vtkPixelExtent &other) const;
106  int Contains(int i, int j) const;
108 
110  int Disjoint(vtkPixelExtent other) const;
111 
113 
114  template<typename T>
115  void Size(T nCells[2]) const;
117 
119  size_t Size() const;
120 
121 
123  void operator&=(const vtkPixelExtent &other);
124 
126  void operator|=(const vtkPixelExtent &other);
127 
128 
129 
131 
132  void Grow(int n);
133  void Grow(int q, int n);
134  void GrowLow(int q, int n);
135  void GrowHigh(int q, int n);
137 
139 
140  void Shrink(int n);
141  void Shrink(int q, int n);
143 
145  void Shift();
146 
148  void Shift(const vtkPixelExtent &ext);
149 
151  void Shift(int *n);
152 
154  void Shift(int q, int n);
155 
159  vtkPixelExtent Split(int dir);
160 
161 
162 
164 
166  void CellToNode();
167  void NodeToCell();
169 
170 
171 
173 
174  template<typename T>
175  static
176  void Size(const vtkPixelExtent &ext, T nCells[2]);
178 
180 
181  static
182  size_t Size(const vtkPixelExtent &ext);
184 
187  static vtkPixelExtent Grow(const vtkPixelExtent &inputExt, int n);
188 
189  static vtkPixelExtent Grow(
190  const vtkPixelExtent &inputExt,
191  const vtkPixelExtent &problemDomain,
192  int n);
193 
194  static vtkPixelExtent GrowLow(
195  const vtkPixelExtent &ext,
196  int q,
197  int n);
198 
199  static vtkPixelExtent GrowHigh(
200  const vtkPixelExtent &ext,
201  int q,
202  int n);
203 
205 
207  static vtkPixelExtent Shrink(
208  const vtkPixelExtent &inputExt,
209  const vtkPixelExtent &problemDomain,
210  int n);
212 
213  static vtkPixelExtent Shrink(
214  const vtkPixelExtent &inputExt,
215  int n);
216 
219  static vtkPixelExtent NodeToCell(const vtkPixelExtent &inputExt);
220 
223  static vtkPixelExtent CellToNode(const vtkPixelExtent &inputExt);
224 
226 
227  static void Shift(int *ij, int n);
228  static void Shift(int *ij, int *n);
230 
231 
233 
235  static void Split(
236  int i,
237  int j,
238  const vtkPixelExtent &ext,
239  std::deque<vtkPixelExtent> &newExts);
241 
243 
245  static void Subtract(
246  const vtkPixelExtent &A,
247  vtkPixelExtent B,
248  std::deque<vtkPixelExtent> &newExts);
250 
254  static void Merge(std::deque<vtkPixelExtent> &exts);
255 
256 private:
257  int Data[4];
258 };
259 
261 
263 std::ostream &operator<<(std::ostream &os, const vtkPixelExtent &ext);
265 
266 //-----------------------------------------------------------------------------
267 template<typename T>
268 void vtkPixelExtent::SetData(const T *ext)
269 {
270  Data[0] = static_cast<int>(ext[0]);
271  Data[1] = static_cast<int>(ext[1]);
272  Data[2] = static_cast<int>(ext[2]);
273  Data[3] = static_cast<int>(ext[3]);
274 }
275 
276 //-----------------------------------------------------------------------------
277 template<typename T>
278 void vtkPixelExtent::SetData(T ilo, T ihi, T jlo, T jhi)
279 {
280  T ext[4] = {ilo, ihi, jlo, jhi};
281  this->SetData(ext);
282 }
283 
284 //-----------------------------------------------------------------------------
285 inline
287 {
288  this->SetData(other.GetData());
289 }
290 
291 //-----------------------------------------------------------------------------
292 template<typename T>
294 {
295  data[0] = static_cast<T>(this->Data[0]);
296  data[1] = static_cast<T>(this->Data[1]);
297  data[2] = static_cast<T>(this->Data[2]);
298  data[3] = static_cast<T>(this->Data[3]);
299 }
300 
301 //-----------------------------------------------------------------------------
302 inline
304 {
305  this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
306 }
307 
308 //-----------------------------------------------------------------------------
309 inline
311 {
312  this->Clear();
313 }
314 
315 //-----------------------------------------------------------------------------
316 template<typename T>
318 {
319  this->SetData(ext);
320 }
321 
322 //-----------------------------------------------------------------------------
323 template<typename T>
325  T ilo,
326  T ihi,
327  T jlo,
328  T jhi)
329 {
330  this->SetData(ilo, ihi, jlo, jhi);
331 }
332 
333 //-----------------------------------------------------------------------------
334 inline
336 {
337  if (&other == this)
338  {
339  return *this;
340  }
341  this->SetData(other);
342  return *this;
343 }
344 
345 //-----------------------------------------------------------------------------
346 inline
348 {
349  *this = other;
350 }
351 
352 //-----------------------------------------------------------------------------
353 template<typename T>
354 void vtkPixelExtent::Size(const vtkPixelExtent &ext, T nCells[2])
355 {
356  nCells[0] = ext[1] - ext[0] + 1;
357  nCells[1] = ext[3] - ext[2] + 1;
358 }
359 
360 //-----------------------------------------------------------------------------
361 inline
363 {
364  return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
365 }
366 
367 //-----------------------------------------------------------------------------
368 template<typename T>
369 void vtkPixelExtent::Size(T nCells[2]) const
370 {
371  vtkPixelExtent::Size(*this, nCells);
372 }
373 
374 //-----------------------------------------------------------------------------
375 inline
376 size_t vtkPixelExtent::Size() const
377 {
378  return vtkPixelExtent::Size(*this);
379 }
380 
381 //-----------------------------------------------------------------------------
382 inline
383 void vtkPixelExtent::GetStartIndex(int first[2]) const
384 {
385  first[0] = this->Data[0];
386  first[1] = this->Data[2];
387 }
388 
389 //-----------------------------------------------------------------------------
390 inline
391 void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
392 {
393  first[0] = this->Data[0] - origin[0];
394  first[1] = this->Data[2] - origin[1];
395 }
396 
397 //-----------------------------------------------------------------------------
398 inline
399 void vtkPixelExtent::GetEndIndex(int last[2]) const
400 {
401  last[0] = this->Data[1];
402  last[1] = this->Data[3];
403 }
404 
405 //-----------------------------------------------------------------------------
406 inline
408 {
409  if ( this->Data[0] > this->Data[1]
410  || this->Data[2] > this->Data[3])
411  {
412  return 1;
413  }
414  return 0;
415 }
416 
417 //-----------------------------------------------------------------------------
418 inline
420 {
421  if ( (this->Data[0] == other.Data[0])
422  && (this->Data[1] == other.Data[1])
423  && (this->Data[2] == other.Data[2])
424  && (this->Data[3] == other.Data[3]) )
425  {
426  return 1;
427  }
428  return 0;
429 }
430 
431 //-----------------------------------------------------------------------------
432 inline
434 {
435  if ( (this->Data[0] <= other.Data[0])
436  && (this->Data[1] >= other.Data[1])
437  && (this->Data[2] <= other.Data[2])
438  && (this->Data[3] >= other.Data[3]) )
439  {
440  return 1;
441  }
442  return 0;
443 }
444 
445 //-----------------------------------------------------------------------------
446 inline
447 int vtkPixelExtent::Contains(int i, int j) const
448 {
449  if ( (this->Data[0] <= i)
450  && (this->Data[1] >= i)
451  && (this->Data[2] <= j)
452  && (this->Data[3] >= j) )
453  {
454  return 1;
455  }
456  return 0;
457 }
458 
459 
460 //-----------------------------------------------------------------------------
461 inline
463 {
464  if (this->Empty())
465  {
466  return;
467  }
468 
469  if (other.Empty())
470  {
471  this->Clear();
472  return;
473  }
474 
475  this->Data[0] = std::max(this->Data[0], other.Data[0]);
476  this->Data[1] = std::min(this->Data[1], other.Data[1]);
477  this->Data[2] = std::max(this->Data[2], other.Data[2]);
478  this->Data[3] = std::min(this->Data[3], other.Data[3]);
479 
480  if (this->Empty())
481  {
482  this->Clear();
483  }
484 }
485 
486 //-----------------------------------------------------------------------------
487 inline
489 {
490  if (other.Empty())
491  {
492  return;
493  }
494 
495  if (this->Empty())
496  {
497  this->SetData(other.GetData());
498  return;
499  }
500 
501  this->Data[0] = std::min(this->Data[0], other.Data[0]);
502  this->Data[1] = std::max(this->Data[1], other.Data[1]);
503  this->Data[2] = std::min(this->Data[2], other.Data[2]);
504  this->Data[3] = std::max(this->Data[3], other.Data[3]);
505 }
506 
507 //-----------------------------------------------------------------------------
508 inline
510 {
511  other &= *this;
512  return other.Empty();
513 }
514 
515 //-----------------------------------------------------------------------------
516 inline
518 {
519  this->Data[0] -= n;
520  this->Data[1] += n;
521  this->Data[2] -= n;
522  this->Data[3] += n;
523 }
524 
525 //-----------------------------------------------------------------------------
526 inline
527 void vtkPixelExtent::Grow(int q, int n)
528 {
529  q *= 2;
530 
531  this->Data[q ] -= n;
532  this->Data[q+1] += n;
533 }
534 
535 //-----------------------------------------------------------------------------
536 inline
537 void vtkPixelExtent::GrowLow(int q, int n)
538 {
539  this->Data[2*q] -= n;
540 }
541 
542 //-----------------------------------------------------------------------------
543 inline
544 void vtkPixelExtent::GrowHigh(int q, int n)
545 {
546  this->Data[2*q+1] += n;
547 }
548 
549 //-----------------------------------------------------------------------------
550 inline
552 {
553  this->Data[0] += n;
554  this->Data[1] -= n;
555  this->Data[2] += n;
556  this->Data[3] -= n;
557 }
558 
559 //-----------------------------------------------------------------------------
560 inline
561 void vtkPixelExtent::Shrink(int q, int n)
562 {
563  q *= 2;
564  this->Data[q ] += n;
565  this->Data[q+1] -= n;
566 }
567 
568 //-----------------------------------------------------------------------------
569 inline
571 {
572  this->Data[0] += n[0];
573  this->Data[1] += n[0];
574  this->Data[2] += n[1];
575  this->Data[3] += n[1];
576 }
577 
578 //-----------------------------------------------------------------------------
579 inline
580 void vtkPixelExtent::Shift(int q, int n)
581 {
582  q *= 2;
583  this->Data[q ] += n;
584  this->Data[q+1] += n;
585 }
586 
587 //-----------------------------------------------------------------------------
588 inline
590 {
591  for (int q=0; q<2; ++q)
592  {
593  int qq = q*2;
594  int n = -other[qq];
595 
596  this->Data[qq ] += n;
597  this->Data[qq+1] += n;
598  }
599 }
600 
601 //-----------------------------------------------------------------------------
602 inline
604 {
605  for (int q=0; q<2; ++q)
606  {
607  int qq = q*2;
608  int n =- this->Data[qq];
609 
610  this->Data[qq ] += n;
611  this->Data[qq+1] += n;
612  }
613 }
614 
615 //-----------------------------------------------------------------------------
616 inline
618 {
619  vtkPixelExtent half;
620 
621  int q = 2 * dir;
622  int l = this->Data[q+1] - this->Data[q] + 1;
623  int s = l/2;
624 
625  if (s)
626  {
627  s += this->Data[q];
628  half = *this;
629  half.Data[q] = s;
630  this->Data[q+1] = s - 1;
631  }
632 
633  return half;
634 }
635 
636 //-----------------------------------------------------------------------------
637 inline
639 {
640  ++this->Data[1];
641  ++this->Data[3];
642 }
643 
644 //-----------------------------------------------------------------------------
645 inline
647 {
648  --this->Data[1];
649  --this->Data[3];
650 }
651 
652 //-----------------------------------------------------------------------------
653 inline
654 bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
655 {
656  return l.Size() < r.Size();
657 }
658 
659 #endif
660 // VTK-HeaderTest-Exclude: vtkPixelExtent.h
void GetStartIndex(int first[2]) const
int Disjoint(vtkPixelExtent other) const
const unsigned int * GetDataU() const
vtkPixelExtent(T width, T height)
int Contains(const vtkPixelExtent &other) const
void GrowLow(int q, int n)
int operator==(const vtkPixelExtent &other) const
const int & operator[](int i) const
bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
vtkPixelExtent & operator=(const vtkPixelExtent &other)
vtkPixelExtent Split(int dir)
void operator|=(const vtkPixelExtent &other)
VTKCOMMONDATAMODEL_EXPORT std::ostream & operator<<(std::ostream &os, const vtkPixelExtent &ext)
void GrowHigh(int q, int n)
size_t Size() const
void SetData(const vtkPixelExtent &ext)
void operator&=(const vtkPixelExtent &other)
void GetEndIndex(int last[2]) const
int Empty() const
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
VTKWRAPPINGJAVA_EXPORT jlong q(JNIEnv *env, jobject obj)
void Size(T nCells[2]) const
void Shrink(int n)
const int * GetData() const
int & operator[](int i)
void Grow(int n)
#define VTKCOMMONDATAMODEL_EXPORT
#define max(a, b)
unsigned int * GetDataU()