CUGL 2.1
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 set (const Path2 &path)
 
void setEpsilon (float epsilon)
 
float getEpsilon () const
 
void reset ()
 
void clear ()
 
void calculate ()
 
std::vector< Vec2getPoints () const
 
size_t getPoints (std::vector< Vec2 > &buffer) const
 
Path2 getPath () const
 
Path2getPath (Path2 *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]

Path2 cugl::PathSmoother::getPath ( ) const

Returns a path object representing the smoothed result.

The resulting path is open.

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

Returns
a polygon representing the triangulation.

◆ getPath() [2/2]

Path2* cugl::PathSmoother::getPath ( Path2 buffer) const

Stores the smoothed path in the given buffer.

If the buffer is not empty, the vertices will be appended to the end of the buffer.

If the calculation is not yet performed, this method will do nothing.

Parameters
bufferThe buffer to store the smoothed path
Returns
a reference to the buffer for chaining.

◆ getPoints() [1/2]

std::vector<Vec2> cugl::PathSmoother::getPoints ( ) 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.

◆ getPoints() [2/2]

size_t cugl::PathSmoother::getPoints ( 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

◆ reset()

void cugl::PathSmoother::reset ( )

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

◆ set() [1/2]

void cugl::PathSmoother::set ( const Path2 path)
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. In addition, only the vertex data is copied. Whether or not the path is closed is ignored.

This method resets all interal data. You will need to reperform the calculation before accessing data.

Parameters
pathThe path to smooth

◆ set() [2/2]

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: