1 package org.opentrafficsim.road.gtu.lane.plan.operational;
2
3 import org.djunits.value.vdouble.scalar.Direction;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djunits.value.vdouble.scalar.Time;
6 import org.djutils.draw.point.OrientedPoint2d;
7 import org.djutils.draw.point.Point2d;
8 import org.opentrafficsim.core.geometry.OtsGeometryException;
9 import org.opentrafficsim.core.geometry.OtsLine2d;
10 import org.opentrafficsim.core.geometry.OtsLine2d.FractionalFallback;
11 import org.opentrafficsim.core.gtu.GtuException;
12 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlan;
13 import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
14 import org.opentrafficsim.core.gtu.plan.operational.Segments;
15 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
16 import org.opentrafficsim.road.network.lane.Lane;
17 import org.opentrafficsim.road.network.lane.LanePosition;
18
19
20
21
22
23
24
25
26
27
28
29
30 public class LaneBasedOperationalPlan extends OperationalPlan
31 {
32
33 private static final long serialVersionUID = 20160120L;
34
35
36 private final boolean deviative;
37
38
39
40
41
42
43
44
45
46
47
48
49 @SuppressWarnings("checkstyle:parameternumber")
50 public LaneBasedOperationalPlan(final LaneBasedGtu gtu, final OtsLine2d path, final Time startTime,
51 final Segments segments, final boolean deviative) throws OperationalPlanException
52 {
53 super(gtu, path, startTime, segments);
54 this.deviative = deviative;
55 }
56
57
58
59
60
61 public final boolean isDeviative()
62 {
63 return this.deviative;
64 }
65
66
67
68
69
70
71
72
73 public final Length getTotalLengthAlongLane(final LaneBasedGtu gtu) throws GtuException
74 {
75 if (!this.deviative)
76 {
77
78 return getTotalLength();
79 }
80
81
82 return getDistanceAlongLane(gtu, getEndLocation());
83 }
84
85
86
87
88
89
90
91 private double getRotZAtFraction(final Lane lane, final boolean start)
92 {
93 double f = start ? 0.0 : 1.0;
94 try
95 {
96 return lane.getCenterLine().getLocationFraction(f).getDirZ();
97 }
98 catch (OtsGeometryException exception)
99 {
100
101 throw new RuntimeException("Unexpected exception while assessing if a GTU is between lanes.", exception);
102 }
103 }
104
105
106
107
108
109
110
111
112 public final Length getDistanceAlongLane(final LaneBasedGtu gtu, final OrientedPoint2d point) throws GtuException
113 {
114
115
116 LanePosition pos = gtu.getReferencePosition();
117 Lane lane = pos.lane();
118
119
120 double length = -lane.coveredDistance(pos.position().si / pos.lane().getLength().si).si;
121 double f = Double.NaN;
122 Direction prevDir = Direction.instantiateSI(getRotZAtFraction(lane, true));
123
124
125 while (Double.isNaN(f))
126 {
127 Lane nextLane = gtu.getNextLaneForRoute(lane);
128 Direction nextDir = Direction.instantiateSI(nextLane == null ? getRotZAtFraction(lane, false)
129 : .5 * getRotZAtFraction(lane, false) + .5 * getRotZAtFraction(nextLane, true));
130 f = lane.getCenterLine().projectFractional(prevDir, nextDir, point.x, point.y, FractionalFallback.NaN);
131
132
133 if (Double.isNaN(f))
134 {
135 if (nextLane == null)
136 {
137
138 f = 1.0;
139 length += lane.coveredDistance(f).si;
140 }
141 else
142 {
143 try
144 {
145
146 Point2d last = lane.getCenterLine().getLast();
147 Point2d first = nextLane.getCenterLine().get(0);
148 if (!(last).equals(first))
149 {
150 OtsLine2d gap = new OtsLine2d(last, first);
151 double fGap = gap.projectFractional(null, null, point.x, point.y, FractionalFallback.NaN);
152 if (!Double.isNaN(fGap))
153 {
154 f = (lane.getLength().si + fGap * gap.getLength().si) / lane.getLength().si;
155 }
156 else
157 {
158
159 length += lane.getLength().si;
160 lane = nextLane;
161 prevDir = nextDir;
162 }
163 }
164 else
165 {
166
167 length += lane.getLength().si;
168 lane = nextLane;
169 prevDir = nextDir;
170 }
171 }
172 catch (OtsGeometryException exception)
173 {
174
175 throw new RuntimeException("Unexpected exception while assessing if a GTU is between lanes.",
176 exception);
177 }
178 }
179 }
180 else
181 {
182
183 length += lane.coveredDistance(f).si;
184 }
185 }
186
187 return Length.instantiateSI(length);
188 }
189
190
191 @Override
192 public final String toString()
193 {
194 return "LaneBasedOperationalPlan [deviative=" + this.deviative + "]";
195 }
196 }