CUGL 1.2
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MIT 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 
330  Poly2(Poly2&& poly) :
331  _vertices(std::move(poly._vertices)), _indices(std::move(poly._indices)),
332  _bounds(std::move(poly._bounds)), _type(poly._type) {}
333 
349  Poly2(const Rect& rect, bool solid=true) { set(rect,solid); }
350 
354  ~Poly2() { }
355 
356 
357 #pragma mark -
358 #pragma mark Static Constructors
359 
369  static Poly2 createLine(const Vec2& origin, const Vec2& dest);
370 
385  static Poly2* createLine(const Vec2& origin, const Vec2& dest, Poly2* dst);
386 
400  static Poly2 createTriangle(const Vec2& a, const Vec2& b, const Vec2& c, bool solid=true);
401 
419  static Poly2* createTriangle(const Vec2& a, const Vec2& b, const Vec2& c, Poly2* dst, bool solid=true);
420 
434  static Poly2 createEllipse(const Vec2& center, const Size& size, unsigned int segments, bool solid=true);
435 
450  static Poly2* createEllipse(const Vec2& center, const Size& size, unsigned int segments, Poly2* dst,
451  bool solid=true);
452 
453 #pragma mark -
454 #pragma mark Setters
455 
466  Poly2& operator= (const Poly2& other) { return set(other); }
467 
475  Poly2& operator=(Poly2&& other) {
476  _vertices = std::move(other._vertices);
477  _indices = std::move(other._indices);
478  _bounds = std::move(other._bounds);
479  _type = other._type;
480  return *this;
481  }
482 
483 
495  Poly2& operator= (const Rect& rect) { return set(rect); }
496 
508  Poly2& set(const std::vector<Vec2>& vertices);
509 
528  Poly2& set(const std::vector<Vec2>& vertices, const std::vector<unsigned short>& indices);
529 
545  Poly2& set(const std::vector<float>& vertices);
546 
571  Poly2& set(const std::vector<float>& vertices, const std::vector<unsigned short>& indices);
572 
586  Poly2& set(Vec2* vertices, int vertsize, int voffset=0);
587 
605  Poly2& set(float* vertices, int vertsize, int voffset=0) {
606  return set((Vec2*)vertices, vertsize/2, voffset/2);
607  }
608 
633  Poly2& set(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
634  int voffset=0, int ioffset=0);
635 
664  Poly2& set(float* vertices, int vertsize, unsigned short* indices, int indxsize,
665  int voffset=0, int ioffset=0) {
666  return set((Vec2*)vertices, vertsize/2, indices, indxsize, voffset/2, ioffset);
667  }
668 
682  Poly2& set(const Poly2& poly);
683 
701  Poly2& set(const Rect& rect, bool solid=true);
702 
709  _vertices.clear();
710  _indices.clear();
711  _type = Type::UNDEFINED;
712  _bounds = Rect::ZERO;
713  return *this;
714  }
715 
716 #pragma mark -
717 #pragma mark Index Methods
718 
737  Poly2& setIndices(const std::vector<unsigned short>& indices);
738 
762  Poly2& setIndices(unsigned short* indices, int indxsize, int ioffset=0);
763 
781  bool isStandardized();
782 
801  bool isValid();
802 
803 
804 #pragma mark -
805 #pragma mark Polygon Attributes
806 
811  size_t size() const { return _vertices.size(); }
812 
818  size_t indexSize() const { return _indices.size(); }
819 
831  Vec2& at(int index) { return _vertices.at(index); }
832 
841  const std::vector<Vec2>& getVertices() const { return _vertices; }
842 
851  const std::vector<unsigned short>& getIndices() const { return _indices; }
852 
863  std::vector<unsigned short>& getIndices() { return _indices; }
864 
873  const Rect& getBounds() const { return _bounds; }
874 
890  Type getType() const { return _type; }
891 
907  void setType(Type type) { _type = type; }
908 
909 
910 #pragma mark -
911 #pragma mark Operators
912 
923  Poly2& operator*=(float scale);
924 
936  Poly2& operator*=(const Vec2& scale);
937 
945  Poly2& operator*=(const Affine2& transform);
946 
956  Poly2& operator*=(const Mat4& transform);
957 
969  Poly2& operator/=(float scale);
970 
982  Poly2& operator/=(const Vec2& scale);
983 
991  Poly2& operator+=(float offset);
992 
1000  Poly2& operator+=(const Vec2& offset);
1001 
1009  Poly2& operator-=(float offset);
1010 
1018  Poly2& operator-=(const Vec2& offset);
1019 
1033  Poly2 operator*(float scale) const { return Poly2(*this) *= scale; }
1034 
1048  Poly2 operator*(const Vec2& scale) const { return Poly2(*this) *= scale; }
1049 
1050 
1060  Poly2 operator*=(const Affine2& transform) const { return Poly2(*this) *= transform; }
1061 
1073  Poly2 operator*(const Mat4& transform) const { return Poly2(*this) *= transform; }
1074 
1088  Poly2 operator/(float scale) const { return Poly2(*this) /= scale; }
1089 
1103  Poly2 operator/(const Vec2& scale) const { return Poly2(*this) /= scale; }
1104 
1114  Poly2 operator+(float offset) const { return Poly2(*this) += offset; }
1115 
1125  Poly2 operator+(const Vec2& offset) const { return Poly2(*this) += offset; }
1126 
1136  Poly2 operator-(float offset) { return Poly2(*this) -= offset; }
1137 
1147  Poly2 operator-(const Vec2& offset) { return Poly2(*this) -= offset; }
1148 
1160  friend Poly2 operator*(float scale, const Poly2& poly) { return poly*scale; }
1161 
1173  friend Poly2 operator*(const Vec2& scale, const Poly2& poly) { return poly*scale; }
1174 
1175 #pragma mark -
1176 #pragma mark Geometry Methods
1177 
1193  std::vector<Vec2> convexHull() const;
1194 
1206  bool contains(const Vec2& point) const;
1207 
1224  bool incident(const Vec2& point, float variance=CU_MATH_EPSILON) const;
1225 
1226 
1227 #pragma mark -
1228 #pragma mark Internal Helper Methods
1229 private:
1236  void computeBounds();
1237 
1247  void computeType();
1248 
1262  Vec3 getBarycentric(const Vec2& point, unsigned short index) const;
1263 
1264  // Make friends with the factory classes
1265  friend class CubicSplineApproximator;
1266  friend class SimpleTriangulator;
1267  friend class DelaunayTriangulator;
1268  friend class PathOutliner;
1269  friend class PathExtruder;
1270 };
1271 
1272 }
1273 #endif /* __CU_POLY2_H__ */
Definition: CUSize.h:57
size_t indexSize() const
Definition: CUPoly2.h:818
static const Rect ZERO
Definition: CURect.h:54
Poly2 & operator*=(float scale)
Type
Definition: CUPoly2.h:123
Definition: CUSimpleTriangulator.h:66
Definition: CUDelaunayTriangulator.h:75
Definition: CUPoly2.h:115
Definition: CUVec2.h:61
Vec2 & at(int index)
Definition: CUPoly2.h:831
friend Poly2 operator*(float scale, const Poly2 &poly)
Definition: CUPoly2.h:1160
bool isStandardized()
Poly2 operator*(float scale) const
Definition: CUPoly2.h:1033
Poly2 operator*(const Vec2 &scale) const
Definition: CUPoly2.h:1048
Poly2 operator/(const Vec2 &scale) const
Definition: CUPoly2.h:1103
Definition: CUAffine2.h:63
const Rect & getBounds() const
Definition: CUPoly2.h:873
void setType(Type type)
Definition: CUPoly2.h:907
Poly2(Poly2 &&poly)
Definition: CUPoly2.h:330
std::vector< unsigned short > & getIndices()
Definition: CUPoly2.h:863
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:1088
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:708
Poly2 operator+(const Vec2 &offset) const
Definition: CUPoly2.h:1125
Poly2 & operator=(const Poly2 &other)
Definition: CUPoly2.h:466
bool contains(const Vec2 &point) const
Poly2 operator+(float offset) const
Definition: CUPoly2.h:1114
Poly2(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:257
Type getType() const
Definition: CUPoly2.h:890
static Poly2 createLine(const Vec2 &origin, const Vec2 &dest)
Poly2 operator-(const Vec2 &offset)
Definition: CUPoly2.h:1147
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:664
size_t size() const
Definition: CUPoly2.h:811
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:1073
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:1136
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:851
Poly2 & set(const std::vector< Vec2 > &vertices)
Definition: CUVec3.h:61
~Poly2()
Definition: CUPoly2.h:354
Poly2(const std::vector< Vec2 > &vertices)
Definition: CUPoly2.h:170
const std::vector< Vec2 > & getVertices() const
Definition: CUPoly2.h:841
friend Poly2 operator*(const Vec2 &scale, const Poly2 &poly)
Definition: CUPoly2.h:1173
Definition: CUPathOutliner.h:79
Poly2 & setIndices(const std::vector< unsigned short > &indices)
Poly2 & set(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:605
Definition: CUAction.h:51
Poly2 operator*=(const Affine2 &transform) const
Definition: CUPoly2.h:1060
Definition: CUMat4.h:83
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:349
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)
Poly2 & operator=(Poly2 &&other)
Definition: CUPoly2.h:475