View Javadoc
1   package org.djutils.draw.curve;
2   
3   import org.djutils.draw.function.ContinuousPiecewiseLinearFunction;
4   import org.djutils.draw.line.PolyLine2d;
5   import org.djutils.draw.point.Point2d;
6   
7   /**
8    * OffsetCurve2d.java.
9    * <p>
10   * Copyright (c) 2024-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
12   * distributed under a three-clause BSD-style license, which can be found at
13   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   */
17  public interface OffsetCurve2d extends Curve2d
18  {
19      /**
20       * Flatten a Curve2d while offsetting with the provided continuous offset into a PolyLine2d.
21       * Implementations should use the flattener when relevant and possible.
22       * @param flattener OffsetFlattener
23       * @param offsets offset data
24       * @return approximation of this <code>curve2d</code> with offset as a <code>PolyLine2d</code>
25       * @throws NullPointerException when <code>flattener</code>, or <code>offsets</code> is <code>null</code>
26       */
27      PolyLine2d toPolyLine(OffsetFlattener2d flattener, ContinuousPiecewiseLinearFunction offsets);
28  
29      /**
30       * Returns the point at the given fraction. The fraction may represent any parameter, such as <i>t</i> in a B&eacute;zier
31       * curve, <i>s</i> in a Clothoid, or simply the fraction of length.
32       * @param fraction the fraction
33       * @param of provides fraction-dependent lateral offset to the point
34       * @return the point at the given <code>fraction</code>
35       */
36      Point2d getPoint(double fraction, ContinuousPiecewiseLinearFunction of);
37  
38      /**
39       * Returns the direction at the given fraction. The fraction may represent any parameter, such as <i>t</i> in a
40       * B&eacute;zier curve, <i>s</i> in a Clothoid, or simply the fraction of length. The default implementation performs a
41       * numerical approach by looking at the direction between the points at fraction, and a point 1e-6 away.
42       * @param fraction the fraction
43       * @param of provides fraction-dependent lateral offset to the curve
44       * @return the direction at the given <code>fraction</code>
45       */
46      default double getDirection(final double fraction, final ContinuousPiecewiseLinearFunction of)
47      {
48          Point2d p1, p2;
49          if (fraction < 0.5) // to prevent going above 1.0
50          {
51              p1 = getPoint(fraction, of);
52              p2 = getPoint(fraction + 1e-6, of);
53          }
54          else
55          {
56              p1 = getPoint(fraction - 1e-6, of);
57              p2 = getPoint(fraction, of);
58          }
59          return p1.directionTo(p2);
60      }
61  
62      /**
63       * Convert a position along the curve to a t-value in the <code>OffsetCurve2d</code> domain. For <code>Arc</code>
64       * and <code>Straight</code>, these t-values are the same. For <code>BezierCubic</code> they're not.
65       * @param position t-value in the <code>ContinuousPiecewiseLinearFunction</code> domain
66       * @return t-value in the <code>ContinuousPiecewiseLinearFunction</code> domain
67       */
68      default double getT(final double position)
69      {
70          return position / getLength();
71      }
72  
73  }