Bezier.java

  1. package org.opentrafficsim.core.geometry;

  2. import java.awt.geom.Line2D;

  3. import org.djutils.exceptions.Throw;

  4. import nl.tudelft.simulation.language.d3.DirectedPoint;

  5. /**
  6.  * Generation of B&eacute;zier curves. <br>
  7.  * The class implements the cubic(...) method to generate a cubic B&eacute;zier curve using the following formula: B(t) = (1 -
  8.  * t)<sup>3</sup>P<sub>0</sub> + 3t(1 - t)<sup>2</sup> P<sub>1</sub> + 3t<sup>2</sup> (1 - t) P<sub>2</sub> + t<sup>3</sup>
  9.  * P<sub>3</sub> where P<sub>0</sub> and P<sub>3</sub> are the end points, and P<sub>1</sub> and P<sub>2</sub> the control
  10.  * points. <br>
  11.  * For a smooth movement, one of the standard implementations if the cubic(...) function offered is the case where P<sub>1</sub>
  12.  * is positioned halfway between P<sub>0</sub> and P<sub>3</sub> starting from P<sub>0</sub> in the direction of P<sub>3</sub>,
  13.  * and P<sub>2</sub> is positioned halfway between P<sub>3</sub> and P<sub>0</sub> starting from P<sub>3</sub> in the direction
  14.  * of P<sub>0</sub>.<br>
  15.  * Finally, an n-point generalization of the B&eacute;zier curve is implemented with the bezier(...) function.
  16.  * <p>
  17.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  18.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  19.  * </p>
  20.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  21.  * initial version Nov 14, 2015 <br>
  22.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  23.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  24.  */
  25. public final class Bezier
  26. {
  27.     /** The default number of points to use to construct a B&eacute;zier curve. */
  28.     private static final int DEFAULT_NUM_POINTS = 64;

  29.     /** Cached factorial values. */
  30.     private static long[] fact = new long[] {1L, 1L, 2L, 6L, 24L, 120L, 720L, 5040L, 40320L, 362880L, 3628800L, 39916800L,
  31.             479001600L, 6227020800L, 87178291200L, 1307674368000L, 20922789888000L, 355687428096000L, 6402373705728000L,
  32.             121645100408832000L, 2432902008176640000L};

  33.     /** Utility class. */
  34.     private Bezier()
  35.     {
  36.         // do not instantiate
  37.     }

  38.     /**
  39.      * Construct a cubic B&eacute;zier curve from start to end with two control points.
  40.      * @param numPoints int; the number of points for the B&eacute;zier curve
  41.      * @param start OTSPoint3D; the start point of the B&eacute;zier curve
  42.      * @param control1 OTSPoint3D; the first control point
  43.      * @param control2 OTSPoint3D; the second control point
  44.      * @param end OTSPoint3D; the end point of the B&eacute;zier curve
  45.      * @return a cubic B&eacute;zier curve between start and end, with the two provided control points
  46.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  47.      *             constructed
  48.      */
  49.     public static OTSLine3D cubic(final int numPoints, final OTSPoint3D start, final OTSPoint3D control1,
  50.             final OTSPoint3D control2, final OTSPoint3D end) throws OTSGeometryException
  51.     {
  52.         Throw.when(numPoints < 2, OTSGeometryException.class, "Number of points too small (got %d; minimum value is 2)",
  53.                 numPoints);
  54.         OTSPoint3D[] points = new OTSPoint3D[numPoints];
  55.         for (int n = 0; n < numPoints; n++)
  56.         {
  57.             double t = n / (numPoints - 1.0);
  58.             double x = B3(t, start.x, control1.x, control2.x, end.x);
  59.             double y = B3(t, start.y, control1.y, control2.y, end.y);
  60.             double z = B3(t, start.z, control1.z, control2.z, end.z);
  61.             points[n] = new OTSPoint3D(x, y, z);
  62.         }
  63.         return new OTSLine3D(points);
  64.     }

  65.     /**
  66.      * Construct a cubic B&eacute;zier curve from start to end with two generated control points at half the distance between
  67.      * start and end. The z-value is interpolated in a linear way.
  68.      * @param numPoints int; the number of points for the B&eacute;zier curve
  69.      * @param start DirectedPoint; the directed start point of the B&eacute;zier curve
  70.      * @param end DirectedPoint; the directed end point of the B&eacute;zier curve
  71.      * @return a cubic B&eacute;zier curve between start and end, with the two provided control points
  72.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  73.      *             constructed
  74.      */
  75.     public static OTSLine3D cubic(final int numPoints, final DirectedPoint start, final DirectedPoint end)
  76.             throws OTSGeometryException
  77.     {
  78.         return cubic(numPoints, start, end, 1.0);
  79.     }

  80.     /**
  81.      * Construct a cubic B&eacute;zier curve from start to end with two generated control points at half the distance between
  82.      * start and end. The z-value is interpolated in a linear way.
  83.      * @param numPoints int; the number of points for the B&eacute;zier curve
  84.      * @param start DirectedPoint; the directed start point of the B&eacute;zier curve
  85.      * @param end DirectedPoint; the directed end point of the B&eacute;zier curve
  86.      * @param shape shape factor; 1 = control points at half the distance between start and end, &gt; 1 results in a pointier
  87.      *            shape, &lt; 1 results in a flatter shape, value should be above 0
  88.      * @return a cubic B&eacute;zier curve between start and end, with the two determined control points
  89.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  90.      *             constructed
  91.      */
  92.     public static OTSLine3D cubic(final int numPoints, final DirectedPoint start, final DirectedPoint end, final double shape)
  93.             throws OTSGeometryException
  94.     {
  95.         return cubic(numPoints, start, end, shape, false);
  96.     }

  97.     /**
  98.      * Construct a cubic B&eacute;zier curve from start to end with two generated control points at half the distance between
  99.      * start and end. The z-value is interpolated in a linear way.
  100.      * @param numPoints int; the number of points for the B&eacute;zier curve
  101.      * @param start DirectedPoint; the directed start point of the B&eacute;zier curve
  102.      * @param end DirectedPoint; the directed end point of the B&eacute;zier curve
  103.      * @param shape shape factor; 1 = control points at half the distance between start and end, &gt; 1 results in a pointier
  104.      *            shape, &lt; 1 results in a flatter shape, value should be above 0
  105.      * @param weighted boolean; control point distance relates to distance to projected point on extended line from other end
  106.      * @return a cubic B&eacute;zier curve between start and end, with the two determined control points
  107.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  108.      *             constructed
  109.      */
  110.     public static OTSLine3D cubic(final int numPoints, final DirectedPoint start, final DirectedPoint end, final double shape,
  111.             final boolean weighted) throws OTSGeometryException
  112.     {
  113.         OTSPoint3D control1;
  114.         OTSPoint3D control2;

  115.         if (weighted)
  116.         {
  117.             // each control point is 'w' * the distance between the end-points away from the respective end point
  118.             // 'w' is a weight given by the distance from the end point to the extended line of the other end point
  119.             double distance = shape * Math.sqrt((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y));
  120.             double cosEnd = Math.cos(end.getRotZ());
  121.             double sinEnd = Math.sin(end.getRotZ());
  122.             double dStart = Line2D.ptLineDist(end.x, end.y, end.x + cosEnd, end.y + sinEnd, start.x, start.y);
  123.             double cosStart = Math.cos(start.getRotZ());
  124.             double sinStart = Math.sin(start.getRotZ());
  125.             double dEnd = Line2D.ptLineDist(start.x, start.y, start.x + cosStart, start.y + sinStart, end.x, end.y);
  126.             double wStart = dStart / (dStart + dEnd);
  127.             double wEnd = dEnd / (dStart + dEnd);
  128.             double wStartDistance = wStart * distance;
  129.             double wEndDistance = wEnd * distance;
  130.             control1 = new OTSPoint3D(start.x + wStartDistance * cosStart, start.y + wStartDistance * sinStart);
  131.             // - (minus) as the angle is where the line leaves, i.e. from shape point to end
  132.             control2 = new OTSPoint3D(end.x - wEndDistance * cosEnd, end.y - wEndDistance * sinEnd);
  133.         }
  134.         else
  135.         {
  136.             // each control point is half the distance between the end-points away from the respective end point
  137.             double distance2 =
  138.                     shape * Math.sqrt((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / 2.0;
  139.             control1 = new OTSPoint3D(start.x + distance2 * Math.cos(start.getRotZ()),
  140.                     start.y + distance2 * Math.sin(start.getRotZ()), start.z);
  141.             control2 = new OTSPoint3D(end.x - distance2 * Math.cos(end.getRotZ()), end.y - distance2 * Math.sin(end.getRotZ()),
  142.                     end.z);
  143.         }

  144.         // Limit control points to not intersect with the other (infinite) line
  145.         OTSPoint3D s = new OTSPoint3D(start);
  146.         OTSPoint3D e = new OTSPoint3D(end);

  147.         // return cubic(numPoints, new OTSPoint3D(start), control1, control2, new OTSPoint3D(end));
  148.         return bezier(numPoints, s, control1, control2, e);
  149.     }

  150.     /**
  151.      * Construct a cubic B&eacute;zier curve from start to end with two generated control points at half the distance between
  152.      * start and end. The z-value is interpolated in a linear way.
  153.      * @param start DirectedPoint; the directed start point of the B&eacute;zier curve
  154.      * @param end DirectedPoint; the directed end point of the B&eacute;zier curve
  155.      * @return a cubic B&eacute;zier curve between start and end, with the two provided control points
  156.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  157.      *             constructed
  158.      */
  159.     public static OTSLine3D cubic(final DirectedPoint start, final DirectedPoint end) throws OTSGeometryException
  160.     {
  161.         return cubic(DEFAULT_NUM_POINTS, start, end);
  162.     }

  163.     /**
  164.      * Calculate the cubic B&eacute;zier point with B(t) = (1 - t)<sup>3</sup>P<sub>0</sub> + 3t(1 - t)<sup>2</sup>
  165.      * P<sub>1</sub> + 3t<sup>2</sup> (1 - t) P<sub>2</sub> + t<sup>3</sup> P<sub>3</sub>.
  166.      * @param t double; the fraction
  167.      * @param p0 double; the first point of the curve
  168.      * @param p1 double; the first control point
  169.      * @param p2 double; the second control point
  170.      * @param p3 double; the end point of the curve
  171.      * @return the cubic bezier value B(t)
  172.      */
  173.     @SuppressWarnings("checkstyle:methodname")
  174.     private static double B3(final double t, final double p0, final double p1, final double p2, final double p3)
  175.     {
  176.         double t2 = t * t;
  177.         double t3 = t2 * t;
  178.         double m = (1.0 - t);
  179.         double m2 = m * m;
  180.         double m3 = m2 * m;
  181.         return m3 * p0 + 3.0 * t * m2 * p1 + 3.0 * t2 * m * p2 + t3 * p3;
  182.     }

  183.     /**
  184.      * Construct a B&eacute;zier curve of degree n.
  185.      * @param numPoints int; the number of points for the B&eacute;zier curve to be constructed
  186.      * @param points OTSPoint3D...; the points of the curve, where the first and last are begin and end point, and the
  187.      *            intermediate ones are control points. There should be at least two points.
  188.      * @return the B&eacute;zier value B(t) of degree n, where n is the number of points in the array
  189.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  190.      *             constructed
  191.      */
  192.     public static OTSLine3D bezier(final int numPoints, final OTSPoint3D... points) throws OTSGeometryException
  193.     {
  194.         OTSPoint3D[] result = new OTSPoint3D[numPoints];
  195.         double[] px = new double[points.length];
  196.         double[] py = new double[points.length];
  197.         double[] pz = new double[points.length];
  198.         for (int i = 0; i < points.length; i++)
  199.         {
  200.             px[i] = points[i].x;
  201.             py[i] = points[i].y;
  202.             pz[i] = points[i].z;
  203.         }
  204.         for (int n = 0; n < numPoints; n++)
  205.         {
  206.             double t = n / (numPoints - 1.0);
  207.             double x = Bn(t, px);
  208.             double y = Bn(t, py);
  209.             double z = Bn(t, pz);
  210.             result[n] = new OTSPoint3D(x, y, z);
  211.         }
  212.         return new OTSLine3D(result);
  213.     }

  214.     /**
  215.      * Construct a B&eacute;zier curve of degree n.
  216.      * @param points OTSPoint3D...; the points of the curve, where the first and last are begin and end point, and the
  217.      *            intermediate ones are control points. There should be at least two points.
  218.      * @return the B&eacute;zier value B(t) of degree n, where n is the number of points in the array
  219.      * @throws OTSGeometryException in case the number of points is less than 2 or the B&eacute;zier curve could not be
  220.      *             constructed
  221.      */
  222.     public static OTSLine3D bezier(final OTSPoint3D... points) throws OTSGeometryException
  223.     {
  224.         return bezier(DEFAULT_NUM_POINTS, points);
  225.     }

  226.     /**
  227.      * Calculate the B&eacute;zier point of degree n, with B(t) = Sum(i = 0..n) [C(n, i) * (1 - t)<sup>n-i</sup> t<sup>i</sup>
  228.      * P<sub>i</sub>], where C(n, k) is the binomial coefficient defined by n! / ( k! (n-k)! ), ! being the factorial operator.
  229.      * @param t double; the fraction
  230.      * @param p double...; the points of the curve, where the first and last are begin and end point, and the intermediate ones
  231.      *            are control points
  232.      * @return the B&eacute;zier value B(t) of degree n, where n is the number of points in the array
  233.      */
  234.     @SuppressWarnings("checkstyle:methodname")
  235.     private static double Bn(final double t, final double... p)
  236.     {
  237.         double b = 0.0;
  238.         double m = (1.0 - t);
  239.         int n = p.length - 1;
  240.         double fn = factorial(n);
  241.         for (int i = 0; i <= n; i++)
  242.         {
  243.             double c = fn / (factorial(i) * (factorial(n - i)));
  244.             b += c * Math.pow(m, n - i) * Math.pow(t, i) * p[i];
  245.         }
  246.         return b;
  247.     }

  248.     /**
  249.      * Calculate factorial(k), which is k * (k-1) * (k-2) * ... * 1. For factorials up to 20, a lookup table is used.
  250.      * @param k int; the parameter
  251.      * @return factorial(k)
  252.      */
  253.     private static double factorial(final int k)
  254.     {
  255.         if (k < fact.length)
  256.         {
  257.             return fact[k];
  258.         }
  259.         double f = 1;
  260.         for (int i = 2; i <= k; i++)
  261.         {
  262.             f = f * i;
  263.         }
  264.         return f;
  265.     }

  266. }