Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Shape2d.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/Rect.h"
27 #include "cinder/Path2d.h"
28 #include "cinder/MatrixAffine2.h"
29 
30 #include <vector>
31 
32 namespace cinder {
33 
34 class Shape2d {
35  public:
36  void moveTo( const Vec2f &p );
37  void moveTo( float x, float y ) { moveTo( Vec2f( x, y ) ); }
38  void lineTo( const Vec2f &p );
39  void lineTo( float x, float y ) { lineTo( Vec2f( x, y ) ); }
40  void quadTo( const Vec2f &p1, const Vec2f &p2 );
41  void quadTo( float x1, float y1, float x2, float y2 ) { quadTo( Vec2f( x1, y1 ), Vec2f( x2, y2 ) ); }
42  void curveTo( const Vec2f &p1, const Vec2f &p2, const Vec2f &p3 );
43  void curveTo( float x1, float y1, float x2, float y2, float x3, float y3 ) { curveTo( Vec2f( x1, y1 ), Vec2f( x2, y2 ), Vec2f( x3, y3 ) ); }
44  void arc( const Vec2f &center, float radius, float startRadians, float endRadians, bool forward = true );
45  void arc( float centerX, float centerY, float radius, float startRadians, float endRadians, bool forward = true ) { arc( Vec2f( centerX, centerY ), radius, startRadians, endRadians, forward ); }
46  void arcTo( const Vec2f &p, const Vec2f &t, float radius );
47  void arcTo( float x, float y, float tanX, float tanY, float radius) { arcTo( Vec2f( x, y ), Vec2f( tanX, tanY ), radius ); }
48  void close();
49 
50  bool empty() const { return mContours.empty(); }
51  void clear() { mContours.clear(); }
52  size_t getNumContours() const { return mContours.size(); }
53 
54  const Path2d& getContour( size_t i ) const { return mContours[i]; }
55  Path2d& getContour( size_t i ) { return mContours[i]; }
56  const std::vector<Path2d>& getContours() const { return mContours; }
57  std::vector<Path2d>& getContours() { return mContours; }
58 
59  const Vec2f& getCurrentPoint() const { return mContours.back().getCurrentPoint(); }
60 
62  void append( const Shape2d &shape );
63  void appendContour( const Path2d &contour ) { mContours.push_back( contour ); }
64  void removeContour( size_t i ) { mContours.erase( mContours.begin() + i ); }
65 
67  void scale( const Vec2f &amount, Vec2f scaleCenter = Vec2f::zero() );
68 
70  void transform( const MatrixAffine2f &matrix );
73 
75  Rectf calcBoundingBox() const;
78 
80  bool contains( const Vec2f &pt ) const;
81 
83 
87  template<typename IT>
88  void iterate( IT &it )
89  {
90  bool stop = false;
91  for( std::vector<Path2d>::const_iterator contourIt = mContours.begin(); contourIt != mContours.end(); ++contourIt ) {
92  size_t pt = 0;
93  it( Path2d::MOVETO, &contourIt->mPoints[0], 0 );
94  pt++;
95  for( std::vector<Path2d::SegmentType>::const_iterator segIt = contourIt->mSegments.begin(); segIt != contourIt->mSegments.end(); ++segIt ) {
96  if( *segIt == Path2d::CLOSE )
97  it( *segIt, &contourIt->mPoints[0], &contourIt->mPoints[pt-1] );
98  else if( ! it( *segIt, &contourIt->mPoints[pt], ( pt > 0 ) ? &contourIt->mPoints[pt-1] : 0 ) ) {
99  stop = true;
100  break;
101  }
102  pt += Path2d::sSegmentTypePointCounts[*segIt];
103  }
104  if( stop ) break;
105  /*else if( contourIt->isClosed() ) {
106  if( ! it( Path2d::CLOSE, contourIt->empty() ? NULL : &contourIt->mPoints[0], ( pt > 0 ) ? &contourIt->mPoints[pt-1] : 0 ) )
107  break;
108  }*/
109  }
110  }
111 
112  private:
113  std::vector<Path2d> mContours;
114 };
115 
116 } // namespace cinder
GLuint GLenum matrix
Definition: GLee.h:10032
GLenum GLint GLint y
Definition: GLee.h:987
void lineTo(float x, float y)
Definition: Shape2d.h:39
Rectf calcBoundingBox() const
Returns the bounding box of the Shape's control points. Note that this is not necessarily the boundin...
Definition: Shape2d.cpp:95
bool empty() const
Definition: Shape2d.h:50
Shape2d transformCopy(const MatrixAffine2f &matrix) const
Returns a copy transformed by matrix.
Definition: Shape2d.cpp:87
void appendContour(const Path2d &contour)
Definition: Shape2d.h:63
Vec2< float > Vec2f
Definition: Vector.h:1314
void arcTo(float x, float y, float tanX, float tanY, float radius)
Definition: Shape2d.h:47
void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
Definition: Shape2d.h:43
const Vec2f & getCurrentPoint() const
Definition: Shape2d.h:59
static Vec2< float > zero()
Definition: Vector.h:295
Definition: Path2d.h:96
void transform(const MatrixAffine2f &matrix)
Transforms the Shape2d by matrix.
Definition: Shape2d.cpp:81
size_t getNumContours() const
Definition: Shape2d.h:52
void append(const Shape2d &shape)
Appends the contours of shape to this shape.
Definition: Shape2d.cpp:69
const Path2d & getContour(size_t i) const
Definition: Shape2d.h:54
Definition: Path2d.h:96
void scale(const Vec2f &amount, Vec2f scaleCenter=Vec2f::zero())
Scales the Shape2d by amount.x on X and amount.y on Y around the center scaleCenter.
Definition: Shape2d.cpp:75
void close()
Definition: Shape2d.cpp:64
void lineTo(const Vec2f &p)
Definition: Shape2d.cpp:37
Rectf calcPreciseBoundingBox() const
Returns the precise bounding box of the Shape's curves. Slower to calculate than calcBoundingBox().
Definition: Shape2d.cpp:115
void removeContour(size_t i)
Definition: Shape2d.h:64
void clear()
Definition: Shape2d.h:51
bool contains(const Vec2f &pt) const
Returns whether the point pt is contained within the boundaries of the shape.
Definition: Shape2d.cpp:135
Path2d & getContour(size_t i)
Definition: Shape2d.h:55
GLenum GLint x
Definition: GLee.h:987
void arc(float centerX, float centerY, float radius, float startRadians, float endRadians, bool forward=true)
Definition: Shape2d.h:45
const std::vector< Path2d > & getContours() const
Definition: Shape2d.h:56
void iterate(IT &it)
Iterates all of the contours and points of a Shape2d.
Definition: Shape2d.h:88
void quadTo(const Vec2f &p1, const Vec2f &p2)
Definition: Shape2d.cpp:42
GLfloat GLfloat p
Definition: GLee.h:8473
std::vector< Path2d > & getContours()
Definition: Shape2d.h:57
void moveTo(const Vec2f &p)
Definition: Shape2d.cpp:31
Definition: Path2d.h:35
void arc(const Vec2f &center, float radius, float startRadians, float endRadians, bool forward=true)
Definition: Shape2d.cpp:52
void curveTo(const Vec2f &p1, const Vec2f &p2, const Vec2f &p3)
Definition: Shape2d.cpp:47
void moveTo(float x, float y)
Definition: Shape2d.h:37
void quadTo(float x1, float y1, float x2, float y2)
Definition: Shape2d.h:41
void arcTo(const Vec2f &p, const Vec2f &t, float radius)
Definition: Shape2d.cpp:59
GLdouble GLdouble t
Definition: GLee.h:1426
static const int sSegmentTypePointCounts[]
Definition: Path2d.h:97
Definition: Shape2d.h:34