View Javadoc
1   package org.opentrafficsim.core.geometry;
2   
3   import org.djutils.draw.line.PolyLine2d;
4   import org.djutils.draw.point.OrientedPoint2d;
5   import org.djutils.draw.point.Point2d;
6   import org.djutils.exceptions.Throw;
7   import org.opentrafficsim.base.geometry.OtsGeometryUtil;
8   
9   /**
10   * Continuous definition of a straight.
11   * <p>
12   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
16   */
17  public class ContinuousStraight implements ContinuousLine
18  {
19  
20      /** Start point with direction. */
21      private final OrientedPoint2d startPoint;
22  
23      /** End point with direction. */
24      private final OrientedPoint2d endPoint;
25  
26      /** Length. */
27      private final double length;
28  
29      /**
30       * Constructor.
31       * @param startPoint start point.
32       * @param length length.
33       */
34      public ContinuousStraight(final OrientedPoint2d startPoint, final double length)
35      {
36          Throw.whenNull(startPoint, "Start point may not be null.");
37          Throw.when(length <= 0.0, IllegalArgumentException.class, "Length must be above 0.");
38          this.startPoint = startPoint;
39          this.endPoint = new OrientedPoint2d(startPoint.x + length * Math.cos(startPoint.dirZ),
40                  startPoint.y + length * Math.sin(startPoint.dirZ), startPoint.dirZ);
41          this.length = length;
42      }
43  
44      @Override
45      public OrientedPoint2d getStartPoint()
46      {
47          return this.startPoint;
48      }
49  
50      @Override
51      public OrientedPoint2d getEndPoint()
52      {
53          return this.endPoint;
54      }
55  
56      @Override
57      public double getStartCurvature()
58      {
59          return 0.0;
60      }
61  
62      @Override
63      public double getEndCurvature()
64      {
65          return 0.0;
66      }
67  
68      /**
69       * Polyline from continuous line. A straight uses no segments.
70       * @return polyline.
71       */
72      public PolyLine2d flatten()
73      {
74          return new PolyLine2d(new Point2d(this.startPoint.x, this.startPoint.y), new Point2d(this.endPoint.x, this.endPoint.y));
75      }
76  
77      /**
78       * Returns a 2-point line. Flattener is ignored.
79       * @param flattener flattener (ignored).
80       * @return flattened line.
81       */
82      @Override
83      public PolyLine2d flatten(final Flattener flattener)
84      {
85          return flatten();
86      }
87  
88      /**
89       * Offset polyline based on variable offset. A straight uses no segments, other than for varying offset.
90       * @param offset offset, should contain keys 0.0 and 1.0.
91       * @return offset polyline
92       */
93      public PolyLine2d offset(final ContinuousDoubleFunction offset)
94      {
95          Throw.whenNull(offset, "Offsets may not be null.");
96          double[] knots = offset.getKnots();
97          double[] knotOffset = new double[knots.length];
98          for (int i = 0; i < knots.length; i++)
99          {
100             knotOffset[i] = offset.apply(knots[i]);
101         }
102         return OtsGeometryUtil.offsetLine(flatten(), knots, knotOffset);
103     }
104 
105     /**
106      * Returns the regular offset line of a 2-point line. Flattener is ignored.
107      * @param offsets offset data.
108      * @param flattener flattener (ignored).
109      * @return flattened line.
110      */
111     @Override
112     public PolyLine2d flattenOffset(final ContinuousDoubleFunction offsets, final Flattener flattener)
113     {
114         return offset(offsets);
115     }
116 
117     @Override
118     public double getLength()
119     {
120         return this.length;
121     }
122 
123     @Override
124     public String toString()
125     {
126         return "ContinuousStraight [startPoint=" + this.startPoint + ", endPoint=" + this.endPoint + ", length=" + this.length
127                 + "]";
128     }
129 
130 }