Class PACurveUtility

java.lang.Object
net.paulhertz.pixelaudio.curves.PACurveUtility

public class PACurveUtility extends Object

A class to maintain static versions of point reduction and curve-modeling methods. Called by PACurveMaker and other curve-modeling applications. Includes an implementation of the Ramer-Douglas-Peucker (RDP) point reduction algorithm, Bezier curve generation methods, and a simple brush stroke modeling method.

Drawing methods are supplied to simplify drawing calls for the objects generated by PACurveUtility and PACurveMaker. The curve and shape drawing methods in particular can be handled by the PABezShape class, which has its own fill and stroke properties for curves (Bezier paths with no fill) and shapes (Bezier paths that may be filled), which are not distinguished within PABezShape. When stroke, fill, and weight are supplied in method arguments for PABezShape instances, the methods will call PABezShape's drawQuick() methods, which use the current drawing context to determine fill, stroke, and weight properties.

See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    buildScheduleFromBezShape(PABezShape shape, int steps, float[] anchorTimesMs, boolean arcLengthTime)
    Schedules times along a gestural curve, typically stored as a PACurveMaker instance.
    static PABezShape
    calculateCurve(ArrayList<processing.core.PVector> framePoints)
    Creates a Bezier curve from a supplied set of points.
    static PABezShape
    calculateWeightedCurve(ArrayList<processing.core.PVector> framePoints, float bias)
     
    static void
    Scales the position of the curve control points on a Bezier curve by a factor determined by the length of the line between the two anchor points and a bias value, such as PACurveUtility.LAMBDA (the default, in this method).
    static PABezShape
    calculateWeightedCurve(PABezShape bezPoints, float bias)
    Scales the position of the curve control points on a Bezier curve by a factor determined by the length of the line between the two anchor points and a bias value, such as PACurveUtility.LAMBDA.
    (package private) static void
    computeControlPoints(float[] K, float[] p1, float[] p2)
     
    static void
    curveDraw(processing.core.PApplet parent, PABezShape curve)
    Draws a Bezier path in a PApplet using 2D curve data and the local stroke color and weight of a PABezShape.
    static void
    curveDraw(processing.core.PApplet parent, PABezShape curve, int curveColor, float curveWeight)
    Draws a Bezier path in a PApplet using 2D curve data and supplied stroke color and weight.
    static void
    curveDraw(processing.core.PGraphics pg, PABezShape curve)
    Draws a Bezier path in a PGraphics using 2D curve data and local stroke color and weight of a PABezShape.
    static void
    curveDraw(processing.core.PGraphics pg, PABezShape curve, int curveColor, float curveWeight)
    Draws a Bezier path in a PGraphics using 2D curve data and supplied stroke color and weight.
    (package private) static int
    findFurthest(ArrayList<processing.core.PVector> points, int a, int b, float epsilon)
     
    static void
    indexedRDP(int startIndex, int endIndex, ArrayList<processing.core.PVector> allPoints, ArrayList<processing.core.PVector> rdpPoints, ArrayList<Integer> rdpIndices, float epsilon)
    Ramer-Douglas-Peucker point reduction algorithm (RDP), reduces points in allPoints and returns the result in rdpPoints.
    static float[]
    intsToFloats(int[] src)
     
    static float[]
    intsToFloats(int[] src, int targetLen)
     
    private static float
    lerp(float a, float b, float u)
    * ---------------- UTILITY ------------------ * *
    (package private) static float
    lineDist(processing.core.PVector c, processing.core.PVector a, processing.core.PVector b)
     
    static void
    lineDraw(processing.core.PApplet parent, ArrayList<processing.core.PVector> points, int lineColor, float lineWeight)
    Draws a line in a PApplet context using the PVector data in points and supplied color and weight.
    static void
    lineDraw(processing.core.PGraphics pg, ArrayList<processing.core.PVector> points, int lineColor, float lineWeight)
    Draws a line in a PGraphics using the 2D PVector data and supplied color and weight.
    static processing.core.PVector
    normalAtPoint(processing.core.PVector a1, processing.core.PVector a2, float u, float d)
    Calculates a normal to a line at a point on the line at parametric distance u, normalized if d = 1.
    static float
    pathLength(List<processing.core.PVector> points)
     
    static void
    pointsDraw(processing.core.PApplet parent, ArrayList<processing.core.PVector> points, int pointColor, float diameter)
    Draws circular points in a PApplet context using the PVector data and supplied color and diameter.
    static void
    pointsDraw(processing.core.PGraphics pg, ArrayList<processing.core.PVector> points, int pointColor, float diameter)
    Draws circular points in a PGraphics context using the PVector data and supplied color and diameter.
    static PABezShape
    quickBrushShape(ArrayList<processing.core.PVector> points, float brushWidth)
     
    static PABezShape
    quickBrushShape(ArrayList<processing.core.PVector> points, float brushWidth, boolean isDrawWeighted, float bias)
    Simulates a brush stroke as a PABezShape object by creating two Bezier curves offset on either side of a supplied list of 2D PVector objects and joining them into a closed shape with pointed ends.
    static PABezShape
    quickBrushShape(ArrayList<processing.core.PVector> points, float brushWidth, float bias)
     
    static void
    rdp(int startIndex, int endIndex, ArrayList<processing.core.PVector> allPoints, ArrayList<processing.core.PVector> rdpPoints, float epsilon)
    Ramer-Douglas-Peucker point reduction algorithm (RDP), reduces points in allPoints and returns the result in rdpPoints.
    static void
    reverseArray(ArrayList<processing.core.PVector> arr, int l, int r)
    Reverses the order of a portion of an ArrayList of PVector objects, between indices l and r, inclusive.
    (package private) static processing.core.PVector
    scalarProjection(processing.core.PVector p, processing.core.PVector a, processing.core.PVector b)
     
    static processing.core.PVector
    scaledNormalAtPoint(processing.core.PVector anchor, processing.core.PVector normal, float d)
     
    static void
    shapeDraw(processing.core.PApplet parent, PABezShape shape)
    Draws a PABezShape in a PApplet using local fill, stroke, and weight of the shape.
    static void
    shapeDraw(processing.core.PApplet parent, PABezShape shape, int shapeFill, int shapeStroke, float shapeWeight)
    Draws a PABezShape in a PApplet using supplied fill, stroke, and weight.
    static void
    shapeDraw(processing.core.PGraphics pg, PABezShape shape)
    Draws a PABezShape in a PGraphics using local fill, stroke, and weight of the shape.
    static void
    shapeDraw(processing.core.PGraphics pg, PABezShape shape, int shapeFill, int shapeStroke, float shapeWeight)
    Draws a PABezShape in a PGraphics using supplied fill, stroke, and weight.
    static processing.core.PVector
    weightedControlVec(float ax, float ay, float cx, float cy, float w, float d)
     

    Methods inherited from class java.lang.Object

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

    • PACurveUtility

      private PACurveUtility()
  • Method Details

    • rdp

      public static void rdp(int startIndex, int endIndex, ArrayList<processing.core.PVector> allPoints, ArrayList<processing.core.PVector> rdpPoints, float epsilon)
      Ramer-Douglas-Peucker point reduction algorithm (RDP), reduces points in allPoints and returns the result in rdpPoints.
      Parameters:
      startIndex - start index in allPoints (usually 0 to begin with)
      endIndex - end index in allPoints (usually allPoints.size()-1 to begin with)
      allPoints - ArrayList of dense points, for example a hand-drawn line
      rdpPoints - an empty ArrayList for accumulating reduced points
      epsilon - the expected distance between reduced points
    • indexedRDP

      public static void indexedRDP(int startIndex, int endIndex, ArrayList<processing.core.PVector> allPoints, ArrayList<processing.core.PVector> rdpPoints, ArrayList<Integer> rdpIndices, float epsilon)
      Ramer-Douglas-Peucker point reduction algorithm (RDP), reduces points in allPoints and returns the result in rdpPoints.
      Parameters:
      startIndex - start index in allPoints (usually 0 to begin with)
      endIndex - end index in allPoints (usually allPoints.size()-1 to begin with)
      allPoints - ArrayList of dense points, for example a hand-drawn line
      rdpPoints - an empty ArrayList for accumulating reduced points
      epsilon - the expected distance between reduced points
    • findFurthest

      static int findFurthest(ArrayList<processing.core.PVector> points, int a, int b, float epsilon)
      Parameters:
      points - a list of points to evaluate
      a - start index in points
      b - end index in points
      epsilon - expected distance between reduced points
      Returns:
    • lineDist

      static float lineDist(processing.core.PVector c, processing.core.PVector a, processing.core.PVector b)
    • scalarProjection

      static processing.core.PVector scalarProjection(processing.core.PVector p, processing.core.PVector a, processing.core.PVector b)
    • calculateCurve

      public static PABezShape calculateCurve(ArrayList<processing.core.PVector> framePoints)
      Creates a Bezier curve from a supplied set of points.
      Parameters:
      framePoints - An array of 2D PVector objects representing the points a Bezier curve will traverse
      Returns:
      A Bezier curve path constructed of PABezShape objects
    • computeControlPoints

      static void computeControlPoints(float[] K, float[] p1, float[] p2)
    • calculateWeightedCurve

      public static PABezShape calculateWeightedCurve(PABezShape bezPoints, float bias)
      Scales the position of the curve control points on a Bezier curve by a factor determined by the length of the line between the two anchor points and a bias value, such as PACurveUtility.LAMBDA. It's most useful when a short control line segment follows a long segment.
      Parameters:
      weightedCurveShape -
      bias -
    • calculateWeightedCurve

      public static PABezShape calculateWeightedCurve(ArrayList<processing.core.PVector> framePoints, float bias)
    • calculateWeightedCurve

      public static void calculateWeightedCurve(PABezShape bezPoints)
      Scales the position of the curve control points on a Bezier curve by a factor determined by the length of the line between the two anchor points and a bias value, such as PACurveUtility.LAMBDA (the default, in this method). It's most useful when a short control line segment follows a long segment.
      Parameters:
      bezPoints - a Bezier path encapsulated in a PABezShape instance
    • weightedControlVec

      public static processing.core.PVector weightedControlVec(float ax, float ay, float cx, float cy, float w, float d)
    • quickBrushShape

      public static PABezShape quickBrushShape(ArrayList<processing.core.PVector> points, float brushWidth, boolean isDrawWeighted, float bias)
      Simulates a brush stroke as a PABezShape object by creating two Bezier curves offset on either side of a supplied list of 2D PVector objects and joining them into a closed shape with pointed ends.
      Parameters:
      points - an ArrayList of 2D PVector objects
      brushWidth - width of the brush in pixels
      isDrawWeighted - create a weighted version of the PABezShape shape
      bias - adjusts distances between control points and anchor points
      Returns:
    • quickBrushShape

      public static PABezShape quickBrushShape(ArrayList<processing.core.PVector> points, float brushWidth)
    • quickBrushShape

      public static PABezShape quickBrushShape(ArrayList<processing.core.PVector> points, float brushWidth, float bias)
    • normalAtPoint

      public static processing.core.PVector normalAtPoint(processing.core.PVector a1, processing.core.PVector a2, float u, float d)
      Calculates a normal to a line at a point on the line at parametric distance u, normalized if d = 1.
      Parameters:
      a1 - 2D PVector, first point
      a2 - 2D PVector, second point
      u - parametric distance along the line from a1 to a2
      d - distance of the normal from the line a1, a2
      Returns:
      Normal to line through a1 and a2
    • scaledNormalAtPoint

      public static processing.core.PVector scaledNormalAtPoint(processing.core.PVector anchor, processing.core.PVector normal, float d)
      Parameters:
      anchor - 2D PVector, the point from which the normal extends
      normal - 2D PVector, normal to anchor
      d - distance for scaling the normal, distance from anchor to returned PVector
      Returns:
      2D PVector, the normal scaled by d
    • reverseArray

      public static void reverseArray(ArrayList<processing.core.PVector> arr, int l, int r)
      Reverses the order of a portion of an ArrayList of PVector objects, between indices l and r, inclusive.
      Parameters:
      arr - an ArrayList of PVector objects
      l - index at which to start reversing order of elements
      r - index at which to stop reversing order of elements
    • buildScheduleFromBezShape

      public static GestureSchedule buildScheduleFromBezShape(PABezShape shape, int steps, float[] anchorTimesMs, boolean arcLengthTime)
      Schedules times along a gestural curve, typically stored as a PACurveMaker instance. A calling sequence might be: PABezShape curve = curveMaker.getCurve(); float rdpTimes = intsToFloats(curveMaker.getReducedTimes()); GestureSchedule curveSchedule = PACurveUtility.buildScheduleFromBezShape(curve, cfg.bezierCurveSteps, rdpTimes, true);
      Parameters:
      shape -
      steps -
      anchorTimesMs -
      arcLengthTime -
      Returns:
    • lerp

      private static float lerp(float a, float b, float u)
      * ---------------- UTILITY ------------------ * *
    • intsToFloats

      public static float[] intsToFloats(int[] src)
      Parameters:
      src - an array of int
      Returns:
      array src converted to float values
    • intsToFloats

      public static float[] intsToFloats(int[] src, int targetLen)
      Parameters:
      src - an array of int
      targetLen - desired length of array of float to consruct
      Returns:
      an array of targetLen float values derived from src
    • pathLength

      public static float pathLength(List<processing.core.PVector> points)
      Parameters:
      points - a List of PVector
      Returns:
      length of the path between the points
    • lineDraw

      public static void lineDraw(processing.core.PApplet parent, ArrayList<processing.core.PVector> points, int lineColor, float lineWeight)
      Draws a line in a PApplet context using the PVector data in points and supplied color and weight.
      Parameters:
      parent - a PApplet where drawing takes place on screen
      points - ArrayList of 2D PVector objects, typically a point set without successive identical points
      lineColor - color for the line that is drawn
      lineWeight - weight for the line that is drawn
    • lineDraw

      public static void lineDraw(processing.core.PGraphics pg, ArrayList<processing.core.PVector> points, int lineColor, float lineWeight)
      Draws a line in a PGraphics using the 2D PVector data and supplied color and weight.
      Parameters:
      pg - a PGraphics where drawing takes place off screen
      points - ArrayList of 2D PVector objects, typically a point set without successive identical points
      lineColor - color for the line that is drawn
      lineWeight - weight for the line that is drawn
    • curveDraw

      public static void curveDraw(processing.core.PApplet parent, PABezShape curve, int curveColor, float curveWeight)
      Draws a Bezier path in a PApplet using 2D curve data and supplied stroke color and weight. The path is drawn with no fill.
      Parameters:
      parent -
      curve -
      curveColor -
      curveWeight -
    • curveDraw

      public static void curveDraw(processing.core.PApplet parent, PABezShape curve)
      Draws a Bezier path in a PApplet using 2D curve data and the local stroke color and weight of a PABezShape. If the PABezShape is filled, the fill will be drawn.
      Parameters:
      parent -
      curve -
    • curveDraw

      public static void curveDraw(processing.core.PGraphics pg, PABezShape curve, int curveColor, float curveWeight)
      Draws a Bezier path in a PGraphics using 2D curve data and supplied stroke color and weight. The path is drawn with no fill.
      Parameters:
      pg -
      curve -
      curveColor -
      curveWeight -
    • curveDraw

      public static void curveDraw(processing.core.PGraphics pg, PABezShape curve)
      Draws a Bezier path in a PGraphics using 2D curve data and local stroke color and weight of a PABezShape. If the PABezShape is filled, the fill will be drawn.
      Parameters:
      pg -
      curve -
    • shapeDraw

      public static void shapeDraw(processing.core.PApplet parent, PABezShape shape, int shapeFill, int shapeStroke, float shapeWeight)
      Draws a PABezShape in a PApplet using supplied fill, stroke, and weight. If weight == 0, the shape is drawn with no stroke.
      Parameters:
      parent -
      shape -
      shapeFill -
      shapeStroke -
      shapeWeight -
    • shapeDraw

      public static void shapeDraw(processing.core.PApplet parent, PABezShape shape)
      Draws a PABezShape in a PApplet using local fill, stroke, and weight of the shape.
      Parameters:
      parent -
      shape -
    • shapeDraw

      public static void shapeDraw(processing.core.PGraphics pg, PABezShape shape, int shapeFill, int shapeStroke, float shapeWeight)
      Draws a PABezShape in a PGraphics using supplied fill, stroke, and weight. If weight == 0, the shape is drawn with no stroke.
      Parameters:
      pg -
      shape -
      shapeFill -
      shapeStroke -
      shapeWeight -
    • shapeDraw

      public static void shapeDraw(processing.core.PGraphics pg, PABezShape shape)
      Draws a PABezShape in a PGraphics using local fill, stroke, and weight of the shape.
      Parameters:
      pg -
      shape -
    • pointsDraw

      public static void pointsDraw(processing.core.PApplet parent, ArrayList<processing.core.PVector> points, int pointColor, float diameter)
      Draws circular points in a PApplet context using the PVector data and supplied color and diameter.
      Parameters:
      parent - a PApplet where drawing takes place on screen
      points - ArrayList of 2D PVector objects, typically a point set without successive identical points
      pointColor - color for the circle that is drawn
      diameter - diameter of circle that is drawn
    • pointsDraw

      public static void pointsDraw(processing.core.PGraphics pg, ArrayList<processing.core.PVector> points, int pointColor, float diameter)
      Draws circular points in a PGraphics context using the PVector data and supplied color and diameter.
      Parameters:
      pg - a PGraphics where drawing takes place off screen
      points - ArrayList of 2D PVector objects, typically a point set without successive identical points
      pointColor - color for the circle that is drawn
      diameter - diameter of circle that is drawn