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
19
20
21
22
23
24
25
26
27
28 public final class LaneGeometryUtil
29 {
30
31
32
33
34 private LaneGeometryUtil()
35 {
36
37 }
38
39
40
41
42
43
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];
51 return new Polygon2d(true, points);
52 }
53
54
55
56
57
58
59
60
61
62
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
72
73
74
75
76
77
78
79
80
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
93
94
95
96
97
98
99
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
114
115
116
117
118
119
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
135
136
137
138
139
140
141
142
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 }