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:
39  {
40  }
41 
42  vtkRect(const T& x, const T& y, const T& width, const T& height)
43  {
44  this->Data[0] = x;
45  this->Data[1] = y;
46  this->Data[2] = width;
47  this->Data[3] = height;
48  }
49 
50  explicit vtkRect(const T* init) : vtkVector<T, 4>(init) { }
51 
53 
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 
68  void SetX(const T& x) { this->Data[0] = x; }
69 
73  const T& GetX() const { return this->Data[0]; }
74 
78  void SetY(const T& y) { this->Data[1] = y; }
79 
83  const T& GetY() const { return this->Data[1]; }
84 
88  void SetWidth(const T& width) { this->Data[2] = width; }
89 
93  const T& GetWidth() const { return this->Data[2]; }
94 
98  void SetHeight(const T& height) { this->Data[3] = height; }
99 
103  const T& GetHeight() const { return this->Data[3]; }
104 
108  const T& GetLeft() const { return this->Data[0]; }
109 
113  T GetRight() const { return this->Data[0] + this->Data[2]; }
114 
118  T GetTop() const { return this->Data[1] + this->Data[3]; }
119 
123  const T& GetBottom() const { return this->Data[1]; }
124 
129  {
130  return vtkVector2<T>(this->GetLeft(), this->GetBottom());
131  }
132 
137  {
138  return vtkVector2<T>(this->GetLeft(), this->GetTop());
139  }
140 
145  {
146  return vtkVector2<T>(this->GetRight(), this->GetBottom());
147  }
148 
153  {
154  return vtkVector2<T>(this->GetRight(), this->GetTop());
155  }
156 
158 
161  void AddPoint(const T point[2])
162  {
163  // This code is written like this to ensure that adding a point gives
164  // exactly the same result as AddRect(vtkRect(x,y,0,0)
165  if (point[0] < this->GetX())
166  {
167  T dx = this->GetX() - point[0];
168  this->SetX(point[0]);
169  this->SetWidth(dx + this->GetWidth());
170  }
171  else if (point[0] > this->GetX())
172  {
173  // this->GetX() is already correct
174  T dx = point[0] - this->GetX();
175  this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
176  }
178 
179  if (point[1] < this->GetY())
180  {
181  T dy = this->GetY() - point[1];
182  this->SetY(point[1]);
183  this->SetHeight(dy + this->GetHeight());
184  }
185  else if (point[1] > this->GetY())
186  {
187  // this->GetY() is already correct
188  T dy = point[1] - this->GetY();
189  this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
190  }
191  }
192 
194 
197  void AddPoint(T x, T y)
198  {
199  T point[2] = {x, y};
200  this->AddPoint(point);
201  }
203 
205 
208  void AddRect(const vtkRect<T> & rect)
209  {
210  if (rect.GetX() < this->GetX())
211  {
212  T dx = this->GetX() - rect.GetX();
213  this->SetX(rect.GetX());
214  this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
215  }
216  else if (rect.GetX() > this->GetX())
217  {
218  T dx = rect.GetX() - this->GetX();
219  // this->GetX() is already correct
220  this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
221  }
222  else
223  {
224  // this->GetX() is already correct
225  this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
226  }
228 
229  if (rect.GetY() < this->GetY())
230  {
231  T dy = this->GetY() - rect.GetY();
232  this->SetY(rect.GetY());
233  this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
234  }
235  else if (rect.GetY() > this->GetY())
236  {
237  T dy = rect.GetY() - this->GetY();
238  // this->GetY() is already correct
239  this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
240  }
241  else
242  {
243  // this->GetY() is already correct
244  this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
245  }
246  }
247 
254  bool IntersectsWith(const vtkRect<T> & rect)
255  {
256  bool intersects = true;
257 
258  if (rect.GetX() < this->GetX())
259  {
260  T dx = this->GetX() - rect.GetX();
261  intersects &= (dx < rect.GetWidth());
262  }
263  else if (rect.GetX() > this->GetX())
264  {
265  T dx = rect.GetX() - this->GetX();
266  intersects &= (dx < this->GetWidth());
267  }
268 
269  if (rect.GetY() < this->GetY())
270  {
271  T dy = this->GetY() - rect.GetY();
272  intersects &= (dy < rect.GetHeight());
273  }
274  else if (rect.GetY() > this->GetY())
275  {
276  T dy = rect.GetY() - this->GetY();
277  intersects &= (dy < this->GetHeight());
278  }
279 
280  return intersects;
281  }
282 };
283 
284 class vtkRecti : public vtkRect<int>
285 {
286 public:
287  vtkRecti() {}
288  vtkRecti(int x, int y, int width, int height)
289  : vtkRect<int>(x, y, width, height) {}
290  explicit vtkRecti(const int *init) : vtkRect<int>(init) {}
291 };
292 
293 class vtkRectf : public vtkRect<float>
294 {
295 public:
296  vtkRectf() {}
297  vtkRectf(float x, float y, float width, float height)
298  : vtkRect<float>(x, y, width, height) {}
299  explicit vtkRectf(const float *init) : vtkRect<float>(init) {}
300 };
301 
302 class vtkRectd : public vtkRect<double>
303 {
304 public:
305  vtkRectd() {}
306  vtkRectd(double x, double y, double width, double height)
307  : vtkRect<double>(x, y, width, height) {}
308  explicit vtkRectd(const double *init) : vtkRect<double>(init) {}
309 };
310 
311 #endif // vtkRect_h
312 // VTK-HeaderTest-Exclude: vtkRect.h
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:145
const T & GetBottom() const
Get the bottom boundary of the rectangle along the Y direction.
Definition: vtkRect.h:123
vtkRectf()
Definition: vtkRect.h:296
templated base type for storage of vectors.
Definition: vtkVector.h:37
void SetY(const T &y)
Set the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:78
vtkRectd(const double *init)
Definition: vtkRect.h:308
templated base type for storage of 2D rectangles.
Definition: vtkRect.h:35
void AddPoint(const T point[2])
Expand this rect to contain the point passed in.
Definition: vtkRect.h:161
const T & GetX() const
Get the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:73
vtkVector< T, 2 > GetTopLeft() const
Get the top left corner of the rect as a vtkVector.
Definition: vtkRect.h:136
const T & GetY() const
Get the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:83
vtkVector< T, 2 > GetBottomRight() const
Get the bottom right corner of the rect as a vtkVector.
Definition: vtkRect.h:144
bool IntersectsWith(const vtkRect< T > &rect)
Returns true if the rect argument overlaps this rect.
Definition: vtkRect.h:254
vtkRecti()
Definition: vtkRect.h:287
void AddRect(const vtkRect< T > &rect)
Expand this rect to contain the rect passed in.
Definition: vtkRect.h:208
void SetWidth(const T &width)
Set the width of the rectanle, i.e.
Definition: vtkRect.h:88
vtkRect()
Definition: vtkRect.h:38
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:56
const T & GetHeight() const
Get the height of the rectangle, i.e.
Definition: vtkRect.h:103
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:297
const T & GetWidth() const
Get the width of the rectangle, i.e.
Definition: vtkRect.h:93
vtkVector2< T > GetBottomLeft() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:128
vtkRect(const T *init)
Definition: vtkRect.h:50
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:288
void AddPoint(T x, T y)
Expand this rect to contain the point passed in.
Definition: vtkRect.h:197
vtkRecti(const int *init)
Definition: vtkRect.h:290
void SetHeight(const T &height)
Set the height of the rectangle, i.e.
Definition: vtkRect.h:98
void SetX(const T &x)
Set the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:68
T GetRight() const
Get the right boundary of the rectangle along the X direction.
Definition: vtkRect.h:113
vtkVector< T, 2 > GetTopRight() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:152
vtkRectd()
Definition: vtkRect.h:305
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:306
T GetTop() const
Get the top boundary of the rectangle along the Y direction.
Definition: vtkRect.h:118
static T Max(const T &a, const T &b)
Returns the maximum of the two arugments provided.
Definition: vtkMath.h:1305
const T & GetLeft() const
Get the left boundary of the rectangle along the X direction.
Definition: vtkRect.h:108
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:42
vtkRectf(const float *init)
Definition: vtkRect.h:299