Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
CUPoly2.h
1 //
2 // CUPoly2.h
3 // Cornell Extensions to Cocos2D
4 //
5 // This module provides a class that represents a simple polygon. The purpose of
6 // this class is to separate the geometry (and math) of a polygon from the rendering
7 // data of a pipeline. This concept is way too tightly coupled throughout all of
8 // Cocos2d, and it makes very simple things extremely complicated. Instead, rendering
9 // is pushed to the scene graph nodes (the subclasses of TexturedNode), where it
10 // belongs.
11 //
12 // The class supports automatic triangulation when desired. Triangulation must
13 // be called explicitly, because not all applications (e.g. wireframes) want
14 // triangulation. This module uses ear-clipping for triangulation, which is faster
15 // than poly2tri for our simple applications.
16 //
17 // Math data types are much lighter-weight than other objects, and are intended to
18 // be copied. That is why we do not use reference counting for these objects.
19 //
20 // Author: Walker White
21 // Version: 11/15/15
22 //
23 #ifndef __CU_POLY2_H__
24 #define __CU_POLY2_H__
25 
26 #include <vector>
27 #include <math/CCGeometry.h>
28 
29 using namespace std;
30 
31 NS_CC_BEGIN
32 
33 #pragma mark -
34 #pragma mark Poly2
35 
53 class CC_DLL Poly2 {
54 public:
55 #pragma mark Renderer Enums
56 
57  enum class Traversal {
59  OPEN,
61  CLOSED,
63  INTERIOR
64  };
66  enum class Joint {
68  NONE,
70  MITRE,
72  BEVEL,
74  ROUND
75  };
77  enum class Cap {
79  NONE,
81  SQUARE,
83  ROUND
84  };
85 
86 private:
88  vector<Vec2> _vertices;
90  vector<unsigned short> _indices;
91  // CACHED DATA
93  Rect _bounds;
94 
96 
102  void computeBounds();
103 
104 
105 public:
106 #pragma mark Constructors
107 
113  Poly2() { }
114 
122  Poly2(const vector<Vec2>& vertices) { set(vertices); }
123 
134  Poly2(const vector<Vec2>& vertices, const vector<unsigned short>& indices) {
135  set(vertices, indices);
136  }
137 
145  Poly2(const vector<float>& vertices) { set(vertices); }
146 
156  Poly2(const vector<float>& vertices, const vector<unsigned short>& indices) {
157  set(vertices, indices);
158  }
159 
169  Poly2(Vec2* vertices, int vertsize, int voffset=0) {
170  set(vertices, vertsize, voffset);
171  }
172 
182  Poly2(float* vertices, int vertsize, int voffset=0) {
183  set(vertices, vertsize, voffset);
184  }
185 
199  Poly2(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
200  int voffset=0, int ioffset=0) {
201  set(vertices, vertsize, indices, indxsize, voffset, ioffset);
202  }
203 
217  Poly2(float* vertices, int vertsize, unsigned short* indices, int indxsize,
218  int voffset=0, int ioffset=0) {
219  set(vertices, vertsize, indices, indxsize, voffset, ioffset);
220  }
221 
229  Poly2(const Poly2& poly) { set(poly); }
230 
242  Poly2(const Rect& rect, bool index=true) { set(rect,index); }
243 
247  ~Poly2() { }
248 
249 
250 #pragma mark Initializers
251 
262  Poly2& set(const vector<Vec2>& vertices);
263 
278  Poly2& set(const vector<Vec2>& vertices, const vector<unsigned short>& indices);
279 
291  Poly2& set(const vector<float>& vertices);
292 
306  Poly2& set(const vector<float>& vertices, const vector<unsigned short>& indices);
307 
321  Poly2& set(Vec2* vertices, int vertsize, int voffset=0);
322 
336  Poly2& set(float* vertices, int vertsize, int voffset=0) {
337  return set((Vec2*)vertices, vertsize/2, voffset/2);
338  }
339 
357  Poly2& set(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
358  int voffset=0, int ioffset=0);
359 
379  Poly2& set(float* vertices, int vertsize, unsigned short* indices, int indxsize,
380  int voffset=0, int ioffset=0) {
381  return set((Vec2*)vertices, vertsize/2, indices, indxsize, voffset/2, ioffset);
382  }
383 
396  Poly2& set(const Poly2& poly);
397 
413  Poly2& set(const Rect& rect, bool index=true);
414 
425  Poly2& setLine(const Vec2& origin, const Vec2& dest);
426 
438  Poly2& setEllipse(const Vec2& center, const Size& size, unsigned int segments);
439 
440 
441 #pragma mark Index Generation
442 
453  Poly2& setIndices(const vector<unsigned short>& indices);
454 
471  Poly2& setIndices(unsigned short* indices, int indxsize, int ioffset=0);
472 
483  Poly2& triangulate();
484 
505  Poly2& traverse(Traversal traversal=Traversal::CLOSED);
506 
542  Poly2* extrude(Poly2& poly, float stroke, bool closed=true, Joint joint=Joint::NONE, Cap cap=Cap::NONE);
543 
544 
545 #pragma mark Accessors
546 
554  const vector<Vec2>& getVertices() const { return _vertices; }
555 
564  const vector<unsigned short>& getIndices() const { return _indices; }
565 
574  const Rect& getBounds() const { return _bounds; }
575 
576 
577 #pragma mark Operators
578 
589  Poly2& operator= (const Poly2& other) { return set(other); }
590 
602  Poly2& operator*=(float scale);
603 
615  Poly2& operator*=(const Vec2& scale);
616 
627  Poly2& operator*=(const Mat4& transform);
628 
640  Poly2& operator/=(float scale);
641 
653  Poly2& operator/=(const Vec2& scale);
654 
666  Poly2& operator+=(float offset);
667 
679  Poly2& operator+=(const Vec2& offset);
680 
692  Poly2& operator-=(float offset);
693 
705  Poly2& operator-=(const Vec2& offset);
706 
718  Poly2 operator*(float scale) const { return Poly2(*this) *= scale; }
719 
731  Poly2 operator*(const Vec2& scale) const { return Poly2(*this) *= scale; }
732 
743  Poly2 operator*(const Mat4& transform) const { return Poly2(*this) *= transform; }
744 
756  Poly2 operator/(float scale) const { return Poly2(*this) /= scale; }
757 
769  Poly2 operator/(const Vec2& scale) const { return Poly2(*this) /= scale; }
770 
782  Poly2 operator+(float offset) const { return Poly2(*this) += offset; }
783 
795  Poly2 operator+(const Vec2& offset) const { return Poly2(*this) += offset; }
796 
808  Poly2 operator-(float offset) { return Poly2(*this) -= offset; }
809 
821  Poly2 operator-(const Vec2& offset) { return Poly2(*this) -= offset; }
822 
834  friend Poly2 operator*(float scale, const Poly2& poly) { return poly*scale; }
835 
847  friend Poly2 operator*(const Vec2& scale, const Poly2& poly) { return poly*scale; }
848 
860  friend Poly2 operator+(float offset, const Poly2& poly) { return poly+offset; }
861 
873  friend Poly2 operator+(const Vec2& offset, const Poly2& poly) { return poly+offset; }
874 
875 };
876 
877 NS_CC_END
878 #endif /* defined(__CU_POLY2_H__) */
const vector< Vec2 > & getVertices() const
Definition: CUPoly2.h:554
const Rect & getBounds() const
Definition: CUPoly2.h:574
~Poly2()
Definition: CUPoly2.h:247
Poly2 operator+(const Vec2 &offset) const
Definition: CUPoly2.h:795
Poly2(Vec2 *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:199
Poly2(const vector< float > &vertices, const vector< unsigned short > &indices)
Definition: CUPoly2.h:156
Poly2 operator/(const Vec2 &scale) const
Definition: CUPoly2.h:769
Poly2(const vector< Vec2 > &vertices)
Definition: CUPoly2.h:122
Poly2 & set(float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:379
Poly2(float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:217
const vector< unsigned short > & getIndices() const
Definition: CUPoly2.h:564
Poly2 operator-(float offset)
Definition: CUPoly2.h:808
Poly2 & set(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:336
friend Poly2 operator+(const Vec2 &offset, const Poly2 &poly)
Definition: CUPoly2.h:873
Poly2()
Definition: CUPoly2.h:113
Poly2(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:182
Poly2 operator*(const Mat4 &transform) const
Definition: CUPoly2.h:743
Poly2 operator-(const Vec2 &offset)
Definition: CUPoly2.h:821
Poly2(Vec2 *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:169
Poly2(const Rect &rect, bool index=true)
Definition: CUPoly2.h:242
Poly2 operator+(float offset) const
Definition: CUPoly2.h:782
friend Poly2 operator+(float offset, const Poly2 &poly)
Definition: CUPoly2.h:860
Cap
Definition: CUPoly2.h:77
Joint
Definition: CUPoly2.h:66
Poly2 operator*(const Vec2 &scale) const
Definition: CUPoly2.h:731
Definition: CUPoly2.h:53
Poly2 operator/(float scale) const
Definition: CUPoly2.h:756
Traversal
Definition: CUPoly2.h:57
friend Poly2 operator*(float scale, const Poly2 &poly)
Definition: CUPoly2.h:834
friend Poly2 operator*(const Vec2 &scale, const Poly2 &poly)
Definition: CUPoly2.h:847
Poly2 operator*(float scale) const
Definition: CUPoly2.h:718
Poly2(const vector< float > &vertices)
Definition: CUPoly2.h:145
Poly2(const Poly2 &poly)
Definition: CUPoly2.h:229
Poly2(const vector< Vec2 > &vertices, const vector< unsigned short > &indices)
Definition: CUPoly2.h:134