Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/Rect.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef Rect_INCLUDE_ONCE
00033 #define Rect_INCLUDE_ONCE
00034 
00035 #include <vlCore/Vector2.hpp>
00036 
00037 namespace vl
00038 {
00042   template<typename T>
00043   class Rect
00044   {
00045   public:
00046     Rect(const Rect& other)
00047     {
00048       mX = other.x();
00049       mY = other.y();
00050       mWidth  = other.width();
00051       mHeight = other.height();
00052     }
00053     Rect()
00054     {
00055       // null Rect
00056       mX = 0;
00057       mY = 0;
00058       mWidth  = -1;
00059       mHeight = -1;
00060     }
00061     Rect(T x, T y, T width, T height) 
00062     { 
00063       mX = x; 
00064       mY = y; 
00065       mWidth = width; 
00066       mHeight = height; 
00067     }
00068 
00069     T x() const { return mX; }
00070     T y() const { return mY; }
00071     T width() const  { return mWidth; }
00072     T height() const { return mHeight; }
00073     T bottom() const { return mY; }
00074     T left() const   { return mX; }
00075     T top() const    { return mY+mHeight-1; }
00076     T right() const  { return mX+mWidth-1;  }
00077 
00078     void setX(T x) { mX = x; }
00079     void setY(T y) { mY = y; }
00080     void setWidth(T w)  { mWidth  = w; }
00081     void setHeight(T h) { mHeight = h; }
00082 
00083     bool isNull()  const { return width() <  0 || height() <  0; }
00084     bool isPoint() const { return width() == 0 && height() == 0; }
00085 
00086     Rect intersected(const Rect& other) const
00087     {
00088       if (isNull() || other.isNull())
00089         return other;
00090       T Ax1 = left();
00091       T Ax2 = right();
00092       T Ay1 = bottom();
00093       T Ay2 = top();
00094       T Bx1 = other.left();
00095       T Bx2 = other.right();
00096       T By1 = other.bottom();
00097       T By2 = other.top();
00098       if (Ax1 < Bx1) Ax1 = Bx1;
00099       if (Ay1 < By1) Ay1 = By1;
00100       if (Ax2 > Bx2) Ax2 = Bx2;
00101       if (Ay2 > By2) Ay2 = By2;
00102       return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
00103     }
00104 
00105     Rect united(const Rect& other) const
00106     {
00107       if (other.isNull())
00108         return *this;
00109       if (isNull())
00110         return other;
00111       T Ax1 = left();
00112       T Ax2 = right();
00113       T Ay1 = bottom();
00114       T Ay2 = top();
00115       T Bx1 = other.left();
00116       T Bx2 = other.right();
00117       T By1 = other.bottom();
00118       T By2 = other.top();
00119       if (Ax1 > Bx1) Ax1 = Bx1;
00120       if (Ay1 > By1) Ay1 = By1;
00121       if (Ax2 < Bx2) Ax2 = Bx2;
00122       if (Ay2 < By2) Ay2 = By2;
00123       return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
00124     }
00125 
00129     bool operator<(const Rect& other) const
00130     {
00131       if (mX != other.mX)
00132         return mX < other.mX;
00133       else
00134       if (mWidth != other.mWidth)
00135         return mWidth < other.mWidth;
00136       else
00137       if (mHeight != other.mHeight)
00138         return mHeight < other.mHeight;
00139       else
00140       if (mY != other.mY)
00141         return mY < other.mY;
00142       else
00143         return false;
00144     }
00145 
00146   protected:
00147     T mX;
00148     T mY;
00149     T mWidth;
00150     T mHeight;
00151   };
00152 
00163   class RectI: public Rect<int>
00164   {
00165   public:
00166     RectI() {}
00167     RectI(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; }
00168     RectI(const Rect<int>& other) { *this = other; }
00169     RectI(const RectI& other): Rect<int>(other) { *this = other; }
00170     // operator Rect<int>() const { return Rect<int>(x(), y(), width(), height()); }
00171     RectI& operator=(const Rect<int>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00172     RectI& operator=(const RectI& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00173     int top() const    { return mY+mHeight-1; }
00174     int right() const  { return mX+mWidth-1;  }
00175     ivec2 bottomLeft() const { return ivec2(bottom(),left()); }
00176     ivec2 topRight() const { return ivec2(top(),right()); }
00177   };
00178 
00189   class RectF: public Rect<float>
00190   {
00191   public:
00192     RectF() {}
00193     RectF(float x, float y, float width, float height) { mX=x; mY=y; mWidth=width; mHeight=height; }
00194     RectF(const RectF& other): Rect<float>(other) { *this = other; }
00195     RectF(const Rect<float>& other) { *this = other; }
00196     // operator Rect<float>() const { return Rect<float>(x(), y(), width(), height()); }
00197     RectF& operator=(const Rect<float>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00198     RectF& operator=(const RectF& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
00199     float top() const    { return mY+mHeight; }
00200     float right() const  { return mX+mWidth;  }
00201     fvec2 bottomLeft() const { return fvec2(bottom(),left()); }
00202     fvec2 topRight() const { return fvec2(top(),right()); }
00203     void addPoint(const fvec2& p)
00204     {
00205       fvec2 tr = topRight();
00206       fvec2 bl = bottomLeft();
00207       if (p.x() < bl.x())
00208         bl.x() = p.x();
00209       if (p.y() < bl.y())
00210         bl.y() = p.y();
00211       if (p.x() > tr.x())
00212         tr.x() = p.x();
00213       if (p.y() > tr.y())
00214         tr.y() = p.y();
00215       mX = bl.x();
00216       mY = bl.y();
00217       mWidth  = tr.x() - bl.x();
00218       mHeight = tr.y() - bl.y();
00219     }
00220   };
00221 }
00222 
00223 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:31.
Permission is granted to use this page to write and publish articles regarding Visualization Library.