CUGL 1.3
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 
109 class Poly2 {
110 #pragma mark Values
111 public:
117  enum class Type {
121  UNDEFINED,
127  SOLID,
133  PATH
134  };
135 
136 private:
138  std::vector<Vec2> _vertices;
140  std::vector<unsigned short> _indices;
142  Rect _bounds;
144  Type _type;
145 
146 #pragma mark -
147 #pragma mark Constructors
148 public:
155  Poly2() : _type(Type::UNDEFINED) { }
156 
164  Poly2(const std::vector<Vec2>& vertices) { set(vertices); }
165 
182  Poly2(const std::vector<Vec2>& vertices, const std::vector<unsigned short>& indices) {
183  set(vertices, indices);
184  }
185 
199  Poly2(const std::vector<float>& vertices) { set(vertices); }
200 
221  Poly2(const std::vector<float>& vertices, const std::vector<unsigned short>& indices) {
222  set(vertices, indices);
223  }
224 
234  Poly2(Vec2* vertices, int vertsize, int voffset=0) {
235  set(vertices, vertsize, voffset);
236  }
237 
251  Poly2(float* vertices, int vertsize, int voffset=0) {
252  set(vertices, vertsize, voffset);
253  }
254 
275  Poly2(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
276  int voffset=0, int ioffset=0) {
277  set(vertices, vertsize, indices, indxsize, voffset, ioffset);
278  }
279 
304  Poly2(float* vertices, int vertsize, unsigned short* indices, int indxsize,
305  int voffset=0, int ioffset=0) {
306  set(vertices, vertsize, indices, indxsize, voffset, ioffset);
307  }
308 
317  Poly2(const Poly2& poly) { set(poly); }
318 
324  Poly2(Poly2&& poly) :
325  _vertices(std::move(poly._vertices)), _indices(std::move(poly._indices)),
326  _bounds(std::move(poly._bounds)), _type(poly._type) {}
327 
343  Poly2(const Rect& rect, bool solid=true) { set(rect,solid); }
344 
348  ~Poly2() { }
349 
350 
351 #pragma mark -
352 #pragma mark Static Constructors
353 
363  static Poly2 createLine(const Vec2& origin, const Vec2& dest);
364 
379  static Poly2* createLine(const Vec2& origin, const Vec2& dest, Poly2* dst);
380 
394  static Poly2 createTriangle(const Vec2& a, const Vec2& b, const Vec2& c, bool solid=true);
395 
413  static Poly2* createTriangle(const Vec2& a, const Vec2& b, const Vec2& c, Poly2* dst, bool solid=true);
414 
428  static Poly2 createEllipse(const Vec2& center, const Size& size, unsigned int segments, bool solid=true);
429 
444  static Poly2* createEllipse(const Vec2& center, const Size& size, unsigned int segments, Poly2* dst,
445  bool solid=true);
446 
447 #pragma mark -
448 #pragma mark Setters
449 
460  Poly2& operator= (const Poly2& other) { return set(other); }
461 
469  Poly2& operator=(Poly2&& other) {
470  _vertices = std::move(other._vertices);
471  _indices = std::move(other._indices);
472  _bounds = std::move(other._bounds);
473  _type = other._type;
474  return *this;
475  }
476 
477 
489  Poly2& operator= (const Rect& rect) { return set(rect); }
490 
502  Poly2& set(const std::vector<Vec2>& vertices);
503 
522  Poly2& set(const std::vector<Vec2>& vertices, const std::vector<unsigned short>& indices);
523 
539  Poly2& set(const std::vector<float>& vertices);
540 
565  Poly2& set(const std::vector<float>& vertices, const std::vector<unsigned short>& indices);
566 
580  Poly2& set(Vec2* vertices, int vertsize, int voffset=0);
581 
599  Poly2& set(float* vertices, int vertsize, int voffset=0) {
600  return set((Vec2*)vertices, vertsize/2, voffset/2);
601  }
602 
627  Poly2& set(Vec2* vertices, int vertsize, unsigned short* indices, int indxsize,
628  int voffset=0, int ioffset=0);
629 
658  Poly2& set(float* vertices, int vertsize, unsigned short* indices, int indxsize,
659  int voffset=0, int ioffset=0) {
660  return set((Vec2*)vertices, vertsize/2, indices, indxsize, voffset/2, ioffset);
661  }
662 
676  Poly2& set(const Poly2& poly);
677 
695  Poly2& set(const Rect& rect, bool solid=true);
696 
703  _vertices.clear();
704  _indices.clear();
705  _type = Type::UNDEFINED;
706  _bounds = Rect::ZERO;
707  return *this;
708  }
709 
710 #pragma mark -
711 #pragma mark Index Methods
712 
731  Poly2& setIndices(const std::vector<unsigned short>& indices);
732 
756  Poly2& setIndices(unsigned short* indices, int indxsize, int ioffset=0);
757 
775  bool isStandardized();
776 
795  bool isValid();
796 
797 
798 #pragma mark -
799 #pragma mark Polygon Attributes
800 
805  size_t size() const { return _vertices.size(); }
806 
812  size_t indexSize() const { return _indices.size(); }
813 
825  Vec2& at(int index) { return _vertices.at(index); }
826 
835  const std::vector<Vec2>& getVertices() const { return _vertices; }
836 
845  const std::vector<unsigned short>& getIndices() const { return _indices; }
846 
857  std::vector<unsigned short>& getIndices() { return _indices; }
858 
867  const Rect& getBounds() const { return _bounds; }
868 
884  Type getType() const { return _type; }
885 
901  void setType(Type type) { _type = type; }
902 
903 
904 #pragma mark -
905 #pragma mark Operators
906 
917  Poly2& operator*=(float scale);
918 
930  Poly2& operator*=(const Vec2& scale);
931 
939  Poly2& operator*=(const Affine2& transform);
940 
950  Poly2& operator*=(const Mat4& transform);
951 
963  Poly2& operator/=(float scale);
964 
976  Poly2& operator/=(const Vec2& scale);
977 
985  Poly2& operator+=(float offset);
986 
994  Poly2& operator+=(const Vec2& offset);
995 
1003  Poly2& operator-=(float offset);
1004 
1012  Poly2& operator-=(const Vec2& offset);
1013 
1027  Poly2 operator*(float scale) const { return Poly2(*this) *= scale; }
1028 
1042  Poly2 operator*(const Vec2& scale) const { return Poly2(*this) *= scale; }
1043 
1044 
1054  Poly2 operator*=(const Affine2& transform) const { return Poly2(*this) *= transform; }
1055 
1067  Poly2 operator*(const Mat4& transform) const { return Poly2(*this) *= transform; }
1068 
1082  Poly2 operator/(float scale) const { return Poly2(*this) /= scale; }
1083 
1097  Poly2 operator/(const Vec2& scale) const { return Poly2(*this) /= scale; }
1098 
1108  Poly2 operator+(float offset) const { return Poly2(*this) += offset; }
1109 
1119  Poly2 operator+(const Vec2& offset) const { return Poly2(*this) += offset; }
1120 
1130  Poly2 operator-(float offset) { return Poly2(*this) -= offset; }
1131 
1141  Poly2 operator-(const Vec2& offset) { return Poly2(*this) -= offset; }
1142 
1155  friend Poly2 operator*(float scale, const Poly2& poly) { return poly*scale; }
1156 
1169  friend Poly2 operator*(const Vec2& scale, const Poly2& poly) { return poly*scale; }
1170 
1171 #pragma mark -
1172 #pragma mark Geometry Methods
1173 
1189  std::vector<Vec2> convexHull() const;
1190 
1202  bool contains(const Vec2& point) const;
1203 
1220  bool incident(const Vec2& point, float variance=CU_MATH_EPSILON) const;
1221 
1222 
1223 #pragma mark -
1224 #pragma mark Internal Helper Methods
1225 private:
1232  void computeBounds();
1233 
1243  void computeType();
1244 
1258  Vec3 getBarycentric(const Vec2& point, unsigned short index) const;
1259 
1260  // Make friends with the factory classes
1261  friend class CubicSplineApproximator;
1262  friend class SimpleTriangulator;
1263  friend class DelaunayTriangulator;
1264  friend class PathOutliner;
1265  friend class PathExtruder;
1266 };
1267 
1268 }
1269 #endif /* __CU_POLY2_H__ */
cugl::PathOutliner
Definition: CUPathOutliner.h:79
cugl::Poly2::operator+
Poly2 operator+(const Vec2 &offset) const
Definition: CUPoly2.h:1119
cugl::CubicSplineApproximator
Definition: CUCubicSplineApproximator.h:70
cugl::Poly2::Type
Type
Definition: CUPoly2.h:117
cugl::Poly2::set
Poly2 & set(float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:658
cugl::Poly2::Poly2
Poly2(const std::vector< float > &vertices, const std::vector< unsigned short > &indices)
Definition: CUPoly2.h:221
cugl::Poly2::getVertices
const std::vector< Vec2 > & getVertices() const
Definition: CUPoly2.h:835
cugl::Poly2::createTriangle
static Poly2 createTriangle(const Vec2 &a, const Vec2 &b, const Vec2 &c, bool solid=true)
cugl::Poly2::Poly2
Poly2()
Definition: CUPoly2.h:155
cugl::Poly2::getBounds
const Rect & getBounds() const
Definition: CUPoly2.h:867
cugl::Poly2::set
Poly2 & set(const std::vector< Vec2 > &vertices)
cugl::Poly2::Type::UNDEFINED
cugl::Poly2::operator*
Poly2 operator*(float scale) const
Definition: CUPoly2.h:1027
cugl::Poly2::operator*
Poly2 operator*(const Mat4 &transform) const
Definition: CUPoly2.h:1067
cugl::Poly2::createEllipse
static Poly2 createEllipse(const Vec2 &center, const Size &size, unsigned int segments, bool solid=true)
cugl::Poly2::Poly2
Poly2(const std::vector< Vec2 > &vertices)
Definition: CUPoly2.h:164
cugl::Size
Definition: CUSize.h:57
cugl::Poly2::isValid
bool isValid()
cugl::Poly2::operator*
friend Poly2 operator*(float scale, const Poly2 &poly)
Definition: CUPoly2.h:1155
cugl::Affine2
Definition: CUAffine2.h:63
cugl::Poly2::operator=
Poly2 & operator=(Poly2 &&other)
Definition: CUPoly2.h:469
cugl::Poly2::Poly2
Poly2(const Rect &rect, bool solid=true)
Definition: CUPoly2.h:343
cugl::Poly2::operator/
Poly2 operator/(float scale) const
Definition: CUPoly2.h:1082
cugl::Poly2::operator*=
Poly2 & operator*=(float scale)
cugl::Poly2::Poly2
Poly2(const std::vector< Vec2 > &vertices, const std::vector< unsigned short > &indices)
Definition: CUPoly2.h:182
cugl::Poly2::operator/=
Poly2 & operator/=(float scale)
cugl::Poly2::operator/
Poly2 operator/(const Vec2 &scale) const
Definition: CUPoly2.h:1097
cugl::Rect
Definition: CURect.h:45
cugl::Mat4
Definition: CUMat4.h:83
cugl::PathExtruder
Definition: CUPathExtruder.h:129
cugl::Poly2::operator*
Poly2 operator*(const Vec2 &scale) const
Definition: CUPoly2.h:1042
cugl::Poly2::set
Poly2 & set(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:599
cugl::Poly2::Poly2
Poly2(float *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:251
cugl::Poly2::Poly2
Poly2(const Poly2 &poly)
Definition: CUPoly2.h:317
cugl::Poly2::operator*=
Poly2 operator*=(const Affine2 &transform) const
Definition: CUPoly2.h:1054
cugl::Poly2::operator*
friend Poly2 operator*(const Vec2 &scale, const Poly2 &poly)
Definition: CUPoly2.h:1169
cugl::Poly2::incident
bool incident(const Vec2 &point, float variance=CU_MATH_EPSILON) const
cugl::Poly2::getIndices
std::vector< unsigned short > & getIndices()
Definition: CUPoly2.h:857
cugl::Poly2::~Poly2
~Poly2()
Definition: CUPoly2.h:348
cugl::Poly2::convexHull
std::vector< Vec2 > convexHull() const
cugl::Poly2::isStandardized
bool isStandardized()
cugl::Poly2::getIndices
const std::vector< unsigned short > & getIndices() const
Definition: CUPoly2.h:845
cugl::Poly2::contains
bool contains(const Vec2 &point) const
cugl::Poly2
Definition: CUPoly2.h:109
cugl::Poly2::Type::PATH
cugl::Poly2::Type::SOLID
cugl::Vec2
Definition: CUVec2.h:61
cugl::Poly2::indexSize
size_t indexSize() const
Definition: CUPoly2.h:812
cugl::Poly2::Poly2
Poly2(Poly2 &&poly)
Definition: CUPoly2.h:324
cugl::Poly2::operator+
Poly2 operator+(float offset) const
Definition: CUPoly2.h:1108
cugl::Poly2::setType
void setType(Type type)
Definition: CUPoly2.h:901
cugl::Poly2::Poly2
Poly2(Vec2 *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:275
cugl::Poly2::createLine
static Poly2 createLine(const Vec2 &origin, const Vec2 &dest)
cugl::Poly2::operator-=
Poly2 & operator-=(float offset)
cugl::Poly2::at
Vec2 & at(int index)
Definition: CUPoly2.h:825
cugl::SimpleTriangulator
Definition: CUSimpleTriangulator.h:66
cugl::Poly2::size
size_t size() const
Definition: CUPoly2.h:805
cugl::Poly2::Poly2
Poly2(Vec2 *vertices, int vertsize, int voffset=0)
Definition: CUPoly2.h:234
cugl::DelaunayTriangulator
Definition: CUDelaunayTriangulator.h:75
cugl::Poly2::operator=
Poly2 & operator=(const Poly2 &other)
Definition: CUPoly2.h:460
cugl::Poly2::setIndices
Poly2 & setIndices(const std::vector< unsigned short > &indices)
cugl::Vec3
Definition: CUVec3.h:61
cugl::Poly2::Poly2
Poly2(const std::vector< float > &vertices)
Definition: CUPoly2.h:199
cugl::Poly2::getType
Type getType() const
Definition: CUPoly2.h:884
cugl::Rect::ZERO
static const Rect ZERO
Definition: CURect.h:54
cugl::Poly2::clear
Poly2 & clear()
Definition: CUPoly2.h:702
cugl::Poly2::operator-
Poly2 operator-(const Vec2 &offset)
Definition: CUPoly2.h:1141
cugl::Poly2::Poly2
Poly2(float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0)
Definition: CUPoly2.h:304
cugl::Poly2::operator+=
Poly2 & operator+=(float offset)
cugl::Poly2::operator-
Poly2 operator-(float offset)
Definition: CUPoly2.h:1130