CUGL 2.1
Cornell University Game Library
|
#include <CUSimpleExtruder.h>
Public Member Functions | |
SimpleExtruder () | |
SimpleExtruder (const std::vector< Vec2 > &points, bool closed) | |
SimpleExtruder (const Path2 &path) | |
~SimpleExtruder () | |
void | setJoint (poly2::Joint joint) |
poly2::Joint | getJoint () const |
void | setEndCap (poly2::EndCap endcap) |
poly2::EndCap | getEndCap () const |
void | setTolerance (float tolerance) |
float | getTolerance () const |
void | setMitreLimit (float limit) |
float | getMitreLimit () const |
void | set (const std::vector< Vec2 > &points, bool closed) |
void | set (const Vec2 *points, size_t size, bool closed) |
void | set (const Path2 &path) |
void | reset () |
void | clear () |
void | calculate (float width) |
void | calculate (float lwidth, float rwidth) |
Poly2 | getPolygon () const |
Poly2 * | getPolygon (Poly2 *buffer) const |
std::vector< Path2 > | getBorder () const |
size_t | getBorder (std::vector< Path2 > &buffer) const |
cugl::Mesh< SpriteVertex2 > | getMesh (Color4 color) const |
cugl::Mesh< SpriteVertex2 > * | getMesh (cugl::Mesh< SpriteVertex2 > *mesh, Color4 color) const |
cugl::Mesh< SpriteVertex2 > | getMesh (Color4 inner, Color4 outer) const |
cugl::Mesh< SpriteVertex2 > * | getMesh (cugl::Mesh< SpriteVertex2 > *mesh, Color4 inner, Color4 outer) const |
Vec2 | getSide (Uint32 index) const |
This class is a factory for extruding paths into a solid polygon.
An extrusion of a path is a polygon that follows the path 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 path. The new polygon has more vertices, depending on the choice of joint (shape at the corners) and cap (shape at the end).
This class is significantly faster than ComplexExtruder. It guarantees sub-millisecond performance for most applications (even thousands of vertices), and so it can be used at framerate. However, this speed comes at significant cost percision. In particular, the Poly2 object created has overlapping triangles, as the algorithm makes no effort to detecting crossing or overlaps. While this is fine for drawing the extrusion with a scene2::PathNode, it is not accurate for high precision geometry computations. In particular, it will not provide an accurate picture of the boundary. For a geometrically accurate extrusion, you should use ComplexExtruder instead.
One advantage of this tool has over ComplexExtruder is that it keeps track of the left and right sides of the extrusion at all times (which is only possible since overlaps are preserved). This information can be abstracted and used to properly fade or texture the stroke.
As with all factories, the methods are broken up into three phases: initialization, calculation, and materialization. To use the factory, you first set the data (in this case a set of vertices or Path2 object) with the initialization methods. You then call the calculation method. Finally, you use the materialization methods to access the data in several different ways.
This division allows us to support multithreaded calculation if the data generation takes too long. However, note that this factory is not thread safe in that you cannot access data while it is still in mid-calculation.
CREDITS: This code heavily inspired by NanoVG by Mikko Mononen (memon), and the bulk of the algorithm is taken from that code. However it is has been altered and optimized according to extensive profiling. @ins ide.o rg
cugl::SimpleExtruder::SimpleExtruder | ( | ) |
Creates an extruder with no vertex data.
cugl::SimpleExtruder::SimpleExtruder | ( | const std::vector< Vec2 > & | points, |
bool | closed | ||
) |
Creates an extruder with the given path.
The path data is copied. The extruder does not retain any references to the original data.
points | The path to extrude |
closed | Whether the path is closed |
cugl::SimpleExtruder::SimpleExtruder | ( | const Path2 & | path | ) |
Creates an extruder with the given path.
The path data is copied. The extruder does not retain any references to the original data.
path | The path to extrude |
cugl::SimpleExtruder::~SimpleExtruder | ( | ) |
Deletes this extruder, releasing all resources.
void cugl::SimpleExtruder::calculate | ( | float | lwidth, |
float | rwidth | ||
) |
Performs an asymmetric extrusion of the current path data.
An extrusion of a path is a polygon that follows the path 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 path. The new polygon has more vertices, depending on the choice of joint (shape at the corners) and cap (shape at the end).
This version of the method allows you to specify the left and right side widths independently. In particular, this allows us to define an "half extrusion" that starts from the center line.
lwidth | The width of the left side of the extrusion |
rwidth | The width of the right side of the extrusion |
|
inline |
Performs a extrusion of the current path data.
An extrusion of a path is a polygon that follows the path 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 path. The new polygon has more vertices, depending on the choice of joint (shape at the corners) and cap (shape at the end).
The stroke width is measured from the left side of the extrusion to the right side of the extrusion. So a stroke of width 20 is actually 10 pixels from the center on each side.
width | The stroke width of the extrusion |
void cugl::SimpleExtruder::clear | ( | ) |
Clears all internal data, including initial path data.
When this method is called, you will need to set a new path before calling calculate. However, the joint, cap, and tolerance settings are preserved.
std::vector<Path2> cugl::SimpleExtruder::getBorder | ( | ) | const |
Returns a (closed) path representing the extrusion border(s)
So long as the calculation is complete, the vector is guaranteed to contain at least one path. Counter-clockwise paths correspond to the exterior boundary of the stroke. Clockwise paths are potential holes in the extrusion. There is no guarantee on the order of the returned paths.
If the calculation is not yet performed, this method will return the empty path.
size_t cugl::SimpleExtruder::getBorder | ( | std::vector< Path2 > & | buffer | ) | const |
Stores a (closed) path representing the extrusion border in the buffer
So long as the calculation is complete, the vector is guaranteed to contain at least one path. Counter-clockwise paths correspond to the exterior boundary of the stroke. Clockwise paths are potential holes in the extrusion. There is no guarantee on the order of the returned paths.
This method will append append its results to the provided buffer. It will not erase any existing data. You should clear the buffer first if you do not want to preserve the original data.
If the calculation is not yet performed, this method will do nothing.
buffer | The buffer to store the path around the extrusion |
|
inline |
Returns the end cap value for the extrusion.
The end cap type determines how the extrusion draws the ends of the line segments at the start and end of the path. See poly2::EndCap for the description of the types.
|
inline |
Returns the joint value for the extrusion.
The joint type determines how the extrusion joins the extruded line segments together. See poly2::Joint for the description of the types.
cugl::Mesh<SpriteVertex2> cugl::SimpleExtruder::getMesh | ( | Color4 | color | ) | const |
Returns a mesh representing the path extrusion.
This method creates a triangular mesh with the vertices of the extrusion, coloring each vertex white. However, if fringe is set to true, then each vertex will instead be colored clear (transparent), unless that vertex is on a zero-width side. This effect can be used to produce border "fringes" around a polygon for anti-aliasing.
If the calculation is not yet performed, this method will return the empty mesh.
cugl::Mesh<SpriteVertex2> cugl::SimpleExtruder::getMesh | ( | Color4 | inner, |
Color4 | outer | ||
) | const |
Returns a mesh representing the path extrusion.
This method creates a triangular mesh with the vertices of the extrusion, coloring each vertex white. However, if fringe is set to true, then each vertex will instead be colored clear (transparent), unless that vertex is on a zero-width side. This effect can be used to produce border "fringes" around a polygon for anti-aliasing.
If the calculation is not yet performed, this method will return the empty mesh.
cugl::Mesh<SpriteVertex2>* cugl::SimpleExtruder::getMesh | ( | cugl::Mesh< SpriteVertex2 > * | mesh, |
Color4 | color | ||
) | const |
Stores a mesh representing the path extrusion in the given buffer
This method will add both the new vertices, and the corresponding indices to the buffer. If the buffer is not empty, the indices will be adjusted accordingly. You should clear the buffer first if you do not want to preserve the original data.
The vertices in this mesh will be colored white by default. However, if fringe is set to true, then each vertex will instead be colored clear (transparent), unless that vertex is on a zero-width side. This effect can be used to produce border "fringes" around a polygon for anti-aliasing.
If the calculation is not yet performed, this method will do nothing.
mesh | The buffer to store the extruded path mesh |
color | The default mesh color |
cugl::Mesh<SpriteVertex2>* cugl::SimpleExtruder::getMesh | ( | cugl::Mesh< SpriteVertex2 > * | mesh, |
Color4 | inner, | ||
Color4 | outer | ||
) | const |
Stores a mesh representing the path extrusion in the given buffer
This method will add both the new vertices, and the corresponding indices to the buffer. If the buffer is not empty, the indices will be adjusted accordingly. You should clear the buffer first if you do not want to preserve the original data.
The vertices in this mesh will be colored white by default. However, if fringe is set to true, then each vertex will instead be colored clear (transparent), unless that vertex is on a zero-width side. This effect can be used to produce border "fringes" around a polygon for anti-aliasing.
If the calculation is not yet performed, this method will do nothing.
mesh | The buffer to store the extruded path mesh |
inner | The interior mesh color |
outer | The exterior mesh color |
|
inline |
Returns the mitre limit of the extrusion.
The mitre limit sets how "pointy" a mitre joint is allowed to be before the algorithm switches it back to a bevel/square joint. Small angles can have very large mitre offsets that go way off-screen.
To determine whether to switch a miter to a bevel, the algorithm will take the two vectors at this joint, normalize them, and then average them. It will multiple the magnitude of that vector by the mitre limit. If that value is less than 1.0, it will switch to a bevel. By default this value is 10.0.
Poly2 cugl::SimpleExtruder::getPolygon | ( | ) | const |
Returns a polygon representing the path extrusion.
The polygon contains the new set of vertices together with the indices defining the extrusion path. The extruder does not maintain references to this polygon and it is safe to modify it.
If the calculation is not yet performed, this method will return the empty polygon.
Stores the path extrusion in the given buffer.
This method will add both the new vertices, and the corresponding indices to the buffer. If the buffer is not empty, the indices will be adjusted accordingly. You should clear the buffer first if you do not want to preserve the original data.
If the calculation is not yet performed, this method will do nothing.
buffer | The buffer to store the extruded path |
Vec2 cugl::SimpleExtruder::getSide | ( | Uint32 | index | ) | const |
Returns the side information for the vertex at the given index
The side information is a two dimensional vector. The x-coordinate indicates left vs. right side. A value of -1 is on the left while 1 is on the right. A value of 0 means an interior node sitting on the path itself.
On the other hand the y-coordinate indicates cap positioning for an open curve. A value of -1 is on the start cap. A value of 1 is on the end cap. 0 values lie along the body of the main curve.
It is possible to have intermediate cap values for both left-right and start-end in the case of rounded caps. In this case, the intermediate value tracks the traversal from one side to another.
index | The vertex index |
|
inline |
Returns the error tolerance of the extrusion.
This value is mostly used to determine the number of segments needed for a rounded joint or endcap. The default value is 0.25f.
void cugl::SimpleExtruder::reset | ( | ) |
Clears all computed data, but still maintains the settings.
This method preserves all initial path data, as well as the joint, cap, and tolerance settings.
void cugl::SimpleExtruder::set | ( | const Path2 & | path | ) |
Sets the path for this extruder.
The path data is copied. The extruder does not retain any references to the original data.
This method resets all interal data. You will need to reperform the calculation before accessing data.
path | The path to extrude |
void cugl::SimpleExtruder::set | ( | const std::vector< Vec2 > & | points, |
bool | closed | ||
) |
Sets the path for this extruder.
The path data is copied. The extruder does not retain any references to the original data. All points will be considered to be corner points.
This method resets all interal data. You will need to reperform the calculation before accessing data.
points | The path to extrude |
closed | Whether the path is closed |
void cugl::SimpleExtruder::set | ( | const Vec2 * | points, |
size_t | size, | ||
bool | closed | ||
) |
Sets the path for this extruder.
The path data is copied. The extruder does not retain any references to the original data. All points will be considered to be corner points.
This method resets all interal data. You will need to reperform the calculation before accessing data.
points | The path to extrude |
size | The number of points |
closed | Whether the path is closed |
|
inline |
Sets the end cap value for the extrusion.
The end cap type determines how the extrusion draws the ends of the line segments at the start and end of the path. See poly2::EndCap for the description of the types.
endcap | The extrusion end cap type |
|
inline |
Sets the joint value for the extrusion.
The joint type determines how the extrusion joins the extruded line segments together. See poly2::Joint for the description of the types.
joint | The extrusion joint type |
|
inline |
Sets the mitre limit of the extrusion.
The mitre limit sets how "pointy" a mitre joint is allowed to be before the algorithm switches it back to a bevel/square joint. Small angles can have very large mitre offsets that go way off-screen.
To determine whether to switch a miter to a bevel, the algorithm will take the two vectors at this joint, normalize them, and then average them. It will multiple the magnitude of that vector by the mitre limit. If that value is less than 1.0, it will switch to a bevel. By default this value is 10.0.
limit | The mitre limit for joint calculations |
|
inline |
Sets the error tolerance of the extrusion.
This value is mostly used to determine the number of segments needed for a rounded joint or endcap. The default value is 0.25f.
tolerance | The error tolerance of the extrusion. |