include/cinder/Area.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/Cinder.h"
00026 #include "cinder/Vector.h"
00027 
00028 #include <iostream>
00029 #include <utility>
00030 #include <boost/rational.hpp>
00031 
00032 namespace cinder {
00033 
00034 template<typename T>
00035 class RectT;
00036 
00037 template<typename T>
00038 class AreaT {
00039  public:
00040     AreaT() {}
00041     AreaT( const Vec2<T> &UL, const Vec2<T> &LR );
00042     AreaT( T aX1, T aY1, T aX2, T aY2 )
00043         { set( aX1, aY1, aX2, aY2 ); }
00044     template<typename Y>
00045     explicit AreaT( const AreaT<Y> &aAreaBase );
00046     explicit AreaT( const RectT<float> &r );
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     Vec2<T>         getSize() const { return Vec2<T>( x2 - x1, y2 - y1 ); }
00053     T               calcArea() const { return getWidth() * getHeight(); }
00054     
00055     void            clipBy( const AreaT<T> &clip );
00056     AreaT<T>        getClipBy( const AreaT<T> &clip ) const;
00057 
00059     void            offset( const Vec2<T> &off );
00061     AreaT<T>        getOffset( const Vec2<T> &off ) const;
00063     void            moveULTo( const Vec2<T> &newUL );
00065     AreaT<T>        getMoveULTo( const Vec2<T> &newUL ) const;
00067     void            expand( T expandX, T expandY ) { x1 -= expandX; x2 += expandX; y1 -= expandY; y2 += expandY; }
00068 
00069     T               getX1() const { return x1; }
00070     void            setX1( T aX1 ) { x1 = aX1; }
00071     T               getY1() const { return y1; }
00072     void            setY1( T aY1 ) { y1 = aY1; }
00073     T               getX2() const { return x2; }
00074     void            setX2( T aX2 ) { x2 = aX2; }
00075     T               getY2() const { return y2; }
00076     void            setY2( T aY2 ) { y2 = aY2; }
00077     Vec2<T>         getUL() const { return Vec2<T>( x1, y1 ); } // left-top offset
00078     Vec2<T>         getLR() const { return Vec2<T>( x2, y2 ); } // right-bottom offset
00079 
00080     bool            contains( const Vec2<T> &offset ) const;
00081     template<typename Y>
00082     bool            contains( const Vec2<Y> &offset ) const { return contains( Vec2<T>( (T)math<Y>::ceil( offset. x ), (T)math<Y>::ceil( offset.y ) ) ); }
00083     bool            intersects( const AreaT<T> &area ) const;
00084 
00086     template<typename Y>
00087     float       distance( const Vec2<Y> &pt ) const;
00089     template<typename Y>
00090     float       distanceSquared( const Vec2<Y> &pt ) const;
00091 
00093     template<typename Y>
00094     Vec2<Y>     closestPoint( const Vec2<Y> &pt ) const;
00095 
00096     T               x1, y1, x2, y2;
00097 
00098     bool            operator==( const AreaT<T> &aArea ) const { return ( ( x1 == aArea.x1 ) && ( y1 == aArea.y1 ) && ( x2 == aArea.x2 ) && ( y2 == aArea.y2 ) ); }
00099     bool            operator<( const AreaT<T> &aArea ) const;
00100 
00101     const AreaT<T>      operator+( const Vec2<T> &o ) const { return this->getOffset( o ); }
00102     const AreaT<T>      operator-( const Vec2<T> &o ) const { return this->getOffset( -o ); }
00103 
00104     const AreaT<T>      operator+( const AreaT<T>& rhs ) const { return AreaT<T>( x1 + rhs.x1, y1 + rhs.y1, x2 + rhs.x2, y2 + rhs.y2 ); }
00105     const AreaT<T>      operator-( const AreaT<T>& rhs ) const { return AreaT<T>( x1 - rhs.x1, y1 - rhs.y1, x2 - rhs.x2, y2 - rhs.y2 ); }
00106 
00107     AreaT<T>&       operator+=( const Vec2<T> &o ) { offset( o ); return *this; }
00108     AreaT<T>&       operator-=( const Vec2<T> &o ) { offset( -o ); return *this; }
00109 
00110     static AreaT<T> proportionalFit( const AreaT<T> &srcArea, const AreaT<T> &dstArea, bool center, bool expand = false );
00111 
00112     friend std::ostream& operator<<( std::ostream &o, const AreaT<T> &area )
00113     {
00114         return o << "(" << area.x1 << ", " << area.y1 << ")-(" << area.x2 << ", " << area.y2 << ")";
00115     }   
00116 };
00117 
00118 typedef AreaT<int32_t>                      Area;
00119 typedef AreaT<boost::rational<int32_t> >    AreaRational;
00120 
00121 extern std::pair<Area,Vec2i> clippedSrcDst( const Area &srcSurfaceBounds, const Area &srcArea, const Area &dstSurfaceBounds, const Vec2i &dstLT );
00122 
00123 } // namespace cinder