Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Rect32f.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/Rect32f.h                        **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter                                    **
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/CompatMacros.h>
00034 #include <ICLUtils/Point32f.h>
00035 #include <ICLUtils/Size32f.h>
00036 #include <ICLUtils/Rect.h>
00037 #include <stdio.h>
00038 #include <algorithm>
00039 
00040 namespace icl {
00041   namespace utils{
00042     
00043     
00045     class ICLUtils_API Rect32f{
00046       public:
00047      
00048       float x;      
00049       float y;      
00050       float width;  
00051       float height; 
00052   
00054       static const Rect32f null;
00055 
00057             Rect32f(){
00058                     this->x = 0.0f;
00059                     this->y = 0.0f;
00060                     this->width = 0.0f;
00061                     this->height = 0.0f;
00062             }
00063 
00065       Rect32f(float x, float y, float width, float height):
00066       x(x),y(y),width(width),height(height){
00067         this->x = x;
00068         this->y = y;
00069         this->width = width;
00070         this->height = height;
00071       }
00072       
00074       Rect32f(const Point32f &p, const Size32f &s){
00075         this->x = p.x;
00076         this->y = p.y;
00077         this->width = s.width;
00078         this->height = s.height;
00079       } 
00080       
00082       Rect32f(const Rect32f &r){
00083         this->x = r.x;
00084         this->y = r.y;
00085         this->width = r.width;
00086         this->height = r.height;
00087       }
00088       
00090       Rect32f(const Rect &rect):
00091         x((float)rect.x),
00092         y((float)rect.y),
00093         width((float)rect.width),
00094         height((float)rect.height){
00095       }
00096   
00098       bool isNull() const { return (*this)==null; }
00099   
00101       bool operator==(const Rect32f &s) const {
00102         return x==s.x && y==s.y && width==s.width && height==s.height;
00103       }
00104   
00106       bool operator!=(const Rect32f &s) const {
00107         return x!=s.x || y!= s.y || width!=s.width || height!=s.height;
00108       }
00109   
00111       Rect32f operator*(double d) const {
00112         return Rect32f(d*x,d*y,d*width,d*height);
00113       }
00114   
00116       Rect32f operator/(double d) const {
00117         return Rect32f(d/x,d/y,d/width,d/height);
00118       }
00119   
00121       Rect32f& operator+=(const Size32f &s){
00122         width+=s.width; height+=s.height; return *this;
00123       }
00124       
00126       Rect32f& operator-=(const Size32f &s){
00127         width-=s.width; height-=s.height; return *this;
00128       }
00129       
00131       Rect32f& operator+=(const Point32f &p){
00132         x+=p.x; y+=p.y; return *this;
00133       }
00134   
00136       Rect32f& operator-=(const Point32f &p){
00137         x-=p.x; y-=p.y; return *this;
00138       }
00139       
00141       Rect32f& operator*=(double d){
00142         x*=d;
00143         y*=d;
00144         width*=d;
00145         height*=d;
00146         return *this;
00147       }
00149       Rect32f& operator/=(double d){
00150         x/=d;
00151         y/=d;
00152         width/=d;
00153         height/=d;
00154         return *this;
00155       }
00157       float getDim() const {return width*height;}
00158   
00160       Rect32f operator&(const Rect32f &r) const {
00161          Point32f ul (iclMax (x, r.x), iclMax (y, r.y));
00162          Point32f lr (iclMin (right(), r.right()), iclMin (bottom(), r.bottom()));
00163          Rect32f result (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
00164          if (result.width > 0 && result.height > 0) return result;
00165          else return null;
00166       }
00167       
00169       Rect32f &operator&=(const Rect32f &r){
00170         (*this)=(*this)&r;
00171         return *this;
00172       }
00173       
00175       Rect32f operator|(const Rect32f &r) const {
00176          Point32f ul (iclMin (x, r.x), iclMin (y, r.y));
00177          Point32f lr (iclMax (right(), r.right()), iclMax (bottom(), r.bottom()));
00178          return Rect32f (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
00179       }
00180   
00182       Rect32f &operator|=(const Rect32f &r){
00183         (*this)=(*this)|r;
00184         return *this;
00185       }
00186       
00188 
00189       Rect32f normalized() const {
00190          Rect32f r (*this);
00191          if (r.width < 0) {r.x += r.width; r.width = -r.width; }
00192          if (r.height < 0) {r.y += r.height; r.height = -r.height; }
00193          return r;
00194       }
00195       
00197       bool contains(const Rect32f &r) const {
00198          return x<=r.x && y <= r.y && right() >= r.right() && bottom() >= r.bottom();
00199       }
00200       
00202       bool contains(float x, float y) const{
00203         return this->x<=x && right()>=x && this->y<=y && bottom()>=y;
00204       }
00205       
00207 
00212       Rect32f &enlarge(float k){
00213         x-=k; y-=k; width+=2*k; height+=2*k;
00214         return *this;
00215       }
00216       
00218 
00219       Rect32f enlarged(float k) const{
00220         return Rect32f(*this).enlarge(k);
00221       }
00222       
00223       
00225       Point32f ul() const {
00226         return Point32f(x,y);
00227       }
00229       Point32f ll() const {
00230         return Point32f(x,y+height);
00231       }
00233       Point32f ur() const {
00234         return Point32f(x+width,y);
00235       }
00237       Point32f lr() const {
00238         return Point32f(x+width,y+height);
00239       }
00240   
00242       float left() const { return x; }
00243   
00245       float right() const { return x+width; }
00246   
00248       float bottom() const { return y+height; }
00249   
00251       float top() const { return y; }
00252   
00254       Size32f getSize() const { return Size32f(width,height); }
00255   
00257       Point32f center() const {
00258         return Point32f(x+width/2,y+height/2);
00259       }
00260       
00262       Rect32f transform(double xfac, double yfac) const { 
00263         return Rect32f(x*xfac,y*yfac,width*xfac,height*yfac);
00264       }
00265     };
00266   
00268     ICLUtils_API std::ostream &operator<<(std::ostream &s, const Rect32f &r);
00269     
00271     ICLUtils_API std::istream &operator>>(std::istream &s, Rect32f &r);
00272   
00273   
00274   } // namespace utils
00275 } // namespace icl
00276 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines