1 package org.opentrafficsim.graphs;
2
3 import static org.junit.Assert.assertEquals;
4
5 import org.djunits.unit.UNITS;
6 import org.djunits.value.vdouble.scalar.Duration;
7 import org.djunits.value.vdouble.scalar.Length;
8 import org.djunits.value.vdouble.scalar.Time;
9 import org.junit.Test;
10 import org.opentrafficsim.core.gtu.GTUException;
11 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
12 import org.opentrafficsim.road.network.lane.Lane;
13
14 /**
15 * <p>
16 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18 * <p>
19 * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
20 * initial version Aug 22, 2014 <br>
21 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
22 */
23 public class TrajectoryPlotTest implements UNITS
24 {
25 /** Sample interval for the TrajectoryPlot. */
26 Duration sampleInterval = new Duration(0.25, SECOND);
27
28 /**
29 * Test the TrajectoryPlot.
30 * @throws Exception which should not happen, but will be treated as an error by the JUnit framework if it does
31 */
32 @Test
33 public final void trajectoryTest() throws Exception
34 {
35 // Length minimumDistance = new Length(1234, METER);
36 // Length maximumDistance = new Length(12345, METER);
37
38 // TODO adapt to new path (List<Lane>) concept
39 /*-
40 TrajectoryPlot tp = new TrajectoryPlot("Trajectory", this.sampleInterval, minimumDistance, maximumDistance);
41 assertTrue("newly created DensityContourPlot should not be null", null != tp);
42 assertEquals("Number of trajectories should initially be 0", 0, tp.getSeriesCount());
43 for (int i = -10; i <= 10; i++)
44 {
45 assertEquals("SeriesKey(" + i + ") should return " + i, i, tp.getSeriesKey(i));
46 }
47 assertEquals("Domain order should be ASCENDING", DomainOrder.ASCENDING, tp.getDomainOrder());
48 // Create a car running 50 km.h
49 Length initialPosition = new Length(2000, METER);
50 Speed initialSpeed = new Speed(50, KM_PER_HOUR);
51 GTUType carType = new GTUType("Car");
52 Length length = new Length(5.0, METER);
53 Length width = new Length(2.0, METER);
54 Map<Lane, Length> initialLongitudinalPositions = new HashMap<>();
55 Lane lane = CarTest.makeLane();
56 initialLongitudinalPositions.put(lane, initialPosition);
57 OTSDEVSSimulator simulator = CarTest.makeSimulator();
58 // We want to start the car simulation at t=100s; therefore we have to advance the simulator up to that time.
59 simulateUntil(new Time(100, SECOND), simulator);
60 Speed maxSpeed = new Speed(120, KM_PER_HOUR);
61 Car car =
62 new Car(12345, carType, null, initialLongitudinalPositions, initialSpeed, length, width, maxSpeed,
63 simulator);
64 // Make the car accelerate with constant acceleration of 0.05 m/s/s for 400 seconds
65 Duration duration = new Duration(400, SECOND);
66 Time endTime = DoubleScalar.plus(simulator.getSimulatorTime().getTime(), duration);
67 car.setState(new GTUFollowingModelResult(new Acceleration(0.05,
68 METER_PER_SECOND_2), endTime));
69 // System.out.println("Car end position " + car.getPosition(car.getNextEvaluationTime()));
70 tp.addData(car);
71 assertEquals("Number of trajectories should now be 1", 1, tp.getSeriesCount());
72 verifyTrajectory(car, 0, tp);
73 simulateUntil(new Time(150, SECOND), simulator);
74 Car secondCar =
75 new Car(2, carType, null, initialLongitudinalPositions, initialSpeed, length, width, maxSpeed,
76 simulator);
77 // Make the second car accelerate with constant acceleration of 0.03 m/s/s for 500 seconds
78 secondCar.setState(new GTUFollowingModelResult(new Acceleration(0.03,
79 METER_PER_SECOND_2), endTime));
80 // System.out.println("Second car end position " + car.getPosition(secondCar.getNextEvaluationTime()));
81 tp.addData(secondCar);
82 assertEquals("Number of trajectories should now be 2", 2, tp.getSeriesCount());
83 verifyTrajectory(car, 0, tp); // first car trajectory should not change by adding the second
84 verifyTrajectory(secondCar, 1, tp);
85 // Check the updateHint method in the PointerHandler
86 // First get the panel that stores the result of updateHint (this is ugly)
87 JLabel hintPanel = null;
88 ChartPanel chartPanel = null;
89 for (Component c0 : tp.getComponents())
90 {
91 for (Component c1 : ((Container) c0).getComponents())
92 {
93 if (c1 instanceof Container)
94 {
95 for (Component c2 : ((Container) c1).getComponents())
96 {
97 // System.out.println("c2 is " + c2);
98 if (c2 instanceof Container)
99 {
100 for (Component c3 : ((Container) c2).getComponents())
101 {
102 // System.out.println("c3 is " + c3);
103 if (c3 instanceof JLabel)
104 {
105 if (null == hintPanel)
106 {
107 hintPanel = (JLabel) c3;
108 }
109 else
110 {
111 fail("There should be only one JPanel in a ContourPlot");
112 }
113 }
114 if (c3 instanceof ChartPanel)
115 {
116 if (null == chartPanel)
117 {
118 chartPanel = (ChartPanel) c3;
119 }
120 else
121 {
122 fail("There should be only one ChartPanel in a ContourPlot");
123 }
124 }
125 }
126 }
127 }
128 }
129 }
130 }
131 if (null == hintPanel)
132 {
133 fail("Could not find a JLabel in ContourPlot");
134 }
135 if (null == chartPanel)
136 {
137 fail("Could not find a ChartPanel in ContourPlot");
138 }
139 assertEquals("Initially the text should be a single space", " ", hintPanel.getText());
140 PointerHandler ph = null;
141 for (MouseListener ml : chartPanel.getMouseListeners())
142 {
143 if (ml instanceof PointerHandler)
144 {
145 if (null == ph)
146 {
147 ph = (PointerHandler) ml;
148 }
149 else
150 {
151 fail("There should be only one PointerHandler on the chartPanel");
152 }
153 }
154 }
155 if (null == ph)
156 {
157 fail("Could not find the PointerHandler for the chartPanel");
158 }
159 ph.updateHint(1, 2);
160 // System.out.println("Hint text is now " + hintPanel.getText());
161 assertFalse("Hint should not be a single space", " ".equals(hintPanel.getText()));
162 ph.updateHint(Double.NaN, Double.NaN);
163 assertEquals("The text should again be a single space", " ", hintPanel.getText());
164 */
165 }
166
167 /**
168 * Verify that a sampled trajectory matches the actual trajectory.
169 * @param car Car; the car whose trajectory was sampled
170 * @param series Integer; the series in the TrajectoryPlot that should correspond to the car
171 * @param tp TrajectoryPlot; the TrajectoryPlot that contains the samples
172 * @throws GTUException when car is not on lane anymore
173 */
174 private void verifyTrajectory(final LaneBasedIndividualGTU car, final int series, final TrajectoryPlot tp)
175 throws GTUException
176 {
177 // XXX we take the first (and only) lane on which the vehicle is registered.
178 Lane lane = car.positions(car.getFront()).keySet().iterator().next();
179 Time initialTime = car.getOperationalPlan().getStartTime();
180 Duration duration = car.getOperationalPlan().getTotalDuration();
181 int expectedNumberOfSamples = (int) (duration.getSI() / this.sampleInterval.getSI());
182 assertEquals("Number of samples in trajectory should be ", expectedNumberOfSamples, tp.getItemCount(series));
183 // Check that the stored trajectory accurately matches the trajectory of the car at all sampling times
184 for (int sample = 0; sample < expectedNumberOfSamples; sample++)
185 {
186 Duration deltaTime = new Duration(this.sampleInterval.getSI() * sample, SECOND);
187 Time sampleTime = initialTime.plus(deltaTime);
188 double sampledTime = tp.getXValue(series, sample);
189 assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
190 sampledTime = tp.getX(series, sample).doubleValue();
191 assertEquals("Sample should have been taken at " + sampleTime, sampleTime.getSI(), sampledTime, 0.0001);
192 Length actualPosition = car.position(lane, car.getFront(), sampleTime);
193 double sampledPosition = tp.getYValue(series, sample);
194 assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition,
195 0.0001);
196 sampledPosition = tp.getY(series, sample).doubleValue();
197 assertEquals("Sample position should have been " + actualPosition, actualPosition.getSI(), sampledPosition,
198 0.0001);
199 }
200 }
201
202 }