Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Rect.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Rect_INCLUDE_ONCE
33 #define Rect_INCLUDE_ONCE
34 
35 #include <vlCore/Vector2.hpp>
36 
37 namespace vl
38 {
42  template<typename T>
43  class Rect
44  {
45  public:
46  Rect(const Rect& other)
47  {
48  mX = other.x();
49  mY = other.y();
50  mWidth = other.width();
51  mHeight = other.height();
52  }
53  Rect()
54  {
55  // null Rect
56  mX = 0;
57  mY = 0;
58  mWidth = -1;
59  mHeight = -1;
60  }
61  Rect(T x, T y, T width, T height)
62  {
63  mX = x;
64  mY = y;
65  mWidth = width;
66  mHeight = height;
67  }
68 
69  T x() const { return mX; }
70  T y() const { return mY; }
71  T width() const { return mWidth; }
72  T height() const { return mHeight; }
73  T bottom() const { return mY; }
74  T left() const { return mX; }
75  T top() const { return mY+mHeight-1; }
76  T right() const { return mX+mWidth-1; }
77 
78  void setX(T x) { mX = x; }
79  void setY(T y) { mY = y; }
80  void setWidth(T w) { mWidth = w; }
81  void setHeight(T h) { mHeight = h; }
82 
83  bool isNull() const { return width() < 0 || height() < 0; }
84  bool isPoint() const { return width() == 0 && height() == 0; }
85 
86  Rect intersected(const Rect& other) const
87  {
88  if (isNull() || other.isNull())
89  return other;
90  T Ax1 = left();
91  T Ax2 = right();
92  T Ay1 = bottom();
93  T Ay2 = top();
94  T Bx1 = other.left();
95  T Bx2 = other.right();
96  T By1 = other.bottom();
97  T By2 = other.top();
98  if (Ax1 < Bx1) Ax1 = Bx1;
99  if (Ay1 < By1) Ay1 = By1;
100  if (Ax2 > Bx2) Ax2 = Bx2;
101  if (Ay2 > By2) Ay2 = By2;
102  return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
103  }
104 
105  Rect united(const Rect& other) const
106  {
107  if (other.isNull())
108  return *this;
109  if (isNull())
110  return other;
111  T Ax1 = left();
112  T Ax2 = right();
113  T Ay1 = bottom();
114  T Ay2 = top();
115  T Bx1 = other.left();
116  T Bx2 = other.right();
117  T By1 = other.bottom();
118  T By2 = other.top();
119  if (Ax1 > Bx1) Ax1 = Bx1;
120  if (Ay1 > By1) Ay1 = By1;
121  if (Ax2 < Bx2) Ax2 = Bx2;
122  if (Ay2 < By2) Ay2 = By2;
123  return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
124  }
125 
129  bool operator<(const Rect& other) const
130  {
131  if (mX != other.mX)
132  return mX < other.mX;
133  else
134  if (mWidth != other.mWidth)
135  return mWidth < other.mWidth;
136  else
137  if (mHeight != other.mHeight)
138  return mHeight < other.mHeight;
139  else
140  if (mY != other.mY)
141  return mY < other.mY;
142  else
143  return false;
144  }
145 
146  protected:
147  T mX;
148  T mY;
151  };
152 
163  class RectI: public Rect<int>
164  {
165  public:
166  RectI() {}
167  RectI(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; }
168  RectI(const Rect<int>& other) { *this = other; }
169  RectI(const RectI& other): Rect<int>(other) { *this = other; }
170  // operator Rect<int>() const { return Rect<int>(x(), y(), width(), height()); }
171  RectI& operator=(const Rect<int>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
172  RectI& operator=(const RectI& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
173  int top() const { return mY+mHeight-1; }
174  int right() const { return mX+mWidth-1; }
175  ivec2 bottomLeft() const { return ivec2(bottom(),left()); }
176  ivec2 topRight() const { return ivec2(top(),right()); }
177  };
178 
189  class RectF: public Rect<float>
190  {
191  public:
192  RectF() {}
193  RectF(float x, float y, float width, float height) { mX=x; mY=y; mWidth=width; mHeight=height; }
194  RectF(const RectF& other): Rect<float>(other) { *this = other; }
195  RectF(const Rect<float>& other) { *this = other; }
196  // operator Rect<float>() const { return Rect<float>(x(), y(), width(), height()); }
197  RectF& operator=(const Rect<float>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
198  RectF& operator=(const RectF& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
199  float top() const { return mY+mHeight; }
200  float right() const { return mX+mWidth; }
201  fvec2 bottomLeft() const { return fvec2(bottom(),left()); }
202  fvec2 topRight() const { return fvec2(top(),right()); }
203  void addPoint(const fvec2& p)
204  {
205  fvec2 tr = topRight();
206  fvec2 bl = bottomLeft();
207  if (p.x() < bl.x())
208  bl.x() = p.x();
209  if (p.y() < bl.y())
210  bl.y() = p.y();
211  if (p.x() > tr.x())
212  tr.x() = p.x();
213  if (p.y() > tr.y())
214  tr.y() = p.y();
215  mX = bl.x();
216  mY = bl.y();
217  mWidth = tr.x() - bl.x();
218  mHeight = tr.y() - bl.y();
219  }
220  };
221 }
222 
223 #endif
ivec2 bottomLeft() const
Definition: Rect.hpp:175
T mHeight
Definition: Rect.hpp:150
Vector2< int > ivec2
A 2 components vector with int precision.
Definition: Vector2.hpp:279
void setY(T y)
Definition: Rect.hpp:79
Rect(T x, T y, T width, T height)
Definition: Rect.hpp:61
void setHeight(T h)
Definition: Rect.hpp:81
RectI(int x, int y, int width, int height)
Definition: Rect.hpp:167
The RectI class represents a 2D rectangular area using int precision.
Definition: Rect.hpp:163
Rect(const Rect &other)
Definition: Rect.hpp:46
RectI & operator=(const Rect< int > &other)
Definition: Rect.hpp:171
T top() const
Definition: Rect.hpp:75
float right() const
Definition: Rect.hpp:200
Rect()
Definition: Rect.hpp:53
T right() const
Definition: Rect.hpp:76
void setX(T x)
Definition: Rect.hpp:78
void addPoint(const fvec2 &p)
Definition: Rect.hpp:203
bool isNull() const
Definition: Rect.hpp:83
RectI & operator=(const RectI &other)
Definition: Rect.hpp:172
Visualization Library main namespace.
T height() const
Definition: Rect.hpp:72
bool operator<(const Rect &other) const
Defines a sort of lexicographic sorting that make possible the use of the Rect class with STL contain...
Definition: Rect.hpp:129
T width() const
Definition: Rect.hpp:71
Vector2< float > fvec2
A 2 components vector with float precision.
Definition: Vector2.hpp:283
float top() const
Definition: Rect.hpp:199
The RectF class represents a 2D rectangular area using float precision.
Definition: Rect.hpp:189
RectF & operator=(const Rect< float > &other)
Definition: Rect.hpp:197
T mX
Definition: Rect.hpp:147
RectI()
Definition: Rect.hpp:166
RectI(const RectI &other)
Definition: Rect.hpp:169
RectF(const Rect< float > &other)
Definition: Rect.hpp:195
T x() const
Definition: Rect.hpp:69
T left() const
Definition: Rect.hpp:74
Rect intersected(const Rect &other) const
Definition: Rect.hpp:86
T mY
Definition: Rect.hpp:148
T y() const
Definition: Rect.hpp:70
RectF()
Definition: Rect.hpp:192
int right() const
Definition: Rect.hpp:174
RectI(const Rect< int > &other)
Definition: Rect.hpp:168
ivec2 topRight() const
Definition: Rect.hpp:176
int top() const
Definition: Rect.hpp:173
fvec2 topRight() const
Definition: Rect.hpp:202
T mWidth
Definition: Rect.hpp:149
bool isPoint() const
Definition: Rect.hpp:84
const T_Scalar & x() const
Definition: Vector2.hpp:133
void setWidth(T w)
Definition: Rect.hpp:80
RectF & operator=(const RectF &other)
Definition: Rect.hpp:198
RectF(float x, float y, float width, float height)
Definition: Rect.hpp:193
const T_Scalar & y() const
Definition: Vector2.hpp:134
Implements the common functions of RectI and RectF.
Definition: Rect.hpp:43
fvec2 bottomLeft() const
Definition: Rect.hpp:201
T bottom() const
Definition: Rect.hpp:73
Rect united(const Rect &other) const
Definition: Rect.hpp:105
RectF(const RectF &other)
Definition: Rect.hpp:194