View Javadoc
1   package org.opentrafficsim.road.network;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.draw.curve.OffsetCurve2d;
5   import org.djutils.draw.curve.Straight2d;
6   import org.djutils.draw.function.ContinuousPiecewiseLinearFunction;
7   import org.djutils.draw.line.PolyLine2d;
8   import org.djutils.draw.line.Polygon2d;
9   import org.djutils.draw.point.Point2d;
10  import org.opentrafficsim.road.network.speed.LaneSpeedLimits;
11  
12  /**
13   * This class is an extension (conceptually, not an actual java extension) of {@code OtsGeometryUtil}. This utility has access
14   * to classes that are specific to the ots-road project, and required to define geometry of objects in this context.
15   * <p>
16   * Copyright (c) 2023-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * </p>
19   * @author Alexander Verbraeck
20   * @author Peter Knoppers
21   * @author Wouter Schakel
22   */
23  public final class LaneGeometryUtil
24  {
25  
26      /**
27       * Utility class.
28       */
29      private LaneGeometryUtil()
30      {
31          //
32      }
33  
34      /**
35       * Returns the contour based on left and right edge.
36       * @param leftEdge left edge, in design line direction
37       * @param rightEdge right edge, in design line direction
38       * @return a closed loop of both edges.
39       */
40      public static Polygon2d getContour(final PolyLine2d leftEdge, final PolyLine2d rightEdge)
41      {
42          Point2d[] points = new Point2d[leftEdge.size() + rightEdge.size()];
43          System.arraycopy(leftEdge.getPointList().toArray(), 0, points, 0, leftEdge.size());
44          System.arraycopy(rightEdge.reverse().getPointList().toArray(), 0, points, leftEdge.size(), rightEdge.size());
45          return new Polygon2d(1e-3, points);
46      }
47  
48      /**
49       * Creates a simple straight lane. This method exists to create lanes for simple tests.
50       * @param link link
51       * @param id id
52       * @param offset end offset
53       * @param width end width
54       * @param laneType lane type
55       * @param speedLimits speed limits
56       * @return lane
57       */
58      public static Lane createStraightLane(final CrossSectionLink link, final String id, final Length offset, final Length width,
59              final LaneType laneType, final LaneSpeedLimits speedLimits)
60      {
61          return createStraightLane(link, id, offset, offset, width, width, laneType, speedLimits);
62      }
63  
64      /**
65       * Creates a simple straight lane. This method exists to create lanes for simple tests.
66       * @param link link
67       * @param id id
68       * @param startOffset start offset
69       * @param endOffset end offset
70       * @param startWidth start width
71       * @param endWidth end width
72       * @param laneType lane type
73       * @param speedLimits speed limits
74       * @return lane
75       */
76      public static Lane createStraightLane(final CrossSectionLink link, final String id, final Length startOffset,
77              final Length endOffset, final Length startWidth, final Length endWidth, final LaneType laneType,
78              final LaneSpeedLimits speedLimits)
79      {
80          ContinuousPiecewiseLinearFunction offset = ContinuousPiecewiseLinearFunction.of(0.0, startOffset.si, 1.0, endOffset.si);
81          ContinuousPiecewiseLinearFunction width = ContinuousPiecewiseLinearFunction.of(0.0, startWidth.si, 1.0, endWidth.si);
82          return createStraightLane(link, id, offset, width, laneType, speedLimits);
83      }
84  
85      /**
86       * Creates a simple straight lane. This method exists to create lanes for simple tests.
87       * @param link link
88       * @param id id
89       * @param offset offset information
90       * @param width offset information
91       * @param laneType lane type
92       * @param speedLimits speed limits
93       * @return lane
94       */
95      public static Lane createStraightLane(final CrossSectionLink link, final String id,
96              final ContinuousPiecewiseLinearFunction offset, final ContinuousPiecewiseLinearFunction width,
97              final LaneType laneType, final LaneSpeedLimits speedLimits)
98      {
99          OffsetCurve2d designLine = new Straight2d(link.getDesignLine().getLocationFraction(0.0), link.getLength().si);
100         return new Lane(link, id, CrossSectionGeometry.of(designLine, null, offset, width), laneType, speedLimits);
101     }
102 
103     /**
104      * Creates a simple straight lane. This method exists to create lanes for simple tests.
105      * @param type stripe data
106      * @param id id
107      * @param link link
108      * @param offset end offset
109      * @param width end width
110      * @return lane
111      */
112     public static Stripe createStraightStripe(final StripeData type, final String id, final CrossSectionLink link,
113             final Length offset, final Length width)
114     {
115         OffsetCurve2d designLine = new Straight2d(link.getDesignLine().getLocationFraction(0.0), link.getLength().si);
116         ContinuousPiecewiseLinearFunction offsetFunc = ContinuousPiecewiseLinearFunction.of(0.0, offset.si, 1.0, offset.si);
117         ContinuousPiecewiseLinearFunction widthFunc = ContinuousPiecewiseLinearFunction.of(0.0, width.si, 1.0, width.si);
118         return new Stripe(id, type, link, CrossSectionGeometry.of(designLine, null, offsetFunc, widthFunc));
119     }
120 
121     /**
122      * Creates a simple straight shoulder. This method exists to create shoulders for simple tests.
123      * @param link link
124      * @param id id
125      * @param startOffset start offset
126      * @param endOffset end offset
127      * @param startWidth start width
128      * @param endWidth end width
129      * @param laneType lane type
130      * @return lane
131      */
132     public static Object createStraightShoulder(final CrossSectionLink link, final String id, final Length startOffset,
133             final Length endOffset, final Length startWidth, final Length endWidth, final LaneType laneType)
134     {
135         OffsetCurve2d designLine = new Straight2d(link.getDesignLine().getLocationFraction(0.0), link.getLength().si);
136         ContinuousPiecewiseLinearFunction offset = ContinuousPiecewiseLinearFunction.of(0.0, startOffset.si, 1.0, endOffset.si);
137         ContinuousPiecewiseLinearFunction width = ContinuousPiecewiseLinearFunction.of(0.0, startWidth.si, 1.0, endWidth.si);
138         return new Shoulder(link, id, CrossSectionGeometry.of(designLine, null, offset, width), laneType);
139     }
140 }