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