1 package org.opentrafficsim.graphs;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.CAR;
5
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import org.djunits.unit.SpeedUnit;
14 import org.djunits.unit.TimeUnit;
15 import org.djunits.unit.UNITS;
16 import org.djunits.value.vdouble.scalar.Acceleration;
17 import org.djunits.value.vdouble.scalar.Duration;
18 import org.djunits.value.vdouble.scalar.Length;
19 import org.djunits.value.vdouble.scalar.Speed;
20 import org.djunits.value.vdouble.scalar.Time;
21 import org.jfree.data.DomainOrder;
22 import org.junit.Test;
23 import org.opentrafficsim.core.dsol.OTSDEVSSimulator;
24 import org.opentrafficsim.core.geometry.OTSPoint3D;
25 import org.opentrafficsim.core.gtu.GTUException;
26 import org.opentrafficsim.core.gtu.GTUType;
27 import org.opentrafficsim.core.network.LongitudinalDirectionality;
28 import org.opentrafficsim.core.network.OTSNetwork;
29 import org.opentrafficsim.core.network.OTSNode;
30 import org.opentrafficsim.road.car.CarTest;
31 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
32 import org.opentrafficsim.road.gtu.lane.tactical.following.FixedAccelerationModel;
33 import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
34 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
35 import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
36 import org.opentrafficsim.road.network.factory.LaneFactory;
37 import org.opentrafficsim.road.network.lane.Lane;
38 import org.opentrafficsim.road.network.lane.LaneType;
39
40
41
42
43
44
45
46
47
48
49 public class TrajectoryPlotTest implements UNITS
50 {
51
52 Duration sampleInterval = new Duration(0.25, SECOND);
53
54
55
56
57
58 @Test
59 public final void trajectoryTest() throws Exception
60 {
61 OTSDEVSSimulator simulator = CarTest.makeSimulator();
62 GTUType gtuType = CAR;
63 Set<GTUType> gtuTypes = new HashSet<GTUType>();
64 gtuTypes.add(gtuType);
65 LaneType laneType = new LaneType("CarLane", gtuTypes);
66 OTSNetwork network = new OTSNetwork("trajectory plot test network");
67 OTSNode node1 = new OTSNode(network, "node 1", new OTSPoint3D(100, 100, 0));
68 OTSNode node2 = new OTSNode(network, "node 2", new OTSPoint3D(1100, 100, 0));
69 OTSNode node3 = new OTSNode(network, "node 3", new OTSPoint3D(10100, 100, 0));
70 List<Lane> trajectory = new ArrayList<Lane>();
71 Speed speedLimit = new Speed(50, SpeedUnit.KM_PER_HOUR);
72 Lane lane1 = LaneFactory.makeMultiLane(network, "12", node1, node2, null, 1, 0, 0, laneType, speedLimit, simulator,
73 LongitudinalDirectionality.DIR_PLUS)[0];
74 trajectory.add(lane1);
75 Lane lane2 = LaneFactory.makeMultiLane(network, "23", node2, node3, null, 1, 0, 0, laneType, speedLimit, simulator,
76 LongitudinalDirectionality.DIR_PLUS)[0];
77 trajectory.add(lane2);
78 TrajectoryPlot tp = new TrajectoryPlot("TestTrajectory", this.sampleInterval, trajectory, simulator);
79 assertEquals("Number of trajectories should initially be 0", 0, tp.getSeriesCount());
80 for (int i = -10; i <= 10; i++)
81 {
82 assertEquals("SeriesKey(" + i + ") should return " + i, i, tp.getSeriesKey(i));
83 }
84 assertEquals("Domain order should be ASCENDING", DomainOrder.ASCENDING, tp.getDomainOrder());
85
86 Length initialPosition = new Length(200, METER);
87 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
88 Length length = new Length(5.0, METER);
89 Length width = new Length(2.0, METER);
90 Map<Lane, Length> initialLongitudinalPositions = new HashMap<>();
91 initialLongitudinalPositions.put(lane1, initialPosition);
92
93 simulator.runUpTo(new Time(100, TimeUnit.BASE_SECOND));
94 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
95 GTUFollowingModelOld gtuFollowingModel =
96 new FixedAccelerationModel(new Acceleration(0, METER_PER_SECOND_2), new Duration(10, SECOND));
97 LaneChangeModel laneChangeModel = new Egoistic();
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 }
202
203
204
205
206
207
208
209
210 private void verifyTrajectory(final LaneBasedIndividualGTU car, final int series, final TrajectoryPlot tp)
211 throws GTUException
212 {
213
214 Lane lane = car.positions(car.getFront()).keySet().iterator().next();
215 Time initialTime = car.getOperationalPlan().getStartTime();
216 Duration duration = car.getOperationalPlan().getTotalDuration();
217 int expectedNumberOfSamples = (int) (duration.getSI() / this.sampleInterval.getSI());
218 assertEquals("Number of samples in trajectory should be ", expectedNumberOfSamples, tp.getItemCount(series));
219
220 for (int sample = 0; sample < expectedNumberOfSamples; sample++)
221 {
222 Duration deltaTime = new Duration(this.sampleInterval.getSI() * sample, SECOND);
223 Time sampleTime = initialTime.plus(deltaTime);
224 double sampledTime = tp.getXValue(series, sample);
225 assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
226 sampledTime = tp.getX(series, sample).doubleValue();
227 assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
228 Length actualPosition = car.position(lane, car.getFront(), sampleTime);
229 double sampledPosition = tp.getYValue(series, sample);
230 assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition, 0.0001);
231 sampledPosition = tp.getY(series, sample).doubleValue();
232 assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition, 0.0001);
233 }
234 }
235
236 }