CUGL
Cornell University Game Library
|
#include <CUCubicSplineApproximator.h>
Public Types | |
enum | Criterion { FLAT, DISTANCE, SPACING } |
Public Member Functions | |
CubicSplineApproximator () | |
CubicSplineApproximator (const CubicSpline *spline) | |
~CubicSplineApproximator () | |
void | set (const CubicSpline *spline) |
void | reset () |
void | clear () |
void | calculate (Criterion criterion=Criterion::DISTANCE, float tolerance=DEFAULT_TOLERANCE) |
Poly2 | getPath () const |
Poly2 * | getPath (Poly2 *buffer) const |
std::vector< float > | getParameters () const |
size_t | getParameters (std::vector< float > buffer) |
std::vector< Vec2 > | getTangents () const |
size_t | getTangents (std::vector< Vec2 > buffer) |
Poly2 * | getTangents (Poly2 *buffer) |
std::vector< Vec2 > | getNormals () const |
size_t | getNormals (std::vector< Vec2 > buffer) |
Poly2 * | getNormals (Poly2 *buffer) |
Poly2 | getAnchors (float radius, int segments=4) const |
Poly2 * | getAnchors (Poly2 *buffer, float radius, int segments=4) const |
Poly2 | getHandles (float radius, int segments=4) const |
Poly2 * | getHandles (Poly2 *buffer, float radius, int segments=4) const |
CubicSpline | getRefinement () const |
CubicSpline * | getRefinement (CubicSpline *buffer) const |
This class is a factory for producing Poly2 objects from a CubicSpline.
In order to draw a cubic spline, we must first convert it to a Poly2 object. All of our rendering tools are designed around the basic Poly2 class. In addition to generating a Poly2 for the spline path, this class can also generate Poly2 objects for UI elements such as handles and anchors.
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 pointer to a CubicSpline) 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, not that this factory keeps a pointer to the spline, and it is unsafe to modify the spline while the calculation is ongoing. If you do multithread the calculation, you should force the user to copy the spline first.
Termination criteria for de Castlejau's recursive subdivision
This is used by the polygon approximation functions. To convert a bezier into a polygon, we recursively subdivide the bezier until we reach the terminal condition. We then use the anchor points of the subdivided bezier to define our polygon.
|
inline |
Creates a spline approximator with no spline data.
|
inline |
Creates a spline approximator with the given spline as its initial data.
spline | The spline to approximate |
cugl::CubicSplineApproximator::~CubicSplineApproximator | ( | ) |
Deletes this spline approximator, releasing all resources.
void cugl::CubicSplineApproximator::calculate | ( | Criterion | criterion = Criterion::DISTANCE , |
float | tolerance = DEFAULT_TOLERANCE |
||
) |
Performs an approximation of the current spline
A polygon approximation is creating by recursively calling de Castlejau's until we reach a stopping condition. The stopping condition is determined by the Criterion. See that enum for a description of how the various stopping conditions work. The tolerance is the value associated with the condition. For example, for condition DISTANCE, tolerance is how far the point can be away from the true curve.
The calculation uses a reference to the spline; it does not copy it. Hence this method is not thread-safe. If you are using this method in a task thread, you should copy the spline first before starting the calculation.
criterion | the stopping condition criterion |
tolerance | the error tolerance of the stopping condition |
|
inline |
Clears all internal data, including the spline data.
When this method is called, you will need to set a new spline before calling calculate.
Poly2 cugl::CubicSplineApproximator::getAnchors | ( | float | radius, |
int | segments = 4 |
||
) | const |
Returns a Poly2 representing handles for the anchor points
This method returns a collection of vertex information for handles at the anchor points. Handles are circular shapes of a given radius. This information may be drawn to provide a visual representation of the anchor points (as seen in Adobe Illustrator).
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose anchors for the control points on the original spline. This latter option is useful when you want to draw a UI for the original control points.
radius | the radius of each handle |
segments | the number of segments in the handle "circle" |
Poly2* cugl::CubicSplineApproximator::getAnchors | ( | Poly2 * | buffer, |
float | radius, | ||
int | segments = 4 |
||
) | const |
Stores vertex information representing the anchor points in the buffer.
This method creates a collection of vertex information for handles at the anchor points. Handles are circular shapes of a given radius. This information may be drawn to provide a visual representation of the anchor points (as seen in Adobe Illustrator).
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose anchors for the control points on the original spline. This latter option is useful when you want to draw a UI for the original control points.
The vertices (and indices) will be appended to the the Poly2 if it is not empty. You should clear the Poly2 first if you do not want to preserve the original data.
buffer | The buffer to store the vertex data |
radius | The radius of each handle |
segments | The number of segments in the handle "circle" |
Poly2 cugl::CubicSplineApproximator::getHandles | ( | float | radius, |
int | segments = 4 |
||
) | const |
Returns a Poly2 representing handles for the tangent points
This method returns vertex information for handles at the tangent points. Handles are circular shapes of a given radius. This information may be passed to a PolygonNode to provide a visual representation of the tangent points (as seen in Adobe Illustrator).
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose the tangents from the control points on the original spline. This latter option is useful when you want to draw a UI for the original tangent points.
radius | the radius of each handle |
segments | the number of segments in the handle "circle" |
Poly2* cugl::CubicSplineApproximator::getHandles | ( | Poly2 * | buffer, |
float | radius, | ||
int | segments = 4 |
||
) | const |
Stores vertex information representing tangent point handles in the buffer.
This method creates vertex information for handles at the tangent points. Handles are circular shapes of a given radius. This information may be passed to a PolygonNode to provide a visual representation of the tangent points (as seen in Adobe Illustrator).
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose the tangents from the control points on the original spline. This latter option is useful when you want to draw a UI for the original tangent points.
The vertices (and indices) will be appended to the the Poly2 if it is not empty. You should clear the Poly2 first if you do not want to preserve the original data.
buffer | The buffer to store the vertex data |
radius | the radius of each handle |
segments | the number of segments in the handle "circle" |
std::vector<Vec2> cugl::CubicSplineApproximator::getNormals | ( | ) | const |
Returns a list of normals for a polygon approximation
There is one normal per control point. If polygon contains n points, this method will also return n normals. The normals are determined by the right tangents. If the spline is open, then the normal of the last point is determined by its left tangent.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose normals for the control points on the original spline. This latter option is useful when you want to draw a UI for the control point normals.
size_t cugl::CubicSplineApproximator::getNormals | ( | std::vector< Vec2 > | buffer | ) |
Stores a list of normals for the approximation in the buffer.
There is one normal per control point. If polygon contains n points, this method will also return n normals. The normals are determined by the right tangents. If the spline is open, then the normal of the last point is determined by its left tangent.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose normals for the control points on the original spline. This latter option is useful when you want to draw a UI for the control point normals.
The normals will be appended to the buffer vector. You should clear the buffer first if you do not want to preserve the original data.
Stores normal data for the approximation in the buffer.
When complete, the Poly2 will contain data for n lines, where the polygon contains n points. Hence this Poly2 may be drawn as a wireframe. Each line is a normal vector anchored at its associated control point. The length of the line is determined by the length of the associated tangent vector (right tangent, except for the last control point).
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose normals for the control points on the original spline. This latter option is useful when you want to draw a UI for the control point normals.
The normal data will be appended to the buffer. You should clear the buffer first if you do not want to preserve the original data.
std::vector<float> cugl::CubicSplineApproximator::getParameters | ( | ) | const |
Returns a list of parameters for a polygon approximation
The parameters correspond to the generating values in the spline polynomial. That is, if you evaluate the polynomial on the parameters, {via Bezerier.getPoint(), you will get the points in the approximating polygon.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose parameters for the control points on the original spline.
size_t cugl::CubicSplineApproximator::getParameters | ( | std::vector< float > | buffer | ) |
Stores a list of parameters for the approximation in the buffer.
The parameters correspond to the generating values in the spline polynomial. That is, if you evaluate the polynomial on the parameters, {via Bezerier.getPoint(), you will get the points in the approximating polygon.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose parameters for the control points on the original spline.
The parameters will be appended to the buffer vector. You should clear the buffer first if you do not want to preserve the original data.
buffer | The buffer to store the parameter data |
Poly2 cugl::CubicSplineApproximator::getPath | ( | ) | const |
Returns a new polygon approximating this spline.
The Poly2 indices will define a path traversing the vertices of the polygon. Hence this Poly2 may be drawn as a wireframe. The indices will define a closed path if the spline is itself closed, and an open path otherwise.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will create a polygon from the control points on the original spline.
Stores vertex information approximating this spline in the buffer.
The Poly2 indices will define a path traversing the vertices of the polygon. Hence this Poly2 may be drawn as a wireframe. The indices will define a closed path if the spline is itself closed, and an open path otherwise.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will create a polygon from the control points on the original spline.
The vertices (and indices) will be appended to the the Poly2 if it is not empty. You should clear the Poly2 first if you do not want to preserve the original data.
buffer | The buffer to store the vertex data |
CubicSpline cugl::CubicSplineApproximator::getRefinement | ( | ) | const |
Returns an expanded version of this spline
When we use de Castlejau's to approximate the spline, it produces a list of control points that are geometrically equal to this spline (e.g. ignoring parameterization). Instead of flattening this information to a polygon, this method presents this data as a new spline.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will copy the original spline.
CubicSpline* cugl::CubicSplineApproximator::getRefinement | ( | CubicSpline * | buffer | ) | const |
Stores an expanded version of this spline in the given buffer.
When we use de Castlejau's to approximate the spline, it produces a list of control points that are geometrically equal to this spline (e.g. ignoring parameterization). Instead of flattening this information to a polygon, this method presents this data as a new spline.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will copy the original spline.
The control points will be appended to the the spline if it is not empty. You should clear the spline first if you do not want to preserve the original data.
buffer | The buffer to store the vertex data |
std::vector<Vec2> cugl::CubicSplineApproximator::getTangents | ( | ) | const |
Returns a list of tangents for a polygon approximation
These tangent vectors are presented in control point order. First, we have the right tangent of the first point, then the left tangent of the second point, then the right, and so on. Hence if the polygon contains n points, this method will return 2(n-1) tangents.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose tangents for the control points on the original spline. This latter option is useful when you want to draw a UI for the control point tangents.
size_t cugl::CubicSplineApproximator::getTangents | ( | std::vector< Vec2 > | buffer | ) |
Stores a list of tangents for the approximation in the buffer.
These tangent vectors are presented in control point order. First, we have the right tangent of the first point, then the left tangent of the second point, then the right, and so on. Hence if the polygon contains n points, this method will return 2(n-1) tangents.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose tangents for the control points on the original spline. This latter option is useful when you want to draw a UI for the control point tangents.
The tangents will be appended to the buffer vector. You should clear the buffer first if you do not want to preserve the original data.
Stores tangent data for the approximation in the buffer.
When complete, the Poly2 will contain data for 2(n-1) lines, where the polygon contains n points. Hence this Poly2 may be drawn as a wireframe. Each line is a tangent vector anchored at its associated control point. The length of the line is determined by the length of the tangent vector.
The resolution of the polygon is determined by the calculate() method. See the description of that method for the various options. If calculate has not been called, this method will choose tangents for the control points on the original spline. This latter option is useful when you want to draw a UI for the control point tangents.
The tangent data will be appended to the buffer. You should clear the buffer first if you do not want to preserve the original data.
|
inline |
Clears all internal data, but still maintains a reference to the spline.
Use this method when you want to reperform the approximation at a different resolution.
|
inline |
Sets the given spline as the data for this spline approximator.
This method resets all interal data. You will need to reperform the calculation before accessing data.
spline | The spline to approximate |