Cinder

  • Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

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>
00033 class RectT {
00034  public:
00035     RectT() {}
00037     RectT( const std::vector<Vec2<T> > &points );
00038     RectT( T aX1, T aY1, T aX2, T aY2 ) {
00039         set( aX1, aY1, aX2, aY2 );
00040     }
00041     RectT( const Vec2<T> &v1, const Vec2<T> &v2 ) {
00042         set( v1.x, v1.y, v2.x, v2.y );
00043     }
00044     RectT( const Area &area );
00045 
00046     void        set( T aX1, T aY1, T aX2, T aY2 );
00047 
00048     T       getWidth() const { return x2 - x1; }
00049     T       getHeight() const { return y2 - y1; }
00050     T       getAspectRatio() const { return getWidth() / getHeight(); }
00051     T       calcArea() const { return getWidth() * getHeight(); }
00052 
00053     void        canonicalize(); // return rect w/ properly ordered coordinates
00054     RectT       canonicalized() const; // return rect w/ properly ordered coordinates
00055     
00056     void        clipBy( const RectT &clip );
00057     RectT       getClipBy( const RectT &clip ) const;
00058     Area        getInteriorArea() const;
00059     void        offset( const Vec2<T> &offset );
00060     RectT       getOffset( const Vec2<T> &off ) const { RectT result( *this ); result.offset( off ); return result; }
00062     void        offsetCenterTo( const Vec2<T> &center ) { offset( center - getCenter() ); }
00063     void        scaleCentered( const Vec2<T> &scale );
00064     void        scaleCentered( T scale );
00065     RectT       scaledCentered( T scale ) const;
00066     void        scale( T scale );
00067     RectT       scaled( T scale ) const;
00068 
00070     template<typename Y>
00071     bool        contains( const Vec2<Y> &pt ) const { return ( pt.x >= x1 ) && ( pt.x <= x2 ) && ( pt.y >= y1 ) && ( pt.y <= y2 ); }
00073     bool        intersects( const RectT &rect ) const;
00074 
00076     T       distance( const Vec2<T> &pt ) const;
00078     T       distanceSquared( const Vec2<T> &pt ) const;
00079 
00081     Vec2<T>     closestPoint( const Vec2<T> &pt ) const;
00082 
00083     T       getX1() const { return x1; }
00084     T       getY1() const { return y1; }
00085     T       getX2() const { return x2; }
00086     T       getY2() const { return y2; }
00087 
00088     Vec2<T>     getUpperLeft() const { return Vec2<T>( x1, y1 ); };
00089     Vec2<T>     getUpperRight() const { return Vec2<T>( x2, y1 ); };
00090     Vec2<T>     getLowerRight() const { return Vec2<T>( x2, y2 ); };    
00091     Vec2<T>     getLowerLeft() const { return Vec2<T>( x1, y2 ); }; 
00092     Vec2<T>     getCenter() const { return Vec2<T>( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 ); }
00093     Vec2<T>     getSize() const { return Vec2<T>( x2 - x1, y2 - y1 ); }
00094 
00096     RectT       getCenteredFit( const RectT &other, bool expand ) const;
00097 
00099     void        include( const Vec2<T> &point );
00101     void        include( const std::vector<Vec2<T> > &points );
00103     void        include( const RectT &rect );
00104 
00105     const RectT<T>      operator+( const Vec2<T> &o ) const { return this->getOffset( o ); }
00106     const RectT<T>      operator-( const Vec2<T> &o ) const { return this->getOffset( -o ); }
00107     const RectT<T>      operator*( T s ) const { return this->scaled( s ); }
00108     const RectT<T>      operator/( T s ) const { return this->scaled( ((T)1) / s ); }
00109 
00110     RectT<T>&       operator+=( const Vec2<T> &o ) { offset( o ); return *this; }
00111     RectT<T>&       operator-=( const Vec2<T> &o ) { offset( -o ); return *this; }
00112     RectT<T>&       operator*=( T s ) { scale( s ); return *this; }
00113     RectT<T>&       operator/=( T s ) { scale( ((T)1) / s ); return *this; }    
00114 
00115     T           x1, y1, x2, y2;
00116     
00117     friend std::ostream& operator<<( std::ostream &o, const RectT &rect )
00118     {
00119         return o << "(" << rect.x1 << ", " << rect.y1 << ")-(" << rect.x2 << ", " << rect.y2 << ")";
00120     }   
00121 
00122 };
00123 
00124 typedef RectT<float>    Rectf;
00125 typedef RectT<double>   Rectd;
00126 
00127 
00128 // This class maps a rectangle into another rectangle
00129 class RectMapping {
00130  public:
00131     RectMapping()
00132         : mSrcRect( 0, 0, 0, 0 ), mDstRect( 0, 0, 0, 0 ) {}
00133     RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect )
00134         : mSrcRect( aSrcRect ), mDstRect( aDstRect ) {}
00135     RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect, bool preserveSrcAspect );
00136         
00137     Vec2f   map( const Vec2f &srcPoint ) const;
00138     Rectf   map( const Rectf &srcRect ) const;
00139     
00140  private:
00141     Rectf   mSrcRect, mDstRect;
00142 };
00143 
00144 extern void getClippedScaledRects( const Area &srcSurfaceBounds, const Rectf &srcRect, const Area &dstSurfaceBounds, const Area &dstArea, Rectf *resultSrcRect, Area *resultDstRect );
00145 
00146 } // namespace cinder