CUGL 2.0
Cornell University Game Library

#include <CUPoly2.h>
Public Member Functions  
Poly2 ()  
Poly2 (const std::vector< Vec2 > &vertices)  
Poly2 (const std::vector< Vec2 > &vertices, const std::vector< Uint32 > &indices)  
Poly2 (const std::vector< float > &vertices)  
Poly2 (const std::vector< float > &vertices, const std::vector< Uint32 > &indices)  
Poly2 (const float *vertices, size_t vertsize)  
Poly2 (const float *vertices, size_t vertsize, const Uint32 *indices, size_t indxsize)  
Poly2 (const Poly2 &poly)  
Poly2 (Poly2 &&poly)  
Poly2 (const Rect rect, bool solid=true)  
~Poly2 ()  
Poly2 &  operator= (const Poly2 &other) 
Poly2 &  operator= (Poly2 &&other) 
Poly2 &  operator= (const Rect rect) 
Poly2 &  set (const std::vector< Vec2 > &vertices) 
Poly2 &  set (const std::vector< Vec2 > &vertices, const std::vector< Uint32 > &indices) 
Poly2 &  set (const std::vector< float > &vertices) 
Poly2 &  set (const std::vector< float > &vertices, const std::vector< Uint32 > &indices) 
Poly2 &  set (const Vec2 *vertices, size_t vertsize) 
Poly2 &  set (const float *vertices, size_t vertsize) 
Poly2 &  set (const Vec2 *vertices, size_t vertsize, const Uint32 *indices, size_t indxsize) 
Poly2 &  set (const float *vertices, size_t vertsize, const Uint32 *indices, size_t indxsize) 
Poly2 &  set (const Poly2 &poly) 
Poly2 &  set (const Rect rect, bool solid=true) 
Poly2 &  setIndices (const std::vector< Uint32 > &indices) 
Poly2 &  setIndices (const Uint32 *indices, size_t indxsize) 
Poly2 &  clear () 
size_t  size () const 
size_t  indexSize () const 
Vec2 &  at (int index) 
const std::vector< Vec2 > &  vertices () const 
std::vector< Vec2 > &  vertices () 
const std::vector< Uint32 > &  indices () const 
std::vector< Uint32 > &  indices () 
const Rect  getBounds () const 
Geometry  getGeometry () const 
void  setGeometry (Geometry geom) 
Poly2 &  operator*= (float scale) 
Poly2 &  operator*= (const Vec2 scale) 
Poly2 &  operator*= (const Affine2 &transform) 
Poly2 &  operator*= (const Mat4 &transform) 
Poly2 &  operator/= (float scale) 
Poly2 &  operator/= (const Vec2 scale) 
Poly2 &  operator+= (float offset) 
Poly2 &  operator+= (const Vec2 offset) 
Poly2 &  operator= (float offset) 
Poly2 &  operator= (const Vec2 offset) 
Poly2  operator* (float scale) const 
Poly2  operator* (const Vec2 scale) const 
Poly2  operator* (const Affine2 &transform) const 
Poly2  operator* (const Mat4 &transform) const 
Poly2  operator/ (float scale) const 
Poly2  operator/ (const Vec2 scale) const 
Poly2  operator+ (float offset) const 
Poly2  operator+ (const Vec2 offset) const 
Poly2  operator (float offset) 
Poly2  operator (const Vec2 offset) 
std::vector< Vec2 >  convexHull () const 
bool  contains (Vec2 point, bool implicit=false) const 
bool  contains (float x, float y, bool implicit=false) const 
bool  incident (Vec2 point, float err=CU_MATH_EPSILON) const 
bool  incident (float x, float y, float err=CU_MATH_EPSILON) const 
int  orientation () const 
void  reverse () 
std::string  toString (bool verbose=false) const 
operator std::string () const  
operator Rect () const  
Static Public Member Functions  
static int  orientation (Vec2 a, Vec2 b, Vec2 c) 
Friends  
class  PolyFactory 
class  PolySplineFactory 
class  SimpleTriangulator 
class  ComplexTriangulator 
class  SimpleExtruder 
class  ComplexExtruder 
class  PathSmoother 
Poly2  operator* (float scale, const Poly2 &poly) 
Poly2  operator* (const Vec2 scale, const Poly2 &poly) 
Class to represent a simple polygon.
This class is intended to represent any polygon (including nonconvex polygons). that does not have selfinterections (as these can cause serious problems with the mathematics). Most polygons are simple, meaning that they have no holes. However, this class does support complex polygons with holes, provided that the polygon is not implicit and has an corresponding mesh.
To define a mesh, the user should provide a set of indices which will be used in rendering. These indices can either represent a triangulation of the polygon, or they can represent a traversal (for a wireframe). The semantics of these indices is provided by the associated value. This class performs no verification. It will not check that a mesh is in proper form, nor will it search for holes or selfintersections. These are the responsibility of the programmer.
Generating indices for a Poly2 can be nontrivial. While this class has standard constructors, allowing the programmer full control, most Poly2 objects are created through alternate means. For simple shapes, like lines, triangles, and ellipses, this class has several static constructors.
For more complex shapes, we have several Poly2 factories. These factories allow for delegating index computation to a separate thread, if it takes too long. These factories are as follows:
SimpleTriangulator: This is a simple earclippingtriangulator for tesselating simple, solid polygons (e.g. no holes or selfintersections).
ComplexTriangulator: This is a Delaunay Triangular that gives a more uniform triangulation in accordance to the Vornoi diagram. It also allows you to create 2d meshes while holes inside of them.
PolyFactory: This is a tool is used to generate several basic path shapes, such as rounded rectangles or arcs. It also allows you construct wireframe traversals of existing polygons.
PolySplineFactory: This is a tool is used to generate a Poly2 object from a Cubic Bezier curve.
SimpleExtruder: This is a tool can take a path polygon and convert it into a solid polygon. This solid polygon is the same as the path, except that the path now has a width and a mitre at the joints. It is fast, but has graphical limitations.
ComplexExtruder: Like SimpleExtruder, this is a tool can take a path polygon and convert it into a solid polygon. It is incredibly versatile and works in all instances. However, it is extremely slow (in the 10s of milliseconds) and is unsuitable for calcuations at framerate.

inline 
Creates an empty polygon.
The created polygon has no vertices and no triangulation. The bounding box is trivial.

inline 
Creates a polygon with the given vertices
The new polygon has no indices and the geometry is IMPLICIT
.
vertices  The vector of vertices (as Vec2) in this polygon 

inline 
Creates a polygon with the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize.
vertices  The vector of vertices (as Vec2) in this polygon 
indices  The vector of indices for the rendering 

inline 
Creates a polygon with the given vertices
The new polygon has no indices.
The float array should have an even number of elements. The number of vertices is half of the size of the array. For each value ii, 2*ii and 2*ii+1 are the coordinates of a single vertex.
The new polygon has no indices and the geometry is IMPLICIT
.
vertices  The vector of vertices (as floats) in this polygon 

inline 
Creates a polygon with the given vertices and indices.
The float array should have an even number of elements. The number of vertices is half of the size of the array. For each value ii, 2*ii and 2*ii+1 are the coordinates of a single vertex.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize.
vertices  The vector of vertices (as floats) in this polygon 
indices  The vector of indices for the rendering 

inline 
Creates a polygon with the given vertices
The new polygon has no indices and the geometry is IMPLICIT
.
vertices  The array of vertices (as Vec2) in this polygon 
vertsize  The number of elements to use from vertices 

inline 
Creates a polygon with the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize.
vertices  The array of vertices (as Vec2) in this polygon 
vertsize  The number of elements to use from vertices 
indices  The array of indices for the rendering 
indxsize  The number of elements to use for the indices 

inline 
Creates a copy of the given polygon.
Both the vertices and the indices are copied.No references to the original polygon are kept.
poly  The polygon to copy 

inline 
Creates a copy with the resource of the given polygon.
poly  The polygon to take from 

inline 
Creates a polygon for the given rectangle.
The polygon will have four vertices, one for each corner of the rectangle. This optional argument (which is true by default) will initialize the indices with a triangulation of the rectangle. In other words, the geometry will be SOLID
. This is faster than using one of the more heavyweight triangulators.
If solid is false, it will still generate indices, but will have CLOSED
geometry instead.
rect  The rectangle to copy 
solid  Whether to treat this rectangle as a solid polygon 

inline 
Deletes the given polygon, freeing all resources.

inline 
Returns a reference to the attribute at the given index.
This accessor will allow you to change the (singular) vertex. It is intended to allow minor distortions to the polygon without changing the underlying mesh.
index  The attribute index 
Poly2& cugl::Poly2::clear  (  ) 
Clears the contents of this polygon and sets the geometry to IMPLICIT
bool cugl::Poly2::contains  (  float  x, 
float  y,  
bool  implicit = false 

)  const 
Returns true if this polygon contains the given point.
This method returns false is the geometry is POINTS
. If the geometry is SOLID
, it checks for containment within the associated triangle mesh. Otherwise, it uses an evenodd crossing rule on the polygon edges (either explicit or implicit) to determine containment.
If the value implicit is true, it will treat the polygon implicitly, even if it has a mesh (and no matter the geometry).
Containment is not strict. Points on the boundary are contained within this polygon.
x  The xcoordinate to test 
y  The ycoordinate to test 
implicit  Whether to ignore indices and use evenodd on vertices 

inline 
Returns true if this polygon contains the given point.
This method returns false is the geometry is POINTS
. If the geometry is SOLID
, it checks for containment within the associated triangle mesh. Otherwise, it uses an evenodd crossing rule on the polygon edges (either explicit or implicit) to determine containment.
If the value implicit is true, it will treat the polygon implicitly, even if it has a mesh (and no matter the geometry).
Containment is not strict. Points on the boundary are contained within this polygon.
point  The point to test 
implicit  Whether to ignore indices and use evenodd on vertices 
std::vector<Vec2> cugl::Poly2::convexHull  (  )  const 
Returns the set of points forming the convex hull of this polygon.
The returned set of points is guaranteed to be a counterclockwise traversal of the hull.
The points on the convex hull define the "border" of the shape. In addition to minimizing the number of vertices, this is useful for determining whether or not a point lies on the boundary.
This implementation is adapted from the example at
http://www.geeksforgeeks.org/convexhullset2grahamscan/

inline 
Returns the bounding box for the polygon
The bounding box is the minimal rectangle that contains all of the vertices in this polygon. It is recomputed whenever the vertices are set.

inline 
Returns the geometry of this polygon.
The type determines the proper form of the indices.
If the geometry is SOLID
, the number of indices should be a multiple of 3. Each triplet should define a triangle over the vertices.
If the geometry is PATH
, the number of indices should be a multiple of 2. Each pair should define a line segment over the vertices.
If the polygon is IMPLICIT
, the index list should be empty.
bool cugl::Poly2::incident  (  float  x, 
float  y,  
float  err = CU_MATH_EPSILON 

)  const 
Returns true if the given point is on the boundary of this polygon.
This method generates uses Geometry to determine the boundaries. For `POINTS, it returns true if point is with margin of error of a vertex. For all other shapes it returns true if it is within margin of error of a line segment.
x  The xcoordinate to test 
y  The ycoordinate to test 
err  The distance tolerance 

inline 
Returns true if the given point is on the boundary of this polygon.
This method generates uses Geometry to determine the boundaries. For `POINTS, it returns true if point is with margin of error of a vertex. For all other shapes it returns true if it is within margin of error of a line segment.
point  The point to check 
err  The distance tolerance 

inline 
Returns the number of indices in a polygon.

inline 
Returns a reference to list of indices.
This accessor will not permit any changes to the index array. To change the array, you must change the polygon via a set() method.
This nonconst version of the method is used by triangulators.

inline 
Returns a reference to list of indices.
This accessor will not permit any changes to the index array. To change the array, you must change the polygon via a set() method.

inline 
Cast from Poly to a string.
Returns a new polygon by transforming all of the vertices of this polygon.
Note: This method does not modify the polygon.
transform  The affine transform 
Returns a new polygon by transforming all of the vertices of this polygon.
The vertices are transformed as points. The zvalue is 0.
Note: This method does not modify the polygon.
transform  The transform matrix 
Returns a new polygon by scaling the vertices nonuniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
Note: This method does not modify the polygon.
scale  The nonuniform scaling factor 

inline 
Returns a new polygon by scaling the vertices uniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
Note: This method does not modify the polygon.
scale  The uniform scaling factor 
Transforms all of the vertices of this polygon.
transform  The affine transform 
Transforms all of the vertices of this polygon.
The vertices are transformed as points. The zvalue is 0.
transform  The transform matrix 
Nonuniformly scales all of the vertices of this polygon.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
scale  The nonuniform scaling factor 
Poly2& cugl::Poly2::operator*=  (  float  scale  ) 
Uniformly scales all of the vertices of this polygon.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
scale  The uniform scaling factor 
Returns a new polygon by translating the vertices nonuniformly.
Note: This method does not modify the polygon.
offset  The nonuniform translation amount 

inline 
Returns a new polygon by translating the vertices uniformly.
Note: This method does not modify the polygon.
offset  The uniform translation amount 
Nonuniformly translates all of the vertices of this polygon.
offset  The nonuniform translation amount 
Poly2& cugl::Poly2::operator+=  (  float  offset  ) 
Uniformly translates all of the vertices of this polygon.
offset  The uniform translation amount 
Returns a new polygon by translating the vertices nonuniformly.
Note: This method does not modify the polygon.
offset  The inverse of the nonuniform translation amount 

inline 
Returns a new polygon by translating the vertices uniformly.
Note: This method does not modify the polygon.
offset  The inverse of the uniform translation amount 
Nonuniformly translates all of the vertices of this polygon.
offset  The inverse of the nonuniform translation amount 
Poly2& cugl::Poly2::operator=  (  float  offset  ) 
Uniformly translates all of the vertices of this polygon.
offset  The inverse of the uniform translation amount 
Returns a new polygon by scaling the vertices nonuniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
Note: This method does not modify the polygon.
scale  The inverse of the nonuniform scaling factor 

inline 
Returns a new polygon by scaling the vertices uniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
Note: This method does not modify the polygon.
scale  The inverse of the uniform scaling factor 
Nonuniformly scales all of the vertices of this polygon.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
scale  The inverse of the nonuniform scaling factor 
Poly2& cugl::Poly2::operator/=  (  float  scale  ) 
Uniformly scales all of the vertices of this polygon.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
scale  The inverse of the uniform scaling factor 
Sets this polygon to be a copy of the given one.
All of the contents are copied, so that this polygon does not hold any references to elements of the other polygon. This method returns a reference to this polygon for chaining.
other  The polygon to copy 
Sets this polygon to be a copy of the given rectangle.
The polygon will have four vertices, one for each corner of the rectangle. In addition, this assignment will initialize the indices with a simple triangulation of the rectangle. The geometry will be SOLID
.
rect  The rectangle to copy 
Sets this polygon to be have the resources of the given one.
other  The polygon to take from 
int cugl::Poly2::orientation  (  )  const 
Returns 1, 0, or 1 indicating the polygon orientation.
If the method returns 1, this is a counterclockwise polygon. If 1, it is a clockwise polygon. If 0, that means it is undefined. The orientation of an IMPLICIT
polygon is always defined as long as it has at least three vertices. Polygons with POINTS
geometry never have a defined orientation.
For polygons with PATH
geometry, the orientation is determined by following the path. However, if the path is disconnected, this could result in more than one orientation, making the orientation undefined.
For polygons with SOLID
geometry, the orientiation is that of the triangles in the triangle mesh. However, if this value is not uniform (some triangles have one orientation and others do not), then this orientation is undefined.
Returns 1, 0, or 1 indicating the orientation of a > b > c
If the function returns 1, this is a counterclockwise turn. If 1, it is a clockwise turn. If 0, it is colinear.
a  The first point 
b  The second point 
c  The third point 
void cugl::Poly2::reverse  (  ) 
Reverses the orientation of this polygon.
If the polygon orientation is undefined, then this method does nothing. Otherwise, it reorders either the vertices or the mesh indices to reverse the orientation. Which one is resorted (vertices or indices) is undefined.

inline 
Sets the polygon to have the given vertices.
The float array should have an even number of elements. The number of vertices is half of the size of the array. For each value ii, 2*ii and 2*ii+1 are the coordinates of a single vertex.
The resulting polygon has no indices and the geometry is IMPLICIT
.
This method returns a reference to this polygon for chaining.
vertices  The array of vertices (as floats) in this polygon 
vertsize  The number of elements to use from vertices 

inline 
Sets the polygon to have the given vertices and indices.
The float array should have an even number of elements. The number of vertices is half of the size of the array. For each value ii, 2*ii and 2*ii+1 are the coordinates of a single vertex.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize. This method returns a reference to this polygon for chaining.
vertices  The array of vertices (as floats) in this polygon 
vertsize  The number of elements to use from vertices 
indices  The array of indices for the rendering 
indxsize  The number of elements to use for the indices 
Sets this polygon to be a copy of the given one.
All of the contents are copied, so that this polygon does not hold any references to elements of the other polygon. This method returns a reference to this polygon for chaining.
This method returns a reference to this polygon for chaining.
poly  The polygon to copy 
Sets the polygon to represent the given rectangle.
The polygon will have four vertices, one for each corner of the rectangle. This optional argument (which is true by default) will initialize the indices with a triangulation of the rectangle. In other words, the geometry will be SOLID
. This is faster than using one of the more heavyweight triangulators.
If solid is false, it will still generate indices, but will have CLOSED
geometry instead.
rect  The rectangle to copy 
solid  Whether to treat this rectangle as a solid polygon 
Poly2& cugl::Poly2::set  (  const std::vector< float > &  vertices  ) 
Sets the polygon to have the given vertices
The float array should have an even number of elements. The number of vertices is half of the size of the array. For each value ii, 2*ii and 2*ii+1 are the coordinates of a single vertex.
The resulting polygon has no indices and the geometry is IMPLICIT
.
This method returns a reference to this polygon for chaining.
vertices  The vector of vertices (as floats) in this polygon 
Poly2& cugl::Poly2::set  (  const std::vector< float > &  vertices, 
const std::vector< Uint32 > &  indices  
) 
Sets a polygon to have the given vertices and indices.
The float array should have an even number of elements. The number of vertices is half of the size of the array. For each value ii, 2*ii and 2*ii+1 are the coordinates of a single vertex.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize.
This method returns a reference to this polygon for chaining.
vertices  The vector of vertices (as floats) in this polygon 
indices  The vector of indices for the rendering 
Sets the polygon to have the given vertices
The resulting polygon has no indices and the geometry is IMPLICIT
.
This method returns a reference to this polygon for chaining.
vertices  The vector of vertices (as Vec2) in this polygon 
Poly2& cugl::Poly2::set  (  const std::vector< Vec2 > &  vertices, 
const std::vector< Uint32 > &  indices  
) 
Sets the polygon to have the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize.
vertices  The vector of vertices (as Vec2) in this polygon 
indices  The vector of indices for the rendering 
Sets the polygon to have the given vertices.
The resulting polygon has no indices and the geometry is IMPLICIT
.
This method returns a reference to this polygon for chaining.
vertices  The array of vertices (as Vec2) in this polygon 
vertsize  The number of elements to use from vertices 
Poly2& cugl::Poly2::set  (  const Vec2 *  vertices, 
size_t  vertsize,  
const Uint32 *  indices,  
size_t  indxsize  
) 
Sets the polygon to have the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize. This method returns a reference to this polygon for chaining.
vertices  The array of vertices (as Vec2) in this polygon 
vertsize  The number of elements to use from vertices 
indices  The array of indices for the rendering 
indxsize  The number of elements to use for the indices 

inline 
Returns the geometry of this polygon.
The type determines the proper form of the indices.
If the geometry is SOLID
, the number of indices should be a multiple of 3. Each triplet should define a triangle over the vertices.
If the geometry is PATH
, the number of indices should be a multiple of 2. Each pair should define a line segment over the vertices.
If the polygon is IMPLICIT
, the index list should be empty.
geom  The geometry of this polygon. 
Poly2& cugl::Poly2::setIndices  (  const std::vector< Uint32 > &  indices  ) 
Sets the indices for this polygon to the ones given.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The index geometry will be assigned via Geometry#categorize. This method returns a reference to this polygon for chaining.
indices  The vector of indices for the shape 
Poly2& cugl::Poly2::setIndices  (  const Uint32 *  indices, 
size_t  indxsize  
) 
Sets the indices for this polygon to the ones given.
A valid list of indices must only refer to vertices in the vertex array. That is, the indices should all be nonnegative, and each value should be less than the number of vertices.
The provided array is copied. The polygon does not retain a reference.
The index geometry will be assigned via Geometry#categorize. This method returns a reference to this polygon for chaining.
indices  The array of indices for the rendering 
indxsize  The number of elements to use for the indices 

inline 
Returns the number of vertices in a polygon.
std::string cugl::Poly2::toString  (  bool  verbose = false  )  const 
Returns a string representation of this polygon for debugging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
verbose  Whether to include class information 

inline 
Returns the list of vertices
This accessor will not permit any changes to the vertex array. To change the array, you must change the polygon via a set() method.

inline 
Returns the list of vertices
This accessor will not permit any changes to the vertex array. To change the array, you must change the polygon via a set() method.
Returns a new polygon by scaling the vertices nonuniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
scale  The nonuniform scaling factor 
poly  The polygon to scale 
Returns a new polygon by scaling the vertices uniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.
scale  The uniform scaling factor 
poly  The polygon to scale 