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.GPL  **
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/Point32f.h>
00034 #include <ICLUtils/Size32f.h>
00035 #include <ICLUtils/Rect.h>
00036 #include <stdio.h>
00037 #include <algorithm>
00038 
00039 namespace icl {
00040   namespace utils{
00041     
00042     
00044     class Rect32f{
00045       public:
00046      
00047       float x;      
00048       float y;      
00049       float width;  
00050       float height; 
00051   
00053       static const Rect32f null;
00054       
00056       Rect32f(float x, float y, float width, float height):
00057       x(x),y(y),width(width),height(height){
00058         this->x = x;
00059         this->y = y;
00060         this->width = width;
00061         this->height = height;
00062       }
00063       
00065       Rect32f(const Point32f &p, const Size32f &s){
00066         this->x = p.x;
00067         this->y = p.y;
00068         this->width = s.width;
00069         this->height = s.height;
00070       } 
00071       
00073       Rect32f(const Rect32f &r=null){
00074         this->x = r.x;
00075         this->y = r.y;
00076         this->width = r.width;
00077         this->height = r.height;
00078       }
00079       
00081       Rect32f(const Rect &rect):
00082         x((float)rect.x),
00083         y((float)rect.y),
00084         width((float)rect.width),
00085         height((float)rect.height){
00086       }
00087   
00089       bool isNull() const { return (*this)==null; }
00090   
00092       bool operator==(const Rect32f &s) const {
00093         return x==s.x && y==s.y && width==s.width && height==s.height;
00094       }
00095   
00097       bool operator!=(const Rect32f &s) const {
00098         return x!=s.x || y!= s.y || width!=s.width || height!=s.height;
00099       }
00100   
00102       Rect32f operator*(double d) const {
00103         return Rect32f(d*x,d*y,d*width,d*height);
00104       }
00105   
00107       Rect32f operator/(double d) const {
00108         return Rect32f(d/x,d/y,d/width,d/height);
00109       }
00110   
00112       Rect32f& operator+=(const Size32f &s){
00113         width+=s.width; height+=s.height; return *this;
00114       }
00115       
00117       Rect32f& operator-=(const Size32f &s){
00118         width-=s.width; height-=s.height; return *this;
00119       }
00120       
00122       Rect32f& operator+=(const Point32f &p){
00123         x+=p.x; y+=p.y; return *this;
00124       }
00125   
00127       Rect32f& operator-=(const Point32f &p){
00128         x-=p.x; y-=p.y; return *this;
00129       }
00130       
00132       Rect32f& operator*=(double d){
00133         x*=d;
00134         y*=d;
00135         width*=d;
00136         height*=d;
00137         return *this;
00138       }
00140       Rect32f& operator/=(double d){
00141         x/=d;
00142         y/=d;
00143         width/=d;
00144         height/=d;
00145         return *this;
00146       }
00148       float getDim() const {return width*height;}
00149   
00151       Rect32f operator&(const Rect32f &r) const {
00152          Point32f ul (iclMax (x, r.x), iclMax (y, r.y));
00153          Point32f lr (iclMin (right(), r.right()), iclMin (bottom(), r.bottom()));
00154          Rect32f result (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
00155          if (result.width > 0 && result.height > 0) return result;
00156          else return null;
00157       }
00158       
00160       Rect32f &operator&=(const Rect32f &r){
00161         (*this)=(*this)&r;
00162         return *this;
00163       }
00164       
00166       Rect32f operator|(const Rect32f &r) const {
00167          Point32f ul (iclMin (x, r.x), iclMin (y, r.y));
00168          Point32f lr (iclMax (right(), r.right()), iclMax (bottom(), r.bottom()));
00169          return Rect32f (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y);
00170       }
00171   
00173       Rect32f &operator|=(const Rect32f &r){
00174         (*this)=(*this)|r;
00175         return *this;
00176       }
00177       
00179 
00180       Rect32f normalized() const {
00181          Rect32f r (*this);
00182          if (r.width < 0) {r.x += r.width; r.width = -r.width; }
00183          if (r.height < 0) {r.y += r.height; r.height = -r.height; }
00184          return r;
00185       }
00186       
00188       bool contains(const Rect32f &r) const {
00189          return x<=r.x && y <= r.y && right() >= r.right() && bottom() >= r.bottom();
00190       }
00191       
00193       bool contains(float x, float y) const{
00194         return this->x<=x && right()>=x && this->y<=y && bottom()>=y;
00195       }
00196       
00198 
00203       Rect32f &enlarge(float k){
00204         x-=k; y-=k; width+=2*k; height+=2*k;
00205         return *this;
00206       }
00207       
00209 
00210       Rect32f enlarged(float k) const{
00211         return Rect32f(*this).enlarge(k);
00212       }
00213       
00214       
00216       Point32f ul() const {
00217         return Point32f(x,y);
00218       }
00220       Point32f ll() const {
00221         return Point32f(x,y+height);
00222       }
00224       Point32f ur() const {
00225         return Point32f(x+width,y);
00226       }
00228       Point32f lr() const {
00229         return Point32f(x+width,y+height);
00230       }
00231   
00233       float left() const { return x; }
00234   
00236       float right() const { return x+width; }
00237   
00239       float bottom() const { return y+height; }
00240   
00242       float top() const { return y; }
00243   
00245       Size32f getSize() const { return Size32f(width,height); }
00246   
00248       Point32f center() const {
00249         return Point32f(x+width/2,y+height/2);
00250       }
00251       
00253       Rect32f transform(double xfac, double yfac) const { 
00254         return Rect32f(x*xfac,y*yfac,width*xfac,height*yfac);
00255       }
00256     };
00257   
00259     std::ostream &operator<<(std::ostream &s, const Rect32f &r);
00260     
00262     std::istream &operator>>(std::istream &s, Rect32f &r);
00263   
00264   
00265   } // namespace utils
00266 } // namespace icl
00267 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines