CUGL 2.1
Cornell University Game Library
|
#include <CUPath2.h>
Public Member Functions | |
Path2 () | |
Path2 (const std::vector< Vec2 > &vertices) | |
Path2 (const Vec2 *vertices, size_t vertsize) | |
Path2 (const Path2 &path) | |
Path2 (Path2 &&path) | |
Path2 (const Rect rect) | |
Path2 (const std::shared_ptr< JsonValue > &data) | |
~Path2 () | |
Path2 & | operator= (const Path2 &other) |
Path2 & | operator= (Path2 &&other) |
Path2 & | operator= (const Rect rect) |
Path2 & | operator= (const std::shared_ptr< JsonValue > &data) |
Path2 & | set (const std::vector< Vec2 > &vertices) |
Path2 & | set (const Vec2 *vertices, size_t vertsize) |
Path2 & | set (const Path2 &path) |
Path2 & | set (const Rect rect) |
Path2 & | set (const std::shared_ptr< JsonValue > &data) |
Path2 & | clear () |
bool | empty () const |
size_t | size () const |
bool | isClosed () const |
Vec2 & | at (size_t index) |
const Vec2 & | at (size_t index) const |
bool | isCorner (size_t index) const |
const std::vector< Vec2 > & | getVertices () const |
const Rect | getBounds () const |
std::vector< Uint32 > | getIndices () const |
size_t | getIndices (std::vector< Uint32 > &buffer) const |
Vec2 | pop () |
void | push (Vec2 point, bool corner=false) |
void | push (float x, float y, bool corner=false) |
Vec2 | remove (size_t index) |
void | add (size_t index, Vec2 point, bool corner=false) |
void | add (size_t index, float x, float y, bool corner=false) |
void | reserve (size_t size) |
std::vector< Uint32 > | convexHull () const |
bool | contains (Vec2 point) const |
bool | contains (float x, float y) const |
bool | incident (Vec2 point, float err=CU_MATH_EPSILON) const |
bool | incident (float x, float y, float err=CU_MATH_EPSILON) const |
Uint32 | leftTurns () const |
bool | isConvex () const |
float | area () const |
int | orientation () const |
Path2 & | reverse () |
Path2 | reversed () const |
Path2 & | operator*= (float scale) |
Path2 & | operator*= (const Vec2 scale) |
Path2 & | operator*= (const Affine2 &transform) |
Path2 & | operator*= (const Mat4 &transform) |
Path2 & | operator/= (float scale) |
Path2 & | operator/= (const Vec2 scale) |
Path2 & | operator+= (const Vec2 offset) |
Path2 & | operator-= (const Vec2 offset) |
Path2 | operator* (float scale) const |
Path2 | operator* (const Vec2 scale) const |
Path2 | operator* (const Affine2 &transform) const |
Path2 | operator* (const Mat4 &transform) const |
Path2 | operator/ (float scale) const |
Path2 | operator/ (const Vec2 scale) const |
Path2 | operator+ (const Vec2 offset) const |
Path2 | operator- (const Vec2 offset) |
Path2 & | operator+= (const Path2 &extra) |
Path2 | operator+ (const Path2 &extra) const |
Path2 | slice (size_t start, size_t end) const |
Path2 | sliceFrom (size_t start) const |
Path2 | sliceTo (size_t end) const |
std::string | toString (bool verbose=false) const |
operator std::string () const | |
operator Rect () const | |
Static Public Member Functions | |
static std::vector< Uint32 > | convexHull (const std::vector< Vec2 > &vertices) |
static int | orientation (const Vec2 &a, const Vec2 &b, const Vec2 &c) |
static int | orientation (const std::vector< Vec2 > &path) |
static int | orientation (const Vec2 *path, size_t size) |
Public Attributes | |
std::vector< Vec2 > | vertices |
std::unordered_set< size_t > | corners |
bool | closed |
Friends | |
class | PathSmoother |
class | PathFactory |
class | SplinePather |
class | SimpleExtruder |
class | ComplexExtruder |
class | EarclipTriangulator |
class | DelaunayTriangulator |
Path2 | operator* (float scale, const Path2 &path) |
Path2 | operator* (const Vec2 scale, const Path2 &path) |
Class to represent a flattened polyline.
This class is intended to represent any continuous polyline. While it may be either open or closed, it should not have any gaps between vertices. If you need a path with gaps, that should be represented by multiple Path2 objects.
It is possible to draw a path object directly to a SpriteBatch. However, in most applications you will want to convert a path object to a Poly2 for width and texturing. In particular, you will often want to either extrude (give stroke width) or triangulate (fill) a path.
We have provided several factories for converting a path to a Poly2. These factories allow for delegating index computation to a separate thread, if it takes too long. These factories are as follows:
EarclipTriangulator: This is a simple earclipping-triangulator for tesselating paths into polygons. It supports holes, but does not support self-intersections. While it produces better (e.g. less thin) triangles than MonotoneTriangulator, this comes at a cost. This triangulator has worst case O(n^2). With that said, it has low overhead and so is very efficient on small polygons.
DelaunayTriangulator: This is a Delaunay Triangular that gives a more uniform triangulation in accordance to the Vornoi diagram. This triangulator uses an advancing-front algorithm that is the fastest in practice (though worst case O(n log n) is not guaranteed). However, it has a lot of overhead that is unnecessary for small polygons. As with EarclipTriangulator, it supports holes, but does not support self-intersections.
PathFactory: 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 polygon meshes.
SimpleExtruder: This is a tool can take a path 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. This algorithm is quite fast, but the resulting polygon may overlap itself. This is ideal for strokes that only need to be drawn and do not need accurate geometric information.
ComplexExtruder: Like SimpleExtruder, this is a tool can take a path polygon and convert it into a solid polygon. However it is much more powerful and guarantees that the resulting polygon has no overlaps. Unforunately, it is extremely slow (in the 10s of milliseconds) and is unsuitable for calcuations at framerate.
|
inline |
Creates an empty path.
The created path has no vertices. The bounding box is trivial.
|
inline |
Creates a path with the given vertices
No vertices are marked are as corner vertices. The path will be open.
vertices | The vector of vertices (as Vec2) in this path |
|
inline |
Creates a path with the given vertices
No vertices are marked are as corner vertices. The path will be open.
vertices | The array of vertices in this path |
vertsize | The number of elements to use from vertices |
|
inline |
Creates a copy of the given path.
Both the vertices and the annotations are copied. No references to the original path are kept.
path | The path to copy |
|
inline |
Creates a copy with the resource of the given path.
As a move constructor, this will invalidate the original path
path | The path to take from |
|
inline |
Creates a path for the given rectangle.
The path will have four vertices, one for each corner of the rectangle. It will be closed.
rect | The rectangle to copy |
|
inline |
Creates a path from the given JsonValue
The JsonValue should either be an array of floats or an JSON object. If it is an array of floats, then it interprets those floats as the vertices. All points are corners and the path is closed.
On the other hand, if it is a JSON object, it supports the following attributes:
"vertices": An (even) list of floats, representing the vertices "corners": A list of integers representing corner positions "closed": A boolean value, representing if the path is closed
All attributes are optional. If "vertices" are missing, then the path will be empty. If "corners" is missing, then all vertices are corners. If "closed" is missing, then the path is closed by default.
data | The JSON object specifying the path |
|
inline |
Deletes the given path, freeing all resources.
void cugl::Path2::add | ( | size_t | index, |
float | x, | ||
float | y, | ||
bool | corner = false |
||
) |
Adds a point at the given index
index | The index to add the point |
x | The x-coordinate to add |
y | The y-coordinate to add |
corner | Whether this point is a corner |
void cugl::Path2::add | ( | size_t | index, |
Vec2 | point, | ||
bool | corner = false |
||
) |
Adds a point at the given index
index | The index to add the point |
point | The point to add |
corner | Whether this point is a corner |
float cugl::Path2::area | ( | ) | const |
Returns the area enclosed by this path.
The area is defined as the sum of oriented triangles in a triangle fan from a point on the convex hull. Counter-clockwise triangles have positive area, while clockwise triangles have negative area. The result agrees with the traditional concept of area for counter clockwise paths.
The area can be used to determine the orientation. It the area is negative, that means this path essentially represents a hole (e.g. is clockwise instead of counter-clockwise).
|
inline |
Returns a reference to the point at the given index.
This accessor will allow you to change the (singular) point. It is intended to allow minor distortions to the path without changing the underlying geometry.
index | The path index |
|
inline |
Returns a reference to the point at the given index.
This accessor will allow you to change the (singular) point. It is intended to allow minor distortions to the path without changing the underlying geometry.
index | The path index |
Path2& cugl::Path2::clear | ( | ) |
Clears the contents of this path
bool cugl::Path2::contains | ( | float | x, |
float | y | ||
) | const |
Returns true if the interior of this path contains the given point.
This mehtod returns false if the path is open. Otherwise, it uses an even-odd crossing rule to determine containment. Containment is not strict. Points on the boundary are contained within this polygon.
x | The x-coordinate to test |
y | The y-coordinate to test |
|
inline |
Returns true if the interior of this path contains the given point.
This mehtod returns false if the path is open. Otherwise, it uses an even-odd crossing rule to determine containment. Containment is not strict. Points on the boundary are contained within this polygon.
point | The point to test |
std::vector<Uint32> cugl::Path2::convexHull | ( | ) | const |
Returns the set of points forming the convex hull of this path.
The returned set of points is guaranteed to be a counter-clockwise 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/convex-hull-set-2-graham-scan/
|
static |
Returns the set of points forming the convex hull of the given points.
The returned set of points is guaranteed to be a counter-clockwise 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/convex-hull-set-2-graham-scan/
vertices | The points for the computation |
|
inline |
Returns true if the path is empty.
const Rect cugl::Path2::getBounds | ( | ) | const |
Returns the bounding box for the path
The bounding box is the minimal rectangle that contains all of the vertices in this path. This method will recompute the bounds and is hence O(n).
std::vector<Uint32> cugl::Path2::getIndices | ( | ) | const |
Returns a list of vertex indices representing this path.
The indices are intended to be used in a drawing mesh to display this path. The number of indices will be a multiple of two.
size_t cugl::Path2::getIndices | ( | std::vector< Uint32 > & | buffer | ) | const |
Stores a list of vertex indices in the given buffer.
The indices are intended to be used in a drawing mesh to display this path. The number of indices will be a multiple of two.
The indices will be appended to the provided vector. You should clear the vector first if you do not want to preserve the original data. If the calculation is not yet performed, this method will do nothing.
buffer | a buffer to store the list of indices. |
|
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 path via a set() method.
bool cugl::Path2::incident | ( | float | x, |
float | y, | ||
float | err = CU_MATH_EPSILON |
||
) | const |
Returns true if the given point is on the path.
This method returns true if the point is within margin of error of a line segment.
x | The x-coordinate to test |
y | The y-coordinate to test |
err | The distance tolerance |
|
inline |
Returns true if the given point is on the path.
This method returns true if the point is within margin of error of a line segment.
point | The point to check |
err | The distance tolerance |
|
inline |
Returns whether the path is closed.
bool cugl::Path2::isConvex | ( | ) | const |
Returns true if this path defines a convex shape.
This method returns false if the path is open.
bool cugl::Path2::isCorner | ( | size_t | index | ) | const |
Returns true if the point at the given index is a corner
Corner points will be assigned a joint style when extruded. Points that are not corners will be extruded smoothly (typically because they are the result of a bezier expansion).
index | The attribute index |
Uint32 cugl::Path2::leftTurns | ( | ) | const |
Returns the number of left turns in this path.
Left turns are determined by looking at the interior angle generated at each point (assuming that the path is intended to be counterclockwise). In the case of an open path, the first and last vertexes are not counted.
This method is a generalization of isConvex that can be used to analyze the convexity of a path.
|
inline |
Cast from Path2 to a string.
Returns a new path by transforming all of the vertices of this path.
Note: This method does not modify the path.
transform | The affine transform |
Returns a new path by transforming all of the vertices of this path.
The vertices are transformed as 3d points. The z-value is 0.
Note: This method does not modify the path.
transform | The transform matrix |
Returns a new path 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 path of this path, then the path will be effectively translated by the scaling.
Note: This method does not modify the path.
scale | The non-uniform scaling factor |
|
inline |
Returns a new path by scaling the vertices uniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
Note: This method does not modify the path.
scale | The uniform scaling factor |
Transforms all of the vertices of this path.
transform | The affine transform |
Transforms all of the vertices of this path.
The vertices are transformed as 3d points. The z-value is 0.
transform | The transform matrix |
Nonuniformly scales all of the vertices of this path.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
scale | The non-uniform scaling factor |
Path2& cugl::Path2::operator*= | ( | float | scale | ) |
Uniformly scales all of the vertices of this path.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
scale | The uniform scaling factor |
Returns a new path by appending extra to the end of this path
The vertices are appended in order to the end of the path. If the original path was closed, the copy is open (regardless of whether or not extra is closed)
Note: This method does not modify the original path.
extra | The path to add |
Returns a new path by translating the vertices
Note: This method does not modify the path.
offset | The translation amount |
Appends the given path to the end of this one
The vertices are appended in order to the end of the path. If the original path was closed, it is now open (regardless of whether or not extra is closed)
extra | The path to append |
Translates all of the vertices of this path.
offset | The translation amount |
Returns a new path by translating the vertices
Note: This method does not modify the path.
offset | The inverse of the translation amount |
Translates all of the vertices of this path.
offset | The inverse of the translation amount |
Returns a new path 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 path of this path, then the path will be effectively translated by the scaling.
Note: This method does not modify the path.
scale | The inverse of the non-uniform scaling factor |
|
inline |
Returns a new path by scaling the vertices uniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
Note: This method does not modify the path.
scale | The inverse of the uniform scaling factor |
Nonuniformly scales all of the vertices of this path.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
scale | The inverse of the non-uniform scaling factor |
Path2& cugl::Path2::operator/= | ( | float | scale | ) |
Uniformly scales all of the vertices of this path.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
scale | The inverse of the uniform scaling factor |
Sets this path to be a copy of the given one.
All of the contents are copied, so that this path does not hold any references to elements of the other path.
This method returns a reference to this path for chaining.
other | The path to copy |
Sets this path to be a copy of the given rectangle.
The path will have four vertices, one for each corner of the rectangle. The path will be closed.
This method returns a reference to this path for chaining.
rect | The rectangle to copy |
Sets this path from the data in the given JsonValue
The JsonValue should either be an array of floats or an JSON object. If it is an array of floats, then it interprets those floats as the vertices. All points are corners and the path is closed.
On the other hand, if it is a JSON object, it supports the following attributes:
"vertices": An (even) list of floats, representing the vertices "corners": A list of integers representing corner positions "closed": A boolean value, representing if the path is closed
All attributes are optional. If "vertices" are missing, then the path will be empty. If "corners" is missing, then all vertices are corners. If "closed" is missing, then the path is closed by default.
data | The JSON object specifying the path |
Sets this path to be have the resources of the given one.
This method returns a reference to this path for chaining.
other | The path to take from |
int cugl::Path2::orientation | ( | ) | const |
Returns -1, 0, or 1 indicating the path orientation.
If the method returns -1, this is a counter-clockwise path. If 1, it is a clockwise path. If 0, that means it is undefined. The orientation can be undefined if all the points are colinear.
|
static |
Returns -1, 0, or 1 indicating the path orientation.
If the method returns -1, this is a counter-clockwise path. If 1, it is a clockwise path. If 0, that means it is undefined. The orientation can be undefined if all the points are colinear.
path | The path to check |
Returns -1, 0, or 1 indicating the orientation of a -> b -> c
If the function returns -1, this is a counter-clockwise 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 |
|
static |
Returns -1, 0, or 1 indicating the path orientation.
If the method returns -1, this is a counter-clockwise path. If 1, it is a clockwise path. If 0, that means it is undefined. The orientation can be undefined if all the points are colinear.
path | The path to check |
size | The path size |
Vec2 cugl::Path2::pop | ( | ) |
Returns the former end point in the path, after removing it
If this path is empty, this will return the zero vector.
void cugl::Path2::push | ( | float | x, |
float | y, | ||
bool | corner = false |
||
) |
Adds a point to the end of this path
x | The x-coordinate to add |
y | The y-coordinate to add |
corner | Whether this point is a corner |
void cugl::Path2::push | ( | Vec2 | point, |
bool | corner = false |
||
) |
Adds a point to the end of this path
point | The point to add |
corner | Whether this point is a corner |
Vec2 cugl::Path2::remove | ( | size_t | index | ) |
Returns the former point at the given index, after removing it
If this path is empty, this will return the zero vector.
void cugl::Path2::reserve | ( | size_t | size | ) |
Allocates space in this path for the given number of points.
This method can help performance when a path is being constructed piecemeal.
size | The number of spots allocated for future points. |
Path2& cugl::Path2::reverse | ( | ) |
Reverses the orientation of this path in place
The path will have all of its vertices in the reverse order from the original. This path will not be affected.
Path2 cugl::Path2::reversed | ( | ) | const |
Returns a path with the reverse orientation of this one.
The path will have all of its vertices in the reverse order from the original. This path will not be affected.
Sets this path to be a copy of the given one.
All of the contents are copied, so that this path does not hold any references to elements of the other path.
This method returns a reference to this path for chaining.
path | The path to copy |
Sets the path to represent the given rectangle.
The path will have four vertices, one for each corner of the rectangle. The path will be closed.
This method returns a reference to this path for chaining.
rect | The rectangle to copy |
Sets this path from the data in the given JsonValue
The JsonValue should either be an array of floats or an JSON object. If it is an array of floats, then it interprets those floats as the vertices. All points are corners and the path is closed.
On the other hand, if it is a JSON object, it supports the following attributes:
"vertices": An (even) list of floats, representing the vertices "corners": A list of integers representing corner positions "closed": A boolean value, representing if the path is closed
All attributes are optional. If "vertices" are missing, then the path will be empty. If "corners" is missing, then all vertices are corners. If "closed" is missing, then the path is closed by default.
data | The JSON object specifying the path |
Sets the path to have the given vertices
No vertices are marked are as corner vertices. The path will be open.
This method returns a reference to this path for chaining.
vertices | The vector of vertices (as Vec2) in this path |
Sets the path to have the given vertices.
No vertices are marked are as corner vertices. The path will be open.
This method returns a reference to this path for chaining.
vertices | The array of vertices in this path |
vertsize | The number of elements to use from vertices |
|
inline |
Returns the number of vertices in a path.
Path2 cugl::Path2::slice | ( | size_t | start, |
size_t | end | ||
) | const |
Returns the slice of this path between start and end.
The sliced path will use the indices from start to end (not including end). It will include the vertices referenced by those indices, and only those vertices. The resulting path is open.
start | The start index |
end | The end index |
|
inline |
Returns the slice of this mesh from the start index to the end.
The sliced mesh will use the indices from start to the end. It will include the vertices referenced by those indices, and only those vertices. The resulting path is open.
start | The start index |
|
inline |
Returns the slice of this mesh from the begining to end.
The sliced mesh will use the indices up to (but not including) end. It will include the vertices referenced by those indices, and only those vertices. The resulting path is open.
end | The end index |
std::string cugl::Path2::toString | ( | bool | verbose = false | ) | const |
Returns a string representation of this path 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 |
Returns a new path 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 path of this path, then the path will be effectively translated by the scaling.
scale | The non-uniform scaling factor |
path | The path to scale |
Returns a new path by scaling the vertices uniformly.
The vertices are scaled from the origin of the coordinate space. This means that if the origin is not path of this path, then the path will be effectively translated by the scaling.
scale | The uniform scaling factor |
path | The path to scale |
|
friend |
Friend classes
bool cugl::Path2::closed |
Whether or not this path is closed
std::unordered_set<size_t> cugl::Path2::corners |
The corner points of this path (used for extrusion).
std::vector<Vec2> cugl::Path2::vertices |
The vector of vertices in this path