VTK  9.3.20240328
vtkRect.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
18 #ifndef vtkRect_h
19 #define vtkRect_h
20 
21 #include "vtkVector.h"
22 
23 #include "vtkMath.h" // for Min, Max
24 
25 VTK_ABI_NAMESPACE_BEGIN
26 template <typename T>
27 class vtkRect : public vtkVector<T, 4>
28 {
29 public:
30  vtkRect() = default;
31 
32  vtkRect(const T& x, const T& y, const T& width, const T& height)
33  {
34  this->Data[0] = x;
35  this->Data[1] = y;
36  this->Data[2] = width;
37  this->Data[3] = height;
38  }
39 
40  explicit vtkRect(const T* init)
41  : vtkVector<T, 4>(init)
42  {
43  }
44 
46 
49  void Set(const T& x, const T& y, const T& width, const T& height)
50  {
51  this->Data[0] = x;
52  this->Data[1] = y;
53  this->Data[2] = width;
54  this->Data[3] = height;
55  }
57 
61  void SetX(const T& x) { this->Data[0] = x; }
62 
66  const T& GetX() const { return this->Data[0]; }
67 
71  void SetY(const T& y) { this->Data[1] = y; }
72 
76  const T& GetY() const { return this->Data[1]; }
77 
81  void SetWidth(const T& width) { this->Data[2] = width; }
82 
86  const T& GetWidth() const { return this->Data[2]; }
87 
91  void SetHeight(const T& height) { this->Data[3] = height; }
92 
96  const T& GetHeight() const { return this->Data[3]; }
97 
101  const T& GetLeft() const { return this->Data[0]; }
102 
106  T GetRight() const { return this->Data[0] + this->Data[2]; }
107 
111  T GetTop() const { return this->Data[1] + this->Data[3]; }
112 
116  const T& GetBottom() const { return this->Data[1]; }
117 
121  vtkVector2<T> GetBottomLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetBottom()); }
122 
126  vtkVector<T, 2> GetTopLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetTop()); }
127 
132  {
133  return vtkVector2<T>(this->GetRight(), this->GetBottom());
134  }
135 
139  vtkVector<T, 2> GetTopRight() const { return vtkVector2<T>(this->GetRight(), this->GetTop()); }
140 
142 
145  void AddPoint(const T point[2])
146  {
147  // This code is written like this to ensure that adding a point gives
148  // exactly the same result as AddRect(vtkRect(x,y,0,0)
149  if (point[0] < this->GetX())
150  {
151  T dx = this->GetX() - point[0];
152  this->SetX(point[0]);
153  this->SetWidth(dx + this->GetWidth());
154  }
155  else if (point[0] > this->GetX())
156  {
157  // this->GetX() is already correct
158  T dx = point[0] - this->GetX();
159  this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
160  }
162 
163  if (point[1] < this->GetY())
164  {
165  T dy = this->GetY() - point[1];
166  this->SetY(point[1]);
167  this->SetHeight(dy + this->GetHeight());
168  }
169  else if (point[1] > this->GetY())
170  {
171  // this->GetY() is already correct
172  T dy = point[1] - this->GetY();
173  this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
174  }
175  }
176 
178 
181  void AddPoint(T x, T y)
182  {
183  T point[2] = { x, y };
184  this->AddPoint(point);
185  }
187 
189 
192  void AddRect(const vtkRect<T>& rect)
193  {
194  if (rect.GetX() < this->GetX())
195  {
196  T dx = this->GetX() - rect.GetX();
197  this->SetX(rect.GetX());
198  this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
199  }
200  else if (rect.GetX() > this->GetX())
201  {
202  T dx = rect.GetX() - this->GetX();
203  // this->GetX() is already correct
204  this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
205  }
206  else
207  {
208  // this->GetX() is already correct
209  this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
210  }
212 
213  if (rect.GetY() < this->GetY())
214  {
215  T dy = this->GetY() - rect.GetY();
216  this->SetY(rect.GetY());
217  this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
218  }
219  else if (rect.GetY() > this->GetY())
220  {
221  T dy = rect.GetY() - this->GetY();
222  // this->GetY() is already correct
223  this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
224  }
225  else
226  {
227  // this->GetY() is already correct
228  this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
229  }
230  }
231 
238  bool IntersectsWith(const vtkRect<T>& rect) const
239  {
240  bool intersects = true;
241 
242  if (rect.GetX() < this->GetX())
243  {
244  T dx = this->GetX() - rect.GetX();
245  intersects &= (dx < rect.GetWidth());
246  }
247  else if (rect.GetX() > this->GetX())
248  {
249  T dx = rect.GetX() - this->GetX();
250  intersects &= (dx < this->GetWidth());
251  }
252 
253  if (rect.GetY() < this->GetY())
254  {
255  T dy = this->GetY() - rect.GetY();
256  intersects &= (dy < rect.GetHeight());
257  }
258  else if (rect.GetY() > this->GetY())
259  {
260  T dy = rect.GetY() - this->GetY();
261  intersects &= (dy < this->GetHeight());
262  }
263 
264  return intersects;
265  }
266 
271  void MoveTo(T x, T y)
272  {
273  this->Data[0] = x;
274  this->Data[1] = y;
275  }
276 
285  bool Intersect(const vtkRect<T>& other)
286  {
287  if (this->IntersectsWith(other))
288  {
289  const T left = vtkMath::Max(this->GetLeft(), other.GetLeft());
290  const T bottom = vtkMath::Max(this->GetBottom(), other.GetBottom());
291  const T right = vtkMath::Min(this->GetRight(), other.GetRight());
292  const T top = vtkMath::Min(this->GetTop(), other.GetTop());
293 
294  this->Data[0] = left;
295  this->Data[1] = bottom;
296  this->Data[2] = (right - left);
297  this->Data[3] = (top - bottom);
298  return true;
299  }
300  return false;
301  }
302 
307  {
308  return vtkVector2d(
309  this->GetX() + 0.5 * this->GetWidth(), this->GetY() + 0.5 * this->GetHeight());
310  }
311 };
312 
313 class vtkRecti : public vtkRect<int>
314 {
315 public:
316  vtkRecti() = default;
317  vtkRecti(int x, int y, int width, int height)
318  : vtkRect<int>(x, y, width, height)
319  {
320  }
321  explicit vtkRecti(const int* init)
322  : vtkRect<int>(init)
323  {
324  }
325 };
326 
327 class vtkRectf : public vtkRect<float>
328 {
329 public:
330  vtkRectf() = default;
331  vtkRectf(float x, float y, float width, float height)
332  : vtkRect<float>(x, y, width, height)
333  {
334  }
335  explicit vtkRectf(const float* init)
336  : vtkRect<float>(init)
337  {
338  }
339 };
340 
341 class vtkRectd : public vtkRect<double>
342 {
343 public:
344  vtkRectd() = default;
345  vtkRectd(double x, double y, double width, double height)
346  : vtkRect<double>(x, y, width, height)
347  {
348  }
349  explicit vtkRectd(const double* init)
350  : vtkRect<double>(init)
351  {
352  }
353 };
354 
355 VTK_ABI_NAMESPACE_END
356 #endif // vtkRect_h
357 // VTK-HeaderTest-Exclude: vtkRect.h
static T Max(const T &a, const T &b)
Returns the maximum of the two arguments provided.
Definition: vtkMath.h:1909
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1902
templated base type for storage of 2D rectangles.
Definition: vtkRect.h:28
vtkVector< T, 2 > GetTopRight() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:139
void SetY(const T &y)
Set the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:71
void AddPoint(T x, T y)
Expand this rect to contain the point passed in.
Definition: vtkRect.h:181
vtkRect()=default
void SetX(const T &x)
Set the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:61
vtkVector2< T > GetBottomLeft() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:121
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:49
vtkRect(const T *init)
Definition: vtkRect.h:40
const T & GetY() const
Get the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:76
const T & GetHeight() const
Get the height of the rectangle, i.e.
Definition: vtkRect.h:96
const T & GetX() const
Get the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:66
T GetRight() const
Get the right boundary of the rectangle along the X direction.
Definition: vtkRect.h:106
vtkVector< T, 2 > GetTopLeft() const
Get the top left corner of the rect as a vtkVector.
Definition: vtkRect.h:126
void SetHeight(const T &height)
Set the height of the rectangle, i.e.
Definition: vtkRect.h:91
bool IntersectsWith(const vtkRect< T > &rect) const
Returns true if the rect argument overlaps this rect.
Definition: vtkRect.h:238
void MoveTo(T x, T y)
Move the rectangle, moving the bottom-left corner to the given position.
Definition: vtkRect.h:271
void AddRect(const vtkRect< T > &rect)
Expand this rect to contain the rect passed in.
Definition: vtkRect.h:192
const T & GetBottom() const
Get the bottom boundary of the rectangle along the Y direction.
Definition: vtkRect.h:116
const T & GetLeft() const
Get the left boundary of the rectangle along the X direction.
Definition: vtkRect.h:101
T GetTop() const
Get the top boundary of the rectangle along the Y direction.
Definition: vtkRect.h:111
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:32
vtkVector2d GetCenter() const
Returns the center of the rect as a vtkVector2d.
Definition: vtkRect.h:306
const T & GetWidth() const
Get the width of the rectangle, i.e.
Definition: vtkRect.h:86
void AddPoint(const T point[2])
Expand this rect to contain the point passed in.
Definition: vtkRect.h:145
vtkVector< T, 2 > GetBottomRight() const
Get the bottom right corner of the rect as a vtkVector.
Definition: vtkRect.h:131
void SetWidth(const T &width)
Set the width of the rectanle, i.e.
Definition: vtkRect.h:81
bool Intersect(const vtkRect< T > &other)
Intersect with other rectangle.
Definition: vtkRect.h:285
vtkRectd(const double *init)
Definition: vtkRect.h:349
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:345
vtkRectd()=default
vtkRectf(const float *init)
Definition: vtkRect.h:335
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:331
vtkRectf()=default
vtkRecti()=default
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:317
vtkRecti(const int *init)
Definition: vtkRect.h:321
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:143
templated base type for storage of vectors.
Definition: vtkVector.h:59
@ point
Definition: vtkX3D.h:236
@ top
Definition: vtkX3D.h:502
@ bottom
Definition: vtkX3D.h:290
@ height
Definition: vtkX3D.h:254