Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
128  static RectT zero() { return RectT( 0, 0, 0, 0 ); }
129 
130  T x1, y1, x2, y2;
131 
132  friend std::ostream& operator<<( std::ostream &o, const RectT &rect )
133  {
134  return o << "(" << rect.x1 << ", " << rect.y1 << ")-(" << rect.x2 << ", " << rect.y2 << ")";
135  }
136 
137 };
138 
141 
142 
143 // This class maps a rectangle into another rectangle
144 class RectMapping {
145  public:
147  : mSrcRect( 0, 0, 0, 0 ), mDstRect( 0, 0, 0, 0 ) {}
148  RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect )
149  : mSrcRect( aSrcRect ), mDstRect( aDstRect ) {}
150  RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect, bool preserveSrcAspect );
151 
152  Vec2f map( const Vec2f &srcPoint ) const;
153  Rectf map( const Rectf &srcRect ) const;
154 
155  private:
156  Rectf mSrcRect, mDstRect;
157 };
158 
159 extern void getClippedScaledRects( const Area &srcSurfaceBounds, const Rectf &srcRect, const Area &dstSurfaceBounds, const Area &dstArea, Rectf *resultSrcRect, Area *resultDstRect );
160 
161 } // namespace cinder
GLuint GLenum matrix
Definition: GLee.h:10032
T x2
Definition: Rect.h:130
void set(T aX1, T aY1, T aX2, T aY2)
Definition: Rect.cpp:50
RectT scaled(T scale) const
Definition: Rect.cpp:190
T getAspectRatio() const
Definition: Rect.h:52
const RectT< T > operator/(T s) const
Definition: Rect.h:117
static RectT zero()
Constructs a rectangle with all values initialized to zero.
Definition: Rect.h:128
Definition: Area.h:37
RectT inflated(const Vec2< T > &amount) const
Definition: Rect.cpp:131
RectT()
Definition: Rect.h:37
RectT< double > Rectd
Definition: Rect.h:140
RectT transformCopy(const class MatrixAffine2< T > &matrix) const
Returns a copy of the Rect transformed by matrix. Represents the bounding box of the transformed Rect...
Definition: Rect.cpp:202
RectT(T aX1, T aY1, T aX2, T aY2)
Definition: Rect.h:40
const RectT< T > operator+(const Vec2< T > &o) const
Definition: Rect.h:114
T x
Definition: Vector.h:71
T getX2() const
Definition: Rect.h:94
RectT< T > & operator/=(T s)
Definition: Rect.h:125
RectMapping(const Rectf &aSrcRect, const Rectf &aDstRect)
Definition: Rect.h:148
GLfloat GLfloat v1
Definition: GLee.h:2445
bool contains(const Vec2< Y > &pt) const
Is a point pt inside the rectangle.
Definition: Rect.h:80
Definition: Area.h:35
Vec2< T > getSize() const
Definition: Rect.h:102
Definition: Vector.h:68
RectT getCenteredFit(const RectT &other, bool expand) const
Definition: Rect.cpp:297
Vec2< T > getLowerRight() const
Definition: Rect.h:99
const RectT< T > operator-(const RectT< T > &rhs) const
Definition: Rect.h:120
RectT< T > & operator+=(const Vec2< T > &o)
Definition: Rect.h:122
RectT getClipBy(const RectT &clip) const
Definition: Rect.cpp:105
void offsetCenterTo(const Vec2< T > &center)
Translates the rectangle so that its center is at center.
Definition: Rect.h:66
RectT< T > & operator-=(const Vec2< T > &o)
Definition: Rect.h:123
void canonicalize()
Definition: Rect.cpp:59
RectMapping()
Definition: Rect.h:146
T getHeight() const
Definition: Rect.h:51
T distanceSquared(const Vec2< T > &pt) const
Returns the squared distance between the point pt and the rectangle. Points inside the rectangle retu...
Definition: Rect.cpp:242
GLfloat GLfloat GLfloat v2
Definition: GLee.h:2451
RectT(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Rect.h:43
Area getInteriorArea() const
Definition: Rect.cpp:288
GLintptr offset
Definition: GLee.h:2095
T calcArea() const
Definition: Rect.h:53
T getY2() const
Definition: Rect.h:95
T getY1() const
Definition: Rect.h:93
Vec2< T > getCenter() const
Definition: Rect.h:101
T getX1() const
Definition: Rect.h:92
void offset(const Vec2< T > &offset)
Definition: Rect.cpp:113
T y
Definition: Vector.h:71
Vec2< T > getUpperRight() const
Definition: Rect.h:98
Vec2< T > closestPoint(const Vec2< T > &pt) const
Returns the nearest point on the Rect rect. Points inside the rectangle return pt.
Definition: Rect.cpp:254
Vec2< T > getUpperLeft() const
Definition: Rect.h:97
bool intersects(const RectT &rect) const
Returns whether rect intersects with this.
Definition: Rect.cpp:218
Represents a two dimensional affine transformation.
Definition: MatrixAffine2.h:38
const RectT< T > operator*(T s) const
Definition: Rect.h:116
T x1
Definition: Rect.h:130
void inflate(const Vec2< T > &amount)
Definition: Rect.cpp:122
Definition: Rect.h:144
void getClippedScaledRects(const Area &srcSurfaceBounds, const Rectf &srcRect, const Area &dstSurfaceBounds, const Area &dstArea, Rectf *resultSrcRect, Area *resultDstRect)
Definition: Rect.cpp:358
const RectT< T > operator+(const RectT< T > &rhs) const
Definition: Rect.h:119
RectT< float > Rectf
Definition: Rect.h:139
void include(const Vec2< T > &point)
Definition: Rect.cpp:265
RectT scaledCentered(T scale) const
Definition: Rect.cpp:163
friend std::ostream & operator<<(std::ostream &o, const RectT &rect)
Definition: Rect.h:132
GLsizei const GLfloat * points
Definition: GLee.h:6298
T getWidth() const
Definition: Rect.h:50
RectT canonicalized() const
Definition: Rect.cpp:75
T y2
Definition: Rect.h:130
T y1
Definition: Rect.h:130
void clipBy(const RectT &clip)
Definition: Rect.cpp:83
GLenum GLenum GLenum GLenum GLenum scale
Definition: GLee.h:8937
GLdouble s
Definition: GLee.h:1378
RectT< T > & operator*=(T s)
Definition: Rect.h:124
const RectT< T > operator-(const Vec2< T > &o) const
Definition: Rect.h:115
T distance(const Vec2< T > &pt) const
Returns the distance between the point pt and the rectangle. Points inside the rectangle return 0...
Definition: Rect.cpp:227
RectT getOffset(const Vec2< T > &off) const
Definition: Rect.h:62
void scaleCentered(const Vec2< T > &scale)
Definition: Rect.cpp:139
void scale(T scale)
Definition: Rect.cpp:172
Vec2f map(const Vec2f &srcPoint) const
Definition: Rect.cpp:340
Vec2< T > getLowerLeft() const
Definition: Rect.h:100