CUGL
Cornell University Game Library
CUPoly2.h
1 //
2 // CUPoly2.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides a class that represents a simple polygon. The purpose
6 // of this class is to separate the geometry (and math) of a polygon from the
7 // rendering data of a pipeline. The class provides the index data necessary
8 // for rendering. In addition, it has some primitive geometry methods such as
9 // containment.
10 //
11 // In OpenGL style, the object is defined by a set of vertices, and a set of
12 // indices that define a mesh over the vertices. This class is intentionally
13 // (based on experience in previous semesters) lightweight. There is no
14 // verification that indices are properly defined. It is up to the user to
15 // verify and specify the components. If you need help with triangulation
16 // or path extrusion, use one the the related factory classes.
17 //
18 // Because math objects are intended to be on the stack, we do not provide
19 // any shared pointer support in this class.
20 //
21 // CUGL zlib License:
22 // This software is provided 'as-is', without any express or implied
23 // warranty. In no event will the authors be held liable for any damages
24 // arising from the use of this software.
25 //
26 // Permission is granted to anyone to use this software for any purpose,
27 // including commercial applications, and to alter it and redistribute it
28 // freely, subject to the following restrictions:
29 //
30 // 1. The origin of this software must not be misrepresented; you must not
31 // claim that you wrote the original software. If you use this software
32 // in a product, an acknowledgment in the product documentation would be
33 // appreciated but is not required.
34 //
35 // 2. Altered source versions must be plainly marked as such, and must not
36 // be misrepresented as being the original software.
37 //
38 // 3. This notice may not be removed or altered from any source distribution.
39 //
40 // Author: Walker White
41 // Version: 6/20/16
42 
43 #ifndef __CU_POLY2_H__
44 #define __CU_POLY2_H__
45 
46 #include <vector>
47 #include "CUVec2.h"
48 #include "CURect.h"
49 
50 namespace cugl {
51 
52 // Forward references
53 class Mat4;
54 class Affine2;
55 
115 class Poly2 {
116 #pragma mark Values
117 public:
123  enum class Type {
127  UNDEFINED,
133  SOLID,
139  PATH
140  };
141 
142 private:
144  std::vector<Vec2> _vertices;
146  std::vector<unsigned short> _indices;
148  Rect _bounds;
150  Type _type;
151 
152 #pragma mark -
153 #pragma mark Constructors
154 public:
161  Poly2() : _type(Type::UNDEFINED) { }
162 
170  Poly2(const std::vector<Vec2>& vertices) { set(vertices); }
171 
188  Poly2(const std::vector<Vec2>& vertices, const std::vector<unsigned short>& indices) {
189  set(vertices, indices);
190  }
191 
205  Poly2(const std::vector<float>& vertices) { set(vertices); }
206 
227  Poly2(const std::vector<float>& vertices, const std::vector<unsigned short>& indices) {
228  set(vertices, indices);
229  }
230 
240  Poly2(Vec2* vertices, int vertsize, int voffset=0) {
241  set(vertices, vertsize, voffset);
242  }
243 
257  Poly2(float* vertices, int vertsize, int voffset=0) {
258  set(vertices, vertsize, voffset);
259  }
260 
281  Poly2(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
282  int voffset=0, int ioffset=0) {
283  set(vertices, vertsize, indices, indxsize, voffset, ioffset);
284  }
285 
310  Poly2(float* vertices, int vertsize, unsigned short* indices, int indxsize,
311  int voffset=0, int ioffset=0) {
312  set(vertices, vertsize, indices, indxsize, voffset, ioffset);
313  }
314 
323  Poly2(const Poly2& poly) { set(poly); }
324 
340  Poly2(const Rect& rect, bool solid=true) { set(rect,solid); }
341 
345  ~Poly2() { }
346 
347 
348 #pragma mark -
349 #pragma mark Static Constructors
350 
360  static Poly2 createLine(const Vec2& origin, const Vec2& dest);
361 
376  static Poly2* createLine(const Vec2& origin, const Vec2& dest, Poly2* dst);
377 
391  static Poly2 createTriangle(const Vec2& a, const Vec2& b, const Vec2& c, bool solid=true);
392 
410  static Poly2* createTriangle(const Vec2& a, const Vec2& b, const Vec2& c, Poly2* dst, bool solid=true);
411 
425  static Poly2 createEllipse(const Vec2& center, const Size& size, unsigned int segments, bool solid=true);
426 
441  static Poly2* createEllipse(const Vec2& center, const Size& size, unsigned int segments, Poly2* dst,
442  bool solid=true);
443 
444 #pragma mark -
445 #pragma mark Setters
446 
457  Poly2& operator= (const Poly2& other) { return set(other); }
458 
470  Poly2& operator= (const Rect& rect) { return set(rect); }
471 
483  Poly2& set(const std::vector<Vec2>& vertices);
484 
503  Poly2& set(const std::vector<Vec2>& vertices, const std::vector<unsigned short>& indices);
504 
520  Poly2& set(const std::vector<float>& vertices);
521 
546  Poly2& set(const std::vector<float>& vertices, const std::vector<unsigned short>& indices);
547 
561  Poly2& set(Vec2* vertices, int vertsize, int voffset=0);
562 
580  Poly2& set(float* vertices, int vertsize, int voffset=0) {
581  return set((Vec2*)vertices, vertsize/2, voffset/2);
582  }
583 
608  Poly2& set(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
609  int voffset=0, int ioffset=0);
610 
639  Poly2& set(float* vertices, int vertsize, unsigned short* indices, int indxsize,
640  int voffset=0, int ioffset=0) {
641  return set((Vec2*)vertices, vertsize/2, indices, indxsize, voffset/2, ioffset);
642  }
643 
657  Poly2& set(const Poly2& poly);
658 
676  Poly2& set(const Rect& rect, bool solid=true);
677 
684  _vertices.clear();
685  _indices.clear();
686  _type = Type::UNDEFINED;
687  _bounds = Rect::ZERO;
688  return *this;
689  }
690 
691 #pragma mark -
692 #pragma mark Index Methods
693 
712  Poly2& setIndices(const std::vector<unsigned short>& indices);
713 
737  Poly2& setIndices(unsigned short* indices, int indxsize, int ioffset=0);
738 
756  bool isStandardized();
757 
776  bool isValid();
777 
778 
779 #pragma mark -
780 #pragma mark Polygon Attributes
781 
792  Vec2& at(int index) { return _vertices.at(index); }
793 
802  const std::vector<Vec2>& getVertices() const { return _vertices; }
803 
812  const std::vector<unsigned short>& getIndices() const { return _indices; }
813 
824  std::vector<unsigned short>& getIndices() { return _indices; }
825 
834  const Rect& getBounds() const { return _bounds; }
835 
851  Type getType() const { return _type; }
852 
868  void setType(Type type) { _type = type; }
869 
870 
871 #pragma mark -
872 #pragma mark Operators
873 
884  Poly2& operator*=(float scale);
885 
897  Poly2& operator*=(const Vec2& scale);
898 
906  Poly2& operator*=(const Affine2& transform);
907 
917  Poly2& operator*=(const Mat4& transform);
918 
930  Poly2& operator/=(float scale);
931 
943  Poly2& operator/=(const Vec2& scale);
944 
952  Poly2& operator+=(float offset);
953 
961  Poly2& operator+=(const Vec2& offset);
962 
970  Poly2& operator-=(float offset);
971 
979  Poly2& operator-=(const Vec2& offset);
980 
994  Poly2 operator*(float scale) const { return Poly2(*this) *= scale; }
995 
1009  Poly2 operator*(const Vec2& scale) const { return Poly2(*this) *= scale; }
1010 
1011 
1021  Poly2 operator*=(const Affine2& transform) const { return Poly2(*this) *= transform; }
1022 
1034  Poly2 operator*(const Mat4& transform) const { return Poly2(*this) *= transform; }
1035 
1049  Poly2 operator/(float scale) const { return Poly2(*this) /= scale; }
1050 
1064  Poly2 operator/(const Vec2& scale) const { return Poly2(*this) /= scale; }
1065 
1075  Poly2 operator+(float offset) const { return Poly2(*this) += offset; }
1076 
1086  Poly2 operator+(const Vec2& offset) const { return Poly2(*this) += offset; }
1087 
1097  Poly2 operator-(float offset) { return Poly2(*this) -= offset; }
1098 
1108  Poly2 operator-(const Vec2& offset) { return Poly2(*this) -= offset; }
1109 
1121  friend Poly2 operator*(float scale, const Poly2& poly) { return poly*scale; }
1122 
1134  friend Poly2 operator*(const Vec2& scale, const Poly2& poly) { return poly*scale; }
1135 
1136 #pragma mark -
1137 #pragma mark Geometry Methods
1138 
1154  std::vector<Vec2> convexHull() const;
1155 
1167  bool contains(const Vec2& point) const;
1168 
1185  bool incident(const Vec2& point, float variance=CU_MATH_EPSILON) const;
1186 
1187 
1188 #pragma mark -
1189 #pragma mark Internal Helper Methods
1190 private:
1197  void computeBounds();
1198 
1208  void computeType();
1209 
1223  Vec2 getBarycentric(const Vec2& point, unsigned short index) const;
1224 
1225  // Make friends with the factory classes
1226  friend class CubicSplineApproximator;
1227  friend class SimpleTriangulator;
1228  friend class PathOutliner;
1229  friend class PathExtruder;
1230 };
1231 
1232 }
1233 #endif /* __CU_POLY2_H__ */
Definition: CUSize.h:57
static const Rect ZERO
Definition: CURect.h:54
Poly2 & operator*=(float scale)
Type
Definition: CUPoly2.h:123
Definition: CUSimpleTriangulator.h:66
Definition: CUPoly2.h:115
Definition: CUVec2.h:61
Vec2 & at(int index)
Definition: CUPoly2.h:792
friend Poly2 operator*(float scale, const Poly2 &poly)
Definition: CUPoly2.h:1121
bool isStandardized()
Poly2 operator*(float scale) const
Definition: CUPoly2.h:994
Poly2 operator*(const Vec2 &scale) const
Definition: CUPoly2.h:1009
Poly2 operator/(const Vec2 &scale) const
Definition: CUPoly2.h:1064
Definition: CUAffine2.h:63
const Rect & getBounds() const
Definition: CUPoly2.h:834
void setType(Type type)
Definition: CUPoly2.h:868
std::vector< unsigned short > & getIndices()
Definition: CUPoly2.h:824
Poly2 & operator/=(float scale)
Poly2(const Poly2 &poly)
Definition: CUPoly2.h:323
Poly2(const std::vector< float > &vertices, const std::vector< unsigned short > &indices)
Definition: CUPoly2.h:227
Poly2 & operator-=(float offset)
Poly2 operator/(float scale) const
Definition: CUPoly2.h:1049
Poly2(const std::vector< float > &vertices)
Definition: CUPoly2.h:205
Poly2(Vec2 *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:281
Poly2 & clear()
Definition: CUPoly2.h:683
Poly2 operator+(const Vec2 &offset) const
Definition: CUPoly2.h:1086
Poly2 & operator=(const Poly2 &other)
Definition: CUPoly2.h:457
bool contains(const Vec2 &point) const
Poly2 operator+(float offset) const
Definition: CUPoly2.h:1075
Poly2(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:257
Type getType() const
Definition: CUPoly2.h:851
static Poly2 createLine(const Vec2 &origin, const Vec2 &dest)
Poly2 operator-(const Vec2 &offset)
Definition: CUPoly2.h:1108
Poly2()
Definition: CUPoly2.h:161
Poly2 & set(float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:639
Definition: CUPathExtruder.h:129
std::vector< Vec2 > convexHull() const
bool incident(const Vec2 &point, float variance=CU_MATH_EPSILON) const
Poly2 operator*(const Mat4 &transform) const
Definition: CUPoly2.h:1034
Poly2(float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:310
bool isValid()
Definition: CURect.h:45
Poly2 operator-(float offset)
Definition: CUPoly2.h:1097
Poly2 & operator+=(float offset)
Poly2(Vec2 *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:240
const std::vector< unsigned short > & getIndices() const
Definition: CUPoly2.h:812
Poly2 & set(const std::vector< Vec2 > &vertices)
~Poly2()
Definition: CUPoly2.h:345
Poly2(const std::vector< Vec2 > &vertices)
Definition: CUPoly2.h:170
const std::vector< Vec2 > & getVertices() const
Definition: CUPoly2.h:802
friend Poly2 operator*(const Vec2 &scale, const Poly2 &poly)
Definition: CUPoly2.h:1134
Definition: CUPathOutliner.h:78
Poly2 & setIndices(const std::vector< unsigned short > &indices)
Poly2 & set(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:580
Definition: CUAnimationNode.h:52
Poly2 operator*=(const Affine2 &transform) const
Definition: CUPoly2.h:1021
Definition: CUMat4.h:92
static Poly2 createTriangle(const Vec2 &a, const Vec2 &b, const Vec2 &c, bool solid=true)
Poly2(const Rect &rect, bool solid=true)
Definition: CUPoly2.h:340
Poly2(const std::vector< Vec2 > &vertices, const std::vector< unsigned short > &indices)
Definition: CUPoly2.h:188
Definition: CUCubicSplineApproximator.h:70
static Poly2 createEllipse(const Vec2 &center, const Size &size, unsigned int segments, bool solid=true)