Class SplinePather

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

public class SplinePather extends Object
A factory class for producing Path2 objects from a Spline2.

In order to draw a cubic spline, we must first convert it to a Path2 object. All of our rendering tools are designed around the basic Path2 class. In addition to generating a Path2 for the spline path, this class can also generate Poly2 objects for UI elements such as handles and anchors.

As with many of our 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 Spline2) 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.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a spline approximator with no spline data.
    Creates a spline approximator with the given spline as its initial data.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Performs an approximation of the current spline
    void
    Clears all internal data, including the spline data.
    getAnchors(float radius)
    Returns a Poly2 representing handles for the anchor points
    getAnchors(float radius, int segments)
    Returns a Poly2 representing handles for the anchor points
    getHandle(float radius)
    Returns a Poly2 representing handles for the tangent points
    getHandle(float radius, int segments)
    Returns a Poly2 representing handles for the tangent points
    com.badlogic.gdx.utils.FloatArray
    Returns a list of normals for a polygon approximation
    float[]
    Returns a list of parameters for a path approximation
    Returns a new path approximating this spline.
    Returns an expanded version of this spline
    com.badlogic.gdx.utils.FloatArray
    Returns a list of tangents for a path approximation
    void
    Clears all internal data, but still maintains a reference to the spline.
    void
    set(Spline2 spline)
    Sets the given spline as the data for this spline approximator.

    Methods inherited from class java.lang.Object

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

    • SplinePather

      public SplinePather()
      Creates a spline approximator with no spline data.
    • SplinePather

      public SplinePather(Spline2 spline)
      Creates a spline approximator with the given spline as its initial data.
      Parameters:
      spline - The spline to approximate
  • Method Details

    • set

      public void set(Spline2 spline)
      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.

      Parameters:
      spline - The spline to approximate
    • reset

      public void reset()
      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.

    • clear

      public void clear()
      Clears all internal data, including the spline data.

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

    • calculate

      public void calculate()
      Performs an approximation of the current spline

      A polygon approximation is creating by recursively calling de Castlejau's until we reach a stopping condition.

      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.

    • getPath

      public Path2 getPath()
      Returns a new path approximating this spline.

      The Path2 indices will define a path traversing the vertices of the spline. The indices will define a closed path if the spline is itself closed, and an open path otherwise.

      The resolution of the path 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 path from the control points on the original spline.

      Returns:
      a new polygon approximating this spline.
    • getParameters

      public float[] getParameters()
      Returns a list of parameters for a path approximation

      The parameters correspond to the generating values in the spline polynomial. That is, if you evaluate the polynomial on the parameters, {via Spline2.getPoint(float), you will get the points in the approximating polygon.

      The resolution of the path 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.

      Returns:
      a list of parameters for a polygon approximation
    • getTangents

      public com.badlogic.gdx.utils.FloatArray getTangents()
      Returns a list of tangents for a path 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 path 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.

      Returns:
      a list of tangents for a polygon approximation
    • getNormals

      public com.badlogic.gdx.utils.FloatArray getNormals()
      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 path 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.

      Returns:
      a list of normals for a polygon approximation
    • getAnchors

      public Poly2 getAnchors(float radius, int segments)
      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 path 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.

      Parameters:
      radius - the radius of each handle
      segments - the number of segments in the handle "circle"
      Returns:
      a Poly2 representing handles for the anchor points
    • getAnchors

      public Poly2 getAnchors(float radius)
      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 path 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.

      Parameters:
      radius - the radius of each handle
      Returns:
      a Poly2 representing handles for the anchor points
    • getHandle

      public Poly2 getHandle(float radius, int segments)
      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 path 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.

      Parameters:
      radius - the radius of each handle
      segments - the number of segments in the handle "circle"
      Returns:
      a Poly2 representing handles for the tangent points
    • getHandle

      public Poly2 getHandle(float radius)
      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 path 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.

      Parameters:
      radius - the radius of each handle
      Returns:
      a Poly2 representing handles for the tangent points
    • getRefinement

      public Spline2 getRefinement()
      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 path 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.

      Returns:
      an expanded version of this spline