VTK  9.2.5
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
37template <typename T>
38class vtkRect : public vtkVector<T, 4>
39{
40public:
41 vtkRect() = default;
42
43 vtkRect(const T& x, const T& y, const T& width, const T& height)
44 {
45 this->Data[0] = x;
46 this->Data[1] = y;
47 this->Data[2] = width;
48 this->Data[3] = height;
49 }
50
51 explicit vtkRect(const T* init)
52 : vtkVector<T, 4>(init)
53 {
54 }
55
57
60 void Set(const T& x, const T& y, const T& width, const T& height)
61 {
62 this->Data[0] = x;
63 this->Data[1] = y;
64 this->Data[2] = width;
65 this->Data[3] = height;
66 }
68
72 void SetX(const T& x) { this->Data[0] = x; }
73
77 const T& GetX() const { return this->Data[0]; }
78
82 void SetY(const T& y) { this->Data[1] = y; }
83
87 const T& GetY() const { return this->Data[1]; }
88
92 void SetWidth(const T& width) { this->Data[2] = width; }
93
97 const T& GetWidth() const { return this->Data[2]; }
98
102 void SetHeight(const T& height) { this->Data[3] = height; }
103
107 const T& GetHeight() const { return this->Data[3]; }
108
112 const T& GetLeft() const { return this->Data[0]; }
113
117 T GetRight() const { return this->Data[0] + this->Data[2]; }
118
122 T GetTop() const { return this->Data[1] + this->Data[3]; }
123
127 const T& GetBottom() const { return this->Data[1]; }
128
132 vtkVector2<T> GetBottomLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetBottom()); }
133
137 vtkVector<T, 2> GetTopLeft() const { return vtkVector2<T>(this->GetLeft(), this->GetTop()); }
138
143 {
144 return vtkVector2<T>(this->GetRight(), this->GetBottom());
145 }
146
150 vtkVector<T, 2> GetTopRight() const { return vtkVector2<T>(this->GetRight(), this->GetTop()); }
151
153
156 void AddPoint(const T point[2])
157 {
158 // This code is written like this to ensure that adding a point gives
159 // exactly the same result as AddRect(vtkRect(x,y,0,0)
160 if (point[0] < this->GetX())
161 {
162 T dx = this->GetX() - point[0];
163 this->SetX(point[0]);
164 this->SetWidth(dx + this->GetWidth());
165 }
166 else if (point[0] > this->GetX())
167 {
168 // this->GetX() is already correct
169 T dx = point[0] - this->GetX();
170 this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
171 }
173
174 if (point[1] < this->GetY())
175 {
176 T dy = this->GetY() - point[1];
177 this->SetY(point[1]);
178 this->SetHeight(dy + this->GetHeight());
179 }
180 else if (point[1] > this->GetY())
181 {
182 // this->GetY() is already correct
183 T dy = point[1] - this->GetY();
184 this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
185 }
186 }
187
189
192 void AddPoint(T x, T y)
193 {
194 T point[2] = { x, y };
195 this->AddPoint(point);
196 }
198
200
203 void AddRect(const vtkRect<T>& rect)
204 {
205 if (rect.GetX() < this->GetX())
206 {
207 T dx = this->GetX() - rect.GetX();
208 this->SetX(rect.GetX());
209 this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
210 }
211 else if (rect.GetX() > this->GetX())
212 {
213 T dx = rect.GetX() - this->GetX();
214 // this->GetX() is already correct
215 this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
216 }
217 else
218 {
219 // this->GetX() is already correct
220 this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
221 }
223
224 if (rect.GetY() < this->GetY())
225 {
226 T dy = this->GetY() - rect.GetY();
227 this->SetY(rect.GetY());
228 this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
229 }
230 else if (rect.GetY() > this->GetY())
231 {
232 T dy = rect.GetY() - this->GetY();
233 // this->GetY() is already correct
234 this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
235 }
236 else
237 {
238 // this->GetY() is already correct
239 this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
240 }
241 }
242
249 bool IntersectsWith(const vtkRect<T>& rect) const
250 {
251 bool intersects = true;
252
253 if (rect.GetX() < this->GetX())
254 {
255 T dx = this->GetX() - rect.GetX();
256 intersects &= (dx < rect.GetWidth());
257 }
258 else if (rect.GetX() > this->GetX())
259 {
260 T dx = rect.GetX() - this->GetX();
261 intersects &= (dx < this->GetWidth());
262 }
263
264 if (rect.GetY() < this->GetY())
265 {
266 T dy = this->GetY() - rect.GetY();
267 intersects &= (dy < rect.GetHeight());
268 }
269 else if (rect.GetY() > this->GetY())
270 {
271 T dy = rect.GetY() - this->GetY();
272 intersects &= (dy < this->GetHeight());
273 }
274
275 return intersects;
276 }
277
282 void MoveTo(T x, T y)
283 {
284 this->Data[0] = x;
285 this->Data[1] = y;
286 }
287
296 bool Intersect(const vtkRect<T>& other)
297 {
298 if (this->IntersectsWith(other))
299 {
300 const T left = vtkMath::Max(this->GetLeft(), other.GetLeft());
301 const T bottom = vtkMath::Max(this->GetBottom(), other.GetBottom());
302 const T right = vtkMath::Min(this->GetRight(), other.GetRight());
303 const T top = vtkMath::Min(this->GetTop(), other.GetTop());
304
305 this->Data[0] = left;
306 this->Data[1] = bottom;
307 this->Data[2] = (right - left);
308 this->Data[3] = (top - bottom);
309 return true;
310 }
311 return false;
312 }
313
318 {
319 return vtkVector2d(
320 this->GetX() + 0.5 * this->GetWidth(), this->GetY() + 0.5 * this->GetHeight());
321 }
322};
323
324class vtkRecti : public vtkRect<int>
325{
326public:
327 vtkRecti() = default;
328 vtkRecti(int x, int y, int width, int height)
329 : vtkRect<int>(x, y, width, height)
330 {
331 }
332 explicit vtkRecti(const int* init)
333 : vtkRect<int>(init)
334 {
335 }
336};
337
338class vtkRectf : public vtkRect<float>
339{
340public:
341 vtkRectf() = default;
342 vtkRectf(float x, float y, float width, float height)
343 : vtkRect<float>(x, y, width, height)
344 {
345 }
346 explicit vtkRectf(const float* init)
347 : vtkRect<float>(init)
348 {
349 }
350};
351
352class vtkRectd : public vtkRect<double>
353{
354public:
355 vtkRectd() = default;
356 vtkRectd(double x, double y, double width, double height)
357 : vtkRect<double>(x, y, width, height)
358 {
359 }
360 explicit vtkRectd(const double* init)
361 : vtkRect<double>(init)
362 {
363 }
364};
365
366#endif // vtkRect_h
367// 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:1695
static T Min(const T &a, const T &b)
Returns the minimum of the two arguments provided.
Definition: vtkMath.h:1688
templated base type for storage of 2D rectangles.
Definition: vtkRect.h:39
vtkVector2< T > GetBottomLeft() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:132
vtkVector< T, 2 > GetBottomRight() const
Get the bottom right corner of the rect as a vtkVector.
Definition: vtkRect.h:142
void SetY(const T &y)
Set the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:82
void AddPoint(T x, T y)
Expand this rect to contain the point passed in.
Definition: vtkRect.h:192
const T & GetBottom() const
Get the bottom boundary of the rectangle along the Y direction.
Definition: vtkRect.h:127
vtkRect()=default
void SetX(const T &x)
Set the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:72
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:60
vtkRect(const T *init)
Definition: vtkRect.h:51
T GetRight() const
Get the right boundary of the rectangle along the X direction.
Definition: vtkRect.h:117
vtkVector< T, 2 > GetTopLeft() const
Get the top left corner of the rect as a vtkVector.
Definition: vtkRect.h:137
void SetHeight(const T &height)
Set the height of the rectangle, i.e.
Definition: vtkRect.h:102
bool IntersectsWith(const vtkRect< T > &rect) const
Returns true if the rect argument overlaps this rect.
Definition: vtkRect.h:249
void MoveTo(T x, T y)
Move the rectangle, moving the bottom-left corner to the given position.
Definition: vtkRect.h:282
void AddRect(const vtkRect< T > &rect)
Expand this rect to contain the rect passed in.
Definition: vtkRect.h:203
const T & GetLeft() const
Get the left boundary of the rectangle along the X direction.
Definition: vtkRect.h:112
const T & GetX() const
Get the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:77
T GetTop() const
Get the top boundary of the rectangle along the Y direction.
Definition: vtkRect.h:122
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:43
const T & GetHeight() const
Get the height of the rectangle, i.e.
Definition: vtkRect.h:107
const T & GetWidth() const
Get the width of the rectangle, i.e.
Definition: vtkRect.h:97
vtkVector2d GetCenter() const
Returns the center of the rect as a vtkVector2d.
Definition: vtkRect.h:317
const T & GetY() const
Get the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:87
vtkVector< T, 2 > GetTopRight() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:150
void AddPoint(const T point[2])
Expand this rect to contain the point passed in.
Definition: vtkRect.h:156
void SetWidth(const T &width)
Set the width of the rectanle, i.e.
Definition: vtkRect.h:92
bool Intersect(const vtkRect< T > &other)
Intersect with other rectangle.
Definition: vtkRect.h:296
vtkRectd(const double *init)
Definition: vtkRect.h:360
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:356
vtkRectd()=default
vtkRectf(const float *init)
Definition: vtkRect.h:346
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:342
vtkRectf()=default
vtkRecti()=default
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:328
vtkRecti(const int *init)
Definition: vtkRect.h:332
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:154
templated base type for storage of vectors.
Definition: vtkVector.h:41