Image Component Library (ICL)
Rect.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/Rect.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter, Robert Haschke **
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/Macros.h>
34 #include <ICLUtils/Point.h>
35 #include <ICLUtils/Size.h>
36 #include <stdio.h>
37 #include <algorithm>
38 #ifdef ICL_HAVE_IPP
39 #include <ipp.h>
40 #endif
41 
42 
43 namespace icl {
44  namespace utils{
45 
46  #ifndef ICL_HAVE_IPP
47  struct IppiRect {
49 
51  int x;
52 
54  int y;
55 
57  int width;
58 
60  int height;
61  };
62  #else
63  #endif
64 
66 
92  class Rect32f;
95  class ICLUtils_API Rect : public IppiRect{
96  public:
97 
99  static const Rect null;
100 
102  Rect(){
103  this->x = 0;
104  this->y = 0;
105  this->width = 0;
106  this->height = 0;
107  }
108 
110  Rect(int x, int y, int width, int height){
111  this->x = x;
112  this->y = y;
113  this->width = width;
114  this->height = height;
115  }
116 
118  Rect(const Point &p, const Size &s){
119  this->x = p.x;
120  this->y = p.y;
121  this->width = s.width;
122  this->height = s.height;
123  }
124 
126  Rect(const Rect &r){
127  this->x = r.x;
128  this->y = r.y;
129  this->width = r.width;
130  this->height = r.height;
131  }
132 
134  Rect(const Rect32f &other);
135 
137  bool isNull() const { return (*this)==null; }
138 
140  bool operator==(const Rect &s) const {
141  return x==s.x && y==s.y && width==s.width && height==s.height;
142  }
143 
145  bool operator!=(const Rect &s) const {
146  return x!=s.x || y!= s.y || width!=s.width || height!=s.height;
147  }
148 
150  Rect operator*(double d) const {
151  return Rect((int)(d*x),(int)(d*y),(int)(d*width),(int)(d*height));
152  }
153 
155  Rect operator/(double d) const {
156  return Rect((int)(x/d),(int)(y/d),(int)(width/d),(int)(height/d));
157  }
158 
159 
161  Rect operator+(const Size &s) const{
162  return Rect(x,y,width+s.width,height+s.height);
163  }
164 
166  Rect operator-(const Size &s) const{
167  return Rect(x,y,width-s.width,height-s.height);
168  }
169 
171  Rect& operator+=(const Size &s){
172  width+=s.width; height+=s.height; return *this;
173  }
174 
176  Rect& operator-=(const Size &s){
177  width-=s.width; height-=s.height; return *this;
178  }
179 
181  Rect operator+(const Point &p) const{
182  return Rect(x+p.x,y+p.y,width,height);
183  }
184 
186  Rect operator-(const Point &p) const{
187  return Rect(x-p.x,y-p.y,width,height);
188  }
189 
191  Rect& operator+=(const Point &p){
192  x+=p.x; y+=p.y; return *this;
193  }
194 
196  Rect& operator-=(const Point &p){
197  x-=p.x; y-=p.y; return *this;
198  }
199 
201  Rect& operator*=(double d){
202  x=(int)((float)x*d);
203  y=(int)((float)y*d);
204  width=(int)((float)width*d);
205  height=(int)((float)height*d);
206  return *this;
207  }
209  Rect& operator/=(double d){
210  x=(int)((float)x/d);
211  y=(int)((float)y/d);
212  width=(int)((float)width/d);
213  height=(int)((float)height/d);
214  return *this;
215  }
217  int getDim() const {return width*height;}
218 
220  Rect operator&(const Rect &r) const {
221  Point ul (iclMax(x, r.x), iclMax(y, r.y));
222  Point lr (iclMin(right(), r.right()), iclMin(bottom(), r.bottom()));
223  Rect result (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
224  if (result.width > 0 && result.height > 0) return result;
225  else return null;
226  }
227 
229  Rect &operator&=(const Rect &r){
230  (*this)=(*this)&r;
231  return *this;
232  }
233 
235  Rect operator|(const Rect &r) const {
236  Point ul (iclMin(x, r.x), iclMin(y, r.y));
237  Point lr (iclMax(right(), r.right()), iclMax(bottom(), r.bottom()));
238  return Rect (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
239  }
240 
242  Rect &operator|=(const Rect &r){
243  (*this)=(*this)|r;
244  return *this;
245  }
246 
248 
249  Rect normalized() const {
250  Rect r (*this);
251  if (r.width < 0) {r.x += r.width; r.width = -r.width; }
252  if (r.height < 0) {r.y += r.height; r.height = -r.height; }
253  return r;
254  }
255 
257  bool contains(const Rect &r) const {
258  return x<=r.x && y <= r.y && right() >= r.right() && bottom() >= r.bottom();
259  }
260 
262 
267  bool contains(int x, int y) const{
268  return this->x<=x && right()>x && this->y<=y && bottom()>y;
269  }
270 
272 
277  Rect &enlarge(int k){
278  x-=k; y-=k; width+=2*k; height+=2*k;
279  return *this;
280  }
281 
283 
284  Rect enlarged(int k) const{
285  return Rect(*this).enlarge(k);
286  }
287 
288 
290  Point ul() const {
291  return Point(x,y);
292  }
294  Point ll() const {
295  return Point(x,y+height);
296  }
298  Point ur() const {
299  return Point(x+width,y);
300  }
302  Point lr() const {
303  return Point(x+width,y+height);
304  }
305 
307  Point center() const {
308  return Point(x+width/2,y+height/2);
309  }
310 
312  int left() const { return x; }
313 
315  int right() const { return x+width; }
316 
318  int bottom() const { return y+height; }
319 
321  int top() const { return y; }
322 
324  Size getSize() const { return Size(width,height); }
325 
326  Rect transform(double xfac, double yfac) const {
327  return Rect((int)(xfac*x),(int)(yfac*y),(int)(xfac*width), (int)(yfac*height));
328  }
329  };
330 
332  ICLUtils_API std::ostream &operator<<(std::ostream &s, const Rect &r);
333 
335  ICLUtils_API std::istream &operator>>(std::istream &s, Rect &r);
336 
337 
338  } // namespace utils
339 } // namespace icl
340 
bool contains(int x, int y) const
returns if the Rect contains a given point (pixel-based)
Definition: Rect.h:267
Rect operator-(const Size &s) const
substracts a size for the rects size
Definition: Rect.h:166
bool isNull() const
checks wether the object instance is null, i.e. all elements are zero
Definition: Rect.h:137
undocument this line if you encounter any issues!
Definition: Any.h:37
Rect(const Point &p, const Size &s)
creates a new Rect with specified offset and size
Definition: Rect.h:118
Rect & operator-=(const Size &s)
substracs a size from the rects size
Definition: Rect.h:176
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
Rect operator|(const Rect &r) const
union of two Rects
Definition: Rect.h:235
Point ur() const
returns upper right point of the rect
Definition: Rect.h:298
Rect & operator+=(const Size &s)
adds a size to the rects size
Definition: Rect.h:171
Rect transform(double xfac, double yfac) const
Definition: Rect.h:326
Rect operator+(const Size &s) const
adds a size to the rects size
Definition: Rect.h:161
Rect()
default constructor
Definition: Rect.h:102
Rect & enlarge(int k)
let the rect grow by k pixles into each direction
Definition: Rect.h:277
#define iclMin(A, B)
Definition: Macros.h:204
int bottom() const
returns the position of the bottom border
Definition: Rect.h:318
int left() const
returns the left border position
Definition: Rect.h:312
int top() const
returns the position of the upper border
Definition: Rect.h:321
bool operator==(const Rect &s) const
checks if two rects are equal
Definition: Rect.h:140
Rect operator+(const Point &p) const
adds a Point to the rects offset
Definition: Rect.h:181
Rect & operator|=(const Rect &r)
inplace union of two rects
Definition: Rect.h:242
bool operator!=(const Rect &s) const
checks if two rects are not equal
Definition: Rect.h:145
Floating point precision implementation of the Rect class.
Definition: Rect32f.h:45
Point ul() const
returns upper left point of the rect
Definition: Rect.h:290
Point center() const
returns the center Point of the rect
Definition: Rect.h:307
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
int getDim() const
returns width*height
Definition: Rect.h:217
Rect normalized() const
rects with negative sizes are normalized to Positive sizes
Definition: Rect.h:249
Rect operator/(double d) const
scales all parameters of the rect by a double value
Definition: Rect.h:155
Size getSize() const
returns the size of the rect
Definition: Rect.h:324
Size class of the ICL.
Definition: Size.h:61
bool contains(const Rect &r) const
returns if a Rect containes another rect
Definition: Rect.h:257
#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.
int right() const
returns the right border position
Definition: Rect.h:315
Rect(const Rect &r)
create a deep copy of a rect
Definition: Rect.h:126
ICLUtils_API std::istream & operator>>(std::istream &s, Point &p)
istream operator
Rect & operator/=(double d)
scales all rect params inplace
Definition: Rect.h:209
Rect enlarged(int k) const
returns an enlarged instance of this rect
Definition: Rect.h:284
ICLQt_API ImgQ operator *(const ImgQ &a, const ImgQ &b)
multiplies two images pixel-wise
Rect & operator&=(const Rect &r)
inplace intersection of two rects
Definition: Rect.h:229
Point class of the ICL used e.g. for the Images ROI offset.
Definition: Point.h:58
Point ll() const
returns lower left point of the rect
Definition: Rect.h:294
Rect(int x, int y, int width, int height)
creates a defined Rect
Definition: Rect.h:110
Rect & operator-=(const Point &p)
substracts a Point from the rects offset
Definition: Rect.h:196
Rectangle class of the ICL used e.g. for the Images ROI-rect.
Definition: Rect.h:95
Rect & operator+=(const Point &p)
adds a Point to the rects offset
Definition: Rect.h:191
Rect operator-(const Point &p) const
substracts a Point from the rects offset
Definition: Rect.h:186
Point lr() const
returns lower right point of the rect
Definition: Rect.h:302