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