Cornell Cocos
Cornell Extensions to Cocos2d
|
#include <CUPoly2.h>
Public Types | |
enum | Traversal { Traversal::OPEN, Traversal::CLOSED, Traversal::INTERIOR } |
enum | Joint { Joint::NONE, Joint::MITRE, Joint::BEVEL, Joint::ROUND } |
enum | Cap { Cap::NONE, Cap::SQUARE, Cap::ROUND } |
Public Member Functions | |
Poly2 () | |
Poly2 (const vector< Vec2 > &vertices) | |
Poly2 (const vector< Vec2 > &vertices, const vector< unsigned short > &indices) | |
Poly2 (const vector< float > &vertices) | |
Poly2 (const vector< float > &vertices, const vector< unsigned short > &indices) | |
Poly2 (Vec2 *vertices, int vertsize, int voffset=0) | |
Poly2 (float *vertices, int vertsize, int voffset=0) | |
Poly2 (Vec2 *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0) | |
Poly2 (float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0) | |
Poly2 (const Poly2 &poly) | |
Poly2 (const Rect &rect, bool index=true) | |
~Poly2 () | |
Poly2 & | set (const vector< Vec2 > &vertices) |
Poly2 & | set (const vector< Vec2 > &vertices, const vector< unsigned short > &indices) |
Poly2 & | set (const vector< float > &vertices) |
Poly2 & | set (const vector< float > &vertices, const vector< unsigned short > &indices) |
Poly2 & | set (Vec2 *vertices, int vertsize, int voffset=0) |
Poly2 & | set (float *vertices, int vertsize, int voffset=0) |
Poly2 & | set (Vec2 *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0) |
Poly2 & | set (float *vertices, int vertsize, unsigned short *indices, int indxsize, int voffset=0, int ioffset=0) |
Poly2 & | set (const Poly2 &poly) |
Poly2 & | set (const Rect &rect, bool index=true) |
Poly2 & | setLine (const Vec2 &origin, const Vec2 &dest) |
Poly2 & | setEllipse (const Vec2 ¢er, const Size &size, unsigned int segments) |
Poly2 & | setIndices (const vector< unsigned short > &indices) |
Poly2 & | setIndices (unsigned short *indices, int indxsize, int ioffset=0) |
Poly2 & | triangulate () |
Poly2 & | traverse (Traversal traversal=Traversal::CLOSED) |
Poly2 * | extrude (Poly2 &poly, float stroke, bool closed=true, Joint joint=Joint::NONE, Cap cap=Cap::NONE) |
const vector< Vec2 > & | getVertices () const |
const vector< unsigned short > & | getIndices () const |
const Rect & | getBounds () const |
Poly2 & | operator= (const Poly2 &other) |
Poly2 & | operator*= (float scale) |
Poly2 & | operator*= (const Vec2 &scale) |
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 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) |
Friends | |
Poly2 | operator* (float scale, const Poly2 &poly) |
Poly2 | operator* (const Vec2 &scale, const Poly2 &poly) |
Poly2 | operator+ (float offset, const Poly2 &poly) |
Poly2 | operator+ (const Vec2 &offset, const Poly2 &poly) |
Class to represent a simple polygon.
This class can represent any polygon (including non-convex polygons) that does not have holes or self-interections. It will not check for holes or self-intersections; those are the responsibility of the programmer.
When instantiating this class, the user can 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 class does not enforce any semantic meaning on these indices. This class simply stores them. The semantic meaning is up the appropriate scene graph or graphics class.
This class does provide several methods for automatic index generation. There is a triangulate() method which will triangulate automatically via an ear-clipping. algorithm. There is also a traveral method for creating indices for a wireframe.
|
strong |
|
strong |
|
strong |
|
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.
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. Remember that vertex ii refers to both elements 2*ii and 2*ii+1 in the array of vertices. This constructor does not verify that the set of indices is valid.
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.
vertices | The vector of vertices (as floats) 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. This constructor does not verify that the set of indices is valid.
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.
vertices | The array of vertices (as Vec2) in this polygon |
vertsize | The number of elements to use from vertices |
voffset | The offset in vertices to start the polygon |
|
inline |
Creates a polygon with the given vertices
The new polygon has no indices.
vertices | The array of vertices (as floats) in this polygon |
vertsize | The number of elements to use from vertices |
voffset | The offset in vertices to start the 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. This constructor does not verify that the set of indices is valid.
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 |
voffset | The offset in vertices to start the polygon |
ioffset | The offset in indices to start from |
|
inline |
Creates a polygon with the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. This constructor does not verify that the set of indices is valid.
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 |
voffset | The offset in vertices to start the polygon |
ioffset | The offset in indices to start from |
|
inline |
Creates a copy of the given polygon.
Both the vertices and the indices are copied.
poly | The polygon to copy |
|
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. This is faster than calling the triangulate() method directly.
rect | The rectangle to copy |
index | Whether to generate indices for the rect |
|
inline |
Deletes the given polygon.
Poly2 * Poly2::extrude | ( | Poly2 & | poly, |
float | stroke, | ||
bool | closed = true , |
||
Joint | joint = Joint::NONE , |
||
Cap | cap = Cap::NONE |
||
) |
Generates a new polygon that is an extrusion of this one.
An extrusion of a polygon is a second polygon that follows the path of the first one, but gives it width. Hence it takes a path and turns it into a solid shape. This is more complicated than simply triangulating the original polygon. The new polygon has more vertices, depending on the choice of joint (shape at the corners) and cap (shape at the end).
Unlike the traverse option, this method cannot be used to extrude an internal polygon tesselation. It assumes that the path is continuous.
CREDITS: This code is ported from the Kivy implementation of Line in package kivy.vertex_instructions. My belief is that this port is acceptable within the scope of the Kivy license. There are no specific credits in that file, so there is no one specific to credit. However, thanks to the Kivy team for doing the heavy lifting on this method.
Because they did all the hard work, I will plug their picture of how joints and end caps work:
http://kivy.org/docs/_images/line-instruction.png
This method does not allocate a new polygon. You provide it with a polygon to store the values. This method returns a reference to that polygon (not the original one) for chaining.
poly | The polygon to store the new vertices and indices |
stroke | The stroke width of the extrusion |
closed | Whether or not the polygon path is closed |
joint | The joint shape (default NONE) for corners |
cap | The cap shape (default NONE) for the two ends |
|
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 a reference to list of indices index.
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 |
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 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 |
|
inline |
Returns a new polygon by scaling the vertices non-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 non-uniform scaling factor |
|
inline |
Returns a new polygon by transforming all of the vertices of this polygon.
The vertices are transformed as points; they are treated if they are homongenous vectors with z = 0.
Mat4 | The transform matrix |
Poly2 & 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 |
Poly2 & Poly2::operator*= | ( | const Vec2 & | scale | ) |
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 non-uniform scaling factor |
Poly2 & Poly2::operator*= | ( | const Mat4 & | transform | ) |
Transforms all of the vertices of this polygon.
The vertices are transformed as points; they are treated if they are homongenous vectors with z = 0.
Mat4 | The transform matrix |
|
inline |
Returns a new polygon by translating the vertices uniformly.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The uniform translation amount |
|
inline |
Returns a new polygon by translating the vertices non-uniformly.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The non-uniform translation amount |
Poly2 & Poly2::operator+= | ( | float | offset | ) |
Uniformly translates all of the vertices of this polygon.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The uniform translation amount |
Poly2 & Poly2::operator+= | ( | const Vec2 & | offset | ) |
Non-uniformly translates all of the vertices of this polygon.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The non-uniform translation amount |
|
inline |
Returns a new polygon by translating the vertices uniformly.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The inverse of the uniform translation amount |
|
inline |
Returns a new polygon by translating the vertices non-uniformly.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The inverse of the non-uniform translation amount |
Poly2 & Poly2::operator-= | ( | float | offset | ) |
Uniformly translates all of the vertices of this polygon.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The inverse of the uniform translation amount |
Poly2 & Poly2::operator-= | ( | const Vec2 & | offset | ) |
Non-uniformly translates all of the vertices of this polygon.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The inverse of the non-uniform translation amount |
|
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.
scale | The inverse of the uniform scaling factor |
|
inline |
Returns a new polygon by scaling the vertices non-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 inverse of the non-uniform scaling factor |
Poly2 & 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 |
Poly2 & Poly2::operator/= | ( | const Vec2 & | scale | ) |
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 non-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.
poly | The polygon to copy |
Poly2 & Poly2::set | ( | const vector< Vec2 > & | vertices | ) |
Sets the polygon to have the given vertices
The new polygon has no indices.
This method returns a reference to this polygon for chaining.
vertices | The vector of vertices (as Vec2) in this polygon |
Poly2 & Poly2::set | ( | const vector< Vec2 > & | vertices, |
const vector< unsigned short > & | 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. Remember that vertex ii refers to both elements 2*ii and 2*ii+1 in the array of vertices. This constructor does not verify that the set of indices is valid.
This method returns a reference to this polygon for chaining.
vertices | The vector of vertices (as Vec2) in this polygon |
indices | The vector of indices for the rendering |
Poly2 & Poly2::set | ( | const vector< float > & | vertices | ) |
Sets the polygon to have the given vertices
The new polygon has no indices.
This method returns a reference to this polygon for chaining.
vertices | The vector of vertices (as floats) in this polygon |
Poly2 & Poly2::set | ( | const vector< float > & | vertices, |
const vector< unsigned short > & | indices | ||
) |
Sets a polygon to have the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. This constructor does not verify that the set of indices is valid.
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 |
Poly2 & Poly2::set | ( | Vec2 * | vertices, |
int | vertsize, | ||
int | voffset = 0 |
||
) |
Sets the polygon to have the given vertices.
The new polygon has no indices.
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 |
voffset | The offset in vertices to start the polygon |
|
inline |
Sets the polygon to have the given vertices.
The new polygon has no indices.
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 |
voffset | The offset in vertices to start the polygon |
Poly2 & Poly2::set | ( | Vec2 * | vertices, |
int | vertsize, | ||
unsigned short * | indices, | ||
int | indxsize, | ||
int | voffset = 0 , |
||
int | ioffset = 0 |
||
) |
Sets the polygon to have the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. This constructor does not verify that the set of indices is valid.
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 |
voffset | The offset in vertices to start the polygon |
ioffset | The offset in indices to start from |
|
inline |
Sets the polygon to have the given vertices and indices.
A valid list of indices must only refer to vertices in the vertex array. Remember that, for float representation, vertex ii refers to both elements 2*ii and 2*ii+1 in the array of vertices. This constructor does not verify that the set of indices is valid.
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 |
voffset | The offset in vertices to start the polygon |
ioffset | The offset in indices to start from |
Sets this polygon to be a copy of the given one.
Both the vertices and the indices are copied. All of the content arrays are copied, so that this polygon does not hold any references to elements of poly.
This method returns a reference to this polygon for chaining.
poly | The polygon to copy |
Poly2 & Poly2::set | ( | const Rect & | rect, |
bool | index = true |
||
) |
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. This is faster than calling the triangulate() method directly.
This method returns a reference to this polygon for chaining.
rect | The rectangle to copy |
index | Whether to generate indices for the rect |
Poly2 & Poly2::setEllipse | ( | const Vec2 & | center, |
const Size & | size, | ||
unsigned int | segments | ||
) |
Sets the polygon to represent an ellipse of the given dimensions.
The triangulation will show the boundary, not the circle tesselation.
center | The ellipse center point. |
size | The size of the ellipse. |
segments | The number of segments to use. |
Poly2 & Poly2::setIndices | ( | const vector< unsigned short > & | 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. Remember that vertex ii refers to both elements 2*ii and 2*ii+1 in the array vertices. This constructor does not verify that the set of indices is valid.
indices | The vector of indices for the rendering |
Poly2 & Poly2::setIndices | ( | unsigned short * | indices, |
int | indxsize, | ||
int | ioffset = 0 |
||
) |
Sets the indices for this polygon to the ones given.
A valid list of indices must only refer to vertices in the vertex array. Remember that vertex ii refers to both elements 2*ii and 2*ii+1 in the array vertices. This constructor does not verify that the set of indices is valid.
The provided array is copied. The polygon does not retain a reference. 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 |
ioffset | The offset in indices to start from |
Poly2 & Poly2::setLine | ( | const Vec2 & | origin, |
const Vec2 & | dest | ||
) |
Sets the polygon to represent a line from origin to dest.
This method returns a reference to this polygon for chaining.
origin | The line origin. |
dest | The line destination. |
Poly2 & Poly2::traverse | ( | Traversal | traversal = Traversal::CLOSED | ) |
Generates indices from a traversal of the polygon vertices.
A traversal is useful for creating a wireframe from a polygon. The type of wireframe generated depends on the traversal value:
OPEN: The traversal is in order, but does not close the ends. CLOSED: The traversal is in order, and closes the ends. INTERIOR: The traverse will outline the default triangulation.
The default is CLOSED_TRAVERSAL.
This method returns a reference to this polygon for chaining.
traversal | The path to traverse for index generation. |
Poly2 & Poly2::triangulate | ( | ) |
Generates indices from a default triangulation of this polygon.
This method uses ear-clipping for triangulation, which is faster than poly2tri for our simple applications.
This method returns a reference to this polygon for chaining.
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 |
Returns a new polygon by scaling the vertices non-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 non-uniform scaling factor |
Returns a new polygon by translating the vertices uniformly.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The uniform translation amount |
Returns a new polygon by translating the vertices non-uniformly.
The vertices are all shifted by the given amount. Note that this operation has no effect on getTriangles(), as the drawing vertices are always normalized to have an origin relative to the polygon bounding box.
offset | The non-uniform translation amount |