1 package org.opentrafficsim.road.gtu.lane.perception.categories;
2
3 import org.djunits.value.vdouble.scalar.Duration;
4 import org.djunits.value.vdouble.scalar.Length;
5 import org.djunits.value.vdouble.scalar.Time;
6 import org.opentrafficsim.base.parameters.ParameterException;
7 import org.opentrafficsim.base.parameters.ParameterTypes;
8 import org.opentrafficsim.core.gtu.GTUException;
9 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10 import org.opentrafficsim.road.gtu.lane.perception.categories.Anticipation.NeighborTriplet;
11 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
12 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUPerceived;
13 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUReal;
14 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTURealCopy;
15
16
17
18
19
20
21
22
23
24
25
26
27 public interface HeadwayGtuType
28 {
29
30
31 public final static HeadwayGtuType WRAP = new HeadwayGtuType()
32 {
33 @Override
34 public HeadwayGTUReal createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
35 final Length distance, final boolean downstream) throws GTUException
36 {
37 return new HeadwayGTUReal(perceivedGtu, distance, true);
38 }
39
40 @Override
41 public HeadwayGTUReal createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
42 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
43 {
44 return new HeadwayGTUReal(perceivedGtu, overlapFront, overlap, overlapRear, true);
45 }
46 };
47
48
49 public final static HeadwayGtuType COPY = new HeadwayGtuType()
50 {
51 @Override
52 public HeadwayGTURealCopy createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
53 final Length distance, final boolean downstream) throws GTUException
54 {
55 return new HeadwayGTURealCopy(perceivedGtu, distance);
56 }
57
58 @Override
59 public HeadwayGTURealCopy createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
60 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
61 {
62 return new HeadwayGTURealCopy(perceivedGtu, overlapFront, overlap, overlapRear);
63 }
64 };
65
66
67
68
69
70
71
72
73
74
75
76 HeadwayGTU createHeadwayGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length distance, boolean downstream)
77 throws GTUException, ParameterException;
78
79
80
81
82
83
84
85
86
87
88
89 HeadwayGTU createHeadwayGtu(LaneBasedGTU perceivingGtu, LaneBasedGTU perceivedGtu, Length overlapFront, Length overlap,
90 Length overlapRear) throws GTUException;
91
92
93
94
95
96
97
98
99
100
101
102
103
104 class PerceivedHeadwayGtuType implements HeadwayGtuType
105 {
106
107
108 private final Estimation estimation;
109
110
111 private final Anticipation anticipation;
112
113
114 private Time updateTime = null;
115
116
117 private Duration tr;
118
119
120 private Time when;
121
122
123 private Length traveledDistance;
124
125
126
127
128
129
130 public PerceivedHeadwayGtuType(final Estimation estimation, final Anticipation anticipation)
131 {
132 this.estimation = estimation;
133 this.anticipation = anticipation;
134 }
135
136
137 @Override
138 public HeadwayGTU createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
139 final Length distance, final boolean downstream) throws GTUException, ParameterException
140 {
141 Time now = perceivedGtu.getSimulator().getSimulatorTime();
142 if (this.updateTime == null || now.si > this.updateTime.si)
143 {
144 this.updateTime = now;
145 this.tr = perceivingGtu.getParameters().getParameter(ParameterTypes.TR);
146 Time whenTemp = now.minus(this.tr);
147 if (this.when == null || (this.when != null && whenTemp.si > this.when.si))
148 {
149
150 this.when = whenTemp;
151 }
152 this.traveledDistance = perceivingGtu.getOdometer().minus(perceivingGtu.getOdometer(this.when));
153 }
154 NeighborTriplet triplet = this.estimation.estimate(perceivingGtu, perceivedGtu, distance, downstream, this.when);
155 triplet = this.anticipation.anticipate(triplet, this.tr, this.traveledDistance);
156 return new HeadwayGTUPerceived(perceivedGtu, triplet.getHeadway(), triplet.getSpeed(), triplet.getAcceleration());
157 }
158
159
160 @Override
161 public HeadwayGTU createHeadwayGtu(final LaneBasedGTU perceivingGtu, final LaneBasedGTU perceivedGtu,
162 final Length overlapFront, final Length overlap, final Length overlapRear) throws GTUException
163 {
164 return new HeadwayGTUPerceived(perceivedGtu, overlapFront, overlap, overlapRear, perceivedGtu.getSpeed(),
165 perceivedGtu.getAcceleration());
166 }
167
168 }
169
170 }