Rect.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Vector.h"
26 #include "cinder/Area.h"
27 
28 #include <vector>
29 
30 namespace cinder {
31 
32 template<typename T> class MatrixAffine2;
33 
34 template<typename T>
35 class RectT {
36  public:
37  RectT() {}
39  RectT( const std::vector<Vec2<T> > &points );
40  RectT( T aX1, T aY1, T aX2, T aY2 ) {
41  set( aX1, aY1, aX2, aY2 );
42  }
43  RectT( const Vec2<T> &v1, const Vec2<T> &v2 ) {
44  set( v1.x, v1.y, v2.x, v2.y );
45  }
46  RectT( const Area &area );
47 
48  void set( T aX1, T aY1, T aX2, T aY2 );
49 
50  T getWidth() const { return x2 - x1; }
51  T getHeight() const { return y2 - y1; }
52  T getAspectRatio() const { return getWidth() / getHeight(); }
53  T calcArea() const { return getWidth() * getHeight(); }
54 
55  void canonicalize(); // return rect w/ properly ordered coordinates
56  RectT canonicalized() const; // return rect w/ properly ordered coordinates
57 
58  void clipBy( const RectT &clip );
59  RectT getClipBy( const RectT &clip ) const;
60  Area getInteriorArea() const;
61  void offset( const Vec2<T> &offset );
62  RectT getOffset( const Vec2<T> &off ) const { RectT result( *this ); result.offset( off ); return result; }
63  void inflate( const Vec2<T> &amount );
64  RectT inflated( const Vec2<T> &amount ) const;
66  void offsetCenterTo( const Vec2<T> &center ) { offset( center - getCenter() ); }
67  void scaleCentered( const Vec2<T> &scale );
68  void scaleCentered( T scale );
69  RectT scaledCentered( T scale ) const;
70  void scale( T scale );
71  void scale( const Vec2<T> &scale );
72  RectT scaled( T scale ) const;
73  RectT scaled( const Vec2<T> &scale ) const;
74 
76  RectT transformCopy( const class MatrixAffine2<T> &matrix ) const;
77 
79  template<typename Y>
80  bool contains( const Vec2<Y> &pt ) const { return ( pt.x >= x1 ) && ( pt.x <= x2 ) && ( pt.y >= y1 ) && ( pt.y <= y2 ); }
82  bool intersects( const RectT &rect ) const;
83 
85  T distance( const Vec2<T> &pt ) const;
87  T distanceSquared( const Vec2<T> &pt ) const;
88 
90  Vec2<T> closestPoint( const Vec2<T> &pt ) const;
91 
92  T getX1() const { return x1; }
93  T getY1() const { return y1; }
94  T getX2() const { return x2; }
95  T getY2() const { return y2; }
96 
97  Vec2<T> getUpperLeft() const { return Vec2<T>( x1, y1 ); };
98  Vec2<T> getUpperRight() const { return Vec2<T>( x2, y1 ); };
99  Vec2<T> getLowerRight() const { return Vec2<T>( x2, y2 ); };
100  Vec2<T> getLowerLeft() const { return Vec2<T>( x1, y2 ); };
101  Vec2<T> getCenter() const { return Vec2<T>( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 ); }
102  Vec2<T> getSize() const { return Vec2<T>( x2 - x1, y2 - y1 ); }
103 
105  RectT getCenteredFit( const RectT &other, bool expand ) const;
106 
108  void include( const Vec2<T> &point );
110  void include( const std::vector<Vec2<T> > &points );
112  void include( const RectT &rect );
113 
114  const RectT<T> operator+( const Vec2<T> &o ) const { return this->getOffset( o ); }
115  const RectT<T> operator-( const Vec2<T> &o ) const { return this->getOffset( -o ); }
116  const RectT<T> operator*( T s ) const { return this->scaled( s ); }
117  const RectT<T> operator/( T s ) const { return this->scaled( ((T)1) / s ); }
118 
119  const RectT<T> operator+( const RectT<T>& rhs ) const { return RectT<T>( x1 + rhs.x1, y1 + rhs.y1, x2 + rhs.x2, y2 + rhs.y2 ); }
120  const RectT<T> operator-( const RectT<T>& rhs ) const { return RectT<T>( x1 - rhs.x1, y1 - rhs.y1, x2 - rhs.x2, y2 - rhs.y2 ); }
121 
122  RectT<T>& operator+=( const Vec2<T> &o ) { offset( o ); return *this; }
123  RectT<T>& operator-=( const Vec2<T> &o ) { offset( -o ); return *this; }
124  RectT<T>& operator*=( T s ) { scale( s ); return *this; }
125  RectT<T>& operator/=( T s ) { scale( ((T)1) / s ); return *this; }
126 
127  T x1, y1, x2, y2;
128 
129  friend std::ostream& operator<<( std::ostream &o, const RectT &rect )
130  {
131  return o << "(" << rect.x1 << ", " << rect.y1 << ")-(" << rect.x2 << ", " << rect.y2 << ")";
132  }
133 
134 };
135 
138 
139 
140 // This class maps a rectangle into another rectangle
141 class RectMapping {
142  public:
144  : mSrcRect( 0, 0, 0, 0 ), mDstRect( 0, 0, 0, 0 ) {}
145  RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect )
146  : mSrcRect( aSrcRect ), mDstRect( aDstRect ) {}
147  RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect, bool preserveSrcAspect );
148 
149  Vec2f map( const Vec2f &srcPoint ) const;
150  Rectf map( const Rectf &srcRect ) const;
151 
152  private:
153  Rectf mSrcRect, mDstRect;
154 };
155 
156 extern void getClippedScaledRects( const Area &srcSurfaceBounds, const Rectf &srcRect, const Area &dstSurfaceBounds, const Area &dstArea, Rectf *resultSrcRect, Area *resultDstRect );
157 
158 } // namespace cinder