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.djutils.exceptions.Try;
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 DirectedPoint; start point.
32       * @param length double; 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      /** {@inheritDoc} */
45      @Override
46      public OrientedPoint2d getStartPoint()
47      {
48          return this.startPoint;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public OrientedPoint2d getEndPoint()
54      {
55          return this.endPoint;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public double getStartCurvature()
61      {
62          return 0.0;
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public double getEndCurvature()
68      {
69          return 0.0;
70      }
71  
72      /**
73       * Polyline from continuous line. A straight uses no segments.
74       * @return PolyLine2d; polyline.
75       */
76      public PolyLine2d flatten()
77      {
78          return new PolyLine2d(new Point2d(this.startPoint.x, this.startPoint.y), new Point2d(this.endPoint.x, this.endPoint.y));
79      }
80  
81      /**
82       * Returns a 2-point line. Flattener is ignored.
83       * @param flattener Flattener; flattener (ignored).
84       * @return PolyLine2d; flattened line.
85       */
86      @Override
87      public PolyLine2d flatten(final Flattener flattener)
88      {
89          return flatten();
90      }
91      
92      /**
93       * Offset polyline based on variable offset. A straight uses no segments, other than for varying offset.
94       * @param offsets FractionalLengthData; offsets, should contain keys 0.0 and 1.0.
95       * @return PolyLine2d; offset polyline.
96       */
97      public PolyLine2d offset(final FractionalLengthData offsets)
98      {
99          Throw.whenNull(offsets, "Offsets may not be null.");
100         return Try.assign(
101                 () -> OtsGeometryUtil.offsetLine(flatten(), offsets.getFractionalLengthsAsArray(), offsets.getValuesAsArray()),
102                 "Unexpected exception while creating straigh OtsLine2d.");
103     }
104 
105     /**
106      * Returns the regular offset line of a 2-point line. Flattener is ignored.
107      * @param offsets FractionalLengthData; offset data.
108      * @param flattener Flattener; flattener (ignored).
109      * @return PolyLine2d; flattened line.
110      */
111     @Override
112     public PolyLine2d flattenOffset(final FractionalLengthData offsets, final Flattener flattener)
113     {
114         return offset(offsets);
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public double getLength()
120     {
121         return this.length;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public String toString()
127     {
128         return "ContinuousStraight [startPoint=" + this.startPoint + ", endPoint=" + this.endPoint + ", length=" + this.length
129                 + "]";
130     }
131 
132 }