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