CUGL 2.0
Cornell University Game Library
Public Member Functions | List of all members
cugl::PathSmoother Class Reference

#include <CUPathSmoother.h>

Public Member Functions

 PathSmoother ()
 
 PathSmoother (const std::vector< Vec2 > &points)
 
 ~PathSmoother ()
 
void set (const std::vector< Vec2 > &points)
 
void setEpsilon (float epsilon)
 
float getEpsilon () const
 
void reset ()
 
void clear ()
 
void calculate ()
 
std::vector< Vec2getPath () const
 
size_t getPath (std::vector< Vec2 > &buffer) const
 
Poly2 getPolygon () const
 
Poly2getPolygon (Poly2 *buffer) const
 

Detailed Description

This class smooths a continuous path of points, reducing the number needed.

A common temptation with mobile games is to track the player's finger gesture by recording all of the finger positions over time. Except that this is a lot of points (and attempting to draw all these points exposed some serious flaws in earlier versions of SpriteBatch). If points are too close together, then some of them can be safely removed without altering the shape of the path.

This class uses the Douglas-Peuker algorithm, as described here:

 https://en.wikipedia.org/wiki/Ramer–Douglas–Peucker_algorithm

The correct epsilon value to use should be found with experimentation. In particular, it depends on the scale of the path being smoothed.

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 another Poly2) 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.

Constructor & Destructor Documentation

◆ PathSmoother() [1/2]

cugl::PathSmoother::PathSmoother ( )

Creates a path smoother with no vertex data.

◆ PathSmoother() [2/2]

cugl::PathSmoother::PathSmoother ( const std::vector< Vec2 > &  points)

Creates a path smoother with the given vertex data.

The vertex data is copied. The smother does not retain any references to the original data.

Parameters
pointsThe vertices to triangulate

◆ ~PathSmoother()

cugl::PathSmoother::~PathSmoother ( )
inline

Deletes this path smoother, releasing all resources.

Member Function Documentation

◆ calculate()

void cugl::PathSmoother::calculate ( )

Performs a triangulation of the current vertex data.

◆ clear()

void cugl::PathSmoother::clear ( )

Clears all internal data, the initial vertex data.

When this method is called, you will need to set a new vertices before calling calculate.

◆ getEpsilon()

float cugl::PathSmoother::getEpsilon ( ) const
inline

Returns the epsilon value for the smoothing algorithm.

The epsilon value specifies the tolerance for the algorithm. At each step, any point that is with epsilon of a line segment is considered to be part of that line segment.

Typically this value is found by experimentation. However, because this is typically used to smooth touch paths (which have integer coordinates), the value should be at least 1 (which is the default).

Returns
the epsilon value for the smoothing algorithm.

◆ getPath() [1/2]

std::vector<Vec2> cugl::PathSmoother::getPath ( ) const

Returns a list of points representing the smoothed path.

The result is guaranteed to be a subset of the original vertex path, order preserved. The smoother does not retain a reference to the returned list; it is safe to modify it.

If the calculation is not yet performed, this method will return the empty list.

Returns
a list of indices representing the triangulation.

◆ getPath() [2/2]

size_t cugl::PathSmoother::getPath ( std::vector< Vec2 > &  buffer) const

Stores the triangulation indices in the given buffer.

The result is guaranteed to be a subset of the original vertex path, order preserved. The points 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.

Returns
the number of elements added to the buffer

◆ getPolygon() [1/2]

Poly2 cugl::PathSmoother::getPolygon ( ) const

Returns a polygon representing the smoothed path.

The polygon contains the path vertices together with the new indices defining an open path. To close the path, simply add two more indices connected the last vertex with the first. The smoother 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.

Returns
a polygon representing the triangulation.

◆ getPolygon() [2/2]

Poly2* cugl::PathSmoother::getPolygon ( Poly2 buffer) const

Stores the triangulation in the given buffer.

The polygon contains the path vertices together with the new indices defining an open path. This method will add both the original vertices, and the corresponding indices to the new 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.

Parameters
bufferThe buffer to store the triangulated polygon
Returns
a reference to the buffer for chaining.

◆ reset()

void cugl::PathSmoother::reset ( )

Clears all internal data, but still maintains the initial vertex data.

◆ set()

void cugl::PathSmoother::set ( const std::vector< Vec2 > &  points)
inline

Sets the vertex data for this path smoother.

The vertex data is copied. The smother 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.

Parameters
pointsThe vertices to triangulate

◆ setEpsilon()

void cugl::PathSmoother::setEpsilon ( float  epsilon)
inline

Sets the epsilon value for the smoothing algorithm.

The epsilon value specifies the tolerance for the algorithm. At each step, any point that is with epsilon of a line segment is considered to be part of that line segment.

Typically this value is found by experimentation. However, because this is typically used to smooth touch paths (which have integer coordinates), the value should be at least 1 (which is the default).

Parameters
epsilonThe epsilon value for the smoothing algorithm.

The documentation for this class was generated from the following file: