VTK  9.1.0
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 "vtkCommonDataModelModule.h" // for export
31#include "vtkSystemIncludes.h" // for VTK's system header config
32
33#include <algorithm> // for inline impl
34#include <climits> // for inline impl
35#include <deque> // for inline impl
36#include <iostream> // for inline impl
37
38class VTKCOMMONDATAMODEL_EXPORT vtkPixelExtent
39{
40public:
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 {
52 this->SetData(T(0), width - T(1), T(0), height - T(1));
53 }
54
55 vtkPixelExtent(const vtkPixelExtent& other);
56
57 vtkPixelExtent& operator=(const vtkPixelExtent& other);
58
62 int& operator[](int i) { return this->Data[i]; }
63 const int& operator[](int i) const { return this->Data[i]; }
64
68 void SetData(const vtkPixelExtent& ext);
69
70 template <typename T>
71 void SetData(const T* ext);
72
73 template <typename T>
74 void SetData(T ilo, T ihi, T jlo, T jhi);
75 void Clear();
76
80 int* GetData() { return this->Data; }
81 const int* GetData() const { return this->Data; }
82
83 template <typename T>
84 void GetData(T data[4]) const;
85
86 unsigned int* GetDataU() { return reinterpret_cast<unsigned int*>(this->Data); }
87
88 const unsigned int* GetDataU() const { 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
136 void operator&=(const vtkPixelExtent& other);
137
141 void operator|=(const vtkPixelExtent& other);
142
144
147 void Grow(int n);
148 void Grow(int q, int n);
149 void GrowLow(int q, int n);
150 void GrowHigh(int q, int n);
152
154
157 void Shrink(int n);
158 void Shrink(int q, int n);
160
164 void Shift();
165
169 void Shift(const vtkPixelExtent& ext);
170
174 void Shift(int* n);
175
179 void Shift(int q, int n);
180
187 vtkPixelExtent Split(int dir);
188
190
193 void CellToNode();
194 void NodeToCell();
196
200 template <typename T>
201 static void Size(const vtkPixelExtent& ext, T nCells[2]);
202
206 static size_t Size(const vtkPixelExtent& ext);
207
213 static vtkPixelExtent Grow(const vtkPixelExtent& inputExt, int n);
214
216 const vtkPixelExtent& inputExt, const vtkPixelExtent& problemDomain, int n);
217
218 static vtkPixelExtent GrowLow(const vtkPixelExtent& ext, int q, int n);
219
220 static vtkPixelExtent GrowHigh(const vtkPixelExtent& ext, int q, int n);
221
227 const vtkPixelExtent& inputExt, const vtkPixelExtent& problemDomain, int n);
228
229 static vtkPixelExtent Shrink(const vtkPixelExtent& inputExt, int n);
230
235 static vtkPixelExtent NodeToCell(const vtkPixelExtent& inputExt);
236
241 static vtkPixelExtent CellToNode(const vtkPixelExtent& inputExt);
242
244
247 static void Shift(int* ij, int n);
248 static void Shift(int* ij, int* n);
250
256 static void Split(int i, int j, const vtkPixelExtent& ext, std::deque<vtkPixelExtent>& newExts);
257
264 static void Subtract(
265 const vtkPixelExtent& A, const vtkPixelExtent& B, std::deque<vtkPixelExtent>& C);
266
272 static void Merge(std::deque<vtkPixelExtent>& exts);
273
274private:
275 int Data[4];
276};
277
281VTKCOMMONDATAMODEL_EXPORT
282std::ostream& operator<<(std::ostream& os, const vtkPixelExtent& ext);
283
284//-----------------------------------------------------------------------------
285template <typename T>
286void vtkPixelExtent::SetData(const T* ext)
287{
288 Data[0] = static_cast<int>(ext[0]);
289 Data[1] = static_cast<int>(ext[1]);
290 Data[2] = static_cast<int>(ext[2]);
291 Data[3] = static_cast<int>(ext[3]);
292}
293
294//-----------------------------------------------------------------------------
295template <typename T>
296void vtkPixelExtent::SetData(T ilo, T ihi, T jlo, T jhi)
297{
298 T ext[4] = { ilo, ihi, jlo, jhi };
299 this->SetData(ext);
300}
301
302//-----------------------------------------------------------------------------
304{
305 this->SetData(other.GetData());
306}
307
308//-----------------------------------------------------------------------------
309template <typename T>
310void vtkPixelExtent::GetData(T data[4]) const
311{
312 data[0] = static_cast<T>(this->Data[0]);
313 data[1] = static_cast<T>(this->Data[1]);
314 data[2] = static_cast<T>(this->Data[2]);
315 data[3] = static_cast<T>(this->Data[3]);
316}
317
318//-----------------------------------------------------------------------------
320{
321 this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
322}
323
324//-----------------------------------------------------------------------------
326{
327 this->Clear();
328}
329
330//-----------------------------------------------------------------------------
331template <typename T>
333{
334 this->SetData(ext);
335}
336
337//-----------------------------------------------------------------------------
338template <typename T>
339vtkPixelExtent::vtkPixelExtent(T ilo, T ihi, T jlo, T jhi)
340{
341 this->SetData(ilo, ihi, jlo, jhi);
342}
343
344//-----------------------------------------------------------------------------
346{
347 if (&other != this)
348 {
349 this->Data[0] = other.Data[0];
350 this->Data[1] = other.Data[1];
351 this->Data[2] = other.Data[2];
352 this->Data[3] = other.Data[3];
353 }
354 return *this;
355}
356
357//-----------------------------------------------------------------------------
359{
360 *this = other;
361}
362
363//-----------------------------------------------------------------------------
364template <typename T>
365void vtkPixelExtent::Size(const vtkPixelExtent& ext, T nCells[2])
366{
367 nCells[0] = ext[1] - ext[0] + 1;
368 nCells[1] = ext[3] - ext[2] + 1;
369}
370
371//-----------------------------------------------------------------------------
372inline size_t vtkPixelExtent::Size(const vtkPixelExtent& ext)
373{
374 return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
375}
376
377//-----------------------------------------------------------------------------
378template <typename T>
379void vtkPixelExtent::Size(T nCells[2]) const
380{
381 vtkPixelExtent::Size(*this, nCells);
382}
383
384//-----------------------------------------------------------------------------
385inline size_t vtkPixelExtent::Size() const
386{
387 return vtkPixelExtent::Size(*this);
388}
389
390//-----------------------------------------------------------------------------
391inline void vtkPixelExtent::GetStartIndex(int first[2]) const
392{
393 first[0] = this->Data[0];
394 first[1] = this->Data[2];
395}
396
397//-----------------------------------------------------------------------------
398inline void vtkPixelExtent::GetStartIndex(int first[2], const int origin[2]) const
399{
400 first[0] = this->Data[0] - origin[0];
401 first[1] = this->Data[2] - origin[1];
402}
403
404//-----------------------------------------------------------------------------
405inline void vtkPixelExtent::GetEndIndex(int last[2]) const
406{
407 last[0] = this->Data[1];
408 last[1] = this->Data[3];
409}
410
411//-----------------------------------------------------------------------------
412inline int vtkPixelExtent::Empty() const
413{
414 if (this->Data[0] > this->Data[1] || this->Data[2] > this->Data[3])
415 {
416 return 1;
417 }
418 return 0;
419}
420
421//-----------------------------------------------------------------------------
422inline bool vtkPixelExtent::operator==(const vtkPixelExtent& other) const
423{
424 if ((this->Data[0] == other.Data[0]) && (this->Data[1] == other.Data[1]) &&
425 (this->Data[2] == other.Data[2]) && (this->Data[3] == other.Data[3]))
426 {
427 return true;
428 }
429 return false;
430}
431
432//-----------------------------------------------------------------------------
433inline int vtkPixelExtent::Contains(const vtkPixelExtent& other) const
434{
435 if ((this->Data[0] <= other.Data[0]) && (this->Data[1] >= other.Data[1]) &&
436 (this->Data[2] <= other.Data[2]) && (this->Data[3] >= other.Data[3]))
437 {
438 return 1;
439 }
440 return 0;
441}
442
443//-----------------------------------------------------------------------------
444inline int vtkPixelExtent::Contains(int i, int j) const
445{
446 if ((this->Data[0] <= i) && (this->Data[1] >= i) && (this->Data[2] <= j) && (this->Data[3] >= j))
447 {
448 return 1;
449 }
450 return 0;
451}
452
453//-----------------------------------------------------------------------------
455{
456 if (this->Empty())
457 {
458 return;
459 }
460
461 if (other.Empty())
462 {
463 this->Clear();
464 return;
465 }
466
467 this->Data[0] = std::max(this->Data[0], other.Data[0]);
468 this->Data[1] = std::min(this->Data[1], other.Data[1]);
469 this->Data[2] = std::max(this->Data[2], other.Data[2]);
470 this->Data[3] = std::min(this->Data[3], other.Data[3]);
471
472 if (this->Empty())
473 {
474 this->Clear();
475 }
476}
477
478//-----------------------------------------------------------------------------
480{
481 if (other.Empty())
482 {
483 return;
484 }
485
486 if (this->Empty())
487 {
488 this->SetData(other.GetData());
489 return;
490 }
491
492 this->Data[0] = std::min(this->Data[0], other.Data[0]);
493 this->Data[1] = std::max(this->Data[1], other.Data[1]);
494 this->Data[2] = std::min(this->Data[2], other.Data[2]);
495 this->Data[3] = std::max(this->Data[3], other.Data[3]);
496}
497
498//-----------------------------------------------------------------------------
500{
501 other &= *this;
502 return other.Empty();
503}
504
505//-----------------------------------------------------------------------------
506inline void vtkPixelExtent::Grow(int n)
507{
508 this->Data[0] -= n;
509 this->Data[1] += n;
510 this->Data[2] -= n;
511 this->Data[3] += n;
512}
513
514//-----------------------------------------------------------------------------
515inline void vtkPixelExtent::Grow(int q, int n)
516{
517 q *= 2;
518
519 this->Data[q] -= n;
520 this->Data[q + 1] += n;
521}
522
523//-----------------------------------------------------------------------------
524inline void vtkPixelExtent::GrowLow(int q, int n)
525{
526 this->Data[2 * q] -= n;
527}
528
529//-----------------------------------------------------------------------------
530inline void vtkPixelExtent::GrowHigh(int q, int n)
531{
532 this->Data[2 * q + 1] += n;
533}
534
535//-----------------------------------------------------------------------------
536inline void vtkPixelExtent::Shrink(int n)
537{
538 this->Data[0] += n;
539 this->Data[1] -= n;
540 this->Data[2] += n;
541 this->Data[3] -= n;
542}
543
544//-----------------------------------------------------------------------------
545inline void vtkPixelExtent::Shrink(int q, int n)
546{
547 q *= 2;
548 this->Data[q] += n;
549 this->Data[q + 1] -= n;
550}
551
552//-----------------------------------------------------------------------------
553inline void vtkPixelExtent::Shift(int* n)
554{
555 this->Data[0] += n[0];
556 this->Data[1] += n[0];
557 this->Data[2] += n[1];
558 this->Data[3] += n[1];
559}
560
561//-----------------------------------------------------------------------------
562inline void vtkPixelExtent::Shift(int q, int n)
563{
564 q *= 2;
565 this->Data[q] += n;
566 this->Data[q + 1] += n;
567}
568
569//-----------------------------------------------------------------------------
570inline void vtkPixelExtent::Shift(const vtkPixelExtent& other)
571{
572 for (int q = 0; q < 2; ++q)
573 {
574 int qq = q * 2;
575 int n = -other[qq];
576
577 this->Data[qq] += n;
578 this->Data[qq + 1] += n;
579 }
580}
581
582//-----------------------------------------------------------------------------
584{
585 for (int q = 0; q < 2; ++q)
586 {
587 int qq = q * 2;
588 int n = -this->Data[qq];
589
590 this->Data[qq] += n;
591 this->Data[qq + 1] += n;
592 }
593}
594
595//-----------------------------------------------------------------------------
597{
598 vtkPixelExtent half;
599
600 int q = 2 * dir;
601 int l = this->Data[q + 1] - this->Data[q] + 1;
602 int s = l / 2;
603
604 if (s)
605 {
606 s += this->Data[q];
607 half = *this;
608 half.Data[q] = s;
609 this->Data[q + 1] = s - 1;
610 }
611
612 return half;
613}
614
615//-----------------------------------------------------------------------------
617{
618 ++this->Data[1];
619 ++this->Data[3];
620}
621
622//-----------------------------------------------------------------------------
624{
625 --this->Data[1];
626 --this->Data[3];
627}
628
629//-----------------------------------------------------------------------------
630inline bool operator<(const vtkPixelExtent& l, const vtkPixelExtent& r)
631{
632 return l.Size() < r.Size();
633}
634
635#endif
636// VTK-HeaderTest-Exclude: vtkPixelExtent.h
Representation of a cartesian pixel plane and common operations on it.
void CellToNode()
In-place conversion from cell based to node based extent, and vise-versa.
static vtkPixelExtent Grow(const vtkPixelExtent &inputExt, const vtkPixelExtent &problemDomain, int n)
static vtkPixelExtent GrowLow(const vtkPixelExtent &ext, int q, int n)
vtkPixelExtent & operator=(const vtkPixelExtent &other)
const int * GetData() const
vtkPixelExtent Split(int dir)
Divide the extent in half in the given direction.
bool operator==(const vtkPixelExtent &other) const
Test for equivalence.
vtkPixelExtent(T width, T height)
static vtkPixelExtent Grow(const vtkPixelExtent &inputExt, int n)
Add or remove ghost cells.
void SetData(const vtkPixelExtent &ext)
Set the extent.
static void Merge(std::deque< vtkPixelExtent > &exts)
Merge compatible extents in the list.
size_t Size() const
Get the total number.
void operator&=(const vtkPixelExtent &other)
In place intersection.
static vtkPixelExtent Shrink(const vtkPixelExtent &inputExt, const vtkPixelExtent &problemDomain, int n)
Remove ghost cells.
static vtkPixelExtent GrowHigh(const vtkPixelExtent &ext, int q, int n)
void NodeToCell()
In-place conversion from cell based to node based extent, and vise-versa.
const unsigned int * GetDataU() const
int Disjoint(vtkPixelExtent other) const
Return non-zero if the extent is disjoint from the other.
static vtkPixelExtent Shrink(const vtkPixelExtent &inputExt, int n)
int & operator[](int i)
Element access.
void GetStartIndex(int first[2]) const
Get the start/end index.
void Shrink(int n)
Shrink the extent by n.
void Shift()
Shifts by low corner of this, moving to the origin.
int * GetData()
Direct access to internal data.
static void Shift(int *ij, int n)
Shift by the given amount while respecting mode.
int Contains(const vtkPixelExtent &other) const
Return non-zero if this extent contains the other.
static vtkPixelExtent NodeToCell(const vtkPixelExtent &inputExt)
Convert from point extent to cell extent while respecting the dimensionality of the data.
unsigned int * GetDataU()
void GetEndIndex(int last[2]) const
Get the start/end index.
void Size(T nCells[2]) const
Get the number in each direction.
int Empty() const
Return true if empty.
const int & operator[](int i) const
static void Split(int i, int j, const vtkPixelExtent &ext, std::deque< vtkPixelExtent > &newExts)
Split ext at i,j, resulting extents (up to 4) are appended to newExts.
static void Subtract(const vtkPixelExtent &A, const vtkPixelExtent &B, std::deque< vtkPixelExtent > &C)
A - B = C C is a set of disjoint extents such that the intersection of B and C is empty and the inter...
static void Shift(int *ij, int *n)
Shift by the given amount while respecting mode.
void GrowLow(int q, int n)
Expand the extents by n.
void operator|=(const vtkPixelExtent &other)
In place union.
void Grow(int n)
Expand the extents by n.
static vtkPixelExtent CellToNode(const vtkPixelExtent &inputExt)
Convert from cell extent to point extent while respecting the dimensionality of the data.
void GrowHigh(int q, int n)
Expand the extents by n.
bool operator<(const vtkPixelExtent &l, const vtkPixelExtent &r)
VTKCOMMONDATAMODEL_EXPORT std::ostream & operator<<(std::ostream &os, const vtkPixelExtent &ext)
Stream insertion operator for formatted output of pixel extents.
VTKCOMMONCORE_EXPORT bool operator==(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)