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
41class VTKCOMMONDATAMODEL_EXPORT vtkPixelExtent
42{
43public:
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>
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
228 const vtkPixelExtent &inputExt,
229 const vtkPixelExtent &problemDomain,
230 int n);
231
233 const vtkPixelExtent &ext,
234 int q,
235 int n);
236
238 const vtkPixelExtent &ext,
239 int q,
240 int n);
241
247 const vtkPixelExtent &inputExt,
248 const vtkPixelExtent &problemDomain,
249 int n);
250
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,
296 std::deque<vtkPixelExtent> &newExts);
297
303 static void Merge(std::deque<vtkPixelExtent> &exts);
304
305private:
306 int Data[4];
307};
308
312VTKCOMMONDATAMODEL_EXPORT
313std::ostream &operator<<(std::ostream &os, const vtkPixelExtent &ext);
314
315//-----------------------------------------------------------------------------
316template<typename T>
317void 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//-----------------------------------------------------------------------------
326template<typename T>
327void 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//-----------------------------------------------------------------------------
334inline
336{
337 this->SetData(other.GetData());
338}
339
340//-----------------------------------------------------------------------------
341template<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//-----------------------------------------------------------------------------
351inline
353{
354 this->SetData<int>(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
355}
356
357//-----------------------------------------------------------------------------
358inline
360{
361 this->Clear();
362}
363
364//-----------------------------------------------------------------------------
365template<typename T>
367{
368 this->SetData(ext);
369}
370
371//-----------------------------------------------------------------------------
372template<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//-----------------------------------------------------------------------------
383inline
385{
386 if (&other == this)
387 {
388 return *this;
389 }
390 this->SetData(other);
391 return *this;
392}
393
394//-----------------------------------------------------------------------------
395inline
397{
398 *this = other;
399}
400
401//-----------------------------------------------------------------------------
402template<typename T>
403void 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//-----------------------------------------------------------------------------
410inline
412{
413 return (ext[1] - ext[0] + 1) * (ext[3] - ext[2] + 1);
414}
415
416//-----------------------------------------------------------------------------
417template<typename T>
418void vtkPixelExtent::Size(T nCells[2]) const
419{
420 vtkPixelExtent::Size(*this, nCells);
421}
422
423//-----------------------------------------------------------------------------
424inline
426{
427 return vtkPixelExtent::Size(*this);
428}
429
430//-----------------------------------------------------------------------------
431inline
432void vtkPixelExtent::GetStartIndex(int first[2]) const
433{
434 first[0] = this->Data[0];
435 first[1] = this->Data[2];
436}
437
438//-----------------------------------------------------------------------------
439inline
440void 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//-----------------------------------------------------------------------------
447inline
448void vtkPixelExtent::GetEndIndex(int last[2]) const
449{
450 last[0] = this->Data[1];
451 last[1] = this->Data[3];
452}
453
454//-----------------------------------------------------------------------------
455inline
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//-----------------------------------------------------------------------------
467inline
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//-----------------------------------------------------------------------------
481inline
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//-----------------------------------------------------------------------------
495inline
496int 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//-----------------------------------------------------------------------------
510inline
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//-----------------------------------------------------------------------------
536inline
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//-----------------------------------------------------------------------------
557inline
559{
560 other &= *this;
561 return other.Empty();
562}
563
564//-----------------------------------------------------------------------------
565inline
567{
568 this->Data[0] -= n;
569 this->Data[1] += n;
570 this->Data[2] -= n;
571 this->Data[3] += n;
572}
573
574//-----------------------------------------------------------------------------
575inline
576void 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//-----------------------------------------------------------------------------
585inline
586void vtkPixelExtent::GrowLow(int q, int n)
587{
588 this->Data[2*q] -= n;
589}
590
591//-----------------------------------------------------------------------------
592inline
593void vtkPixelExtent::GrowHigh(int q, int n)
594{
595 this->Data[2*q+1] += n;
596}
597
598//-----------------------------------------------------------------------------
599inline
601{
602 this->Data[0] += n;
603 this->Data[1] -= n;
604 this->Data[2] += n;
605 this->Data[3] -= n;
606}
607
608//-----------------------------------------------------------------------------
609inline
610void vtkPixelExtent::Shrink(int q, int n)
611{
612 q *= 2;
613 this->Data[q ] += n;
614 this->Data[q+1] -= n;
615}
616
617//-----------------------------------------------------------------------------
618inline
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//-----------------------------------------------------------------------------
628inline
629void vtkPixelExtent::Shift(int q, int n)
630{
631 q *= 2;
632 this->Data[q ] += n;
633 this->Data[q+1] += n;
634}
635
636//-----------------------------------------------------------------------------
637inline
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//-----------------------------------------------------------------------------
651inline
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//-----------------------------------------------------------------------------
665inline
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//-----------------------------------------------------------------------------
686inline
688{
689 ++this->Data[1];
690 ++this->Data[3];
691}
692
693//-----------------------------------------------------------------------------
694inline
696{
697 --this->Data[1];
698 --this->Data[3];
699}
700
701//-----------------------------------------------------------------------------
702inline
704{
705 return l.Size() < r.Size();
706}
707
708#endif
709// 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
static void Subtract(const vtkPixelExtent &A, vtkPixelExtent B, std::deque< vtkPixelExtent > &newExts)
A - B = C C is a set of disjoint extents such that the intersection of B and C is empty and the inter...
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)
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 conatins 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
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 Shift(int *ij, int *n)
void GrowLow(int q, int 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)
@ dir
Definition: vtkX3D.h:324
@ height
Definition: vtkX3D.h:254
@ data
Definition: vtkX3D.h:315
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)
#define max(a, b)