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 AreaT { 00036 public: 00037 AreaT() {} 00038 AreaT( const Vec2<T> &UL, const Vec2<T> &LR ); 00039 AreaT( T aX1, T aY1, T aX2, T aY2 ) 00040 { set( aX1, aY1, aX2, aY2 ); } 00041 template<typename Y> 00042 explicit AreaT( const AreaT<Y> &aAreaBase ); 00043 00044 void set( T aX1, T aY1, T aX2, T aY2 ); 00045 00046 T getWidth() const { return x2 - x1; } 00047 T getHeight() const { return y2 - y1; } 00048 Vec2<T> getSize() const { return Vec2<T>( x2 - x1, y2 - y1 ); } 00049 T calcArea() const { return getWidth() * getHeight(); } 00050 00051 void clipBy( const AreaT<T> &clip ); 00052 AreaT<T> getClipBy( const AreaT<T> &clip ) const; 00053 00055 void offsetBy( const Vec2<T> &offset ); 00057 AreaT<T> getOffsetBy( const Vec2<T> &offset ) const; 00059 void moveULTo( const Vec2<T> &newUL ); 00061 AreaT<T> getMoveULTo( const Vec2<T> &newUL ) const; 00063 void expand( T expandX, T expandY ) { x1 -= expandX; x2 += expandX; y1 -= expandY; y2 += expandY; } 00064 00065 T getX1() const { return x1; } 00066 void setX1( T aX1 ) { x1 = aX1; } 00067 T getY1() const { return y1; } 00068 void setY1( T aY1 ) { y1 = aY1; } 00069 T getX2() const { return x2; } 00070 void setX2( T aX2 ) { x2 = aX2; } 00071 T getY2() const { return y2; } 00072 void setY2( T aY2 ) { y2 = aY2; } 00073 Vec2<T> getUL() const { return Vec2<T>( x1, y1 ); } // left-top offset 00074 Vec2<T> getLR() const { return Vec2<T>( x2, y2 ); } // right-bottom offset 00075 00076 bool isInside( const Vec2<T> &offset ) const; 00077 template<typename Y> 00078 bool isInside( const Vec2<Y> &offset ) const { return isInside( Vec2<T>( (T)math<Y>::ceil( offset. x ), (T)math<Y>::ceil( offset.y ) ) ); } 00079 bool intersects( const AreaT<T> &area ) const; 00080 00081 T x1, y1, x2, y2; 00082 00083 bool operator==( const AreaT<T> &aArea ) const { return ( ( x1 == aArea.x1 ) && ( y1 == aArea.y1 ) && ( x2 == aArea.x2 ) && ( y2 == aArea.y2 ) ); } 00084 bool operator<( const AreaT<T> &aArea ) const; 00085 AreaT<T> operator+( const Vec2<T> &offset ) const { return this->getOffsetBy( offset ); } 00086 00087 static AreaT<T> proportionalFit( const AreaT<T> &srcArea, const AreaT<T> &dstArea, bool center, bool expand = false ); 00088 00089 friend std::ostream& operator<<( std::ostream &o, const AreaT<T> &area ) 00090 { 00091 return o << "(" << area.x1 << ", " << area.y1 << ")-(" << area.x2 << ", " << area.y2 << ")"; 00092 } 00093 }; 00094 00095 typedef AreaT<int32_t> Area; 00096 typedef AreaT<boost::rational<int32_t> > AreaRational; 00097 00098 extern std::pair<Area,Vec2i> clippedSrcDst( const Area &srcSurfaceBounds, const Area &srcArea, const Area &dstSurfaceBounds, const Vec2i &dstLT ); 00099 00100 } // namespace cinder