Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Rect.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/Rect.h                           **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter, Robert Haschke                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/Macros.h>
00034 #include <ICLUtils/Point.h>
00035 #include <ICLUtils/Size.h>
00036 #include <stdio.h>
00037 #include <algorithm>
00038 #ifdef ICL_HAVE_IPP
00039 #include <ipp.h>
00040 #endif
00041 
00042 
00043 namespace icl {
00044   namespace utils{
00045     
00046   #ifndef ICL_HAVE_IPP
00047 
00048     struct IppiRect {
00049   
00051       int x;
00052       
00054       int y;
00055   
00057       int width;
00058   
00060       int height;
00061     };
00062   #else
00063   #endif
00064     
00066 
00092     class Rect32f;
00095     class ICLUtils_API Rect : public IppiRect{
00096       public:
00097       
00099       static const Rect null;
00100 
00102             Rect(){
00103                     this->x = 0;
00104                     this->y = 0;
00105                     this->width = 0;
00106                     this->height = 0;
00107             }
00108 
00110       Rect(int x, int y, int width, int height){
00111         this->x = x;
00112         this->y = y;
00113         this->width = width;
00114         this->height = height;
00115       }
00116       
00118       Rect(const Point &p, const Size &s){
00119         this->x = p.x;
00120         this->y = p.y;
00121         this->width = s.width;
00122         this->height = s.height;
00123       } 
00124       
00126       Rect(const Rect &r){
00127         this->x = r.x;
00128         this->y = r.y;
00129         this->width = r.width;
00130         this->height = r.height;
00131       }
00132   
00134       Rect(const Rect32f &other);
00135   
00137       bool isNull() const { return (*this)==null; }
00138   
00140       bool operator==(const Rect &s) const {
00141         return x==s.x && y==s.y && width==s.width && height==s.height;
00142       }
00143   
00145       bool operator!=(const Rect &s) const {
00146         return x!=s.x || y!= s.y || width!=s.width || height!=s.height;
00147       }
00148   
00150       Rect operator*(double d) const {
00151         return Rect((int)(d*x),(int)(d*y),(int)(d*width),(int)(d*height));
00152       }
00153   
00155       Rect operator/(double d) const {
00156         return Rect((int)(x/d),(int)(y/d),(int)(width/d),(int)(height/d));
00157       }
00158   
00159      
00161       Rect operator+(const Size &s) const{
00162         return Rect(x,y,width+s.width,height+s.height);
00163       }
00164   
00166       Rect operator-(const Size &s) const{
00167         return Rect(x,y,width-s.width,height-s.height);
00168       }
00169       
00171       Rect& operator+=(const Size &s){
00172         width+=s.width; height+=s.height; return *this;
00173       }
00174   
00176       Rect& operator-=(const Size &s){
00177         width-=s.width; height-=s.height; return *this;
00178       }
00179       
00181       Rect operator+(const Point &p) const{
00182         return Rect(x+p.x,y+p.y,width,height);
00183       }
00184   
00186       Rect operator-(const Point &p) const{
00187         return Rect(x-p.x,y-p.y,width,height);
00188       }
00189       
00191       Rect& operator+=(const Point &p){
00192         x+=p.x; y+=p.y; return *this;
00193       }
00194   
00196       Rect& operator-=(const Point &p){
00197         x-=p.x; y-=p.y; return *this;
00198       }
00199       
00201       Rect& operator*=(double d){
00202         x=(int)((float)x*d); 
00203         y=(int)((float)y*d);
00204         width=(int)((float)width*d); 
00205         height=(int)((float)height*d); 
00206         return *this;
00207       }
00209       Rect& operator/=(double d){
00210         x=(int)((float)x/d); 
00211         y=(int)((float)y/d);
00212         width=(int)((float)width/d); 
00213         height=(int)((float)height/d); 
00214         return *this;
00215       }
00217       int getDim() const {return width*height;}
00218 
00220       Rect operator&(const Rect &r) const {
00221          Point ul (iclMax(x, r.x), iclMax(y, r.y));
00222          Point lr (iclMin(right(), r.right()), iclMin(bottom(), r.bottom()));
00223          Rect result (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
00224          if (result.width > 0 && result.height > 0) return result;
00225          else return null;
00226       }
00227 
00229       Rect &operator&=(const Rect &r){
00230         (*this)=(*this)&r;
00231         return *this;
00232       }
00233 
00235       Rect operator|(const Rect &r) const {
00236          Point ul (iclMin(x, r.x), iclMin(y, r.y));
00237          Point lr (iclMax(right(), r.right()), iclMax(bottom(), r.bottom()));
00238          return Rect (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
00239       }
00240 
00242       Rect &operator|=(const Rect &r){
00243         (*this)=(*this)|r;
00244         return *this;
00245       }
00246       
00248 
00249       Rect normalized() const {
00250          Rect r (*this);
00251          if (r.width < 0) {r.x += r.width; r.width = -r.width; }
00252          if (r.height < 0) {r.y += r.height; r.height = -r.height; }
00253          return r;
00254       }
00255       
00257       bool contains(const Rect &r) const {
00258          return x<=r.x && y <= r.y && right() >= r.right() && bottom() >= r.bottom();
00259       }
00260       
00262 
00267       bool contains(int x, int y) const{
00268         return this->x<=x && right()>x && this->y<=y && bottom()>y;
00269       }
00270   
00272 
00277       Rect &enlarge(int k){
00278         x-=k; y-=k; width+=2*k; height+=2*k;
00279         return *this;
00280       }
00281       
00283 
00284       Rect enlarged(int k) const{
00285         return Rect(*this).enlarge(k);
00286       }
00287       
00288       
00290       Point ul() const {
00291         return Point(x,y);
00292       }
00294       Point ll() const {
00295         return Point(x,y+height);
00296       }
00298       Point ur() const {
00299         return Point(x+width,y);
00300       }
00302       Point lr() const {
00303         return Point(x+width,y+height);
00304       }
00305   
00307       Point center() const {
00308         return Point(x+width/2,y+height/2);
00309       }
00310       
00312       int left() const { return x; }
00313   
00315       int right() const { return x+width; }
00316   
00318       int bottom() const { return y+height; }
00319   
00321       int top() const { return y; }
00322   
00324       Size getSize() const { return Size(width,height); }
00325   
00326       Rect transform(double xfac, double yfac) const { 
00327         return Rect((int)(xfac*x),(int)(yfac*y),(int)(xfac*width), (int)(yfac*height)); 
00328       }
00329     };
00330   
00332     ICLUtils_API std::ostream &operator<<(std::ostream &s, const Rect &r);
00333     
00335     ICLUtils_API std::istream &operator>>(std::istream &s, Rect &r);
00336   
00337   
00338   } // namespace utils
00339 } // namespace icl
00340 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines