Image Component Library (ICL)
Rect32f.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Rect32f.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Point32f.h>
35 #include <ICLUtils/Size32f.h>
36 #include <ICLUtils/Rect.h>
37 #include <stdio.h>
38 #include <algorithm>
39 
40 namespace icl {
41  namespace utils{
42 
43 
46  public:
47 
48  float x;
49  float y;
50  float width;
51  float height;
52 
54  static const Rect32f null;
55 
58  this->x = 0.0f;
59  this->y = 0.0f;
60  this->width = 0.0f;
61  this->height = 0.0f;
62  }
63 
65  Rect32f(float x, float y, float width, float height):
66  x(x),y(y),width(width),height(height){
67  this->x = x;
68  this->y = y;
69  this->width = width;
70  this->height = height;
71  }
72 
74  Rect32f(const Point32f &p, const Size32f &s){
75  this->x = p.x;
76  this->y = p.y;
77  this->width = s.width;
78  this->height = s.height;
79  }
80 
82  Rect32f(const Rect32f &r){
83  this->x = r.x;
84  this->y = r.y;
85  this->width = r.width;
86  this->height = r.height;
87  }
88 
90  Rect32f(const Rect &rect):
91  x((float)rect.x),
92  y((float)rect.y),
93  width((float)rect.width),
94  height((float)rect.height){
95  }
96 
98  bool isNull() const { return (*this)==null; }
99 
101  bool operator==(const Rect32f &s) const {
102  return x==s.x && y==s.y && width==s.width && height==s.height;
103  }
104 
106  bool operator!=(const Rect32f &s) const {
107  return x!=s.x || y!= s.y || width!=s.width || height!=s.height;
108  }
109 
111  Rect32f operator*(double d) const {
112  return Rect32f(d*x,d*y,d*width,d*height);
113  }
114 
116  Rect32f operator/(double d) const {
117  return Rect32f(d/x,d/y,d/width,d/height);
118  }
119 
122  width+=s.width; height+=s.height; return *this;
123  }
124 
127  width-=s.width; height-=s.height; return *this;
128  }
129 
132  x+=p.x; y+=p.y; return *this;
133  }
134 
137  x-=p.x; y-=p.y; return *this;
138  }
139 
141  Rect32f& operator*=(double d){
142  x*=d;
143  y*=d;
144  width*=d;
145  height*=d;
146  return *this;
147  }
149  Rect32f& operator/=(double d){
150  x/=d;
151  y/=d;
152  width/=d;
153  height/=d;
154  return *this;
155  }
157  float getDim() const {return width*height;}
158 
160  Rect32f operator&(const Rect32f &r) const {
161  Point32f ul (iclMax (x, r.x), iclMax (y, r.y));
162  Point32f lr (iclMin (right(), r.right()), iclMin (bottom(), r.bottom()));
163  Rect32f result (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
164  if (result.width > 0 && result.height > 0) return result;
165  else return null;
166  }
167 
170  (*this)=(*this)&r;
171  return *this;
172  }
173 
175  Rect32f operator|(const Rect32f &r) const {
176  Point32f ul (iclMin (x, r.x), iclMin (y, r.y));
177  Point32f lr (iclMax (right(), r.right()), iclMax (bottom(), r.bottom()));
178  return Rect32f (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
179  }
180 
183  (*this)=(*this)|r;
184  return *this;
185  }
186 
188 
189  Rect32f normalized() const {
190  Rect32f r (*this);
191  if (r.width < 0) {r.x += r.width; r.width = -r.width; }
192  if (r.height < 0) {r.y += r.height; r.height = -r.height; }
193  return r;
194  }
195 
197  bool contains(const Rect32f &r) const {
198  return x<=r.x && y <= r.y && right() >= r.right() && bottom() >= r.bottom();
199  }
200 
202  bool contains(float x, float y) const{
203  return this->x<=x && right()>=x && this->y<=y && bottom()>=y;
204  }
205 
207 
212  Rect32f &enlarge(float k){
213  x-=k; y-=k; width+=2*k; height+=2*k;
214  return *this;
215  }
216 
218 
219  Rect32f enlarged(float k) const{
220  return Rect32f(*this).enlarge(k);
221  }
222 
223 
225  Point32f ul() const {
226  return Point32f(x,y);
227  }
229  Point32f ll() const {
230  return Point32f(x,y+height);
231  }
233  Point32f ur() const {
234  return Point32f(x+width,y);
235  }
237  Point32f lr() const {
238  return Point32f(x+width,y+height);
239  }
240 
242  float left() const { return x; }
243 
245  float right() const { return x+width; }
246 
248  float bottom() const { return y+height; }
249 
251  float top() const { return y; }
252 
254  Size32f getSize() const { return Size32f(width,height); }
255 
257  Point32f center() const {
258  return Point32f(x+width/2,y+height/2);
259  }
260 
262  Rect32f transform(double xfac, double yfac) const {
263  return Rect32f(x*xfac,y*yfac,width*xfac,height*yfac);
264  }
265  };
266 
268  ICLUtils_API std::ostream &operator<<(std::ostream &s, const Rect32f &r);
269 
271  ICLUtils_API std::istream &operator>>(std::istream &s, Rect32f &r);
272 
273 
274  } // namespace utils
275 } // namespace icl
276 
float top() const
returns the position of the upper border
Definition: Rect32f.h:251
Rect32f(const Rect32f &r)
create a deep copy of a rect
Definition: Rect32f.h:82
undocument this line if you encounter any issues!
Definition: Any.h:37
Rect32f & operator&=(const Rect32f &r)
inplace intersection of two rects
Definition: Rect32f.h:169
Point32f center() const
returns the center point of the rect
Definition: Rect32f.h:257
float y
y position of this point
Definition: Point32f.h:48
float y
!< x pos (upper left)
Definition: Rect32f.h:49
Rect32f operator/(double d) const
scales all parameters of the rect by a double value
Definition: Rect32f.h:116
Point32f lr() const
returns lower right point of the rect
Definition: Rect32f.h:237
float right() const
returns the right border position
Definition: Rect32f.h:245
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
Rect32f & operator-=(const Size32f &s)
substracs a size to the rects size
Definition: Rect32f.h:126
bool operator==(const Rect32f &s) const
checks if two rects are equal
Definition: Rect32f.h:101
Rect32f(const Rect &rect)
create a floating point rect from given int-valued rect
Definition: Rect32f.h:90
bool contains(const Rect32f &r) const
returns if a Rect32f containes another rect
Definition: Rect32f.h:197
#define iclMin(A, B)
Definition: Macros.h:204
Rect32f()
default constructor
Definition: Rect32f.h:57
Rect32f & operator+=(const Size32f &s)
adds a size to the rects size
Definition: Rect32f.h:121
bool operator!=(const Rect32f &s) const
checks if two rects are not equal
Definition: Rect32f.h:106
Rect32f operator|(const Rect32f &r) const
union of two Rect32fs
Definition: Rect32f.h:175
float x
Definition: Rect32f.h:48
Rect32f & operator/=(double d)
scales all rect params inplace
Definition: Rect32f.h:149
Floating point precision implementation of the Rect class.
Definition: Rect32f.h:45
bool contains(float x, float y) const
returns if the Rect32f contains a given point
Definition: Rect32f.h:202
bool isNull() const
checks wether the object instance is null, i.e. all elements are zero
Definition: Rect32f.h:98
Size32f getSize() const
returns the size of the rect
Definition: Rect32f.h:254
float height
!< width of the rect
Definition: Rect32f.h:51
FixedMatrix< T, V_COLS, M_ROWS_AND_COLS > & operator *=(FixedMatrix< T, V_COLS, M_ROWS_AND_COLS > &v, const FixedMatrix< T, M_ROWS_AND_COLS, M_ROWS_AND_COLS > &m)
Matrix multiplication (inplace)
Definition: FixedMatrix.h:959
Rect32f(const Point32f &p, const Size32f &s)
creates a new Rect32f with specified offset and size
Definition: Rect32f.h:74
Rect32f enlarged(float k) const
returns an enlarged instance of this rect
Definition: Rect32f.h:219
Rect32f & enlarge(float k)
let the rect grow by k pixles into each direction
Definition: Rect32f.h:212
Point32f ur() const
returns upper right point of the rect
Definition: Rect32f.h:233
#define iclMax(A, B)
Definition: Macros.h:207
ICLUtils_API std::ostream & operator<<(std::ostream &s, const ConfigFile &cf)
Default ostream operator to put a ConfigFile into a stream.
ICLUtils_API std::istream & operator>>(std::istream &s, Point &p)
istream operator
float bottom() const
returns the position of the bottom border
Definition: Rect32f.h:248
Point32f ul() const
returns upper left point of the rect
Definition: Rect32f.h:225
ICLQt_API ImgQ operator *(const ImgQ &a, const ImgQ &b)
multiplies two images pixel-wise
float getDim() const
returns width*height
Definition: Rect32f.h:157
Single precission 3D Vectors Point class of the ICL.
Definition: Point32f.h:41
Rect32f & operator+=(const Point32f &p)
adds a Point to the rects offset
Definition: Rect32f.h:131
Rect32f & operator-=(const Point32f &p)
substracts a Point to the rects offset
Definition: Rect32f.h:136
Rect32f & operator|=(const Rect32f &r)
inplace union of two rects
Definition: Rect32f.h:182
Rect32f(float x, float y, float width, float height)
creates a defined Rect32f
Definition: Rect32f.h:65
Point32f ll() const
returns lower left point of the rect
Definition: Rect32f.h:229
float x
x position of this point
Definition: Point32f.h:45
float height
Definition: Size32f.h:47
float width
!< y pos (upper left)
Definition: Rect32f.h:50
ICLQt_API void rect(ImgQ &image, int x, int y, int w, int h, int rounding=0)
draws a rect into an image
Rect32f normalized() const
rects with negative sizes are normalized to Positive sizes
Definition: Rect32f.h:189
Size32f class of the ICL (float valued)
Definition: Size32f.h:40
float width
Definition: Size32f.h:44
Rectangle class of the ICL used e.g. for the Images ROI-rect.
Definition: Rect.h:95
Rect32f transform(double xfac, double yfac) const
multiplies the rect's x and width by xfac and y and height by yfac
Definition: Rect32f.h:262
float left() const
returns the left border position
Definition: Rect32f.h:242