include/cinder/Rect.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/Vector.h"
00026 #include "cinder/Area.h"
00027 
00028 #include <vector>
00029 
00030 namespace cinder {
00031 
00032 template<typename T> class MatrixAffine2;
00033 
00034 template<typename T>
00035 class RectT {
00036  public:
00037     RectT() {}
00039     RectT( const std::vector<Vec2<T> > &points );
00040     RectT( T aX1, T aY1, T aX2, T aY2 ) {
00041         set( aX1, aY1, aX2, aY2 );
00042     }
00043     RectT( const Vec2<T> &v1, const Vec2<T> &v2 ) {
00044         set( v1.x, v1.y, v2.x, v2.y );
00045     }
00046     RectT( const Area &area );
00047 
00048     void        set( T aX1, T aY1, T aX2, T aY2 );
00049 
00050     T       getWidth() const { return x2 - x1; }
00051     T       getHeight() const { return y2 - y1; }
00052     T       getAspectRatio() const { return getWidth() / getHeight(); }
00053     T       calcArea() const { return getWidth() * getHeight(); }
00054 
00055     void        canonicalize(); // return rect w/ properly ordered coordinates
00056     RectT       canonicalized() const; // return rect w/ properly ordered coordinates
00057     
00058     void        clipBy( const RectT &clip );
00059     RectT       getClipBy( const RectT &clip ) const;
00060     Area        getInteriorArea() const;
00061     void        offset( const Vec2<T> &offset );
00062     RectT       getOffset( const Vec2<T> &off ) const { RectT result( *this ); result.offset( off ); return result; }
00063     void        inflate( const Vec2<T> &amount );
00064     RectT       inflated( const Vec2<T> &amount ) const;
00066     void        offsetCenterTo( const Vec2<T> &center ) { offset( center - getCenter() ); }
00067     void        scaleCentered( const Vec2<T> &scale );
00068     void        scaleCentered( T scale );
00069     RectT       scaledCentered( T scale ) const;
00070     void        scale( T scale );
00071     void        scale( const Vec2<T> &scale );
00072     RectT       scaled( T scale ) const;
00073     RectT       scaled( const Vec2<T> &scale ) const;
00074 
00076     RectT       transformCopy( const class MatrixAffine2<T> &matrix ) const;
00077 
00079     template<typename Y>
00080     bool        contains( const Vec2<Y> &pt ) const { return ( pt.x >= x1 ) && ( pt.x <= x2 ) && ( pt.y >= y1 ) && ( pt.y <= y2 ); }
00082     bool        intersects( const RectT &rect ) const;
00083 
00085     T       distance( const Vec2<T> &pt ) const;
00087     T       distanceSquared( const Vec2<T> &pt ) const;
00088 
00090     Vec2<T>     closestPoint( const Vec2<T> &pt ) const;
00091 
00092     T       getX1() const { return x1; }
00093     T       getY1() const { return y1; }
00094     T       getX2() const { return x2; }
00095     T       getY2() const { return y2; }
00096 
00097     Vec2<T>     getUpperLeft() const { return Vec2<T>( x1, y1 ); };
00098     Vec2<T>     getUpperRight() const { return Vec2<T>( x2, y1 ); };
00099     Vec2<T>     getLowerRight() const { return Vec2<T>( x2, y2 ); };    
00100     Vec2<T>     getLowerLeft() const { return Vec2<T>( x1, y2 ); }; 
00101     Vec2<T>     getCenter() const { return Vec2<T>( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 ); }
00102     Vec2<T>     getSize() const { return Vec2<T>( x2 - x1, y2 - y1 ); }
00103 
00105     RectT       getCenteredFit( const RectT &other, bool expand ) const;
00106 
00108     void        include( const Vec2<T> &point );
00110     void        include( const std::vector<Vec2<T> > &points );
00112     void        include( const RectT &rect );
00113 
00114     const RectT<T>      operator+( const Vec2<T> &o ) const { return this->getOffset( o ); }
00115     const RectT<T>      operator-( const Vec2<T> &o ) const { return this->getOffset( -o ); }
00116     const RectT<T>      operator*( T s ) const { return this->scaled( s ); }
00117     const RectT<T>      operator/( T s ) const { return this->scaled( ((T)1) / s ); }
00118 
00119     const RectT<T>      operator+( const RectT<T>& rhs ) const { return RectT<T>( x1 + rhs.x1, y1 + rhs.y1, x2 + rhs.x2, y2 + rhs.y2 ); }
00120     const RectT<T>      operator-( const RectT<T>& rhs ) const { return RectT<T>( x1 - rhs.x1, y1 - rhs.y1, x2 - rhs.x2, y2 - rhs.y2 ); }
00121 
00122     RectT<T>&       operator+=( const Vec2<T> &o ) { offset( o ); return *this; }
00123     RectT<T>&       operator-=( const Vec2<T> &o ) { offset( -o ); return *this; }
00124     RectT<T>&       operator*=( T s ) { scale( s ); return *this; }
00125     RectT<T>&       operator/=( T s ) { scale( ((T)1) / s ); return *this; }    
00126 
00127     T           x1, y1, x2, y2;
00128     
00129     friend std::ostream& operator<<( std::ostream &o, const RectT &rect )
00130     {
00131         return o << "(" << rect.x1 << ", " << rect.y1 << ")-(" << rect.x2 << ", " << rect.y2 << ")";
00132     }   
00133 
00134 };
00135 
00136 typedef RectT<float>    Rectf;
00137 typedef RectT<double>   Rectd;
00138 
00139 
00140 // This class maps a rectangle into another rectangle
00141 class RectMapping {
00142  public:
00143     RectMapping()
00144         : mSrcRect( 0, 0, 0, 0 ), mDstRect( 0, 0, 0, 0 ) {}
00145     RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect )
00146         : mSrcRect( aSrcRect ), mDstRect( aDstRect ) {}
00147     RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect, bool preserveSrcAspect );
00148         
00149     Vec2f   map( const Vec2f &srcPoint ) const;
00150     Rectf   map( const Rectf &srcRect ) const;
00151     
00152  private:
00153     Rectf   mSrcRect, mDstRect;
00154 };
00155 
00156 extern void getClippedScaledRects( const Area &srcSurfaceBounds, const Rectf &srcRect, const Area &dstSurfaceBounds, const Area &dstArea, Rectf *resultSrcRect, Area *resultDstRect );
00157 
00158 } // namespace cinder