VTK
vtkRect.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVector.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 =========================================================================*/
15 
29 #ifndef vtkRect_h
30 #define vtkRect_h
31 
32 #include "vtkVector.h"
33 
34 #include "vtkMath.h" // for Min, Max
35 
36 template<typename T>
37 class vtkRect : public vtkVector<T, 4>
38 {
39 public:
41  {
42  }
43 
44  vtkRect(const T& x, const T& y, const T& width, const T& height)
45  {
46  this->Data[0] = x;
47  this->Data[1] = y;
48  this->Data[2] = width;
49  this->Data[3] = height;
50  }
51 
52  explicit vtkRect(const T* init) : vtkVector<T, 4>(init) { }
53 
55 
56  void Set(const T& x, const T& y, const T& width, const T& height)
57  {
58  this->Data[0] = x;
59  this->Data[1] = y;
60  this->Data[2] = width;
61  this->Data[3] = height;
62  }
64 
66  void SetX(const T& x) { this->Data[0] = x; }
67 
69  const T& GetX() const { return this->Data[0]; }
70 
72  void SetY(const T& y) { this->Data[1] = y; }
73 
75  const T& GetY() const { return this->Data[1]; }
76 
78  void SetWidth(const T& width) { this->Data[2] = width; }
79 
81  const T& GetWidth() const { return this->Data[2]; }
82 
84  void SetHeight(const T& height) { this->Data[3] = height; }
85 
87  const T& GetHeight() const { return this->Data[3]; }
88 
90  const T& GetLeft() const { return this->Data[0]; }
91 
93  T GetRight() const { return this->Data[0] + this->Data[2]; }
94 
96  T GetTop() const { return this->Data[1] + this->Data[3]; }
97 
99  const T& GetBottom() const { return this->Data[1]; }
100 
102 
104  {
105  return vtkVector2<T>(this->GetLeft(), this->GetBottom());
106  }
108 
110 
112  {
113  return vtkVector2<T>(this->GetLeft(), this->GetTop());
114  }
116 
118 
120  {
121  return vtkVector2<T>(this->GetRight(), this->GetBottom());
122  }
124 
126 
128  {
129  return vtkVector2<T>(this->GetRight(), this->GetTop());
130  }
132 
134 
135  void AddPoint(const T point[2])
136  {
137  // This code is written like this to ensure that adding a point gives
138  // exactly the same result as AddRect(vtkRect(x,y,0,0)
139  if (point[0] < this->GetX())
140  {
141  T dx = this->GetX() - point[0];
142  this->SetX(point[0]);
143  this->SetWidth(dx + this->GetWidth());
144  }
145  else if (point[0] > this->GetX())
146  {
147  // this->GetX() is already correct
148  T dx = point[0] - this->GetX();
149  this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
150  }
152 
153  if (point[1] < this->GetY())
154  {
155  T dy = this->GetY() - point[1];
156  this->SetY(point[1]);
157  this->SetHeight(dy + this->GetHeight());
158  }
159  else if (point[1] > this->GetY())
160  {
161  // this->GetY() is already correct
162  T dy = point[1] - this->GetY();
163  this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
164  }
165  }
166 
168 
169  void AddPoint(T x, T y)
170  {
171  T point[2] = {x, y};
172  this->AddPoint(point);
173  }
175 
177 
178  void AddRect(const vtkRect<T> & rect)
179  {
180  if (rect.GetX() < this->GetX())
181  {
182  T dx = this->GetX() - rect.GetX();
183  this->SetX(rect.GetX());
184  this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
185  }
186  else if (rect.GetX() > this->GetX())
187  {
188  T dx = rect.GetX() - this->GetX();
189  // this->GetX() is already correct
190  this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
191  }
192  else
193  {
194  // this->GetX() is already correct
195  this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
196  }
198 
199  if (rect.GetY() < this->GetY())
200  {
201  T dy = this->GetY() - rect.GetY();
202  this->SetY(rect.GetY());
203  this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
204  }
205  else if (rect.GetY() > this->GetY())
206  {
207  T dy = rect.GetY() - this->GetY();
208  // this->GetY() is already correct
209  this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
210  }
211  else
212  {
213  // this->GetY() is already correct
214  this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
215  }
216  }
217 
219 
223  bool IntersectsWith(const vtkRect<T> & rect)
224  {
225  bool intersects = true;
227 
228  if (rect.GetX() < this->GetX())
229  {
230  T dx = this->GetX() - rect.GetX();
231  intersects &= (dx < rect.GetWidth());
232  }
233  else if (rect.GetX() > this->GetX())
234  {
235  T dx = rect.GetX() - this->GetX();
236  intersects &= (dx < this->GetWidth());
237  }
238 
239  if (rect.GetY() < this->GetY())
240  {
241  T dy = this->GetY() - rect.GetY();
242  intersects &= (dy < rect.GetHeight());
243  }
244  else if (rect.GetY() > this->GetY())
245  {
246  T dy = rect.GetY() - this->GetY();
247  intersects &= (dy < this->GetHeight());
248  }
249 
250  return intersects;
251  }
252 };
253 
254 class vtkRecti : public vtkRect<int>
255 {
256 public:
257  vtkRecti() {}
258  vtkRecti(int x, int y, int width, int height)
259  : vtkRect<int>(x, y, width, height) {}
260  explicit vtkRecti(const int *init) : vtkRect<int>(init) {}
261 };
262 
263 class vtkRectf : public vtkRect<float>
264 {
265 public:
266  vtkRectf() {}
267  vtkRectf(float x, float y, float width, float height)
268  : vtkRect<float>(x, y, width, height) {}
269  explicit vtkRectf(const float *init) : vtkRect<float>(init) {}
270 };
271 
272 class vtkRectd : public vtkRect<double>
273 {
274 public:
275  vtkRectd() {}
276  vtkRectd(double x, double y, double width, double height)
277  : vtkRect<double>(x, y, width, height) {}
278  explicit vtkRectd(const double *init) : vtkRect<double>(init) {}
279 };
280 
281 #endif // vtkRect_h
282 // VTK-HeaderTest-Exclude: vtkRect.h
T Data[Size]
Definition: vtkTuple.h:136
vtkRectf()
Definition: vtkRect.h:266
templated base type for storage of vectors.
Definition: vtkVector.h:39
const T & GetBottom() const
Definition: vtkRect.h:99
void SetY(const T &y)
Definition: vtkRect.h:72
vtkRectd(const double *init)
Definition: vtkRect.h:278
templated base type for storage of 2D rectangles.
Definition: vtkRect.h:37
void AddPoint(const T point[2])
Definition: vtkRect.h:135
const T & GetLeft() const
Definition: vtkRect.h:90
bool IntersectsWith(const vtkRect< T > &rect)
Definition: vtkRect.h:223
vtkRecti()
Definition: vtkRect.h:257
void AddRect(const vtkRect< T > &rect)
Definition: vtkRect.h:178
void SetWidth(const T &width)
Definition: vtkRect.h:78
const T & GetWidth() const
Definition: vtkRect.h:81
T GetRight() const
Definition: vtkRect.h:93
vtkRect()
Definition: vtkRect.h:40
T GetTop() const
Definition: vtkRect.h:96
vtkVector2< T > GetBottomLeft() const
Definition: vtkRect.h:103
vtkVector< T, 2 > GetTopRight() const
Definition: vtkRect.h:127
void Set(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:56
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:267
const T & GetY() const
Definition: vtkRect.h:75
vtkRect(const T *init)
Definition: vtkRect.h:52
vtkVector< T, 2 > GetBottomRight() const
Definition: vtkRect.h:119
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:258
void AddPoint(T x, T y)
Definition: vtkRect.h:169
vtkRecti(const int *init)
Definition: vtkRect.h:260
void SetHeight(const T &height)
Definition: vtkRect.h:84
const T & GetX() const
Definition: vtkRect.h:69
void SetX(const T &x)
Definition: vtkRect.h:66
vtkVector< T, 2 > GetTopLeft() const
Definition: vtkRect.h:111
vtkRectd()
Definition: vtkRect.h:275
const T & GetHeight() const
Definition: vtkRect.h:87
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:276
static T Max(const T &a, const T &b)
Definition: vtkMath.h:1053
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:44
vtkRectf(const float *init)
Definition: vtkRect.h:269