Class PathSmoother

java.lang.Object
edu.cornell.gdiac.math.PathSmoother

public class PathSmoother extends Object
A factory class to smooth a continuous path of points, reducing the number needed.

A common temptation with mobile games is to track the player's mouse movements by recording all of the mouse 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 many of our factory classes, 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 Summary

    Constructors
    Constructor
    Description
    Creates a path smoother with no vertex data.
    PathSmoother(float[] points)
    Creates a path smoother with the given vertex data.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Performs a triangulation of the current vertex data.
    void
    Clears all internal data, the initial vertex data.
    float
    Returns the epsilon value for the smoothing algorithm.
    Returns a path object representing the smoothed result.
    float[]
    Returns a list of points representing the smoothed path.
    void
    Clears all internal data, but still maintains the initial vertex data.
    void
    set(float[] points)
    Sets the vertex data for this path smoother.
    void
    set(Path2 path)
    Sets the vertex data for this path smoother.
    void
    setEpsilon(float epsilon)
    Sets the epsilon value for the smoothing algorithm.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PathSmoother

      public PathSmoother()
      Creates a path smoother with no vertex data.
    • PathSmoother

      public PathSmoother(float[] 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:
      points - The vertices to triangulate
  • Method Details

    • set

      public void set(float[] points)
      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:
      points - The vertices to triangulate
    • set

      public void set(Path2 path)
      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:
      path - The path to smooth
    • setEpsilon

      public void setEpsilon(float epsilon)
      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:
      epsilon - The epsilon value for the smoothing algorithm.
    • getEpsilon

      public float getEpsilon()
      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.
    • reset

      public void reset()
      Clears all internal data, but still maintains the initial vertex data.
    • clear

      public void 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.
    • calculate

      public void calculate()
      Performs a triangulation of the current vertex data.
    • getPoints

      public float[] getPoints()
      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

      public Path2 getPath()
      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.