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
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:
42 {
43 }
44
45 vtkRect(const T& x, const T& y, const T& width, const T& height)
46 {
47 this->Data[0] = x;
48 this->Data[1] = y;
49 this->Data[2] = width;
50 this->Data[3] = height;
51 }
52
53 explicit vtkRect(const T* init) : vtkVector<T, 4>(init) { }
54
56
59 void Set(const T& x, const T& y, const T& width, const T& height)
60 {
61 this->Data[0] = x;
62 this->Data[1] = y;
63 this->Data[2] = width;
64 this->Data[3] = height;
65 }
67
71 void SetX(const T& x) { this->Data[0] = x; }
72
76 const T& GetX() const { return this->Data[0]; }
77
81 void SetY(const T& y) { this->Data[1] = y; }
82
86 const T& GetY() const { return this->Data[1]; }
87
91 void SetWidth(const T& width) { this->Data[2] = width; }
92
96 const T& GetWidth() const { return this->Data[2]; }
97
101 void SetHeight(const T& height) { this->Data[3] = height; }
102
106 const T& GetHeight() const { return this->Data[3]; }
107
111 const T& GetLeft() const { return this->Data[0]; }
112
116 T GetRight() const { return this->Data[0] + this->Data[2]; }
117
121 T GetTop() const { return this->Data[1] + this->Data[3]; }
122
126 const T& GetBottom() const { return this->Data[1]; }
127
132 {
133 return vtkVector2<T>(this->GetLeft(), this->GetBottom());
134 }
135
140 {
141 return vtkVector2<T>(this->GetLeft(), this->GetTop());
142 }
143
148 {
149 return vtkVector2<T>(this->GetRight(), this->GetBottom());
150 }
151
156 {
157 return vtkVector2<T>(this->GetRight(), this->GetTop());
158 }
159
161
164 void AddPoint(const T point[2])
165 {
166 // This code is written like this to ensure that adding a point gives
167 // exactly the same result as AddRect(vtkRect(x,y,0,0)
168 if (point[0] < this->GetX())
169 {
170 T dx = this->GetX() - point[0];
171 this->SetX(point[0]);
172 this->SetWidth(dx + this->GetWidth());
173 }
174 else if (point[0] > this->GetX())
175 {
176 // this->GetX() is already correct
177 T dx = point[0] - this->GetX();
178 this->SetWidth(vtkMath::Max(dx, this->GetWidth()));
179 }
181
182 if (point[1] < this->GetY())
183 {
184 T dy = this->GetY() - point[1];
185 this->SetY(point[1]);
186 this->SetHeight(dy + this->GetHeight());
187 }
188 else if (point[1] > this->GetY())
189 {
190 // this->GetY() is already correct
191 T dy = point[1] - this->GetY();
192 this->SetHeight(vtkMath::Max(dy, this->GetHeight()));
193 }
194 }
195
197
200 void AddPoint(T x, T y)
201 {
202 T point[2] = {x, y};
203 this->AddPoint(point);
204 }
206
208
211 void AddRect(const vtkRect<T> & rect)
212 {
213 if (rect.GetX() < this->GetX())
214 {
215 T dx = this->GetX() - rect.GetX();
216 this->SetX(rect.GetX());
217 this->SetWidth(vtkMath::Max(dx + this->GetWidth(), rect.GetWidth()));
218 }
219 else if (rect.GetX() > this->GetX())
220 {
221 T dx = rect.GetX() - this->GetX();
222 // this->GetX() is already correct
223 this->SetWidth(vtkMath::Max(dx + rect.GetWidth(), this->GetWidth()));
224 }
225 else
226 {
227 // this->GetX() is already correct
228 this->SetWidth(vtkMath::Max(rect.GetWidth(), this->GetWidth()));
229 }
231
232 if (rect.GetY() < this->GetY())
233 {
234 T dy = this->GetY() - rect.GetY();
235 this->SetY(rect.GetY());
236 this->SetHeight(vtkMath::Max(dy + this->GetHeight(), rect.GetHeight()));
237 }
238 else if (rect.GetY() > this->GetY())
239 {
240 T dy = rect.GetY() - this->GetY();
241 // this->GetY() is already correct
242 this->SetHeight(vtkMath::Max(dy + rect.GetHeight(), this->GetHeight()));
243 }
244 else
245 {
246 // this->GetY() is already correct
247 this->SetHeight(vtkMath::Max(rect.GetHeight(), this->GetHeight()));
248 }
249 }
250
257 bool IntersectsWith(const vtkRect<T> & rect)
258 {
259 bool intersects = true;
260
261 if (rect.GetX() < this->GetX())
262 {
263 T dx = this->GetX() - rect.GetX();
264 intersects &= (dx < rect.GetWidth());
265 }
266 else if (rect.GetX() > this->GetX())
267 {
268 T dx = rect.GetX() - this->GetX();
269 intersects &= (dx < this->GetWidth());
270 }
271
272 if (rect.GetY() < this->GetY())
273 {
274 T dy = this->GetY() - rect.GetY();
275 intersects &= (dy < rect.GetHeight());
276 }
277 else if (rect.GetY() > this->GetY())
278 {
279 T dy = rect.GetY() - this->GetY();
280 intersects &= (dy < this->GetHeight());
281 }
282
283 return intersects;
284 }
285};
286
287class vtkRecti : public vtkRect<int>
288{
289public:
291 vtkRecti(int x, int y, int width, int height)
292 : vtkRect<int>(x, y, width, height) {}
293 explicit vtkRecti(const int *init) : vtkRect<int>(init) {}
294};
295
296class vtkRectf : public vtkRect<float>
297{
298public:
300 vtkRectf(float x, float y, float width, float height)
301 : vtkRect<float>(x, y, width, height) {}
302 explicit vtkRectf(const float *init) : vtkRect<float>(init) {}
303};
304
305class vtkRectd : public vtkRect<double>
306{
307public:
309 vtkRectd(double x, double y, double width, double height)
310 : vtkRect<double>(x, y, width, height) {}
311 explicit vtkRectd(const double *init) : vtkRect<double>(init) {}
312};
313
314#endif // vtkRect_h
315// VTK-HeaderTest-Exclude: vtkRect.h
static T Max(const T &a, const T &b)
Returns the maximum of the two arugments provided.
Definition: vtkMath.h:1268
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:131
vtkVector< T, 2 > GetBottomRight() const
Get the bottom right corner of the rect as a vtkVector.
Definition: vtkRect.h:147
void SetY(const T &y)
Set the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:81
void AddPoint(T x, T y)
Expand this rect to contain the point passed in.
Definition: vtkRect.h:200
const T & GetBottom() const
Get the bottom boundary of the rectangle along the Y direction.
Definition: vtkRect.h:126
void SetX(const T &x)
Set the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:71
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:59
vtkRect(const T *init)
Definition: vtkRect.h:53
T GetRight() const
Get the right boundary of the rectangle along the X direction.
Definition: vtkRect.h:116
vtkVector< T, 2 > GetTopLeft() const
Get the top left corner of the rect as a vtkVector.
Definition: vtkRect.h:139
void SetHeight(const T &height)
Set the height of the rectangle, i.e.
Definition: vtkRect.h:101
void AddRect(const vtkRect< T > &rect)
Expand this rect to contain the rect passed in.
Definition: vtkRect.h:211
const T & GetLeft() const
Get the left boundary of the rectangle along the X direction.
Definition: vtkRect.h:111
const T & GetX() const
Get the x component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:76
T GetTop() const
Get the top boundary of the rectangle along the Y direction.
Definition: vtkRect.h:121
vtkRect(const T &x, const T &y, const T &width, const T &height)
Definition: vtkRect.h:45
const T & GetHeight() const
Get the height of the rectangle, i.e.
Definition: vtkRect.h:106
bool IntersectsWith(const vtkRect< T > &rect)
Returns true if the rect argument overlaps this rect.
Definition: vtkRect.h:257
const T & GetWidth() const
Get the width of the rectangle, i.e.
Definition: vtkRect.h:96
const T & GetY() const
Get the y component of the rectangle bottom corner, i.e.
Definition: vtkRect.h:86
vtkVector< T, 2 > GetTopRight() const
Get the bottom left corner of the rect as a vtkVector.
Definition: vtkRect.h:155
void AddPoint(const T point[2])
Expand this rect to contain the point passed in.
Definition: vtkRect.h:164
void SetWidth(const T &width)
Set the width of the rectanle, i.e.
Definition: vtkRect.h:91
vtkRect()
Definition: vtkRect.h:41
vtkRectd()
Definition: vtkRect.h:308
vtkRectd(const double *init)
Definition: vtkRect.h:311
vtkRectd(double x, double y, double width, double height)
Definition: vtkRect.h:309
vtkRectf(const float *init)
Definition: vtkRect.h:302
vtkRectf(float x, float y, float width, float height)
Definition: vtkRect.h:300
vtkRectf()
Definition: vtkRect.h:299
vtkRecti(int x, int y, int width, int height)
Definition: vtkRect.h:291
vtkRecti()
Definition: vtkRect.h:290
vtkRecti(const int *init)
Definition: vtkRect.h:293
T Data[Size]
The only thing stored in memory!
Definition: vtkTuple.h:145
templated base type for storage of vectors.
Definition: vtkVector.h:41
@ point
Definition: vtkX3D.h:236
@ height
Definition: vtkX3D.h:254