VTK  9.2.20220926
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 
30 #ifndef vtkRect_h
31 #define vtkRect_h
32 
33 #include "vtkVector.h"
34 
35 #include "vtkMath.h" // for Min, Max
36 
37 VTK_ABI_NAMESPACE_BEGIN
38 template <typename T>
39 class vtkRect : public vtkVector<T, 4>
40 {
41 public:
42  vtkRect() = default;
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)
53  : vtkVector<T, 4>(init)
54  {
55  }
56 
58 
61  void Set(const T& x, const T& y, const T& width, const T& height)
62  {
63  this->Data[0] = x;
64  this->Data[1] = y;
65  this->Data[2] = width;
66  this->Data[3] = height;
67  }
69 
73  void SetX(const T& x) { this->Data[0] = x; }
74 
78  const T& GetX() const { return this->Data[0]; }
79 
83  void SetY(const T& y) { this->Data[1] = y; }
84 
88  const T& GetY() const { return this->Data[1]; }
89 
93  void SetWidth(const T& width) { this->Data[2] = width; }
94 
98  const T& GetWidth() const { return this->Data[2]; }
99 
103  void SetHeight(const T& height) { this->Data[3] = height; }
104 
108  const T& GetHeight() const { return this->Data[3]; }
109 
113  const T& GetLeft() const { return this->Data[0]; }
114 
118  T GetRight() const { return this->Data[0] + this->Data[2]; }
119 
123  T GetTop() const { return this->Data[1] + this->Data[3]; }
124 
128  const T& GetBottom() const { return this->Data[1]; }
129 
133  vtkVector2<T> GetBottomLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetBottom()); }
134 
138  vtkVector<T, 2> GetTopLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetTop()); }
139 
144  {
145  return vtkVector2<T>(this->GetRight(), this->GetBottom());
146  }
147 
151  vtkVector<T, 2> GetTopRight() const { return vtkVector2<T>(this->GetRight(), this->GetTop()); }
152 
154 
157  void AddPoint(const T point[2])
158  {
159  // This code is written like this to ensure that adding a point gives
160  // exactly the same result as AddRect(vtkRect(x,y,0,0)
161  if (point[0] < this->GetX())
162  {
163  T dx = this->GetX() - point[0];
164  this->SetX(point[0]);
165  this->SetWidth(dx + this->GetWidth());
166  }
167  else if (point[0] > this->GetX())
168  {
169  // this->GetX() is already correct
170  T dx = point[0] - this->GetX();
171  this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
172  }
174 
175  if (point[1] < this->GetY())
176  {
177  T dy = this->GetY() - point[1];
178  this->SetY(point[1]);
179  this->SetHeight(dy + this->GetHeight());
180  }
181  else if (point[1] > this->GetY())
182  {
183  // this->GetY() is already correct
184  T dy = point[1] - this->GetY();
185  this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
186  }
187  }
188 
190 
193  void AddPoint(T x, T y)
194  {
195  T point[2] = { x, y };
196  this->AddPoint(point);
197  }
199 
201 
204  void AddRect(const vtkRect<T>& rect)
205  {
206  if (rect.GetX() < this->GetX())
207  {
208  T dx = this->GetX() - rect.GetX();
209  this->SetX(rect.GetX());
210  this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
211  }
212  else if (rect.GetX() > this->GetX())
213  {
214  T dx = rect.GetX() - this->GetX();
215  // this->GetX() is already correct
216  this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
217  }
218  else
219  {
220  // this->GetX() is already correct
221  this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
222  }
224 
225  if (rect.GetY() < this->GetY())
226  {
227  T dy = this->GetY() - rect.GetY();
228  this->SetY(rect.GetY());
229  this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
230  }
231  else if (rect.GetY() > this->GetY())
232  {
233  T dy = rect.GetY() - this->GetY();
234  // this->GetY() is already correct
235  this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
236  }
237  else
238  {
239  // this->GetY() is already correct
240  this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
241  }
242  }
243 
250  bool IntersectsWith(const vtkRect<T>& rect) const
251  {
252  bool intersects = true;
253 
254  if (rect.GetX() < this->GetX())
255  {
256  T dx = this->GetX() - rect.GetX();
257  intersects &= (dx < rect.GetWidth());
258  }
259  else if (rect.GetX() > this->GetX())
260  {
261  T dx = rect.GetX() - this->GetX();
262  intersects &= (dx < this->GetWidth());
263  }
264 
265  if (rect.GetY() < this->GetY())
266  {
267  T dy = this->GetY() - rect.GetY();
268  intersects &= (dy < rect.GetHeight());
269  }
270  else if (rect.GetY() > this->GetY())
271  {
272  T dy = rect.GetY() - this->GetY();
273  intersects &= (dy < this->GetHeight());
274  }
275 
276  return intersects;
277  }
278 
283  void MoveTo(T x, T y)
284  {
285  this->Data[0] = x;
286  this->Data[1] = y;
287  }
288 
297  bool Intersect(const vtkRect<T>& other)
298  {
299  if (this->IntersectsWith(other))
300  {
301  const T left = vtkMath::Max(this->GetLeft(), other.GetLeft());
302  const T bottom = vtkMath::Max(this->GetBottom(), other.GetBottom());
303  const T right = vtkMath::Min(this->GetRight(), other.GetRight());
304  const T top = vtkMath::Min(this->GetTop(), other.GetTop());
305 
306  this->Data[0] = left;
307  this->Data[1] = bottom;
308  this->Data[2] = (right - left);
309  this->Data[3] = (top - bottom);
310  return true;
311  }
312  return false;
313  }
314 
319  {
320  return vtkVector2d(
321  this->GetX() + 0.5 * this->GetWidth(), this->GetY() + 0.5 * this->GetHeight());
322  }
323 };
324 
325 class vtkRecti : public vtkRect<int>
326 {
327 public:
328  vtkRecti() = default;
329  vtkRecti(int x, int y, int width, int height)
330  : vtkRect<int>(x, y, width, height)
331  {
332  }
333  explicit vtkRecti(const int* init)
334  : vtkRect<int>(init)
335  {
336  }
337 };
338 
339 class vtkRectf : public vtkRect<float>
340 {
341 public:
342  vtkRectf() = default;
343  vtkRectf(float x, float y, float width, float height)
344  : vtkRect<float>(x, y, width, height)
345  {
346  }
347  explicit vtkRectf(const float* init)
348  : vtkRect<float>(init)
349  {
350  }
351 };
352 
353 class vtkRectd : public vtkRect<double>
354 {
355 public:
356  vtkRectd() = default;
357  vtkRectd(double x, double y, double width, double height)
358  : vtkRect<double>(x, y, width, height)
359  {
360  }
361  explicit vtkRectd(const double* init)
362  : vtkRect<double>(init)
363  {
364  }
365 };
366 
368 #endif // vtkRect_h
369 // 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:1819
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1812
templated base type for storage of 2D rectangles.
Definition: vtkRect.h:40
vtkVector< T, 2 > GetTopRight() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:151
void SetY(const T &y)
Set the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:83
void AddPoint(T x, T y)
Expand this rect to contain the point passed in.
Definition: vtkRect.h:193
vtkRect()=default
void SetX(const T &x)
Set the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:73
vtkVector2< T > GetBottomLeft() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:133
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:61
vtkRect(const T *init)
Definition: vtkRect.h:52
const T & GetY() const
Get the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:88
const T & GetHeight() const
Get the height of the rectangle, i.e.
Definition: vtkRect.h:108
const T & GetX() const
Get the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:78
T GetRight() const
Get the right boundary of the rectangle along the X direction.
Definition: vtkRect.h:118
vtkVector< T, 2 > GetTopLeft() const
Get the top left corner of the rect as a vtkVector.
Definition: vtkRect.h:138
void SetHeight(const T &height)
Set the height of the rectangle, i.e.
Definition: vtkRect.h:103
bool IntersectsWith(const vtkRect< T > &rect) const
Returns true if the rect argument overlaps this rect.
Definition: vtkRect.h:250
void MoveTo(T x, T y)
Move the rectangle, moving the bottom-left corner to the given position.
Definition: vtkRect.h:283
void AddRect(const vtkRect< T > &rect)
Expand this rect to contain the rect passed in.
Definition: vtkRect.h:204
const T & GetBottom() const
Get the bottom boundary of the rectangle along the Y direction.
Definition: vtkRect.h:128
const T & GetLeft() const
Get the left boundary of the rectangle along the X direction.
Definition: vtkRect.h:113
T GetTop() const
Get the top boundary of the rectangle along the Y direction.
Definition: vtkRect.h:123
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:44
vtkVector2d GetCenter() const
Returns the center of the rect as a vtkVector2d.
Definition: vtkRect.h:318
const T & GetWidth() const
Get the width of the rectangle, i.e.
Definition: vtkRect.h:98
void AddPoint(const T point[2])
Expand this rect to contain the point passed in.
Definition: vtkRect.h:157
vtkVector< T, 2 > GetBottomRight() const
Get the bottom right corner of the rect as a vtkVector.
Definition: vtkRect.h:143
void SetWidth(const T &width)
Set the width of the rectanle, i.e.
Definition: vtkRect.h:93
bool Intersect(const vtkRect< T > &other)
Intersect with other rectangle.
Definition: vtkRect.h:297
vtkRectd(const double *init)
Definition: vtkRect.h:361
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:357
vtkRectd()=default
vtkRectf(const float *init)
Definition: vtkRect.h:347
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:343
vtkRectf()=default
vtkRecti()=default
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:329
vtkRecti(const int *init)
Definition: vtkRect.h:333
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:155
templated base type for storage of vectors.
Definition: vtkVector.h:101
@ point
Definition: vtkX3D.h:248
@ top
Definition: vtkX3D.h:514
@ bottom
Definition: vtkX3D.h:302
@ height
Definition: vtkX3D.h:266