Image Component Library (ICL)
|
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.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/Point.h> 00034 #include <ICLUtils/Size.h> 00035 #include <stdio.h> 00036 #include <algorithm> 00037 #ifdef HAVE_IPP 00038 #include <ipp.h> 00039 #endif 00040 00041 00042 namespace icl { 00043 namespace utils{ 00044 00045 #ifndef HAVE_IPP 00046 00047 struct IppiRect { 00048 00050 int x; 00051 00053 int y; 00054 00056 int width; 00057 00059 int height; 00060 }; 00061 #else 00062 #endif 00063 00065 00091 class Rect32f; 00094 class Rect : public IppiRect{ 00095 public: 00096 00098 static const Rect null; 00099 00101 Rect(int x, int y, int width, int height){ 00102 this->x = x; 00103 this->y = y; 00104 this->width = width; 00105 this->height = height; 00106 } 00107 00109 Rect(const Point &p, const Size &s){ 00110 this->x = p.x; 00111 this->y = p.y; 00112 this->width = s.width; 00113 this->height = s.height; 00114 } 00115 00117 Rect(const Rect &r=null){ 00118 this->x = r.x; 00119 this->y = r.y; 00120 this->width = r.width; 00121 this->height = r.height; 00122 } 00123 00125 Rect(const Rect32f &other); 00126 00128 bool isNull() const { return (*this)==null; } 00129 00131 bool operator==(const Rect &s) const { 00132 return x==s.x && y==s.y && width==s.width && height==s.height; 00133 } 00134 00136 bool operator!=(const Rect &s) const { 00137 return x!=s.x || y!= s.y || width!=s.width || height!=s.height; 00138 } 00139 00141 Rect operator*(double d) const { 00142 return Rect((int)(d*x),(int)(d*y),(int)(d*width),(int)(d*height)); 00143 } 00144 00146 Rect operator/(double d) const { 00147 return Rect((int)(x/d),(int)(y/d),(int)(width/d),(int)(height/d)); 00148 } 00149 00150 00152 Rect operator+(const Size &s) const{ 00153 return Rect(x,y,width+s.width,height+s.height); 00154 } 00155 00157 Rect operator-(const Size &s) const{ 00158 return Rect(x,y,width-s.width,height-s.height); 00159 } 00160 00162 Rect& operator+=(const Size &s){ 00163 width+=s.width; height+=s.height; return *this; 00164 } 00165 00167 Rect& operator-=(const Size &s){ 00168 width-=s.width; height-=s.height; return *this; 00169 } 00170 00172 Rect operator+(const Point &p) const{ 00173 return Rect(x+p.x,y+p.y,width,height); 00174 } 00175 00177 Rect operator-(const Point &p) const{ 00178 return Rect(x-p.x,y-p.y,width,height); 00179 } 00180 00182 Rect& operator+=(const Point &p){ 00183 x+=p.x; y+=p.y; return *this; 00184 } 00185 00187 Rect& operator-=(const Point &p){ 00188 x-=p.x; y-=p.y; return *this; 00189 } 00190 00192 Rect& operator*=(double d){ 00193 x=(int)((float)x*d); 00194 y=(int)((float)y*d); 00195 width=(int)((float)width*d); 00196 height=(int)((float)height*d); 00197 return *this; 00198 } 00200 Rect& operator/=(double d){ 00201 x=(int)((float)x/d); 00202 y=(int)((float)y/d); 00203 width=(int)((float)width/d); 00204 height=(int)((float)height/d); 00205 return *this; 00206 } 00208 int getDim() const {return width*height;} 00209 00211 Rect operator&(const Rect &r) const { 00212 Point ul (std::max (x, r.x), std::max (y, r.y)); 00213 Point lr (std::min (right(), r.right()), std::min (bottom(), r.bottom())); 00214 Rect result (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y); 00215 if (result.width > 0 && result.height > 0) return result; 00216 else return null; 00217 } 00218 00220 Rect &operator&=(const Rect &r){ 00221 (*this)=(*this)&r; 00222 return *this; 00223 } 00224 00226 Rect operator|(const Rect &r) const { 00227 Point ul (std::min (x, r.x), std::min (y, r.y)); 00228 Point lr (std::max (right(), r.right()), std::max (bottom(), r.bottom())); 00229 return Rect (ul.x, ul.y, lr.x-ul.x, lr.y-ul.y); 00230 } 00231 00233 Rect &operator|=(const Rect &r){ 00234 (*this)=(*this)|r; 00235 return *this; 00236 } 00237 00239 00240 Rect normalized() const { 00241 Rect r (*this); 00242 if (r.width < 0) {r.x += r.width; r.width = -r.width; } 00243 if (r.height < 0) {r.y += r.height; r.height = -r.height; } 00244 return r; 00245 } 00246 00248 bool contains(const Rect &r) const { 00249 return x<=r.x && y <= r.y && right() >= r.right() && bottom() >= r.bottom(); 00250 } 00251 00253 00258 bool contains(int x, int y) const{ 00259 return this->x<=x && right()>x && this->y<=y && bottom()>y; 00260 } 00261 00263 00268 Rect &enlarge(int k){ 00269 x-=k; y-=k; width+=2*k; height+=2*k; 00270 return *this; 00271 } 00272 00274 00275 Rect enlarged(int k) const{ 00276 return Rect(*this).enlarge(k); 00277 } 00278 00279 00281 Point ul() const { 00282 return Point(x,y); 00283 } 00285 Point ll() const { 00286 return Point(x,y+height); 00287 } 00289 Point ur() const { 00290 return Point(x+width,y); 00291 } 00293 Point lr() const { 00294 return Point(x+width,y+height); 00295 } 00296 00298 Point center() const { 00299 return Point(x+width/2,y+height/2); 00300 } 00301 00303 int left() const { return x; } 00304 00306 int right() const { return x+width; } 00307 00309 int bottom() const { return y+height; } 00310 00312 int top() const { return y; } 00313 00315 Size getSize() const { return Size(width,height); } 00316 00317 Rect transform(double xfac, double yfac) const { 00318 return Rect((int)(xfac*x),(int)(yfac*y),(int)(xfac*width), (int)(yfac*height)); 00319 } 00320 }; 00321 00323 std::ostream &operator<<(std::ostream &s, const Rect &r); 00324 00326 std::istream &operator>>(std::istream &s, Rect &r); 00327 00328 00329 } // namespace utils 00330 } // namespace icl 00331