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