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 
27 #ifndef vtkRect_h
28 #define vtkRect_h
29 
30 #include "vtkVector.h"
31 
32 #include "vtkMath.h" // for Min, Max
33 
34 template <typename T>
35 class vtkRect : public vtkVector<T, 4>
36 {
37 public:
38  vtkRect() {}
39 
40  vtkRect(const T& x, const T& y, const T& width, const T& height)
41  {
42  this->Data[0] = x;
43  this->Data[1] = y;
44  this->Data[2] = width;
45  this->Data[3] = height;
46  }
47 
48  explicit vtkRect(const T* init)
49  : vtkVector<T, 4>(init)
50  {
51  }
52 
54 
57  void Set(const T& x, const T& y, const T& width, const T& height)
58  {
59  this->Data[0] = x;
60  this->Data[1] = y;
61  this->Data[2] = width;
62  this->Data[3] = height;
63  }
65 
69  void SetX(const T& x) { this->Data[0] = x; }
70 
74  const T& GetX() const { return this->Data[0]; }
75 
79  void SetY(const T& y) { this->Data[1] = y; }
80 
84  const T& GetY() const { return this->Data[1]; }
85 
89  void SetWidth(const T& width) { this->Data[2] = width; }
90 
94  const T& GetWidth() const { return this->Data[2]; }
95 
99  void SetHeight(const T& height) { this->Data[3] = height; }
100 
104  const T& GetHeight() const { return this->Data[3]; }
105 
109  const T& GetLeft() const { return this->Data[0]; }
110 
114  T GetRight() const { return this->Data[0] + this->Data[2]; }
115 
119  T GetTop() const { return this->Data[1] + this->Data[3]; }
120 
124  const T& GetBottom() const { return this->Data[1]; }
125 
129  vtkVector2<T> GetBottomLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetBottom()); }
130 
134  vtkVector<T, 2> GetTopLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetTop()); }
135 
140  {
141  return vtkVector2<T>(this->GetRight(), this->GetBottom());
142  }
143 
147  vtkVector<T, 2> GetTopRight() const { return vtkVector2<T>(this->GetRight(), this->GetTop()); }
148 
150 
153  void AddPoint(const T point[2])
154  {
155  // This code is written like this to ensure that adding a point gives
156  // exactly the same result as AddRect(vtkRect(x,y,0,0)
157  if (point[0] < this->GetX())
158  {
159  T dx = this->GetX() - point[0];
160  this->SetX(point[0]);
161  this->SetWidth(dx + this->GetWidth());
162  }
163  else if (point[0] > this->GetX())
164  {
165  // this->GetX() is already correct
166  T dx = point[0] - this->GetX();
167  this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
168  }
170 
171  if (point[1] < this->GetY())
172  {
173  T dy = this->GetY() - point[1];
174  this->SetY(point[1]);
175  this->SetHeight(dy + this->GetHeight());
176  }
177  else if (point[1] > this->GetY())
178  {
179  // this->GetY() is already correct
180  T dy = point[1] - this->GetY();
181  this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
182  }
183  }
184 
186 
189  void AddPoint(T x, T y)
190  {
191  T point[2] = { x, y };
192  this->AddPoint(point);
193  }
195 
197 
200  void AddRect(const vtkRect<T>& rect)
201  {
202  if (rect.GetX() < this->GetX())
203  {
204  T dx = this->GetX() - rect.GetX();
205  this->SetX(rect.GetX());
206  this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
207  }
208  else if (rect.GetX() > this->GetX())
209  {
210  T dx = rect.GetX() - this->GetX();
211  // this->GetX() is already correct
212  this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
213  }
214  else
215  {
216  // this->GetX() is already correct
217  this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
218  }
220 
221  if (rect.GetY() < this->GetY())
222  {
223  T dy = this->GetY() - rect.GetY();
224  this->SetY(rect.GetY());
225  this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
226  }
227  else if (rect.GetY() > this->GetY())
228  {
229  T dy = rect.GetY() - this->GetY();
230  // this->GetY() is already correct
231  this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
232  }
233  else
234  {
235  // this->GetY() is already correct
236  this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
237  }
238  }
239 
246  bool IntersectsWith(const vtkRect<T>& rect) const
247  {
248  bool intersects = true;
249 
250  if (rect.GetX() < this->GetX())
251  {
252  T dx = this->GetX() - rect.GetX();
253  intersects &= (dx < rect.GetWidth());
254  }
255  else if (rect.GetX() > this->GetX())
256  {
257  T dx = rect.GetX() - this->GetX();
258  intersects &= (dx < this->GetWidth());
259  }
260 
261  if (rect.GetY() < this->GetY())
262  {
263  T dy = this->GetY() - rect.GetY();
264  intersects &= (dy < rect.GetHeight());
265  }
266  else if (rect.GetY() > this->GetY())
267  {
268  T dy = rect.GetY() - this->GetY();
269  intersects &= (dy < this->GetHeight());
270  }
271 
272  return intersects;
273  }
274 
279  void MoveTo(T x, T y)
280  {
281  this->Data[0] = x;
282  this->Data[1] = y;
283  }
284 
293  bool Intersect(const vtkRect<T>& other)
294  {
295  if (this->IntersectsWith(other))
296  {
297  const T left = vtkMath::Max(this->GetLeft(), other.GetLeft());
298  const T bottom = vtkMath::Max(this->GetBottom(), other.GetBottom());
299  const T right = vtkMath::Min(this->GetRight(), other.GetRight());
300  const T top = vtkMath::Min(this->GetTop(), other.GetTop());
301 
302  this->Data[0] = left;
303  this->Data[1] = bottom;
304  this->Data[2] = (right - left);
305  this->Data[3] = (top - bottom);
306  return true;
307  }
308  return false;
309  }
310 };
311 
312 class vtkRecti : public vtkRect<int>
313 {
314 public:
315  vtkRecti() {}
316  vtkRecti(int x, int y, int width, int height)
317  : vtkRect<int>(x, y, width, height)
318  {
319  }
320  explicit vtkRecti(const int* init)
321  : vtkRect<int>(init)
322  {
323  }
324 };
325 
326 class vtkRectf : public vtkRect<float>
327 {
328 public:
329  vtkRectf() {}
330  vtkRectf(float x, float y, float width, float height)
331  : vtkRect<float>(x, y, width, height)
332  {
333  }
334  explicit vtkRectf(const float* init)
335  : vtkRect<float>(init)
336  {
337  }
338 };
339 
340 class vtkRectd : public vtkRect<double>
341 {
342 public:
343  vtkRectd() {}
344  vtkRectd(double x, double y, double width, double height)
345  : vtkRect<double>(x, y, width, height)
346  {
347  }
348  explicit vtkRectd(const double* init)
349  : vtkRect<double>(init)
350  {
351  }
352 };
353 
354 #endif // vtkRect_h
355 // VTK-HeaderTest-Exclude: vtkRect.h
vtkTuple::Data
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:143
vtkRectf::vtkRectf
vtkRectf(const float *init)
Definition: vtkRect.h:334
vtkRectd::vtkRectd
vtkRectd()
Definition: vtkRect.h:343
vtkMath.h
vtkRect::GetTopRight
vtkVector< T, 2 > GetTopRight() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:147
vtkRect::GetY
const T & GetY() const
Get the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:84
vtkRect::GetBottom
const T & GetBottom() const
Get the bottom boundary of the rectangle along the Y direction.
Definition: vtkRect.h:124
vtkVector
templated base type for storage of vectors.
Definition: vtkVector.h:37
vtkVector2
Definition: vtkVector.h:149
vtkMath::Max
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1336
vtkRect::GetRight
T GetRight() const
Get the right boundary of the rectangle along the X direction.
Definition: vtkRect.h:114
vtkRect::GetTop
T GetTop() const
Get the top boundary of the rectangle along the Y direction.
Definition: vtkRect.h:119
vtkRect::GetLeft
const T & GetLeft() const
Get the left boundary of the rectangle along the X direction.
Definition: vtkRect.h:109
vtkRect::IntersectsWith
bool IntersectsWith(const vtkRect< T > &rect) const
Returns true if the rect argument overlaps this rect.
Definition: vtkRect.h:246
vtkRect
templated base type for storage of 2D rectangles.
Definition: vtkRect.h:35
vtkRect::Set
void Set(const T &x, const T &y, const T &width, const T &height)
Set the x, y components of the rectangle, and the width/height.
Definition: vtkRect.h:57
vtkVector.h
vtkRect::AddPoint
void AddPoint(T x, T y)
Expand this rect to contain the point passed in.
Definition: vtkRect.h:189
vtkX3D::top
Definition: vtkX3D.h:508
vtkRect::GetX
const T & GetX() const
Get the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:74
vtkRect::vtkRect
vtkRect(const T *init)
Definition: vtkRect.h:48
vtkRect::SetX
void SetX(const T &x)
Set the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:69
vtkRectd
Definition: vtkRect.h:340
vtkRectd::vtkRectd
vtkRectd(const double *init)
Definition: vtkRect.h:348
vtkRecti::vtkRecti
vtkRecti()
Definition: vtkRect.h:315
vtkRect::GetHeight
const T & GetHeight() const
Get the height of the rectangle, i.e.
Definition: vtkRect.h:104
vtkRect::vtkRect
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:40
vtkRect::SetHeight
void SetHeight(const T &height)
Set the height of the rectangle, i.e.
Definition: vtkRect.h:99
vtkRect::SetWidth
void SetWidth(const T &width)
Set the width of the rectanle, i.e.
Definition: vtkRect.h:89
vtkX3D::point
Definition: vtkX3D.h:242
vtkRect::SetY
void SetY(const T &y)
Set the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:79
vtkRecti::vtkRecti
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:316
vtkX3D::bottom
Definition: vtkX3D.h:296
vtkX3D::height
Definition: vtkX3D.h:260
vtkRect::GetTopLeft
vtkVector< T, 2 > GetTopLeft() const
Get the top left corner of the rect as a vtkVector.
Definition: vtkRect.h:134
vtkRectf::vtkRectf
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:330
vtkRect::GetBottomLeft
vtkVector2< T > GetBottomLeft() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:129
vtkMath::Min
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1329
vtkRect::Intersect
bool Intersect(const vtkRect< T > &other)
Intersect with other rectangle.
Definition: vtkRect.h:293
vtkRect::vtkRect
vtkRect()
Definition: vtkRect.h:38
vtkRect::AddPoint
void AddPoint(const T point[2])
Expand this rect to contain the point passed in.
Definition: vtkRect.h:153
vtkRect::AddRect
void AddRect(const vtkRect< T > &rect)
Expand this rect to contain the rect passed in.
Definition: vtkRect.h:200
vtkRect::GetWidth
const T & GetWidth() const
Get the width of the rectangle, i.e.
Definition: vtkRect.h:94
vtkRect::MoveTo
void MoveTo(T x, T y)
Move the rectangle, moving the bottom-left corner to the given position.
Definition: vtkRect.h:279
vtkRecti
Definition: vtkRect.h:312
vtkRectf
Definition: vtkRect.h:326
vtkRectf::vtkRectf
vtkRectf()
Definition: vtkRect.h:329
vtkRectd::vtkRectd
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:344
vtkRecti::vtkRecti
vtkRecti(const int *init)
Definition: vtkRect.h:320
vtkRect::GetBottomRight
vtkVector< T, 2 > GetBottomRight() const
Get the bottom right corner of the rect as a vtkVector.
Definition: vtkRect.h:139